cloudy trunk
|
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 /*state_get_put get or save state - called by cloudy - job is either "get" or "put" */ 00004 /*state_do - worker to actually get or put the structure - 00005 * called by state_get_put below */ 00006 #include "cddefines.h" 00007 #include "taulines.h" 00008 #include "iso.h" 00009 #include "struc.h" 00010 #include "mole.h" 00011 #include "rfield.h" 00012 #include "iterations.h" 00013 #include "h2.h" 00014 #include "dense.h" 00015 #include "opacity.h" 00016 #include "atomfeii.h" 00017 #include "state.h" 00018 00019 static bool lgGet; 00020 static FILE *ioSTATE; 00021 00022 /*state_do - worker to actually get or put the structure - 00023 * called by state_get_put below */ 00024 STATIC void state_do( void *pnt , size_t sizeof_pnt ) 00025 { 00026 size_t n; 00027 double sanity = 1., 00028 chk_sanity; 00029 size_t sizeof_sanity =sizeof( sanity ); 00030 00031 DEBUG_ENTRY( "state_do()" ); 00032 00033 /* nothing to do if this is not positive */ 00034 if( sizeof_pnt == 0 ) 00035 return; 00036 00037 if( lgGet ) 00038 { 00039 /* get option - read in data */ 00040 if( (n=fread( pnt , 1 , sizeof_pnt , ioSTATE )) - sizeof_pnt ) 00041 { 00042 fprintf( ioQQQ, " state_do failed reading state file, wanted %lu got %lu\n", 00043 (unsigned long)sizeof_pnt , 00044 (unsigned long)n); 00045 cdEXIT(EXIT_FAILURE); 00046 } 00047 /* perform sanity check */ 00048 if( (n=fread( &chk_sanity , 1 , sizeof_sanity , ioSTATE )) - sizeof_sanity ) 00049 { 00050 fprintf( ioQQQ, " state_do failed reading sanity par of state file, wanted %lu got %lu\n", 00051 (unsigned long)sizeof_sanity , 00052 (unsigned long)n); 00053 cdEXIT(EXIT_FAILURE); 00054 } 00055 if( ! fp_equal( sanity, chk_sanity ) ) 00056 { 00057 fprintf( ioQQQ, " state_do sanity fails in state file, wanted %g got %g\n", 00058 sanity , 00059 chk_sanity); 00060 cdEXIT(EXIT_FAILURE); 00061 } 00062 } 00063 else 00064 { 00065 /* put option - write out data */ 00066 fwrite( pnt , 1 , sizeof_pnt , ioSTATE ); 00067 /* write sanity check */ 00068 fwrite( &sanity , 1 , sizeof_sanity , ioSTATE ); 00069 } 00070 00071 return; 00072 } 00073 00074 /*state_get_put get or save state - called by cloudy - job is either "get" or "put" */ 00075 void state_get_put( const char chJob[] ) 00076 { 00077 long int ipISO , nelem , ipHi, i , 00078 n , ion; 00079 00080 DEBUG_ENTRY( "state_get_put()" ); 00081 00082 if( (strcmp( chJob , "get" ) == 0) ) 00083 { 00084 lgGet = true; 00085 ioSTATE = open_data( state.chGetFilename, "rb", AS_LOCAL_ONLY ); 00086 } 00087 else if( (strcmp( chJob , "put" ) == 0) ) 00088 { 00089 lgGet = false; 00090 char chFilename[INPUT_LINE_LENGTH]; 00091 if( !state.lgPutAll && iteration <= iterations.itermx ) 00092 { 00093 /* not last iteration and do not want to save state for all iters, so simply quit */ 00094 return; 00095 } 00096 00097 /* get base of file name */ 00098 strcpy( chFilename , state.chPutFilename ); 00099 /* append iteration number if ALL keyword appears */ 00100 if( state.lgPutAll ) 00101 { 00102 char chIteration[INPUT_LINE_LENGTH]; 00103 sprintf( chIteration , "_%li", iteration ); 00104 strcat( chFilename , chIteration ); 00105 } 00106 ioSTATE = open_data( chFilename, "wb", AS_LOCAL_ONLY ); 00107 } 00108 else 00109 TotalInsanity(); 00110 00111 if( state.lgState_print ) 00112 fprintf(ioQQQ," Print state quantities, start iso seq \n"); 00113 00114 /* actually do the read / write */ 00115 Transitions.state_do( ioSTATE, lgGet ); 00116 ExtraLymanLines.state_do( ioSTATE, lgGet ); 00117 00118 for( ipISO=ipH_LIKE; ipISO<NISO; ++ipISO ) 00119 { 00120 /* loop over all iso-electronic sequences */ 00121 for( nelem=ipISO; nelem<LIMELM; ++nelem ) 00122 { 00123 if( nelem < 2 || dense.lgElmtOn[nelem] ) 00124 { 00125 /* arrays are dim'd iso.numLevels_max[ipH_LIKE][nelem]+1 */ 00126 for( ipHi=1; ipHi < iso.numLevels_max[ipISO][nelem]; ++ipHi ) 00127 { 00128 if( state.lgState_print ) 00129 { 00130 fprintf(ioQQQ," start ISO ipISO= %li, nelem= %li, ipHi %li \n", 00131 ipISO , 00132 nelem , 00133 ipHi); 00134 for( n=0; n< ipHi; ++n ) 00135 { 00136 fprintf(ioQQQ," ISO %li %li %li %li %.4e %.4e \n", 00137 ipISO , nelem , ipHi , n , 00138 Transitions[ipISO][nelem][ipHi][n].Emis->TauIn , 00139 Transitions[ipISO][nelem][ipHi][n].Emis->TauTot ); 00140 } 00141 fprintf(ioQQQ," end ISO ipISO\n"); 00142 } 00143 } 00144 00145 if( state.lgState_print ) 00146 { 00147 fprintf(ioQQQ," start Ext ipISO= %li, nelem= %li, got %li \n", 00148 ipISO , 00149 nelem , 00150 iso.nLyman_malloc[ipISO] ); 00151 } 00152 if( state.lgState_print ) 00153 { 00154 for( n=2; n< iso.nLyman_malloc[ipISO]; ++n ) 00155 { 00156 fprintf(ioQQQ," Ext %li %li %li %.4e %.4e \n", 00157 ipISO , nelem , n , 00158 ExtraLymanLines[ipISO][nelem][n].Emis->TauIn , 00159 ExtraLymanLines[ipISO][nelem][n].Emis->TauTot ); 00160 } 00161 fprintf(ioQQQ," end Ext ipISO\n"); 00162 } 00163 } 00164 } 00165 } 00166 00167 state_do( TauLines , (size_t)(nLevel1+1)*sizeof(transition ) ); 00168 if( state.lgState_print ) 00169 { 00170 for( n=0; n< (nLevel1+1); ++n ) 00171 { 00172 fprintf(ioQQQ," Taulines %li %.4e %.4e \n", 00173 n , 00174 TauLines[n].Emis->TauIn , 00175 TauLines[n].Emis->TauTot ); 00176 } 00177 } 00178 00179 state_do( TauLine2 , (size_t)nWindLine *sizeof(transition ) ); 00180 if( state.lgState_print ) 00181 { 00182 for( n=0; n< nWindLine; ++n ) 00183 { 00184 fprintf(ioQQQ," TauLine2 %li %.4e %.4e \n", 00185 n , 00186 TauLine2[n].Emis->TauIn , 00187 TauLine2[n].Emis->TauTot ); 00188 } 00189 } 00190 00191 state_do( UTALines , (size_t)nUTA *sizeof(transition ) ); 00192 if( state.lgState_print ) 00193 { 00194 for( n=0; n< nUTA; ++n ) 00195 { 00196 fprintf(ioQQQ," UTALines %li %.4e %.4e \n", 00197 n , 00198 UTALines[n].Emis->TauIn , 00199 UTALines[n].Emis->TauTot ); 00200 } 00201 } 00202 00203 state_do( HFLines , (size_t)nHFLines *sizeof(transition ) ); 00204 if( state.lgState_print ) 00205 { 00206 for( n=0; n< nHFLines; ++n ) 00207 { 00208 fprintf(ioQQQ," HFLines %li %.4e %.4e \n", 00209 n , 00210 HFLines[n].Emis->TauIn , 00211 HFLines[n].Emis->TauTot ); 00212 } 00213 } 00214 00215 state_do( C12O16Rotate, (size_t)nCORotate *sizeof(transition ) ); 00216 if( state.lgState_print ) 00217 { 00218 for( n=0; n< nCORotate; ++n ) 00219 { 00220 fprintf(ioQQQ," C12O16Rotate %li %.4e %.4e \n", 00221 n , 00222 C12O16Rotate[n].Emis->TauIn , 00223 C12O16Rotate[n].Emis->TauTot ); 00224 } 00225 } 00226 00227 state_do( C13O16Rotate, (size_t)nCORotate *sizeof(transition ) ); 00228 if( state.lgState_print ) 00229 { 00230 for( n=0; n< nCORotate; ++n ) 00231 { 00232 fprintf(ioQQQ," C13O16Rotate %li %.4e %.4e \n", 00233 n , 00234 C13O16Rotate[n].Emis->TauIn , 00235 C13O16Rotate[n].Emis->TauTot ); 00236 } 00237 } 00238 00239 for( ipHi=1; ipHi < FeII.nFeIILevel; ++ipHi ) 00240 { 00241 state_do( Fe2LevN[ipHi] , (size_t)ipHi*sizeof(transition) ); 00242 if( state.lgState_print ) 00243 { 00244 for( n=0; n< ipHi; ++n ) 00245 { 00246 fprintf(ioQQQ," Fe2LevN %li %li %.4e %.4e \n", 00247 ipHi , n , 00248 Fe2LevN[ipHi][n].Emis->TauIn , 00249 Fe2LevN[ipHi][n].Emis->TauTot ); 00250 } 00251 } 00252 } 00253 for( i=0; i<2; ++i ) 00254 { 00255 state_do( opac.TauAbsGeo[i] , (size_t)rfield.nupper*sizeof(realnum) ); 00256 if( state.lgState_print ) 00257 { 00258 for( n=0; n< rfield.nupper; ++n ) 00259 { 00260 fprintf(ioQQQ," TauAbsGeo %li %li %.4e \n", 00261 i , n , 00262 opac.TauAbsGeo[i][n] ); 00263 } 00264 } 00265 00266 state_do( opac.TauScatGeo[i] , (size_t)rfield.nupper*sizeof(realnum) ); 00267 if( state.lgState_print ) 00268 { 00269 for( n=0; n< rfield.nupper; ++n ) 00270 { 00271 fprintf(ioQQQ," TauScatGeo %li %li %.4e \n", 00272 i , n , 00273 opac.TauAbsGeo[i][n] ); 00274 } 00275 } 00276 00277 state_do( opac.TauTotalGeo[i], (size_t)rfield.nupper*sizeof(realnum) ); 00278 if( state.lgState_print ) 00279 { 00280 for( n=0; n< rfield.nupper; ++n ) 00281 { 00282 fprintf(ioQQQ," TauTotalGeo %li %li %.4e \n", 00283 i , n , 00284 opac.TauAbsGeo[i][n] ); 00285 } 00286 } 00287 00288 } 00289 00290 /* the large H2 molecule, only if on */ 00291 if( h2.lgH2ON ) 00292 H2Lines.state_do( ioSTATE, lgGet ); 00293 00294 /* the struc variables */ 00295 state_do( &struc.nzlim, sizeof(struc.nzlim ) ); 00296 state_do( &struc.dr_ionfrac_limit, sizeof(struc.dr_ionfrac_limit ) ); 00297 state_do( &struc.nzone, sizeof(struc.nzone ) ); 00298 00299 state_do( struc.testr, (size_t)(struc.nzlim)*sizeof(realnum ) ); 00300 state_do( struc.volstr, (size_t)(struc.nzlim)*sizeof(realnum ) ); 00301 state_do( struc.drad_x_fillfac, (size_t)(struc.nzlim)*sizeof(realnum ) ); 00302 state_do( struc.drad, (size_t)(struc.nzlim)*sizeof(realnum ) ); 00303 state_do( struc.histr, (size_t)(struc.nzlim)*sizeof(realnum ) ); 00304 state_do( struc.hiistr, (size_t)(struc.nzlim)*sizeof(realnum ) ); 00305 state_do( struc.ednstr, (size_t)(struc.nzlim)*sizeof(realnum ) ); 00306 state_do( struc.o3str, (size_t)(struc.nzlim)*sizeof(realnum ) ); 00307 state_do( struc.pressure,(size_t)(struc.nzlim)*sizeof(realnum ) ); 00308 state_do( struc.GasPressure ,(size_t)(struc.nzlim)*sizeof(realnum ) ); 00309 state_do( struc.pres_radiation_lines_curr ,(size_t)(struc.nzlim)*sizeof(realnum ) ); 00310 state_do( struc.hden ,(size_t)(struc.nzlim)*sizeof(realnum ) ); 00311 state_do( struc.DenParticles ,(size_t)(struc.nzlim)*sizeof(realnum ) ); 00312 state_do( struc.DenMass,(size_t)(struc.nzlim)*sizeof(realnum ) ); 00313 state_do( struc.depth,(size_t)(struc.nzlim)*sizeof(realnum ) ); 00314 state_do( struc.xLyman_depth , (size_t)(struc.nzlim)*sizeof(realnum ) ); 00315 00316 state_do( struc.coolstr,(size_t)(struc.nzlim)*sizeof(double ) ); 00317 state_do( struc.heatstr , (size_t)(struc.nzlim)*sizeof(double ) ); 00318 00319 for( nelem=ipHYDROGEN; nelem<(LIMELM+3); ++nelem ) 00320 { 00321 for( ion=0; ion<(LIMELM+1); ++ion ) 00322 { 00323 state_do( struc.xIonDense[nelem][ion] , (size_t)(struc.nzlim)*sizeof(realnum ) ); 00324 } 00325 } 00326 00327 /* the hydrogen molecules */ 00328 /*realnum *Molec[N_H_MOLEC];*/ 00329 for( n=0; n<N_H_MOLEC; ++n ) 00330 { 00331 state_do( struc.H2_molec[n] , (size_t)(struc.nzlim)*sizeof(realnum ) ); 00332 } 00333 for( n=0; n<mole.num_comole_calc; ++n ) 00334 { 00335 state_do( struc.CO_molec[n] , (size_t)(struc.nzlim)*sizeof(realnum ) ); 00336 } 00337 00338 for( nelem=ipHYDROGEN; nelem<LIMELM; ++nelem ) 00339 { 00340 state_do( struc.gas_phase[nelem] , (size_t)(struc.nzlim)*sizeof(realnum ) ); 00341 } 00342 00343 /*fprintf(ioQQQ,"DEBUG done\n");*/ 00344 00345 /* close the file */ 00346 fclose( ioSTATE ); 00347 return; 00348 }