fourdst::libcomposition v2.3.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 <format>
5#include <string_view>
6#include <string>
7#include <optional>
8#include <limits>
9#include <stdexcept>
10
15namespace fourdst::atomic {
21 inline double convert_jpi_to_double(const std::string& jpi_string) noexcept;
22
52 struct Species {
53 std::string m_name;
54 std::string m_el;
55 int m_nz;
56 int m_n;
57 int m_z;
58 int m_a;
60 std::string m_betaCode;
62 double m_halfLife_s;
63 std::string m_spinParity;
64 std::string m_decayModes;
65 double m_atomicMass;
67 mutable std::optional<double> m_spin = std::nullopt;
68
90 const std::string_view name,
91 const std::string_view el,
92 const int nz,
93 const int n,
94 const int z,
95 const int a,
96 const double bindingEnergy,
97 const std::string_view betaCode,
98 const double betaDecayEnergy,
99 const double halfLife_s,
100 const std::string_view spinParity,
101 const std::string_view decayModes,
102 const double atomicMass,
103 const double atomicMassUnc
104 ) :
105 m_name(name),
106 m_el(el),
107 m_nz(nz),
108 m_n(n),
109 m_z(z),
110 m_a(a),
114 m_halfLife_s(halfLife_s),
117 m_atomicMass(atomicMass),
118 m_atomicMassUnc(atomicMassUnc) {};
119
126 m_name = species.m_name;
127 m_el = species.m_el;
128 m_nz = species.m_nz;
129 m_n = species.m_n;
130 m_z = species.m_z;
131 m_a = species.m_a;
132 m_bindingEnergy = species.m_bindingEnergy;
133 m_betaCode = species.m_betaCode;
134 m_betaDecayEnergy = species.m_betaDecayEnergy;
135 m_halfLife_s = species.m_halfLife_s;
136 m_spinParity = species.m_spinParity;
137 m_decayModes = species.m_decayModes;
138 m_atomicMass = species.m_atomicMass;
139 m_atomicMassUnc = species.m_atomicMassUnc;
140 }
141
142
147 [[nodiscard]] double mass() const {
148 return m_atomicMass;
149 }
150
155 [[nodiscard]] double massUnc() const {
156 return m_atomicMassUnc;
157 }
158
163 [[nodiscard]] double halfLife() const {
164 return m_halfLife_s;
165 }
166
171 [[nodiscard]] std::string_view spinParity() const {
172 return m_spinParity;
173 }
174
179 [[nodiscard]] std::string_view decayModes() const {
180 return m_decayModes;
181 }
182
187 [[nodiscard]] double bindingEnergy() const {
188 return m_bindingEnergy;
189 }
190
195 [[nodiscard]] double betaDecayEnergy() const {
196 return m_betaDecayEnergy;
197 }
198
203 [[nodiscard]] std::string_view betaCode() const {
204 return m_betaCode;
205 }
206
211 [[nodiscard]] std::string_view name() const {
212 return m_name;
213 }
214
219 [[nodiscard]] std::string_view el() const {
220 return m_el;
221 }
222
227 [[nodiscard]] int nz() const {
228 return m_nz;
229 }
230
235 [[nodiscard]] int n() const {
236 return m_n;
237 }
238
243 [[nodiscard]] int z() const {
244 return m_z;
245 }
246
251 [[nodiscard]] int a() const {
252 return m_a;
253 }
254
259 [[nodiscard]] double spin() const {
260 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
262 }
263 return m_spin.value();
264 }
265
272 friend std::ostream& operator<<(std::ostream& os, const Species& species) {
273 os << species.m_name;
274 return os;
275 }
276
277 friend bool operator==(const Species& lhs, const Species& rhs);
278 friend bool operator!=(const Species& lhs, const Species& rhs);
279 friend std::partial_ordering 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
300 inline std::partial_ordering operator<=>(const Species &lhs, const Species &rhs) {
301 if (const auto cmp = lhs.m_atomicMass <=> rhs.m_atomicMass; cmp != 0) {
302 return cmp;
303 }
304
305 return lhs.m_name <=> rhs.m_name;
306 }
307
308
309
310
341 inline double convert_jpi_to_double(const std::string& jpi_string) noexcept {
342 std::string s = jpi_string;
343
344 if (s.empty()) {
345 return std::numeric_limits<double>::quiet_NaN();
346 }
347
348 std::erase_if(s, [](const char c) {
349 return c == '(' || c == ')' || c == '*' || c == '#';
350 });
351
352 if (s == "+" || s == "-") {
353 return 0.0;
354 }
355
356 if (const size_t comma_pos = s.find(','); comma_pos != std::string::npos) {
357 s = s.substr(0, comma_pos);
358 }
359
360 if (!s.empty() && (s.back() == '+' || s.back() == '-')) {
361 s.pop_back();
362 }
363
364 if (s.empty()) {
365 return std::numeric_limits<double>::quiet_NaN();
366 }
367
368 try {
369 if (size_t slash_pos = s.find('/'); slash_pos != std::string::npos) {
370 if (slash_pos == 0) {
371 s = "1" + s;
372 slash_pos = 1;
373 }
374 const std::string numerator_str = s.substr(0, slash_pos);
375 const std::string denominator_str = s.substr(slash_pos + 1);
376 if (denominator_str.empty()) {
377 return std::numeric_limits<double>::quiet_NaN();
378 }
379 const double numerator = std::stod(numerator_str);
380 const double denominator = std::stod(denominator_str);
381 if (denominator == 0.0) {
382 return std::numeric_limits<double>::quiet_NaN();
383 }
384 return numerator / denominator;
385 } else {
386 return std::stod(s);
387 }
388 } catch (const std::invalid_argument&) {
389 return std::numeric_limits<double>::quiet_NaN();
390 } catch (const std::out_of_range&) {
391 return std::numeric_limits<double>::quiet_NaN();
392 }
393 }
394
395}
396
420
421template<>
422struct std::hash<fourdst::atomic::Species> {
428 size_t operator()(const fourdst::atomic::Species& s) const noexcept {
429 return std::hash<std::string>()(s.m_name);
430 }
431};
Contains canonical information about atomic species and elements used by 4D-STAR.
bool operator==(const Species &lhs, const Species &rhs)
Equality operator for Species. Compares based on name.
bool operator!=(const Species &lhs, const Species &rhs)
Inequality operator for Species. Compares based on name.
static const std::unordered_map< std::string, const Species & > species
Map of species names to their corresponding Species objects.
Definition species.h:3579
std::partial_ordering operator<=>(const Species &lhs, const Species &rhs)
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.
Represents an atomic species (isotope) with its fundamental physical properties.
double spin() const
Gets the nuclear spin as a numeric value.
double m_betaDecayEnergy
Beta decay energy in keV.
int z() const
Gets the atomic number (number of protons).
std::string_view name() const
Gets the name of the species.
std::string_view el() const
Gets the element symbol of the species.
double halfLife() const
Gets the half-life of the 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.
double m_atomicMassUnc
Uncertainty in the atomic mass.
std::string m_name
Name of the species (e.g., "Fe56").
int m_nz
NZ identifier, typically 1000*Z + A.
std::string m_decayModes
Decay modes as a string.
std::string_view betaCode() const
Gets the beta decay code.
int m_a
Mass number (N + Z).
std::string m_el
Element symbol (e.g., "Fe").
friend bool operator==(const Species &lhs, const Species &rhs)
Equality operator for Species. Compares based on name.
double betaDecayEnergy() const
Gets the beta decay energy of the species.
Species(const Species &species)
Copy constructor for Species.
std::string m_spinParity
Spin and parity as a string (e.g., "1/2-").
int m_n
Number of neutrons.
int n() const
Gets the number of neutrons.
std::optional< double > m_spin
Nuclear spin as a double, derived from m_spinParity.
int nz() const
Gets the NZ identifier of the species.
std::string_view decayModes() const
Gets the decay modes as a string.
int m_z
Atomic number (number of protons).
double m_atomicMass
Atomic mass in atomic mass units (u).
std::string m_betaCode
Beta decay code.
double bindingEnergy() const
Gets the binding energy of the species.
double massUnc() const
Gets the uncertainty in the atomic mass.
double m_bindingEnergy
Binding energy in keV.
friend std::ostream & operator<<(std::ostream &os, const Species &species)
Overloads the stream insertion operator for easy printing of a Species object.
int a() const
Gets the mass number.
friend bool operator!=(const Species &lhs, const Species &rhs)
Inequality operator for Species. Compares based on name.
double mass() const
Gets the atomic mass of the species.
double m_halfLife_s
Half-life in seconds. A value of -1.0 typically indicates stability.
friend std::partial_ordering operator<=>(const Species &lhs, const Species &rhs)
std::string_view spinParity() const
Gets the spin and parity as a string.
size_t operator()(const fourdst::atomic::Species &s) const noexcept
Computes the hash for a Species object.