cloudy  trunk
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
grid_do.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2008 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 "grains.h"
13 #include "parse.h"
14 #include "punch.h"
15 #include "optimize.h"
16 #include "grid.h"
17 
18 /*grid_do called by cdDrive, calls gridXspec, returns false if ok, true for disaster */
19 bool grid_do(void)
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  /* variables with optimizer */
36  for( i=0; i < LIMPAR; i++ )
37  {
38  optimize.OptIncrm[i] = 0.;
39  optimize.varang[i][0] = -FLT_MAX;
40  optimize.varang[i][1] = FLT_MAX;
41  /* this should be overwritten by format of vary line */
42  strcpy( optimize.chVarFmt[i], "error - no optimizer line image was set" );
43  }
44 
45  /* necessary to do this to keep all lines in */
46  prt.lgFaintOn = false;
47  conv.LimFail = 1000;
48 
49  /* this initializes variables at the start of each simulation
50  * in a grid, before the parser is called - this must set any values
51  * that may be changed by the command parser */
53 
54  /* call READR the first time to scan off all variable options */
55  /* this is just an initial parsing to get the number of iterations and
56  * the number of varied parameters. The other Init* routines are not
57  * called after this because this is all done again later for each grid point */
58  ParseCommands();
59 
60  /* done parsing, now turn off punch headers. */
61  punch.lgPunHeader = false;
62 
63  /* >>chng 06 may 20, do not turn off printout if grid calculation is in place
64  * rather than optimization */
65  if( !grid.lgGrid )
66  {
67  called.lgTalk = false;
68  /* this flag is needed to turn print on to have effect */
69  called.lgTalkIsOK = false;
70  }
71 
72  /* >>chng 00 aug 09, return memory allocated for grains, they are not used, PvH */
74 
76 
77  /* check that more than 1 observed intensities or column densities were entered */
78  if( ((optimize.nlobs + (long)optimize.lgOptLum + optimize.nTempObs + optimize.ncobs) < 1 ) &&
79  !grid.lgGrid )
80  {
81  fprintf( ioQQQ, " The input stream has vary commands, but\n" );
82  fprintf( ioQQQ, " no observed quantities were entered. Whats up?\n" );
83  fprintf( ioQQQ, " Use the NO VARY command to input vary options but not try to perform this.\n" );
84  cdEXIT(EXIT_FAILURE);
85  }
86 
87  /* check that the total number of parameters to vary is greater than 1 */
88  if( optimize.nvary < 1 )
89  {
90  fprintf( ioQQQ, " No parameters to vary were entered. Whats up?\n" );
91  cdEXIT(EXIT_FAILURE);
92  }
93 
94  if( ( strcmp(optimize.chOptRtn,"XSPE") == 0 ) && ( optimize.nRangeSet != optimize.nvary ) )
95  {
96  fprintf( ioQQQ, " Every parameter with a VARY option must have a GRID specified,\n" );
97  fprintf( ioQQQ, " and the GRID must be specified after the VARY option.\n" );
98  fprintf( ioQQQ, " These requirements were not satisfied for %ld parameter(s).\n", abs(optimize.nvary - optimize.nRangeSet) );
99  cdEXIT(EXIT_FAILURE);
100  }
101 
102  /* lgTrOptm set with trace grid command */
103  if( trace.lgTrOptm )
104  {
105  for( i=0; i < optimize.nvary; i++ )
106  {
107  /*print the command format as debugging aid */
108  fprintf( ioQQQ, "%s\n", optimize.chVarFmt[i]);
109 
110  /* now generate the actual command with parameter,
111  * there will be from 1 to 3 numbers on the line */
112  if( optimize.nvarxt[i] == 1 )
113  {
114  /* case with 1 parameter */
115  sprintf( chLine , optimize.chVarFmt[i], optimize.vparm[0][i] );
116  }
117 
118  else if( optimize.nvarxt[i] == 2 )
119  {
120  /* case with 2 parameter */
121  sprintf( chLine , optimize.chVarFmt[i], optimize.vparm[0][i], optimize.vparm[1][i]);
122  }
123 
124  else if( optimize.nvarxt[i] == 3 )
125  {
126  /* case with 3 parameter */
127  sprintf( chLine , optimize.chVarFmt[i],
128  optimize.vparm[0][i], optimize.vparm[1][i] , optimize.vparm[2][i] );
129  }
130 
131  else if( optimize.nvarxt[i] == 4 )
132  {
133  /* case with 4 parameter */
134  sprintf( chLine , optimize.chVarFmt[i],
135  optimize.vparm[0][i], optimize.vparm[1][i] , optimize.vparm[2][i], optimize.vparm[3][i] );
136  }
137 
138  else if( optimize.nvarxt[i] == 5 )
139  {
140  /* case with 5 parameter */
141  sprintf( chLine , optimize.chVarFmt[i],
142  optimize.vparm[0][i], optimize.vparm[1][i] , optimize.vparm[2][i],
143  optimize.vparm[3][i], optimize.vparm[4][i]);
144  }
145 
146  else
147  {
148  fprintf(ioQQQ,"The number of variable options on this line makes no sense to me1\n");
149  cdEXIT(EXIT_FAILURE);
150  }
151 
152  /* print the resulting command line*/
153  fprintf( ioQQQ, "%s\n", chLine );
154  }
155  }
156 
157  /* option to change default increments; if zero then leave as is */
158  for( i=0; i < LIMPAR; i++ )
159  {
160  if( optimize.OptIncrm[i] != 0. )
161  {
163  }
164  }
165 
166  /* say who we are */
167  if( strcmp(optimize.chOptRtn,"XSPE") == 0 )
168  {
169  fprintf( ioQQQ, " Grid Driver\n" );
170  }
171  else
172  {
173  fprintf( ioQQQ, " Optimization Driver\n" );
174  }
175  int indent = (int)((122 - strlen(t_version::Inst().chVersion))/2);
176  fprintf( ioQQQ, "%*cCloudy %s\n\n",indent,' ',t_version::Inst().chVersion);
177  fprintf( ioQQQ, "%23c**************************************%7.7s**************************************\n", ' ', t_version::Inst().chDate );
178  fprintf( ioQQQ, "%23c*%81c*\n", ' ', ' ' );
179 
180  /* now echo initial input quantities with flag for vary */
181  /* first loop steps over all command lines entered */
182  for( i=0; i <= input.nSave; i++ )
183  {
184  /* put space to start line, overwrite if vary found */
185  strcpy( chNote, " " );
186  /* loop over all vary commands, see if this is one */
187  for( j=0; j < optimize.nvary; j++ )
188  {
189  /* input.nSave is on C array counting, rest are on fortran */
190  if( i == optimize.nvfpnt[j] )
191  {
192  /* this is a vary command, put keyword at start */
193  strcpy( chNote, "VARY>>>" );
194  }
195  }
196 
197  fprintf( ioQQQ, "%22.7s * %-80s*\n", chNote, input.chCardSav[i] );
198  }
199  fprintf( ioQQQ, "%23c*%81c*\n", ' ', ' ' );
200  fprintf( ioQQQ, "%23c***********************************************************************************\n\n\n", ' ' );
201 
202  /* option to trace logical flow within this sub */
203  if( optimize.lgOptimFlow )
204  {
205  for( j=0; j < optimize.nvary; j++ )
206  {
207  i = optimize.nvfpnt[j];
208  fprintf( ioQQQ, " trace:%80.80s\n", input.chCardSav[i]);
209  fprintf( ioQQQ, "%80.80s\n", optimize.chVarFmt[j]);
210  fprintf( ioQQQ, " number of variables on line:%4ld\n",
211  optimize.nvarxt[j] );
212  fprintf( ioQQQ, " Values:" );
213  for( ii=1; ii <= optimize.nvarxt[j]; ii++ )
214  {
215  fprintf( ioQQQ, "%10.2e", optimize.vparm[ii-1][j] );
216  }
217  fprintf( ioQQQ, "\n" );
218  }
219  }
220 
221 
222  if( strcmp(optimize.chOptRtn,"PHYM") == 0 )
223  {
224  fprintf( ioQQQ, " Up to%5ld iterations will be performed,\n",
226  fprintf( ioQQQ, " and the final version of the input file will be written to the file %s\n",
227  chOptimFileName );
228 
229  fprintf( ioQQQ, " The optimize_phymir method will be used" );
230  if( optimize.lgParallel ) {
231  fprintf( ioQQQ, " in parallel mode.\n The maximum no. of CPU's to be used is %1ld.\n",optimize.useCPU );
232  }
233  else {
234  fprintf( ioQQQ, " in sequential mode.\n" );
235  }
236  }
237 
238  else if( strcmp(optimize.chOptRtn,"SUBP") == 0 )
239  {
240  fprintf( ioQQQ, " Up to%5ld iterations will be performed,\n",
242  fprintf( ioQQQ, " and the final version of the input file will be written to the file %s\n",
243  chOptimFileName );
244 
245  fprintf( ioQQQ, " Subplex method will be used.\n" );
246  }
247 
248  else if( strcmp(optimize.chOptRtn,"XSPE") == 0 )
249  {
250  fprintf( ioQQQ, " Producing grid output.\n" );
251  }
252 
253  else
254  {
255  fprintf( ioQQQ, " I do not understand what method to use.\n" );
256  fprintf( ioQQQ, " Sorry.\n" );
257  cdEXIT(EXIT_FAILURE);
258  }
259 
260  fprintf( ioQQQ, "\n%4ld parameter(s) will be varied. The first lines, and the increments are:\n",
261  optimize.nvary );
262 
263  for( i=0; i < optimize.nvary; i++ )
264  {
265  optimize.varmax[i] = -FLT_MAX;
266  optimize.varmin[i] = FLT_MAX;
267  /* write formatted to output using the format held in chVarFmt(np) */
268 
269  /* now generate the actual command with parameter,
270  * there will be from 1 to 3 numbers on the line */
271  if( optimize.nvarxt[i] == 1 )
272  {
273  /* case with 1 parameter */
274  sprintf( chLine , optimize.chVarFmt[i], optimize.vparm[0][i] );
275  }
276 
277  else if( optimize.nvarxt[i] == 2 )
278  {
279  /* case with 2 parameter */
280  sprintf( chLine , optimize.chVarFmt[i], optimize.vparm[0][i], optimize.vparm[1][i]);
281  }
282 
283  else if( optimize.nvarxt[i] == 3 )
284  {
285  /* case with 3 parameter */
286  sprintf( chLine , optimize.chVarFmt[i],
287  optimize.vparm[0][i], optimize.vparm[1][i] , optimize.vparm[2][i] );
288  }
289 
290  else if( optimize.nvarxt[i] == 4 )
291  {
292  /* case with 4 parameter */
293  sprintf( chLine , optimize.chVarFmt[i],
294  optimize.vparm[0][i], optimize.vparm[1][i] , optimize.vparm[2][i] , optimize.vparm[3][i] );
295  }
296 
297  else if( optimize.nvarxt[i] == 5 )
298  {
299  /* case with 5 parameter */
300  sprintf( chLine , optimize.chVarFmt[i],
301  optimize.vparm[0][i], optimize.vparm[1][i] , optimize.vparm[2][i],
302  optimize.vparm[3][i] , optimize.vparm[4][i]);
303  }
304 
305  else
306  {
307  fprintf(ioQQQ,"The number of variable options on this line makes no sense to me2\n");
308  cdEXIT(EXIT_FAILURE);
309  }
310 
311  fprintf( ioQQQ, "\n %s\n", chLine );
312  fprintf( ioQQQ, " Initial increment is%6.3f, the limits are%10.2e to %10.2e\n",
313  optimize.vincr[i], optimize.varang[i][0], optimize.varang[i][1] );
314  }
315 
316  /* this will be number of times grid calls cloudy */
317  optimize.nOptimiz = 0;
318  /* >>chng 06 jan 26, moved this to an option on the command line */
319  /* grid.numParamValues = 10; */
320 
321  if( strcmp(optimize.chOptRtn,"XSPE") == 0 )
322  {
323  for( j=0; j < optimize.nvary; j++ )
324  {
325  /* ptem[j] = optimize.vparm[0][j]; */
326  ptem[j] = optimize.varang[j][0];
327  /* >> chng 06 sep 4, delta is now read at command line. */
328  /* delta[j] = optimize.vincr[j]; */
329  /* delta[j] = ( optimize.varang[j][1] - optimize.varang[j][0] )/
330  ((realnum)grid.numParamValues[j] - 1.f); */
331  }
332  /* >>chng 06 aug 23, fill rest of array with zeros. */
333  for( j=optimize.nvary; j < LIMPAR; j++ )
334  {
335  ptem[j] = 0.f;
336  grid.paramIncrements[j] = 0.f;
337  }
338 
339  gridXspec(ptem,optimize.nvary);
340  for( j=0; j < optimize.nvary; j++ )
341  {
342  optimize.vparm[0][j] = ptem[j];
343  }
344 
345  fprintf( ioQQQ, " **************************************************\n" );
346  fprintf( ioQQQ, " **************************************************\n" );
347  fprintf( ioQQQ, " **************************************************\n" );
348  fprintf( ioQQQ, "\n Cloudy was called %4ld times.\n\n", optimize.nOptimiz );
349 
350  /* though a page eject */
351  fprintf( ioQQQ, "\f" );
352  }
353  else
354  {
356  }
357 
358  if( lgAbort )
359  {
360  /* busted set means there were serious problems somewhere */
361  return 1;
362  }
363  else
364  {
365  /* return 0 if everything is ok */
366  return 0;
367  }
368 }

Generated for cloudy by doxygen 1.8.1.2