cloudy  trunk
mole.h
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 
4 #ifndef MOLE_H_
5 #define MOLE_H_
6 
7 /* mole.h */
8 
9 #include "count_ptr.h"
10 #include "elementnames.h"
11 #include "transition.h"
12 
13 #define SMALLABUND 1e-24
14 
16 
17 class chem_atom;
18 
19 class chem_element {
20  explicit chem_element(); // Do not implement
21  chem_element &operator=(const chem_element&); // Do not implement
22 public:
23  explicit chem_element(int Z, const char*label) : Z(Z), label(label)
24  {}
25  ~chem_element() throw()
26  {}
27  const int Z;
28  const string label;
29  map<int, count_ptr<chem_atom> > isotopes;
30  //(first -> Atomic A; second -> chem_atom )
31  //(first -> -1 for bogus isotope, i.e. where no
32  // isotopes have been explicitly defined)
33 };
34 
35 typedef map<int, count_ptr<chem_atom> >::iterator isotopes_i;
36 
37 class chem_atom {
38 public:
39  // Link back to basic element for convenience -- not a count_ptr
40  // as this would lead to a reference cycle (and hence the
41  // destructors never being called). Many-to-one relation suggests
42  // that the weak link should be this way around.
44  int A; /* mass number */
45  vector<int> ipMl; /* Atom and ion species in molecule arrays */
46  realnum mass_amu; /* mass of isotope in AMU */
47  double frac; /* fraction of element in this isotope */
48 
49  bool lgMeanAbundance( void ) const
50  {
51  return ( A < 0 );
52  }
53 
54  /* Chemical symbols for elements */
55  string label( void ) const
56  {
57  if( lgMeanAbundance() )
58  return el->label;
59  else if( el->Z==1 && A==2 )
60  {
61  // Deuterium is a special case
62  return "D\0";
63  }
64  else
65  {
66  char str[4];
67  sprintf(str,"^%d",A);
68  return ( str + el->label );
69  }
70  }
71 
72  int compare ( const chem_atom &b ) const
73  {
74  // sort by proton number first
75  if ( el->Z < b.el->Z )
76  return -1;
77  else if ( el->Z > b.el->Z )
78  return 1;
79 
80  if (mass_amu < b.mass_amu)
81  return -1;
82  else if (mass_amu > b.mass_amu)
83  return 1;
84  else if (A < b.A )
85  return -1;
86  else
87  return 0;
88  }
89 };
90 inline bool operator< (const chem_atom &a, const chem_atom &b)
91 {
92  return a.compare(b) < 0;
93 }
94 inline bool operator> (const chem_atom &a, const chem_atom &b)
95 {
96  return a.compare(b) > 0;
97 }
98 inline bool operator<= (const chem_atom &a, const chem_atom &b)
99 {
100  return a.compare(b) <= 0;
101 }
102 inline bool operator>= (const chem_atom &a, const chem_atom &b)
103 {
104  return a.compare(b) >= 0;
105 }
106 inline bool operator== (const chem_atom &a, const chem_atom &b)
107 {
108  return a.compare(b) == 0;
109 }
110 inline bool operator!= (const chem_atom &a, const chem_atom &b)
111 {
112  return !(a == b);
113 }
114 
115 typedef vector< count_ptr<chem_atom> > ChemAtomList;
116 extern ChemAtomList atom_list;
118 extern chem_element *null_element;
119 extern chem_atom *null_atom;
120 
122 {
123 public:
125  const count_ptr<chem_atom>& b) const
126  {
127  return *a < *b;
128  }
129 };
130 
131 /* Structure containing molecule data, initially only CO */
132 class molecule {
133 public:
134  typedef map<const count_ptr<chem_atom>, int,
136 
137  string parentLabel;
139  bool isEnabled; /* Is it enabled? */
140 
141  /* Species physical data */
142  string label;
144  int charge;
145  bool lgExcit;
146  bool lgGas_Phase;
147  int n_nuclei(void) const
148  {
149  int num = 0;
150  for (nAtomsMap::const_iterator el = nAtom.begin();
151  el != nAtom.end(); ++el)
152  {
153  num += el->second;
154  }
155  return num;
156  }
157  bool isMonatomic(void) const
158  {
159  if (nAtom.size() == 1 && nAtom.begin()->second == 1)
160  return true;
161  return false;
162  }
163 
167  /* Parameters as computational object */
170 
171  chem_atom *heavyAtom(void) //const
172  {
173  for( nAtomsMap::reverse_iterator it=nAtom.rbegin(); it!=nAtom.rend(); ++it )
174  {
175  if (0 != it->second )
176  {
177  return it->first.get_ptr();
178  }
179  }
180  return null_atom;
181  }
182 
183  int compare(const molecule &mol2) const
184  {
185  nAtomsMap::const_reverse_iterator it1, it2;
186 
187  for( it1 = nAtom.rbegin(), it2 = mol2.nAtom.rbegin();
188  it1 != nAtom.rend() && it2 != mol2.nAtom.rend(); ++it1, ++it2 )
189  {
190  if( *(it1->first) > *(it2->first) )
191  return 1;
192  else if( *(it1->first) < *(it2->first) )
193  return -1;
194  else if( it1->second > it2->second)
195  return 1;
196  else if( it1->second < it2->second)
197  return -1;
198  }
199 
200  if( it1 != nAtom.rend() && it2 == mol2.nAtom.rend() )
201  return 1;
202  else if( it1 == nAtom.rend() && it2 != mol2.nAtom.rend() )
203  return -1;
204  else
205  ASSERT( it1 == nAtom.rend() && it2 == mol2.nAtom.rend() );
206 
207  // sort by label if falls through to here
208  return ( label.compare(mol2.label) );
209 
210  }
211 };
212 
213 /* iterators on nAtom */
214 typedef molecule::nAtomsMap::iterator nAtoms_i;
215 typedef molecule::nAtomsMap::reverse_iterator nAtoms_ri;
216 typedef molecule::nAtomsMap::const_reverse_iterator nAtoms_cri;
217 
219 extern void mole_drive(void);
220 
222 extern void mole_create_react(void);
223 
224 class mole_reaction;
225 
226 mole_reaction *mole_findrate_s(const char buf[]);
227 
228 extern void mole_print_species_reactions( molecule *speciesToPrint );
229 
230 extern molecule *null_mole;
231 
232 extern molecule *findspecies(const char buf[]);
233 
267 
268 public:
269  void init(void);
270 
271  void make_species(void);
272 
274  void zero(void);
275 
277  bool lgNoMole;
278 
281 
283  bool lgH2Ozer;
284 
287 
289  bool lgStancil;
290 
295 
300 
305 
309 
310  // flag saying whether to model isotopes (and isotopologues) of a given element
311  vector<bool> lgTreatIsotopes;
312 
315 
316  typedef vector<count_ptr<molecule> > MoleculeList;
318 
319  static void sort(MoleculeList::iterator start,
320  MoleculeList::iterator end);
321  static void sort(molecule **start, molecule **end);
322 };
323 
325 
326 class molezone {
327 public:
329  {
330  init();
331  }
332  void init (void)
333  {
334  location = NULL;
335  levels = NULL;
336  lines = NULL;
337  zero();
338  }
339  void zero (void)
340  {
341  src = 0.;
342  snk = 0.;
343  den = 0.;
344  column = 0.;
345  nAtomLim = -1;
346  xFracLim = 0.;
347  column_old = 0.;
348  }
349  double *location;
352  double src, snk;
353 
356 
357  /* Current zone data */
358  double den;
360  int nAtomLim;
363  /* Historical solution data */
365 };
366 
367 extern molezone *null_molezone;
368 
370 {
371 public:
372  void set_location( long nelem, long ion, double *dense );
373  void set_isotope_abundances( void );
374  double sink_rate_tot(const char chSpecies[]) const;
375  double sink_rate_tot(const molecule* const sp) const;
376  double sink_rate(const molecule* const sp, const mole_reaction& rate) const;
377  double sink_rate(const molecule* const sp, const char buf[]) const;
378  double source_rate_tot(const char chSpecies[]) const;
379  double source_rate_tot(const molecule* const sp) const;
382  double dissoc_rate(const char chSpecies[]) const;
383  double chem_heat(void) const;
384  double findrk(const char buf[]) const;
385  double findrate(const char buf[]) const;
386 
388 
390  double elec;
391 
394  double **source , **sink;
395 
396  realnum ***xMoleChTrRate;/***[LIMELM][LIMELM+1][LIMELM+1];*/
397 
398  valarray<class molezone> species;
399 
400  vector<double> reaction_rks;
401  vector<double> old_reaction_rks;
402  long old_zone;
403 };
404 
405 extern t_mole_local mole;
406 
407 extern molezone *findspecieslocal(const char buf[]);
408 
409 extern void mole_punch(FILE *punit, const char speciesname[], const char args[], bool lgHeader, bool lgData, double depth);
410 
411 extern void total_molecule_elems(realnum total[LIMELM]);
412 extern void total_molecule_deut(realnum &total);
413 
414 extern realnum total_molecules(void);
415 
416 extern realnum total_molecules_gasphase(void);
417 
418 extern void mole_make_list(void);
419 extern void mole_make_groups(void);
420 
422 
423 bool lgDifferByExcitation( const molecule &mol1, const molecule &mol2 );
424 
425 extern void mole_update_species_cache(void);
426 
427 void mole_update_sources(void);
428 
429 void mole_rk_bigchange(void);
430 
433  vector< int >& numAtoms,
434  string atom_old,
435  string atom_new,
436  string embellishments,
437  vector<string>& newLabels );
438 
439 bool parse_species_label( const char label[], ChemAtomList &atomsLeftToRight, vector<int> &numAtoms, string &embellishments );
440 bool parse_species_label( const char mylab[], ChemAtomList &atomsLeftToRight, vector<int> &numAtoms, string &embellishments,
441  bool &lgExcit, int &charge, bool &lgGas_Phase );
442 
443 #endif /* MOLE_H_ */
444 
lgDifferByExcitation
bool lgDifferByExcitation(const molecule &mol1, const molecule &mol2)
Definition: mole_species.cpp:804
t_mole_global::lgNonEquilChem
bool lgNonEquilChem
Definition: mole.h:294
t_mole_global::sort
static void sort(MoleculeList::iterator start, MoleculeList::iterator end)
molecule::lgGas_Phase
bool lgGas_Phase
Definition: mole.h:146
findspecieslocal
molezone * findspecieslocal(const char buf[])
Definition: mole_species.cpp:833
chem_element
Definition: mole.h:19
count_ptr
Definition: count_ptr.h:11
dense
t_dense dense
Definition: dense.cpp:24
elementnames.h
t_mole_local::source_rate_tot
double source_rate_tot(const char chSpecies[]) const
Definition: mole_reactions.cpp:4076
molecule::isMonatomic
bool isMonatomic(void) const
Definition: mole.h:157
t_mole_local
Definition: mole.h:369
realnum
float realnum
Definition: cddefines.h:103
molecule::parentIndex
int parentIndex
Definition: mole.h:138
t_mole_global::lgStancil
bool lgStancil
Definition: mole.h:289
nAtoms_i
molecule::nAtomsMap::iterator nAtoms_i
Definition: mole.h:214
unresolved_atom_list
ChemAtomList unresolved_atom_list
Definition: mole_species.cpp:70
t_mole_local::elec
double elec
Definition: mole.h:390
molecule::index
int index
Definition: mole.h:169
mole_reaction
Definition: mole_priv.h:49
t_mole_global::zero
void zero(void)
Definition: mole.cpp:36
t_mole_local::grain_area
double grain_area
Definition: mole.h:387
operator>
bool operator>(const chem_atom &a, const chem_atom &b)
Definition: mole.h:94
ChemAtomList
vector< count_ptr< chem_atom > > ChemAtomList
Definition: mole.h:115
mole_make_groups
void mole_make_groups(void)
Definition: mole_species.cpp:1142
mole_findrate_s
mole_reaction * mole_findrate_s(const char buf[])
Definition: mole_reactions.cpp:3872
MOLE_ACTIVE
@ MOLE_ACTIVE
Definition: mole.h:15
t_mole_global::list
MoleculeList list
Definition: mole.h:317
t_mole_global::init
void init(void)
Definition: mole.cpp:11
operator!=
bool operator!=(const chem_atom &a, const chem_atom &b)
Definition: mole.h:110
molecule::state
enum mole_state state
Definition: mole.h:168
molecule::charge
int charge
Definition: mole.h:144
molezone::src
double src
Definition: mole.h:352
mole_print_species_reactions
void mole_print_species_reactions(molecule *speciesToPrint)
Definition: newton_step.cpp:316
molezone::nAtomLim
int nAtomLim
Definition: mole.h:360
ASSERT
#define ASSERT(exp)
Definition: cddefines.h:578
MOLE_PASSIVE
@ MOLE_PASSIVE
Definition: mole.h:15
t_mole_local::sink_rate
double sink_rate(const molecule *const sp, const mole_reaction &rate) const
Definition: mole_reactions.cpp:3952
molecule::mole_mass
realnum mole_mass
Definition: mole.h:165
chem_atom::el
chem_element * el
Definition: mole.h:43
t_mole_local::grain_density
double grain_density
Definition: mole.h:387
molecule::nAtomsMap
map< const count_ptr< chem_atom >, int, element_pointer_value_less > nAtomsMap
Definition: mole.h:135
total_molecule_elems
void total_molecule_elems(realnum total[LIMELM])
Definition: mole_species.cpp:1063
mole_global
t_mole_global mole_global
Definition: mole.cpp:6
null_molezone
molezone * null_molezone
Definition: mole_species.cpp:66
mole_create_react
void mole_create_react(void)
Definition: mole_reactions.cpp:1665
t_mole_local::findrate
double findrate(const char buf[]) const
Definition: mole_reactions.cpp:3900
t_mole_local::sink
double ** sink
Definition: mole.h:394
t_mole_local::grain_saturation
double grain_saturation
Definition: mole.h:387
t_mole_local::set_location
void set_location(long nelem, long ion, double *dense)
Definition: mole_species.cpp:1018
t_mole_local::old_zone
long old_zone
Definition: mole.h:402
chem_element::label
const string label
Definition: mole.h:28
operator==
bool operator==(const chem_atom &a, const chem_atom &b)
Definition: mole.h:106
transition.h
mole_state
mole_state
Definition: mole.h:15
total_molecules
realnum total_molecules(void)
Definition: mole_species.cpp:1109
chem_atom::compare
int compare(const chem_atom &b) const
Definition: mole.h:72
molecule::groupnum
int groupnum
Definition: mole.h:169
molecule::lgExcit
bool lgExcit
Definition: mole.h:145
mole_punch
void mole_punch(FILE *punit, const char speciesname[], const char args[], bool lgHeader, bool lgData, double depth)
Definition: mole_reactions.cpp:4221
operator>=
bool operator>=(const chem_atom &a, const chem_atom &b)
Definition: mole.h:102
mole_update_sources
void mole_update_sources(void)
Definition: mole_drive.cpp:106
mole_drive
void mole_drive(void)
Definition: mole_drive.cpp:41
total_molecule_deut
void total_molecule_deut(realnum &total)
Definition: mole_species.cpp:1034
molezone::levels
qList * levels
Definition: mole.h:354
molecule::heavyAtom
chem_atom * heavyAtom(void)
Definition: mole.h:171
t_mole_global::lgNoHeavyMole
bool lgNoHeavyMole
Definition: mole.h:280
qList
Definition: quantumstate.h:40
null_mole
molecule * null_mole
Definition: mole_species.cpp:64
t_mole_local::xMoleChTrRate
realnum *** xMoleChTrRate
Definition: mole.h:396
t_mole_global::lgGrain_mole_deplete
bool lgGrain_mole_deplete
Definition: mole.h:308
atoms
t_atoms atoms
Definition: atoms.cpp:5
chem_atom::mass_amu
realnum mass_amu
Definition: mole.h:46
t_mole_local::sink_rate_tot
double sink_rate_tot(const char chSpecies[]) const
Definition: mole_reactions.cpp:3922
t_mole_global::num_total
int num_total
Definition: mole.h:314
t_mole_local::old_reaction_rks
vector< double > old_reaction_rks
Definition: mole.h:401
molecule::form_enthalpy
realnum form_enthalpy
Definition: mole.h:164
nAtoms_cri
molecule::nAtomsMap::const_reverse_iterator nAtoms_cri
Definition: mole.h:216
chem_atom::ipMl
vector< int > ipMl
Definition: mole.h:45
chem_element::isotopes
map< int, count_ptr< chem_atom > > isotopes
Definition: mole.h:29
molezone::column
realnum column
Definition: mole.h:359
chem_atom::A
int A
Definition: mole.h:44
chem_element::chem_element
chem_element(int Z, const char *label)
Definition: mole.h:23
mole_cmp_num_in_out_reactions
void mole_cmp_num_in_out_reactions(void)
Definition: mole_reactions.cpp:2183
chem_element::~chem_element
~chem_element()
Definition: mole.h:25
molecule::isEnabled
bool isEnabled
Definition: mole.h:139
LIMELM
const int LIMELM
Definition: cddefines.h:258
mole_make_list
void mole_make_list(void)
Definition: mole_species.cpp:296
t_mole_global::lgNoMole
bool lgNoMole
Definition: mole.h:277
t_mole_global::lgLeidenHack
bool lgLeidenHack
Definition: mole.h:286
mole
t_mole_local mole
Definition: mole.cpp:7
MOLE_NULL
@ MOLE_NULL
Definition: mole.h:15
molecule::n_nuclei
int n_nuclei(void) const
Definition: mole.h:147
element_pointer_value_less
Definition: mole.h:121
isotopes_i
map< int, count_ptr< chem_atom > >::iterator isotopes_i
Definition: mole.h:35
t_mole_local::set_isotope_abundances
void set_isotope_abundances(void)
Definition: mole_species.cpp:989
start
STATIC void start(long, realnum[], realnum[], long, long[], realnum *, int *)
t_mole_local::source
double ** source
Definition: mole.h:394
null_element
chem_element * null_element
Definition: mole_species.cpp:67
molezone::molezone
molezone()
Definition: mole.h:328
t_mole_global::lgNeutrals
bool lgNeutrals
Definition: mole.h:304
t_mole_global::lgProtElim
bool lgProtElim
Definition: mole.h:299
chem_atom::label
string label(void) const
Definition: mole.h:55
molezone::location
double * location
Definition: mole.h:349
molezone::lines
TransitionList * lines
Definition: mole.h:355
molezone::zero
void zero(void)
Definition: mole.h:339
chem_element::chem_element
chem_element()
molezone::column_old
realnum column_old
Definition: mole.h:364
mole_rk_bigchange
void mole_rk_bigchange(void)
Definition: mole_reactions.cpp:2866
TransitionList
Definition: transition.h:274
create_isotopologues_one
void create_isotopologues_one(ChemAtomList &atoms, vector< int > &numAtoms, string atom_old, string atom_new, string embellishments, vector< string > &newLabels)
Definition: mole_species.cpp:387
chem_element::operator=
chem_element & operator=(const chem_element &)
chem_element::Z
const int Z
Definition: mole.h:27
operator<=
bool operator<=(const chem_atom &a, const chem_atom &b)
Definition: mole.h:98
molezone::snk
double snk
Definition: mole.h:352
total_molecules_gasphase
realnum total_molecules_gasphase(void)
Definition: mole_species.cpp:1124
chem_atom
Definition: mole.h:37
t_mole_global::MoleculeList
vector< count_ptr< molecule > > MoleculeList
Definition: mole.h:316
molezone::init
void init(void)
Definition: mole.h:332
molezone
Definition: mole.h:326
t_mole_global::num_compacted
int num_compacted
Definition: mole.h:314
count_ptr.h
chem_atom::lgMeanAbundance
bool lgMeanAbundance(void) const
Definition: mole.h:49
findspecies
molecule * findspecies(const char buf[])
Definition: mole_species.cpp:814
t_mole_global::lgTreatIsotopes
vector< bool > lgTreatIsotopes
Definition: mole.h:311
t_mole_local::dissoc_rate
double dissoc_rate(const char chSpecies[]) const
Definition: mole_reactions.cpp:3999
null_atom
chem_atom * null_atom
Definition: mole_species.cpp:68
molecule
Definition: mole.h:132
t_mole_local::species
valarray< class molezone > species
Definition: mole.h:398
parse_species_label
bool parse_species_label(const char label[], ChemAtomList &atomsLeftToRight, vector< int > &numAtoms, string &embellishments)
Definition: mole_species.cpp:678
molecule::nAtom
nAtomsMap nAtom
Definition: mole.h:143
element_pointer_value_less::operator()
bool operator()(const count_ptr< chem_atom > &a, const count_ptr< chem_atom > &b) const
Definition: mole.h:124
molecule::compare
int compare(const molecule &mol2) const
Definition: mole.h:183
atom_list
ChemAtomList atom_list
Definition: mole_species.cpp:71
molecule::label
string label
Definition: mole.h:142
operator<
bool operator<(const chem_atom &a, const chem_atom &b)
Definition: mole.h:90
chSpecies
char ** chSpecies
Definition: taulines.cpp:13
t_mole_local::reaction_rks
vector< double > reaction_rks
Definition: mole.h:400
t_mole_local::findrk
double findrk(const char buf[]) const
Definition: mole_reactions.cpp:3886
nAtoms_ri
molecule::nAtomsMap::reverse_iterator nAtoms_ri
Definition: mole.h:215
t_mole_global::lgH2Ozer
bool lgH2Ozer
Definition: mole.h:283
molezone::den
double den
Definition: mole.h:358
t_mole_global::lgFederman
bool lgFederman
Definition: mole.h:288
mole_update_species_cache
void mole_update_species_cache(void)
Definition: mole_species.cpp:866
t_mole_global
Definition: mole.h:266
t_mole_global::num_calc
int num_calc
Definition: mole.h:314
molezone::xFracLim
realnum xFracLim
Definition: mole.h:361
molecule::parentLabel
string parentLabel
Definition: mole.h:137
t_mole_local::chem_heat
double chem_heat(void) const
Definition: mole_reactions.cpp:4115
chem_atom::frac
double frac
Definition: mole.h:47
t_mole_global::make_species
void make_species(void)
Definition: mole_species.cpp:140