GPLIB++
levaniso.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include "Aniso1DMTObjective.h"
3 #include "MTStation.h"
4 #include "gentypes.h"
5 #include "CLevanisoConf.h"
6 #include "lm.h"
7 #include "MTFitSetup.h"
8 #include <boost/bind.hpp>
9 #include "PTensor1DMTObjective.h"
10 #include <boost/shared_ptr.hpp>
11 #include "PTensorMTStation.h"
12 
13 using namespace std;
14 using namespace gplib;
15 
16 boost::shared_ptr<PlottableObjective> MTObjective;
17 CLevanisoConf Configuration;
18 void misfit(double *p, double *x, int m, int n, void *data)
19  {
20  const unsigned int nlayers = m / 4;
21  const unsigned int nparam = m;
22  ttranscribed mtmember(nparam);
23  for (int i = 0; i < nparam; ++i)
24  {
25  mtmember(i) = p[i];
26  }
27 
28  if (Configuration.weights.at(0) > 0)
29  {
30  MTObjective->CalcPerformance(mtmember);
31  const unsigned int nelements = MTObjective->GetMisfit().size();
32  for (size_t i = 0; i < nelements; ++i)
33  {
34  x[i] = MTObjective->GetMisfit()(i) * Configuration.weights.at(0);
35  }
36  }
37  }
38 
39 int main()
40  {
41 
42  string version = "$Id: levaniso.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
43  cout << endl << endl;
44  cout << "Program " << version << endl;
45  cout << " Levenberg Marquardt to MT data for 1D anisotropic structure"
46  << endl;
47  cout << " look at levaniso.conf for configuration and setup " << endl;
48  cout << " output files will be of the form " << endl;
49  cout << " best_lev + ending " << endl;
50 
51  //CMTStation MTData,Best;
52 
53  Configuration.GetData("levaniso.conf");
54  try
55  {
56  //MTData.GetData(Configuration.mtinputdata);
57 
58 
59  //setup MT Objective function
60 
61 
62  if (Configuration.mtfit == "ptensor")
63  {
64  PTensorMTStation PTData;
65  PTData.GetData(Configuration.ptensordata);
66  boost::shared_ptr<PTensor1DMTObjective> PTObjective(
67  new PTensor1DMTObjective(PTData));
68  MTObjective = PTObjective;
69  }
70  else
71  {
73  MTData.GetData(Configuration.mtinputdata);
74  boost::shared_ptr<Aniso1DMTObjective> AnisoObjective(
75  new Aniso1DMTObjective(MTData));
76  SetupMTFitParameters(Configuration, *AnisoObjective);
77  MTObjective = AnisoObjective;
78  }
79  MTObjective->SetFitExponent(2);
80 
81  const int nlayers = Configuration.minres.size();
82  const int nparams = nlayers * 4;
83  ttranscribed mtmember(nparams);
84  copy(Configuration.startres.begin(), Configuration.startres.end(),
85  mtmember.begin());
86  copy(Configuration.startthick.begin(), Configuration.startthick.end(),
87  mtmember.begin() + nlayers);
88  copy(Configuration.startaniso.begin(), Configuration.startaniso.end(),
89  mtmember.begin() + 2 * nlayers);
90  copy(Configuration.startstrike.begin(),
91  Configuration.startstrike.end(), mtmember.begin() + 3 * nlayers);
92  double startfit = MTObjective->CalcPerformance(mtmember);
93 
94  cout << "Start misfit: " << startfit << endl;
95 
96  //double p[nparams],lb[nparams],ub[nparams];
97  double *p = new double[nparams];
98  double *lb = new double[nparams];
99  double *ub = new double[nparams];
100  double *x;
101 
102  int n = 0;
103  if (Configuration.weights.at(0) > 0)
104  n += MTObjective->GetSynthData().size();
105 
106  if (n == 0)
107  {
108  cerr << "One weight has to be > 0 !";
109  return 100;
110  }
111  x = new double[n];
112  double opts[LM_OPTS_SZ], info[LM_INFO_SZ];
113 
114  opts[0] = LM_INIT_MU;
115  opts[1] = 1E-15;
116  opts[2] = 1E-15;
117  opts[3] = 1E-20;
118  opts[4] = LM_DIFF_DELTA; // relevant only if the finite difference jacobian version is used
119 
120  for (int i = 0; i < nlayers; ++i)
121  {
122  p[i] = Configuration.startres.at(i);
123  lb[i] = Configuration.minres.at(i);
124  ub[i] = Configuration.maxres.at(i);
125  }
126  for (int i = 0; i < nlayers; ++i)
127  {
128  p[i + nlayers] = Configuration.startthick.at(i);
129  lb[i + nlayers] = Configuration.minthick.at(i);
130  ub[i + nlayers] = Configuration.maxthick.at(i);
131  }
132  for (int i = 0; i < nlayers; ++i)
133  {
134  p[i + 2 * nlayers] = Configuration.startaniso.at(i);
135  lb[i + 2 * nlayers] = Configuration.minaniso.at(i);
136  ub[i + 2 * nlayers] = Configuration.maxaniso.at(i);
137  }
138  for (int i = 0; i < nlayers; ++i)
139  {
140  p[i + 3 * nlayers] = Configuration.startstrike.at(i);
141  lb[i + 3 * nlayers] = Configuration.minstrike.at(i);
142  ub[i + 3 * nlayers] = Configuration.maxstrike.at(i);
143  }
144 
145  for (int i = 0; i < n; i++)
146  x[i] = 0.0;
147 
148  double ret = dlevmar_bc_dif(misfit, p, x, nparams, n, lb, ub,
149  Configuration.maxiter, opts, info, NULL, NULL, NULL); // no jacobian
150  cout << "Levenberg-Marquardt returned " << ret << " in " << info[5]
151  << "iter, reason " << info[6] << endl;
152 
153  cout << endl << " Minimization info:" << endl;
154  for (int i = 0; i < LM_INFO_SZ; ++i)
155  cout << info[i] << " ";
156  cout << endl;
157 
158  ttranscribed mtbest(nparams);
159  for (int i = 0; i < nparams; ++i)
160  {
161  mtbest(i) = p[i];
162  }
163  cout << endl;
164  double mtdiff = MTObjective->CalcPerformance(mtbest);
165 
166  cout << "MT-Fit: " << mtdiff << endl;
167  ostringstream filename;
168  filename << "best_lev";
169 
170  cout << " Saved as : " << filename.str() << endl;
171  MTObjective->WriteData(filename.str());
172  MTObjective->WriteModel(filename.str() + "_mt.mod");
173  MTObjective->WritePlot(filename.str() + "_mt.plot");
174 
175  delete[] x;
176  delete[] p;
177  delete[] lb;
178  delete[] ub;
179  } catch (FatalException &e)
180  {
181  cerr << e.what() << endl;
182  return -1;
183  }
184  return 0;
185  }
void SetupMTFitParameters(const tConfObject &Configuration, C1DMTObjective &Objective)
Definition: MTFitSetup.h:8
ublas::vector< double > ttranscribed
Definition: gentypes.h:21
This is a special objective function to fit phase tensor MT data.
boost::shared_ptr< PlottableObjective > MTObjective
Definition: levaniso.cpp:16
CLevanisoConf Configuration
Definition: levaniso.cpp:17
virtual void GetData(const std::string filename)
read in data from file, determines format by ending
Definition: MTStation.cpp:597
int main()
Definition: levaniso.cpp:39
The class MTStation is used to store the transfer functions and related information for a MT-site...
Definition: MTStation.h:17
CMTStation MTData
Definition: cadijoint.cpp:15
void GetData(const std::string &filename)
void misfit(double *p, double *x, int m, int n, void *data)
Definition: levaniso.cpp:18
string version
Definition: makeinput.cpp:16
The basic exception class for all errors that arise in gplib.