cloudy trunk
Loading...
Searching...
No Matches
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 */
19bool 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 */
62
63 /* >>chng 00 aug 09, return memory allocated for grains, they are not used, PvH */
64 gv.clear();
65
66 optimize.nvary = optimize.nparm;
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 {
73 optimize.vincr[i] = optimize.OptIncrm[i];
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",
230 optimize.nIterOptim );
231 fprintf( ioQQQ, " and the final version of the input file will be written to the file %s\n",
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",
252 optimize.nIterOptim );
253 fprintf( ioQQQ, " and the final version of the input file will be written to the file %s\n",
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",
327 grid.paramIncrements[i], grid.paramLimits[i][0], grid.paramLimits[i][1] );
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_called called
Definition called.cpp:5
FILE * ioQQQ
Definition cddefines.cpp:7
bool lgAbort
Definition cddefines.cpp:10
const int INPUT_LINE_LENGTH
Definition cddefines.h:254
#define EXIT_FAILURE
Definition cddefines.h:140
#define cdEXIT(FAIL)
Definition cddefines.h:434
float realnum
Definition cddefines.h:103
#define DEBUG_ENTRY(funcname)
Definition cddefines.h:684
static t_version & Inst()
Definition cddefines.h:175
t_conv conv
Definition conv.cpp:5
static t_cpu cpu
Definition cpu.h:355
GrainVar gv
Definition grainvar.cpp:5
t_grid grid
Definition grid.cpp:5
void gridXspec(realnum *, long)
bool grid_do()
Definition grid_do.cpp:19
void InitDefaultsPreparse(void)
t_input input
Definition input.cpp:12
char chOptimFileName[INPUT_LINE_LENGTH]
Definition optimize.cpp:6
t_optimize optimize
Definition optimize.cpp:5
bool lgOptimize_do(void)
const long LIMPAR
Definition optimize.h:61
void ParseCommands(void)
t_prt prt
Definition prt.cpp:10
t_trace trace
Definition trace.cpp:5