GPLIB++
mtanalys.cpp
Go to the documentation of this file.
1 #include "C1DMTObjective.h"
2 #include "GradFunc.h"
3 #include "convert.h"
4 #include "C1DMTSynthData.h"
5 #include "netcdfcpp.h"
6 #include <iostream>
7 #include <fstream>
8 #include <boost/bind.hpp>
9 #include "VecMat.h"
10 #include "Adaptors.h"
11 #include <algorithm>
12 #include <string>
13 #include "NetCDFTools.h"
14 #include "MTFitSetup.h"
15 #include "C1dInvGaConf.h"
16 #include "NumUtil.h"
17 
18 using namespace std;
19 
20 int main()
21  {
22  C1dInvGaConf Configuration;
23  Configuration.GetData("1dinvga.conf");
24  CMTStation MTData;
25  string datanamebase;
26  cout << "Data name base: ";
27  cin >> datanamebase;
28  MTData.GetData(datanamebase + ".mtt");
29 
30  C1DMTObjective MTObjective(MTData);
31  SetupMTFitParameters(Configuration, MTObjective);
32 
33  string modelnamebase;
34  cout << "Model name base: ";
35  cin >> modelnamebase;
36  C1DMTSynthData MTModel;
37  MTModel.readmodel(modelnamebase + "_mt.mod");
38 
39  gplib::rvec MTModelVector = MTModel.GetModelVector();
40  const size_t nmodelparms = MTModelVector.size();
41  gplib::rvec varfactor(nmodelparms);
42  double MTOldRMS = MTObjective.CalcPerformance(MTModelVector);
43  cout << "RMS: " << MTOldRMS << endl;
44  gplib::rvec OldData(MTObjective.GetSynthData());
45  for (size_t i = 0; i < nmodelparms; ++i)
46  {
47  varfactor(i) = 1.0;
48  }
49  ModelAnalysis MTAnalyser(MTObjective);
50  MTAnalyser.SetModel(MTModelVector);
51  MTAnalyser.SetVariationFactor(varfactor);
52  ofstream evals("mtevals.plot");
53  gplib::rvec svals = MTAnalyser.GetSingularValues();
54  gplib::rvec svalratios = svals;
55  svalratios /= svals(0); // determine ratios to larges eigenvalue
56  cout << "MT Singular Values : " << svals << endl;
57  //cout << "Relative SVD Threshold: ";
58  //double svdthreshold = 0.0;
59  //cin >> svdthreshold;
60 
61  gplib::rvec modelerrors(nmodelparms);
62  modelerrors *= 0.0;
63  const double errorratio = 0.1;
64  gplib::rvec parameterthresholds = MTModelVector;
65  for (size_t i = 0; i < nmodelparms / 2; ++i)
66  parameterthresholds(i) = pow(10, parameterthresholds(i));
67  parameterthresholds *= errorratio;
68  size_t eigenvindex = 0;
69 
70  C1DMTObjective::datafuncvector_t ErrorFunctions =
71  MTObjective.GetErrorFunctions();
72  const size_t nerrorfuncs = ErrorFunctions.size();
73  const size_t ndata = MTData.GetMTData().size();
74  gplib::rmat DataCovar(nerrorfuncs * ndata, nerrorfuncs * ndata);
75  fill_n(DataCovar.data().begin(), DataCovar.size1() * DataCovar.size2(), 0.0);
76  ;
77  for (size_t e = 0; e < nerrorfuncs; ++e)
78  {
79  for (int i = 0; i < ndata; ++i)
80  {
81  DataCovar(i + e * ndata, i + e * ndata) = gplib::pow2(
82  ErrorFunctions.at(e)(&MTData.GetMTData().at(i)));
83  }
84  }
85  ofstream mterrors("mterrors");
86  gplib::rmat ModelCovar = MTAnalyser.GetModelCovariance(DataCovar);
87  std::vector<double> reserr(nmodelparms / 2, 0.0), thickerr(nmodelparms / 2,
88  0.0);
89  const unsigned int nevals = svalratios.size();
90  while (sum(modelerrors) < sum(parameterthresholds) && eigenvindex < nevals)
91  {
92  cout << "Accum Model Errors: " << sum(modelerrors) << endl;
93  cout << "Accum Threshold Errors: " << sum(parameterthresholds) << endl;
94  MTAnalyser.SetRelativeSVDThreshold(svalratios(eigenvindex));
95  ModelCovar = MTAnalyser.GetModelCovariance(DataCovar);
96  double currerr = 0;
97  for (size_t i = 0; i < MTModel.GetThicknesses().size(); ++i)
98  {
99  currerr = 2.3025 * MTModel.GetResistivities().at(i) * sqrt(
100  ModelCovar(i, i));
101  mterrors << "Res " << i << " : " << currerr << endl;
102  reserr.at(i) = currerr;
103  modelerrors(i) = currerr;
104  }
105  for (size_t i = MTModel.GetThicknesses().size(); i < 2
106  * MTModel.GetThicknesses().size(); ++i)
107  {
108  currerr = sqrt(ModelCovar(i, i));
109  mterrors << "Th " << i - MTModel.GetThicknesses().size() << " : "
110  << currerr << endl;
111  modelerrors(i) = currerr;
112  thickerr.at(i - MTModel.GetThicknesses().size()) = currerr;
113  }
114  ++eigenvindex;
115  }
116  cout << "Final Accum Model Errors: " << sum(modelerrors) << endl;
117  cout << "Accum Threshold Errors: " << sum(parameterthresholds) << endl;
118  WriteMatrixAsNetCDF("mtcovar.nc", ModelCovar);
119  WriteMatrixAsNetCDF("mtcorr.nc", MTAnalyser.GetModelCorrelation(DataCovar));
120  WriteMatrixAsNetCDF("mtsens.nc", MTAnalyser.GetSensitivityMatrix());
121  WriteMatrixAsNetCDF("mtres.nc", MTAnalyser.GetModelResolution());
122  WriteMatrixAsNetCDF("mtnull.nc", MTAnalyser.GetModelNullspace());
123  WriteMatrixAsNetCDF("mtsinv.nc", MTAnalyser.GetSensitivityInverse());
124  WriteMatrixAsNetCDF("senscheck.nc", prod(
125  MTAnalyser.GetSensitivityInverse(), MTAnalyser.GetSensitivityMatrix()));
126 
127  MTModel.SetResistivityErrors(reserr);
128  MTModel.SetThicknessErrors(thickerr);
129  MTModel.writeplot("mterr.plot");
130  }
131 
gplib::rmat GetModelCorrelation(const gplib::rmat &DataCovar)
Definition: GradFunc.cpp:181
void SetupMTFitParameters(const tConfObject &Configuration, C1DMTObjective &Objective)
Definition: MTFitSetup.h:8
gplib::rmat GetModelNullspace()
Definition: GradFunc.cpp:165
void SetVariationFactor(const gplib::rvec varfac)
Definition: GradFunc.h:41
gplib::rvec GetSingularValues()
Definition: GradFunc.cpp:201
boost::shared_ptr< PlottableObjective > MTObjective
Definition: cadianiso.cpp:16
void SetModel(const gplib::rvec &TheModel)
Definition: GradFunc.cpp:87
CMTStation MTData
Definition: cadijoint.cpp:15
gplib::rmat GetModelCovariance(const gplib::rmat &DataCovar)
The calculation of the Model Covariance is based on Menke, p. 122 formula 7.37.
Definition: GradFunc.cpp:172
const gplib::rmat & GetSensitivityMatrix()
Definition: GradFunc.h:51
void SetRelativeSVDThreshold(const double t)
Definition: GradFunc.h:46
int main()
Definition: mtanalys.cpp:20
gplib::rmat GetModelResolution()
Definition: GradFunc.cpp:155
The class ModelAnalysis is used to calculate resolution matrix, nullspace and other parameters for mo...
Definition: GradFunc.h:20
CLevanisoConf Configuration
Definition: cadianiso.cpp:17
const gplib::rmat & GetSensitivityInverse() const
Definition: GradFunc.h:56