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.

Coupling.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 11/11/2016 01:47:25 PM
  11. * @author Trevor Irons (ti)
  12. * @email tirons@egi.utah.edu
  13. * @copyright Copyright (c) 2016, University of Utah
  14. * @copyright Copyright (c) 2016, Lemma Software, LLC
  15. * @copyright Copyright (c) 2008, Colorado School of Mines
  16. */
  17. #include "Coupling.h"
  18. #include "FieldPoints.h"
  19. namespace Lemma {
  20. // ==================== FRIEND METHODS =====================
  21. std::ostream &operator << (std::ostream &stream, const Coupling &ob) {
  22. stream << ob.Serialize() << "\n---\n"; // End of doc ---
  23. return stream;
  24. }
  25. // ==================== LIFECYCLE =======================
  26. //--------------------------------------------------------------------------------------
  27. // Class: Coupling
  28. // Method: Coupling
  29. // Description: constructor (locked)
  30. //--------------------------------------------------------------------------------------
  31. Coupling::Coupling (const ctor_key&) : LemmaObject( ) {
  32. } // ----- end of method Coupling::Coupling (constructor) -----
  33. //--------------------------------------------------------------------------------------
  34. // Class: Coupling
  35. // Method: Coupling
  36. // Description: DeSerializing constructor (locked)
  37. //--------------------------------------------------------------------------------------
  38. Coupling::Coupling (const YAML::Node& node, const ctor_key&) : LemmaObject(node) {
  39. } // ----- end of method Coupling::Coupling (constructor) -----
  40. //--------------------------------------------------------------------------------------
  41. // Class: Coupling
  42. // Method: NewSP()
  43. // Description: public constructor returing a shared_ptr
  44. //--------------------------------------------------------------------------------------
  45. std::shared_ptr< Coupling > Coupling::NewSP() {
  46. return std::make_shared< Coupling >( ctor_key() );
  47. }
  48. //--------------------------------------------------------------------------------------
  49. // Class: Coupling
  50. // Method: ~Coupling
  51. // Description: destructor (protected)
  52. //--------------------------------------------------------------------------------------
  53. Coupling::~Coupling () {
  54. } // ----- end of method Coupling::~Coupling (destructor) -----
  55. //--------------------------------------------------------------------------------------
  56. // Class: Coupling
  57. // Method: Serialize
  58. //--------------------------------------------------------------------------------------
  59. YAML::Node Coupling::Serialize ( ) const {
  60. YAML::Node node = LemmaObject::Serialize();
  61. node.SetTag( GetName() );
  62. // Coils Transmitters & Receivers
  63. for ( auto txm : TxRx) {
  64. node[txm.first] = txm.second->Serialize();
  65. }
  66. // LayeredEarthEM
  67. node["SigmaModel"] = SigmaModel->Serialize();
  68. node["tol"] = tol;
  69. node["minLevel"] = minLevel;
  70. node["maxLevel"] = maxLevel;
  71. return node;
  72. } // ----- end of method Coupling::Serialize -----
  73. //--------------------------------------------------------------------------------------
  74. // Class: Coupling
  75. // Method: DeSerialize
  76. //--------------------------------------------------------------------------------------
  77. std::shared_ptr<Coupling> Coupling::DeSerialize ( const YAML::Node& node ) {
  78. if (node.Tag() != "Coupling" ) {
  79. throw DeSerializeTypeMismatch( "Coupling", node.Tag());
  80. }
  81. return std::make_shared< Coupling > ( node, ctor_key() );
  82. } // ----- end of method Coupling::DeSerialize -----
  83. //--------------------------------------------------------------------------------------
  84. // Class: Coupling
  85. // Method: DeSerialize
  86. //--------------------------------------------------------------------------------------
  87. Complex Coupling::Calculate (const std::vector< std::string>& Tx, const std::vector<std::string >& Rx,
  88. bool vtkOutput ) {
  89. // All EM calculations will share same field points
  90. cpoints = FieldPoints::NewSP();
  91. cpoints->SetNumberOfPoints(8);
  92. for (auto tx : Tx) {
  93. // Set up EMEarth
  94. EMEarths[tx] = EMEarth1D::NewSP();
  95. EMEarths[tx]->AttachWireAntenna(TxRx[tx]);
  96. EMEarths[tx]->AttachLayeredEarthEM(SigmaModel);
  97. EMEarths[tx]->AttachFieldPoints( cpoints );
  98. EMEarths[tx]->SetFieldsToCalculate(H);
  99. // TODO query for method, altough with flat antennae, this is fastest
  100. EMEarths[tx]->SetHankelTransformMethod(ANDERSON801);
  101. EMEarths[tx]->SetTxRxMode(TX);
  102. TxRx[tx]->SetCurrent(1.);
  103. }
  104. for (auto rx : Rx) {
  105. if (EMEarths.count(rx)) {
  106. EMEarths[rx]->SetTxRxMode(TXRX);
  107. } else {
  108. EMEarths[rx] = EMEarth1D::NewSP();
  109. EMEarths[rx]->AttachWireAntenna(TxRx[rx]);
  110. EMEarths[rx]->AttachLayeredEarthEM(SigmaModel);
  111. EMEarths[rx]->AttachFieldPoints( cpoints );
  112. EMEarths[rx]->SetFieldsToCalculate(H);
  113. // TODO query for method, altough with flat antennae, this is fastest
  114. EMEarths[rx]->SetHankelTransformMethod(ANDERSON801);
  115. EMEarths[rx]->SetTxRxMode(RX);
  116. TxRx[rx]->SetCurrent(1.);
  117. }
  118. }
  119. SUM = 0;
  120. IntegrateOnOctreeGrid( vtkOutput );
  121. std::cout << "\nFinished KERNEL\n";
  122. return SUM;
  123. }
  124. //--------------------------------------------------------------------------------------
  125. // Class: Coupling
  126. // Method: IntegrateOnOctreeGrid
  127. //--------------------------------------------------------------------------------------
  128. void Coupling::IntegrateOnOctreeGrid( bool vtkOutput) {
  129. Vector3r cpos = Origin + Size/2.;
  130. VOLSUM = 0;
  131. nleaves = 0;
  132. if (!vtkOutput) {
  133. EvaluateKids( Size, 0, cpos, Complex(100.));
  134. } else {
  135. #ifdef LEMMAUSEVTK
  136. vtkHyperOctree* oct = vtkHyperOctree::New();
  137. oct->SetDimension(3);
  138. oct->SetOrigin( Origin(0), Origin(1), Origin(2) );
  139. oct->SetSize( Size(0), Size(1), Size(2) );
  140. vtkHyperOctreeCursor* curse = oct->NewCellCursor();
  141. curse->ToRoot();
  142. EvaluateKids2( Size, 0, cpos, VectorXcr::Ones(PulseI.size()), oct, curse );
  143. for (int iq=0; iq<PulseI.size(); ++iq) {
  144. // Fill in leaf data
  145. vtkDoubleArray* kr = vtkDoubleArray::New();
  146. kr->SetNumberOfComponents(1);
  147. kr->SetName("Re($K_0$)");
  148. kr->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  149. vtkDoubleArray* ki = vtkDoubleArray::New();
  150. ki->SetNumberOfComponents(1);
  151. ki->SetName("Im($K_0$)");
  152. ki->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  153. vtkDoubleArray* km = vtkDoubleArray::New();
  154. km->SetNumberOfComponents(1);
  155. km->SetName("mod($K_0$)");
  156. km->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  157. vtkIntArray* kid = vtkIntArray::New();
  158. kid->SetNumberOfComponents(1);
  159. kid->SetName("ID");
  160. kid->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  161. vtkIntArray* kerr = vtkIntArray::New();
  162. kerr->SetNumberOfComponents(1);
  163. kerr->SetName("nleaf");
  164. //Real LeafVol(0);
  165. for (auto leaf : LeafDict) {
  166. kr->InsertTuple1( leaf.first, std::real(leaf.second(iq)) );
  167. ki->InsertTuple1( leaf.first, std::imag(leaf.second(iq)) );
  168. km->InsertTuple1( leaf.first, std::abs(leaf.second(iq)) );
  169. kid->InsertTuple1( leaf.first, leaf.first );
  170. //LeafVol += std::real(leaf.second);
  171. }
  172. //std::cout << "\n\nLeafVol=" << LeafVol << std::endl;
  173. for (auto leaf : LeafDictIdx) {
  174. kerr->InsertTuple1( leaf.first, leaf.second );
  175. }
  176. auto kri = oct->GetLeafData()->AddArray(kr);
  177. auto kii = oct->GetLeafData()->AddArray(ki);
  178. auto kmi = oct->GetLeafData()->AddArray(km);
  179. auto kidi = oct->GetLeafData()->AddArray(kid);
  180. auto keri = oct->GetLeafData()->AddArray(kerr);
  181. auto write = vtkXMLHyperOctreeWriter::New();
  182. //write.SetDataModeToAscii()
  183. write->SetInputData(oct);
  184. std::string fname = std::string("octree-") + to_string(ilay)
  185. + std::string("-") + to_string(iq) + std::string(".vto");
  186. write->SetFileName(fname.c_str());
  187. write->Write();
  188. write->Delete();
  189. oct->GetLeafData()->RemoveArray( kri );
  190. oct->GetLeafData()->RemoveArray( kii );
  191. oct->GetLeafData()->RemoveArray( kmi );
  192. oct->GetLeafData()->RemoveArray( kidi );
  193. oct->GetLeafData()->RemoveArray( keri );
  194. kerr->Delete();
  195. kid->Delete();
  196. kr->Delete();
  197. ki->Delete();
  198. km->Delete();
  199. }
  200. curse->Delete();
  201. oct->Delete();
  202. #else
  203. throw std::runtime_error("IntegrateOnOctreeGrid with vtkOutput requires Lemma with VTK support");
  204. #endif
  205. }
  206. std::cout << "\nVOLSUM=" << VOLSUM << "\tActual=" << Size(0)*Size(1)*Size(2)
  207. << "\tDifference=" << VOLSUM - (Size(0)*Size(1)*Size(2)) << std::endl;
  208. }
  209. //--------------------------------------------------------------------------------------
  210. // Class: Coupling
  211. // Method: f
  212. //--------------------------------------------------------------------------------------
  213. Complex Coupling::f( const Vector3r& r, const Real& volume, const Vector3cr& Ht, const Vector3cr& Hr ) {
  214. return volume*Ht.dot(Hr);
  215. }
  216. //--------------------------------------------------------------------------------------
  217. // Class: Coupling
  218. // Method: EvaluateKids
  219. //--------------------------------------------------------------------------------------
  220. void Coupling::EvaluateKids( const Vector3r& size, const int& level, const Vector3r& cpos,
  221. const Complex& parentVal ) {
  222. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  223. //std::cout.flush();
  224. // Next level step, interested in one level below
  225. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  226. Vector3r step = size.array() / (Real)(1 << (level+1) );
  227. Real vol = (step(0)*step(1)*step(2)); // volume of each child
  228. Vector3r pos = cpos - step/2.;
  229. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  230. 0, 0, 0,
  231. step[0], 0, 0,
  232. 0, step[1], 0,
  233. step[0], step[1], 0,
  234. 0, 0, step[2],
  235. step[0], 0, step[2],
  236. 0, step[1], step[2],
  237. step[0], step[1], step[2] ).finished();
  238. VectorXcr kvals(8); // individual kernel vals
  239. cpoints->ClearFields();
  240. for (int ichild=0; ichild<8; ++ichild) {
  241. Vector3r cp = pos; // Eigen complains about combining these
  242. cp += posadd.row(ichild);
  243. cpoints->SetLocation( ichild, cp );
  244. }
  245. Eigen::Matrix<Complex, 3, 8> Ht = Eigen::Matrix<Complex, 3, 8>::Zero();
  246. Eigen::Matrix<Complex, 3, 8> Hr = Eigen::Matrix<Complex, 3, 8>::Zero();
  247. for ( auto EMCalc : EMEarths ) {
  248. EMCalc.second->GetFieldPoints()->ClearFields();
  249. EMCalc.second->CalculateWireAntennaFields();
  250. switch (EMCalc.second->GetTxRxMode()) {
  251. case TX:
  252. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  253. break;
  254. case RX:
  255. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  256. break;
  257. case TXRX:
  258. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  259. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  260. break;
  261. default:
  262. break;
  263. }
  264. }
  265. for (int ichild=0; ichild<8; ++ichild) {
  266. Vector3r cp = pos; // Eigen complains about combining these
  267. cp += posadd.row(ichild);
  268. kvals(ichild) = f(cp, vol, Ht.col(ichild), Hr.col(ichild));
  269. }
  270. Complex ksum = kvals.sum(); // Kernel sum
  271. // Evaluate whether or not furthur splitting is needed
  272. if ( std::abs(ksum-parentVal) > tol || level < minLevel && level < maxLevel ) {
  273. // Not a leaf dive further in
  274. for (int ichild=0; ichild<8; ++ichild) {
  275. Vector3r cp = pos; // Eigen complains about combining these
  276. cp += posadd.row(ichild);
  277. EvaluateKids( size, level+1, cp, kvals(ichild) );
  278. }
  279. return; // not leaf
  280. }
  281. // implicit else, is a leaf
  282. SUM += ksum;
  283. VOLSUM += 8.*vol;
  284. nleaves += 1; // could say += 8 just as fairly
  285. return; // is leaf
  286. }
  287. #ifdef LEMMAUSEVTK
  288. //--------------------------------------------------------------------------------------
  289. // Class: Coupling
  290. // Method: EvaluateKids2 -- same as Evaluate Kids, but include VTK octree generation
  291. //--------------------------------------------------------------------------------------
  292. void Coupling::EvaluateKids2( const Vector3r& size, const int& level, const Vector3r& cpos,
  293. const Complex& parentVal, vtkHyperOctree* oct, vtkHyperOctreeCursor* curse) {
  294. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  295. std::cout.flush();
  296. // Next level step, interested in one level below
  297. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  298. Vector3r step = size.array() / (Real)(1 << (level+1) );
  299. Real vol = (step(0)*step(1)*step(2)); // volume of each child
  300. Vector3r pos = cpos - step/2.;
  301. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  302. 0, 0, 0,
  303. step[0], 0, 0,
  304. 0, step[1], 0,
  305. step[0], step[1], 0,
  306. 0, 0, step[2],
  307. step[0], 0, step[2],
  308. 0, step[1], step[2],
  309. step[0], step[1], step[2] ).finished();
  310. VectorXcr kvals(8); // individual kernel vals
  311. cpoints->ClearFields();
  312. for (int ichild=0; ichild<8; ++ichild) {
  313. Vector3r cp = pos; // Eigen complains about combining these
  314. cp += posadd.row(ichild);
  315. cpoints->SetLocation( ichild, cp );
  316. }
  317. Eigen::Matrix<Complex, 3, 8> Ht = Eigen::Matrix<Complex, 3, 8>::Zero();
  318. Eigen::Matrix<Complex, 3, 8> Hr = Eigen::Matrix<Complex, 3, 8>::Zero();
  319. for ( auto EMCalc : EMEarths ) {
  320. //EMCalc->GetFieldPoints()->ClearFields();
  321. EMCalc.second->CalculateWireAntennaFields();
  322. switch (EMCalc.second->GetTxRxMode()) {
  323. case TX:
  324. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  325. break;
  326. case RX:
  327. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  328. break;
  329. case TXRX:
  330. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  331. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  332. break;
  333. default:
  334. break;
  335. }
  336. }
  337. for (int ichild=0; ichild<8; ++ichild) {
  338. Vector3r cp = pos; // Eigen complains about combining these
  339. cp += posadd.row(ichild);
  340. kvals(ichild) = f(cp, vol, Ht.col(ichild), Hr.col(ichild));
  341. }
  342. Complex ksum = kvals.sum(); // Kernel sum
  343. // Evaluate whether or not furthur splitting is needed
  344. if ( std::abs(ksum-parentVal) > tol || level < minLevel && level < maxLevel ) {
  345. oct->SubdivideLeaf(curse);
  346. for (int ichild=0; ichild<8; ++ichild) {
  347. curse->ToChild(ichild);
  348. Vector3r cp = pos; // Eigen complains about combining these
  349. cp += posadd.row(ichild);
  350. /* Test for position via alternative means */
  351. /*
  352. Real p[3];
  353. GetPosition(curse, p);
  354. if ( (Vector3r(p) - cp).norm() > 1e-8 ) {
  355. std::cout << "ERROR @ nleaves" << nleaves << "\n" << cp[0] << "\t" << p[0] << "\t" << cp[1] << "\t" << p[1]
  356. << "\t" << cp[2] << "\t" << p[2] << "\t" << vol<< std::endl;
  357. throw std::runtime_error("doom");
  358. }
  359. */
  360. /* End of position test */
  361. EvaluateKids2( size, level+1, cp, kvals.row(ichild), oct, curse );
  362. curse->ToParent();
  363. }
  364. return; // not a leaf
  365. }
  366. LeafDict[curse->GetLeafId()] = ksum/(8.*vol);
  367. LeafDictIdx[curse->GetLeafId()] = nleaves;
  368. Kern.row(ilay) += ksum;
  369. VOLSUM += 8*vol;
  370. nleaves += 1;
  371. return; // is a leaf
  372. }
  373. //--------------------------------------------------------------------------------------
  374. // Class: Coupling
  375. // Method: GetPosition
  376. //--------------------------------------------------------------------------------------
  377. void Coupling::GetPosition( vtkHyperOctreeCursor* Cursor, Real* p ) {
  378. Real ratio=1.0/(1<<(Cursor->GetCurrentLevel()));
  379. //step = ((Size).array() / std::pow(2.,Cursor->GetCurrentLevel()));
  380. p[0]=(Cursor->GetIndex(0)+.5)*ratio*this->Size[0]+this->Origin[0] ;//+ .5*step[0];
  381. p[1]=(Cursor->GetIndex(1)+.5)*ratio*this->Size[1]+this->Origin[1] ;//+ .5*step[1];
  382. p[2]=(Cursor->GetIndex(2)+.5)*ratio*this->Size[2]+this->Origin[2] ;//+ .5*step[2];
  383. }
  384. #endif
  385. } // ---- end of namespace Lemma ----
  386. /* vim: set tabstop=4 expandtab */
  387. /* vim: set filetype=cpp */