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