cloudy  trunk
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
state.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 /*state_get_put get or save state - called by cloudy - job is either "get" or "put" */
4 /*state_do - worker to actually get or put the structure -
5  * called by state_get_put below */
6 #include "cddefines.h"
7 #include "taulines.h"
8 #include "iso.h"
9 #include "struc.h"
10 #include "mole.h"
11 #include "rfield.h"
12 #include "iterations.h"
13 #include "h2.h"
14 #include "dense.h"
15 #include "opacity.h"
16 #include "atomfeii.h"
17 #include "state.h"
18 
19 static bool lgGet;
20 static FILE *ioSTATE;
21 
22 /*state_do - worker to actually get or put the structure -
23  * called by state_get_put below */
24 STATIC void state_do( void *pnt , size_t sizeof_pnt )
25 {
26  size_t n;
27  double sanity = 1.,
28  chk_sanity;
29  size_t sizeof_sanity =sizeof( sanity );
30 
31  DEBUG_ENTRY( "state_do()" );
32 
33  /* nothing to do if this is not positive */
34  if( sizeof_pnt == 0 )
35  return;
36 
37  if( lgGet )
38  {
39  /* get option - read in data */
40  if( (n=fread( pnt , 1 , sizeof_pnt , ioSTATE )) - sizeof_pnt )
41  {
42  fprintf( ioQQQ, " state_do failed reading state file, wanted %lu got %lu\n",
43  (unsigned long)sizeof_pnt ,
44  (unsigned long)n);
45  cdEXIT(EXIT_FAILURE);
46  }
47  /* perform sanity check */
48  if( (n=fread( &chk_sanity , 1 , sizeof_sanity , ioSTATE )) - sizeof_sanity )
49  {
50  fprintf( ioQQQ, " state_do failed reading sanity par of state file, wanted %lu got %lu\n",
51  (unsigned long)sizeof_sanity ,
52  (unsigned long)n);
53  cdEXIT(EXIT_FAILURE);
54  }
55  if( ! fp_equal( sanity, chk_sanity ) )
56  {
57  fprintf( ioQQQ, " state_do sanity fails in state file, wanted %g got %g\n",
58  sanity ,
59  chk_sanity);
60  cdEXIT(EXIT_FAILURE);
61  }
62  }
63  else
64  {
65  /* put option - write out data */
66  fwrite( pnt , 1 , sizeof_pnt , ioSTATE );
67  /* write sanity check */
68  fwrite( &sanity , 1 , sizeof_sanity , ioSTATE );
69  }
70 
71  return;
72 }
73 
74 /*state_get_put get or save state - called by cloudy - job is either "get" or "put" */
75 void state_get_put( const char chJob[] )
76 {
77  long int ipISO , nelem , ipHi, i ,
78  n , ion;
79 
80  DEBUG_ENTRY( "state_get_put()" );
81 
82  if( (strcmp( chJob , "get" ) == 0) )
83  {
84  lgGet = true;
86  }
87  else if( (strcmp( chJob , "put" ) == 0) )
88  {
89  lgGet = false;
90  char chFilename[INPUT_LINE_LENGTH];
92  {
93  /* not last iteration and do not want to save state for all iters, so simply quit */
94  return;
95  }
96 
97  /* get base of file name */
98  strcpy( chFilename , state.chPutFilename );
99  /* append iteration number if ALL keyword appears */
100  if( state.lgPutAll )
101  {
102  char chIteration[INPUT_LINE_LENGTH];
103  sprintf( chIteration , "_%li", iteration );
104  strcat( chFilename , chIteration );
105  }
106  ioSTATE = open_data( chFilename, "wb", AS_LOCAL_ONLY );
107  }
108  else
109  TotalInsanity();
110 
111  if( state.lgState_print )
112  fprintf(ioQQQ," Print state quantities, start iso seq \n");
113 
114  /* actually do the read / write */
115  Transitions.state_do( ioSTATE, lgGet );
116  ExtraLymanLines.state_do( ioSTATE, lgGet );
117 
118  for( ipISO=ipH_LIKE; ipISO<NISO; ++ipISO )
119  {
120  /* loop over all iso-electronic sequences */
121  for( nelem=ipISO; nelem<LIMELM; ++nelem )
122  {
123  if( nelem < 2 || dense.lgElmtOn[nelem] )
124  {
125  /* arrays are dim'd iso.numLevels_max[ipH_LIKE][nelem]+1 */
126  for( ipHi=1; ipHi < iso.numLevels_max[ipISO][nelem]; ++ipHi )
127  {
128  if( state.lgState_print )
129  {
130  fprintf(ioQQQ," start ISO ipISO= %li, nelem= %li, ipHi %li \n",
131  ipISO ,
132  nelem ,
133  ipHi);
134  for( n=0; n< ipHi; ++n )
135  {
136  fprintf(ioQQQ," ISO %li %li %li %li %.4e %.4e \n",
137  ipISO , nelem , ipHi , n ,
138  Transitions[ipISO][nelem][ipHi][n].Emis->TauIn ,
139  Transitions[ipISO][nelem][ipHi][n].Emis->TauTot );
140  }
141  fprintf(ioQQQ," end ISO ipISO\n");
142  }
143  }
144 
145  if( state.lgState_print )
146  {
147  fprintf(ioQQQ," start Ext ipISO= %li, nelem= %li, got %li \n",
148  ipISO ,
149  nelem ,
150  iso.nLyman_malloc[ipISO] );
151  }
152  if( state.lgState_print )
153  {
154  for( n=2; n< iso.nLyman_malloc[ipISO]; ++n )
155  {
156  fprintf(ioQQQ," Ext %li %li %li %.4e %.4e \n",
157  ipISO , nelem , n ,
158  ExtraLymanLines[ipISO][nelem][n].Emis->TauIn ,
159  ExtraLymanLines[ipISO][nelem][n].Emis->TauTot );
160  }
161  fprintf(ioQQQ," end Ext ipISO\n");
162  }
163  }
164  }
165  }
166 
167  state_do( TauLines , (size_t)(nLevel1+1)*sizeof(transition ) );
168  if( state.lgState_print )
169  {
170  for( n=0; n< (nLevel1+1); ++n )
171  {
172  fprintf(ioQQQ," Taulines %li %.4e %.4e \n",
173  n ,
174  TauLines[n].Emis->TauIn ,
175  TauLines[n].Emis->TauTot );
176  }
177  }
178 
179  state_do( TauLine2 , (size_t)nWindLine *sizeof(transition ) );
180  if( state.lgState_print )
181  {
182  for( n=0; n< nWindLine; ++n )
183  {
184  fprintf(ioQQQ," TauLine2 %li %.4e %.4e \n",
185  n ,
186  TauLine2[n].Emis->TauIn ,
187  TauLine2[n].Emis->TauTot );
188  }
189  }
190 
191  state_do( UTALines , (size_t)nUTA *sizeof(transition ) );
192  if( state.lgState_print )
193  {
194  for( n=0; n< nUTA; ++n )
195  {
196  fprintf(ioQQQ," UTALines %li %.4e %.4e \n",
197  n ,
198  UTALines[n].Emis->TauIn ,
199  UTALines[n].Emis->TauTot );
200  }
201  }
202 
203  state_do( HFLines , (size_t)nHFLines *sizeof(transition ) );
204  if( state.lgState_print )
205  {
206  for( n=0; n< nHFLines; ++n )
207  {
208  fprintf(ioQQQ," HFLines %li %.4e %.4e \n",
209  n ,
210  HFLines[n].Emis->TauIn ,
211  HFLines[n].Emis->TauTot );
212  }
213  }
214 
215  state_do( C12O16Rotate, (size_t)nCORotate *sizeof(transition ) );
216  if( state.lgState_print )
217  {
218  for( n=0; n< nCORotate; ++n )
219  {
220  fprintf(ioQQQ," C12O16Rotate %li %.4e %.4e \n",
221  n ,
222  C12O16Rotate[n].Emis->TauIn ,
223  C12O16Rotate[n].Emis->TauTot );
224  }
225  }
226 
227  state_do( C13O16Rotate, (size_t)nCORotate *sizeof(transition ) );
228  if( state.lgState_print )
229  {
230  for( n=0; n< nCORotate; ++n )
231  {
232  fprintf(ioQQQ," C13O16Rotate %li %.4e %.4e \n",
233  n ,
234  C13O16Rotate[n].Emis->TauIn ,
235  C13O16Rotate[n].Emis->TauTot );
236  }
237  }
238 
239  for( ipHi=1; ipHi < FeII.nFeIILevel; ++ipHi )
240  {
241  state_do( Fe2LevN[ipHi] , (size_t)ipHi*sizeof(transition) );
242  if( state.lgState_print )
243  {
244  for( n=0; n< ipHi; ++n )
245  {
246  fprintf(ioQQQ," Fe2LevN %li %li %.4e %.4e \n",
247  ipHi , n ,
248  Fe2LevN[ipHi][n].Emis->TauIn ,
249  Fe2LevN[ipHi][n].Emis->TauTot );
250  }
251  }
252  }
253  for( i=0; i<2; ++i )
254  {
255  state_do( opac.TauAbsGeo[i] , (size_t)rfield.nupper*sizeof(realnum) );
256  if( state.lgState_print )
257  {
258  for( n=0; n< rfield.nupper; ++n )
259  {
260  fprintf(ioQQQ," TauAbsGeo %li %li %.4e \n",
261  i , n ,
262  opac.TauAbsGeo[i][n] );
263  }
264  }
265 
266  state_do( opac.TauScatGeo[i] , (size_t)rfield.nupper*sizeof(realnum) );
267  if( state.lgState_print )
268  {
269  for( n=0; n< rfield.nupper; ++n )
270  {
271  fprintf(ioQQQ," TauScatGeo %li %li %.4e \n",
272  i , n ,
273  opac.TauAbsGeo[i][n] );
274  }
275  }
276 
277  state_do( opac.TauTotalGeo[i], (size_t)rfield.nupper*sizeof(realnum) );
278  if( state.lgState_print )
279  {
280  for( n=0; n< rfield.nupper; ++n )
281  {
282  fprintf(ioQQQ," TauTotalGeo %li %li %.4e \n",
283  i , n ,
284  opac.TauAbsGeo[i][n] );
285  }
286  }
287 
288  }
289 
290  /* the large H2 molecule, only if on */
291  if( h2.lgH2ON )
292  H2Lines.state_do( ioSTATE, lgGet );
293 
294  /* the struc variables */
295  state_do( &struc.nzlim, sizeof(struc.nzlim ) );
297  state_do( &struc.nzone, sizeof(struc.nzone ) );
298 
299  state_do( struc.testr, (size_t)(struc.nzlim)*sizeof(realnum ) );
300  state_do( struc.volstr, (size_t)(struc.nzlim)*sizeof(realnum ) );
301  state_do( struc.drad_x_fillfac, (size_t)(struc.nzlim)*sizeof(realnum ) );
302  state_do( struc.drad, (size_t)(struc.nzlim)*sizeof(realnum ) );
303  state_do( struc.histr, (size_t)(struc.nzlim)*sizeof(realnum ) );
304  state_do( struc.hiistr, (size_t)(struc.nzlim)*sizeof(realnum ) );
305  state_do( struc.ednstr, (size_t)(struc.nzlim)*sizeof(realnum ) );
306  state_do( struc.o3str, (size_t)(struc.nzlim)*sizeof(realnum ) );
307  state_do( struc.pressure,(size_t)(struc.nzlim)*sizeof(realnum ) );
308  state_do( struc.GasPressure ,(size_t)(struc.nzlim)*sizeof(realnum ) );
310  state_do( struc.hden ,(size_t)(struc.nzlim)*sizeof(realnum ) );
311  state_do( struc.DenParticles ,(size_t)(struc.nzlim)*sizeof(realnum ) );
312  state_do( struc.DenMass,(size_t)(struc.nzlim)*sizeof(realnum ) );
313  state_do( struc.depth,(size_t)(struc.nzlim)*sizeof(realnum ) );
314  state_do( struc.xLyman_depth , (size_t)(struc.nzlim)*sizeof(realnum ) );
315 
316  state_do( struc.coolstr,(size_t)(struc.nzlim)*sizeof(double ) );
317  state_do( struc.heatstr , (size_t)(struc.nzlim)*sizeof(double ) );
318 
319  for( nelem=ipHYDROGEN; nelem<(LIMELM+3); ++nelem )
320  {
321  for( ion=0; ion<(LIMELM+1); ++ion )
322  {
323  state_do( struc.xIonDense[nelem][ion] , (size_t)(struc.nzlim)*sizeof(realnum ) );
324  }
325  }
326 
327  /* the hydrogen molecules */
328  /*realnum *Molec[N_H_MOLEC];*/
329  for( n=0; n<N_H_MOLEC; ++n )
330  {
331  state_do( struc.H2_molec[n] , (size_t)(struc.nzlim)*sizeof(realnum ) );
332  }
333  for( n=0; n<mole.num_comole_calc; ++n )
334  {
335  state_do( struc.CO_molec[n] , (size_t)(struc.nzlim)*sizeof(realnum ) );
336  }
337 
338  for( nelem=ipHYDROGEN; nelem<LIMELM; ++nelem )
339  {
340  state_do( struc.gas_phase[nelem] , (size_t)(struc.nzlim)*sizeof(realnum ) );
341  }
342 
343  /*fprintf(ioQQQ,"DEBUG done\n");*/
344 
345  /* close the file */
346  fclose( ioSTATE );
347  return;
348 }

Generated for cloudy by doxygen 1.8.3.1