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.8KB

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. std::ostream &operator<<(std::ostream &stream,
  15. const LayeredEarth &ob) {
  16. stream << *(EarthModel*)(&ob);
  17. //stream << "Class Name : "<< ob.Name << "\n";
  18. stream << "Number of Layers "<< ob.NumberOfLayers << "\n";
  19. return stream;
  20. }
  21. // ==================== LIFECYCLE ===================================
  22. LayeredEarth::LayeredEarth(const std::string&name) :
  23. EarthModel(name),
  24. NumberOfLayers(0), NumberOfInterfaces(0) {
  25. }
  26. LayeredEarth::~LayeredEarth() {
  27. if (NumberOfReferences != 0) {
  28. throw DeleteObjectWithReferences(this);
  29. }
  30. }
  31. #ifdef HAVE_YAMLCPP
  32. LayeredEarth::LayeredEarth(const YAML::Node& node) : EarthModel(node)
  33. {
  34. NumberOfLayers = node["NumberOfLayers"].as<int>();
  35. NumberOfInterfaces = node["NumberOfInterfaces"].as<int>();
  36. LayerThickness = node["LayerThickness"].as<VectorXr>();
  37. }
  38. YAML::Node LayeredEarth::Serialize() const {
  39. YAML::Node node = EarthModel::Serialize();
  40. node["NumberOfLayers"] = NumberOfLayers;
  41. node["NumberOfInterfaces"] = NumberOfInterfaces;
  42. node["LayerThickness"] = LayerThickness;
  43. node.SetTag( Name );
  44. return node;
  45. }
  46. #endif
  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. }