levaniso.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include "Aniso1DMTObjective.h"
00003 #include "MTStation.h"
00004 #include "gentypes.h"
00005 #include "CLevanisoConf.h"
00006 #include "lm.h"
00007 #include "MTFitSetup.h"
00008 #include <boost/bind.hpp>
00009 #include "PTensor1DMTObjective.h"
00010 #include <boost/shared_ptr.hpp>
00011 #include "PTensorMTStation.h"
00012 
00013 using namespace std;
00014 using namespace gplib;
00015 
00016 boost::shared_ptr<PlottableObjective> MTObjective;
00017 CLevanisoConf Configuration;
00018 void misfit(double *p, double *x, int m, int n, void *data)
00019   {
00020     const unsigned int nlayers = m / 4;
00021     const unsigned int nparam = m;
00022     ttranscribed mtmember(nparam);
00023     for (int i = 0; i < nparam; ++i)
00024       {
00025         mtmember(i) = p[i];
00026       }
00027 
00028     if (Configuration.weights.at(0) > 0)
00029       {
00030         MTObjective->CalcPerformance(mtmember);
00031         const unsigned int nelements = MTObjective->GetMisfit().size();
00032         for (size_t i = 0; i < nelements; ++i)
00033           {
00034             x[i] = MTObjective->GetMisfit()(i) * Configuration.weights.at(0);
00035           }
00036       }
00037   }
00038 
00039 int main()
00040   {
00041 
00042     string version = "$Id: levaniso.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
00043     cout << endl << endl;
00044     cout << "Program " << version << endl;
00045     cout << " Levenberg Marquardt to  MT data for 1D anisotropic structure"
00046         << endl;
00047     cout << " look at levaniso.conf for configuration and setup " << endl;
00048     cout << " output files will be of the form " << endl;
00049     cout << " best_lev + ending " << endl;
00050 
00051     //CMTStation MTData,Best;
00052 
00053     Configuration.GetData("levaniso.conf");
00054     try
00055       {
00056         //MTData.GetData(Configuration.mtinputdata);
00057 
00058 
00059         //setup MT Objective function
00060 
00061 
00062         if (Configuration.mtfit == "ptensor")
00063           {
00064             PTensorMTStation PTData;
00065             PTData.GetData(Configuration.ptensordata);
00066             boost::shared_ptr<PTensor1DMTObjective> PTObjective(
00067                 new PTensor1DMTObjective(PTData));
00068             MTObjective = PTObjective;
00069           }
00070         else
00071           {
00072             MTStation MTData;
00073             MTData.GetData(Configuration.mtinputdata);
00074             boost::shared_ptr<Aniso1DMTObjective> AnisoObjective(
00075                 new Aniso1DMTObjective(MTData));
00076             SetupMTFitParameters(Configuration, *AnisoObjective);
00077             MTObjective = AnisoObjective;
00078           }
00079         MTObjective->SetFitExponent(2);
00080 
00081         const int nlayers = Configuration.minres.size();
00082         const int nparams = nlayers * 4;
00083         ttranscribed mtmember(nparams);
00084         copy(Configuration.startres.begin(), Configuration.startres.end(),
00085             mtmember.begin());
00086         copy(Configuration.startthick.begin(), Configuration.startthick.end(),
00087             mtmember.begin() + nlayers);
00088         copy(Configuration.startaniso.begin(), Configuration.startaniso.end(),
00089             mtmember.begin() + 2 * nlayers);
00090         copy(Configuration.startstrike.begin(),
00091             Configuration.startstrike.end(), mtmember.begin() + 3 * nlayers);
00092         double startfit = MTObjective->CalcPerformance(mtmember);
00093 
00094         cout << "Start misfit: " << startfit << endl;
00095 
00096         //double p[nparams],lb[nparams],ub[nparams];
00097         double *p = new double[nparams];
00098         double *lb = new double[nparams];
00099         double *ub = new double[nparams];
00100         double *x;
00101 
00102         int n = 0;
00103         if (Configuration.weights.at(0) > 0)
00104           n += MTObjective->GetSynthData().size();
00105 
00106         if (n == 0)
00107           {
00108             cerr << "One weight has to be > 0 !";
00109             return 100;
00110           }
00111         x = new double[n];
00112         double opts[LM_OPTS_SZ], info[LM_INFO_SZ];
00113 
00114         opts[0] = LM_INIT_MU;
00115         opts[1] = 1E-15;
00116         opts[2] = 1E-15;
00117         opts[3] = 1E-20;
00118         opts[4] = LM_DIFF_DELTA; // relevant only if the finite difference jacobian version is used
00119 
00120         for (int i = 0; i < nlayers; ++i)
00121           {
00122             p[i] = Configuration.startres.at(i);
00123             lb[i] = Configuration.minres.at(i);
00124             ub[i] = Configuration.maxres.at(i);
00125           }
00126         for (int i = 0; i < nlayers; ++i)
00127           {
00128             p[i + nlayers] = Configuration.startthick.at(i);
00129             lb[i + nlayers] = Configuration.minthick.at(i);
00130             ub[i + nlayers] = Configuration.maxthick.at(i);
00131           }
00132         for (int i = 0; i < nlayers; ++i)
00133           {
00134             p[i + 2 * nlayers] = Configuration.startaniso.at(i);
00135             lb[i + 2 * nlayers] = Configuration.minaniso.at(i);
00136             ub[i + 2 * nlayers] = Configuration.maxaniso.at(i);
00137           }
00138         for (int i = 0; i < nlayers; ++i)
00139           {
00140             p[i + 3 * nlayers] = Configuration.startstrike.at(i);
00141             lb[i + 3 * nlayers] = Configuration.minstrike.at(i);
00142             ub[i + 3 * nlayers] = Configuration.maxstrike.at(i);
00143           }
00144 
00145         for (int i = 0; i < n; i++)
00146           x[i] = 0.0;
00147 
00148         double ret = dlevmar_bc_dif(misfit, p, x, nparams, n, lb, ub,
00149             Configuration.maxiter, opts, info, NULL, NULL, NULL); // no jacobian
00150         cout << "Levenberg-Marquardt returned " << ret << " in " << info[5]
00151             << "iter, reason " << info[6] << endl;
00152 
00153         cout << endl << " Minimization info:" << endl;
00154         for (int i = 0; i < LM_INFO_SZ; ++i)
00155           cout << info[i] << " ";
00156         cout << endl;
00157 
00158         ttranscribed mtbest(nparams);
00159         for (int i = 0; i < nparams; ++i)
00160           {
00161             mtbest(i) = p[i];
00162           }
00163         cout << endl;
00164         double mtdiff = MTObjective->CalcPerformance(mtbest);
00165 
00166         cout << "MT-Fit: " << mtdiff << endl;
00167         ostringstream filename;
00168         filename << "best_lev";
00169 
00170         cout << " Saved as : " << filename.str() << endl;
00171         MTObjective->WriteData(filename.str());
00172         MTObjective->WriteModel(filename.str() + "_mt.mod");
00173         MTObjective->WritePlot(filename.str() + "_mt.plot");
00174 
00175         delete[] x;
00176         delete[] p;
00177         delete[] lb;
00178         delete[] ub;
00179       } catch (FatalException &e)
00180       {
00181         cerr << e.what() << endl;
00182         return -1;
00183       }
00184     return 0;
00185   }

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