cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
generic_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 
4 #include "cddefines.h"
5 
6 #include "generic_state.h"
7 
8 #include "dynamics.h"
9 #include "mole.h"
10 #include "phycon.h"
11 #include "radius.h"
12 #include "rfield.h"
13 #include "species.h"
14 
15 double column(const genericState&gs)
16 {
17  if ( gs.sp != NULL )
18  return gs.sp->column;
19  else if (gs.q.associated())
20  return gs.q.ColDen();
21  else if (gs.val != NULL)
22  return *gs.val;
23  else
24  return 0.;
25 }
26 
27 double density(const genericState& gs)
28 {
29  if ( gs.sp != NULL )
30  return gs.sp->den;
31  else if (gs.q.associated())
32  return gs.q.Pop();
33  else if (gs.val != NULL)
34  return *gs.val;
35  else
36  return 0.;
37 }
38 double depart(const genericState& gs)
39 {
40  if ( gs.sp != NULL )
41  return 1.0;
42  else if (gs.q.associated())
43  return gs.q.DepartCoef();
44  else if (gs.val != NULL)
45  return *gs.val;
46  else
47  return 1.0;
48 }
49 double energy(const genericState& gs)
50 {
51  if ( gs.sp != NULL )
52  return 0.0;
53  else if (gs.q.associated())
54  return AnuUnit( gs.q.energy().Ryd() );
55  else if (gs.val != NULL)
56  return *gs.val;
57  else
58  return 0.0;
59 }
60 double levels(const genericState& gs)
61 {
62  if ( gs.sp != NULL && gs.sp->levels != NULL )
63  return (double) gs.sp->levels->size();
64  else if (gs.val != NULL)
65  return *gs.val;
66  else
67  return 0.0;
68 }
69 
70 string genericState::label() const
71 {
72  if ( sp != NULL && sp != null_molezone )
73  return sp->global()->label;
74  else if (q.associated())
75  return q.chLabel();
76  else if (val != NULL)
77  return valLabel;
78  else
79  return "";
80 }
81 
82 string genericState::database() const
83 {
84  if ( sp != NULL && sp != null_molezone && sp->dbase != NULL )
85  return sp->dbase->database;
86  else
87  return "";
88 }
89 
91 {
92  if (sp != NULL && sp != null_molezone)
93  return true;
94  else if (q.associated())
95  return true;
96  else if (val != NULL)
97  return true;
98  else
99  return false;
100 }
101 
102 static const long IGNORE_LEVEL = -1;
103 const molezone *getLevelsGeneric( const char *chLabel, bool lgValidate, vector<long> &LevelList )
104 {
105  DEBUG_ENTRY( "getLevels()" );
106 
107  char chLabel_species[NCHLAB] = { 0 };
108  strcpy( chLabel_species, chLabel );
109 
110  long nLevelLo = -1,
111  nLevelHi = -1;
112  bool lgLevels = false;
113 
114  for(char* s=chLabel_species; *s != '\0'; ++s)
115  {
116  if (*s == '[')
117  {
118  lgLevels = true;
119  *s = '\0';
120  char *t;
121  ++s;
122  if (*s != ':')
123  {
124  nLevelLo = strtol(s,&t,10);
125  if (nLevelLo <= 0)
126  {
127  fprintf(ioQQQ,"PROBLEM: In request for species \"%s\"\n",chLabel);
128  fprintf(ioQQQ,"Excitation levels must be at >= 1\n");
130  }
131  s = t;
132  }
133  else
134  {
135  nLevelLo = 1;
136  }
137  if (*s != ':')
138  {
139  nLevelHi = nLevelLo;
140  }
141  else
142  {
143  ++s;
144  if (*s == ']')
145  {
146  nLevelHi = 0; // Tag for all levels
147  }
148  else
149  {
150  nLevelHi = strtol(s,&t,10);
151  s = t;
152  if (nLevelHi <= 0)
153  {
154  fprintf(ioQQQ,"PROBLEM: In request for species \"%s\"\n",chLabel);
155  fprintf(ioQQQ,"Excitation levels must be at >= 1\n");
157  }
158  }
159  }
160  if (*s != ']')
161  {
162  fprintf(ioQQQ,"PROBLEM: In request for species \"%s\"\n",chLabel);
163  fprintf(ioQQQ,"Excitation levels must be in balanced brackets\n");
165  }
166  break;
167  }
168  }
169 
170  const molezone *sp = findspecieslocal(chLabel_species);
171 
172  if ( sp == null_molezone)
173  {
174  if (lgValidate)
175  {
176  fprintf(ioQQQ,"PROBLEM: Request for unidentified species \"%s\"\n",chLabel_species);
178  }
179  }
180  else if ( lgLevels )
181  {
182  if ( ! sp->levels )
183  {
184  if (lgValidate)
185  {
186  fprintf(ioQQQ,"PROBLEM: Request for level in unresolved species \"%s\"\n",chLabel_species);
188  }
189  // If no levels, map all population into ground state
190  if (nLevelHi == 0)
191  nLevelHi = nLevelLo;
192  for (int nLevel = nLevelLo; nLevel<=nLevelHi; ++nLevel)
193  {
194  if ( nLevel == 1 )
195  LevelList.push_back( nLevel );
196  else
197  LevelList.push_back( IGNORE_LEVEL );
198  }
199  }
200  else if ( size_t(nLevelLo) > sp->levels->size() || size_t(nLevelHi) > sp->levels->size() )
201  {
202  if (lgValidate)
203  {
204  fprintf(ioQQQ,"PROBLEM: Request for level \"%s\", but species only has %lu levels\n",
205  chLabel,(unsigned long)sp->levels->size());
207  }
208  }
209  else
210  {
211  // Offset by one to convert from numeric index to C array index
212  if (nLevelHi == 0)
213  nLevelHi = sp->levels->size();
214  for (int nLevel = nLevelLo; nLevel<=nLevelHi; ++nLevel)
215  {
216  LevelList.push_back( nLevel-1 );
217  }
218  }
219  }
220 
221  return sp;
222 }
223 
224 vector<genericState> matchGeneric(const char *chLabel, bool lgValidate)
225 {
226  DEBUG_ENTRY("matchGeneric()");
227  vector<genericState> v;
228 
229  if (chLabel[0] == '*')
230  {
231  if (strncmp(chLabel+1,"depth",6) == 0)
232  {
233  v.push_back(genericState("*depth",&radius.depth_mid_zone));
234  return v;
235  }
236  else if (strncmp(chLabel+1,"AV",3) == 0)
237  {
238  v.push_back(genericState("*AV",&rfield.extin_mag_V_point));
239  return v;
240  }
241  else if (strncmp(chLabel+1,"AVx",4) == 0)
242  {
243  v.push_back(genericState("*AVx",&rfield.extin_mag_V_extended));
244  return v;
245  }
246  else if (strncmp(chLabel+1,"time",5) == 0)
247  {
248  v.push_back(genericState("*time",&dynamics.time_elapsed));
249  return v;
250  }
251  else if (strncmp(chLabel+1,"temp",5) == 0)
252  {
253  v.push_back(genericState("*temp",&phycon.te));
254  return v;
255  }
256  else if (strncmp(chLabel+1,"all",4) == 0)
257  {
258  for( size_t i=0; i<mole_global.list.size(); ++i )
259  {
260  v.push_back( genericState(&(mole.species[i])) );
261  }
262  return v;
263  }
264  }
265 
266  vector<long> LevelList;
267  const molezone *sp = getLevelsGeneric( chLabel, lgValidate, LevelList );
268 
269  if( sp != null_molezone && LevelList.size() > 0 )
270  {
271  for( vector<long>::iterator nLevel = LevelList.begin();
272  nLevel != LevelList.end(); ++nLevel)
273  {
274  if( sp->levels )
275  v.push_back(genericState((*sp->levels)[ *nLevel ]));
276  else if( *nLevel != IGNORE_LEVEL )
277  v.push_back(genericState(sp));
278  else
279  v.push_back(genericState());
280  }
281  }
282  else if( sp != null_molezone )
283  {
284  v.push_back(genericState(sp));
285  }
286  return v;
287 }
t_mole_global mole_global
Definition: mole.cpp:7
double depart(const genericState &gs)
const molecule * global() const
Definition: mole.h:404
t_phycon phycon
Definition: phycon.cpp:6
vector< genericState > matchGeneric(const char *chLabel, bool lgValidate)
double DepartCoef() const
Definition: quantumstate.h:311
realnum column
Definition: mole.h:422
FILE * ioQQQ
Definition: cddefines.cpp:7
molezone * findspecieslocal(const char buf[])
Definition: mole.h:378
t_dynamics dynamics
Definition: dynamics.cpp:42
bool associated() const
const double * val
Definition: generic_state.h:21
string database
Definition: species.h:40
qList * levels
Definition: mole.h:417
double energy(const genericState &gs)
t_mole_local mole
Definition: mole.cpp:8
t_rfield rfield
Definition: rfield.cpp:9
valarray< class molezone > species
Definition: mole.h:468
#define EXIT_FAILURE
Definition: cddefines.h:168
size_t size() const
Definition: quantumstate.h:121
string valLabel
Definition: generic_state.h:22
#define cdEXIT(FAIL)
Definition: cddefines.h:482
const molezone * getLevelsGeneric(const char *chLabel, bool lgValidate, vector< long > &LevelList)
string database() const
species * dbase
Definition: mole.h:416
double depth_mid_zone
Definition: radius.h:31
double column(const genericState &gs)
const molezone * sp
Definition: generic_state.h:19
t_radius radius
Definition: radius.cpp:5
double AnuUnit(realnum energy)
Definition: service.cpp:197
double extin_mag_V_point
Definition: rfield.h:258
double levels(const genericState &gs)
double density(const genericState &gs)
double extin_mag_V_extended
Definition: rfield.h:262
string chLabel() const
double Ryd() const
Definition: energy.h:33
double den
Definition: mole.h:421
bool associated() const
Definition: quantumstate.h:286
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:723
double Pop() const
Definition: quantumstate.h:307
Energy energy() const
Definition: quantumstate.h:295
string label
Definition: mole.h:156
const int NCHLAB
Definition: cddefines.h:304
int fprintf(const Output &stream, const char *format,...)
Definition: service.cpp:1121
MoleculeList list
Definition: mole.h:365
double time_elapsed
Definition: dynamics.h:105
string label() const
double te
Definition: phycon.h:21
qStateConstProxy q
Definition: generic_state.h:20
molezone * null_molezone
double ColDen() const
Definition: quantumstate.h:303
static const long IGNORE_LEVEL