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.

Hantenna.cpp 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // ===========================================================================
  2. //
  3. // Filename: hantenna.cpp
  4. //
  5. // Created: 10/07/2010 08:57:04 AM
  6. // Modified: 11 April 2018
  7. // Compiler: Tested with g++, icpc, and MSVC 2017
  8. //
  9. // Author: Trevor Irons (ti)
  10. //
  11. // Copyright (C) 2012,2018 Trevor Irons
  12. //
  13. // Organisation: Lemma Software
  14. //
  15. // Email: Trevor.Irons@lemmasoftware.org
  16. //
  17. // ===========================================================================
  18. /**
  19. @file
  20. @author Trevor Irons
  21. @date 10/07/2010
  22. $Id$
  23. **/
  24. #include "LemmaCore"
  25. #include "FDEM1D"
  26. #include "timer.h"
  27. #if defined(__clang__)
  28. /* Clang/LLVM. ---------------------------------------------- */
  29. const char* compiler = "clang";
  30. #elif defined(__ICC) || defined(__INTEL_COMPILER)
  31. /* Intel ICC/ICPC. ------------------------------------------ */
  32. const char* compiler = "icpc";
  33. #elif defined(__GNUC__) || defined(__GNUG__)
  34. /* GNU GCC/G++. --------------------------------------------- */
  35. const char* compiler = "gcc (GCC) ";// __VERSION__;
  36. const char* ver = __VERSION__;
  37. #elif defined(_MSC_VER)
  38. /* Microsoft Visual Studio. --------------------------------- */
  39. const char* compiler = "msvc ";
  40. const int ver = _MSC_FULL_VER;
  41. #elif defined(__PGI)
  42. /* Portland Group PGCC/PGCPP. ------------------------------- */
  43. const char* compiler = "pgc";
  44. #endif
  45. using namespace Lemma;
  46. std::vector<Real> readinpfile(const std::string& fname);
  47. std::vector<std::string> readinpfile2(const std::string& fname);
  48. int main(int argc, char** argv) {
  49. const char *buildString = __DATE__ ", " __TIME__;
  50. std::cout
  51. << "===========================================================================\n"
  52. << "Lemma " << LEMMA_VERSION << "\n"
  53. << "[" << compiler << " " << ver << " " << buildString << "]\n"
  54. << "This program is part of Lemma, a geophysical modelling and inversion API. \n"
  55. << " This Source Code Form is subject to the terms of the Mozilla Public\n"
  56. << " License, v. 2.0. If a copy of the MPL was not distributed with this\n"
  57. << " file, You can obtain one at http://mozilla.org/MPL/2.0/. \n"
  58. << "Copyright (C) 2018 Lemma Software \n"
  59. << "More information may be found at: https://lemmasoftware.org\n"
  60. << " info@lemmasoftware.org\n"
  61. << "===========================================================================\n\n"
  62. << "Hantenna calculates the harmonic H field from polygonal wire loop sources\n";
  63. if (argc < 5) {
  64. std::cout << "usage: hantenna.exe trans.inp cond.inp points.inp config.inp \n";
  65. exit(0);
  66. }
  67. #ifdef LEMMAUSEOMP
  68. std::cout << "OpenMP is using " << omp_get_max_threads() << " threads" << std::endl;
  69. #endif
  70. std::vector<Real> Trans = readinpfile(std::string(argv[1]));
  71. std::vector<Real> CondMod = readinpfile(std::string(argv[2]));
  72. std::vector<Real> Points = readinpfile(std::string(argv[3]));
  73. std::vector<std::string> config = readinpfile2(std::string(argv[4]));
  74. //////////////////////////////////////
  75. // Define transmitter
  76. auto trans = PolygonalWireAntenna::NewSP();
  77. trans->SetNumberOfPoints((int)(Trans[0]));
  78. int ip=1;
  79. for ( ; ip<=(int)(Trans[0])*2; ip+=2) {
  80. trans->SetPoint(ip/2, Vector3r (Trans[ip], Trans[ip+1], -1e-3));
  81. }
  82. trans->SetNumberOfFrequencies(1);
  83. trans->SetFrequency(0, Trans[ip]);
  84. trans->SetCurrent(Trans[ip+1]);
  85. trans->SetMinDipoleRatio(atof(config[1].c_str()));
  86. trans->SetMinDipoleMoment(atof(config[2].c_str()));
  87. trans->SetMaxDipoleMoment(atof(config[3].c_str()));
  88. /////////////////////////////////////
  89. // Field calculations
  90. auto receivers = FieldPoints::NewSP();
  91. int nx = (int)Points[0];
  92. int ny = (int)Points[1];
  93. int nz = (int)Points[2];
  94. Real ox = Points[3];
  95. Real oy = Points[4];
  96. Real oz = Points[5];
  97. Vector3r loc;
  98. VectorXr dx(nx-1);
  99. VectorXr dy(ny-1);
  100. VectorXr dz(nz-1);
  101. ip = 6;
  102. int ir = 0;
  103. for ( ; ip <6+nx-1; ++ip) {
  104. dx[ir] = Points[ip];
  105. ++ir;
  106. }
  107. ir = 0;
  108. for ( ; ip <6+ny-1+nx-1; ++ip) {
  109. dy[ir] = Points[ip];
  110. ++ir;
  111. }
  112. ir = 0;
  113. for ( ; ip <6+nz-1+ny-1+nx-1; ++ip) {
  114. dz[ir] = Points[ip];
  115. ++ir;
  116. }
  117. receivers->SetNumberOfPoints(nx*ny*nz);
  118. ir = 0;
  119. Real pz = oz;
  120. for (int iz=0; iz<nz; ++iz) {
  121. Real py = oy;
  122. for (int iy=0; iy<ny; ++iy) {
  123. Real px = ox;
  124. for (int ix=0; ix<nx; ++ix) {
  125. loc << px, py, pz;
  126. receivers->SetLocation(ir, loc);
  127. if (ix < nx-1) px += dx[ix];
  128. ++ ir;
  129. }
  130. if (iy<ny-1) py += dy[iy];
  131. }
  132. if (iz<nz-1) pz += dz[iz];
  133. }
  134. ////////////////////////////////////
  135. // Define model
  136. auto earth = LayeredEarthEM::NewSP();
  137. VectorXcr sigma;
  138. VectorXr thick;
  139. earth->SetNumberOfLayers(static_cast<int>(CondMod[0])+1);
  140. sigma.resize(static_cast<int>(CondMod[0])+1); sigma(0) = 0; // airlayer
  141. thick.resize(static_cast<int>(CondMod[0])-1);
  142. int ilay=1;
  143. for ( ; ilay/2<CondMod[0]-1; ilay+=2) {
  144. sigma(ilay/2+1) = 1./CondMod[ilay];
  145. thick(ilay/2) = CondMod[ilay+1];
  146. }
  147. sigma(ilay/2+1) = 1./ CondMod[ilay];
  148. earth->SetLayerConductivity(sigma);
  149. if (thick.size() > 0) earth->SetLayerThickness(thick);
  150. auto EmEarth = EMEarth1D::NewSP();
  151. EmEarth->AttachWireAntenna(trans);
  152. EmEarth->AttachLayeredEarthEM(earth);
  153. EmEarth->AttachFieldPoints(receivers);
  154. EmEarth->SetFieldsToCalculate(H);
  155. EmEarth->SetHankelTransformMethod(string2Enum<HANKELTRANSFORMTYPE>(config[0]));
  156. EmEarth->SetHankelTransformMethod(FHTKEY201);
  157. ///////////////////////////////////////////////
  158. // Keep track of time
  159. jsw_timer timer;
  160. timer.begin();
  161. clock_t launch = clock();
  162. EmEarth->CalculateWireAntennaFields(true); // true=status bar
  163. Real paTime = timer.end();
  164. std::cout << "\n\n===========================================\ncalc. real time: " << paTime/60. << "\t[m]\n";
  165. std::cout << "calc. user time: " << (clock()-launch)/CLOCKS_PER_SEC/60. << "\t[CPU m]"
  166. << std::endl;
  167. ////////////////////////////////////
  168. // Report
  169. std::fstream hrep("hfield.yaml", std::ios::out);
  170. std::fstream hreal("hfield.dat", std::ios::out);
  171. hrep << *EmEarth << std::endl;
  172. hrep.close();
  173. //hreal << *trans << std::endl;
  174. //hreal << *earth << std::endl;
  175. hreal << "// Right hand coordinate system, z is positive down\n";
  176. hreal << "// x[m]\ty[m]\tz[m]\tHx[A/m]\tHy[A/m]\tHz[A/m]\n";
  177. hreal.precision(8);
  178. int i=0;
  179. for (int iz=0; iz<nz; ++iz) {
  180. for (int iy=0; iy<ny; ++iy) {
  181. for (int ix=0; ix<nx; ++ix) {
  182. hreal << receivers->GetLocation(i).transpose() << "\t";
  183. hreal << receivers->GetHfield(0, i).transpose() << "\n";
  184. ++i;
  185. }
  186. }
  187. }
  188. hreal.close();
  189. // Clean up
  190. }
  191. std::vector<Real> readinpfile(const std::string& fname) {
  192. std::string buf;
  193. char dump[255];
  194. std::vector<Real> vals;
  195. std::fstream input(fname.c_str(), std::ios::in);
  196. if (input.fail()) {
  197. std::cerr << "Input file " << fname << " failed to open\n";
  198. exit(EXIT_FAILURE);
  199. }
  200. while (input >> buf) {
  201. if (buf.substr(0,2) == "//") {
  202. input.getline(dump, 255);
  203. } else {
  204. vals.push_back( atof(buf.c_str() ));
  205. }
  206. }
  207. return vals;
  208. }
  209. std::vector<std::string> readinpfile2(const std::string& fname) {
  210. std::string buf;
  211. char dump[255];
  212. std::vector<std::string> vals;
  213. std::fstream input(fname.c_str(), std::ios::in);
  214. if (input.fail()) {
  215. std::cerr << "Input file " << fname << " failed to open\n";
  216. exit(EXIT_FAILURE);
  217. }
  218. while (input >> buf) {
  219. if (buf.substr(0,2) == "//") {
  220. input.getline(dump, 255);
  221. } else {
  222. vals.push_back( std::string(buf.c_str() ));
  223. }
  224. }
  225. return vals;
  226. }