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.

LayeredEarth.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 06/24/2009
  9. @version $Id: layeredearth.cpp 210 2015-02-25 02:57:03Z tirons $
  10. **/
  11. #include "LayeredEarth.h"
  12. namespace Lemma {
  13. // ==================== FRIENDS ======================
  14. /*
  15. std::ostream &operator<<(std::ostream &stream,
  16. const LayeredEarth &ob) {
  17. stream << *(EarthModel*)(&ob);
  18. //stream << "Class Name : "<< ob.Name << "\n";
  19. stream << "Number of Layers "<< ob.NumberOfLayers << "\n";
  20. return stream;
  21. }
  22. */
  23. std::ostream &operator << (std::ostream &stream, const LayeredEarth &ob) {
  24. stream << ob.Serialize() << "\n---\n"; // End of doc --- as a direct stream should encapulste thingy
  25. return stream;
  26. }
  27. // ==================== LIFECYCLE ===================================
  28. LayeredEarth::LayeredEarth( ) : EarthModel( ),
  29. NumberOfLayers(0), NumberOfInterfaces(0) {
  30. }
  31. LayeredEarth::~LayeredEarth() {
  32. }
  33. LayeredEarth::LayeredEarth(const YAML::Node& node) : EarthModel(node)
  34. {
  35. NumberOfLayers = node["NumberOfLayers"].as<int>();
  36. NumberOfInterfaces = node["NumberOfInterfaces"].as<int>();
  37. LayerThickness = node["LayerThickness"].as<VectorXr>();
  38. }
  39. YAML::Node LayeredEarth::Serialize() const {
  40. YAML::Node node = EarthModel::Serialize();
  41. node["NumberOfLayers"] = NumberOfLayers;
  42. node["NumberOfInterfaces"] = NumberOfInterfaces;
  43. node["LayerThickness"] = LayerThickness;
  44. node.SetTag( this->GetName() );
  45. return node;
  46. }
  47. // ==================== OPERATIONS ===================================
  48. // ==================== ACCESS ===================================
  49. void LayeredEarth::SetLayerThickness(const VectorXr &thick) {
  50. if (thick.size() != this->NumberOfLayers - 2)
  51. throw EarthModelParametersDoNotMatchNumberOfLayers( );
  52. LayerThickness = thick;
  53. }
  54. // ==================== INQUIRY ===================================
  55. int LayeredEarth::GetNumberOfLayers () {
  56. return this->NumberOfLayers;
  57. }
  58. int LayeredEarth::GetNumberOfNonAirLayers () {
  59. return this->NumberOfLayers - 1;
  60. }
  61. Real LayeredEarth::GetLayerThickness(const int &ilay) {
  62. // Take into account infinite top and bottom layers
  63. // estimate infinity by 1000 m
  64. if (ilay < 0 || ilay > NumberOfLayers - 1) {
  65. throw RequestForNonValidEarthModelParameter( );
  66. } else if (ilay == 0) {
  67. return 1000.;
  68. } else if (ilay == NumberOfLayers - 1) {
  69. return 1000.;
  70. } else {
  71. return this->LayerThickness(ilay-1);
  72. }
  73. }
  74. Real LayeredEarth::GetLayerDepth(const int &ilay) {
  75. Real depth = 0;
  76. if (ilay == 0) {
  77. return depth;
  78. } else {
  79. for (int i=1; i<=ilay; ++i) {
  80. depth += GetLayerThickness(i);
  81. }
  82. }
  83. return depth;
  84. }
  85. int LayeredEarth::GetLayerAtThisDepth(const Real& depth) {
  86. if (depth <= 0 || NumberOfLayers < 2) {
  87. return 0;
  88. }
  89. Real laydep = 0;
  90. for (int ilay=0; ilay<NumberOfLayers-2; ++ilay) {
  91. laydep += LayerThickness[ilay];
  92. if (laydep >= depth) { return ilay+1; }
  93. }
  94. return NumberOfLayers-1;
  95. }
  96. EarthModelWithLessThanTwoLayers::EarthModelWithLessThanTwoLayers() :
  97. runtime_error( "EARTH MODEL WITH LESS THAN TWO LAYERS") { }
  98. EarthModelWithMoreThanMaxLayers::EarthModelWithMoreThanMaxLayers() :
  99. runtime_error( "EARTH MODEL WITH MORE THAN MAX LAYERS") { }
  100. EarthModelParametersDoNotMatchNumberOfLayers::
  101. EarthModelParametersDoNotMatchNumberOfLayers( ) :
  102. runtime_error( "EARTH MODEL PARAMETERS DO NOT MATCH NUMBER OF LAYERS")
  103. {}
  104. RequestForNonValidEarthModelParameter::
  105. RequestForNonValidEarthModelParameter() :
  106. runtime_error( "REQUEST FOR NON VALID EARTH MODEL PARAMETER") {}
  107. }