cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mole.h
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 #ifndef MOLE_H_
5 #define MOLE_H_
6 
7 /* mole.h */
8 
9 #include "count_ptr.h"
10 #include "module.h"
11 
12 class TransitionList;
13 class qList;
14 class species;
15 class molezone;
16 
17 #define SMALLABUND 1e-24
18 
20 
21 class chem_nuclide;
22 
23 class chem_element {
24  explicit chem_element(); // Do not implement
25  chem_element &operator=(const chem_element&); // Do not implement
26 public:
27  explicit chem_element(int Z, const char*label) : Z(Z), label(label)
28  {}
29  ~chem_element() throw()
30  {}
31  const int Z;
32  const string label;
33  map<int, count_ptr<chem_nuclide> > isotopes;
34  //(first -> Atomic A; second -> chem_nuclide )
35  //(first -> -1 for bogus isotope, i.e. where no
36  // isotopes have been explicitly defined)
37 };
38 
39 typedef map<int, count_ptr<chem_nuclide> >::iterator isotopes_i;
40 
41 class chem_nuclide {
42 public:
43  // Link back to basic element for convenience -- not a count_ptr
44  // as this would lead to a reference cycle (and hence the
45  // destructors never being called). Many-to-one relation suggests
46  // that the weak link should be this way around.
48  int A; /* mass number */
49  unsigned long index; /* index in list for temporary use */
50  vector<int> ipMl; /* Atom and ion species in molecule arrays */
51  realnum mass_amu; /* mass of isotope in AMU */
52  double frac; /* fraction of element in this isotope */
53 
54  bool lgMeanAbundance( void ) const
55  {
56  return ( A < 0 );
57  }
58 
59  // May have independent logic in the future...
60  bool lgHasLinkedIon( void ) const
61  {
62  return lgMeanAbundance();
63  }
64 
65  /* Chemical symbols for elements */
66  string label( void ) const
67  {
68  if( lgMeanAbundance() )
69  return el->label;
70  else if( el->Z==1 && A==2 )
71  {
72  // Deuterium is a special case
73  return "D\0";
74  }
75  else
76  {
77  char str[20];
78  sprintf(str,"^%d",A);
79  return ( str + el->label );
80  }
81  }
82 
83  int compare ( const chem_nuclide &b ) const
84  {
85  // sort by proton number first
86  if ( el->Z < b.el->Z )
87  return -1;
88  if ( el->Z > b.el->Z )
89  return 1;
90 
91  if ( A < b.A )
92  return -1;
93  if ( A > b.A )
94  return 1;
95 
96  return 0;
97  }
98 };
99 inline bool operator< (const chem_nuclide &a, const chem_nuclide &b)
100 {
101  return a.compare(b) < 0;
102 }
103 inline bool operator> (const chem_nuclide &a, const chem_nuclide &b)
104 {
105  return a.compare(b) > 0;
106 }
107 inline bool operator<= (const chem_nuclide &a, const chem_nuclide &b)
108 {
109  return a.compare(b) <= 0;
110 }
111 inline bool operator>= (const chem_nuclide &a, const chem_nuclide &b)
112 {
113  return a.compare(b) >= 0;
114 }
115 inline bool operator== (const chem_nuclide &a, const chem_nuclide &b)
116 {
117  return a.compare(b) == 0;
118 }
119 inline bool operator!= (const chem_nuclide &a, const chem_nuclide &b)
120 {
121  return !(a == b);
122 }
123 
124 typedef vector< count_ptr<chem_nuclide> > ChemNuclideList;
126 typedef vector< count_ptr<chem_element> > ChemElementList;
128 extern chem_element *null_element;
129 extern chem_nuclide *null_nuclide;
130 
132 {
133 public:
135  const count_ptr<chem_nuclide>& b) const
136  {
137  return *a < *b;
138  }
139 };
140 
141 /* Structure containing molecule data, initially only CO */
142 class molecule {
143 public:
144  typedef map<const count_ptr<chem_nuclide>, int,
146 
147  string parentLabel;
149  {
150  return parentLabel.empty();
151  }
153  bool isEnabled; /* Is it enabled? */
154 
155  /* Species physical data */
156  string label;
158  int charge;
159  bool lgExcit;
160  bool lgGas_Phase;
161  int n_nuclei(void) const
162  {
163  int num = 0;
164  for (nNucsMap::const_iterator el = nNuclide.begin();
165  el != nNuclide.end(); ++el)
166  {
167  num += el->second;
168  }
169  return num;
170  }
171  int nElement(int nelem)
172  {
173  int num = 0;
174  for (nNucsMap::const_iterator el = nNuclide.begin();
175  el != nNuclide.end(); ++el)
176  {
177  if (el->first->el->Z-1 == nelem)
178  num += el->second;
179  }
180  return num;
181  }
182  bool isMonatomic(void) const
183  {
184  if (nNuclide.size() == 1 && nNuclide.begin()->second == 1)
185  return true;
186  return false;
187  }
188 
192  /* Parameters as computational object */
195  int n_react;
197  class molezone *local(void) const;
198 
199  chem_nuclide *heavyAtom(void) //const
200  {
201  for( nNucsMap::reverse_iterator it=nNuclide.rbegin(); it!=nNuclide.rend(); ++it )
202  {
203  if (0 != it->second )
204  {
205  return it->first.get_ptr();
206  }
207  }
208  return null_nuclide;
209  }
210 
211  int compare(const molecule &mol2) const
212  {
213  nNucsMap::const_reverse_iterator it1, it2;
214 
215  for( it1 = nNuclide.rbegin(), it2 = mol2.nNuclide.rbegin();
216  it1 != nNuclide.rend() && it2 != mol2.nNuclide.rend(); ++it1, ++it2 )
217  {
218  if( *(it1->first) > *(it2->first) )
219  return 1;
220  else if( *(it1->first) < *(it2->first) )
221  return -1;
222  else if( it1->second > it2->second)
223  return 1;
224  else if( it1->second < it2->second)
225  return -1;
226  }
227 
228  if( it1 != nNuclide.rend() && it2 == mol2.nNuclide.rend() )
229  return 1;
230  else if( it1 == nNuclide.rend() && it2 != mol2.nNuclide.rend() )
231  return -1;
232  else
233  ASSERT( it1 == nNuclide.rend() && it2 == mol2.nNuclide.rend() );
234 
235  // sort by label if falls through to here
236  return ( label.compare(mol2.label) );
237 
238  }
239 };
240 
241 /* iterators on nNuclide */
242 typedef molecule::nNucsMap::iterator nNucs_i;
243 typedef molecule::nNucsMap::reverse_iterator nNucs_ri;
244 typedef molecule::nNucsMap::const_reverse_iterator nNucs_cri;
245 
247 extern void mole_drive(void);
248 
250 extern void mole_create_react(void);
251 
252 class mole_reaction;
253 
254 mole_reaction *mole_findrate_s(const char buf[]);
255 
256 extern molecule *null_mole;
257 
258 inline bool exists (const molecule* m)
259 {
260  return m != null_mole;
261 }
262 
264 extern molecule *findspecies(const char buf[]);
266 extern molecule *findspecies_validate(const char buf[]);
267 
269 extern molezone *findspecieslocal(const char buf[]);
271 extern molezone *findspecieslocal_validate(const char buf[]);
272 
273 extern count_ptr<chem_nuclide> findnuclide(const char buf[]);
274 
307 class Properties;
308 
309 
310 class t_mole_global : public module {
311 
312 public:
313  const char *chName() const
314  {
315  return "mole_global";
316  }
317  void zero();
318  void comment(t_warnings&) {}
320  void init(void);
321 
322  void make_species(void);
323 
325  bool lgNoMole;
326 
329 
331  bool lgH2Ozer;
332 
335 
337  bool lgStancil;
338 
343 
348 
353 
357 
358  // flag saying whether to model isotopes (and isotopologues) of a given element
359  vector<bool> lgTreatIsotopes;
360 
363 
364  typedef vector<count_ptr<molecule> > MoleculeList;
366 
367  map<string,bool> offReactions;
368  map<string,Properties > speciesProperties;
369 
370  static void sort(MoleculeList::iterator start,
371  MoleculeList::iterator end);
372  static void sort(molecule **start, molecule **end);
373 
374 };
375 
377 
378 class molezone {
379 public:
381  {
382  init();
383  }
384  void init (void)
385  {
386  location = NULL;
387  levels = NULL;
388  lines = NULL;
389  dbase = NULL;
390  zero();
391  }
392  void zero (void)
393  {
394  src = 0.;
395  snk = 0.;
396  den = 0.;
397  column = 0.;
399  xFracLim = 0.;
400  column_old = 0.;
401  index = -1;
402  }
403  int index;
404  const molecule* global() const
405  {
406  if (index == -1)
407  return null_mole;
408  return mole_global.list[index].get_ptr();
409  }
410 
411  double *location;
414  double src, snk;
415 
419 
420  /* Current zone data */
421  double den;
423  chem_nuclide* atomLim; /* atom which is closest to limiting molecule density */
426  /* Historical solution data */
428 };
429 
430 extern molezone *null_molezone;
431 
432 class t_mole_local : public module
433 {
434 public:
435  void alloc();
436  void zero();
437  void comment(t_warnings&) {}
438  const char *chName() const
439  {
440  return "mole";
441  }
442  void set_ion_locations( );
443  void set_isotope_abundances( void );
444  double sink_rate_tot(const char chSpecies[]) const;
445  double sink_rate_tot(const molecule* const sp) const;
446  double sink_rate(const molecule* const sp, const mole_reaction& rate) const;
447  double sink_rate(const molecule* const sp, const char buf[]) const;
448  double source_rate_tot(const char chSpecies[]) const;
449  double source_rate_tot(const molecule* const sp) const;
452  double dissoc_rate(const char chSpecies[]) const;
453  double chem_heat(void) const;
454  double findrk(const char buf[]) const;
455  double findrate(const char buf[]) const;
456 
458 
460  double elec;
461 
464  double **source , **sink;
465 
466  realnum ***xMoleChTrRate;/***[LIMELM][LIMELM+1][LIMELM+1];*/
467 
468  valarray<class molezone> species;
469 
470  vector<double> reaction_rks;
471  vector<double> old_reaction_rks;
472  long old_zone;
473 };
474 
475 extern t_mole_local mole;
476 
477 inline bool exists (const molezone* m)
478 {
479  return m != null_molezone;
480 }
481 
482 inline molezone *molecule::local(void) const
483 {
484  if ( index != -1)
485  return &mole.species[index];
486  else
487  return null_molezone;
488 }
489 
490 extern void total_molecule_elems(realnum total[LIMELM]);
491 extern void total_molecule_deut(realnum &total);
492 
493 extern realnum total_molecules(void);
494 
495 extern realnum total_molecules_gasphase(void);
496 
497 extern void mole_make_list(void);
498 extern void mole_make_groups(void);
499 
501 
502 bool lgDifferByExcitation( const molecule &mol1, const molecule &mol2 );
503 
504 extern void mole_update_species_cache(void);
505 
506 void mole_update_sources(void);
507 
508 void mole_rk_bigchange(void);
509 
512  vector< int >& numAtoms,
513  string atom_old,
514  string atom_new,
515  string embellishments,
516  vector<string>& newLabels );
517 
519  unsigned position,
521  vector< int >& numAtoms,
522  string atom_old,
523  string atom_new,
524  string embellishments,
525  string& newLabel );
526 
527 bool parse_species_label( const char label[], ChemNuclideList &atomsLeftToRight, vector<int> &numAtoms, string &embellishments );
528 bool parse_species_label( const char mylab[], ChemNuclideList &atomsLeftToRight, vector<int> &numAtoms, string &embellishments,
529  bool &lgExcit, int &charge, bool &lgGas_Phase );
530 
531 /*HMRATE compile molecular rates using Hollenbach and McKee fits */
532 /* #define HMRATE(a,b,c) ( ((b) == 0 && (c) == 0) ? (a) : \
533  * ( ((c) == 0) ? (a)*pow(phycon.te/300.,(b)) : \
534  * ( ((c)/phycon.te > 50.) ? 0. : ( ((b) == 0) ? (a)*exp(-(c)/phycon.te) : \
535  * (a)*pow(phycon.te/300.,(b))*exp(-(c)/phycon.te) ) ) ) ) */
536 
537 inline double hmrate4( double a, double b, double c, double te )
538 {
539  if( b == 0. && c == 0. )
540  return a;
541  else if( c == 0. )
542  return a*pow(te/300.,b);
543  else if( b == 0. )
544  return a*sexp(c/te);
545  else
546  return a*pow(te/300.,b)*sexp(c/te);
547 }
548 
549 class Parser;
550 void ParseChemistry(Parser &p);
551 
552 #endif /* MOLE_H_ */
553 
double sink_rate_tot(const char chSpecies[]) const
t_mole_global mole_global
Definition: mole.cpp:7
double grain_area
Definition: mole.h:457
bool lgStancil
Definition: mole.h:337
nNucsMap nNuclide
Definition: mole.h:157
int compare(const chem_nuclide &b) const
Definition: mole.h:83
static void sort(MoleculeList::iterator start, MoleculeList::iterator end)
vector< double > reaction_rks
Definition: mole.h:470
chem_element * el
Definition: mole.h:47
int index
Definition: mole.h:403
molecule::nNucsMap::iterator nNucs_i
Definition: mole.h:242
molecule * null_mole
void mole_create_react(void)
long old_zone
Definition: mole.h:472
int n_nuclei(void) const
Definition: mole.h:161
vector< bool > lgTreatIsotopes
Definition: mole.h:359
int num_total
Definition: mole.h:362
mole_state
Definition: mole.h:19
realnum total_molecules(void)
int num_calc
Definition: mole.h:362
void mole_drive(void)
Definition: mole_drive.cpp:29
map< int, count_ptr< chem_nuclide > >::iterator isotopes_i
Definition: mole.h:39
void make_species(void)
void total_molecule_deut(realnum &total)
const molecule * global() const
Definition: mole.h:404
int parentIndex
Definition: mole.h:152
bool operator<(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:88
void zero()
Definition: mole.cpp:119
bool lgH2Ozer
Definition: mole.h:331
const string label
Definition: mole.h:32
void mole_make_list(void)
void comment(t_warnings &)
Definition: mole.h:437
double findrk(const char buf[]) const
sys_float sexp(sys_float x)
Definition: service.cpp:999
TransitionList * lines
Definition: mole.h:418
realnum column
Definition: mole.h:422
bool lgProtElim
Definition: mole.h:347
vector< count_ptr< chem_element > > ChemElementList
Definition: mole.h:126
molezone * findspecieslocal(const char buf[])
double source_rate_tot(const char chSpecies[]) const
Definition: mole.h:378
bool lgGas_Phase
Definition: mole.h:160
bool operator!=(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:113
ChemNuclideList nuclide_list
Definition: mole.h:142
bool parse_species_label(const char label[], ChemNuclideList &atomsLeftToRight, vector< int > &numAtoms, string &embellishments)
Definition: parser.h:43
int A
Definition: mole.h:48
int num_compacted
Definition: mole.h:362
realnum mass_amu
Definition: mole.h:51
vector< count_ptr< molecule > > MoleculeList
Definition: mole.h:364
char ** chSpecies
Definition: taulines.cpp:14
bool lgNonEquilChem
Definition: mole.h:342
molezone * findspecieslocal_validate(const char buf[])
map< string, bool > offReactions
Definition: mole.h:367
realnum form_enthalpy
Definition: mole.h:189
double grain_density
Definition: mole.h:457
Definition: mole.h:19
vector< int > ipMl
Definition: mole.h:50
ChemElementList element_list
bool isEnabled
Definition: mole.h:153
realnum xFracLim
Definition: mole.h:424
double ** sink
Definition: mole.h:464
void mole_cmp_num_in_out_reactions(void)
double chem_heat(void) const
void init(void)
Definition: mole.cpp:52
qList * levels
Definition: mole.h:417
molecule::nNucsMap::reverse_iterator nNucs_ri
Definition: mole.h:243
vector< double > old_reaction_rks
Definition: mole.h:471
bool lgLeidenHack
Definition: mole.h:334
map< int, count_ptr< chem_nuclide > > isotopes
Definition: mole.h:33
void zero()
Definition: mole.cpp:10
bool lgMeanAbundance(void) const
Definition: mole.h:54
double ** source
Definition: mole.h:464
string parentLabel
Definition: mole.h:147
int groupnum
Definition: mole.h:194
class molezone * local(void) const
Definition: mole.h:482
chem_nuclide * null_nuclide
t_mole_local mole
Definition: mole.cpp:8
molecule * findspecies(const char buf[])
chem_element & operator=(const chem_element &)
void create_isotopologues_one_position(unsigned position, ChemNuclideList &atoms, vector< int > &numAtoms, string atom_old, string atom_new, string embellishments, string &newLabel)
t_atoms atoms
Definition: atoms.cpp:5
molezone()
Definition: mole.h:380
realnum *** xMoleChTrRate
Definition: mole.h:466
~chem_element()
Definition: mole.h:29
float realnum
Definition: cddefines.h:124
valarray< class molezone > species
Definition: mole.h:468
enum mole_state state
Definition: mole.h:193
double sink_rate(const molecule *const sp, const mole_reaction &rate) const
void mole_rk_bigchange(void)
count_ptr< chem_nuclide > findnuclide(const char buf[])
double snk
Definition: mole.h:414
int index
Definition: mole.h:194
realnum total_molecules_gasphase(void)
const int Z
Definition: mole.h:31
bool operator()(const count_ptr< chem_nuclide > &a, const count_ptr< chem_nuclide > &b) const
Definition: mole.h:134
bool lgExcit
Definition: mole.h:159
species * dbase
Definition: mole.h:416
int n_react
Definition: mole.h:195
bool exists(const molecule *m)
Definition: mole.h:258
map< const count_ptr< chem_nuclide >, int, element_pointer_value_less > nNucsMap
Definition: mole.h:145
bool operator>=(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:103
int compare(const molecule &mol2) const
Definition: mole.h:211
molecule::nNucsMap::const_reverse_iterator nNucs_cri
Definition: mole.h:244
vector< count_ptr< chem_nuclide > > ChemNuclideList
Definition: mole.h:124
void mole_update_sources(void)
Definition: mole_drive.cpp:55
chem_nuclide * heavyAtom(void)
Definition: mole.h:199
#define ASSERT(exp)
Definition: cddefines.h:613
void set_isotope_abundances(void)
bool lgNeutrals
Definition: mole.h:352
double findrate(const char buf[]) const
STATIC void start(long, realnum[], realnum[], long, long[], realnum *, int *)
bool operator==(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:108
const int LIMELM
Definition: cddefines.h:308
int nElement(int nelem)
Definition: mole.h:171
bool isMonatomic(void) const
Definition: mole.h:182
int charge
Definition: mole.h:158
bool operator<=(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:98
double den
Definition: mole.h:421
bool operator>(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:93
bool lgGrain_mole_deplete
Definition: mole.h:356
bool lgDifferByExcitation(const molecule &mol1, const molecule &mol2)
void mole_update_species_cache(void)
const char * chName() const
Definition: mole.h:438
void alloc()
Definition: mole.cpp:91
string label
Definition: mole.h:156
const char * chName() const
Definition: mole.h:313
void zero(void)
Definition: mole.h:392
double elec
Definition: mole.h:460
realnum mole_mass
Definition: mole.h:190
MoleculeList list
Definition: mole.h:365
double dissoc_rate(const char chSpecies[]) const
void comment(t_warnings &)
Definition: mole.h:318
bool isIsotopicTotalSpecies() const
Definition: mole.h:148
mole_reaction * mole_findrate_s(const char buf[])
map< string, Properties > speciesProperties
Definition: mole.h:368
double frac
Definition: mole.h:52
string label(void) const
Definition: mole.h:66
double hmrate4(double a, double b, double c, double te)
Definition: mole.h:537
chem_nuclide * atomLim
Definition: mole.h:423
bool lgNoHeavyMole
Definition: mole.h:328
bool lgFederman
Definition: mole.h:336
void set_ion_locations()
bool lgNoMole
Definition: mole.h:325
void mole_make_groups(void)
realnum column_old
Definition: mole.h:427
double src
Definition: mole.h:414
unsigned long index
Definition: mole.h:49
chem_element * null_element
void total_molecule_elems(realnum total[LIMELM])
Definition: module.h:26
molecule * findspecies_validate(const char buf[])
bool lgHasLinkedIon(void) const
Definition: mole.h:60
molezone * null_molezone
double * location
Definition: mole.h:411
chem_element(int Z, const char *label)
Definition: mole.h:27
void create_isotopologues(ChemNuclideList &atoms, vector< int > &numAtoms, string atom_old, string atom_new, string embellishments, vector< string > &newLabels)
void init(void)
Definition: mole.h:384
double grain_saturation
Definition: mole.h:457
void ParseChemistry(Parser &p)
Definition: mole.cpp:144