Surface NMR forward modelling
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ForwardFID.cpp 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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---\n"; // End of doc ---
  22. return stream;
  23. }
  24. // ==================== LIFECYCLE =======================
  25. //--------------------------------------------------------------------------------------
  26. // Class: ForwardFID
  27. // Method: ForwardFID
  28. // Description: constructor (locked)
  29. //--------------------------------------------------------------------------------------
  30. ForwardFID::ForwardFID (const ctor_key&) : MerlinObject( ) {
  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&) : MerlinObject(node) {
  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: ForwardModel
  91. //--------------------------------------------------------------------------------------
  92. std::shared_ptr<DataFID> ForwardFID::ForwardModel ( std::shared_ptr<LayeredEarthMR> Mod ) {
  93. MatrixXcr K0 = Kernel->GetKernel();
  94. int nq = K0.cols();
  95. int nt = WindowCentres.size();
  96. CalcQTMatrix( Mod->GetT2StarBins() );
  97. // Forward calculation is just a matrix vector product
  98. VectorXcr data = QT*Mod->GetModelVector();
  99. // TODO add noise
  100. // rearrange solution back into a matrix
  101. MatrixXcr B(Eigen::Map<MatrixXcr>(data.data(), nt, nq));
  102. //std::cout << B.imag().transpose() <<std::endl;
  103. auto FID = DataFID::NewSP();
  104. FID->FIDData = B;
  105. FID->WindowEdges = WindowEdges;
  106. FID->WindowCentres = WindowCentres;
  107. FID->PulseMoment = Kernel->GetPulseCurrent()*Kernel->GetPulseDuration();
  108. return FID;
  109. } // ----- end of method ForwardFID::ForwardModel -----
  110. //--------------------------------------------------------------------------------------
  111. // Class: ForwardFID
  112. // Method: LogSpaced
  113. //--------------------------------------------------------------------------------------
  114. void ForwardFID::SetLogSpacedWindows ( const Real& first, const Real& last,
  115. const int& n ) {
  116. WindowEdges = VectorXr::Zero(n);
  117. Real m = 1./(n-1.);
  118. Real quotient = std::pow(last/first, m);
  119. WindowEdges[0] = first;
  120. for (int i=1; i<n; ++i) {
  121. WindowEdges[i] = WindowEdges[i-1]*quotient;
  122. }
  123. WindowCentres = (WindowEdges.head(n-1) + WindowEdges.tail(n-1)) / 2;
  124. return;
  125. } // ----- end of method ForwardFID::LogSpaced -----
  126. //--------------------------------------------------------------------------------------
  127. // Class: ForwardFID
  128. // Method: SetKernel
  129. //--------------------------------------------------------------------------------------
  130. void ForwardFID::SetKernel ( std::shared_ptr< KernelV0 > K0 ) {
  131. Kernel = K0;
  132. return ;
  133. } // ----- end of method ForwardFID::SetKernel -----
  134. //--------------------------------------------------------------------------------------
  135. // Class: ForwardFID
  136. // Method: CalcQTMatrix
  137. //--------------------------------------------------------------------------------------
  138. void ForwardFID::CalcQTMatrix ( VectorXr T2Bins ) {
  139. MatrixXcr K0 = Kernel->GetKernel();
  140. VectorXcr K0r(Eigen::Map<VectorXcr>(K0.data(), K0.cols()*K0.rows()));
  141. // K0 = nq \times nlay
  142. // Qt = nq*nt \times nlay*nT2
  143. int nLay = K0.rows();
  144. int nq = K0.cols();
  145. int nt = WindowCentres.size();
  146. int nT2 = T2Bins.size();
  147. //std::cout << "# nLay " << nLay << std::endl;
  148. //std::cout << "# nq " << nq << std::endl;
  149. //std::cout << "# nt " << nt << std::endl;
  150. //std::cout << "# nT2 " << nT2 << std::endl;
  151. QT = MatrixXcr::Zero( nq*nt, nLay*nT2 );
  152. // std::cout << "K0 " << K0.rows() << "\t" << K0.cols() << std::endl;
  153. // std::cout << "QT " << QT.rows() << "\t" << QT.cols() << std::endl;
  154. // Ugly!
  155. int ir=0;
  156. for (int iq=0; iq<nq; ++iq) {
  157. for (int it=0; it<nt; ++it) {
  158. int ic=0;
  159. for (int ilay=0; ilay<nLay; ++ilay) {
  160. for (int it2=0; it2<nT2; ++it2) {
  161. QT(ir, ic) = K0(ilay, iq) * std::exp( -WindowCentres[it]/T2Bins[it2] );
  162. ++ic;
  163. }
  164. }
  165. ++ir;
  166. }
  167. }
  168. // std::cout << QT.imag() << std::endl;
  169. return ;
  170. } // ----- end of method ForwardFID::CalcQTMatrix -----
  171. } // ---- end of namespace Lemma ----
  172. /* vim: set tabstop=4 expandtab: */
  173. /* vim: set filetype=cpp: */