Cloudy
Spectral Synthesis Code for Astrophysics
Loading...
Searching...
No Matches
mole.h
Go to the documentation of this file.
1/* This file is part of Cloudy and is copyright (C)1978-2025 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
12class TransitionList;
13class qList;
14class species;
15class molezone;
16
17static const double SMALLABUND = 1e-24;
18
20
21class chem_nuclide;
22
24 explicit chem_element(); // Do not implement
25 chem_element &operator=(const chem_element&); // Do not implement
26public:
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
39typedef map<int, shared_ptr<chem_nuclide> >::iterator isotopes_i;
40
42public:
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
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};
103inline bool operator< (const chem_nuclide &a, const chem_nuclide &b)
104{
105 return a.compare(b) < 0;
106}
107inline bool operator> (const chem_nuclide &a, const chem_nuclide &b)
108{
109 return a.compare(b) > 0;
110}
111inline bool operator<= (const chem_nuclide &a, const chem_nuclide &b)
112{
113 return a.compare(b) <= 0;
114}
115inline bool operator>= (const chem_nuclide &a, const chem_nuclide &b)
116{
117 return a.compare(b) >= 0;
118}
119inline bool operator== (const chem_nuclide &a, const chem_nuclide &b)
120{
121 return a.compare(b) == 0;
122}
123inline bool operator!= (const chem_nuclide &a, const chem_nuclide &b)
124{
125 return !(a == b);
126}
127
128typedef vector< shared_ptr<chem_nuclide> > ChemNuclideList;
130typedef vector< shared_ptr<chem_element> > ChemElementList;
133
135{
136public:
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 */
145class molecule {
146public:
147 typedef map<const shared_ptr<chem_nuclide>, int,
149
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;
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
211
212 /* Parameters as computational object */
216
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 */
262typedef molecule::nNucsMap::iterator nNucs_i;
263typedef molecule::nNucsMap::reverse_iterator nNucs_ri;
264typedef molecule::nNucsMap::const_reverse_iterator nNucs_cri;
265
267extern void mole_drive(void);
268
270extern void mole_create_react(void);
271
272class mole_reaction;
273
274mole_reaction *mole_findrate_s(const char buf[]);
275
276extern molecule *null_mole;
277
278inline bool exists (const molecule* m)
279{
280 return m != null_mole;
281}
282
284extern molecule *findspecies(const char buf[]);
286extern molecule *findspecies_validate(const char buf[]);
287
289extern molezone *findspecieslocal(const char buf[]);
291extern molezone *findspecieslocal_validate(const char buf[]);
292
293extern shared_ptr<chem_nuclide> findnuclide(const char buf[]);
294
319
326
327class Properties;
328
329
330class t_mole_global : public module {
331
332public:
333 const char *chName() const
334 {
335 return "mole_global";
336 }
337 void zero();
340 void init(void);
341
342 void make_species(void);
343
346
349
352
358
365
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
407class molezone {
408public:
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;
441
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 */
454
455 /* Historical solution data */
457};
458
459extern molezone *null_molezone;
460
461class t_mole_local : public module
462{
463public:
464 void alloc();
465 void zero();
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;
502};
503
504extern t_mole_local mole;
505
506inline bool exists (const molezone* m)
507{
508 return m != null_molezone;
509}
510
511inline molezone *molecule::local(void) const
512{
513 if ( index != -1)
514 return &mole.species[index];
515 else
516 return null_molezone;
517}
518
519extern void total_molecule_elems(realnum total[LIMELM]);
520extern void total_molecule_deut(realnum &total);
521
522extern realnum total_molecules(void);
523
525
532
533extern void mole_make_list(void);
534extern void mole_make_groups(void);
535
537
538bool lgDifferByExcitation( const molecule &mol1, const molecule &mol2 );
539
540extern void mole_update_species_cache(void);
541
542void mole_update_sources(void);
543
544void 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
563bool parse_species_label( const char label[], ChemNuclideList &atomsLeftToRight, vector<int> &numAtoms, string &embellishments );
564bool 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/* hmrate4 is clone of hmrate but takes a, b, c and te as explicit arguments */
574inline double hmrate4( double a, double b, double c, double te )
575{
576 /* UMIST rates are simple temperaturr power laws that
577 * can become large at the high temperatures Cloudy
578 * may encounter. Do not extrapolate rates above T>2.5e3K
579 * THIS CODE MUST BE KEPT PARALLEL WITH HMRATE IN MOLE_REACTIONS.CPP */
580 if (b >0.)
581 te = min(te, 2500.);
582 if( b == 0. && c == 0. )
583 return a;
584 else if( c == 0. )
585 return a*pow(te/300.,b);
586 else if( b == 0. )
587 return a*sexp(c/te);
588 else
589 return a*pow(te/300.,b)*sexp(c/te);
590}
591
592class Parser;
593void ParseChemistry(Parser &p);
594
601bool isSpecies( const string &chSpecies );
602
609bool isMolecule( const string &chSpecies );
610
613void test_isMolecule();
614
619void getMolecules( vector<string> &allMolecules );
620
621#endif /* MOLE_H_ */
t_atoms atoms
Definition atoms.cpp:5
#define NULL
Definition cddefines.h:115
#define ASSERT(exp)
Definition cddefines.h:637
sys_float sexp(sys_float x)
Definition service.cpp:1203
const int LIMELM
Definition cddefines.h:318
float realnum
Definition cddefines.h:127
double pow(double x, int i)
Definition cddefines.h:782
long min(int a, long b)
Definition cddefines.h:766
Definition parser.h:43
Definition parser.h:930
Definition transition.h:288
Definition mole.h:23
chem_element(int Z, const char *label)
Definition mole.h:27
~chem_element()
Definition mole.h:29
map< int, shared_ptr< chem_nuclide > > isotopes
Definition mole.h:33
const int Z
Definition mole.h:31
chem_element & operator=(const chem_element &)
const string label
Definition mole.h:32
Definition mole.h:41
double frac
Definition mole.h:52
int A
Definition mole.h:48
vector< int > ipMl
Definition mole.h:50
string label(void) const
Definition mole.h:70
chem_element * wel
Definition mole.h:47
realnum mass_amu
Definition mole.h:51
bool lgMeanAbundance(void) const
Definition mole.h:58
chem_element * el() const
Definition mole.h:54
int compare(const chem_nuclide &b) const
Definition mole.h:87
unsigned long index
Definition mole.h:49
bool lgHasLinkedIon(void) const
Definition mole.h:64
Definition mole.h:135
bool operator()(const shared_ptr< chem_nuclide > &a, const shared_ptr< chem_nuclide > &b) const
Definition mole.h:137
module()
Definition module.h:29
Definition mole_priv.h:48
Definition mole.h:145
int parentIndex
Definition mole.h:155
nNucsMap nNuclide
Definition mole.h:160
realnum form_enthalpy
Definition mole.h:209
bool lgGas_Phase
Definition mole.h:163
int charge
Definition mole.h:161
bool isEnabled
Definition mole.h:156
string parentLabel
Definition mole.h:150
chem_nuclide * heavyAtom(void)
Definition mole.h:219
bool lgExcit
Definition mole.h:162
realnum mole_mass
Definition mole.h:210
bool isIsotopicTotalSpecies() const
Definition mole.h:151
int nElement(int nelem)
Definition mole.h:174
string label
Definition mole.h:159
int compare(const molecule &mol2) const
Definition mole.h:231
map< const shared_ptr< chem_nuclide >, int, element_pointer_value_less > nNucsMap
Definition mole.h:148
class molezone * local(void) const
Definition mole.h:511
enum mole_state state
Definition mole.h:213
int groupnum
Definition mole.h:214
bool isMolecule() const
Definition mole.h:202
int n_react
Definition mole.h:215
int n_nuclei(void) const
Definition mole.h:164
bool isMonatomic(void) const
Definition mole.h:190
int index
Definition mole.h:214
Definition mole.h:407
int index
Definition mole.h:432
qList * levels
Definition mole.h:446
const molecule * global() const
Definition mole.h:433
realnum column
Definition mole.h:451
double snk
Definition mole.h:443
double * location
Definition mole.h:440
void init(void)
Definition mole.h:413
double den
Definition mole.h:450
double src
Definition mole.h:443
void zero(void)
Definition mole.h:421
chem_nuclide * atomLim
Definition mole.h:452
realnum xFracLim
Definition mole.h:453
realnum column_old
Definition mole.h:456
species * dbase
Definition mole.h:445
TransitionList * lines
Definition mole.h:447
molezone()
Definition mole.h:409
Definition quantumstate.h:36
Definition species.h:12
Definition mole.h:330
void comment(t_warnings &)
Definition mole.h:338
bool lgLeidenHack
Definition mole.h:357
bool lgNoHeavyMole
Definition mole.h:348
bool lgH2Ozer
Definition mole.h:351
map< string, bool > offReactions
Definition mole.h:396
bool lgFederman
Definition mole.h:364
bool lgNeutrals
Definition mole.h:381
void make_species(void)
Definition mole_species.cpp:112
int num_compacted
Definition mole.h:391
vector< shared_ptr< molecule > > MoleculeList
Definition mole.h:393
const char * chName() const
Definition mole.h:333
bool lgStancil
Definition mole.h:366
void zero()
Definition mole.cpp:10
MoleculeList list
Definition mole.h:394
int num_calc
Definition mole.h:391
bool lgNoMole
Definition mole.h:345
map< string, Properties > speciesProperties
Definition mole.h:397
bool lgNonEquilChem
Definition mole.h:371
void init(void)
Definition mole.cpp:59
static void sort(MoleculeList::iterator start, MoleculeList::iterator end)
vector< bool > lgTreatIsotopes
Definition mole.h:388
int num_total
Definition mole.h:391
bool lgGrain_mole_deplete
Definition mole.h:385
bool lgProtElim
Definition mole.h:376
Definition mole.h:462
void zero()
Definition mole.cpp:119
double dissoc_rate(const char chSpecies[]) const
Definition mole_reactions.cpp:4248
multi_arr< realnum, 3 > xMoleChTrRate
Definition mole.h:495
vector< double > reaction_rks
Definition mole.h:499
double sink_rate(const molecule *const sp, const mole_reaction &rate) const
Definition mole_reactions.cpp:4219
double source_rate_tot(const char chSpecies[]) const
Definition mole_reactions.cpp:4325
double grain_density
Definition mole.h:486
double findrate(const char buf[]) const
Definition mole_reactions.cpp:4167
double findrk(const char buf[]) const
Definition mole_reactions.cpp:4153
vector< double > old_reaction_rks
Definition mole.h:500
void set_ion_locations()
Definition mole_species.cpp:1011
double grain_saturation
Definition mole.h:486
double grain_area
Definition mole.h:486
void alloc()
Definition mole.cpp:98
double chem_heat(void) const
Definition mole_reactions.cpp:4364
long old_zone
Definition mole.h:501
void set_isotope_abundances(void)
Definition mole_species.cpp:979
double elec
Definition mole.h:489
void comment(t_warnings &)
Definition mole.h:466
valarray< class molezone > species
Definition mole.h:497
double sink_rate_tot(const char chSpecies[]) const
Definition mole_reactions.cpp:4189
multi_arr< double, 2 > sink
Definition mole.h:493
const char * chName() const
Definition mole.h:467
multi_arr< double, 2 > source
Definition mole.h:493
Definition warnings.h:11
t_mole_global mole_global
Definition mole.cpp:7
t_mole_local mole
Definition mole.cpp:8
void mole_create_react(void)
Definition mole_reactions.cpp:1748
shared_ptr< chem_nuclide > findnuclide(const char buf[])
Definition mole_species.cpp:842
bool operator<(const chem_nuclide &a, const chem_nuclide &b)
Definition mole.h:103
realnum total_molecules_gasphase(void)
Definition mole_species.cpp:1109
void test_isMolecule()
Definition mole_species.cpp:1247
bool exists(const molecule *m)
Definition mole.h:278
void getMolecules(vector< string > &allMolecules)
Definition mole_species.cpp:1262
vector< shared_ptr< chem_element > > ChemElementList
Definition mole.h:130
molecule::nNucsMap::iterator nNucs_i
Definition mole.h:262
t_mole_global mole_global
Definition mole.cpp:7
void mole_make_groups(void)
Definition mole_species.cpp:1138
realnum total_molecules(void)
Definition mole_species.cpp:1094
molecule * findspecies_validate(const char buf[])
Definition mole_species.cpp:798
molezone * findspecieslocal(const char buf[])
Definition mole_species.cpp:821
bool operator!=(const chem_nuclide &a, const chem_nuclide &b)
Definition mole.h:123
void mole_cmp_num_in_out_reactions(void)
Definition mole_reactions.cpp:2337
bool operator>(const chem_nuclide &a, const chem_nuclide &b)
Definition mole.h:107
void mole_rk_bigchange(void)
Definition mole_reactions.cpp:3040
bool isMolecule(const string &chSpecies)
Definition mole_species.cpp:1222
molecule::nNucsMap::reverse_iterator nNucs_ri
Definition mole.h:263
void mole_drive(void)
Definition mole_drive.cpp:29
void create_isotopologues(ChemNuclideList &atoms, vector< int > &numAtoms, string atom_old, string atom_new, string embellishments, vector< string > &newLabels)
Definition mole_species.cpp:338
void ParseChemistry(Parser &p)
Definition mole.cpp:132
molecule::nNucsMap::const_reverse_iterator nNucs_cri
Definition mole.h:264
realnum species_gasphase_density(const string &chSpecies)
Definition mole_species.cpp:1124
ChemElementList element_list
Definition mole_species.cpp:55
mole_reaction * mole_findrate_s(const char buf[])
Definition mole_reactions.cpp:4139
chem_nuclide * null_nuclide
Definition mole_species.cpp:53
molezone * findspecieslocal_validate(const char buf[])
Definition mole_species.cpp:833
void mole_update_sources(void)
Definition mole_drive.cpp:55
mole_state
Definition mole.h:19
@ MOLE_NULL
Definition mole.h:19
@ MOLE_ACTIVE
Definition mole.h:19
@ MOLE_PASSIVE
Definition mole.h:19
t_mole_local mole
Definition mole.cpp:8
void mole_update_species_cache(void)
Definition mole_species.cpp:856
void mole_make_list(void)
Definition mole_species.cpp:247
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
double hmrate4(double a, double b, double c, double te)
Definition mole.h:574
map< int, shared_ptr< chem_nuclide > >::iterator isotopes_i
Definition mole.h:39
bool lgDifferByExcitation(const molecule &mol1, const molecule &mol2)
Definition mole_species.cpp:769
bool operator<=(const chem_nuclide &a, const chem_nuclide &b)
Definition mole.h:111
bool operator>=(const chem_nuclide &a, const chem_nuclide &b)
Definition mole.h:115
ChemNuclideList nuclide_list
Definition mole_species.cpp:56
molecule * findspecies(const char buf[])
Definition mole_species.cpp:779
molezone * null_molezone
Definition mole_species.cpp:51
vector< shared_ptr< chem_nuclide > > ChemNuclideList
Definition mole.h:128
bool operator==(const chem_nuclide &a, const chem_nuclide &b)
Definition mole.h:119
bool isSpecies(const string &chSpecies)
Definition mole_species.cpp:1197
void total_molecule_elems(realnum total[LIMELM])
Definition mole_species.cpp:1070
bool parse_species_label(const char label[], ChemNuclideList &atomsLeftToRight, vector< int > &numAtoms, string &embellishments)
Definition mole_species.cpp:645
void total_molecule_deut(realnum &total)
Definition mole_species.cpp:1041
molecule * null_mole
Definition mole_species.cpp:50
#define SMALLABUND
Definition mole_solve.cpp:44
STATIC void start(long, realnum[], realnum[], long, long[], realnum *, int *)
char ** chSpecies
Definition taulines.cpp:14