Surface NMR forward modelling
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Coupling.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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, Complex(100.0), oct, curse );
  143. // Fill in leaf data
  144. vtkDoubleArray* kr = vtkDoubleArray::New();
  145. kr->SetNumberOfComponents(1);
  146. kr->SetName("Re($K_0$)");
  147. kr->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  148. vtkDoubleArray* ki = vtkDoubleArray::New();
  149. ki->SetNumberOfComponents(1);
  150. ki->SetName("Im($K_0$)");
  151. ki->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  152. vtkDoubleArray* km = vtkDoubleArray::New();
  153. km->SetNumberOfComponents(1);
  154. km->SetName("mod($K_0$)");
  155. km->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  156. vtkIntArray* kid = vtkIntArray::New();
  157. kid->SetNumberOfComponents(1);
  158. kid->SetName("ID");
  159. kid->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  160. vtkIntArray* kerr = vtkIntArray::New();
  161. kerr->SetNumberOfComponents(1);
  162. kerr->SetName("nleaf");
  163. //Real LeafVol(0);
  164. for (auto leaf : LeafDict) {
  165. kr->InsertTuple1( leaf.first, std::real(leaf.second) );
  166. ki->InsertTuple1( leaf.first, std::imag(leaf.second) );
  167. km->InsertTuple1( leaf.first, std::abs(leaf.second) );
  168. kid->InsertTuple1( leaf.first, leaf.first );
  169. //LeafVol += std::real(leaf.second);
  170. }
  171. //std::cout << "\n\nLeafVol=" << LeafVol << std::endl;
  172. for (auto leaf : LeafDictIdx) {
  173. kerr->InsertTuple1( leaf.first, leaf.second );
  174. }
  175. auto kri = oct->GetLeafData()->AddArray(kr);
  176. auto kii = oct->GetLeafData()->AddArray(ki);
  177. auto kmi = oct->GetLeafData()->AddArray(km);
  178. auto kidi = oct->GetLeafData()->AddArray(kid);
  179. auto keri = oct->GetLeafData()->AddArray(kerr);
  180. auto write = vtkXMLHyperOctreeWriter::New();
  181. //write.SetDataModeToAscii()
  182. write->SetInputData(oct);
  183. std::string fname = std::string("octree-couple") + std::string(".vto");
  184. write->SetFileName(fname.c_str());
  185. write->Write();
  186. write->Delete();
  187. oct->GetLeafData()->RemoveArray( kri );
  188. oct->GetLeafData()->RemoveArray( kii );
  189. oct->GetLeafData()->RemoveArray( kmi );
  190. oct->GetLeafData()->RemoveArray( kidi );
  191. oct->GetLeafData()->RemoveArray( keri );
  192. kerr->Delete();
  193. kid->Delete();
  194. kr->Delete();
  195. ki->Delete();
  196. km->Delete();
  197. curse->Delete();
  198. oct->Delete();
  199. #else
  200. throw std::runtime_error("IntegrateOnOctreeGrid with vtkOutput requires Lemma with VTK support");
  201. #endif
  202. }
  203. std::cout << "\nVOLSUM=" << VOLSUM << "\tActual=" << Size(0)*Size(1)*Size(2)
  204. << "\tDifference=" << VOLSUM - (Size(0)*Size(1)*Size(2)) << std::endl;
  205. }
  206. //--------------------------------------------------------------------------------------
  207. // Class: Coupling
  208. // Method: f
  209. //--------------------------------------------------------------------------------------
  210. Complex Coupling::f( const Vector3r& r, const Real& volume, const Vector3cr& Ht, const Vector3cr& Hr ) {
  211. return volume*Ht.dot(Hr);
  212. //return Ht.dot(Hr);
  213. }
  214. //--------------------------------------------------------------------------------------
  215. // Class: Coupling
  216. // Method: EvaluateKids
  217. //--------------------------------------------------------------------------------------
  218. void Coupling::EvaluateKids( const Vector3r& size, const int& level, const Vector3r& cpos,
  219. const Complex& parentVal ) {
  220. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  221. std::cout.flush();
  222. // Next level step, interested in one level below
  223. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  224. Vector3r step = size.array() / (Real)(1 << (level+1) );
  225. Real vol = (step(0)*step(1)*step(2)); // volume of each child
  226. Vector3r pos = cpos - step/2.;
  227. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  228. 0, 0, 0,
  229. step[0], 0, 0,
  230. 0, step[1], 0,
  231. step[0], step[1], 0,
  232. 0, 0, step[2],
  233. step[0], 0, step[2],
  234. 0, step[1], step[2],
  235. step[0], step[1], step[2] ).finished();
  236. VectorXcr kvals(8); // individual kernel vals
  237. cpoints->ClearFields();
  238. for (int ichild=0; ichild<8; ++ichild) {
  239. Vector3r cp = pos; // Eigen complains about combining these
  240. cp += posadd.row(ichild);
  241. cpoints->SetLocation( ichild, cp );
  242. }
  243. Eigen::Matrix<Complex, 3, 8> Ht = Eigen::Matrix<Complex, 3, 8>::Zero();
  244. Eigen::Matrix<Complex, 3, 8> Hr = Eigen::Matrix<Complex, 3, 8>::Zero();
  245. for ( auto EMCalc : EMEarths ) {
  246. EMCalc.second->GetFieldPoints()->ClearFields();
  247. EMCalc.second->CalculateWireAntennaFields();
  248. switch (EMCalc.second->GetTxRxMode()) {
  249. case TX:
  250. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  251. break;
  252. case RX:
  253. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  254. break;
  255. case TXRX:
  256. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  257. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  258. break;
  259. default:
  260. break;
  261. }
  262. }
  263. for (int ichild=0; ichild<8; ++ichild) {
  264. Vector3r cp = pos; // Eigen complains about combining these
  265. cp += posadd.row(ichild);
  266. kvals(ichild) = f(cp, vol, Ht.col(ichild), Hr.col(ichild));
  267. }
  268. Complex ksum = kvals.sum(); // Kernel sum
  269. // Evaluate whether or not furthur splitting is needed
  270. if ( std::abs(ksum-parentVal) > tol || level < minLevel && level < maxLevel ) {
  271. // Not a leaf dive further in
  272. for (int ichild=0; ichild<8; ++ichild) {
  273. Vector3r cp = pos; // Eigen complains about combining these
  274. cp += posadd.row(ichild);
  275. EvaluateKids( size, level+1, cp, kvals(ichild) );
  276. }
  277. return; // not leaf
  278. }
  279. // implicit else, is a leaf
  280. SUM += ksum;
  281. VOLSUM += 8.*vol;
  282. nleaves += 1; // could say += 8 just as fairly
  283. return; // is leaf
  284. }
  285. #ifdef LEMMAUSEVTK
  286. //--------------------------------------------------------------------------------------
  287. // Class: Coupling
  288. // Method: EvaluateKids2 -- same as Evaluate Kids, but include VTK octree generation
  289. //--------------------------------------------------------------------------------------
  290. void Coupling::EvaluateKids2( const Vector3r& size, const int& level, const Vector3r& cpos,
  291. const Complex& parentVal, vtkHyperOctree* oct, vtkHyperOctreeCursor* curse) {
  292. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  293. std::cout.flush();
  294. // Next level step, interested in one level below
  295. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  296. Vector3r step = size.array() / (Real)(1 << (level+1) );
  297. Real vol = (step(0)*step(1)*step(2)); // volume of each child
  298. Vector3r pos = cpos - step/2.;
  299. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  300. 0, 0, 0,
  301. step[0], 0, 0,
  302. 0, step[1], 0,
  303. step[0], step[1], 0,
  304. 0, 0, step[2],
  305. step[0], 0, step[2],
  306. 0, step[1], step[2],
  307. step[0], step[1], step[2] ).finished();
  308. VectorXcr kvals(8); // individual kernel vals
  309. cpoints->ClearFields();
  310. for (int ichild=0; ichild<8; ++ichild) {
  311. Vector3r cp = pos; // Eigen complains about combining these
  312. cp += posadd.row(ichild);
  313. cpoints->SetLocation( ichild, cp );
  314. }
  315. Eigen::Matrix<Complex, 3, 8> Ht = Eigen::Matrix<Complex, 3, 8>::Zero();
  316. Eigen::Matrix<Complex, 3, 8> Hr = Eigen::Matrix<Complex, 3, 8>::Zero();
  317. for ( auto EMCalc : EMEarths ) {
  318. //EMCalc->GetFieldPoints()->ClearFields();
  319. EMCalc.second->CalculateWireAntennaFields();
  320. switch (EMCalc.second->GetTxRxMode()) {
  321. case TX:
  322. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  323. break;
  324. case RX:
  325. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  326. break;
  327. case TXRX:
  328. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  329. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  330. break;
  331. default:
  332. break;
  333. }
  334. }
  335. for (int ichild=0; ichild<8; ++ichild) {
  336. Vector3r cp = pos; // Eigen complains about combining these
  337. cp += posadd.row(ichild);
  338. kvals(ichild) = f(cp, vol, Ht.col(ichild), Hr.col(ichild));
  339. }
  340. Complex ksum = kvals.sum(); // Kernel sum
  341. // Evaluate whether or not furthur splitting is needed
  342. if ( std::abs(ksum-parentVal) > tol || level < minLevel && level < maxLevel ) {
  343. oct->SubdivideLeaf(curse);
  344. for (int ichild=0; ichild<8; ++ichild) {
  345. curse->ToChild(ichild);
  346. Vector3r cp = pos; // Eigen complains about combining these
  347. cp += posadd.row(ichild);
  348. /* Test for position via alternative means */
  349. /*
  350. Real p[3];
  351. GetPosition(curse, p);
  352. if ( (Vector3r(p) - cp).norm() > 1e-8 ) {
  353. std::cout << "ERROR @ nleaves" << nleaves << "\n" << cp[0] << "\t" << p[0] << "\t" << cp[1] << "\t" << p[1]
  354. << "\t" << cp[2] << "\t" << p[2] << "\t" << vol<< std::endl;
  355. throw std::runtime_error("doom");
  356. }
  357. */
  358. /* End of position test */
  359. EvaluateKids2( size, level+1, cp, kvals(ichild), oct, curse );
  360. curse->ToParent();
  361. }
  362. return; // not a leaf
  363. }
  364. LeafDict[curse->GetLeafId()] = ksum/(8.*vol);
  365. LeafDictIdx[curse->GetLeafId()] = nleaves;
  366. SUM += ksum;
  367. VOLSUM += 8*vol;
  368. nleaves += 1;
  369. return; // is a leaf
  370. }
  371. //--------------------------------------------------------------------------------------
  372. // Class: Coupling
  373. // Method: GetPosition
  374. //--------------------------------------------------------------------------------------
  375. void Coupling::GetPosition( vtkHyperOctreeCursor* Cursor, Real* p ) {
  376. Real ratio=1.0/(1<<(Cursor->GetCurrentLevel()));
  377. //step = ((Size).array() / std::pow(2.,Cursor->GetCurrentLevel()));
  378. p[0]=(Cursor->GetIndex(0)+.5)*ratio*this->Size[0]+this->Origin[0] ;//+ .5*step[0];
  379. p[1]=(Cursor->GetIndex(1)+.5)*ratio*this->Size[1]+this->Origin[1] ;//+ .5*step[1];
  380. p[2]=(Cursor->GetIndex(2)+.5)*ratio*this->Size[2]+this->Origin[2] ;//+ .5*step[2];
  381. }
  382. #endif
  383. } // ---- end of namespace Lemma ----
  384. /* vim: set tabstop=4 expandtab */
  385. /* vim: set filetype=cpp */