diff options
Diffstat (limited to 'interface')
-rw-r--r-- | interface/makefile | 13 | ||||
-rw-r--r-- | interface/opt.cc | 181 | ||||
-rw-r--r-- | interface/opt.h | 29 |
3 files changed, 223 insertions, 0 deletions
diff --git a/interface/makefile b/interface/makefile new file mode 100644 index 0000000..5cf7a99 --- /dev/null +++ b/interface/makefile @@ -0,0 +1,13 @@ +INC=-I../ + + +libopt.a:opt.o + $(AR) $@ opt.o + +opt.o:opt.cc opt.h + $(CPP) $< -o $@ $(INC) $(CPPFLAGS) + + +clean: + $(RM) *.o + $(RM) *~ diff --git a/interface/opt.cc b/interface/opt.cc new file mode 100644 index 0000000..51762f5 --- /dev/null +++ b/interface/opt.cc @@ -0,0 +1,181 @@ +#include "opt.h" +#include <utilities/opt_types.hpp> +#include <models/models.hpp> +#include <map> +#include <core/freeze_param.hpp> +#include <core/default_data_set.hpp> +//#include <iostream> + +using namespace std; +using namespace opt_utilities; +const static 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_method(dopt::powell_method()); + dopt::chisq cq; + // cq.verbose(true); + fit.set_statistic(cq); + } + +}; + + +extern "C" +{ + +void 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; + break; + } + } + + return; +} + + +void free_fit_(const int& n) +{ + map<int,fit_space>::iterator iter=fit_space_map.find(n); + if(iter==fit_space_map.end()) + { + return; + } + fit_space_map.erase(iter); +} + +void 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; + } + // 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.push_back(d); + } + iter->second.fit.load_data(ds); +} + + +void 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; + } + try + { + iter->second.fit.set_model(opt_utilities::get_1dmodel_by_name(model_name)); + } + catch(opt_exception& e) + { + //cout<<model_name<<endl; + cout<<e.what()<<endl; + throw e; + } +} + +void 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; + } + iter->second.fit.set_param_value(pname,value); +} + + +void 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; + } + 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) + { + iter->second.fit.set_param_modifier(fp); + } +} + + +void thaw_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; + } + 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) + { + //iter->second.fit.set_param_modifier(fp); + } +} + +void perform_fit_(const int& nfit) +{ + map<int,fit_space>::iterator iter=fit_space_map.find(nfit); + if(iter==fit_space_map.end()) + { + return; + } + iter->second.fit.fit(); +} + + +void 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; + } + // cerr<<"fdsaf"<<r<<endl; + r=iter->second.fit.get_param_value(pname); + +} + + + +} + diff --git a/interface/opt.h b/interface/opt.h new file mode 100644 index 0000000..e9c3221 --- /dev/null +++ b/interface/opt.h @@ -0,0 +1,29 @@ +#ifndef OPT_H +#define OPT_H +#define F77 +#ifdef F77 +#define alloc_fit_ alloc_fit__ +#define free_fit_ free_fit__ +#define load_data_ load_data__ +#define set_model_ set_model__ +#define set_param_ set_param__ +#define freeze_param_ freeze_param__ +#define thaw_param_ thaw_param__ +#define perform_fit_ perform_fit__ +#define get_param_ get_param__ +#endif + +extern "C" +{ + void alloc_fit_(int&); + void free_fit_(const int& nxc); + void load_data_(const int& nfit,const int& ndatas,double* x,double* y,double* yl,double* yu=0,double* xl=0,double* xu=0); + void set_model_(const int& nfit,const char* model_name); + void set_param_(const int& nfit,const char* param_name,const double& value); + void freeze_param_(const int& nfit,const char* param_name); + void thraw_param_(const int& nfit,const char* param_name); + void perform_fit_(const int& nfit); + void get_param_(const int& nfit,double& r,const char* param_name); +} + +#endif |