aboutsummaryrefslogtreecommitdiffstats
path: root/interface/opt.cc
blob: 49113a19703fcb01ee4924d0d11a4ebaeca93508 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "opt.h"
#include <utilities/opt_types.hpp>
#include <math/num_diff.hpp>
#include <map>
#include <core/freeze_param.hpp>
#include <data_sets/default_data_set.hpp>
#include "type_depository.hpp"
#include <memory>

//models:
#include <models/gauss1d.hpp>
#include <models/bl1d.hpp>
#include <models/nfw1d.hpp>
#include <models/bpl1d.hpp>
#include <models/beta1d.hpp>
#include <models/nbeta1d.hpp>
#include <models/dbeta1d.hpp>
#include <models/lin1d.hpp>
#include <models/pl1d.hpp>
#include <models/poly1d.hpp>
#include <models/bremss.hpp>
#include <models/beta2d2.hpp>
#include <models/beta2d.hpp>
#include <models/dbeta2d2.hpp>
#include <models/dbeta2d3.hpp>
#include <models/dbeta2d.hpp>
#include <models/polar_ellipse.hpp>
//end models
//#include <iostream>

using namespace std;
using namespace opt_utilities;
static const int max_fit_space_num=100;
struct fit_space;
static map<int,fit_space> fit_space_map;
struct fit_space
{
  dopt::fitter fit;
  ///  dopt::model model;
  fit_space()
  {
    fit.set_opt_method(dopt::powell_method());
    dopt::chisq cq;
    dopt::leastsq lsq;
    //  cq.verbose(true);
    fit.set_statistic(cq);
  }
  
};

std::vector<std::string> model_names;

void regist_model(const dopt::model& m,const char* addr)
{
  
  if(find(model_names.begin(),model_names.end(),m.get_type_name())!=model_names.end())
    {
      cerr<<m.get_type_name()<<" has been registed"<<endl;
      return;
    }
  cerr<<"registing:"<<m.get_type_name()<<endl;
  model_names.push_back(m.get_type_name());
  opt_utilities::register_model(m);
}



class initializer
{
public:
  initializer()
  {
    regist_model(lin1d<double>(),"lin1d");
  }
}_initializer;

extern "C"
{
  
  int alloc_fit_(int& n)
  {
    n=0;
    for(int i=1;i<max_fit_space_num;++i)
      {
	if(fit_space_map.find(i)==fit_space_map.end())
	  {
	    fit_space_map.insert(make_pair(i,fit_space()));
	    //cout<<i<<endl;
	    //return i;
	    n=i;
	    return 0;
	  }
      }
    
    return 1;
  }
  
  
  int free_fit_(const int& n)
  {
    map<int,fit_space>::iterator iter=fit_space_map.find(n);
    if(iter==fit_space_map.end())
      {
	return 1;
      }
    fit_space_map.erase(iter);
    return 0;
  }
  
  int load_data_(const int& nfit,const int& ndatas,double* x,double* y,double* yl,double* yu,double* xl,double* xu)
  {
    map<int,fit_space>::iterator iter=fit_space_map.find(nfit);
    if(iter==fit_space_map.end())
      {
	return 1;
      }
    //  cout<<x[0]<<endl;
    default_data_set<double,double> ds;
    for(int i=0;i<ndatas;++i)
      {
	data<double,double> d(x[i],y[i],yl[i],(yu==0?yl[i]:yu[i]),(xl==0?0:xl[i]),(xu==0?0:xu[i]));
	//  cout<<x[i]<<" "<<y[i]<<endl;
	ds.add_data(d);
      }
    iter->second.fit.load_data(ds);
    return 0;
  }
  
  
  int set_model_(const int& nfit,const char* model_name)
  {
    map<int,fit_space>::iterator iter=fit_space_map.find(nfit);
    
    if(iter==fit_space_map.end())
      {
	cerr<<"fit not found"<<endl;
	
	return 1;
      }
    try
      {
	std::auto_ptr<dopt::model> p(opt_utilities::get_model<double,double,std::vector<double>,std::string>(model_name));
	iter->second.fit.set_model(*p);
	return 0;
      }
    catch(opt_exception& e)
      {
	//cout<<model_name<<endl;
	cout<<e.what()<<endl;
	//throw e;
	return 1;
      }
  }
  
  int set_param_(const int& nfit,const char* pname,const double& value)
  {
    map<int,fit_space>::iterator iter=fit_space_map.find(nfit);
    cerr<<"pname="<<pname<<endl;
    cerr<<"value="<<value<<endl;
    if(iter==fit_space_map.end())
      {
	cerr<<"fit not found"<<endl;
	return 1;
      }
    iter->second.fit.set_param_value(pname,value);
    return 0;
  }
  
  
  int freeze_param_(const int& nfit,const char* pname)
  {
    map<int,fit_space>::iterator iter=fit_space_map.find(nfit);
    if(iter==fit_space_map.end())
      {
	return 1;
      }
    opt_utilities::freeze_param<double,double,std::vector<double> > fp(pname);
    try
      {
	dynamic_cast<opt_utilities::freeze_param<double,double,vector<double> >& >(iter->second.fit.get_param_modifier())+=fp;
	return 0;
      }
    catch(opt_exception& e)
      {
	(void)e;
	iter->second.fit.set_param_modifier(fp);
	return 0;
      }
    return 0;
  }
  
  
  int thraw_param_(const int& nfit,const char* pname)
  {
    map<int,fit_space>::iterator iter=fit_space_map.find(nfit);
    if(iter==fit_space_map.end())
      {
	return 1;
      }
    opt_utilities::freeze_param<double,double,std::vector<double> > fp(pname);
    try
      {
	dynamic_cast<opt_utilities::freeze_param<double,double,vector<double> >& >(iter->second.fit.get_param_modifier())-=fp;
      }
    catch(opt_exception& e)
      {
	(void)e;
	//iter->second.fit.set_param_modifier(fp);
      }
    return 0;
  }
  
  int perform_fit_(const int& nfit)
  {
    map<int,fit_space>::iterator iter=fit_space_map.find(nfit);
    if(iter==fit_space_map.end())
      {
	return 1;
      }
    iter->second.fit.fit();
    return 0;
  }
  
  
  int get_param_(const int& nfit,double& r,const char* pname)
  {
    map<int,fit_space>::iterator iter=fit_space_map.find(nfit);
    if(iter==fit_space_map.end())
      {
	//return 0;
	cerr<<"fit not found"<<endl;
	r=0;
	return 1;
      }
    //  cerr<<"fdsaf"<<r<<endl;
    r=iter->second.fit.get_param_value(pname);
    return 0;
  }
}