GPLIB++
C1DMTSynthData.cpp
Go to the documentation of this file.
1 #include "C1DMTSynthData.h"
2 #include <fstream>
3 #include <algorithm>
4 #include <cassert>
5 #include "FatalException.h"
6 
7 using namespace std;
8 
9 namespace gplib
10  {
11  const int frequenzen = 50;
12  //these are the frequencies we calculate the response at, if no frequencies are set before the calculation
13  const float T[frequenzen] =
14  { 0.0025, 0.00308337, 0.00513901, 0.00625, 0.0104167, 0.0125, 0.0208333,
15  0.025, 0.0416667, 0.05, 0.0833333, 0.1, 0.166667, 0.2, 0.333333, 0.4,
16  0.667, 0.64, 1.06667, 1.29333, 2.15554, 2.59997, 4.33332, 5.19994,
17  8.66701, 10.666667, 16.000, 21.333, 32.000, 42.667, 64.0, 85.332,
18  127.992, 170.678, 256.016, 341.297, 512.033, 682.594, 1023.541,
19  1366.120, 2049.180, 2732.240, 4098.361, 5464.481, 8196.722,
20  10869.565, 16393.443, 28800, 43200, 86400 };
21 
22  C1DMTSynthData::C1DMTSynthData()
23  {
24  }
25 
26  C1DMTSynthData::~C1DMTSynthData()
27  {
28  }
29 
30  C1DMTSynthData::C1DMTSynthData(const C1DMTSynthData &old) :
31  MTStation(old), Z(old.Z), calc_frequencies(old.calc_frequencies),
32  resistivity(old.resistivity), thickness(old.thickness), reserrors(
33  old.reserrors), thickerrors(old.thickerrors)
34  {
35 
36  }
37  void C1DMTSynthData::WritePlot(std::string filename)
38  {
39  ofstream outfile(filename.c_str());
40  double cumthick = 0;
41  assert(resistivity.size() == thickness.size());
42 
43  for (unsigned int i = 0; i < thickness.size(); ++i)
44  {
45  outfile << cumthick << " " << resistivity.at(i);
46  if (!thickerrors.empty() || !reserrors.empty())
47  outfile << " " << thickerrors.at(i) << " " << reserrors.at(i);
48  outfile << endl;
49  cumthick += thickness.at(i);
50  outfile << cumthick << " " << resistivity.at(i);
51  if (!thickerrors.empty() || !reserrors.empty())
52  outfile << " " << thickerrors.at(i) << " " << reserrors.at(i);
53  outfile << endl;
54  }
55  }
56 
57  void C1DMTSynthData::WriteModel(string filename)
58  {
59  assert(resistivity.size() == thickness.size());
60  ofstream outfile(filename.c_str());
61 
62  outfile << thickness.size() << endl;
63  for (unsigned int i = 0; i < thickness.size(); ++i)
64  {
65  outfile << thickness.at(i) << " " << resistivity.at(i) << endl;
66  }
67  }
68 
70  {
71  assert(resistivity.size() == thickness.size());
72  const int length = resistivity.size();
73  gplib::rvec result(length * 2);
74  for (int i = 0; i < length; ++i)
75  {
76  result(i) = log10(resistivity.at(i));
77  result(i + length) = thickness.at(i);
78  }
79  return result;
80  }
81 
82  void C1DMTSynthData::ReadModel(string filename)
83  {
84  int layers;
85  double currthick, curres;
86  int i = 0;
87 
88  ifstream infile(filename.c_str());
89  infile >> layers;
90  if (!infile.good() || layers < 0)
91  throw FatalException("Problem reading file: " + filename);
92  thickness.reserve(layers);
93  resistivity.reserve(layers);
94  while (infile.good())
95  {
96  infile >> currthick >> curres;
97  if (infile.good())
98  {
99  thickness.push_back(currthick);
100  resistivity.push_back(curres);
101  ++i;
102  }
103  }
104  assert(resistivity.size() == thickness.size());
105  assert(i==layers);
106  }
107  //fill data member variables with data from Cagniard algorithm
109 
110  {
111  assert(resistivity.size() == thickness.size());
112  calc_frequencies = GetFrequencies();
113  if (calc_frequencies.empty())
114  {
115  for (int i = 0; i < frequenzen; ++i)
116  calc_frequencies.push_back(1.0 / T[i]);
117  }
118  Calc();
119  Assign(calc_frequencies.size());
120  for (unsigned int i = 0; i < calc_frequencies.size(); ++i)
121  {
122  MTData.at(i).frequency = calc_frequencies.at(i);
123  MTData.at(i).Zxy = Z.at(i);
124  MTData.at(i).Zyx = -Z.at(i);
125  }
126  Update();
127  }
128 
129  void C1DMTSynthData::Calc()
130  {
131  dcomp omegamu;
132  dcomp kcurr, klow;
133  dcomp xi;
134  vector<dcomp> alpha(thickness.size(), 0);
135  dcomp adm;
136  double d;
137  double sigmacurr, sigmalow;
138  Z.clear();
139  for (unsigned int i = 0; i < calc_frequencies.size(); ++i)
140  {
141  omegamu = -I * 8e-7 * PI * PI * calc_frequencies.at(i);
142  fill(alpha.begin(), alpha.end(), 0);
143  sigmacurr = 1.0 / resistivity.back();
144  kcurr = sqrt(omegamu * sigmacurr);
145  for (int layerindex = thickness.size() - 2; layerindex >= 0; --layerindex)
146  {
147  sigmalow = 1.0 / resistivity.at(layerindex + 1);
148  sigmacurr = 1.0 / resistivity.at(layerindex);
149  kcurr = sqrt(omegamu * sigmacurr);
150  klow = sqrt(omegamu * sigmalow);
151  if (kcurr.real() < 0.0)
152  {
153  kcurr *= -1.0;
154  klow *= -1.0;
155  }
156  xi = omegamu * (sigmacurr - sigmalow) / std::pow(kcurr + klow, 2);
157  alpha.at(layerindex + 1) = (xi + alpha.at(layerindex + 1)) / (1. + xi * alpha.at(
158  layerindex + 1));
159  d = thickness.at(layerindex) * 1000.0;
160  alpha.at(layerindex) = alpha.at(layerindex + 1) * exp(-2. * kcurr * d);
161  }
162  adm = kcurr / (-I * 2. * PI * calc_frequencies.at(i)) * ((1.
163  - alpha.at(0)) / (1. + alpha.at(0)));
164  Z.push_back(conj(1. / (1000.0 * adm)));
165 
166  }
167  }
168  }
gplib::rvec GetModelVector()
Return the model as a single vector first log10 of all resistivities, then all thicknesses in km...
const float T[frequenzen]
void WriteModel(std::string filename)
Write model into file for cagniard algorithm.
void ReadModel(std::string filename)
Read the model from a file.
The class MTStation is used to store the transfer functions and related information for a MT-site...
Definition: MTStation.h:17
CMTStation MTData
Definition: cadijoint.cpp:15
virtual void CalcSynthetic()
Calculate the synthetic data given the previously set parameters.
const int frequenzen
void Assign(const int nfreq)
Assign() assigns zero to all derived quantities, this makes the calculation.
Definition: MTStation.cpp:74
void WritePlot(std::string filename)
Write out a file that can be used for plotting with xmgrace first column depth, second column resisti...
const float T[frequenzen]
Definition: mtinvnet.cpp:18
Calculate synthetic MT data for a 1D model using Cagniard's algorithm.
trealdata GetFrequencies() const
return the available frequencies in a single vector
Definition: MTStation.cpp:136
const int frequenzen
Definition: mtinvnet.cpp:17
The basic exception class for all errors that arise in gplib.
void Update()
Update all derived quantities.
Definition: MTStation.cpp:84