GPLIB++
perturbmodels.cpp
Go to the documentation of this file.
1 #include "C1DMTObjective.h"
2 #include "MTRecObjective.h"
3 #include "SeismicDataComp.h"
4 #include "GradFunc.h"
5 #include "UniformRNG.h"
6 #include "convert.h"
7 #include "C1DMTSynthData.h"
8 #include "ResPkModel.h"
9 #include "netcdfcpp.h"
10 #include <iostream>
11 #include <fstream>
12 #include <boost/bind.hpp>
13 #include "VecMat.h"
14 #include <algorithm>
15 #include <string>
16 #include "C1dInvGaConf.h"
17 #include "MTFitSetup.h"
18 #include "NetCDFTools.h"
19 using namespace std;
20 
21 int main()
22  {
23 
24  CMTStation MTData;
25  SeismicDataComp RecData;
26  string datanamebase;
27  cout << "Data name base: ";
28  cin >> datanamebase;
29  RecData.GetData(datanamebase + ".rec");
30  MTData.GetData(datanamebase + ".mtt");
31 
32  C1dInvGaConf Configuration;
33  Configuration.GetData("1dinvga.conf");
34 
35  C1DMTObjective MTObjective(MTData);
36  SetupMTFitParameters(Configuration, MTObjective);
37 
38  C1DRecObjective RecObjective(RecData, Configuration.shift,
39  Configuration.omega, Configuration.sigma, Configuration.cc,
40  Configuration.slowness);
41  RecObjective.SetPoisson(Configuration.poisson);
42  RecObjective.SetTimeWindow(Configuration.starttime, Configuration.endtime);
43  RecObjective.SetErrorLevel(Configuration.recerror);
44  RecObjective.SetFitExponent(Configuration.recfitexponent);
45  string modelnamebase;
46  cout << "Model name base: ";
47  cin >> modelnamebase;
48  ResPkModel RecModel;
49  C1DMTSynthData MTModel;
50  RecModel.GetData(modelnamebase + "_rec.mod");
51  MTModel.readmodel(modelnamebase + "_mt.mod");
52 
53  gplib::rvec MTModelVector = MTModel.GetModelVector();
54 
55  double MTOldRMS = MTObjective.CalcPerformance(MTModelVector);
56  gplib::rvec OldData(MTObjective.GetSynthData());
57  ModelAnalysis MTAnalyser(MTObjective);
58  MTAnalyser.SetModel(MTModelVector);
59 
60  cout << "MT Singular Values : " << MTAnalyser.GetSingularValues() << endl;
61  cout << "Relative SVD Threshold: ";
62  double svdthreshold = 0.0;
63  cin >> svdthreshold;
64  MTAnalyser.SetRelativeSVDThreshold(svdthreshold);
65 
66  const size_t nrecmodelparms = RecModel.SVelocity.size() * 2;
67  const size_t nlayers = RecModel.SVelocity.size();
68  gplib::rvec RecModelVector(nrecmodelparms);
69  for (int i = 0; i < nlayers; ++i)
70  {
71  RecModelVector(i) = RecModel.Thickness.at(i);
72  RecModelVector(i + nlayers) = RecModel.SVelocity.at(i);
73  }
74  double RecOldRMS = RecObjective.CalcPerformance(RecModelVector);
75  RecObjective.WriteData("old");
76  cout << "Saved old model " << endl << flush;
77  ModelAnalysis RecAnalyser(RecObjective);
78  RecAnalyser.SetModel(RecModelVector);
79  RecAnalyser.SetRelativeSVDThreshold(svdthreshold);
80 
81  cout << "Rec Singular Values : " << RecAnalyser.GetSingularValues() << endl;
82  //cout << "Rec Analysis R: " << RecAnalyser.GetModelResolution() << endl;
83  //cout << "Rec Analysis Null: " << RecAnalyser.GetModelNullspace() << endl;
84 
85 
86  gplib::rvec MTProjection(MTModelVector), MTPerturb(MTModelVector),
87  MTNewModel(MTModelVector);
88  gplib::rvec RecProjection(RecModelVector), RecPerturb(RecModelVector),
89  RecNewModel(RecModelVector);
90  UniformRNG Random;
91  double MTNewRMS = MTOldRMS;
92 
93  const int maxit = 20;
94  int currit = 0;
95  ublas::vector<double> Perturb(MTModelVector.size() / 2);
96  MTPerturb *= 0.0;
97  RecPerturb *= 0.0;
98  for (int i = 0; i < Perturb.size(); ++i)
99  Perturb(i) = Random.GetNumber(-1., 1.);
100 
101  for (size_t i = 0; i < nlayers; ++i)
102  {
103  MTPerturb(i + nlayers) = Perturb(i);
104  RecPerturb(i) = -Perturb(i);
105  }
106 
107  MTNewModel = MTModelVector;
108  const double rmsincrease = 0.05;
109  while (currit < maxit && std::abs(MTNewRMS - MTOldRMS) / MTOldRMS < rmsincrease)
110  {
111  for (size_t i = nlayers; i < MTPerturb.size(); ++i)
112  MTPerturb(i) = Random.GetNumber(-1., 1.);
113  MTProjection = prod(MTAnalyser.GetModelNullspace(), MTPerturb);
114  MTNewModel = MTModelVector + MTProjection;
115  MTNewRMS = MTObjective.CalcPerformance(MTNewModel);
116  MTObjective.WritePlot("new_mt" + stringify(currit));
117  cout << "MT New RMS: " << MTNewRMS << endl;
118  ++currit;
119  }
120  currit = 0;
121  double RecNewRMS = RecOldRMS;
122  RecNewModel = RecModelVector;
123  while (currit < maxit && std::abs(RecNewRMS - RecOldRMS) / RecOldRMS
124  < rmsincrease)
125  {
126  for (size_t i = 0; i < RecPerturb.size(); ++i)
127  RecPerturb(i) = Random.GetNumber(-1., 1.);
128  RecProjection = prod(RecAnalyser.GetModelNullspace(), RecPerturb);
129  RecNewModel = RecModelVector + RecProjection;
130  //RecNewModel(RecNewModel.size()/2-1) = 1.0;
131  RecNewRMS = RecObjective.CalcPerformance(RecNewModel);
132  RecObjective.WritePlot("new_rec" + stringify(currit));
133  cout << "Rec New RMS: " << RecNewRMS << endl;
134  ++currit;
135  }
136 
137  RecObjective.WriteData("new.rec");
138  MTObjective.WriteData("new.mtt");
139 
140  MTObjective.WritePlot("new_mt.plot");
141  RecObjective.WriteModel("new_rec.mod");
142  MTObjective.WriteModel("new_mt.mod");
143  cout << "MT OldRMS: " << MTOldRMS << " MT NewRMS: " << MTNewRMS << endl;
144  cout << "Rec OldRMS: " << RecOldRMS << " Rec NewRMS: " << RecNewRMS << endl;
145  cout << "MT Old Model: " << MTModelVector << endl;
146  cout << "MT New Model: " << MTNewModel << endl;
147  cout << "Rec Old Model: " << RecModelVector << endl;
148  cout << "Rec New Model: " << RecNewModel << endl;
149 
150  WriteMatrixAsNetCDF("mtres.nc", MTAnalyser.GetModelResolution());
151  WriteMatrixAsNetCDF("mtnull.nc", MTAnalyser.GetModelNullspace());
152  WriteMatrixAsNetCDF("recres.nc", RecAnalyser.GetModelResolution());
153  WriteMatrixAsNetCDF("recnull.nc", RecAnalyser.GetModelNullspace());
154 
155  }
void SetupMTFitParameters(const tConfObject &Configuration, C1DMTObjective &Objective)
Definition: MTFitSetup.h:8
gplib::rmat GetModelNullspace()
Definition: GradFunc.cpp:165
gplib::rvec GetSingularValues()
Definition: GradFunc.cpp:201
boost::shared_ptr< PlottableObjective > MTObjective
Definition: cadianiso.cpp:16
C1DRecObjective RecObjective(RecData, Configuration.shift, Configuration.omega, Configuration.sigma, Configuration.wlevel, Configuration.slowness)
void SetModel(const gplib::rvec &TheModel)
Definition: GradFunc.cpp:87
CMTStation MTData
Definition: cadijoint.cpp:15
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
SeismicDataComp RecData
Definition: cadijoint.cpp:16
int main()