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

Generated on Fri Jul 4 15:30:21 2008 for GPLIB++ by  doxygen 1.5.5