fourdst::libcomposition v2.0.1 Robust atomic species information library

Loading...
Searching...
No Matches
atomicSpecies.h
Go to the documentation of this file.
1#pragma once
2
3
4#include <string_view>
5#include <string>
6#include <optional>
7#include <limits>
8
9
14 namespace fourdst::atomic {
20 inline double convert_jpi_to_double(const std::string& jpi_string) noexcept;
21
51 struct Species {
52 std::string m_name;
53 std::string m_el;
54 int m_nz;
55 int m_n;
56 int m_z;
57 int m_a;
58 double m_bindingEnergy;
59 std::string m_betaCode;
60 double m_betaDecayEnergy;
61 double m_halfLife_s;
62 std::string m_spinParity;
63 std::string m_decayModes;
64 double m_atomicMass;
65 double m_atomicMassUnc;
66 mutable std::optional<double> m_spin = std::nullopt;
67
88 Species(
89 const std::string_view name,
90 const std::string_view el,
91 const int nz,
92 const int n,
93 const int z,
94 const int a,
95 const double bindingEnergy,
96 const std::string_view betaCode,
97 const double betaDecayEnergy,
98 const double halfLife_s,
99 const std::string_view spinParity,
100 const std::string_view decayModes,
101 const double atomicMass,
102 const double atomicMassUnc
103 ) :
104 m_name(name),
105 m_el(el),
106 m_nz(nz),
107 m_n(n),
108 m_z(z),
109 m_a(a),
110 m_bindingEnergy(bindingEnergy),
111 m_betaCode(betaCode),
112 m_betaDecayEnergy(betaDecayEnergy),
113 m_halfLife_s(halfLife_s),
114 m_spinParity(spinParity),
115 m_decayModes(decayModes),
116 m_atomicMass(atomicMass),
117 m_atomicMassUnc(atomicMassUnc) {};
118
124 Species(const Species& species) {
125 m_name = species.m_name;
126 m_el = species.m_el;
127 m_nz = species.m_nz;
128 m_n = species.m_n;
129 m_z = species.m_z;
130 m_a = species.m_a;
131 m_bindingEnergy = species.m_bindingEnergy;
132 m_betaCode = species.m_betaCode;
133 m_betaDecayEnergy = species.m_betaDecayEnergy;
134 m_halfLife_s = species.m_halfLife_s;
135 m_spinParity = species.m_spinParity;
136 m_decayModes = species.m_decayModes;
137 m_atomicMass = species.m_atomicMass;
138 m_atomicMassUnc = species.m_atomicMassUnc;
139 }
140
141
146 [[nodiscard]] double mass() const {
147 return m_atomicMass;
148 }
149
154 [[nodiscard]] double massUnc() const {
155 return m_atomicMassUnc;
156 }
157
162 [[nodiscard]] double halfLife() const {
163 return m_halfLife_s;
164 }
165
170 [[nodiscard]] std::string_view spinParity() const {
171 return m_spinParity;
172 }
173
178 [[nodiscard]] std::string_view decayModes() const {
179 return m_decayModes;
180 }
181
186 [[nodiscard]] double bindingEnergy() const {
187 return m_bindingEnergy;
188 }
189
194 [[nodiscard]] double betaDecayEnergy() const {
195 return m_betaDecayEnergy;
196 }
197
202 [[nodiscard]] std::string_view betaCode() const {
203 return m_betaCode;
204 }
205
210 [[nodiscard]] std::string_view name() const {
211 return m_name;
212 }
213
218 [[nodiscard]] std::string_view el() const {
219 return m_el;
220 }
221
226 [[nodiscard]] int nz() const {
227 return m_nz;
228 }
229
234 [[nodiscard]] int n() const {
235 return m_n;
236 }
237
242 [[nodiscard]] int z() const {
243 return m_z;
244 }
245
250 [[nodiscard]] int a() const {
251 return m_a;
252 }
253
258 [[nodiscard]] double spin() const {
259 if (!m_spin.has_value()) { // The spin calculation is very expensive, and we almost never need it so we only compute it the first time it is requested
260 m_spin = convert_jpi_to_double(m_spinParity);
261 }
262 return m_spin.value();
263 }
264
271 friend std::ostream& operator<<(std::ostream& os, const Species& species) {
272 os << species.m_name;
273 return os;
274 }
275
276 friend bool operator==(const Species& lhs, const Species& rhs);
277 friend bool operator!=(const Species& lhs, const Species& rhs);
278 friend bool operator<(const Species& lhs, const Species& rhs);
279 friend bool operator>(const Species& lhs, const Species& rhs);
280 };
281
287 inline bool operator==(const Species& lhs, const Species& rhs) {
288 return (lhs.m_name == rhs.m_name);
289 }
290
296 inline bool operator!=(const Species& lhs, const Species& rhs) {
297 return (lhs.m_name != rhs.m_name);
298 }
299
305 inline bool operator<(const Species& lhs, const Species& rhs) {
306 return (lhs.m_atomicMass < rhs.m_atomicMass);
307 }
308
314 inline bool operator>(const Species& lhs, const Species& rhs) {
315 return (lhs.m_atomicMass > rhs.m_atomicMass);
316 }
317
348 inline double convert_jpi_to_double(const std::string& jpi_string) noexcept {
349 std::string s = jpi_string;
350
351 if (s.empty()) {
352 return std::numeric_limits<double>::quiet_NaN();
353 }
354
355 std::erase_if(s, [](const char c) {
356 return c == '(' || c == ')' || c == '*' || c == '#';
357 });
358
359 if (s == "+" || s == "-") {
360 return 0.0;
361 }
362
363 if (const size_t comma_pos = s.find(','); comma_pos != std::string::npos) {
364 s = s.substr(0, comma_pos);
365 }
366
367 if (!s.empty() && (s.back() == '+' || s.back() == '-')) {
368 s.pop_back();
369 }
370
371 if (s.empty()) {
372 return std::numeric_limits<double>::quiet_NaN();
373 }
374
375 try {
376 if (size_t slash_pos = s.find('/'); slash_pos != std::string::npos) {
377 if (slash_pos == 0) {
378 s = "1" + s;
379 slash_pos = 1;
380 }
381 const std::string numerator_str = s.substr(0, slash_pos);
382 const std::string denominator_str = s.substr(slash_pos + 1);
383 if (denominator_str.empty()) {
384 return std::numeric_limits<double>::quiet_NaN();
385 }
386 const double numerator = std::stod(numerator_str);
387 const double denominator = std::stod(denominator_str);
388 if (denominator == 0.0) {
389 return std::numeric_limits<double>::quiet_NaN();
390 }
391 return numerator / denominator;
392 } else {
393 return std::stod(s);
394 }
395 } catch (const std::invalid_argument&) {
396 return std::numeric_limits<double>::quiet_NaN();
397 } catch (const std::out_of_range&) {
398 return std::numeric_limits<double>::quiet_NaN();
399 }
400 }
401
402}
403
427 template<>
428 struct std::hash<fourdst::atomic::Species> {
434 size_t operator()(const fourdst::atomic::Species& s) const noexcept {
435 return std::hash<std::string>()(s.m_name);
436 }
437}; // namespace std
fourdst::atomic
Contains canonical information about atomic species and elements used by 4D-STAR.
fourdst::atomic::operator==
bool operator==(const Species &lhs, const Species &rhs)
Equality operator for Species. Compares based on name.
Definition atomicSpecies.h:287
fourdst::atomic::operator>
bool operator>(const Species &lhs, const Species &rhs)
Greater-than operator for Species. Compares based on atomic mass.
Definition atomicSpecies.h:314
fourdst::atomic::operator!=
bool operator!=(const Species &lhs, const Species &rhs)
Inequality operator for Species. Compares based on name.
Definition atomicSpecies.h:296
fourdst::atomic::operator<
bool operator<(const Species &lhs, const Species &rhs)
Less-than operator for Species. Compares based on atomic mass.
Definition atomicSpecies.h:305
fourdst::atomic::species
static const std::unordered_map< std::string, const Species & > species
Map of species names to their corresponding Species objects.
Definition species.h:3579
fourdst::atomic::convert_jpi_to_double
double convert_jpi_to_double(const std::string &jpi_string) noexcept
Converts a spin-parity string (JPI string) to a double-precision floating-point number.
Definition atomicSpecies.h:348
fourdst
fourdst::atomic::Species
Represents an atomic species (isotope) with its fundamental physical properties.
Definition atomicSpecies.h:51
fourdst::atomic::Species::spin
double spin() const
Gets the nuclear spin as a numeric value.
Definition atomicSpecies.h:258
fourdst::atomic::Species::m_betaDecayEnergy
double m_betaDecayEnergy
Beta decay energy in keV.
Definition atomicSpecies.h:60
fourdst::atomic::Species::z
int z() const
Gets the atomic number (number of protons).
Definition atomicSpecies.h:242
fourdst::atomic::Species::name
std::string_view name() const
Gets the name of the species.
Definition atomicSpecies.h:210
fourdst::atomic::Species::el
std::string_view el() const
Gets the element symbol of the species.
Definition atomicSpecies.h:218
fourdst::atomic::Species::halfLife
double halfLife() const
Gets the half-life of the species.
Definition atomicSpecies.h:162
fourdst::atomic::Species::Species
Species(const std::string_view name, const std::string_view el, const int nz, const int n, const int z, const int a, const double bindingEnergy, const std::string_view betaCode, const double betaDecayEnergy, const double halfLife_s, const std::string_view spinParity, const std::string_view decayModes, const double atomicMass, const double atomicMassUnc)
Constructs a Species object with detailed properties.
Definition atomicSpecies.h:88
fourdst::atomic::Species::m_atomicMassUnc
double m_atomicMassUnc
Uncertainty in the atomic mass.
Definition atomicSpecies.h:65
fourdst::atomic::Species::m_name
std::string m_name
Name of the species (e.g., "Fe56").
Definition atomicSpecies.h:52
fourdst::atomic::Species::m_nz
int m_nz
NZ identifier, typically 1000*Z + A.
Definition atomicSpecies.h:54
fourdst::atomic::Species::m_decayModes
std::string m_decayModes
Decay modes as a string.
Definition atomicSpecies.h:63
fourdst::atomic::Species::betaCode
std::string_view betaCode() const
Gets the beta decay code.
Definition atomicSpecies.h:202
fourdst::atomic::Species::m_a
int m_a
Mass number (N + Z).
Definition atomicSpecies.h:57
fourdst::atomic::Species::m_el
std::string m_el
Element symbol (e.g., "Fe").
Definition atomicSpecies.h:53
fourdst::atomic::Species::operator==
friend bool operator==(const Species &lhs, const Species &rhs)
Equality operator for Species. Compares based on name.
Definition atomicSpecies.h:287
fourdst::atomic::Species::betaDecayEnergy
double betaDecayEnergy() const
Gets the beta decay energy of the species.
Definition atomicSpecies.h:194
fourdst::atomic::Species::Species
Species(const Species &species)
Copy constructor for Species.
Definition atomicSpecies.h:124
fourdst::atomic::Species::m_spinParity
std::string m_spinParity
Spin and parity as a string (e.g., "1/2-").
Definition atomicSpecies.h:62
fourdst::atomic::Species::m_n
int m_n
Number of neutrons.
Definition atomicSpecies.h:55
fourdst::atomic::Species::n
int n() const
Gets the number of neutrons.
Definition atomicSpecies.h:234
fourdst::atomic::Species::m_spin
std::optional< double > m_spin
Nuclear spin as a double, derived from m_spinParity.
Definition atomicSpecies.h:66
fourdst::atomic::Species::nz
int nz() const
Gets the NZ identifier of the species.
Definition atomicSpecies.h:226
fourdst::atomic::Species::decayModes
std::string_view decayModes() const
Gets the decay modes as a string.
Definition atomicSpecies.h:178
fourdst::atomic::Species::m_z
int m_z
Atomic number (number of protons).
Definition atomicSpecies.h:56
fourdst::atomic::Species::m_atomicMass
double m_atomicMass
Atomic mass in atomic mass units (u).
Definition atomicSpecies.h:64
fourdst::atomic::Species::operator>
friend bool operator>(const Species &lhs, const Species &rhs)
Greater-than operator for Species. Compares based on atomic mass.
Definition atomicSpecies.h:314
fourdst::atomic::Species::m_betaCode
std::string m_betaCode
Beta decay code.
Definition atomicSpecies.h:59
fourdst::atomic::Species::bindingEnergy
double bindingEnergy() const
Gets the binding energy of the species.
Definition atomicSpecies.h:186
fourdst::atomic::Species::massUnc
double massUnc() const
Gets the uncertainty in the atomic mass.
Definition atomicSpecies.h:154
fourdst::atomic::Species::m_bindingEnergy
double m_bindingEnergy
Binding energy in keV.
Definition atomicSpecies.h:58
fourdst::atomic::Species::operator<<
friend std::ostream & operator<<(std::ostream &os, const Species &species)
Overloads the stream insertion operator for easy printing of a Species object.
Definition atomicSpecies.h:271
fourdst::atomic::Species::a
int a() const
Gets the mass number.
Definition atomicSpecies.h:250
fourdst::atomic::Species::operator!=
friend bool operator!=(const Species &lhs, const Species &rhs)
Inequality operator for Species. Compares based on name.
Definition atomicSpecies.h:296
fourdst::atomic::Species::mass
double mass() const
Gets the atomic mass of the species.
Definition atomicSpecies.h:146
fourdst::atomic::Species::m_halfLife_s
double m_halfLife_s
Half-life in seconds. A value of -1.0 typically indicates stability.
Definition atomicSpecies.h:61
fourdst::atomic::Species::operator<
friend bool operator<(const Species &lhs, const Species &rhs)
Less-than operator for Species. Compares based on atomic mass.
Definition atomicSpecies.h:305
fourdst::atomic::Species::spinParity
std::string_view spinParity() const
Gets the spin and parity as a string.
Definition atomicSpecies.h:170
std::hash< fourdst::atomic::Species >::operator()
size_t operator()(const fourdst::atomic::Species &s) const noexcept
Computes the hash for a Species object.
Definition atomicSpecies.h:434