cloudy  trunk
 All Data Structures Namespaces 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-2013 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 
20 static bool lgGet;
21 static FILE *ioSTATE;
22 
23 /*state_do - worker to actually get or put the structure -
24  * called by state_get_put below */
25 STATIC void state_do( void *pnt , size_t sizeof_pnt )
26 {
27  size_t n;
28  double sanity = 1.,
29  chk_sanity;
30  size_t sizeof_sanity =sizeof( sanity );
31 
32  DEBUG_ENTRY( "state_do()" );
33 
34  /* nothing to do if this is not positive */
35  if( sizeof_pnt == 0 )
36  return;
37 
38  if( lgGet )
39  {
40  /* get option - read in data */
41  if( (n=fread( pnt , 1 , sizeof_pnt , ioSTATE )) - sizeof_pnt )
42  {
43  fprintf( ioQQQ, " state_do failed reading state file, wanted %lu got %lu\n",
44  (unsigned long)sizeof_pnt ,
45  (unsigned long)n);
47  }
48  /* perform sanity check */
49  if( (n=fread( &chk_sanity , 1 , sizeof_sanity , ioSTATE )) - sizeof_sanity )
50  {
51  fprintf( ioQQQ, " state_do failed reading sanity par of state file, wanted %lu got %lu\n",
52  (unsigned long)sizeof_sanity ,
53  (unsigned long)n);
55  }
56  if( ! fp_equal( sanity, chk_sanity ) )
57  {
58  fprintf( ioQQQ, " state_do sanity fails in state file, wanted %g got %g\n",
59  sanity ,
60  chk_sanity);
62  }
63  }
64  else
65  {
66  /* put option - write out data */
67  fwrite( pnt , 1 , sizeof_pnt , ioSTATE );
68  /* write sanity check */
69  fwrite( &sanity , 1 , sizeof_sanity , ioSTATE );
70  }
71 
72  return;
73 }
74 
75 /*state_get_put get or save state - called by cloudy - job is either "get" or "put" */
76 void state_get_put( const char chJob[] )
77 {
78  long int ipISO , nelem , ipHi, i ,
79  n , ion;
80 
81  DEBUG_ENTRY( "state_get_put()" );
82 
83  if( (strcmp( chJob , "get" ) == 0) )
84  {
85  lgGet = true;
86  ioSTATE = open_data( state.chGetFilename, "rb", AS_LOCAL_ONLY );
87  }
88  else if( (strcmp( chJob , "put" ) == 0) )
89  {
90  lgGet = false;
91  char chFilename[INPUT_LINE_LENGTH];
92  if( !state.lgPutAll && iteration <= iterations.itermx )
93  {
94  /* not last iteration and do not want to save state for all iters, so simply quit */
95  return;
96  }
97 
98  /* get base of file name */
99  strcpy( chFilename , state.chPutFilename );
100  /* append iteration number if ALL keyword appears */
101  if( state.lgPutAll )
102  {
103  char chIteration[INPUT_LINE_LENGTH];
104  sprintf( chIteration , "_%li", iteration );
105  strcat( chFilename , chIteration );
106  }
107  ioSTATE = open_data( chFilename, "wb", AS_LOCAL_ONLY );
108  }
109  else
110  TotalInsanity();
111 
112  if( state.lgState_print )
113  fprintf(ioQQQ," Print state quantities, start iso seq \n");
114 
115  /* actually do the read / write */
116  fixit(); // Wouldn't actually work, as these classes contain pointers
117 #if 0
118  iso_sp.state_do( ioSTATE, lgGet );
119  ExtraLymanLines.state_do( ioSTATE, lgGet );
120 #endif
121 
122  for( ipISO=ipH_LIKE; ipISO<NISO; ++ipISO )
123  {
124  /* loop over all iso-electronic sequences */
125  for( nelem=ipISO; nelem<LIMELM; ++nelem )
126  {
127  if( nelem < 2 || dense.lgElmtOn[nelem] )
128  {
129  /* arrays are dim'd iso_sp[ipH_LIKE][nelem].numLevels_max+1 */
130  for( ipHi=1; ipHi < iso_sp[ipISO][nelem].numLevels_max; ++ipHi )
131  {
132  if( state.lgState_print )
133  {
134  fprintf(ioQQQ," start ISO ipISO= %li, nelem= %li, ipHi %li \n",
135  ipISO ,
136  nelem ,
137  ipHi);
138  for( n=0; n< ipHi; ++n )
139  {
140  fprintf(ioQQQ," ISO %li %li %li %li %.4e %.4e \n",
141  ipISO , nelem , ipHi , n ,
142  iso_sp[ipISO][nelem].trans(ipHi,n).Emis().TauIn() ,
143  iso_sp[ipISO][nelem].trans(ipHi,n).Emis().TauTot() );
144  }
145  fprintf(ioQQQ," end ISO ipISO\n");
146  }
147  }
148 
149  if( state.lgState_print )
150  {
151  fprintf(ioQQQ," start Ext ipISO= %li, nelem= %li, got %li \n",
152  ipISO ,
153  nelem ,
154  iso_ctrl.nLyman_malloc[ipISO] );
155  }
156  if( state.lgState_print )
157  {
158  for( n=2; n< iso_ctrl.nLyman_malloc[ipISO]; ++n )
159  {
160  TransitionList::iterator tr = ExtraLymanLines[ipISO][nelem].begin()+ipExtraLymanLines[ipISO][nelem][n];
161  fprintf(ioQQQ," Ext %li %li %li %.4e %.4e \n",
162  ipISO , nelem , n ,
163  (*tr).Emis().TauIn() ,
164  (*tr).Emis().TauTot() );
165  }
166  fprintf(ioQQQ," end Ext ipISO\n");
167  }
168  }
169  }
170  }
171 
172  fixit(); // Broken -- contains pointers...
173  //state_do( TauLines.begin(), (size_t)(nLevel1+1)*sizeof(transition) );
174  if( state.lgState_print )
175  {
176  for( n=0; n< (nLevel1+1); ++n )
177  {
178  fprintf(ioQQQ," Taulines %li %.4e %.4e \n",
179  n ,
180  TauLines[n].Emis().TauIn() ,
181  TauLines[n].Emis().TauTot() );
182  }
183  }
184 
185  //state_do( TauLine2.begin()+0, (size_t)nWindLine *sizeof(transition) );
186  if( state.lgState_print )
187  {
188  for( n=0; n< nWindLine; ++n )
189  {
190  fprintf(ioQQQ," TauLine2 %li %.4e %.4e \n",
191  n ,
192  TauLine2[n].Emis().TauIn() ,
193  TauLine2[n].Emis().TauTot() );
194  }
195  }
196 
197  // this implicitly assumes that a vector stores its data contiguously
198  //state_do( UTALines.begin()+0, (size_t)nUTA *sizeof(transition) );
199  if( state.lgState_print )
200  {
201  for( n=0; n< nUTA; ++n )
202  {
203  fprintf(ioQQQ," UTALines %li %.4e %.4e \n",
204  n ,
205  UTALines[n].Emis().TauIn() ,
206  UTALines[n].Emis().TauTot() );
207  }
208  }
209 
210  //state_do( HFLines.begin()+0, (size_t)nHFLines *sizeof(transition) );
211  if( state.lgState_print )
212  {
213  for( n=0; n< nHFLines; ++n )
214  {
215  fprintf(ioQQQ," HFLines %li %.4e %.4e \n",
216  n ,
217  HFLines[n].Emis().TauIn() ,
218  HFLines[n].Emis().TauTot() );
219  }
220  }
221 
222  fixit(); // Won't work as transition includes pointers...
223 #if 0
224  Fe2LevN.state_do( ioSTATE, lgGet );
225 #endif
226  if( state.lgState_print )
227  {
228  for( ipHi=1; ipHi < FeII.nFeIILevel_malloc; ++ipHi )
229  {
230  for( n=0; n< ipHi; ++n )
231 
232  {
234  fprintf(ioQQQ," Fe2LevN %li %li %.4e %.4e \n",
235  ipHi , n ,
236  (*tr).Emis().TauIn() ,
237  (*tr).Emis().TauTot() );
238  }
239  }
240  }
241  for( i=0; i<2; ++i )
242  {
243  state_do( opac.TauAbsGeo[i] , (size_t)rfield.nupper*sizeof(realnum) );
244  if( state.lgState_print )
245  {
246  for( n=0; n< rfield.nupper; ++n )
247  {
248  fprintf(ioQQQ," TauAbsGeo %li %li %.4e \n",
249  i , n ,
250  opac.TauAbsGeo[i][n] );
251  }
252  }
253 
254  state_do( opac.TauScatGeo[i] , (size_t)rfield.nupper*sizeof(realnum) );
255  if( state.lgState_print )
256  {
257  for( n=0; n< rfield.nupper; ++n )
258  {
259  fprintf(ioQQQ," TauScatGeo %li %li %.4e \n",
260  i , n ,
261  opac.TauAbsGeo[i][n] );
262  }
263  }
264 
265  state_do( opac.TauTotalGeo[i], (size_t)rfield.nupper*sizeof(realnum) );
266  if( state.lgState_print )
267  {
268  for( n=0; n< rfield.nupper; ++n )
269  {
270  fprintf(ioQQQ," TauTotalGeo %li %li %.4e \n",
271  i , n ,
272  opac.TauAbsGeo[i][n] );
273  }
274  }
275 
276  }
277 
278  /* the large H2 molecule, only if on */
279  if( h2.lgEnabled )
280  {
281  //Wouldn't have worked -- transition contained pointers...
282  fixit();
283  //state_do( &h2.sys.trans, h2.sys.trans.size() * sizeof(transition) );
284  //state_do( &h2.states, h2.states.size() * sizeof( quantumState ) );
285  }
286 
287  /* the struc variables */
288  state_do( &struc.nzlim, sizeof(struc.nzlim ) );
290 
291  state_do( struc.testr, (size_t)(struc.nzlim)*sizeof(realnum ) );
292  state_do( struc.volstr, (size_t)(struc.nzlim)*sizeof(realnum ) );
293  state_do( struc.drad_x_fillfac, (size_t)(struc.nzlim)*sizeof(realnum ) );
294  state_do( struc.drad, (size_t)(struc.nzlim)*sizeof(realnum ) );
295  state_do( struc.histr, (size_t)(struc.nzlim)*sizeof(realnum ) );
296  state_do( struc.hiistr, (size_t)(struc.nzlim)*sizeof(realnum ) );
297  state_do( struc.ednstr, (size_t)(struc.nzlim)*sizeof(realnum ) );
298  state_do( struc.o3str, (size_t)(struc.nzlim)*sizeof(realnum ) );
299  state_do( struc.pressure,(size_t)(struc.nzlim)*sizeof(realnum ) );
300  state_do( struc.GasPressure ,(size_t)(struc.nzlim)*sizeof(realnum ) );
302  state_do( struc.hden ,(size_t)(struc.nzlim)*sizeof(realnum ) );
303  state_do( struc.DenParticles ,(size_t)(struc.nzlim)*sizeof(realnum ) );
304  state_do( struc.DenMass,(size_t)(struc.nzlim)*sizeof(realnum ) );
305  state_do( struc.depth,(size_t)(struc.nzlim)*sizeof(realnum ) );
306  state_do( struc.xLyman_depth , (size_t)(struc.nzlim)*sizeof(realnum ) );
307 
308  state_do( struc.coolstr,(size_t)(struc.nzlim)*sizeof(double ) );
309  state_do( struc.heatstr , (size_t)(struc.nzlim)*sizeof(double ) );
310 
311  for( nelem=ipHYDROGEN; nelem<LIMELM; ++nelem )
312  {
313  for( ion=0; ion<(LIMELM+1); ++ion )
314  {
315  state_do( struc.xIonDense[nelem][ion] , (size_t)(struc.nzlim)*sizeof(realnum ) );
316  }
317  }
318  for( long ipISO=ipH_LIKE; ipISO<NISO; ++ipISO )
319  {
320  for( nelem=ipISO; nelem<LIMELM; ++nelem)
321  {
322  if( dense.lgElmtOn[nelem] )
323  {
324  for( long level=0; level < iso_sp[ipISO][nelem].numLevels_local; ++level )
325  {
326  state_do( struc.StatesElem[nelem][nelem-ipISO][level] , (size_t)(struc.nzlim)*sizeof(realnum ) );
327  }
328  }
329  }
330  }
331 
332  for( n=0; n<mole_global.num_calc; ++n )
333  {
334  state_do( struc.molecules[n] , (size_t)(struc.nzlim)*sizeof(realnum ) );
335  }
336  state_do( struc.H2_abund , (size_t)(struc.nzlim)*sizeof(realnum ) );
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 }
realnum * hden
Definition: struc.h:25
t_mole_global mole_global
Definition: mole.cpp:6
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
Definition: cpu.cpp:616
TransitionList UTALines("UTALines",&AnonStates)
realnum dr_ionfrac_limit
Definition: struc.h:84
NORETURN void TotalInsanity(void)
Definition: service.cpp:886
t_opac opac
Definition: opacity.cpp:5
int num_calc
Definition: mole.h:314
long int nLevel1
Definition: taulines.cpp:28
t_struc struc
Definition: struc.cpp:6
t_isoCTRL iso_ctrl
Definition: iso.cpp:6
const int NISO
Definition: cddefines.h:265
realnum * ednstr
Definition: struc.h:25
TransitionList HFLines("HFLines",&AnonStates)
realnum ** molecules
Definition: struc.h:71
static FILE * ioSTATE
Definition: state.cpp:21
t_dense dense
Definition: dense.cpp:24
iterator begin(void)
Definition: transition.h:305
realnum * volstr
Definition: struc.h:25
double * heatstr
Definition: struc.h:78
FILE * ioQQQ
Definition: cddefines.cpp:7
TransitionList TauLine2("TauLine2",&AnonStates)
double * coolstr
Definition: struc.h:78
char chPutFilename[INPUT_LINE_LENGTH]
Definition: state.h:15
Definition: state.h:11
t_iso_sp iso_sp[NISO][LIMELM]
Definition: iso.cpp:8
realnum * DenMass
Definition: struc.h:25
long int nupper
Definition: rfield.h:46
long int iteration
Definition: cddefines.cpp:16
bool fp_equal(sys_float x, sys_float y, int n=3)
Definition: cddefines.h:816
realnum * depth
Definition: struc.h:25
bool lgPutAll
Definition: state.h:24
bool lgState_print
Definition: state.h:27
long int nFeIILevel_malloc
Definition: atomfeii.h:193
#define STATIC
Definition: cddefines.h:101
realnum * GasPressure
Definition: struc.h:25
bool lgEnabled
Definition: h2_priv.h:342
realnum ** TauScatGeo
Definition: opacity.h:83
multi_arr< int, 3 > ipExtraLymanLines
Definition: taulines.cpp:24
realnum * drad
Definition: struc.h:25
t_rfield rfield
Definition: rfield.cpp:8
float realnum
Definition: cddefines.h:107
realnum **** StatesElem
Definition: struc.h:67
#define EXIT_FAILURE
Definition: cddefines.h:144
const int INPUT_LINE_LENGTH
Definition: cddefines.h:258
bool lgElmtOn[LIMELM]
Definition: dense.h:146
#define cdEXIT(FAIL)
Definition: cddefines.h:438
diatomics h2("h2", 4100.,&hmi.H2_total, Yan_H2_CS)
t_iterations iterations
Definition: iterations.cpp:5
long nWindLine
Definition: cdinit.cpp:19
t_state state
Definition: state.cpp:19
realnum * DenParticles
Definition: struc.h:25
realnum * H2_abund
Definition: struc.h:71
STATIC void state_do(void *pnt, size_t sizeof_pnt)
Definition: state.cpp:25
long int nzlim
Definition: struc.h:19
long int itermx
Definition: iterations.h:26
realnum ** TauTotalGeo
Definition: opacity.h:87
realnum * drad_x_fillfac
Definition: struc.h:25
TransitionList TauLines("TauLines",&AnonStates)
const int ipH_LIKE
Definition: iso.h:62
vector< vector< TransitionList > > ExtraLymanLines
Definition: taulines.cpp:25
const int LIMELM
Definition: cddefines.h:262
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:688
realnum ** gas_phase
Definition: struc.h:75
t_FeII FeII
Definition: atomfeii.cpp:5
realnum * histr
Definition: struc.h:25
realnum ** TauAbsGeo
Definition: opacity.h:82
realnum * xLyman_depth
Definition: struc.h:25
void state_get_put(const char chJob[])
Definition: state.cpp:76
long int nHFLines
Definition: taulines.cpp:31
multi_arr< int, 2 > ipFe2LevN
Definition: taulines.cpp:34
long int numLevels_max
Definition: iso.h:493
realnum * testr
Definition: struc.h:25
long int nUTA
Definition: taulines.cpp:26
char chGetFilename[INPUT_LINE_LENGTH]
Definition: state.h:15
const int ipHYDROGEN
Definition: cddefines.h:309
void fixit(void)
Definition: service.cpp:991
realnum * hiistr
Definition: struc.h:25
realnum *** xIonDense
Definition: struc.h:64
static bool lgGet
Definition: state.cpp:20
long int numLevels_local
Definition: iso.h:498
realnum * o3str
Definition: struc.h:25
long int nLyman_malloc[NISO]
Definition: iso.h:334
realnum * pressure
Definition: struc.h:25
TransitionList Fe2LevN("Fe2LevN",&Fe2LevNStates)
realnum * pres_radiation_lines_curr
Definition: struc.h:25