Cloudy
Spectral Synthesis Code for Astrophysics
 All Classes 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-2023 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 #include "energy.h"
8 #include "flux.h"
9 #include "lines.h"
10 
12 void optimize_do();
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 const realnum VRSNEW = realnum(5.00);
46 
47 typedef double chi2_type;
48 
49 const chi2_type BIG_CHI2 = chi2_type(FLT_MAX);
50 
51 void optimize_phymir(realnum[],const realnum[],long,chi2_type*,realnum);
52 
54 chi2_type optimize_func(const realnum param[], int grid_index = -1);
55 
56 /* varypar.h */
58 const long LIMEXT = 5L;
59 const long LIMPAR = 20L;
60 
61 extern string chOptimFileName; /* name of output file from optimization run */
62 
64 
65 // this struct is written straight to a binary file, so it should be
66 // self-contained and NOT contain any pointers to other data!
67 // this also implies that only POD types are allowed here
68 template<class X, class Y = X, int NP = 32, int NSTR = 32>
70 {
71  X p_xmax;
72  Y p_ymax;
73  X p_xp[2*NP+1][NP];
74  Y p_yp[2*NP+1];
75  X p_absmin[NP];
76  X p_absmax[NP];
77  X p_varmin[NP];
78  X p_varmax[NP];
79  X p_a2[NP][NP];
80  X p_c1[NP];
81  X p_c2[NP];
82  X p_xc[NP];
83  X p_xcold[NP];
84  X p_vers;
86  X p_dmax;
87  X p_dold;
88  Y p_ymin;
89  int32 p_dim;
90  int32 p_sdim;
91  int32 p_nvar;
92  int32 p_noptim;
93  int32 p_maxiter;
94  int32 p_jmin;
95  int32 p_maxcpu;
96  int32 p_curcpu;
98  char p_chState[NSTR];
99  char p_chStr1[NSTR];
100  char p_chStr2[NSTR];
101  char p_chStr3[NSTR];
102 
103  // ONLY data items BEFORE this item will be written to the state file
104  Y (*p_func)(const X[],int);
105 
106  // private methods
107  void p_clear1();
108  void p_wr_state(const char*) const;
109  void p_rd_state(const char*);
110  Y p_execute_job( const X[], int, int );
111  // p_execute_job_parallel MUST be const, otherwise changes by child processes may be lost!
112  void p_execute_job_parallel( const X[], int, int ) const;
113  void p_barrier( int, int );
114  void p_process_output(int, int );
115  void p_evaluate_hyperblock();
117  void p_reset_hyperblock();
118  void p_phygrm( X[][NP], int );
119  bool p_lgLimitExceeded(const X[]) const;
120  X p_delta( int i, int j ) const { return ( i == j ) ? X(1.) : X(0.); }
122 
123 public:
124  // public methods, there should be no data here...
126  void clear() { p_clear1(); }
127  void init_minmax( const X[], const X[], int );
128  void init_state_file_name( const char* );
129  void init_strings( const string&, const string&, const char* );
130  void initial_run( Y (*)(const X[],int), int, const X[], const X[], X, int, phymir_mode, int );
131  void continue_from_state( Y (*)(const X[],int), int, const char*, X, int, phymir_mode, int );
132  void optimize();
133  void optimize_with_restart();
134  bool lgMaxIterExceeded() const { return ( p_noptim >= p_maxiter ); }
135  bool lgInitialized() const { return ( p_nvar > 0 ); }
136  bool lgConverged() const { return ( p_dmax <= p_toler ); }
137  bool lgConvergedRestart() const;
138  X xval( int i ) const { return p_xc[i]; }
139  X xmin( int i ) const { return max(p_varmin[i],p_absmin[i]); }
140  X xmax( int i ) const { return min(p_varmax[i],p_absmax[i]); }
141  Y yval() const { return p_ymin; }
142  int32 noptim() const { return p_noptim; }
143 };
144 
145 
167 struct t_optimize {
168 
170  bool lgVaryOn;
172  bool lgNoVary;
173 
175  bool lgOptimr;
176 
180 
182 
186 
190 
193 
195  long int nvarxt[LIMPAR],
196  nvfpnt[LIMPAR];
197 
199  varang[LIMPAR][2];
200 
201  long int nvary,
202  nparm,
203  nRangeSet;
204  bool lgVarOn;
205 
206  long int nIterOptim;
207 
209  vector<string> chColDen_label;
210  vector<long> ion_ColDen;
211  vector<realnum> ColDen_Obs;
212  vector<realnum> ColDen_error;
213 
216  vector<LineID> lineids;
218  vector<realnum> errorwave;
219  vector<long> ipobs;
220  vector<realnum> xLineInt_Obs;
221  vector<realnum> xLineInt_error;
222 
224  vector<string> chTempLab;
225  vector<long> ionTemp;
226  vector<realnum> temp_obs;
227  vector<realnum> temp_error;
228  vector<string> chTempWeight;
229 
231  bool lgOptDiam;
235 
237  vector<long> ContIndex;
238  vector<Energy> ContEner;
239  vector<Flux> ContNFnu;
240  vector<chi2_type> ContNFnuErr;
241 
243 
245  long int nOptimiz;
246 
250  long int nTrOpt;
251  bool lgTrOpt;
254  bool lgOptLum;
255  int nOptLum;
256 
259  bool lgOptCont;
260  long useCPU;
261 
263  char chOptRtn[5];
264 
265  double SavGenericData[10];
266 
268  {
269  for( long i=0; i < LIMPAR; ++i )
270  lgOptimizeAsLinear[i] = false;
271  }
272 };
273 extern t_optimize optimize;
274 
275 #endif /* OPTIMIZE_H_ */
void p_rd_state(const char *)
Definition: optimize_phymir.cpp:141
X p_absmin[NP]
Definition: optimize.h:75
realnum optint
Definition: optimize.h:248
Y yval() const
Definition: optimize.h:141
realnum varmax[LIMPAR]
Definition: optimize.h:184
int32 p_sdim
Definition: optimize.h:90
X xval(int i) const
Definition: optimize.h:138
X xmin(int i) const
Definition: optimize.h:139
X p_absmax[NP]
Definition: optimize.h:76
Definition: optimize.h:167
const int FILENAME_PATH_LENGTH_2
Definition: cddefines.h:306
int32 p_curcpu
Definition: optimize.h:96
long int nRangeSet
Definition: optimize.h:201
vector< realnum > ColDen_Obs
Definition: optimize.h:211
long int nvfpnt[LIMPAR]
Definition: optimize.h:195
vector< realnum > xLineInt_error
Definition: optimize.h:221
vector< Flux > ContNFnu
Definition: optimize.h:239
Definition: optimize.h:63
chi2_type optDiam
Definition: optimize.h:233
Y p_execute_job(const X[], int, int)
Definition: optimize_phymir.cpp:188
bool lgTrOpt
Definition: optimize.h:251
chi2_type optimize_func(const realnum param[], int grid_index=-1)
Definition: optimize_func.cpp:19
X xmax(int i) const
Definition: optimize.h:140
long int nOptimiz
Definition: optimize.h:245
phymir_state()
Definition: optimize.h:125
int32 p_noptim
Definition: optimize.h:92
chi2_type optDiamErr
Definition: optimize.h:234
Definition: optimize.h:63
realnum varang[LIMPAR][2]
Definition: optimize.h:198
void p_wr_state(const char *) const
Definition: optimize_phymir.cpp:85
const chi2_type BIG_CHI2
Definition: optimize.h:49
void optimize_phymir(realnum[], const realnum[], long, chi2_type *, realnum)
t_optimize optimize
Definition: optimize.cpp:6
char p_chStr1[NSTR]
Definition: optimize.h:99
X p_a2[NP][NP]
Definition: optimize.h:79
void vary_input(bool *lgLimOK, int grid_index)
Definition: vary_input.cpp:9
bool lgVaryOn
Definition: optimize.h:170
X p_delta(int i, int j) const
Definition: optimize.h:120
char chVarFmt[LIMPAR][FILENAME_PATH_LENGTH_2]
Definition: optimize.h:262
int32 noptim() const
Definition: optimize.h:142
realnum vparm[LIMEXT][LIMPAR]
Definition: optimize.h:189
double SavGenericData[10]
Definition: optimize.h:265
bool lgInitialParse
Definition: optimize.h:179
void p_execute_job_parallel(const X[], int, int) const
Definition: optimize_phymir.cpp:242
bool lgVarOn
Definition: optimize.h:204
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)
Definition: optimize_subplx.cpp:64
void continue_from_state(Y(*)(const X[], int), int, const char *, X, int, phymir_mode, int)
Definition: optimize_phymir.cpp:642
vector< realnum > temp_error
Definition: optimize.h:227
char p_chStr3[NSTR]
Definition: optimize.h:101
void init_strings(const string &, const string &, const char *)
Definition: optimize_phymir.cpp:565
X p_xmax
Definition: optimize.h:71
long int nIterOptim
Definition: optimize.h:206
bool lgOptimr
Definition: optimize.h:175
int32 p_maxiter
Definition: optimize.h:93
void p_barrier(int, int)
Definition: optimize_phymir.cpp:276
int32 p_maxcpu
Definition: optimize.h:95
t_optimize()
Definition: optimize.h:267
int32 p_nvar
Definition: optimize.h:91
void init_minmax(const X[], const X[], int)
Definition: optimize_phymir.cpp:549
void optimize()
Definition: optimize_phymir.cpp:687
bool lgParallel
Definition: optimize.h:258
long int nparm
Definition: optimize.h:201
vector< realnum > ColDen_error
Definition: optimize.h:212
Y p_ymin
Definition: optimize.h:88
X p_dold
Definition: optimize.h:87
long int nTrOpt
Definition: optimize.h:250
float realnum
Definition: cddefines.h:127
int nEmergent
Definition: optimize.h:215
long max(int a, long b)
Definition: cddefines.h:821
vector< realnum > xLineInt_Obs
Definition: optimize.h:220
bool lgConvergedRestart() const
Definition: optimize_phymir.cpp:720
X p_vers
Definition: optimize.h:84
int nOptLum
Definition: optimize.h:255
vector< string > chTempLab
Definition: optimize.h:224
long min(int a, long b)
Definition: cddefines.h:766
const long LIMPAR
Definition: optimize.h:59
realnum vincr[LIMPAR]
Definition: optimize.h:192
void p_clear1()
Definition: optimize_phymir.cpp:32
phymir_mode p_mode
Definition: optimize.h:97
void p_reset_transformation_matrix()
Definition: optimize_phymir.cpp:538
realnum optier
Definition: optimize.h:249
vector< LineID > lineids
Definition: optimize.h:216
bool lgOptimizeAsLinear[LIMPAR]
Definition: optimize.h:181
void clear()
Definition: optimize.h:126
char chOptRtn[5]
Definition: optimize.h:263
const long LIMEXT
Definition: optimize.h:58
double chi2_type
Definition: optimize.h:47
char p_chStr2[NSTR]
Definition: optimize.h:100
const realnum VRSNEW
Definition: optimize.h:45
vector< long > ContIndex
Definition: optimize.h:237
X p_varmax[NP]
Definition: optimize.h:78
Y(* p_func)(const X[], int)
Definition: optimize.h:104
vector< chi2_type > ContNFnuErr
Definition: optimize.h:240
bool lgOptimize
Definition: optimize.h:252
void init_state_file_name(const char *)
Definition: optimize_phymir.cpp:581
vector< long > ionTemp
Definition: optimize.h:225
long useCPU
Definition: optimize.h:260
vector< long > ipobs
Definition: optimize.h:219
Definition: optimize.h:63
Definition: optimize.h:63
bool p_lgLimitExceeded(const X[]) const
Definition: optimize_phymir.cpp:523
void optimize_do()
Definition: optimize_do.cpp:12
bool lgMaxIterExceeded() const
Definition: optimize.h:134
bool lgDiamInCM
Definition: optimize.h:232
vector< Energy > ContEner
Definition: optimize.h:238
X p_xcold[NP]
Definition: optimize.h:83
vector< realnum > temp_obs
Definition: optimize.h:226
void p_setup_next_hyperblock()
Definition: optimize_phymir.cpp:360
X p_toler
Definition: optimize.h:85
bool lgOptCont
Definition: optimize.h:259
vector< string > chColDen_label
Definition: optimize.h:209
bool lgOptDiam
Definition: optimize.h:231
char p_chState[NSTR]
Definition: optimize.h:98
realnum varmin[LIMPAR]
Definition: optimize.h:185
void p_reset_hyperblock()
Definition: optimize_phymir.cpp:478
void optimize_with_restart()
Definition: optimize_phymir.cpp:704
void p_evaluate_hyperblock()
Definition: optimize_phymir.cpp:329
void p_process_output(int, int)
Definition: optimize_phymir.cpp:308
Y p_ymax
Definition: optimize.h:72
X p_varmin[NP]
Definition: optimize.h:77
bool lgOptLum
Definition: optimize.h:254
X p_xc[NP]
Definition: optimize.h:82
int32 p_jmin
Definition: optimize.h:94
string chOptimFileName
Definition: optimize.h:69
vector< string > chTempWeight
Definition: optimize.h:228
realnum OptGlobalErr
Definition: optimize.h:242
int32 p_dim
Definition: optimize.h:89
Y p_yp[2 *NP+1]
Definition: optimize.h:74
realnum OptIncrm[LIMPAR]
Definition: optimize.h:198
bool lgNoVary
Definition: optimize.h:172
bool lgOptimFlow
Definition: optimize.h:247
X p_c1[NP]
Definition: optimize.h:80
vector< long > ion_ColDen
Definition: optimize.h:210
bool lgConverged() const
Definition: optimize.h:136
X p_c2[NP]
Definition: optimize.h:81
long int nvarxt[LIMPAR]
Definition: optimize.h:195
void initial_run(Y(*)(const X[], int), int, const X[], const X[], X, int, phymir_mode, int)
Definition: optimize_phymir.cpp:590
void p_phygrm(X[][NP], int)
Definition: optimize_phymir.cpp:496
long int nvary
Definition: optimize.h:201
X p_dmax
Definition: optimize.h:86
phymir_mode
Definition: optimize.h:63
bool lgInitialized() const
Definition: optimize.h:135
vector< realnum > errorwave
Definition: optimize.h:218
X p_xp[2 *NP+1][NP]
Definition: optimize.h:73