Lemma is an Electromagnetics API
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.

DCSurvey.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 10/08/2014 01:52:04 PM
  11. * @author Trevor Irons (ti)
  12. * @email Trevor.Irons@lemmasoftware.org
  13. * @copyright Copyright (c) 2014, 2018 Trevor Irons
  14. */
  15. #include "DCSurvey.h"
  16. namespace Lemma {
  17. // ==================== FRIEND METHODS =====================
  18. std::ostream &operator << (std::ostream &stream, const DCSurvey &ob) {
  19. stream << ob.Serialize() << "\n";
  20. return stream;
  21. }
  22. // ==================== LIFECYCLE =======================
  23. //--------------------------------------------------------------------------------------
  24. // Class: DCSurvey
  25. // Method: DCSurvey
  26. // Description: constructor (protected)
  27. //--------------------------------------------------------------------------------------
  28. DCSurvey::DCSurvey (const ctor_key& key) : LemmaObject(key) {
  29. } // ----- end of method DCSurvey::DCSurvey (constructor) -----
  30. //--------------------------------------------------------------------------------------
  31. // Class: DCSurvey
  32. // Method: DCSurvey
  33. // Description: DeSerializing constructor (protected)
  34. //--------------------------------------------------------------------------------------
  35. DCSurvey::DCSurvey (const YAML::Node& node, const ctor_key& key) : LemmaObject(node, key) {
  36. if (node.Tag() != "DCSurvey") {
  37. throw std::runtime_error("DCSurvey->DeSerialize cannot deserialize non-DCSurvey");
  38. }
  39. std::map<YAML::Node, int> eMap;
  40. for(YAML::const_iterator it=node["Electrodes"].begin(); it!=node["Electrodes"].end(); ++it) {
  41. std::string e = it->first.as<std::string>();
  42. Electrodes.push_back( DCIPElectrode::DeSerialize( node["Electrodes"][e] ) );
  43. eMap[ node["Electrodes"][Electrodes.size()-1] ] = Electrodes.size() -1 ;
  44. OrderedElectrodeLabels.push_back(e);
  45. ElectrodeLabelMap[e] = std::pair< std::shared_ptr<DCIPElectrode>, int>(Electrodes[Electrodes.size()-1], Electrodes.size()-1);
  46. }
  47. //std::cout << "J-0\n" << node["Injections"]["J-0"] << std::endl;
  48. int ij = 0 ;
  49. for(YAML::const_iterator it=node["Injections"].begin(); it!=node["Injections"].end(); ++it) {
  50. //std::cout << "it->first" << it->first << std::endl;
  51. A_Electrodes.push_back( ElectrodeLabelMap[ it->second["A"]["Label"].as<std::string>() ].second );
  52. B_Electrodes.push_back( ElectrodeLabelMap[ it->second["B"]["Label"].as<std::string>() ].second );
  53. J_Electrodes.push_back( it->second["J"].as<Real>() );
  54. //std::string Jstr = it->first.as<std::string>(); // J-1
  55. M_Electrodes.push_back( std::vector<int>() );
  56. N_Electrodes.push_back( std::vector<int>() );
  57. for (int ii=0; ii<it->second["Measurements"]["Number"].as<int>(); ++ii) {
  58. std::string Mstr = std::string("V-") + to_string(ii);
  59. //std::cout << "measurements" << it->second["Measurements"][Mstr]["M"]["Label"] << std::endl;
  60. M_Electrodes[ij].push_back( ElectrodeLabelMap[ it->second["Measurements"][Mstr]["M"]["Label"].as<std::string>() ].second );
  61. N_Electrodes[ij].push_back( ElectrodeLabelMap[ it->second["Measurements"][Mstr]["N"]["Label"].as<std::string>() ].second );
  62. }
  63. ++ ij;
  64. }
  65. } // ----- end of method DCSurvey::DCSurvey (constructor) -----
  66. //--------------------------------------------------------------------------------------
  67. // Class: DCSurvey
  68. // Method: New()
  69. // Description: public constructor
  70. //--------------------------------------------------------------------------------------
  71. std::shared_ptr<DCSurvey> DCSurvey::NewSP() {
  72. return std::make_shared<DCSurvey>( ctor_key() );
  73. }
  74. //--------------------------------------------------------------------------------------
  75. // Class: DCSurvey
  76. // Method: ~DCSurvey
  77. // Description: destructor (protected)
  78. //--------------------------------------------------------------------------------------
  79. DCSurvey::~DCSurvey () {
  80. } // ----- end of method DCSurvey::~DCSurvey (destructor) -----
  81. //--------------------------------------------------------------------------------------
  82. // Class: DCSurvey
  83. // Method: Serialize
  84. //--------------------------------------------------------------------------------------
  85. YAML::Node DCSurvey::Serialize ( ) const {
  86. YAML::Node node = LemmaObject::Serialize();
  87. node.SetTag( this->GetName() );
  88. node["NumberOfElectrodes"] = Electrodes.size();
  89. // All the electrodes
  90. for (std::map<std::string, std::pair< std::shared_ptr<DCIPElectrode>, int> >::const_iterator it = ElectrodeLabelMap.begin();
  91. it != ElectrodeLabelMap.end(); ++it) {
  92. node["Electrodes"][ it->first ] = it->second.first->Serialize();
  93. }
  94. // Injections and Measurements
  95. for (unsigned int ic=0; ic<A_Electrodes.size(); ++ic) {
  96. std::string strLab = std::string("J-") + to_string(ic);
  97. node["Injections"][strLab]["A"] = node["Electrodes"][ OrderedElectrodeLabels[ A_Electrodes[ic] ] ];
  98. node["Injections"][strLab]["B"] = node["Electrodes"][ OrderedElectrodeLabels[ B_Electrodes[ic] ] ];
  99. node["Injections"][strLab]["J"] = J_Electrodes[ic];
  100. node["Injections"][strLab]["Measurements"]["Number"] = M_Electrodes[ic].size();
  101. for (unsigned int iv=0; iv<M_Electrodes[ic].size(); ++iv) {
  102. node["Injections"][strLab]["Measurements"][std::string("V-") + to_string(iv)]["M"] =
  103. node["Electrodes"][ OrderedElectrodeLabels[M_Electrodes[ic][iv]] ];
  104. node["Injections"][strLab]["Measurements"][std::string("V-") + to_string(iv)]["N"] =
  105. node["Electrodes"][ OrderedElectrodeLabels[N_Electrodes[ic][iv]] ];
  106. }
  107. }
  108. return node;
  109. } // ----- end of method DCSurvey::Serialize -----
  110. //--------------------------------------------------------------------------------------
  111. // Class: DCSurvey
  112. // Method: DeSerialize
  113. //--------------------------------------------------------------------------------------
  114. std::shared_ptr<DCSurvey> DCSurvey::DeSerialize ( const YAML::Node& node ) {
  115. if ( node.Tag() != DCSurvey::CName ) {
  116. throw DeSerializeTypeMismatch( DCSurvey::CName, node.Tag());
  117. }
  118. return std::make_shared< DCSurvey > ( node, ctor_key() );
  119. } // ----- end of method DCSurvey::DeSerialize -----
  120. //--------------------------------------------------------------------------------------
  121. // Class: DCSurvey
  122. // Method: PoundElectrode
  123. //--------------------------------------------------------------------------------------
  124. int DCSurvey::PoundElectrode ( std::shared_ptr<DCIPElectrode> Electrode, const std::string& tag, const int&nodeID ) {
  125. Electrodes.push_back(Electrode);
  126. if (tag != "NULL") {
  127. OrderedElectrodeLabels.push_back(tag);
  128. //ElectrodeTagMap[tag] = Electrode;
  129. ElectrodeLabelMap[tag] = std::pair< std::shared_ptr<DCIPElectrode>, int> (Electrode, Electrodes.size()-1);
  130. Electrode->SetLabel(tag);
  131. } else {
  132. OrderedElectrodeLabels.push_back( std::string("E") + to_string(Electrodes.size()-1) );
  133. //ElectrodeTagMap[std::string("E") + to_string(Electrodes.size()-1)] = Electrode;
  134. ElectrodeLabelMap[std::string("E") + to_string(Electrodes.size()-1)] =
  135. std::pair< std::shared_ptr<DCIPElectrode>, int>(Electrode, Electrodes.size()-1);
  136. Electrode->SetLabel( std::string("E") + to_string(Electrodes.size()-1) );
  137. }
  138. return static_cast<int>( Electrodes.size() ) ;
  139. } // ----- end of method DCSurvey::PoundElectrode -----
  140. //--------------------------------------------------------------------------------------
  141. // Class: DCSurvey
  142. // Method: PoundElectrode
  143. //--------------------------------------------------------------------------------------
  144. std::shared_ptr<DCIPElectrode> DCSurvey::PoundElectrode ( const Vector3r& loc, const std::string& tag, const int& nodeID ) {
  145. auto Electrode = DCIPElectrode::NewSP();
  146. Electrode->SetLocation( loc );
  147. Electrodes.push_back(Electrode);
  148. if (tag != "NULL") {
  149. OrderedElectrodeLabels.push_back(tag);
  150. ElectrodeLabelMap[tag] = std::pair< std::shared_ptr<DCIPElectrode>, int> (Electrode, Electrodes.size()-1);
  151. Electrode->SetLabel(tag);
  152. } else {
  153. OrderedElectrodeLabels.push_back( std::string("E") + to_string(Electrodes.size()-1) );
  154. ElectrodeLabelMap[std::string("E") + to_string(Electrodes.size()-1)] =
  155. std::pair< std::shared_ptr<DCIPElectrode>, int>(Electrode, Electrodes.size()-1);
  156. Electrode->SetLabel( std::string("E") + to_string(Electrodes.size()-1) );
  157. }
  158. return Electrode;
  159. } // ----- end of method DCSurvey::PoundElectrode -----
  160. //--------------------------------------------------------------------------------------
  161. // Class: DCSurvey
  162. // Method: PoundElectrode
  163. //--------------------------------------------------------------------------------------
  164. #ifdef LEMMAUSEVTK
  165. std::shared_ptr<DCIPElectrode> DCSurvey::PoundElectrode( const int& nodeID, vtkDataSet* Mesh, const std::string& tag ) {
  166. auto Electrode = DCIPElectrode::NewSP();
  167. double* loc = Mesh->GetPoint(nodeID);
  168. Electrode->SetLocation( Vector3r(loc[0], loc[1], loc[2]) );
  169. Electrodes.push_back(Electrode);
  170. if (tag != "NULL") {
  171. OrderedElectrodeLabels.push_back(tag);
  172. ElectrodeLabelMap[tag] = std::pair< std::shared_ptr<DCIPElectrode>, int> (Electrode, Electrodes.size()-1);
  173. Electrode->SetLabel(tag);
  174. } else {
  175. OrderedElectrodeLabels.push_back( std::string("E") + to_string(Electrodes.size()-1) );
  176. ElectrodeLabelMap[std::string("E") + to_string(Electrodes.size()-1)] =
  177. std::pair< std::shared_ptr<DCIPElectrode>, int>(Electrode, Electrodes.size()-1);
  178. Electrode->SetLabel( std::string("E") + to_string(Electrodes.size()-1) );
  179. }
  180. return Electrode;
  181. } // ----- end of method DCSurvey::PoundElectrode -----
  182. #endif
  183. //--------------------------------------------------------------------------------------
  184. // Class: DCSurvey
  185. // Method: PullElectrodes
  186. //--------------------------------------------------------------------------------------
  187. void DCSurvey::PullElectrodes ( ) {
  188. Electrodes.clear();
  189. OrderedElectrodeLabels.clear();
  190. ElectrodeLabelMap.clear();
  191. A_Electrodes.clear();
  192. B_Electrodes.clear();
  193. M_Electrodes.clear();
  194. N_Electrodes.clear();
  195. return ;
  196. } // ----- end of method DCSurvey::PullElectrodes -----
  197. //--------------------------------------------------------------------------------------
  198. // Class: DCSurvey
  199. // Method: AddInjection
  200. //--------------------------------------------------------------------------------------
  201. int DCSurvey::AddInjection ( std::shared_ptr<DCIPElectrode> A, std::shared_ptr<DCIPElectrode> B, const Real& J ) {
  202. bool fA = false;
  203. bool fB = false;
  204. for (unsigned int i=0; i<Electrodes.size(); ++i) {
  205. if (Electrodes[i] == A) {
  206. A_Electrodes.push_back(i);
  207. M_Electrodes.push_back( std::vector<int>() );
  208. N_Electrodes.push_back( std::vector<int>() );
  209. fA = true;
  210. }
  211. if (Electrodes[i] == B) {
  212. B_Electrodes.push_back(i);
  213. fB = true;
  214. }
  215. if (fA == true && fB == true) break;
  216. }
  217. if (!fA) {
  218. throw std::runtime_error( "Injection point A not found" );
  219. }
  220. if (!fB) {
  221. throw std::runtime_error( "Injection point B not found" );
  222. }
  223. J_Electrodes.push_back(J);
  224. return A_Electrodes.size()-1; // we want index
  225. } // ----- end of method DCSurvey::AddInjection -----
  226. //--------------------------------------------------------------------------------------
  227. // Class: DCSurvey
  228. // Method: AddMeasurement
  229. //--------------------------------------------------------------------------------------
  230. void DCSurvey::AddMeasurement ( const int& iJ, std::shared_ptr<DCIPElectrode> M, std::shared_ptr<DCIPElectrode> N ) {
  231. bool fM = false;
  232. bool fN = false;
  233. for (unsigned int i=0; i<Electrodes.size(); ++i) {
  234. if (Electrodes[i] == M) {
  235. M_Electrodes[iJ].push_back(i);
  236. fM = true;
  237. }
  238. if (Electrodes[i] == N) {
  239. N_Electrodes[iJ].push_back(i);
  240. fN = true;
  241. }
  242. if (fM == true && fN == true) break;
  243. }
  244. if (!fM) {
  245. throw std::runtime_error( "Injection point M not found" );
  246. }
  247. if (!fN) {
  248. throw std::runtime_error( "Injection point N not found" );
  249. }
  250. return ;
  251. } // ----- end of method DCSurvey::AddMeasurement -----
  252. //--------------------------------------------------------------------------------------
  253. // Class: DCSurvey
  254. // Method: AddMeasurement
  255. //--------------------------------------------------------------------------------------
  256. void DCSurvey::AddMeasurement ( const int& iJ, const std::string& M, const std::string& N ) {
  257. std::pair< std::shared_ptr<DCIPElectrode>, int> Mp = ElectrodeLabelMap[M];
  258. std::pair< std::shared_ptr<DCIPElectrode>, int> Np = ElectrodeLabelMap[N];
  259. if (Mp.first == NULL) {
  260. throw std::runtime_error( "Injection point M not found" );
  261. }
  262. if (Np.first == NULL) {
  263. throw std::runtime_error( "Injection point N not found" );
  264. }
  265. M_Electrodes[iJ].push_back( Mp.second );
  266. N_Electrodes[iJ].push_back( Np.second );
  267. return ;
  268. } // ----- end of method DCSurvey::AddMeasurement -----
  269. //--------------------------------------------------------------------------------------
  270. // Class: DCSurvey
  271. // Method: GetA
  272. //--------------------------------------------------------------------------------------
  273. void DCSurvey::GetA (const int& iA, int& iB, Real& J ) {
  274. iB = Electrodes[A_Electrodes[iA]]->GetNodeID();
  275. J = J_Electrodes[iA];
  276. return ;
  277. } // ----- end of method DCSurvey::GetA -----
  278. //--------------------------------------------------------------------------------------
  279. // Class: DCSurvey
  280. // Method: GetB
  281. //--------------------------------------------------------------------------------------
  282. void DCSurvey::GetB (const int& iA, int& iB, Real& J ) {
  283. iB = Electrodes[B_Electrodes[iA]]->GetNodeID();
  284. J = -J_Electrodes[iA];
  285. return ;
  286. } // ----- end of method DCSurvey::GetA -----
  287. } // ----- end of Lemma name -----