cloudy  trunk
grid_do.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2013 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 /*grid_do called by cdDrive, this returns 0 if things went ok, 1 for disaster */
4 #include "cddefines.h"
5 #include "conv.h"
6 #include "input.h"
7 #include "called.h"
8 #include "version.h"
9 #include "init.h"
10 #include "prt.h"
11 #include "trace.h"
12 #include "grainvar.h"
13 #include "parse.h"
14 #include "save.h"
15 #include "optimize.h"
16 #include "grid.h"
17 
18 /* grid_do called by cdDrive, calls gridXspec or lgOptimize_do, returns false if ok, true for disaster */
19 bool grid_do()
20 {
21  char chLine[INPUT_LINE_LENGTH],
22  chNote[8];
23  long int i,
24  ii,
25  j;
26  realnum ptem[LIMPAR];
27 
28  DEBUG_ENTRY( "grid_do()" );
29 
30  /* main driver for optimization runs
31  * Drives cloudy to grid variables;*/
32 
33  /* code originally written by R.F. Carswell, IOA Cambridge */
34 
35  /* this will be number of times grid calls cloudy */
36  optimize.nOptimiz = 0;
37 
38  /* variables with optimizer */
39  for( i=0; i < LIMPAR; i++ )
40  {
41  optimize.OptIncrm[i] = 0.;
42  optimize.varang[i][0] = -FLT_MAX;
43  optimize.varang[i][1] = FLT_MAX;
44  /* this should be overwritten by format of vary line */
45  strcpy( optimize.chVarFmt[i], "error - no optimizer line image was set" );
46  }
47 
48  /* necessary to do this to keep all lines in */
49  prt.lgFaintOn = false;
50  conv.LimFail = 1000;
51 
52  /* this initializes variables at the start of each simulation
53  * in a grid, before the parser is called - this must set any values
54  * that may be changed by the command parser */
56 
57  /* call READR the first time to scan off all variable options */
58  /* this is just an initial parsing to get the number of iterations and
59  * the number of varied parameters. The other Init* routines are not
60  * called after this because this is all done again later for each grid point */
61  ParseCommands();
62 
63  /* >>chng 00 aug 09, return memory allocated for grains, they are not used, PvH */
64  gv.clear();
65 
67 
68  /* option to change default increments; if zero then leave as is */
69  for( i=0; i < LIMPAR; i++ )
70  {
71  if( optimize.OptIncrm[i] != 0. )
72  {
74  }
75  }
76 
77  if( called.lgTalk )
78  {
79  /* check that at least 1 observed quantity was entered */
80  unsigned long nObsQuant = optimize.xLineInt_Obs.size() + optimize.ContNFnu.size() +
81  optimize.temp_obs.size() + optimize.ColDen_Obs.size();
82  if( optimize.lgOptLum )
83  nObsQuant++;
84  if( optimize.lgOptDiam )
85  nObsQuant++;
86  if( nObsQuant == 0 && !grid.lgGrid )
87  {
88  fprintf( ioQQQ, " The input stream has vary commands, but\n" );
89  fprintf( ioQQQ, " no observed quantities were entered. Whats up?\n" );
90  fprintf( ioQQQ, " Use the NO VARY command if you intended to disable optimization.\n" );
92  }
93 
94  /* check that the total number of parameters to vary is greater than 1 */
95  if( optimize.nvary < 1 )
96  {
97  fprintf( ioQQQ, " No parameters to vary were entered. Whats up?\n" );
99  }
100 
101  if( optimize.nvary > long(nObsQuant) && !grid.lgGrid )
102  {
103  fprintf( ioQQQ, " PROBLEM - More parameters are varied then there are observables.\n" );
104  fprintf( ioQQQ, " PROBLEM - This run may not converge as a result.\n" );
105  fprintf( ioQQQ, " PROBLEM - Please reduce the number of free parameters,"
106  " or add more observables.\n" );
107  }
108 
109  if( strcmp(optimize.chOptRtn,"XSPE") == 0 && optimize.nRangeSet != optimize.nvary )
110  {
111  fprintf( ioQQQ, " Every parameter with a VARY option must have a GRID specified,\n" );
112  fprintf( ioQQQ, " and the GRID must be specified after the VARY option.\n" );
113  fprintf( ioQQQ, " These requirements were not satisfied for %ld parameter(s).\n",
114  abs(optimize.nvary - optimize.nRangeSet) );
116  }
117 
118  /* lgTrOptm set with trace grid command */
119  if( trace.lgTrOptm )
120  {
121  for( i=0; i < optimize.nvary; i++ )
122  {
123  /*print the command format as debugging aid */
124  fprintf( ioQQQ, "%s\n", optimize.chVarFmt[i]);
125 
126  /* now generate the actual command with parameter,
127  * there will be from 1 to 3 numbers on the line */
128  if( optimize.nvarxt[i] == 1 )
129  {
130  /* case with 1 parameter */
131  sprintf( chLine, optimize.chVarFmt[i], optimize.vparm[0][i] );
132  }
133 
134  else if( optimize.nvarxt[i] == 2 )
135  {
136  /* case with 2 parameter */
137  sprintf( chLine, optimize.chVarFmt[i], optimize.vparm[0][i],
138  optimize.vparm[1][i]);
139  }
140 
141  else if( optimize.nvarxt[i] == 3 )
142  {
143  /* case with 3 parameter */
144  sprintf( chLine, optimize.chVarFmt[i],
145  optimize.vparm[0][i], optimize.vparm[1][i], optimize.vparm[2][i] );
146  }
147 
148  else if( optimize.nvarxt[i] == 4 )
149  {
150  /* case with 4 parameter */
151  sprintf( chLine, optimize.chVarFmt[i],
152  optimize.vparm[0][i], optimize.vparm[1][i], optimize.vparm[2][i],
153  optimize.vparm[3][i] );
154  }
155 
156  else if( optimize.nvarxt[i] == 5 )
157  {
158  /* case with 5 parameter */
159  sprintf( chLine, optimize.chVarFmt[i],
160  optimize.vparm[0][i], optimize.vparm[1][i], optimize.vparm[2][i],
161  optimize.vparm[3][i], optimize.vparm[4][i]);
162  }
163 
164  else
165  {
166  fprintf(ioQQQ,"The number of variable options on this line makes no sense to me1\n");
168  }
169 
170  /* print the resulting command line*/
171  fprintf( ioQQQ, "%s\n", chLine );
172  }
173  }
174 
175  /* say who we are */
176  if( strcmp(optimize.chOptRtn,"XSPE") == 0 )
177  fprintf( ioQQQ, "%58cGrid Driver\n", ' ' );
178  else
179  fprintf( ioQQQ, "%54cOptimization Driver\n", ' ' );
180  int indent = (int)((122 - strlen(t_version::Inst().chVersion))/2);
181  fprintf( ioQQQ, "%*cCloudy %s\n\n",indent,' ',t_version::Inst().chVersion);
182  fprintf( ioQQQ, "%23c**************************************%7.7s**************************************\n",
183  ' ', t_version::Inst().chDate );
184  fprintf( ioQQQ, "%23c*%81c*\n", ' ', ' ' );
185 
186  /* now echo initial input quantities with flag for vary */
187  /* first loop steps over all command lines entered */
188  for( i=0; i <= input.nSave; i++ )
189  {
190  /* put space to start line, overwrite if vary found */
191  strcpy( chNote, " " );
192  /* loop over all vary commands, see if this is one */
193  for( j=0; j < optimize.nvary; j++ )
194  {
195  /* input.nSave is on C array counting, rest are on fortran */
196  if( i == optimize.nvfpnt[j] )
197  {
198  /* this is a vary command, put keyword at start */
199  strcpy( chNote, "VARY>>>" );
200  }
201  }
202 
203  fprintf( ioQQQ, "%22.7s * %-80s*\n", chNote, input.chCardSav[i] );
204  }
205  fprintf( ioQQQ, "%23c*%81c*\n", ' ', ' ' );
206  fprintf( ioQQQ, "%23c***********************************************************************************\n\n\n", ' ' );
207 
208  /* option to trace logical flow within this sub */
209  if( optimize.lgOptimFlow )
210  {
211  for( j=0; j < optimize.nvary; j++ )
212  {
213  i = optimize.nvfpnt[j];
214  fprintf( ioQQQ, " trace:%80.80s\n", input.chCardSav[i]);
215  fprintf( ioQQQ, "%80.80s\n", optimize.chVarFmt[j]);
216  fprintf( ioQQQ, " number of variables on line:%4ld\n",
217  optimize.nvarxt[j] );
218  fprintf( ioQQQ, " Values:" );
219  for( ii=1; ii <= optimize.nvarxt[j]; ii++ )
220  {
221  fprintf( ioQQQ, "%10.2e", optimize.vparm[ii-1][j] );
222  }
223  fprintf( ioQQQ, "\n" );
224  }
225  }
226 
227  if( strcmp(optimize.chOptRtn,"PHYM") == 0 )
228  {
229  fprintf( ioQQQ, " Up to %ld iterations will be performed,\n",
231  fprintf( ioQQQ, " and the final version of the input file will be written to the file %s\n",
232  chOptimFileName );
233 
234  fprintf( ioQQQ, " The Phymir method will be used" );
235  if( optimize.lgParallel )
236  {
237  if( cpu.i().lgMPI() )
238  fprintf( ioQQQ, " in MPI mode.\n" );
239  else
240  fprintf( ioQQQ, " in parallel mode.\n" );
241 
242  fprintf( ioQQQ, " The maximum no. of CPU's to be used is %ld.\n",
243  optimize.useCPU );
244  }
245  else
246  fprintf( ioQQQ, " in sequential mode.\n" );
247  }
248 
249  else if( strcmp(optimize.chOptRtn,"SUBP") == 0 )
250  {
251  fprintf( ioQQQ, " Up to %ld iterations will be performed,\n",
253  fprintf( ioQQQ, " and the final version of the input file will be written to the file %s\n",
254  chOptimFileName );
255 
256  fprintf( ioQQQ, " The Subplex method will be used.\n" );
257  }
258 
259  else if( strcmp(optimize.chOptRtn,"XSPE") == 0 )
260  {
261  fprintf( ioQQQ, " Producing grid output.\n" );
262  }
263 
264  else
265  {
266  fprintf( ioQQQ, " I do not understand what method to use.\n" );
267  fprintf( ioQQQ, " Sorry.\n" );
269  }
270 
271  fprintf( ioQQQ, "\n %ld parameter(s) will be varied. The first lines, and the increments are:\n",
272  optimize.nvary );
273 
274  for( i=0; i < optimize.nvary; i++ )
275  {
276  optimize.varmax[i] = -FLT_MAX;
277  optimize.varmin[i] = FLT_MAX;
278  /* write formatted to output using the format held in chVarFmt(np) */
279 
280  /* now generate the actual command with parameter,
281  * there will be from 1 to 3 numbers on the line */
282  if( optimize.nvarxt[i] == 1 )
283  {
284  /* case with 1 parameter */
285  sprintf( chLine, optimize.chVarFmt[i], optimize.vparm[0][i] );
286  }
287 
288  else if( optimize.nvarxt[i] == 2 )
289  {
290  /* case with 2 parameter */
291  sprintf( chLine, optimize.chVarFmt[i], optimize.vparm[0][i], optimize.vparm[1][i]);
292  }
293 
294  else if( optimize.nvarxt[i] == 3 )
295  {
296  /* case with 3 parameter */
297  sprintf( chLine, optimize.chVarFmt[i],
298  optimize.vparm[0][i], optimize.vparm[1][i], optimize.vparm[2][i] );
299  }
300 
301  else if( optimize.nvarxt[i] == 4 )
302  {
303  /* case with 4 parameter */
304  sprintf( chLine, optimize.chVarFmt[i],
305  optimize.vparm[0][i], optimize.vparm[1][i], optimize.vparm[2][i],
306  optimize.vparm[3][i] );
307  }
308 
309  else if( optimize.nvarxt[i] == 5 )
310  {
311  /* case with 5 parameter */
312  sprintf( chLine, optimize.chVarFmt[i],
313  optimize.vparm[0][i], optimize.vparm[1][i], optimize.vparm[2][i],
314  optimize.vparm[3][i], optimize.vparm[4][i]);
315  }
316 
317  else
318  {
319  fprintf(ioQQQ,"The number of variable options on this line makes no sense to me2\n");
321  }
322 
323  fprintf( ioQQQ, "\n %s\n", chLine );
324  if( strcmp(optimize.chOptRtn,"XSPE") == 0 )
325  fprintf( ioQQQ, " %s increment is %.3g, the limits are %.3g to %.3g\n",
326  grid.lgLinearIncrements[i] ? "Linear" : "Log",
328  else
329  fprintf( ioQQQ, " Initial increment is %.3g, the limits are %.3g to %.3g\n",
330  optimize.vincr[i], optimize.varang[i][0], optimize.varang[i][1] );
331  }
332  }
333 
334  if( strcmp(optimize.chOptRtn,"XSPE") == 0 )
335  {
336  if( called.lgTalk )
337  {
338  if( cpu.i().lgMPI() )
339  fprintf( ioQQQ, "\n Running in MPI grid mode on %ld CPUs. ", cpu.i().nCPU() );
340  else
341  fprintf( ioQQQ, "\n Running in single-CPU grid mode. " );
342  fprintf( ioQQQ, "I will now start to write the input files.\n\n" );
343  }
344 
345  for( j=0; j < optimize.nvary; j++ )
346  ptem[j] = grid.paramLimits[j][0];
347  for( j=optimize.nvary; j < LIMPAR; j++ )
348  {
349  ptem[j] = 0.f;
350  grid.paramIncrements[j] = 0.f;
351  grid.lgLinearIncrements[j] = false;
352  }
353 
354  gridXspec(ptem,optimize.nvary);
355 
356  if( called.lgTalk )
357  {
358  fprintf( ioQQQ, " **************************************************\n" );
359  fprintf( ioQQQ, " **************************************************\n" );
360  fprintf( ioQQQ, " **************************************************\n" );
361  fprintf( ioQQQ, "\n Writing input files has been completed.\n\n\n" );
362  }
363  }
364  else
365  {
366  called.lgTalk = false;
367  /* this flag is needed to turn print on to have effect */
368  called.lgTalkIsOK = false;
369 
371  }
372 
373  return lgAbort;
374 }
t_optimize::nIterOptim
long int nIterOptim
Definition: optimize.h:205
t_optimize::temp_obs
vector< realnum > temp_obs
Definition: optimize.h:227
lgAbort
bool lgAbort
Definition: cddefines.cpp:10
t_optimize::ColDen_Obs
vector< realnum > ColDen_Obs
Definition: optimize.h:210
t_optimize::vincr
realnum vincr[LIMPAR]
Definition: optimize.h:191
ParseCommands
void ParseCommands(void)
Definition: parse_commands.cpp:90
t_optimize::nparm
long int nparm
Definition: optimize.h:201
t_optimize::nOptimiz
long int nOptimiz
Definition: optimize.h:246
t_optimize::lgParallel
bool lgParallel
Definition: optimize.h:259
Singleton< t_version >::Inst
static t_version & Inst()
Definition: cddefines.h:175
ioQQQ
FILE * ioQQQ
Definition: cddefines.cpp:7
realnum
float realnum
Definition: cddefines.h:103
conv.h
InitDefaultsPreparse
void InitDefaultsPreparse(void)
Definition: init_defaults_preparse.cpp:49
grid
t_grid grid
Definition: grid.cpp:5
t_input::chCardSav
char chCardSav[NKRD][INPUT_LINE_LENGTH]
Definition: input.h:32
cpu
static t_cpu cpu
Definition: cpu.h:355
trace.h
t_grid::paramIncrements
realnum paramIncrements[LIMPAR]
Definition: grid.h:34
t_conv::LimFail
long int LimFail
Definition: conv.h:235
grid.h
input
t_input input
Definition: input.cpp:12
t_optimize::chOptRtn
char chOptRtn[5]
Definition: optimize.h:264
version.h
lgOptimize_do
bool lgOptimize_do(void)
Definition: optimize_do.cpp:13
GrainVar::clear
void clear()
Definition: grainvar.h:464
t_optimize::lgOptLum
bool lgOptLum
Definition: optimize.h:255
parse.h
t_optimize::varmax
realnum varmax[LIMPAR]
Definition: optimize.h:183
t_optimize::OptIncrm
realnum OptIncrm[LIMPAR]
Definition: optimize.h:197
optimize
t_optimize optimize
Definition: optimize.cpp:5
t_cpu::i
t_cpu_i & i()
Definition: cpu.h:347
t_optimize::vparm
realnum vparm[LIMEXT][LIMPAR]
Definition: optimize.h:188
t_optimize::chVarFmt
char chVarFmt[LIMPAR][FILENAME_PATH_LENGTH_2]
Definition: optimize.h:263
EXIT_FAILURE
#define EXIT_FAILURE
Definition: cddefines.h:140
grid_do
bool grid_do()
Definition: grid_do.cpp:19
t_grid::lgLinearIncrements
bool lgLinearIncrements[LIMPAR]
Definition: grid.h:35
init.h
trace
t_trace trace
Definition: trace.cpp:5
prt
t_prt prt
Definition: prt.cpp:10
t_trace::lgTrOptm
bool lgTrOptm
Definition: trace.h:67
cddefines.h
optimize.h
t_called::lgTalk
bool lgTalk
Definition: called.h:12
t_optimize::nvarxt
long int nvarxt[LIMPAR]
Definition: optimize.h:194
t_optimize::lgOptimFlow
bool lgOptimFlow
Definition: optimize.h:248
gridXspec
void gridXspec(realnum *, long)
cdEXIT
#define cdEXIT(FAIL)
Definition: cddefines.h:434
t_optimize::ContNFnu
vector< Flux > ContNFnu
Definition: optimize.h:240
t_cpu_i::nCPU
long nCPU() const
Definition: cpu.h:321
save.h
t_optimize::nvary
long int nvary
Definition: optimize.h:200
chOptimFileName
char chOptimFileName[INPUT_LINE_LENGTH]
Definition: optimize.cpp:6
prt.h
t_optimize::xLineInt_Obs
vector< realnum > xLineInt_Obs
Definition: optimize.h:221
t_prt::lgFaintOn
bool lgFaintOn
Definition: prt.h:202
t_cpu_i::lgMPI
bool lgMPI() const
Definition: cpu.h:322
t_optimize::varang
realnum varang[LIMPAR][2]
Definition: optimize.h:198
t_input::nSave
long int nSave
Definition: input.h:46
INPUT_LINE_LENGTH
const int INPUT_LINE_LENGTH
Definition: cddefines.h:254
grainvar.h
t_optimize::varmin
realnum varmin[LIMPAR]
Definition: optimize.h:184
LIMPAR
const long LIMPAR
Definition: optimize.h:61
called
t_called called
Definition: called.cpp:5
conv
t_conv conv
Definition: conv.cpp:5
t_optimize::nvfpnt
long int nvfpnt[LIMPAR]
Definition: optimize.h:195
gv
GrainVar gv
Definition: grainvar.cpp:5
t_optimize::lgOptDiam
bool lgOptDiam
Definition: optimize.h:232
t_grid::paramLimits
realnum paramLimits[LIMPAR][2]
Definition: grid.h:33
called.h
t_optimize::nRangeSet
long int nRangeSet
Definition: optimize.h:202
t_optimize::useCPU
long useCPU
Definition: optimize.h:261
t_called::lgTalkIsOK
bool lgTalkIsOK
Definition: called.h:23
input.h
t_grid::lgGrid
bool lgGrid
Definition: grid.h:40
DEBUG_ENTRY
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:684