cloudy  trunk
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;
87  }
88  else if( (strcmp( chJob , "put" ) == 0) )
89  {
90  lgGet = false;
91  char chFilename[INPUT_LINE_LENGTH];
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 }
t_struc::coolstr
double * coolstr
Definition: struc.h:78
t_struc::gas_phase
realnum ** gas_phase
Definition: struc.h:75
open_data
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
Definition: cpu.cpp:625
h2
diatomics h2("h2", 4100., &hmi.H2_total, Yan_H2_CS)
t_struc::xIonDense
realnum *** xIonDense
Definition: struc.h:64
t_opac::TauScatGeo
realnum ** TauScatGeo
Definition: opacity.h:83
struc.h
dense
t_dense dense
Definition: dense.cpp:24
lgGet
static bool lgGet
Definition: state.cpp:20
rfield
t_rfield rfield
Definition: rfield.cpp:8
ioQQQ
FILE * ioQQQ
Definition: cddefines.cpp:7
nLevel1
long int nLevel1
Definition: taulines.cpp:28
diatomics::lgEnabled
bool lgEnabled
Definition: h2_priv.h:345
realnum
float realnum
Definition: cddefines.h:103
iterations
t_iterations iterations
Definition: iterations.cpp:5
rfield.h
nWindLine
long nWindLine
Definition: cdinit.cpp:19
STATIC
#define STATIC
Definition: cddefines.h:97
ipExtraLymanLines
multi_arr< int, 3 > ipExtraLymanLines
Definition: taulines.cpp:24
nUTA
long int nUTA
Definition: taulines.cpp:26
t_opac::TauTotalGeo
realnum ** TauTotalGeo
Definition: opacity.h:87
mole.h
t_struc::testr
realnum * testr
Definition: struc.h:25
AS_LOCAL_ONLY
@ AS_LOCAL_ONLY
Definition: cpu.h:208
t_dense::lgElmtOn
bool lgElmtOn[LIMELM]
Definition: dense.h:146
t_struc::drad
realnum * drad
Definition: struc.h:53
t_struc::dr_ionfrac_limit
realnum dr_ionfrac_limit
Definition: struc.h:84
t_iso_sp::numLevels_local
long int numLevels_local
Definition: iso.h:498
t_struc::StatesElem
realnum **** StatesElem
Definition: struc.h:67
TransitionList::begin
iterator begin(void)
Definition: transition.h:305
ProxyIterator
Definition: proxy_iterator.h:58
t_struc::drad_x_fillfac
realnum * drad_x_fillfac
Definition: struc.h:27
t_struc::hiistr
realnum * hiistr
Definition: struc.h:29
t_isoCTRL::nLyman_malloc
long int nLyman_malloc[NISO]
Definition: iso.h:336
t_opac::TauAbsGeo
realnum ** TauAbsGeo
Definition: opacity.h:82
struc
t_struc struc
Definition: struc.cpp:6
iso.h
ioSTATE
static FILE * ioSTATE
Definition: state.cpp:21
ipHYDROGEN
const int ipHYDROGEN
Definition: cddefines.h:305
t_struc::H2_abund
realnum * H2_abund
Definition: struc.h:72
opac
t_opac opac
Definition: opacity.cpp:5
t_struc::depth
realnum * depth
Definition: struc.h:51
t_struc::nzlim
long int nzlim
Definition: struc.h:19
t_FeII::nFeIILevel_malloc
long int nFeIILevel_malloc
Definition: atomfeii.h:193
EXIT_FAILURE
#define EXIT_FAILURE
Definition: cddefines.h:140
t_state
Definition: state.h:11
dense.h
t_iterations::itermx
long int itermx
Definition: iterations.h:26
Fe2LevN
TransitionList Fe2LevN("Fe2LevN", &Fe2LevNStates)
cddefines.h
t_struc::DenMass
realnum * DenMass
Definition: struc.h:49
t_iso_sp::numLevels_max
long int numLevels_max
Definition: iso.h:493
ExtraLymanLines
vector< vector< TransitionList > > ExtraLymanLines
Definition: taulines.cpp:25
TotalInsanity
NORETURN void TotalInsanity(void)
Definition: service.cpp:886
t_struc::volstr
realnum * volstr
Definition: struc.h:26
TauLine2
TransitionList TauLine2("TauLine2", &AnonStates)
t_struc::histr
realnum * histr
Definition: struc.h:28
t_struc::xLyman_depth
realnum * xLyman_depth
Definition: struc.h:55
t_struc::pres_radiation_lines_curr
realnum * pres_radiation_lines_curr
Definition: struc.h:43
t_struc::hden
realnum * hden
Definition: struc.h:45
FeII
t_FeII FeII
Definition: atomfeii.cpp:5
ipFe2LevN
multi_arr< int, 2 > ipFe2LevN
Definition: taulines.cpp:34
state_do
STATIC void state_do(void *pnt, size_t sizeof_pnt)
Definition: state.cpp:25
t_state::chGetFilename
char chGetFilename[INPUT_LINE_LENGTH]
Definition: state.h:16
LIMELM
const int LIMELM
Definition: cddefines.h:258
cdEXIT
#define cdEXIT(FAIL)
Definition: cddefines.h:434
UTALines
TransitionList UTALines("UTALines", &AnonStates)
state_get_put
void state_get_put(const char chJob[])
Definition: state.cpp:76
t_rfield::nupper
long int nupper
Definition: rfield.h:46
iteration
long int iteration
Definition: cddefines.cpp:16
TauLines
TransitionList TauLines("TauLines", &AnonStates)
t_struc::pressure
realnum * pressure
Definition: struc.h:33
state
t_state state
Definition: state.cpp:19
INPUT_LINE_LENGTH
const int INPUT_LINE_LENGTH
Definition: cddefines.h:254
t_struc::heatstr
double * heatstr
Definition: struc.h:79
fp_equal
bool fp_equal(sys_float x, sys_float y, int n=3)
Definition: cddefines.h:812
t_struc::DenParticles
realnum * DenParticles
Definition: struc.h:47
t_struc::ednstr
realnum * ednstr
Definition: struc.h:30
t_state::lgState_print
bool lgState_print
Definition: state.h:27
iso_ctrl
t_isoCTRL iso_ctrl
Definition: iso.cpp:6
fixit
void fixit(void)
Definition: service.cpp:991
t_struc::GasPressure
realnum * GasPressure
Definition: struc.h:41
taulines.h
nHFLines
long int nHFLines
Definition: taulines.cpp:31
t_struc::o3str
realnum * o3str
Definition: struc.h:31
t_state::chPutFilename
char chPutFilename[INPUT_LINE_LENGTH]
Definition: state.h:15
iterations.h
state.h
opacity.h
iso_sp
t_iso_sp iso_sp[NISO][LIMELM]
Definition: iso.cpp:8
h2.h
t_struc::molecules
realnum ** molecules
Definition: struc.h:71
NISO
const int NISO
Definition: cddefines.h:261
t_state::lgPutAll
bool lgPutAll
Definition: state.h:24
mole_global
t_mole_global mole_global
Definition: mole.cpp:6
atomfeii.h
t_mole_global::num_calc
int num_calc
Definition: mole.h:314
DEBUG_ENTRY
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:684
ipH_LIKE
const int ipH_LIKE
Definition: iso.h:62
HFLines
TransitionList HFLines("HFLines", &AnonStates)