00001 /* This file is part of Cloudy and is copyright (C)1978-2008 by Gary J. Ferland and 00002 * others. For conditions of distribution and use see copyright notice in license.txt */ 00003 /*cdInit routine to initialize variables, called at start of calculation */ 00004 /*cdExit exit handler to terminate calculation, called at end of calculation */ 00005 /* unset EXTERN so that everything is defined here */ 00006 #define EXTERN 00007 #include "cddefines.h" 00008 00009 /* used for punching map*/ 00010 FILE *ioMAP = NULL; 00011 00012 /* external ZeroNum used to div by zero 00013 * ok here since never changed*/ 00014 const double ZeroNum = 0.; 00015 00016 /* this must go here since it defines NTA needed for other lines*/ 00017 #include "taulines.h" 00018 00019 /* following is true extern in taulines.h */ 00020 long nWindLine = NWINDDIM; 00021 00022 /* set number of CO rotation levels to default, 20 as set in taulines.h */ 00023 long int nCORotate = NCOROTATE; 00024 00025 /* say that space not allocated yet */ 00026 bool lgCORotateMalloc = false; 00027 00028 #include "abund.h" 00029 #include "atmdat.h" 00030 #include "atoms.h" 00031 #include "atomfeii.h" 00032 #include "assertresults.h" 00033 #include "broke.h" 00034 #include "ca.h" 00035 #include "called.h" 00036 #include "carb.h" 00037 #include "cddrive.h" 00038 /* this will be set true when cdInit is called. The definition is in cdInit. 00039 * Other routines will check that this is true when they are called, 00040 * to verify that cdInit was called first */ 00041 bool lgcdInitCalled=false; 00042 #include "colden.h" 00043 #include "conv.h" 00044 #include "continuum.h" 00045 #include "coolheavy.h" 00046 #include "dense.h" 00047 #include "doppvel.h" 00048 #include "dynamics.h" 00049 #include "elementnames.h" 00050 #include "embesq.h" 00051 #include "extinc.h" 00052 #include "fe.h" 00053 #include "fudgec.h" 00054 #include "geometry.h" 00055 #include "grainvar.h" 00056 #include "grid.h" 00057 #include "h2.h" 00058 #include "h2_priv.h" 00059 bool lgH2_READ_DATA = false; 00060 #include "he.h" 00061 #include "heavy.h" 00062 #include "hextra.h" 00063 #include "hmi.h" 00064 #include "hydrogenic.h" 00065 /* this is set true once space malloced, then never change 00066 * number of levels again with hydrogenic command, 00067 * also to make sure MALLOC only happens one time */ 00068 bool lgHydroMalloc = false; 00069 /* */ 00070 #include "hyperfine.h" 00071 #include "input.h" 00072 #include "ionbal.h" 00073 #include "iso.h" 00074 #include "iterations.h" 00075 #include "lines.h" 00076 /* these are the definitions of the line save arrays in lines.h */ 00077 LinSv *LineSv=NULL; 00078 #include "magnetic.h" 00079 #include "hcmap.h" 00080 #include "mean.h" 00081 #include "mewecoef.h" 00082 #include "mole.h" 00083 #include "nitro.h" 00084 #include "noexec.h" 00085 #include "numderiv.h" 00086 #include "oxy.h" 00087 #include "parse.h" 00088 #include "peimbt.h" 00089 #include "phycon.h" 00090 #include "plot.h" 00091 #include "sil.h" 00092 #include "version.h" 00093 /* this is set true when space is allocated for the FeII arrays, 00094 * once this happens FeII.nFeIILevel cannot be changed with the atom feii levels command */ 00095 bool lgFeIIMalloc=false; 00096 /* */ 00097 #include "pressure.h" 00098 #include "prt.h" 00099 #include "punch.h" 00100 #include "radius.h" 00101 #include "rfield.h" 00102 /* set true when malloced, init to false */ 00103 bool lgRfieldMalloced=false; 00104 #include "opacity.h" 00105 bool lgOpacMalloced=false; 00106 #include "rt.h" 00107 #include "secondaries.h" 00108 #include "state.h" 00109 #include "stopcalc.h" 00110 #include "struc.h" 00111 #include "thermal.h" 00112 #include "timesc.h" 00113 #include "trace.h" 00114 #include "warnings.h" 00115 #include "wind.h" 00116 #include "init.h" 00117 /* include MPI header if MPI_ENABLED true 00118 * This flag indicates whether we are multi-processing. 00119 * if this is true then all output happens at the end, 00120 * if false then each set of results is printed and flushed when it happens. 00121 * must be set true on parallel machines, false on serial */ 00122 #ifdef MPI_ENABLED 00123 # include <mpi.h> 00124 #endif 00125 00126 /* flag set true when cdMPI called, tells exit handler to clean up MPI */ 00127 /* lint error saying never used for anything is fine -- 00128 * only is used if MPI flags are set, this is so that we do not call 00129 * MPI_Finalize when on serial queue on parallel machine */ 00130 static bool lgMPI=false; 00131 00132 /*cdMPI sets flag telling exit handler to call MPI_Finalize, must 00133 * call after cdInit when MPI is used */ 00134 void cdMPI(void) /* set flag so that exit handler will clean up MPI */ 00135 { 00136 lgMPI = true; 00137 return; 00138 } 00139 00140 /* =================================================================== */ 00141 void cdInit(void) 00142 { 00143 long i; 00144 double vtest; 00145 00146 DEBUG_ENTRY( "cdInit()" ); 00147 00148 /* set ioQQQ to standard output */ 00149 ioQQQ = stdout; 00150 00151 /* set ioStdin to standard input, will get line images from here */ 00152 ioStdin = stdin; 00153 00154 /* set ioPrnErr to stderr */ 00155 ioPrnErr = stderr; 00156 00157 /* but don't want to usually print to stderr */ 00158 lgPrnErr = false; 00159 00160 /* set flag saying that cdInit has been called */ 00161 lgcdInitCalled = true; 00162 00163 /*test if the following integer types have the correct width*/ 00164 if( sizeof(int16) != 2 || sizeof(uint16) != 2 || sizeof(int32) != 4 || sizeof(uint32) != 4 ) 00165 TotalInsanity(); 00166 00167 /********************************************************* 00168 * on a VAX compile with /G_FLOATING option on FORTRAN; * 00169 * following makes sure this happened. * 00170 *********************************************************/ 00171 vtest = 1e-35; 00172 vtest /= 1e35; 00173 if( vtest == 0. ) 00174 { 00175 fprintf( ioQQQ, " Something is wrong with the double precision. Use /g_floating on a VAX\n" ); 00176 } 00177 00178 /* initialize some variables dealing with cloudy's interaction with machine environment */ 00179 /* if TALK is true then do standard printout 00180 * if false then never say anything */ 00181 called.lgTalk = true; 00182 /* this flag is needed to turn print on to have effect */ 00183 called.lgTalkIsOK = true; 00184 /* means talk not forced off by call to cdTalk*/ 00185 called.lgTalkForcedOff = false; 00186 00187 optimize.lgNoVary = false; 00188 optimize.lgVaryOn = false; 00189 optimize.lgOptimr = false; 00190 grid.lgGrid = false; 00191 grid.nGridCommands = 0; 00192 00193 for( i=0; i<NUM_OUTPUT_TYPES; i++ ) 00194 { 00195 grid.lgOutputTypeOn[i] = false; 00196 } 00197 00198 for( i=0; i<LIMPAR; i++ ) 00199 { 00200 grid.numParamValues[i] = 0; 00201 } 00202 00203 /* this is a global variable in assertresults.h, and can be checked by 00204 * other routines to see if asserts are ok - (most calculations will not use asserts, 00205 * and this will be the only place values are set, although they will be checked in maincl) */ 00206 lgAssertsOK = true; 00207 lgBigBotch = false; 00208 lgPrtSciNot = false; 00209 00210 /* number of lines entered with cdLine 00211 * both check that number less than NKRD, the limit 00212 * the line save array is defined from 0 through input.nSave */ 00213 input.nSave = -1; 00214 00215 /* nRead is the number of the command in the input stream - many optimize options 00216 * point to it to refer to the original command. it is incremented before 00217 * it is used, so will become 0. it is the array element within the stack 00218 * of emission lines */ 00219 input.nRead = -1; 00220 00221 /* this is number of init lines read in */ 00222 input.nSaveIni = 0; 00223 input.lgUnderscoreFound = false; 00224 input.lgBracketFound = false; 00225 00226 /* this is sanity check that lines are read in ok */ 00227 for( i=0; i < NKRD; i++ ) 00228 { 00229 strcpy( input.chCardSav[i], "error! - no line image input" ); 00230 } 00231 00232 /* start the timer to log execution time */ 00233 cdSetExecTime(); 00234 00235 /* zero out lots of variables */ 00236 zero(); 00237 return; 00238 } 00239 00240 00241 /* =================================================================== */ 00242 /*cdExit the routine that should be called to exit cloudy */ 00243 NORETURN void cdExit( 00244 /* EXIT_FAILURE for failure, EXIT_SUCCESS for success */ 00245 int iexit) 00246 { 00247 enum {DEBUG_LOC=false}; 00248 if( DEBUG_LOC ) 00249 fprintf(ioQQQ," cdExit called\n"); 00250 00251 /* if doing an MPI run, just throw an exception 00252 * so that cddrive can report the problem and continue 00253 * computing other grid points */ 00254 if( lgMPI ) 00255 { 00256 throw bad_mpi(-1); 00257 } 00258 else 00259 { 00260 /* close any open units */ 00261 ClosePunchFiles( true ); 00262 exit( iexit ); 00263 } 00264 } 00265