cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cloudy.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2017 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 /*cloudy the main routine, this IS Cloudy, return 0 normal exit, 1 error exit,
4  * called by maincl when used as standalone program */
5 /*BadStart announce that things are so bad the calculation cannot even start */
6 #include "cddefines.h"
7 #include "save.h"
8 #include "noexec.h"
9 #include "lines.h"
10 #include "abund.h"
11 #include "continuum.h"
12 #include "warnings.h"
13 #include "atmdat.h"
14 #include "prt.h"
15 #include "conv.h"
16 #include "parse.h"
17 #include "dynamics.h"
18 #include "init.h"
19 #include "opacity.h"
20 #include "state.h"
21 #include "rt.h"
22 #include "monitor_results.h"
23 #include "zones.h"
24 #include "iterations.h"
25 #include "plot.h"
26 #include "radius.h"
27 #include "grid.h"
28 #include "cloudy.h"
29 #include "ionbal.h"
30 #include "called.h"
31 #include "dense.h"
32 
33 /*BadStart announce that things are so bad the calculation cannot even start */
34 STATIC void BadStart();
35 
36 /* returns 1 if disaster strikes, 0 if everything appears ok */
37 bool cloudy()
38 {
39  DEBUG_ENTRY( "cloudy()" );
40 
41  bool lgOK;
42 
43  /*
44  * this is the main routine to drive the modules that compute a model
45  * when cloudy is used as a stand-alone program
46  * the main program (maincl) calls cdInit then cdDrive
47  * this sub is called by cdDrive which returns upon exiting
48  *
49  * this routine uses the following variables:
50  *
51  * nzone
52  * this is the zone number, and is incremented here
53  * is zero during search phase, 1 for first zone at illuminated face
54  * logical function iter_end_check returns a true condition if NZONE reaches
55  * NEND(ITER), the limit to the number of zones in this iteration
56  * nzone is totally controlled in this subroutine
57  *
58  * iteration
59  * this is the iteration number, it is 1 for the first iteration
60  * and is incremented in this subroutine at the end of each iteration
61  *
62  * iterations.itermx
63  * this is the limit to the number of iterations, and is entered
64  * by the user
65  * This routine returns when iteration > iterations.itermx
66  */
67 
68  /* nzone is zero while initial search for conditions takes place
69  * and is incremented to 1 for start of first zone */
70  nzone = 0;
71  fnzone = 0.;
72 
73  /* iteration is iteration number, calculation is complete when iteration > iterations.itermx */
74  iteration = 1;
75 
76  /* this initializes variables at the start of each simulation
77  * in a grid, before the parser is called - this must set any values
78  * that may be changed by the command parser */
80 
81  /* Read the isotope data and allocate the required space */
82  LoadIsotopes();
83 
84  /* scan in and parse input data */
85  ParseCommands();
86 
87  /* fix abundances of elements, in abundances.cpp */
88  AbundancesSet();
89 
91 
92  /* one time creation of some variables */
94 
95  /* initialize vars that can only be done after parsing the commands
96  * sets up CO network since this depends on which elements are on
97  * and whether chemistry is enabled */
99 
100  /* ContCreateMesh calls fill to set up continuum energy mesh if first call,
101  * otherwise reset to original mesh.
102  * This is AFTER ParseCommands so that
103  * path and mesh size can be set with commands before mesh is set */
104  ContCreateMesh();
105 
106  /* create several data sets by allocating memory and reading in data files,
107  * but only if this is first call */
108  atmdat_readin();
109 
110  /* fix pointers to ionization edges and frequency array
111  * calls iso_create
112  * this routine only returns if this is a later call of code */
114 
116 
117  /* Badnell_rec_init This code is written by Terry Yun, 2005 *
118  * It reads dielectronic recombination rate coefficient fits into 3D arrays */
120 
122 
123  /* set continuum normalization, continuum itself, and ionization stage limits */
125 
127 
128  SetPrintLineCol();
129 
130  /* print header */
131  PrtHeader();
132 
133  /* this is an option to stop after initial setup */
134  if( noexec.lgNoExec )
135  return false;
136 
137  /* guess some outward optical depths, set inward optical depths,
138  * also calls RT_line_all so escape probabilities ok before printout of trace */
139  RT_tau_init();
140 
141  /* generate initial set of opacities, but only if this is the first call
142  * in this core load
143  * grains only exist AFTER this routine exits */
145 
147 
148  /* this checks that various parts of the code still work properly */
149  SanityCheck("begin");
150 
151  /* option to recover state from previous calculation */
152  if( state.lgGet_state )
153  state_get_put( "get" );
154 
156 
157  /* find the initial temperature, punt if initial conditions outside range of code,
158  * abort condition set by flag lgAbort */
159  if( ConvInitSolution() )
160  {
161  // create line stacks for possible printout before bailing out
162  LineStackCreate();
163  BadStart();
164  return true;
165  }
166  // create line stacks ...
167  LineStackCreate();
168 
169  /* set thickness of first zone */
170  radius_first();
171 
172  /* find thickness of next zone */
173  if( radius_next() )
174  {
175  BadStart();
176  return true;
177  }
178 
179  /* set up some zone variables, correct continuum for sphericity,
180  * after return, radius is equal to the inner radius, not outer radius
181  * of this zone */
182  ZoneStart("init");
183 
184  /* print all abundances, gas phase and grains, in abundances.c */
185  /* >>chng 06 mar 05, move AbundancesPrt() after ZoneStart("init") so that
186  * GrnVryDpth() gives correct values for the inner face of the cloud, PvH */
187  AbundancesPrt();
188 
189  /* this is an option to stop after printing header only */
190  if( prt.lgOnlyHead )
191  return false;
192 
193  plot("FIRST");
194 
195  /* outer loop is over number of iterations
196  * >>chng 05 mar 12, chng from test on true to not aborting */
197  while( !lgAbort )
198  {
199  IterStart();
200  nzone = 0;
201  fnzone = 0.;
202 
203  /* loop over zones across cloud for this iteration,
204  * iter_end_check checks whether model is complete and this iteration is done
205  * returns true is current iteration is complete
206  * calls PrtZone to print zone results */
207  while( !iter_end_check() )
208  {
209  /* the zone number, 0 during search phase, first zone is 1 */
210  ++nzone;
211  /* this is the zone number plus the number of calls to bottom solvers
212  * from top pressure solver, divided by 100 */
213  fnzone = (double)nzone;
214 
215  /* use changes in opacity, temperature, ionization, to fix new dr for next zone */
216  /* >>chng 03 oct 29, move radius_next up to here from below, so that
217  * precise correct zone thickness will be used in current zone, so that
218  * column density and thickness will be exact
219  * abort condition is possible */
220  if( radius_next() )
221  break;
222 
223  /* following sets zone thickness, drad, to drnext */
224  /* set variable dealing with new radius, in zones.c */
225  ZoneStart("incr");
226 
227  /* converge the pressure-temperature-ionization solution for this zone
228  * NB ignoring return value - should be ok (return 1 for abort)
229  * we can't break here in case of abort since there is still housekeeping
230  * that must be done in following routines */
232 
233  /* generate diffuse emission from this zone, add to outward & reflected beams */
234  RT_diffuse();
235 
236  /* do work associated with incrementing this radius,
237  * total attenuation and dilution of radiation fields takes place here,
238  * reflected continuum incremented here
239  * various mean and extremes of solution are also remembered here */
241 
242  /* attenuation of diffuse and beamed continua */
243  RT_continuum();
244 
245  /* increment optical depths
246  * final evaluation of line rates and cooling */
247  RT_tau_inc();
248 
249  /* fill in emission line array, adds outward lines */
250  /* >>chng 99 dec 29, moved to here from below RT_tau_inc,
251  * lines adds lines to outward beam,
252  * and these are attenuated in radius_increment */
253  lines();
254 
255  /* possibly save out some results from this zone */
256  SaveDo("MIDL");
257 
258  /* do some things to finish out this zone */
259  ZoneEnd();
260 
261  // default false due to slow time - set true with set check energy every zone
262  // to allow per zone check of energy conservation, relatively slow
263  // when chianti is fully enabled
265  {
266  /* Ensure that energy has been conserved. Complain and punt if not */
267  if( !lgConserveEnergy() )
268  {
269  fprintf( ioQQQ, " PROBLEM DISASTER Energy was not conserved at zone %li\n", nzone );
270  ShowMe();
271  lgAbort = true;
272  }
273  }
274  }
275  /* end loop over zones */
276 
277  /* close out this iteration, in iter_startend.c */
278  IterEnd();
279 
280  /* print out some comments, generate warning and cautions*/
281  PrtComment();
282 
283  /* save stuff only needed on completion of this iteration */
284  SaveDo("LAST" );
285 
286  /* second call to plot routine, to complete plots for this iteration */
287  plot("SECND");
288 
289  /* print out the results */
290  PrtFinal();
291 
292  /*ConvIterCheck check whether model has converged or more iterations
293  * are needed - implements the iter to convergence command
294  * acts by setting iterations.itermx to iteration if converged*/
295  ConvIterCheck();
296 
297  /* option to save state */
298  if( state.lgPut_state )
299  state_get_put( "put" );
300 
301  /* >>chng 06 mar 03 move block to here, had been after PrtFinal,
302  * so that save state will save reset optical depths */
303  /* this is the normal exit, occurs if we reached limit to number of iterations,
304  * or if code has set busted */
305  /* >>chng 06 mar 18, add flag for time dependent simulation completed */
307  break;
308 
309  /* reset limiting and initial optical depth variables */
310  RT_tau_reset();
311 
312  /* increment iteration counter */
313  ++iteration;
314 
315  /* reinitialize some variables to initial conditions at previous first zone
316  * routine in startenditer.c */
317  IterRestart();
318 
319  /* reset zone number to 0 - done here since this is only routine that sets nzone */
320  nzone = 0;
321  fnzone = 0.;
322 
323  ZoneStart("init");
324 
325  /* find new initial temperature, punt if initial conditions outside range of code,
326  * if ConvInitSolution() fails, lgAbort will always be true, so we check that */
327  if( ConvInitSolution() )
328  break;
329  }
330 
331  CloseSaveFiles( false );
332 
333  /* this checks that various parts of the code worked properly */
334  SanityCheck("final");
335 
336  if( called.lgTalk )
337  {
338  fprintf(ioQQQ,"---------------Convergence statistics---------------\n");
339  fprintf(ioQQQ,"%10.3g mean iterations/state convergence\n",((double)conv.getCounter("CONV_BASE_LOOPS"))/
340  (MAX2((double)conv.getCounter("CONV_BASE_CALLS"),1.0)));
341  fprintf(ioQQQ,"%10.3g mean cx acceleration loops/iteration\n",((double)conv.getCounter("CONV_BASE_ACCELS"))/
342  (MAX2((double)conv.getCounter("CONV_BASE_LOOPS"),1.0)));
343  fprintf(ioQQQ,"%10.3g mean iso convergence loops/ion solve\n",((double)conv.getCounter("ISO_LOOPS"))/
344  (MAX2((double)conv.getCounter("ION_SOLVES"),1.0)));
345  fprintf(ioQQQ,"%10.3g mean steps/chemistry solve\n",((double)conv.getCounter("MOLE_SOLVE_STEPS"))/
346  (MAX2((double)conv.getCounter("MOLE_SOLVE"),1.0)));
347  fprintf(ioQQQ,"%10.3g mean step length searches/chemistry step\n",((double)conv.getCounter("NEWTON_LOOP"))/
348  (MAX2((double)conv.getCounter("NEWTON"),1.0)));
349  fprintf(ioQQQ,"----------------------------------------------------\n\n");
350  }
351 
352  /* check whether any asserts were present and failed.
353  * return is true if ok, false if not. routine also checks
354  * number of warnings and returns false if not ok */
355  lgOK = lgCheckMonitors(ioQQQ);
356 
357  if( lgOK && !warnings.lgWarngs && !lgAbort )
358  {
359  /* no failed asserts or warnings */
360  return false;
361  }
362  else
363  {
364  /* there were failed asserts or warnings */
365  return true;
366  }
367 }
368 
369 /*BadStart announce that things are so bad the calculation cannot even start */
371 {
372  char chLine[INPUT_LINE_LENGTH];
373 
374  DEBUG_ENTRY( "BadStart()" );
375 
376  /* initialize the line saver */
377  warnings.zero();
378  sprintf( warnings.chRgcln[0], " Calculation stopped because initial conditions out of bounds." );
379  sprintf( chLine, " W-Calculation could not begin." );
380  warnin(chLine);
381 
382  if( grid.lgGrid )
383  {
384  /* possibly save out some results from this zone when doing grid
385  * since grid save files expect something at this grid point */
386  SaveDo("MIDL");
387  SaveDo("LAST" );
388  }
389  return;
390 }
void InitSimPostparse(void)
void plot(const char *chCall)
Definition: plot.cpp:39
void PrtFinal(void)
Definition: prt_final.cpp:555
bool lgPut_state
Definition: state.h:29
void radius_first(void)
bool lgGrid
Definition: grid.h:42
bool cloudy()
Definition: cloudy.cpp:37
void ZoneEnd(void)
void OpacityCreateAll(void)
bool lgCheckEnergyEveryZone
Definition: continuum.h:102
void AbundancesSet(void)
Definition: abundances.cpp:145
STATIC void BadStart()
Definition: cloudy.cpp:370
t_warnings warnings
Definition: warnings.cpp:11
int iter_end_check(void)
t_conv conv
Definition: conv.cpp:5
void CloseSaveFiles(bool lgFinal)
t_noexec noexec
Definition: noexec.cpp:4
FILE * ioQQQ
Definition: cddefines.cpp:7
void RT_tau_inc(void)
Definition: rt_tau_inc.cpp:40
void SanityCheck(const char *chJob)
long int nzone
Definition: cddefines.cpp:14
bool lgTalk
Definition: called.h:12
char chRgcln[2][INPUT_LINE_LENGTH]
Definition: warnings.h:34
t_dynamics dynamics
Definition: dynamics.cpp:42
bool lgNoExec
Definition: noexec.h:14
void ZoneStart(const char *chMode)
bool lgStatic_completed
Definition: dynamics.h:111
void IterEnd(void)
void SaveDo(const char *chTime)
Definition: save_do.cpp:817
void warnin(const char *chLine)
Definition: warnings.h:74
void lines(void)
Definition: prt_lines.cpp:35
void RT_diffuse(void)
Definition: rt_diffuse.cpp:35
void ParseCommands(void)
long int iteration
Definition: cddefines.cpp:16
void RT_tau_reset(void)
void Badnell_rec_init(void)
void ContCreatePointers(void)
void IterRestart(void)
void ContCreateMesh()
void AbundancesPrt(void)
Definition: abundances.cpp:31
void PrtComment(void)
Definition: prt_comment.cpp:66
#define STATIC
Definition: cddefines.h:118
bool lgConserveEnergy()
Definition: energy.cpp:311
t_continuum continuum
Definition: continuum.cpp:6
void RT_continuum(void)
long getCounter(const long type) const
Definition: conv.h:327
void LineStackCreate(void)
bool lgOnlyHead
Definition: prt.h:224
const int INPUT_LINE_LENGTH
Definition: cddefines.h:301
void resolveLevels()
Definition: prt.cpp:222
void PrtHeader(void)
Definition: prt_header.cpp:17
t_iterations iterations
Definition: iterations.cpp:6
t_state state
Definition: state.cpp:18
t_grid grid
Definition: grid.cpp:5
t_prt prt
Definition: prt.cpp:14
void radius_increment(void)
long int itermx
Definition: iterations.h:37
#define ASSERT(exp)
Definition: cddefines.h:617
void ConvIterCheck(void)
t_prt_matrix matrix
Definition: prt.h:238
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:729
bool lgGet_state
Definition: state.h:29
int radius_next(void)
void RT_tau_init(void)
Definition: rt_tau_init.cpp:28
int ConvPresTempEdenIoniz(void)
#define MAX2(a, b)
Definition: cddefines.h:828
void state_get_put(const char chJob[])
Definition: state.cpp:83
bool lgCheckMonitors(FILE *ioMONITOR)
void atmdat_readin(void)
int fprintf(const Output &stream, const char *format,...)
Definition: service.cpp:1217
void zero(void)
Definition: warnings.cpp:13
void InitDefaultsPreparse(void)
bool lgElemsConserved(void)
Definition: dense.cpp:119
void LoadIsotopes()
Definition: isotopes.cpp:9
double fnzone
Definition: cddefines.cpp:15
void ShowMe(void)
Definition: service.cpp:205
void InitCoreloadPostparse(void)
bool ConvInitSolution()
bool lgWarngs
Definition: warnings.h:44
t_called called
Definition: called.cpp:4
void IterStart(void)
bool lgAbort
Definition: cddefines.cpp:10
void SetPrintLineCol()
Definition: prt.cpp:28
void ContSetIntensity()