Surface NMR forward modelling
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ForwardFID.cpp 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* This file is part of Lemma, a geophysical modelling and inversion API.
  2. * More information is available at http://lemmasoftware.org
  3. */
  4. /* This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file
  10. * @date 08/28/2017 09:03:03 AM
  11. * @version $Id$
  12. * @author Trevor Irons (ti)
  13. * @email tirons@egi.utah.edu
  14. * @copyright Copyright (c) 2017, University of Utah
  15. * @copyright Copyright (c) 2017, Lemma Software, LLC
  16. */
  17. #include "ForwardFID.h"
  18. namespace Lemma {
  19. // ==================== FRIEND METHODS =====================
  20. std::ostream &operator << (std::ostream &stream, const ForwardFID &ob) {
  21. stream << ob.Serialize() << "\n";
  22. return stream;
  23. }
  24. // ==================== LIFECYCLE =======================
  25. //--------------------------------------------------------------------------------------
  26. // Class: ForwardFID
  27. // Method: ForwardFID
  28. // Description: constructor (locked)
  29. //--------------------------------------------------------------------------------------
  30. ForwardFID::ForwardFID (const ctor_key& key) : MerlinObject( key ) {
  31. } // ----- end of method ForwardFID::ForwardFID (constructor) -----
  32. //--------------------------------------------------------------------------------------
  33. // Class: ForwardFID
  34. // Method: ForwardFID
  35. // Description: DeSerializing constructor (locked)
  36. //--------------------------------------------------------------------------------------
  37. ForwardFID::ForwardFID (const YAML::Node& node, const ctor_key& key) : MerlinObject(node, key) {
  38. } // ----- end of method ForwardFID::ForwardFID (constructor) -----
  39. //--------------------------------------------------------------------------------------
  40. // Class: ForwardFID
  41. // Method: NewSP()
  42. // Description: public constructor returing a shared_ptr
  43. //--------------------------------------------------------------------------------------
  44. std::shared_ptr< ForwardFID > ForwardFID::NewSP() {
  45. return std::make_shared< ForwardFID >( ctor_key() );
  46. }
  47. //--------------------------------------------------------------------------------------
  48. // Class: ForwardFID
  49. // Method: ~ForwardFID
  50. // Description: destructor (protected)
  51. //--------------------------------------------------------------------------------------
  52. ForwardFID::~ForwardFID () {
  53. } // ----- end of method ForwardFID::~ForwardFID (destructor) -----
  54. //--------------------------------------------------------------------------------------
  55. // Class: ForwardFID
  56. // Method: Serialize
  57. //--------------------------------------------------------------------------------------
  58. YAML::Node ForwardFID::Serialize ( ) const {
  59. YAML::Node node = MerlinObject::Serialize();
  60. node.SetTag( GetName() );
  61. // FILL IN CLASS SPECIFICS HERE
  62. if (Kernel != nullptr) {
  63. node["Kernel"] = Kernel->Serialize();
  64. }
  65. node["WindowEdges"] = WindowEdges;
  66. node["WindowCentres"] = WindowCentres;
  67. node["RDP"] = RDP;
  68. return node;
  69. } // ----- end of method ForwardFID::Serialize -----
  70. //--------------------------------------------------------------------------------------
  71. // Class: ForwardFID
  72. // Method: DeSerialize
  73. //--------------------------------------------------------------------------------------
  74. std::shared_ptr<ForwardFID> ForwardFID::DeSerialize ( const YAML::Node& node ) {
  75. if (node.Tag() != "ForwardFID" ) {
  76. throw DeSerializeTypeMismatch( "ForwardFID", node.Tag());
  77. }
  78. return std::make_shared< ForwardFID > ( node, ctor_key() );
  79. } // ----- end of method ForwardFID::DeSerialize -----
  80. //--------------------------------------------------------------------------------------
  81. // Class: ForwardFID
  82. // Method: SetWindowEdges
  83. //--------------------------------------------------------------------------------------
  84. void ForwardFID::SetWindowEdges ( const VectorXr& Edges ) {
  85. WindowEdges = Edges;
  86. return ;
  87. } // ----- end of method ForwardFID::SetWindowEdges -----
  88. //--------------------------------------------------------------------------------------
  89. // Class: ForwardFID
  90. // Method: SetNoiseFloor
  91. //--------------------------------------------------------------------------------------
  92. void ForwardFID::SetNoiseFloor ( const Real& floor ) {
  93. assert (floor > 0.);
  94. NoiseFloor = floor;
  95. return ;
  96. } // ----- end of method ForwardFID::SetNoiseFloor -----
  97. //--------------------------------------------------------------------------------------
  98. // Class: ForwardFID
  99. // Method: ForwardModel
  100. //--------------------------------------------------------------------------------------
  101. std::shared_ptr<DataFID> ForwardFID::ForwardModel ( std::shared_ptr<LayeredEarthMR> Mod ) {
  102. MatrixXcr K0 = Kernel->GetKernel();
  103. int nq = K0.cols();
  104. int nt = WindowCentres.size();
  105. CalcQTMatrix( Mod->GetT2StarBins() );
  106. // Forward calculation is just a matrix vector product
  107. VectorXcr data = QT*Mod->GetModelVector();
  108. // rearrange solution back into a matrix
  109. MatrixXcr B(Eigen::Map<MatrixXcr>(data.data(), nt, nq)); // Complex Data
  110. MatrixXr N = MatrixXr::Zero(nt, nq); // Noise
  111. B.real() = B.array().abs();
  112. B.imag() *= 0.;
  113. // TODO add noise
  114. if (NoiseFloor > 1e-5) {
  115. std::random_device rd;
  116. std::mt19937 gen(rd());
  117. std::normal_distribution<> d(0,1e-9*NoiseFloor);
  118. for (int iq=0; iq<nq; ++iq) {
  119. for (int it=0; it<nt; ++it) {
  120. B(it, iq) += Complex(d(gen), d(gen)) /
  121. std::sqrt( WindowEdges(it+1)-WindowEdges(it) );
  122. N(it,iq) = (1e-9*NoiseFloor) / std::sqrt(WindowEdges(it+1)-WindowEdges(it));
  123. }
  124. }
  125. }
  126. //std::cout << B.imag().transpose() <<std::endl;
  127. auto FID = DataFID::NewSP();
  128. FID->FIDData = B;
  129. FID->NoiseEstimate = N;
  130. FID->Qt = QT;
  131. FID->TrueModel = Mod->GetModelVector();
  132. FID->WindowEdges = WindowEdges;
  133. FID->WindowCentres = WindowCentres;
  134. FID->PulseMoment = Kernel->GetPulseCurrent()*Kernel->GetPulseDuration();
  135. return FID;
  136. } // ----- end of method ForwardFID::ForwardModel -----
  137. //--------------------------------------------------------------------------------------
  138. // Class: ForwardFID
  139. // Method: LogSpaced
  140. //--------------------------------------------------------------------------------------
  141. void ForwardFID::SetLogSpacedWindows ( const Real& first, const Real& last,
  142. const int& n ) {
  143. WindowEdges = VectorXr::Zero(n);
  144. Real m = 1./(n-1.);
  145. Real quotient = std::pow(last/first, m);
  146. WindowEdges[0] = first;
  147. for (int i=1; i<n; ++i) {
  148. WindowEdges[i] = WindowEdges[i-1]*quotient;
  149. }
  150. WindowCentres = (WindowEdges.head(n-1) + WindowEdges.tail(n-1)) / 2;
  151. return;
  152. } // ----- end of method ForwardFID::LogSpaced -----
  153. //--------------------------------------------------------------------------------------
  154. // Class: ForwardFID
  155. // Method: SetKernel
  156. //--------------------------------------------------------------------------------------
  157. void ForwardFID::SetKernel ( std::shared_ptr< KernelV0 > K0 ) {
  158. Kernel = K0;
  159. return ;
  160. } // ----- end of method ForwardFID::SetKernel -----
  161. //--------------------------------------------------------------------------------------
  162. // Class: ForwardFID
  163. // Method: CalcQTMatrix
  164. //--------------------------------------------------------------------------------------
  165. void ForwardFID::CalcQTMatrix ( VectorXr T2Bins ) {
  166. MatrixXcr K0 = Kernel->GetKernel();
  167. VectorXcr K0r(Eigen::Map<VectorXcr>(K0.data(), K0.cols()*K0.rows()));
  168. // K0 = nq \times nlay
  169. // Qt = nq*nt \times nlay*nT2
  170. int nLay = K0.rows();
  171. int nq = K0.cols();
  172. int nt = WindowCentres.size();
  173. int nT2 = T2Bins.size();
  174. //std::cout << "# nLay " << nLay << std::endl;
  175. //std::cout << "# nq " << nq << std::endl;
  176. //std::cout << "# nt " << nt << std::endl;
  177. //std::cout << "# nT2 " << nT2 << std::endl;
  178. QT = MatrixXcr::Zero( nq*nt, nLay*nT2 );
  179. // std::cout << "K0 " << K0.rows() << "\t" << K0.cols() << std::endl;
  180. // std::cout << "QT " << QT.rows() << "\t" << QT.cols() << std::endl;
  181. // Ugly!
  182. int ir=0;
  183. for (int iq=0; iq<nq; ++iq) {
  184. for (int it=0; it<nt; ++it) {
  185. int ic=0;
  186. for (int ilay=0; ilay<nLay; ++ilay) {
  187. for (int it2=0; it2<nT2; ++it2) {
  188. QT(ir, ic) = K0(ilay, iq) * std::exp( -WindowCentres[it]/T2Bins[it2] );
  189. ++ic;
  190. }
  191. }
  192. ++ir;
  193. }
  194. }
  195. // std::cout << QT.imag() << std::endl;
  196. return ;
  197. } // ----- end of method ForwardFID::CalcQTMatrix -----
  198. } // ---- end of namespace Lemma ----
  199. /* vim: set tabstop=4 expandtab: */
  200. /* vim: set filetype=cpp: */