瀏覽代碼

Forward modelling of FID data is possible, but final format of files not nailed down.

master
T-bone 7 年之前
父節點
當前提交
d2072d1b78
共有 10 個檔案被更改,包括 202 行新增15 行删除
  1. 3
    0
      examples/CMakeLists.txt
  2. 9
    11
      examples/ForwardFID.cpp
  3. 42
    0
      examples/ModelAligner.cpp
  4. 11
    0
      include/DataFID.h
  5. 16
    2
      include/ForwardFID.h
  6. 13
    0
      include/LayeredEarthMR.h
  7. 5
    0
      src/DataFID.cpp
  8. 67
    2
      src/ForwardFID.cpp
  9. 7
    0
      src/KernelV0.cpp
  10. 29
    0
      src/LayeredEarthMR.cpp

+ 3
- 0
examples/CMakeLists.txt 查看文件

@@ -7,6 +7,9 @@ target_link_libraries(  KernelV0-2  "lemmacore" "fdem1d" "merlin")
7 7
 add_executable( KV0-3loops KV0-3loops.cpp  )
8 8
 target_link_libraries(  KV0-3loops  "lemmacore" "fdem1d" "merlin")
9 9
 
10
+add_executable( ModelAligner ModelAligner.cpp ) 
11
+target_link_libraries(  ModelAligner  "lemmacore" "fdem1d" "merlin")
12
+
10 13
 add_executable( ForwardFID ForwardFID.cpp  )
11 14
 target_link_libraries(  ForwardFID  "lemmacore" "fdem1d" "merlin")
12 15
 

+ 9
- 11
examples/ForwardFID.cpp 查看文件

@@ -22,24 +22,22 @@ using namespace Lemma;
22 22
 
23 23
 int main(int argc, char** argv) {
24 24
 
25
-    if (argc<5) {
26
-        std::cout << "./ForwardFID Kernel.yaml TxString RxString  vtkoutput<true/false> \n";
27
-    //    return(EXIT_FAILURE);
25
+    if (argc<3) {
26
+        std::cout << "./ForwardFID Kernel.yaml Model.yaml  \n";
27
+        return(EXIT_FAILURE);
28 28
     }
29 29
     auto Kernel = KernelV0::DeSerialize(YAML::LoadFile(argv[1]));
30 30
     //    std::cout << *Kernel;
31 31
 
32
-    auto Model = LayeredEarthMR::NewSP();
33
-        Model->AlignWithKernel(Kernel);
34
-        Model->SetT2StarBins(10, 500, 20);
35
-        std::cout << *Model << std::endl;
32
+    auto Model = LayeredEarthMR::DeSerialize(YAML::LoadFile(argv[2]));
33
+    //    std::cout << *Model << std::endl;
36 34
 
37
-/*
38 35
     auto Forward = ForwardFID::NewSP();
39 36
         //Forward->SetWindowEdges( VectorXr::LinSpaced(10,0,1) );
40 37
         Forward->SetLogSpacedWindows(10,1000,30);
41
-        //auto FID =  Forward->ForwardModel(Model);
42
-    //std::cout << *Forward << std::endl;
43
-*/
38
+        Forward->SetKernel(Kernel);
39
+        auto FID = Forward->ForwardModel(Model);
40
+    std::cout << *FID << std::endl;
41
+
44 42
     return EXIT_SUCCESS;
45 43
 }

+ 42
- 0
examples/ModelAligner.cpp 查看文件

@@ -0,0 +1,42 @@
1
+/* This file is part of Lemma, a geophysical modelling and inversion API.
2
+ * More information is available at http://lemmasoftware.org
3
+ */
4
+
5
+/* This Source Code Form is subject to the terms of the Mozilla Public
6
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
7
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
+ */
9
+
10
+/**
11
+ * @file
12
+ * @date      08/30/2017 04:08:53 AM
13
+ * @version   $Id$
14
+ * @author    Trevor Irons (ti)
15
+ * @email     tirons@egi.utah.edu
16
+ * @copyright Copyright (c) 2017, University of Utah
17
+ * @copyright Copyright (c) 2017, Trevor Irons & Lemma Software, LLC
18
+ */
19
+
20
+#include <Merlin>
21
+using namespace Lemma;
22
+
23
+int main(int argc, char** argv) {
24
+
25
+    if (argc<5) {
26
+        std::cout << "ModelAligner aligns a dummy model with a pre-calculated"
27
+                  << "imaging kernel.\n\n"
28
+                  << "./ModelAligner Kernel.yaml T2Low T2High nT2  \n";
29
+
30
+        return(EXIT_FAILURE);
31
+    }
32
+
33
+    auto Kernel = KernelV0::DeSerialize(YAML::LoadFile(argv[1]));
34
+
35
+    auto Model = LayeredEarthMR::NewSP();
36
+        Model->AlignWithKernel(Kernel);
37
+        Model->SetT2StarBins(10, 500, 20);
38
+
39
+    std::cout << *Model << std::endl;
40
+}
41
+
42
+

+ 11
- 0
include/DataFID.h 查看文件

@@ -36,6 +36,8 @@ namespace Lemma {
36 36
 
37 37
         friend std::ostream &operator<<(std::ostream &stream, const DataFID &ob);
38 38
 
39
+        friend class ForwardFID;
40
+
39 41
         protected:
40 42
         /*
41 43
          *  This key is used to lock the constructor. It is protected so that inhereted
@@ -124,6 +126,15 @@ namespace Lemma {
124 126
         /** ASCII string representation of the class name */
125 127
         static constexpr auto CName = "DataFID";
126 128
 
129
+        /** Holds the actual data */
130
+        MatrixXcr   FIDData;
131
+
132
+        VectorXr    WindowCentres;
133
+
134
+        VectorXr    WindowEdges;
135
+
136
+        VectorXr    PulseMoment;
137
+
127 138
     }; // -----  end of class  DataFID  -----
128 139
 }  // -----  end of namespace Lemma ----
129 140
 

+ 16
- 2
include/ForwardFID.h 查看文件

@@ -24,9 +24,11 @@
24 24
 #include "LayeredEarthEM.h"
25 25
 #include "PolygonalWireAntenna.h"
26 26
 #include "EMEarth1D.h"
27
+
27 28
 #include "MerlinObject.h"
28 29
 #include "DataFID.h"
29 30
 #include "KernelV0.h"
31
+#include "LayeredEarthMR.h"
30 32
 
31 33
 namespace Lemma {
32 34
 
@@ -107,7 +109,7 @@ namespace Lemma {
107 109
          *  Performs forward model calculation based on input parameters
108 110
          *  @return Merlin class representing data
109 111
          */
110
-        std::shared_ptr<DataFID> ForwardModel();
112
+        std::shared_ptr<DataFID> ForwardModel( std::shared_ptr<LayeredEarthMR> );
111 113
 
112 114
         // ====================  ACCESS        =======================
113 115
         /**
@@ -125,6 +127,11 @@ namespace Lemma {
125 127
          */
126 128
         void SetLogSpacedWindows(const Real& first, const Real& last, const int& n);
127 129
 
130
+        /**
131
+         *   @param[in] K0 is the initial amplitude imaging kernel
132
+         */
133
+        void SetKernel( std::shared_ptr< KernelV0 > K0 );
134
+
128 135
         // ====================  INQUIRY       =======================
129 136
         /**
130 137
          *  Returns the name of the underlying class, similiar to Python's type
@@ -144,7 +151,11 @@ namespace Lemma {
144 151
         // ====================  DATA MEMBERS  =========================
145 152
         private:
146 153
 
147
-        void CalcQTMatrix(  );
154
+        /**
155
+         * Calculates the QT matrix
156
+         * @param[in] T2StarBins are the T2* bins to use
157
+         */
158
+        void CalcQTMatrix( VectorXr T2StarBins );
148 159
 
149 160
         /** ASCII string representation of the class name */
150 161
         static constexpr auto CName = "ForwardFID";
@@ -158,6 +169,9 @@ namespace Lemma {
158 169
         /** Time gate centres */
159 170
         VectorXr WindowCentres;
160 171
 
172
+        /** QT matrix */
173
+        MatrixXcr QT;
174
+
161 175
         /** Include RDP effects? */
162 176
         bool RDP = false;
163 177
 

+ 13
- 0
include/LayeredEarthMR.h 查看文件

@@ -102,7 +102,20 @@ namespace Lemma {
102 102
 
103 103
         // ====================  ACCESS        =======================
104 104
 
105
+        /**
106
+         * @return a VectorXr of the T2* bins
107
+         */
108
+        VectorXr GetT2StarBins();
105 109
 
110
+        /**
111
+         * @return a flattened version of the model matrix
112
+         */
113
+        VectorXr GetModelVector();
114
+
115
+        /**
116
+         * @return the model matrix
117
+         */
118
+        MatrixXr GetModelMatrix();
106 119
 
107 120
         /**
108 121
          *  Sets the T2StarBins to solve for, these are log spaced

+ 5
- 0
src/DataFID.cpp 查看文件

@@ -75,6 +75,11 @@ namespace Lemma {
75 75
         YAML::Node node = MerlinObject::Serialize();
76 76
         node.SetTag( GetName() );
77 77
         // FILL IN CLASS SPECIFICS HERE
78
+        node["FID_REAL"] = FIDData.real().eval();
79
+        node["FID_IMAG"] = FIDData.imag().eval();
80
+        node["WindowCentres"] = WindowCentres;
81
+        node["WindowEdges"] =  WindowEdges;
82
+        node["PulseMoment"] = PulseMoment;
78 83
         return node;
79 84
     }		// -----  end of method DataFID::Serialize  -----
80 85
 

+ 67
- 2
src/ForwardFID.cpp 查看文件

@@ -109,8 +109,28 @@ namespace Lemma {
109 109
     //       Class:  ForwardFID
110 110
     //      Method:  ForwardModel
111 111
     //--------------------------------------------------------------------------------------
112
-    std::shared_ptr<DataFID> ForwardFID::ForwardModel (  ) {
112
+    std::shared_ptr<DataFID> ForwardFID::ForwardModel ( std::shared_ptr<LayeredEarthMR> Mod ) {
113
+
114
+        MatrixXcr K0 = Kernel->GetKernel();
115
+        int nq = K0.cols();
116
+        int nt = WindowCentres.size();
117
+
118
+        CalcQTMatrix( Mod->GetT2StarBins() );
119
+
120
+        // Forward calculation is just a matrix vector product
121
+        VectorXcr data = QT*Mod->GetModelVector();
122
+
123
+        // TODO add noise
124
+
125
+        // rearrange solution back into a matrix
126
+        MatrixXcr B(Eigen::Map<MatrixXcr>(data.data(), nt, nq));
127
+        //std::cout << B.imag().transpose() <<std::endl;
113 128
         auto FID = DataFID::NewSP();
129
+
130
+        FID->FIDData = B;
131
+        FID->WindowEdges = WindowEdges;
132
+        FID->WindowCentres = WindowCentres;
133
+        FID->PulseMoment = Kernel->GetPulseCurrent()*Kernel->GetPulseDuration();
114 134
         return FID;
115 135
     }		// -----  end of method ForwardFID::ForwardModel  -----
116 136
 
@@ -131,11 +151,56 @@ namespace Lemma {
131 151
         return;
132 152
     }		// -----  end of method ForwardFID::LogSpaced  -----
133 153
 
154
+
155
+    //--------------------------------------------------------------------------------------
156
+    //       Class:  ForwardFID
157
+    //      Method:  SetKernel
158
+    //--------------------------------------------------------------------------------------
159
+    void ForwardFID::SetKernel ( std::shared_ptr< KernelV0 > K0 ) {
160
+        Kernel = K0;
161
+        return ;
162
+    }		// -----  end of method ForwardFID::SetKernel  -----
163
+
164
+
134 165
     //--------------------------------------------------------------------------------------
135 166
     //       Class:  ForwardFID
136 167
     //      Method:  CalcQTMatrix
137 168
     //--------------------------------------------------------------------------------------
138
-    void ForwardFID::CalcQTMatrix (  ) {
169
+    void ForwardFID::CalcQTMatrix ( VectorXr T2Bins ) {
170
+        MatrixXcr K0 = Kernel->GetKernel();
171
+        VectorXcr K0r(Eigen::Map<VectorXcr>(K0.data(), K0.cols()*K0.rows()));
172
+
173
+        // K0 = nq     \times nlay
174
+        // Qt = nq*nt  \times nlay*nT2
175
+
176
+        int nLay = K0.rows();
177
+        int nq = K0.cols();
178
+        int nt = WindowCentres.size();
179
+        int nT2 = T2Bins.size();
180
+        //std::cout << "# nLay " << nLay << std::endl;
181
+        //std::cout << "# nq " << nq << std::endl;
182
+        //std::cout << "# nt " << nt << std::endl;
183
+        //std::cout << "# nT2 " << nT2 << std::endl;
184
+
185
+        QT = MatrixXcr::Zero( nq*nt, nLay*nT2 );
186
+//        std::cout << "K0 " << K0.rows() << "\t" << K0.cols() << std::endl;
187
+//        std::cout << "QT " << QT.rows() << "\t" << QT.cols() << std::endl;
188
+
189
+        // Ugly!
190
+        int ir=0;
191
+        for (int iq=0; iq<nq; ++iq) {
192
+            for (int it=0; it<nt; ++it) {
193
+                int ic=0;
194
+                for (int ilay=0; ilay<nLay; ++ilay) {
195
+                    for (int it2=0; it2<nT2; ++it2) {
196
+                        QT(ir, ic) = K0(ilay, iq) * std::exp( -WindowCentres[it]/T2Bins[it2] );
197
+                        ++ic;
198
+                    }
199
+                }
200
+                ++ir;
201
+            }
202
+        }
203
+//        std::cout << QT.imag() << std::endl;
139 204
         return ;
140 205
     }		// -----  end of method ForwardFID::CalcQTMatrix  -----
141 206
 

+ 7
- 0
src/KernelV0.cpp 查看文件

@@ -85,6 +85,12 @@ namespace Lemma {
85 85
             }
86 86
         }
87 87
 
88
+        if (node["K0"]) {
89
+            Kern = MatrixXcr::Zero( Interfaces.size()-1, PulseI.size()  ).array() + 1.;
90
+            for ( int ilay=0; ilay<Interfaces.size()-1; ++ilay ) {
91
+                Kern.row(ilay) = node["K0"]["layer-" + to_string(ilay) ].as<VectorXcr>();
92
+            }
93
+        }
88 94
     }  // -----  end of method KernelV0::KernelV0  (constructor)  -----
89 95
 
90 96
     //--------------------------------------------------------------------------------------
@@ -137,6 +143,7 @@ namespace Lemma {
137 143
         node["IntegrationSize"] = Size;
138 144
         node["IntegrationOrigin"] = Origin;
139 145
 
146
+        // TODO, use better matrix encapulation
140 147
         if (Kern.array().abs().any() > 1e-16) {
141 148
             for ( int ilay=0; ilay<Interfaces.size()-1; ++ilay ) {
142 149
                 node["K0"]["layer-" + to_string(ilay) ] = static_cast<VectorXcr>(Kern.row(ilay));

+ 29
- 0
src/LayeredEarthMR.cpp 查看文件

@@ -146,6 +146,35 @@ namespace Lemma {
146 146
         return ;
147 147
     }		// -----  end of method LayeredEarthMR::InitModelMat  -----
148 148
 
149
+
150
+    //--------------------------------------------------------------------------------------
151
+    //       Class:  LayeredEarthMR
152
+    //      Method:  GetT2StarBins
153
+    //--------------------------------------------------------------------------------------
154
+    VectorXr LayeredEarthMR::GetT2StarBins (  ) {
155
+        return T2StarBins ;
156
+    }		// -----  end of method LayeredEarthMR::GetT2StarBins  -----
157
+
158
+
159
+    //--------------------------------------------------------------------------------------
160
+    //       Class:  LayeredEarthMR
161
+    //      Method:  GetModelMatrix
162
+    //--------------------------------------------------------------------------------------
163
+    MatrixXr LayeredEarthMR::GetModelMatrix (  ) {
164
+        return ModelMat ;
165
+    }		// -----  end of method LayeredEarthMR::GetModelMatrix  -----
166
+
167
+
168
+    //--------------------------------------------------------------------------------------
169
+    //       Class:  LayeredEarthMR
170
+    //      Method:  GetModelVector
171
+    //--------------------------------------------------------------------------------------
172
+    VectorXr LayeredEarthMR::GetModelVector (  ) {
173
+        VectorXr B(Eigen::Map<VectorXr>(ModelMat.data(), ModelMat.cols()*ModelMat.rows()));
174
+        return B;
175
+    }		// -----  end of method LayeredEarthMR::GetModelVector  -----
176
+
177
+
149 178
 } // ----  end of namespace Lemma  ----
150 179
 
151 180
 /* vim: set tabstop=4 expandtab: */

Loading…
取消
儲存