Lemma is an Electromagnetics API
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

utgmrprint.cpp 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // ===========================================================================
  2. // This file is distributed with Lemma,
  3. //
  4. // Filename: utsnmrinversion1d.cpp
  5. //
  6. // Created: 10/07/2010 08:57:04 AM
  7. // Compiler: Tested with g++, icpc, and MSVC 2010
  8. //
  9. // Author: Trevor Irons (ti)
  10. //
  11. // Organisation: Colorado School of Mines (CSM)
  12. // United States Geological Survey (USGS)
  13. //
  14. // Email: tirons@mines.edu, tirons@usgs.gov
  15. //
  16. // This program is free software: you can redistribute it and/or modify
  17. // it under the terms of the GNU General Public License as published by
  18. // the Free Software Foundation, either version 3 of the License, or
  19. // (at your option) any later version.
  20. //
  21. // This program is distributed in the hope that it will be useful,
  22. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. // GNU General Public License for more details.
  25. //
  26. // You should have received a copy of the GNU General Public License
  27. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. //
  29. // ===========================================================================
  30. /**
  31. @file
  32. @author Trevor Irons
  33. @date 10/07/2010
  34. @version 0.0
  35. **/
  36. #ifdef LEMMAUSEVTK
  37. #include "vtkRenderer.h"
  38. #include "vtkRenderWindow.h"
  39. #include "vtkRenderWindowInteractor.h"
  40. #include "vtkPNGWriter.h"
  41. #include "vtkRenderLargeImage.h"
  42. #endif
  43. #include "receiverpoints.h"
  44. #include "emearth1d.h"
  45. #include "PolygonalWireAntenna.h"
  46. using namespace Lemma;
  47. std::vector<Real> readinpfile(const std::string& fname);
  48. int main(int argc, char** argv) {
  49. std::cout <<
  50. "\n"
  51. << "hantenna - a programme for computing the h field from polygonal wire\n"
  52. << "loop sources \n\n"
  53. << "The following copyrights apply to this application:\n"
  54. << "Copyright (C) 2009, 2010, 2011, 2012 Colorado School of Mines\n"
  55. << "Copyright (C) 2009, 2010, 2011, 2012 Trevor Irons\n"
  56. << "Copyright (C) 2011 Broken Spoke Development, LLC\n\n"
  57. << "hantenna was built using Lemma (Lemma is an Electromagnetics Modelling API)\n"
  58. << "More information may be found at https://lemmasoftware.org\n\n"
  59. << "This program is free software: you can redistribute it and/or modify\n"
  60. << "it under the terms of the GNU General Public License as published by\n"
  61. << "the Free Software Foundation, either version 3 of the License, or\n"
  62. << "(at your option) any later version.\n\n"
  63. << "This program is distributed in the hope that it will be useful,\n"
  64. << "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  65. << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  66. << "GNU General Public License for more details.\n\n"
  67. << "You should have received a copy of the GNU General Public License\n"
  68. << "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n";
  69. if (argc < 4) {
  70. std::cout << "usage: hantenna.exe trans.inp cond.inp points.inp \n";
  71. exit(0);
  72. }
  73. std::vector<Real> Trans = readinpfile(std::string(argv[1]));
  74. std::vector<Real> CondMod = readinpfile(std::string(argv[2]));
  75. std::vector<Real> Points = readinpfile(std::string(argv[3]));
  76. //////////////////////////////////////
  77. // Define transmitter
  78. PolygonalWireAntenna* trans = PolygonalWireAntenna::New();
  79. trans->SetNumberOfPoints((int)(Trans[0]));
  80. int ip=1;
  81. for ( ; ip<=(int)(Trans[0])*2; ip+=2) {
  82. trans->SetPoint(ip/2, Vector3r (Trans[ip], Trans[ip+1], -1e-3));
  83. }
  84. trans->SetNumberOfFrequencies(1);
  85. trans->SetFrequency(0, Trans[ip]);
  86. trans->SetCurrent(Trans[ip+1]);
  87. // Receivers
  88. ReceiverPoints *receivers = ReceiverPoints::New();
  89. int nx = (int)Points[0];
  90. int ny = (int)Points[1];
  91. int nz = (int)Points[2];
  92. Real ox = Points[3];
  93. Real oy = Points[4];
  94. Real oz = Points[5];
  95. Vector3r loc;
  96. VectorXr dx(nx-1); // TODO map the dx, dy, dz vectors
  97. VectorXr dy(ny-1);
  98. VectorXr dz(nz-1);
  99. ip = 6;
  100. int ir = 0;
  101. for ( ; ip <6+nx-1; ++ip) {
  102. dx[ir] = Points[ip];
  103. ++ir;
  104. }
  105. ir = 0;
  106. for ( ; ip <6+ny-1+nx-1; ++ip) {
  107. dy[ir] = Points[ip];
  108. ++ir;
  109. }
  110. ir = 0;
  111. for ( ; ip <6+nz-1+ny-1+nx-1; ++ip) {
  112. dz[ir] = Points[ip];
  113. ++ir;
  114. }
  115. receivers->SetNumberOfReceivers(nx*ny*nz);
  116. ir = 0;
  117. Real pz = oz;
  118. for (int iz=0; iz<nz; ++iz) {
  119. Real py = oy;
  120. for (int iy=0; iy<ny; ++iy) {
  121. Real px = ox;
  122. for (int ix=0; ix<nx; ++ix) {
  123. loc << px, py, pz;
  124. receivers->SetLocation(ir, loc);
  125. if (ix < nx-1) px += dx[ix];
  126. ++ ir;
  127. }
  128. if (iy<ny-1) py += dy[iy];
  129. }
  130. if (iz<nz-1) pz += dz[iz];
  131. }
  132. ////////////////////////////////////
  133. // Define model
  134. LayeredEarthEM *earth = LayeredEarthEM::New();
  135. VectorXcr sigma;
  136. VectorXr thick;
  137. earth->SetNumberOfLayers(CondMod[0]+1);
  138. sigma.resize(CondMod[0]+1); sigma(0) = 0; // airlayer
  139. thick.resize(CondMod[0]-1);
  140. int ilay=1;
  141. for ( ; ilay/2<CondMod[0]-1; ilay+=2) {
  142. sigma(ilay/2+1) = 1./CondMod[ilay];
  143. thick(ilay/2) = CondMod[ilay+1];
  144. }
  145. sigma(ilay/2+1) = 1./ CondMod[ilay];
  146. earth->SetLayerConductivity(sigma);
  147. if (thick.size() > 0) earth->SetLayerThickness(thick);
  148. EMEarth1D *EmEarth = EMEarth1D::New();
  149. EmEarth->AttachWireAntenna(trans);
  150. EmEarth->AttachLayeredEarthEM(earth);
  151. EmEarth->AttachReceiverPoints(receivers);
  152. EmEarth->SetFieldsToCalculate(H);
  153. EmEarth->CalculateWireAntennaFields();
  154. #ifdef LEMMAUSEVTK
  155. vtkRenderer *renderer = vtkRenderer::New();
  156. vtkRenderWindow *renWin = vtkRenderWindow::New();
  157. vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  158. trans->ApproximateWithElectricDipoles( (VectorXr (3) << 0,0,0).finished() );
  159. vtkActor **dipActors = new vtkActor*[trans->GetNumberOfDipoles()];
  160. for (int id=0; id<trans->GetNumberOfDipoles(); ++id) {
  161. dipActors[id] = trans->GetVtkActor(id);
  162. renderer->AddActor(dipActors[id]);
  163. }
  164. vtkActor *hfield = receivers->GetVtkGlyphActor (HFIELDREAL, 3, 1e2, 0);
  165. vtkActor *hfieldi = receivers->GetVtkGlyphActor (HFIELDIMAG, 3, 1e2, 0);
  166. hfieldi->GetProperty()->SetColor(1,0,1);
  167. hfield->GetProperty()->SetColor(0,1,0);
  168. renderer->AddActor(hfield);
  169. renderer->AddActor(hfieldi);
  170. renderer->SetBackground(0,0,0);
  171. // Render the window
  172. renWin->AddRenderer(renderer);
  173. iren->SetRenderWindow(renWin);
  174. iren->Initialize();
  175. iren->Start();
  176. iren->Render();
  177. cout << "Enter File name?: ";
  178. std::string pngName;
  179. std::cin >> pngName;
  180. vtkPNGWriter *pngwrite = vtkPNGWriter::New();
  181. vtkRenderLargeImage *renlarge = vtkRenderLargeImage::New();
  182. renlarge->SetInput(renderer);
  183. renlarge->SetMagnification(2);
  184. pngwrite->SetInputConnection(renlarge->GetOutputPort());
  185. pngName.append(".png");
  186. pngwrite->SetFileName(pngName.c_str());
  187. pngwrite->Write();
  188. iren->Delete();
  189. renWin->Delete();
  190. renderer->Delete();
  191. #endif
  192. ////////////////////////////////////
  193. // Report
  194. std::fstream hreal("hfield.dat", std::ios::out);
  195. hreal << *trans << std::endl;
  196. hreal << *earth << std::endl;
  197. hreal << "Right hand coordinate system, z is positive down\n";
  198. hreal << "x[m]\ty[m]\tz[m]\tHx[A/m]\tHy[A/m]\tHz[A/m]\n";
  199. hreal.precision(8);
  200. int i=0;
  201. for (int iz=0; iz<nz; ++iz) {
  202. for (int iy=0; iy<ny; ++iy) {
  203. for (int ix=0; ix<nx; ++ix) {
  204. hreal << receivers->GetLocation(i).transpose() << "\t";
  205. hreal << std::real(receivers->GetHfield(0, i).transpose()[0]) << "\t";
  206. hreal << std::imag(receivers->GetHfield(0, i).transpose()[0]) << "\t";
  207. hreal << std::real(receivers->GetHfield(0, i).transpose()[1]) << "\t";
  208. hreal << std::imag(receivers->GetHfield(0, i).transpose()[1]) << "\t";
  209. hreal << std::real(receivers->GetHfield(0, i).transpose()[2]) << "\t";
  210. hreal << std::imag(receivers->GetHfield(0, i).transpose()[2]) << "\n";
  211. ++i;
  212. }
  213. }
  214. }
  215. hreal.close();
  216. // Clean up
  217. EmEarth->Delete();
  218. earth->Delete();
  219. receivers->Delete();
  220. trans->Delete();
  221. }
  222. std::vector<Real> readinpfile(const std::string& fname) {
  223. std::string buf;
  224. char dump[255];
  225. std::vector<Real> vals;
  226. std::fstream input(fname.c_str(), std::ios::in);
  227. if (input.fail()) {
  228. std::cerr << "Input file " << fname << " failed to open\n";
  229. exit(EXIT_FAILURE);
  230. }
  231. while (input >> buf) {
  232. if (buf.substr(0,2) == "//") {
  233. input.getline(dump, 255);
  234. } else {
  235. vals.push_back( atof(buf.c_str() ));
  236. }
  237. }
  238. return vals;
  239. }