perturbmodels.cpp

Go to the documentation of this file.
00001 #include "C1DMTObjective.h"
00002 #include "MTRecObjective.h"
00003 #include "SeismicDataComp.h"
00004 #include "GradFunc.h"
00005 #include "UniformRNG.h"
00006 #include "convert.h"
00007 #include "C1DMTSynthData.h"
00008 #include "ResPkModel.h"
00009 #include "netcdfcpp.h"
00010 #include <iostream>
00011 #include <fstream>
00012 #include <boost/bind.hpp>
00013 #include "VecMat.h"
00014 #include <algorithm>
00015 #include <string>
00016 #include "C1dInvGaConf.h"
00017 #include "MTFitSetup.h"
00018 #include "NetCDFTools.h"
00019 using namespace std;
00020 
00021 int main()
00022   {
00023 
00024     CMTStation MTData;
00025     SeismicDataComp RecData;
00026     string datanamebase;
00027     cout << "Data name base: ";
00028     cin >> datanamebase;
00029     RecData.GetData(datanamebase + ".rec");
00030     MTData.GetData(datanamebase + ".mtt");
00031 
00032     C1dInvGaConf Configuration;
00033     Configuration.GetData("1dinvga.conf");
00034 
00035     C1DMTObjective MTObjective(MTData);
00036     SetupMTFitParameters(Configuration, MTObjective);
00037 
00038     C1DRecObjective RecObjective(RecData, Configuration.shift,
00039         Configuration.omega, Configuration.sigma, Configuration.cc,
00040         Configuration.slowness);
00041     RecObjective.SetPoisson(Configuration.poisson);
00042     RecObjective.SetTimeWindow(Configuration.starttime, Configuration.endtime);
00043     RecObjective.SetErrorLevel(Configuration.recerror);
00044     RecObjective.SetFitExponent(Configuration.recfitexponent);
00045     string modelnamebase;
00046     cout << "Model name base: ";
00047     cin >> modelnamebase;
00048     ResPkModel RecModel;
00049     C1DMTSynthData MTModel;
00050     RecModel.GetData(modelnamebase + "_rec.mod");
00051     MTModel.readmodel(modelnamebase + "_mt.mod");
00052 
00053     gplib::rvec MTModelVector = MTModel.GetModelVector();
00054 
00055     double MTOldRMS = MTObjective.CalcPerformance(MTModelVector);
00056     gplib::rvec OldData(MTObjective.GetSynthData());
00057     ModelAnalysis MTAnalyser(MTObjective);
00058     MTAnalyser.SetModel(MTModelVector);
00059 
00060     cout << "MT Singular Values : " << MTAnalyser.GetSingularValues() << endl;
00061     cout << "Relative SVD Threshold: ";
00062     double svdthreshold = 0.0;
00063     cin >> svdthreshold;
00064     MTAnalyser.SetRelativeSVDThreshold(svdthreshold);
00065 
00066     const size_t nrecmodelparms = RecModel.SVelocity.size() * 2;
00067     const size_t nlayers = RecModel.SVelocity.size();
00068     gplib::rvec RecModelVector(nrecmodelparms);
00069     for (int i = 0; i < nlayers; ++i)
00070       {
00071         RecModelVector(i) = RecModel.Thickness.at(i);
00072         RecModelVector(i + nlayers) = RecModel.SVelocity.at(i);
00073       }
00074     double RecOldRMS = RecObjective.CalcPerformance(RecModelVector);
00075     RecObjective.WriteData("old");
00076     cout << "Saved old model " << endl << flush;
00077     ModelAnalysis RecAnalyser(RecObjective);
00078     RecAnalyser.SetModel(RecModelVector);
00079     RecAnalyser.SetRelativeSVDThreshold(svdthreshold);
00080 
00081     cout << "Rec Singular Values : " << RecAnalyser.GetSingularValues() << endl;
00082     //cout << "Rec Analysis R: " << RecAnalyser.GetModelResolution() << endl;
00083     //cout << "Rec Analysis Null: " << RecAnalyser.GetModelNullspace() << endl;
00084 
00085 
00086     gplib::rvec MTProjection(MTModelVector), MTPerturb(MTModelVector),
00087         MTNewModel(MTModelVector);
00088     gplib::rvec RecProjection(RecModelVector), RecPerturb(RecModelVector),
00089         RecNewModel(RecModelVector);
00090     UniformRNG Random;
00091     double MTNewRMS = MTOldRMS;
00092 
00093     const int maxit = 20;
00094     int currit = 0;
00095     ublas::vector<double> Perturb(MTModelVector.size() / 2);
00096     MTPerturb *= 0.0;
00097     RecPerturb *= 0.0;
00098     for (int i = 0; i < Perturb.size(); ++i)
00099       Perturb(i) = Random.GetNumber(-1., 1.);
00100 
00101     for (size_t i = 0; i < nlayers; ++i)
00102       {
00103         MTPerturb(i + nlayers) = Perturb(i);
00104         RecPerturb(i) = -Perturb(i);
00105       }
00106 
00107     MTNewModel = MTModelVector;
00108     const double rmsincrease = 0.05;
00109     while (currit < maxit && std::abs(MTNewRMS - MTOldRMS) / MTOldRMS < rmsincrease)
00110       {
00111         for (size_t i = nlayers; i < MTPerturb.size(); ++i)
00112           MTPerturb(i) = Random.GetNumber(-1., 1.);
00113         MTProjection = prod(MTAnalyser.GetModelNullspace(), MTPerturb);
00114         MTNewModel = MTModelVector + MTProjection;
00115         MTNewRMS = MTObjective.CalcPerformance(MTNewModel);
00116         MTObjective.WritePlot("new_mt" + stringify(currit));
00117         cout << "MT New RMS: " << MTNewRMS << endl;
00118         ++currit;
00119       }
00120     currit = 0;
00121     double RecNewRMS = RecOldRMS;
00122     RecNewModel = RecModelVector;
00123     while (currit < maxit && std::abs(RecNewRMS - RecOldRMS) / RecOldRMS
00124         < rmsincrease)
00125       {
00126         for (size_t i = 0; i < RecPerturb.size(); ++i)
00127           RecPerturb(i) = Random.GetNumber(-1., 1.);
00128         RecProjection = prod(RecAnalyser.GetModelNullspace(), RecPerturb);
00129         RecNewModel = RecModelVector + RecProjection;
00130         //RecNewModel(RecNewModel.size()/2-1) = 1.0;
00131         RecNewRMS = RecObjective.CalcPerformance(RecNewModel);
00132         RecObjective.WritePlot("new_rec" + stringify(currit));
00133         cout << "Rec New RMS: " << RecNewRMS << endl;
00134         ++currit;
00135       }
00136 
00137     RecObjective.WriteData("new.rec");
00138     MTObjective.WriteData("new.mtt");
00139 
00140     MTObjective.WritePlot("new_mt.plot");
00141     RecObjective.WriteModel("new_rec.mod");
00142     MTObjective.WriteModel("new_mt.mod");
00143     cout << "MT OldRMS: " << MTOldRMS << " MT NewRMS: " << MTNewRMS << endl;
00144     cout << "Rec OldRMS: " << RecOldRMS << " Rec NewRMS: " << RecNewRMS << endl;
00145     cout << "MT Old Model: " << MTModelVector << endl;
00146     cout << "MT New Model: " << MTNewModel << endl;
00147     cout << "Rec Old Model: " << RecModelVector << endl;
00148     cout << "Rec New Model: " << RecNewModel << endl;
00149 
00150     WriteMatrixAsNetCDF("mtres.nc", MTAnalyser.GetModelResolution());
00151     WriteMatrixAsNetCDF("mtnull.nc", MTAnalyser.GetModelNullspace());
00152     WriteMatrixAsNetCDF("recres.nc", RecAnalyser.GetModelResolution());
00153     WriteMatrixAsNetCDF("recnull.nc", RecAnalyser.GetModelNullspace());
00154 
00155   }

Generated on Tue May 4 16:52:14 2010 for GPLIB++ by  doxygen 1.5.8