Cloudy
Spectral Synthesis Code for Astrophysics
 All Classes 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 "module.h"
10 #include "container_classes.h"
11 
12 class TransitionList;
13 class qList;
14 class species;
15 class molezone;
16 
17 static const double 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, shared_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, shared_ptr<chem_nuclide> >::iterator isotopes_i;
40 
41 class chem_nuclide {
42 public:
43  // Link back to basic element for convenience -- not a shared_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  chem_element* el() const
55  {
56  return wel;
57  }
58  bool lgMeanAbundance( void ) const
59  {
60  return ( A < 0 );
61  }
62 
63  // May have independent logic in the future...
64  bool lgHasLinkedIon( void ) const
65  {
66  return lgMeanAbundance();
67  }
68 
69  /* Chemical symbols for elements */
70  string label( void ) const
71  {
72  if( lgMeanAbundance() )
73  return el()->label;
74  else if( el()->Z==1 && A==2 )
75  {
76  // Deuterium is a special case
77  return "D";
78  }
79  else
80  {
81  char str[20];
82  sprintf(str,"^%d",A);
83  return ( str + el()->label );
84  }
85  }
86 
87  int compare ( const chem_nuclide &b ) const
88  {
89  // sort by proton number first
90  if ( el()->Z < b.el()->Z )
91  return -1;
92  if ( el()->Z > b.el()->Z )
93  return 1;
94 
95  if ( A < b.A )
96  return -1;
97  if ( A > b.A )
98  return 1;
99 
100  return 0;
101  }
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.compare(b) == 0;
122 }
123 inline bool operator!= (const chem_nuclide &a, const chem_nuclide &b)
124 {
125  return !(a == b);
126 }
127 
128 typedef vector< shared_ptr<chem_nuclide> > ChemNuclideList;
130 typedef vector< shared_ptr<chem_element> > ChemElementList;
132 extern chem_nuclide *null_nuclide;
133 
135 {
136 public:
137  bool operator()(const shared_ptr<chem_nuclide>& a,
138  const shared_ptr<chem_nuclide>& b) const
139  {
140  return *a < *b;
141  }
142 };
143 
144 /* Structure containing molecule data, initially only CO */
145 class molecule {
146 public:
147  typedef map<const shared_ptr<chem_nuclide>, int,
149 
150  string parentLabel;
152  {
153  return parentLabel.empty();
154  }
156  bool isEnabled; /* Is it enabled? */
157 
158  /* Species physical data */
159  string label;
161  int charge;
162  bool lgExcit;
163  bool lgGas_Phase;
164  int n_nuclei(void) const
165  {
166  int num = 0;
167  for (nNucsMap::const_iterator el = nNuclide.begin();
168  el != nNuclide.end(); ++el)
169  {
170  num += el->second;
171  }
172  return num;
173  }
174  int nElement(int nelem)
175  {
176  int num = 0;
177  for (nNucsMap::const_iterator el = nNuclide.begin();
178  el != nNuclide.end(); ++el)
179  {
180  if (el->first->el()->Z-1 == nelem)
181  num += el->second;
182  }
183  return num;
184  }
185 
190  bool isMonatomic(void) const
191  {
192  if (nNuclide.size() == 1 && nNuclide.begin()->second == 1)
193  return true;
194  return false;
195  }
196 
202  bool isMolecule() const
203  {
204  if( nNuclide.size() > 1 || ( nNuclide.size() == 1 && nNuclide.begin()->second > 1 ) )
205  return true;
206  return false;
207  }
208 
212  /* Parameters as computational object */
215  int n_react;
217  class molezone *local(void) const;
218 
219  chem_nuclide *heavyAtom(void) //const
220  {
221  for( nNucsMap::reverse_iterator it=nNuclide.rbegin(); it!=nNuclide.rend(); ++it )
222  {
223  if (0 != it->second )
224  {
225  return it->first.get();
226  }
227  }
228  return null_nuclide;
229  }
230 
231  int compare(const molecule &mol2) const
232  {
233  nNucsMap::const_reverse_iterator it1, it2;
234 
235  for( it1 = nNuclide.rbegin(), it2 = mol2.nNuclide.rbegin();
236  it1 != nNuclide.rend() && it2 != mol2.nNuclide.rend(); ++it1, ++it2 )
237  {
238  if( *(it1->first) > *(it2->first) )
239  return 1;
240  else if( *(it1->first) < *(it2->first) )
241  return -1;
242  else if( it1->second > it2->second)
243  return 1;
244  else if( it1->second < it2->second)
245  return -1;
246  }
247 
248  if( it1 != nNuclide.rend() && it2 == mol2.nNuclide.rend() )
249  return 1;
250  else if( it1 == nNuclide.rend() && it2 != mol2.nNuclide.rend() )
251  return -1;
252  else
253  ASSERT( it1 == nNuclide.rend() && it2 == mol2.nNuclide.rend() );
254 
255  // sort by label if falls through to here
256  return ( label.compare(mol2.label) );
257 
258  }
259 };
260 
261 /* iterators on nNuclide */
262 typedef molecule::nNucsMap::iterator nNucs_i;
263 typedef molecule::nNucsMap::reverse_iterator nNucs_ri;
264 typedef molecule::nNucsMap::const_reverse_iterator nNucs_cri;
265 
267 extern void mole_drive(void);
268 
270 extern void mole_create_react(void);
271 
272 class mole_reaction;
273 
274 mole_reaction *mole_findrate_s(const char buf[]);
275 
276 extern molecule *null_mole;
277 
278 inline bool exists (const molecule* m)
279 {
280  return m != null_mole;
281 }
282 
284 extern molecule *findspecies(const char buf[]);
286 extern molecule *findspecies_validate(const char buf[]);
287 
289 extern molezone *findspecieslocal(const char buf[]);
291 extern molezone *findspecieslocal_validate(const char buf[]);
292 
293 extern shared_ptr<chem_nuclide> findnuclide(const char buf[]);
294 
327 class Properties;
328 
329 
330 class t_mole_global : public module {
331 
332 public:
333  const char *chName() const
334  {
335  return "mole_global";
336  }
337  void zero();
338  void comment(t_warnings&) {}
340  void init(void);
341 
342  void make_species(void);
343 
345  bool lgNoMole;
346 
349 
351  bool lgH2Ozer;
352 
358 
365 
366  bool lgStancil;
367 
372 
377 
382 
386 
387  // flag saying whether to model isotopes (and isotopologues) of a given element
388  vector<bool> lgTreatIsotopes;
389 
392 
393  typedef vector<shared_ptr<molecule> > MoleculeList;
395 
396  map<string,bool> offReactions;
397  map<string,Properties > speciesProperties;
398 
399  static void sort(MoleculeList::iterator start,
400  MoleculeList::iterator end);
401  static void sort(molecule **start, molecule **end);
402 
403 };
404 
406 
407 class molezone {
408 public:
410  {
411  init();
412  }
413  void init (void)
414  {
415  location = NULL;
416  levels = NULL;
417  lines = NULL;
418  dbase = NULL;
419  zero();
420  }
421  void zero (void)
422  {
423  src = 0.;
424  snk = 0.;
425  den = 0.;
426  column = 0.;
428  xFracLim = 0.;
429  column_old = 0.;
430  index = -1;
431  }
432  int index;
433  const molecule* global() const
434  {
435  if (index == -1)
436  return null_mole;
437  return mole_global.list[index].get();
438  }
439 
440  double *location;
443  double src, snk;
444 
448 
449  /* Current zone data */
450  double den;
452  chem_nuclide* atomLim; /* atom which is closest to limiting molecule density */
455  /* Historical solution data */
457 };
458 
459 extern molezone *null_molezone;
460 
461 class t_mole_local : public module
462 {
463 public:
464  void alloc();
465  void zero();
466  void comment(t_warnings&) {}
467  const char *chName() const
468  {
469  return "mole";
470  }
471  void set_ion_locations( );
472  void set_isotope_abundances( void );
473  double sink_rate_tot(const char chSpecies[]) const;
474  double sink_rate_tot(const molecule* const sp) const;
475  double sink_rate(const molecule* const sp, const mole_reaction& rate) const;
476  double sink_rate(const molecule* const sp, const char buf[]) const;
477  double source_rate_tot(const char chSpecies[]) const;
478  double source_rate_tot(const molecule* const sp) const;
481  double dissoc_rate(const char chSpecies[]) const;
482  double chem_heat(void) const;
483  double findrk(const char buf[]) const;
484  double findrate(const char buf[]) const;
485 
487 
489  double elec;
490 
494 
496 
497  valarray<class molezone> species;
498 
499  vector<double> reaction_rks;
500  vector<double> old_reaction_rks;
501  long old_zone;
502 };
503 
504 extern t_mole_local mole;
505 
506 inline bool exists (const molezone* m)
507 {
508  return m != null_molezone;
509 }
510 
511 inline molezone *molecule::local(void) const
512 {
513  if ( index != -1)
514  return &mole.species[index];
515  else
516  return null_molezone;
517 }
518 
519 extern void total_molecule_elems(realnum total[LIMELM]);
520 extern void total_molecule_deut(realnum &total);
521 
522 extern realnum total_molecules(void);
523 
524 extern realnum total_molecules_gasphase(void);
525 
532 
533 extern void mole_make_list(void);
534 extern void mole_make_groups(void);
535 
537 
538 bool lgDifferByExcitation( const molecule &mol1, const molecule &mol2 );
539 
540 extern void mole_update_species_cache(void);
541 
542 void mole_update_sources(void);
543 
544 void mole_rk_bigchange(void);
545 
548  vector< int >& numAtoms,
549  string atom_old,
550  string atom_new,
551  string embellishments,
552  vector<string>& newLabels );
553 
555  unsigned position,
557  vector< int >& numAtoms,
558  string atom_old,
559  string atom_new,
560  string embellishments,
561  string& newLabel );
562 
563 bool parse_species_label( const char label[], ChemNuclideList &atomsLeftToRight, vector<int> &numAtoms, string &embellishments );
564 bool parse_species_label( const char mylab[], ChemNuclideList &atomsLeftToRight, vector<int> &numAtoms, string &embellishments,
565  bool &lgExcit, int &charge, bool &lgGas_Phase );
566 
567 /*HMRATE compile molecular rates using Hollenbach and McKee fits */
568 /* #define HMRATE(a,b,c) ( ((b) == 0 && (c) == 0) ? (a) : \
569  * ( ((c) == 0) ? (a)*pow(phycon.te/300.,(b)) : \
570  * ( ((c)/phycon.te > 50.) ? 0. : ( ((b) == 0) ? (a)*exp(-(c)/phycon.te) : \
571  * (a)*pow(phycon.te/300.,(b))*exp(-(c)/phycon.te) ) ) ) ) */
572 
573 inline double hmrate4( double a, double b, double c, double te )
574 {
575  if( b == 0. && c == 0. )
576  return a;
577  else if( c == 0. )
578  return a*pow(te/300.,b);
579  else if( b == 0. )
580  return a*sexp(c/te);
581  else
582  return a*pow(te/300.,b)*sexp(c/te);
583 }
584 
585 class Parser;
586 void ParseChemistry(Parser &p);
587 
594 bool isSpecies( const string &chSpecies );
595 
602 bool isMolecule( const string &chSpecies );
603 
606 void test_isMolecule();
607 
612 void getMolecules( vector<string> &allMolecules );
613 
614 #endif /* MOLE_H_ */
Definition: warnings.h:11
double sink_rate_tot(const char chSpecies[]) const
Definition: mole_reactions.cpp:4169
double grain_area
Definition: mole.h:486
bool lgStancil
Definition: mole.h:366
nNucsMap nNuclide
Definition: mole.h:160
chem_element * el() const
Definition: mole.h:54
int compare(const chem_nuclide &b) const
Definition: mole.h:87
static void sort(MoleculeList::iterator start, MoleculeList::iterator end)
vector< double > reaction_rks
Definition: mole.h:499
int index
Definition: mole.h:432
Definition: mole.h:330
Definition: mole_priv.h:48
Definition: mole.h:19
molecule::nNucsMap::iterator nNucs_i
Definition: mole.h:262
molecule * null_mole
Definition: mole_species.cpp:50
void mole_create_react(void)
Definition: mole_reactions.cpp:1736
long old_zone
Definition: mole.h:501
int n_nuclei(void) const
Definition: mole.h:164
vector< bool > lgTreatIsotopes
Definition: mole.h:388
int num_total
Definition: mole.h:391
bool operator>(const chem_nuclide &a, const chem_nuclide &b)
Definition: mole.h:107
mole_state
Definition: mole.h:19
realnum total_molecules(void)
Definition: mole_species.cpp:1094
int num_calc
Definition: mole.h:391
shared_ptr< chem_nuclide > findnuclide(const char buf[])
Definition: mole_species.cpp:842
void mole_drive(void)
Definition: mole_drive.cpp:29
void make_species(void)
Definition: mole_species.cpp:112
void total_molecule_deut(realnum &total)
Definition: mole_species.cpp:1041
const molecule * global() const
Definition: mole.h:433
void ParseChemistry(Parser &p)
Definition: mole.cpp:132
Definition: mole.h:461
int parentIndex
Definition: mole.h:155
void test_isMolecule()
Definition: mole_species.cpp:1247
void zero()
Definition: mole.cpp:119
Definition: mole.h:23
void getMolecules(vector< string > &allMolecules)
Definition: mole_species.cpp:1262
bool lgH2Ozer
Definition: mole.h:351
bool isSpecies(const string &chSpecies)
Definition: mole_species.cpp:1197
const string label
Definition: mole.h:32
void mole_make_list(void)
Definition: mole_species.cpp:247
void comment(t_warnings &)
Definition: mole.h:466
double findrk(const char buf[]) const
Definition: mole_reactions.cpp:4133
bool operator==(const chem_nuclide &a, const chem_nuclide &b)
Definition: mole.h:119
sys_float sexp(sys_float x)
Definition: service.cpp:1203
map< int, shared_ptr< chem_nuclide > > isotopes
Definition: mole.h:33
TransitionList * lines
Definition: mole.h:447
realnum column
Definition: mole.h:451
bool lgProtElim
Definition: mole.h:376
molezone * findspecieslocal(const char buf[])
Definition: mole_species.cpp:821
double source_rate_tot(const char chSpecies[]) const
Definition: mole_reactions.cpp:4305
Definition: mole.h:407
bool lgGas_Phase
Definition: mole.h:163
ChemNuclideList nuclide_list
Definition: mole_species.cpp:56
realnum species_gasphase_density(const string &chSpecies)
Definition: mole_species.cpp:1124
Definition: mole.h:145
bool parse_species_label(const char label[], ChemNuclideList &atomsLeftToRight, vector< int > &numAtoms, string &embellishments)
Definition: mole_species.cpp:645
Definition: parser.h:41
int A
Definition: mole.h:48
int num_compacted
Definition: mole.h:391
realnum mass_amu
Definition: mole.h:51
char ** chSpecies
Definition: taulines.cpp:14
bool lgNonEquilChem
Definition: mole.h:371
molezone * findspecieslocal_validate(const char buf[])
Definition: mole_species.cpp:833
map< string, bool > offReactions
Definition: mole.h:396
realnum form_enthalpy
Definition: mole.h:209
double grain_density
Definition: mole.h:486
Definition: mole.h:19
vector< int > ipMl
Definition: mole.h:50
ChemElementList element_list
Definition: mole_species.cpp:55
bool isEnabled
Definition: mole.h:156
realnum xFracLim
Definition: mole.h:453
void mole_cmp_num_in_out_reactions(void)
Definition: mole_reactions.cpp:2324
map< const shared_ptr< chem_nuclide >, int, element_pointer_value_less > nNucsMap
Definition: mole.h:148
double chem_heat(void) const
Definition: mole_reactions.cpp:4344
void init(void)
Definition: mole.cpp:59
qList * levels
Definition: mole.h:446
molecule::nNucsMap::reverse_iterator nNucs_ri
Definition: mole.h:263
Definition: mole.h:19
vector< double > old_reaction_rks
Definition: mole.h:500
Definition: quantumstate.h:35
bool lgLeidenHack
Definition: mole.h:357
vector< shared_ptr< chem_element > > ChemElementList
Definition: mole.h:130
void zero()
Definition: mole.cpp:10
bool lgMeanAbundance(void) const
Definition: mole.h:58
string parentLabel
Definition: mole.h:150
int groupnum
Definition: mole.h:214
class molezone * local(void) const
Definition: mole.h:511
chem_nuclide * null_nuclide
Definition: mole_species.cpp:53
multi_arr< realnum, 3 > xMoleChTrRate
Definition: mole.h:495
molecule * findspecies(const char buf[])
Definition: mole_species.cpp:779
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)
Definition: mole_species.cpp:363
t_atoms atoms
Definition: atoms.cpp:5
molezone()
Definition: mole.h:409
~chem_element()
Definition: mole.h:29
float realnum
Definition: cddefines.h:127
valarray< class molezone > species
Definition: mole.h:497
enum mole_state state
Definition: mole.h:213
double sink_rate(const molecule *const sp, const mole_reaction &rate) const
Definition: mole_reactions.cpp:4199
void mole_rk_bigchange(void)
Definition: mole_reactions.cpp:3020
double snk
Definition: mole.h:443
int index
Definition: mole.h:214
realnum total_molecules_gasphase(void)
Definition: mole_species.cpp:1109
const int Z
Definition: mole.h:31
t_mole_global mole_global
Definition: mole.cpp:7
bool lgExcit
Definition: mole.h:162
species * dbase
Definition: mole.h:445
int n_react
Definition: mole.h:215
bool exists(const molecule *m)
Definition: mole.h:278
#define NULL
Definition: cddefines.h:115
bool operator<(const chem_nuclide &a, const chem_nuclide &b)
Definition: mole.h:103
int compare(const molecule &mol2) const
Definition: mole.h:231
vector< shared_ptr< chem_nuclide > > ChemNuclideList
Definition: mole.h:128
molecule::nNucsMap::const_reverse_iterator nNucs_cri
Definition: mole.h:264
void mole_update_sources(void)
Definition: mole_drive.cpp:55
chem_nuclide * heavyAtom(void)
Definition: mole.h:219
#define ASSERT(exp)
Definition: cddefines.h:637
void set_isotope_abundances(void)
Definition: mole_species.cpp:979
bool operator<=(const chem_nuclide &a, const chem_nuclide &b)
Definition: mole.h:111
bool isMolecule(const string &chSpecies)
Definition: mole_species.cpp:1222
bool lgNeutrals
Definition: mole.h:381
double findrate(const char buf[]) const
Definition: mole_reactions.cpp:4147
STATIC void start(long, realnum[], realnum[], long, long[], realnum *, int *)
const int LIMELM
Definition: cddefines.h:318
Definition: mole.h:41
int nElement(int nelem)
Definition: mole.h:174
bool isMonatomic(void) const
Definition: mole.h:190
int charge
Definition: mole.h:161
double den
Definition: mole.h:450
multi_arr< double, 2 > sink
Definition: mole.h:493
multi_arr< double, 2 > source
Definition: mole.h:493
bool lgGrain_mole_deplete
Definition: mole.h:385
bool lgDifferByExcitation(const molecule &mol1, const molecule &mol2)
Definition: mole_species.cpp:769
map< int, shared_ptr< chem_nuclide > >::iterator isotopes_i
Definition: mole.h:39
void mole_update_species_cache(void)
Definition: mole_species.cpp:856
Definition: transition.h:297
bool operator>=(const chem_nuclide &a, const chem_nuclide &b)
Definition: mole.h:115
Definition: mole.h:134
static const double SMALLABUND
Definition: mole.h:17
const char * chName() const
Definition: mole.h:467
void alloc()
Definition: mole.cpp:98
string label
Definition: mole.h:159
const char * chName() const
Definition: mole.h:333
void zero(void)
Definition: mole.h:421
double elec
Definition: mole.h:489
realnum mole_mass
Definition: mole.h:210
bool operator!=(const chem_nuclide &a, const chem_nuclide &b)
Definition: mole.h:123
MoleculeList list
Definition: mole.h:394
double dissoc_rate(const char chSpecies[]) const
Definition: mole_reactions.cpp:4228
void comment(t_warnings &)
Definition: mole.h:338
bool isIsotopicTotalSpecies() const
Definition: mole.h:151
mole_reaction * mole_findrate_s(const char buf[])
Definition: mole_reactions.cpp:4119
map< string, Properties > speciesProperties
Definition: mole.h:397
double frac
Definition: mole.h:52
bool isMolecule() const
Definition: mole.h:202
double pow(double x, int i)
Definition: cddefines.h:782
string label(void) const
Definition: mole.h:70
Definition: parser.h:920
double hmrate4(double a, double b, double c, double te)
Definition: mole.h:573
chem_nuclide * atomLim
Definition: mole.h:452
bool lgNoHeavyMole
Definition: mole.h:348
bool lgFederman
Definition: mole.h:364
t_mole_local mole
Definition: mole.cpp:8
void set_ion_locations()
Definition: mole_species.cpp:1011
bool lgNoMole
Definition: mole.h:345
vector< shared_ptr< molecule > > MoleculeList
Definition: mole.h:393
void mole_make_groups(void)
Definition: mole_species.cpp:1138
realnum column_old
Definition: mole.h:456
Definition: species.h:11
double src
Definition: mole.h:443
unsigned long index
Definition: mole.h:49
void total_molecule_elems(realnum total[LIMELM])
Definition: mole_species.cpp:1070
Definition: module.h:26
molecule * findspecies_validate(const char buf[])
Definition: mole_species.cpp:798
bool lgHasLinkedIon(void) const
Definition: mole.h:64
molezone * null_molezone
Definition: mole_species.cpp:51
double * location
Definition: mole.h:440
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)
Definition: mole_species.cpp:338
bool operator()(const shared_ptr< chem_nuclide > &a, const shared_ptr< chem_nuclide > &b) const
Definition: mole.h:137
chem_element * wel
Definition: mole.h:47
void init(void)
Definition: mole.h:413
double grain_saturation
Definition: mole.h:486