GPLIB++
recanalys.cpp
Go to the documentation of this file.
1 #include "C1DRecObjective.h"
2 #include "GradFunc.h"
3 #include "convert.h"
4 #include "SeismicDataComp.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 "ResPkModel.h"
15 #include "C1dInvGaConf.h"
16 #include "MatNorm.h"
17 #include "NumUtil.h"
18 
19 using namespace std;
20 
21 int main()
22  {
23  SeismicDataComp RecData;
24 
25  string datanamebase;
26  cout << "Data name base: ";
27  cin >> datanamebase;
28  RecData.GetData(datanamebase + ".rec");
29 
30  C1dInvGaConf Configuration;
31  Configuration.GetData("1dinvga.conf");
32 
33  C1DRecObjective RecObjective(RecData, Configuration.shift,
34  Configuration.omega, Configuration.sigma, Configuration.cc,
35  Configuration.slowness);
36 
37  string modelnamebase;
38  cout << "Model name base: ";
39  cin >> modelnamebase;
40  ResPkModel RecModel;
41  RecModel.GetData(modelnamebase + "_rec.mod");
42 
43  const size_t nmodelparms = RecModel.SVelocity.size() * 2;
44  gplib::rvec RecModelVector(nmodelparms);
45  for (int i = 0; i < nmodelparms / 2; ++i)
46  {
47  RecModelVector(i) = RecModel.Thickness.at(i);
48  RecModelVector(i + nmodelparms / 2) = RecModel.SVelocity.at(i);
49  }
50 
51  gplib::rvec varfactor(nmodelparms);
52  RecObjective.SetPoisson(Configuration.poisson);
53  RecObjective.SetTimeWindow(Configuration.starttime, Configuration.endtime);
54  RecObjective.SetErrorLevel(Configuration.recerror);
55  RecObjective.SetFitExponent(Configuration.recfitexponent);
56  double RecOldRMS = RecObjective.CalcPerformance(RecModelVector);
57  cout << "RMS: " << RecOldRMS << endl;
58  gplib::rvec OldData(RecObjective.GetSynthData());
59  for (size_t i = 0; i < nmodelparms; ++i)
60  {
61  varfactor(i) = 1.0;
62  }
63 
64  ModelAnalysis RecAnalyser(RecObjective);
65  RecAnalyser.SetModel(RecModelVector);
66  RecAnalyser.SetVariationFactor(varfactor);
67  ofstream evals("mtevals.plot");
68  gplib::rvec svals = RecAnalyser.GetSingularValues();
69  gplib::rvec svalratios = svals;
70  svalratios /= svals(0); // determine ratios to larges eigenvalue
71  cout << "MT Singular Values : " << svals << endl;
72  //cout << "Relative SVD Threshold: ";
73  //double svdthreshold = 0.0;
74  //cin >> svdthreshold;
75 
76  gplib::rvec modelerrors(nmodelparms);
77  fill_n(modelerrors.begin(), nmodelparms, 0.0);
78  const double errorratio = 0.1;
79  gplib::rvec parameterthresholds = RecModelVector;
80  for (size_t i = 0; i < nmodelparms; ++i)
81  parameterthresholds(i) *= varfactor(i);
82 
83  parameterthresholds *= errorratio;
84  size_t eigenvindex = 0;
85 
86  const size_t ndata = RecData.GetData().size();
87  gplib::rmat DataCovar(ndata, ndata);
88  fill_n(DataCovar.data().begin(), DataCovar.size1() * DataCovar.size2(), 0.0);
89 
90  for (int i = 0; i < ndata; ++i)
91  {
92  DataCovar(i, i) = gplib::pow2(RecData.GetData().at(i)
93  * Configuration.recerror);
94  }
95 
96  ofstream recerrors("recerrors");
97  gplib::rmat ModelCovar = RecAnalyser.GetModelCovariance(DataCovar);
98 
99  while (sum(modelerrors) < sum(parameterthresholds))
100  {
101  cout << "Ratio: " << svalratios(eigenvindex) << endl;
102  RecAnalyser.SetRelativeSVDThreshold(svalratios(eigenvindex));
103  cout << "Accum Model Errors: " << sum(modelerrors) << endl;
104  cout << "Accum Threshold Errors: " << sum(parameterthresholds) << endl;
105 
106  ModelCovar = RecAnalyser.GetModelCovariance(DataCovar);
107  double currerr = 0;
108  for (size_t i = 0; i < nmodelparms; ++i)
109  {
110  currerr = sqrt(ModelCovar(i, i));
111  recerrors << i << " : " << currerr << endl;
112  modelerrors(i) = currerr;
113  }
114  ++eigenvindex;
115  }
116  std::vector<double> thickerr, svelerr;
117  for (int i = 0; i < nmodelparms / 2; ++i)
118  {
119  thickerr.push_back(modelerrors(i));
120  svelerr.push_back(modelerrors(i + nmodelparms / 2));
121  }
122  RecModel.SetThickErrors(thickerr);
123  RecModel.SetSVelErrors(svelerr);
124  cout << "Final Accum Model Errors: " << sum(modelerrors) << endl;
125  cout << "Accum Threshold Errors: " << sum(parameterthresholds) << endl;
126  WriteMatrixAsNetCDF("reccovar.nc", ModelCovar);
127  WriteMatrixAsNetCDF("reccorr.nc",
128  RecAnalyser.GetModelCorrelation(DataCovar));
129  WriteMatrixAsNetCDF("recsens.nc", RecAnalyser.GetSensitivityMatrix());
130  WriteMatrixAsNetCDF("recres.nc", RecAnalyser.GetModelResolution());
131  WriteMatrixAsNetCDF("recnull.nc", RecAnalyser.GetModelNullspace());
132  WriteMatrixAsNetCDF("recsinv.nc", RecAnalyser.GetSensitivityInverse());
133  RecModel.PlotVelWithErrors("recerr.plot");
134  }
135 
gplib::rmat GetModelCorrelation(const gplib::rmat &DataCovar)
Definition: GradFunc.cpp:181
gplib::rmat GetModelNullspace()
Definition: GradFunc.cpp:165
void SetVariationFactor(const gplib::rvec varfac)
Definition: GradFunc.h:41
gplib::rvec GetSingularValues()
Definition: GradFunc.cpp:201
C1DRecObjective RecObjective(RecData, Configuration.shift, Configuration.omega, Configuration.sigma, Configuration.wlevel, Configuration.slowness)
void SetModel(const gplib::rvec &TheModel)
Definition: GradFunc.cpp:87
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
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
int main()
Definition: recanalys.cpp:21
SeismicDataComp RecData
Definition: cadijoint.cpp:16
const gplib::rmat & GetSensitivityInverse() const
Definition: GradFunc.h:56