cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
optimize.h
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2022 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 
4 #ifndef OPTIMIZE_H_
5 #define OPTIMIZE_H_
6 
7 class Energy;
8 class Flux;
9 
12 bool lgOptimize_do(void);
13 
17 void vary_input(bool *lgLimOK, int grid_index);
18 
33 void optimize_subplex(long int n,
34  double tol,
35  long int maxnfe,
36  long int mode,
37  realnum scale[],
38  realnum x[],
39  realnum *fx,
40  long int *nfe,
41  realnum work[],
42  long int iwork[],
43  long int *iflag);
44 
45 /*<NP->*/
47 const realnum VRSNEW = realnum(4.00);
48 
49 typedef double chi2_type;
50 
51 const chi2_type BIG_CHI2 = chi2_type(FLT_MAX);
52 
53 void optimize_phymir(realnum[],const realnum[],long,chi2_type*,realnum);
54 
56 chi2_type optimize_func(const realnum param[], int grid_index = -1);
57 
58 /* varypar.h */
60 const long LIMEXT = 5L;
61 const long LIMPAR = 20L;
62 
66 
67 // this struct is written straight to a binary file, so it should be
68 // self-contained and NOT contain any pointers to other data!
69 // this also implies that only POD types are allowed here
70 template<class X, class Y = X, int NP = 32, int NSTR = 32>
72 {
73  X p_xmax;
74  Y p_ymax;
75  X p_xp[2*NP+1][NP];
76  Y p_yp[2*NP+1];
77  X p_absmin[NP];
78  X p_absmax[NP];
79  X p_varmin[NP];
80  X p_varmax[NP];
81  X p_a2[NP][NP];
82  X p_c1[NP];
83  X p_c2[NP];
84  X p_xc[NP];
85  X p_xcold[NP];
86  X p_vers;
88  X p_dmax;
89  X p_dold;
90  Y p_ymin;
91  int32 p_dim;
92  int32 p_sdim;
93  int32 p_nvar;
94  int32 p_noptim;
95  int32 p_maxiter;
96  int32 p_jmin;
97  int32 p_maxcpu;
98  int32 p_curcpu;
100  char p_chState[NSTR];
101  char p_chStr1[NSTR];
102  char p_chStr2[NSTR];
103  char p_chStr3[NSTR];
104 
105  // ONLY data items BEFORE this item will be written to the state file
106  uint32 p_size;
107  Y (*p_func)(const X[],int);
108 
109  // private methods
110  void p_clear1();
111  void p_wr_state(const char*) const;
112  void p_rd_state(const char*);
113  Y p_execute_job( const X[], int, int );
114  // p_execute_job_parallel MUST be const, otherwise changes by child processes may be lost!
115  void p_execute_job_parallel( const X[], int, int ) const;
116  void p_barrier( int, int );
117  void p_process_output(int, int );
118  void p_evaluate_hyperblock();
120  void p_reset_hyperblock();
121  void p_phygrm( X[][NP], int );
122  bool p_lgLimitExceeded(const X[]) const;
123  X p_delta( int i, int j ) const { return ( i == j ) ? X(1.) : X(0.); }
125 
126 public:
127  // public methods, there should be no data here...
129  void clear() { p_clear1(); }
130  void init_minmax( const X[], const X[], int );
131  void init_state_file_name( const char* );
132  void init_strings( const char*, const char*, const char* );
133  void initial_run( Y (*)(const X[],int), int, const X[], const X[], X, int, phymir_mode, int );
134  void continue_from_state( Y (*)(const X[],int), int, const char*, X, int, phymir_mode, int );
135  void optimize();
136  void optimize_with_restart();
137  bool lgMaxIterExceeded() const { return ( p_noptim >= p_maxiter ); }
138  bool lgInitialized() const { return ( p_nvar > 0 ); }
139  bool lgConverged() const { return ( p_dmax <= p_toler ); }
140  bool lgConvergedRestart() const;
141  X xval( int i ) const { return p_xc[i]; }
142  X xmin( int i ) const { return max(p_varmin[i],p_absmin[i]); }
143  X xmax( int i ) const { return min(p_varmax[i],p_absmax[i]); }
144  Y yval() const { return p_ymin; }
145  int32 noptim() const { return p_noptim; }
146 };
147 
148 
170 struct t_optimize {
171 
173  bool lgVaryOn;
175  bool lgNoVary;
176 
178  bool lgOptimr;
179 
183 
185 
189 
193 
196 
198  long int nvarxt[LIMPAR],
199  nvfpnt[LIMPAR];
200 
202  varang[LIMPAR][2];
203 
204  long int nvary,
205  nparm,
206  nRangeSet;
207  bool lgVarOn;
208 
209  long int nIterOptim;
210 
212  vector<string> chColDen_label;
213  vector<long> ion_ColDen;
214  vector<realnum> ColDen_Obs;
215  vector<realnum> ColDen_error;
216 
219  vector<string> chLineLabel;
221  vector<realnum> wavelength;
223  vector<realnum> errorwave;
224  vector<long> ipobs;
225  vector<realnum> xLineInt_Obs;
226  vector<realnum> xLineInt_error;
227 
229  vector<string> chTempLab;
230  vector<long> ionTemp;
231  vector<realnum> temp_obs;
232  vector<realnum> temp_error;
233  vector<string> chTempWeight;
234 
236  bool lgOptDiam;
240 
242  vector<long> ContIndex;
243  vector<Energy> ContEner;
244  vector<Flux> ContNFnu;
245  vector<chi2_type> ContNFnuErr;
246 
248 
250  long int nOptimiz;
251 
255  long int nTrOpt;
256  bool lgTrOpt;
259  bool lgOptLum;
260  int nOptLum;
261 
264  bool lgOptCont;
265  long useCPU;
266 
268  char chOptRtn[5];
269 
270  double SavGenericData[10];
271 
272  };
273 extern t_optimize optimize;
274 
275 #endif /* OPTIMIZE_H_ */
void p_rd_state(const char *)
X p_absmin[NP]
Definition: optimize.h:77
realnum optint
Definition: optimize.h:253
Y yval() const
Definition: optimize.h:144
realnum varmax[LIMPAR]
Definition: optimize.h:187
int32 p_sdim
Definition: optimize.h:92
X xval(int i) const
Definition: optimize.h:141
X xmin(int i) const
Definition: optimize.h:142
X p_absmax[NP]
Definition: optimize.h:78
const int FILENAME_PATH_LENGTH_2
Definition: cddefines.h:296
int32 p_curcpu
Definition: optimize.h:98
long int nRangeSet
Definition: optimize.h:204
vector< realnum > ColDen_Obs
Definition: optimize.h:214
long int nvfpnt[LIMPAR]
Definition: optimize.h:198
vector< realnum > xLineInt_error
Definition: optimize.h:226
vector< Flux > ContNFnu
Definition: optimize.h:244
chi2_type optDiam
Definition: optimize.h:238
Y p_execute_job(const X[], int, int)
bool lgTrOpt
Definition: optimize.h:256
chi2_type optimize_func(const realnum param[], int grid_index=-1)
X xmax(int i) const
Definition: optimize.h:143
long int nOptimiz
Definition: optimize.h:250
int32 p_noptim
Definition: optimize.h:94
chi2_type optDiamErr
Definition: optimize.h:239
realnum varang[LIMPAR][2]
Definition: optimize.h:201
void p_wr_state(const char *) const
const chi2_type BIG_CHI2
Definition: optimize.h:51
vector< string > chLineLabel
Definition: optimize.h:219
void optimize_phymir(realnum[], const realnum[], long, chi2_type *, realnum)
char p_chStr1[NSTR]
Definition: optimize.h:101
X p_a2[NP][NP]
Definition: optimize.h:81
void vary_input(bool *lgLimOK, int grid_index)
Definition: vary_input.cpp:9
bool lgVaryOn
Definition: optimize.h:173
X p_delta(int i, int j) const
Definition: optimize.h:123
char chVarFmt[LIMPAR][FILENAME_PATH_LENGTH_2]
Definition: optimize.h:267
int32 noptim() const
Definition: optimize.h:145
realnum vparm[LIMEXT][LIMPAR]
Definition: optimize.h:192
double SavGenericData[10]
Definition: optimize.h:270
bool lgInitialParse
Definition: optimize.h:182
void p_execute_job_parallel(const X[], int, int) const
Definition: flux.h:9
bool lgVarOn
Definition: optimize.h:207
void init_strings(const char *, const char *, const char *)
void optimize_subplex(long int n, double tol, long int maxnfe, long int mode, realnum scale[], realnum x[], realnum *fx, long int *nfe, realnum work[], long int iwork[], long int *iflag)
void continue_from_state(Y(*)(const X[], int), int, const char *, X, int, phymir_mode, int)
vector< realnum > temp_error
Definition: optimize.h:232
char p_chStr3[NSTR]
Definition: optimize.h:103
long int nIterOptim
Definition: optimize.h:209
bool lgOptimr
Definition: optimize.h:178
int32 p_maxiter
Definition: optimize.h:95
void p_barrier(int, int)
int32 p_maxcpu
Definition: optimize.h:97
int32 p_nvar
Definition: optimize.h:93
void init_minmax(const X[], const X[], int)
bool lgParallel
Definition: optimize.h:263
long int nparm
Definition: optimize.h:204
vector< realnum > ColDen_error
Definition: optimize.h:215
long int nTrOpt
Definition: optimize.h:255
float realnum
Definition: cddefines.h:124
const int INPUT_LINE_LENGTH
Definition: cddefines.h:301
int nEmergent
Definition: optimize.h:218
long max(int a, long b)
Definition: cddefines.h:817
vector< realnum > xLineInt_Obs
Definition: optimize.h:225
bool lgConvergedRestart() const
int nOptLum
Definition: optimize.h:260
vector< string > chTempLab
Definition: optimize.h:229
long min(int a, long b)
Definition: cddefines.h:762
const long LIMPAR
Definition: optimize.h:61
t_optimize optimize
Definition: optimize.cpp:6
realnum vincr[LIMPAR]
Definition: optimize.h:195
phymir_mode p_mode
Definition: optimize.h:99
void p_reset_transformation_matrix()
realnum optier
Definition: optimize.h:254
vector< realnum > wavelength
Definition: optimize.h:221
bool lgOptimizeAsLinear[LIMPAR]
Definition: optimize.h:184
void clear()
Definition: optimize.h:129
char chOptRtn[5]
Definition: optimize.h:268
const long LIMEXT
Definition: optimize.h:60
double chi2_type
Definition: optimize.h:49
char p_chStr2[NSTR]
Definition: optimize.h:102
const realnum VRSNEW
Definition: optimize.h:47
vector< long > ContIndex
Definition: optimize.h:242
X p_varmax[NP]
Definition: optimize.h:80
Y(* p_func)(const X[], int)
Definition: optimize.h:107
vector< chi2_type > ContNFnuErr
Definition: optimize.h:245
bool lgOptimize
Definition: optimize.h:257
void init_state_file_name(const char *)
Definition: energy.h:9
vector< long > ionTemp
Definition: optimize.h:230
long useCPU
Definition: optimize.h:265
uint32 p_size
Definition: optimize.h:106
vector< long > ipobs
Definition: optimize.h:224
bool p_lgLimitExceeded(const X[]) const
bool lgMaxIterExceeded() const
Definition: optimize.h:137
bool lgDiamInCM
Definition: optimize.h:237
vector< Energy > ContEner
Definition: optimize.h:243
X p_xcold[NP]
Definition: optimize.h:85
vector< realnum > temp_obs
Definition: optimize.h:231
void p_setup_next_hyperblock()
bool lgOptCont
Definition: optimize.h:264
vector< string > chColDen_label
Definition: optimize.h:212
bool lgOptDiam
Definition: optimize.h:236
char p_chState[NSTR]
Definition: optimize.h:100
realnum varmin[LIMPAR]
Definition: optimize.h:188
void p_reset_hyperblock()
void optimize_with_restart()
void p_evaluate_hyperblock()
void p_process_output(int, int)
X p_varmin[NP]
Definition: optimize.h:79
bool lgOptLum
Definition: optimize.h:259
X p_xc[NP]
Definition: optimize.h:84
int32 p_jmin
Definition: optimize.h:96
bool lgOptimize_do(void)
Definition: optimize_do.cpp:12
vector< string > chTempWeight
Definition: optimize.h:233
realnum OptGlobalErr
Definition: optimize.h:247
int32 p_dim
Definition: optimize.h:91
Y p_yp[2 *NP+1]
Definition: optimize.h:76
realnum OptIncrm[LIMPAR]
Definition: optimize.h:201
bool lgNoVary
Definition: optimize.h:175
bool lgOptimFlow
Definition: optimize.h:252
X p_c1[NP]
Definition: optimize.h:82
vector< long > ion_ColDen
Definition: optimize.h:213
bool lgConverged() const
Definition: optimize.h:139
char chOptimFileName[INPUT_LINE_LENGTH]
Definition: optimize.cpp:7
X p_c2[NP]
Definition: optimize.h:83
long int nvarxt[LIMPAR]
Definition: optimize.h:198
void initial_run(Y(*)(const X[], int), int, const X[], const X[], X, int, phymir_mode, int)
void p_phygrm(X[][NP], int)
long int nvary
Definition: optimize.h:204
phymir_mode
Definition: optimize.h:65
bool lgInitialized() const
Definition: optimize.h:138
vector< realnum > errorwave
Definition: optimize.h:223
X p_xp[2 *NP+1][NP]
Definition: optimize.h:75