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.

PolygonalWireAntenna.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* This file is part of Lemma, a geophysical modelling and inversion API */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. @file
  7. @author Trevor Irons
  8. @date 05/18/2010
  9. @version $Id: PolygonalWireAntenna.cpp 211 2015-02-27 05:43:26Z tirons $
  10. **/
  11. #include "PolygonalWireAntenna.h"
  12. namespace Lemma {
  13. std::ostream &operator << (std::ostream &stream, const PolygonalWireAntenna &ob) {
  14. stream << ob.Serialize() << "\n---\n"; // End of doc --- as a direct stream should encapulste thingy
  15. return stream;
  16. }
  17. // ==================== LIFECYCLE =======================
  18. PolygonalWireAntenna::PolygonalWireAntenna( const ctor_key& key ) : WireAntenna( WireAntenna::ctor_key() ), minDipoleRatio(.15),
  19. minDipoleMoment(1e-6), maxDipoleMoment(1e1), rRepeat(1e10,1e10,1e10) {
  20. Points.setZero();
  21. //rRepeat.setOnes();
  22. }
  23. PolygonalWireAntenna::PolygonalWireAntenna( const YAML::Node& node, const ctor_key& ) : WireAntenna(node, WireAntenna::ctor_key() ) {
  24. minDipoleRatio = node["minDipoleRatio"].as<Real>();
  25. maxDipoleMoment = node["maxDipoleMoment"].as<Real>();
  26. minDipoleMoment = node["minDipoleMoment"].as<Real>();
  27. }
  28. PolygonalWireAntenna::~PolygonalWireAntenna() {
  29. }
  30. //--------------------------------------------------------------------------------------
  31. // Class: PolygonalWireAntenna
  32. // Method: Serialize
  33. //--------------------------------------------------------------------------------------
  34. YAML::Node PolygonalWireAntenna::Serialize ( ) const {
  35. YAML::Node node = WireAntenna::Serialize();
  36. node.SetTag( this->GetName() );
  37. node["minDipoleRatio"] = minDipoleRatio;
  38. node["maxDipoleMoment"] = maxDipoleMoment;
  39. node["minDipoleMoment"] = minDipoleMoment;
  40. return node;
  41. } // ----- end of method PolygonalWireAntenna::Serialize -----
  42. //--------------------------------------------------------------------------------------
  43. // Class: WireAntenna
  44. // Method: DeSerialize
  45. //--------------------------------------------------------------------------------------
  46. std::shared_ptr<PolygonalWireAntenna> PolygonalWireAntenna::DeSerialize ( const YAML::Node& node ) {
  47. if (node.Tag() != "PolygonalWireAntenna") {
  48. throw DeSerializeTypeMismatch( "PolygonalWireAntenna", node.Tag());
  49. }
  50. return std::make_shared<PolygonalWireAntenna> ( node, ctor_key() );
  51. } // ----- end of method WireAntenna::DeSerialize -----
  52. std::shared_ptr<PolygonalWireAntenna> PolygonalWireAntenna::NewSP() {
  53. return std::make_shared<PolygonalWireAntenna>( ctor_key() );
  54. }
  55. std::shared_ptr<WireAntenna> PolygonalWireAntenna::Clone() const {
  56. auto copy = PolygonalWireAntenna::NewSP();
  57. copy->minDipoleRatio = this->minDipoleRatio;
  58. copy->minDipoleMoment = this->minDipoleMoment;
  59. copy->maxDipoleMoment = this->maxDipoleMoment;
  60. copy->NumberOfPoints = this->NumberOfPoints;
  61. copy->Freqs = this->Freqs;
  62. copy->Current = this->Current;
  63. copy->NumberOfTurns = this->NumberOfTurns;
  64. copy->Points = this->Points;
  65. //copy->Dipoles = this->Dipoles; // no, disaster
  66. return copy;
  67. }
  68. std::shared_ptr<PolygonalWireAntenna> PolygonalWireAntenna::ClonePA() const {
  69. auto copy = PolygonalWireAntenna::NewSP();
  70. copy->minDipoleRatio = this->minDipoleRatio;
  71. copy->minDipoleMoment = this->minDipoleMoment;
  72. copy->maxDipoleMoment = this->maxDipoleMoment;
  73. copy->NumberOfPoints = this->NumberOfPoints;
  74. copy->Freqs = this->Freqs;
  75. copy->Current = this->Current;
  76. copy->NumberOfTurns = this->NumberOfTurns;
  77. copy->Points = this->Points;
  78. //copy->Dipoles = this->Dipoles; // no, disaster
  79. return copy;
  80. }
  81. void PolygonalWireAntenna::SetMinDipoleRatio (const Real& ratio) {
  82. minDipoleRatio = ratio;
  83. }
  84. void PolygonalWireAntenna::SetMinDipoleMoment (const Real& m) {
  85. minDipoleMoment = m;
  86. }
  87. void PolygonalWireAntenna::SetMaxDipoleMoment (const Real& m) {
  88. maxDipoleMoment = m;
  89. }
  90. // ==================== OPERATIONS =======================
  91. void PolygonalWireAntenna::ApproximateWithElectricDipoles(const Vector3r &rp) {
  92. // Only resplit if necessary. Save a few cycles if repeated
  93. if ( (rRepeat-rp).norm() > 1e-16 ) {
  94. Dipoles.clear();
  95. // loop over all segments
  96. for (int iseg=0; iseg<NumberOfPoints-1; ++iseg) {
  97. InterpolateLineSegment(Points.col(iseg), Points.col(iseg+1), rp);
  98. }
  99. rRepeat = rp;
  100. } else {
  101. for (unsigned int id=0; id<Dipoles.size(); ++id) {
  102. Dipoles[id]->SetFrequencies(Freqs);
  103. }
  104. }
  105. }
  106. Vector3r PolygonalWireAntenna::ClosestPointOnLine(const Vector3r &p1,
  107. const Vector3r &p2, const Vector3r &tp) {
  108. Vector3r v1 = p2 - p1;
  109. Vector3r v2 = p1 - tp;
  110. Vector3r v3 = p1 - p2;
  111. Vector3r v4 = p2 - tp;
  112. Real dot1 = v2.dot(v1);
  113. Real dot2 = v1.dot(v1);
  114. Real dot3 = v4.dot(v3);
  115. Real dot4 = v3.dot(v3);
  116. Real t1 = -1.*dot1/dot2;
  117. Real t2 = -1.*dot3/dot4;
  118. Vector3r pos = p1+v1*t1 ;
  119. // check if on line
  120. // else give back the closest end point
  121. if ( t1>=0 && t2>=0. ) {
  122. return pos;
  123. } else if (t1<0) {
  124. return p1;
  125. } else {
  126. return p2;
  127. }
  128. }
  129. void PolygonalWireAntenna::PushXYZDipoles(const Vector3r &step,
  130. const Vector3r &cp, const Vector3r &dir,
  131. std::vector< std::shared_ptr<DipoleSource> > &xDipoles) {
  132. Real scale = (Real)(NumberOfTurns)*Current;
  133. auto tx = DipoleSource::NewSP();
  134. tx->SetLocation(cp);
  135. tx->SetType(UNGROUNDEDELECTRICDIPOLE);
  136. tx->SetPolarisation(dir);
  137. tx->SetFrequencies(Freqs);
  138. tx->SetMoment(scale*step.norm());
  139. xDipoles.push_back(tx);
  140. }
  141. void PolygonalWireAntenna::CorrectOverstepXYZDipoles(const Vector3r &step,
  142. const Vector3r &cp, const Vector3r &dir,
  143. std::vector< std::shared_ptr<DipoleSource> > &xDipoles ) {
  144. Real scale = (Real)(NumberOfTurns)*Current;
  145. // X oriented dipoles
  146. if (step.norm() > minDipoleMoment) {
  147. xDipoles[xDipoles.size()-1]->SetLocation(cp);
  148. xDipoles[xDipoles.size()-1]->SetMoment(scale*step.norm());
  149. }
  150. }
  151. void PolygonalWireAntenna::InterpolateLineSegment(const Vector3r &p1,
  152. const Vector3r &p2, const Vector3r & tp) {
  153. Vector3r phat = (p1-p2).array() / (p1-p2).norm();
  154. Vector3r c = this->ClosestPointOnLine(p1, p2, tp);
  155. Real dtp = (tp-c).norm(); // distance to point at c
  156. Real dc1 = (p1-c).norm(); // distance to c from p1
  157. Real dc2 = (p2-c).norm(); // distance to c from p1
  158. // unit vector
  159. Vector3r cdir = (p2-p1).array() / (p2-p1).norm();
  160. ///////////////////
  161. // dipoles for this segment
  162. std::vector< std::shared_ptr<DipoleSource> > xDipoles;
  163. // go towards p1
  164. if ( ((c-p1).array().abs() > minDipoleMoment).any() ) {
  165. // cp = current pos, lp = last pos
  166. Vector3r cp = c + phat*(dtp*minDipoleRatio)*.5;
  167. Vector3r lp = c;
  168. Real dist = (cp-p1).norm();
  169. Real dist_old = dist+1.;
  170. // check that we didn't run past the end, or that we aren't starting at
  171. // the end, or that initial step runs over!
  172. Vector3r dir = (p1-cp).array() / (p1-cp).norm(); // direction of movement
  173. Vector3r step = phat*(dtp*minDipoleRatio);
  174. Vector3r stepold = Vector3r::Zero();
  175. // (dir-phat) just shows if we are stepping towards or away from p1
  176. while (dist < dist_old && (dir-phat).norm() < 1e-8) {
  177. PushXYZDipoles(step, cp, cdir, xDipoles);
  178. // Make 1/2 of previous step, 1/2 of this step, store this step
  179. stepold = step;
  180. step = phat*( (cp-tp).norm()*minDipoleRatio );
  181. while ( (step.array().abs() > maxDipoleMoment).any() ) {
  182. step *= .5;
  183. }
  184. lp = cp;
  185. cp += .5*stepold + .5*step;
  186. dist = (cp-p1).norm();
  187. dir = (p1-cp).array() / (p1-cp).norm();
  188. }
  189. // cp now points to end last of dipole moments
  190. cp -= .5*step;
  191. // Fix last dipole position, so that entire wire is represented,
  192. // and no more
  193. Real distLastSeg = (c - cp).norm();
  194. if (distLastSeg + minDipoleMoment < dc1) {
  195. // case 1: understep, add dipole
  196. step = (p1-cp).array();
  197. cp += .5*step;
  198. PushXYZDipoles(step, cp, cdir, xDipoles);
  199. } else if (distLastSeg > dc1 + minDipoleMoment) {
  200. // case 2: overstep, reposition dipole and size
  201. step = (p1 - (lp-.5*stepold));
  202. cp = (lp-.5*stepold) + (.5*step);
  203. CorrectOverstepXYZDipoles(step, cp, cdir, xDipoles);
  204. }
  205. // else case 0: nearly 'perfect' fit do nothing
  206. }
  207. // go towards p2
  208. if ( ( (c-p2).array().abs() > minDipoleMoment).any() ) {
  209. // cp = current pos, lp = last pos
  210. Vector3r step = -phat*(dtp*minDipoleRatio);
  211. while ( (step.array().abs() > maxDipoleMoment).any() ) {
  212. step *= .5;
  213. }
  214. Vector3r cp = c + step*.5;
  215. Vector3r lp = c;
  216. Real dist = (p2-cp).norm();
  217. Real dist_old = dist+1e3;
  218. // check that we didn't run past the end, or that we aren't starting at
  219. // the end, or that initial step runs over!
  220. Vector3r dir = (p2-cp).array() / (p2-cp).norm(); // direction of movement
  221. Vector3r stepold = Vector3r::Zero();
  222. // (dir-phat) just shows if we are stepping towards or away from p1
  223. while (dist < dist_old && (dir+phat).norm() < 1e-8) {
  224. PushXYZDipoles(step, cp, cdir, xDipoles);
  225. // Make 1/2 of previous step, 1/2 of this step, store this step
  226. stepold = step;
  227. step = -phat*( (cp-tp).norm()*minDipoleRatio );
  228. while ( (step.array().abs() > maxDipoleMoment).any() ) {
  229. step *= .5;
  230. }
  231. lp = cp;
  232. cp += .5*stepold + .5*step;
  233. dist = (cp-p2).norm();
  234. dir = (p2-cp).array() / (p2-cp).norm();
  235. }
  236. // cp now points to end last of dipole moments
  237. cp -= .5*step;
  238. // Fix last dipole position, so that entire wire is represented,
  239. // and no more
  240. Real distLastSeg = (c - cp).norm();
  241. if (distLastSeg + minDipoleMoment < dc2) {
  242. // case 1: understep, add dipole
  243. step = (p2-cp).array();
  244. cp += .5*step;
  245. PushXYZDipoles(step, cp, cdir, xDipoles);
  246. } else if (distLastSeg > dc2 + minDipoleMoment) {
  247. // case 2: overstep, reposition dipole and size
  248. step = (p2 - (lp-.5*stepold));
  249. cp = (lp-.5*stepold) + (.5*step);
  250. CorrectOverstepXYZDipoles(step, cp, cdir, xDipoles);
  251. }
  252. // else case 0: nearly 'perfect' fit do nothing
  253. }
  254. Dipoles.insert(Dipoles.end(), xDipoles.begin(), xDipoles.end());
  255. }
  256. }