21#include "quill/LogMacros.h"
24#include <unordered_map>
41 template<
typename A,
typename B>
42 std::vector<A> sortVectorBy(
43 std::vector<A> toSort,
44 const std::vector<B>& by
46 std::vector<std::size_t> indices(by.size());
47 for (
size_t i = 0; i < indices.size(); i++) {
51 std::ranges::sort(indices, [&](
size_t a,
size_t b) {
55 std::vector<A> sorted;
56 sorted.reserve(indices.size());
58 for (
const auto idx: indices) {
59 sorted.push_back(toSort[idx]);
65 std::optional<fourdst::atomic::Species> getSpecies(
const std::string& symbol) {
72 void throw_unknown_symbol(quill::Logger* logger,
const std::string& symbol) {
73 LOG_ERROR(logger,
"Symbol {} is not a valid species symbol (not in the species database)", symbol);
77 void throw_unregistered_symbol(quill::Logger* logger,
const std::string& symbol) {
78 LOG_ERROR(logger,
"Symbol {} is not registered in the composition.", symbol);
85 const std::vector<std::string>& symbols
87 for (
const auto& symbol : symbols) {
93 const std::set<std::string>& symbols
95 for (
const auto& symbol : symbols) {
101 const std::vector<atomic::Species> &species
103 for (
const auto& s : species) {
109 const std::set<atomic::Species> &species
111 for (
const auto& s : species) {
117 const std::vector<std::string>& symbols,
118 const std::vector<double>& molarAbundances
120 if (symbols.size() != molarAbundances.size()) {
121 LOG_CRITICAL(
getLogger(),
"The number of symbols and molarAbundances must be equal (got {} symbols and {} molarAbundances).", symbols.size(), molarAbundances.size());
122 throw exceptions::InvalidCompositionError(
"The number of symbols and fractions must be equal. Got " + std::to_string(symbols.size()) +
" symbols and " + std::to_string(molarAbundances.size()) +
" fractions.");
125 for (
const auto &[symbol, y] : std::views::zip(symbols, molarAbundances)) {
132 const std::vector<atomic::Species> &species,
133 const std::vector<double> &molarAbundances
135 if (species.size() != molarAbundances.size()) {
136 LOG_CRITICAL(
getLogger(),
"The number of species and molarAbundances must be equal (got {} species and {} molarAbundances).", species.size(), molarAbundances.size());
137 throw exceptions::InvalidCompositionError(
"The number of species and fractions must be equal. Got " + std::to_string(species.size()) +
" species and " + std::to_string(molarAbundances.size()) +
" fractions.");
140 for (
const auto& [s, y] : std::views::zip(species, molarAbundances)) {
147 const std::set<std::string> &symbols,
148 const std::vector<double> &molarAbundances
150 if (symbols.size() != molarAbundances.size()) {
151 LOG_CRITICAL(
getLogger(),
"The number of symbols and molarAbundances must be equal (got {} symbols and {} molarAbundances).", symbols.size(), molarAbundances.size());
152 throw exceptions::InvalidCompositionError(
"The number of symbols and fractions must be equal. Got " + std::to_string(symbols.size()) +
" symbols and " + std::to_string(molarAbundances.size()) +
" fractions.");
155 for (
const auto& [symbol, y] : std::views::zip(sortVectorBy<std::string>(std::vector<std::string>(symbols.begin(), symbols.end()), molarAbundances), molarAbundances)) {
171 if (
this != &other) {
179 const std::string& symbol
181 const auto result = getSpecies(symbol);
183 throw_unknown_symbol(
getLogger(), symbol);
190 const std::vector<std::string>& symbols
192 for (
const auto& symbol : symbols) {
207 const std::vector<atomic::Species> &species
209 for (
const auto& s : species) {
215 std::set<std::string> symbols;
217 symbols.insert(std::string(species.name()));
228 const auto species = getSpecies(symbol);
230 throw_unknown_symbol(
getLogger(), symbol);
239 throw_unregistered_symbol(
getLogger(), std::string(species.
name()));
241 std::map<atomic::Species, double> raw_mass;
242 double totalMass = 0;
244 const double contrib = y * sp.mass();
245 totalMass += contrib;
246 raw_mass.emplace(sp, contrib);
248 return raw_mass.at(species) / totalMass;
252 std::unordered_map<atomic::Species, double> mass_fractions;
256 return mass_fractions;
261 const std::string& symbol
263 const auto species = getSpecies(symbol);
265 throw_unknown_symbol(
getLogger(), symbol);
274 throw_unregistered_symbol(
getLogger(), std::string(species.
name()));
276 double total_moles_per_gram = 0.0;
278 total_moles_per_gram += y;
284 std::unordered_map<atomic::Species, double> number_fractions;
288 return number_fractions;
292 const std::string &symbol
294 const auto species = getSpecies(symbol);
296 throw_unknown_symbol(
getLogger(), symbol);
306 throw_unregistered_symbol(
getLogger(), std::string(species.
name()));
315 sum += x/species.mass();
324 Ye += species.z() * y;
334 if (
m_cache.canonicalComp.has_value()) {
335 return m_cache.canonicalComp.value();
341 for (
const auto& symbol : canonicalH) {
346 for (
const auto& symbol : canonicalHe) {
353 const bool isHIsotope = canonicalH.contains(
species);
354 const bool isHeIsotope = canonicalHe.contains(
species);
356 if (isHIsotope || isHeIsotope) {
364 const double Z = 1.0 - (canonicalComposition.
X + canonicalComposition.
Y);
365 if (std::abs(Z - canonicalComposition.
Z) > 1e-16) {
366 LOG_ERROR(
getLogger(),
"Validation composition Z (X-Y = {}) is different than canonical composition Z ({}) (∑a_i where a_i != H/He).", Z, canonicalComposition.
Z);
367 throw exceptions::InvalidCompositionError(
"Validation composition Z (X-Y = " + std::to_string(Z) +
") is different than canonical composition Z (" + std::to_string(canonicalComposition.
Z) +
") (∑a_i where a_i != H/He).");
369 m_cache.canonicalComp = canonicalComposition;
370 return canonicalComposition;
374 if (
m_cache.massFractions.has_value()) {
375 return m_cache.massFractions.value();
378 std::vector<double> massFractionVector;
379 std::vector<double> speciesMass;
386 speciesMass.push_back(
species.mass());
389 std::vector<double> massFractions = sortVectorBy(massFractionVector, speciesMass);
390 m_cache.massFractions = massFractions;
391 return massFractions;
396 if (
m_cache.numberFractions.has_value()) {
397 return m_cache.numberFractions.value();
400 std::vector<double> numberFractionVector;
401 std::vector<double> speciesMass;
408 speciesMass.push_back(
species.mass());
411 std::vector<double> numberFractions = sortVectorBy(numberFractionVector, speciesMass);
412 m_cache.numberFractions = numberFractions;
413 return numberFractions;
417 if (
m_cache.molarAbundances.has_value()) {
418 return m_cache.molarAbundances.value();
421 std::vector<double> molarAbundanceVector;
422 std::vector<double> speciesMass;
428 molarAbundanceVector.push_back(y);
429 speciesMass.push_back(
species.mass());
432 std::vector<double> molarAbundances = sortVectorBy(molarAbundanceVector, speciesMass);
433 m_cache.molarAbundances = molarAbundances;
434 return molarAbundances;
439 const std::string &symbol
441 const auto species = getSpecies(symbol);
443 throw_unknown_symbol(
getLogger(), symbol);
453 LOG_ERROR(
getLogger(),
"Species {} is not in the composition.",
species.name());
456 if (
m_cache.sortedSpecies.has_value()) {
457 return std::distance(
458 m_cache.sortedSpecies->begin(),
460 m_cache.sortedSpecies.value().begin(),
461 m_cache.sortedSpecies.value().end(),
467 std::vector<atomic::Species> speciesVector;
468 std::vector<double> speciesMass;
474 speciesVector.emplace_back(s);
475 speciesMass.push_back(s.mass());
478 std::vector<atomic::Species> sortedSpecies = sortVectorBy(speciesVector, speciesMass);
479 m_cache.sortedSpecies = sortedSpecies;
480 return std::distance(sortedSpecies.begin(), std::ranges::find(sortedSpecies,
species));
486 if (
m_cache.sortedSpecies.has_value()) {
487 return m_cache.sortedSpecies.value().at(index);
490 std::vector<atomic::Species> speciesVector;
491 std::vector<double> speciesMass;
497 speciesVector.emplace_back(
species);
498 speciesMass.push_back(
species.mass());
501 std::vector<atomic::Species> sortedSymbols = sortVectorBy(speciesVector, speciesMass);
502 if (index >= sortedSymbols.size()) {
503 LOG_ERROR(
getLogger(),
"Index {} is out of range for composition of size {}.", index, sortedSymbols.size());
504 throw std::out_of_range(
"Index " + std::to_string(index) +
" is out of range for composition of size " + std::to_string(sortedSymbols.size()) +
".");
506 return sortedSymbols.at(index);
516 const std::string &symbol
518 const auto species = getSpecies(symbol);
520 throw_unknown_symbol(
getLogger(), symbol);
530 const std::string &symbol,
531 const double &molar_abundance
533 const auto species = getSpecies(symbol);
535 throw_unknown_symbol(
getLogger(), symbol);
543 const double &molar_abundance
548 if (molar_abundance < 0.0) {
549 LOG_ERROR(
getLogger(),
"Molar abundance must be non-negative for symbol {}. Currently it is {}.",
species.name(), molar_abundance);
556 const std::vector<std::string> &symbols,
557 const std::vector<double> &molar_abundances
559 for (
const auto& [symbol, y] : std::views::zip(symbols, molar_abundances)) {
565 const std::vector<atomic::Species> &
species,
566 const std::vector<double> &molar_abundances
568 for (
const auto& [s, y] : std::views::zip(
species, molar_abundances)) {
574 const std::set<std::string> &symbols,
575 const std::vector<double> &molar_abundances
577 for (
const auto& [symbol, y] : std::views::zip(symbols, molar_abundances)) {
583 const std::set<atomic::Species> &
species,
584 const std::vector<double> &molar_abundances
586 for (
const auto& [s, y] : std::views::zip(
species, molar_abundances)) {
597 os <<
"Composition(Mass Fractions => [";
CompositionCache m_cache
Cache for computed properties to avoid redundant calculations.
size_t getSpeciesIndex(const std::string &symbol) const override
get the index in the sorted vector representation for a given symbol
bool contains(const atomic::Species &species) const noexcept override
Checks if a given species is present in the composition.
std::unordered_map< atomic::Species, double > getNumberFraction() const noexcept override
Gets the number fractions of all species in the composition.
Composition()=default
Default constructor.
void setMolarAbundance(const std::string &symbol, const double &molar_abundance)
Sets the molar abundance for a given symbol.
const std::set< atomic::Species > & getRegisteredSpecies() const noexcept override
Get a set of all species that are registered in the composition.
void registerSpecies(const atomic::Species &species) noexcept
Registers a new species by extracting its symbol.
void registerSymbol(const std::string &symbol)
Registers a new symbol for inclusion in the composition.
std::set< std::string > getRegisteredSymbols() const noexcept override
Gets the registered symbols.
std::set< atomic::Species > m_registeredSpecies
Set of registered species in the composition.
static quill::Logger * getLogger()
Gets the logger instance for the Composition class. This is static to ensure that all composition obj...
Composition & operator=(Composition const &other)
Assignment operator.
double getElectronAbundance() const noexcept override
Compute the electron abundance of the composition.
size_t size() const noexcept override
Gets the number of registered species in the composition.
std::unordered_map< atomic::Species, double > getMassFraction() const noexcept override
Gets the mass fractions of all species in the composition.
std::map< atomic::Species, double > m_molarAbundances
Map of species to their molar abundances.
CanonicalComposition getCanonicalComposition() const
Compute the canonical composition (X, Y, Z) of the composition.
std::vector< double > getMolarAbundanceVector() const noexcept override
Get a uniform vector representation of the molar abundances stored in the composition object sorted s...
double getMolarAbundance(const std::string &symbol) const override
Gets the molar abundances of all species in the composition.
std::vector< double > getNumberFractionVector() const noexcept override
Get a uniform vector representation of the number fractions stored in the composition object sorted s...
atomic::Species getSpeciesAtIndex(size_t index) const override
Get the species at a given index in the sorted vector representation.
std::vector< double > getMassFractionVector() const noexcept override
Get a uniform vector representation of the mass fraction stored in the composition object sorted such...
double getMeanParticleMass() const noexcept override
Compute the mean particle mass of the composition.
Exception thrown when a composition is in an invalid or inconsistent state.
Exception thrown when an unknown symbol is encountered.
Exception thrown when a symbol is used that has not been registered.
Contains canonical information about atomic species and elements used by 4D-STAR.
static const Species H_7("H-7", "H", 5, 6, 1, 7, 940.0, "B-", 23062.0, 652.0, "/2+#", "n ?", 7.052749, 1078.0)
static const Species H_2("H-2", "H", 0, 1, 1, 2, 1112.2831, "B-", std::numeric_limits< double >::quiet_NaN(), std::numeric_limits< double >::infinity(), "+*", "S=0.0145 78", 2.014101777844, 1.5e-05)
static const Species H_6("H-6", "H", 4, 5, 1, 6, 961.6395, "B-", 24283.6294, 294.0, "-#", "?;3n ?", 6.044955437, 272.816)
static const Species He_10("He-10", "He", 6, 8, 2, 10, 2995.134, "B-", 16144.5191, 260.0, "+", "n=100", 10.052815306, 99.676)
static const Species He_9("He-9", "He", 5, 7, 2, 9, 3349.038, "B-", 15980.9213, 2.5, "/2(+)", "=100", 9.043946414, 50.259)
static const std::unordered_map< std::string, const Species & > species
Map of species names to their corresponding Species objects.
static const Species He_8("He-8", "He", 4, 6, 2, 8, 3924.521, "B-", 10663.8784, 119.5, "+", "-=100;B-n=16 1;B-t=0.9 1", 8.033934388, 0.095)
static const Species He_3("He-3", "He", -1, 1, 2, 3, 2572.68044, "B-", -13736.0, std::numeric_limits< double >::infinity(), "/2+*", "S=0.0002 2", 3.01602932197, 6e-05)
static const Species H_1("H-1", "H", -1, 0, 1, 1, 0.0, "B-", std::numeric_limits< double >::quiet_NaN(), std::numeric_limits< double >::infinity(), "/2+*", "S=99.9855 78", 1.007825031898, 1.4e-05)
static const Species H_5("H-5", "H", 3, 4, 1, 5, 1336.3592, "B-", 21661.2131, 86.0, "1/2+)", "n=100", 5.035311492, 96.02)
static const Species He_7("He-7", "He", 3, 5, 2, 7, 4123.0578, "B-", 11166.0229, 2.51, "3/2)-", "=100", 7.027990652, 8.115)
static const Species He_4("He-4", "He", 0, 2, 2, 4, 7073.9156, "B-", -22898.274, std::numeric_limits< double >::infinity(), "+", "S=99.9998 2", 4.00260325413, 0.00016)
static const Species He_5("He-5", "He", 1, 3, 2, 5, 5512.1325, "B-", -447.6529, 602.0, "/2-", "=100", 5.012057224, 21.47)
static const Species H_3("H-3", "H", 1, 2, 1, 3, 2827.2654, "B-", 18.59202, 388789632.0, "/2+*", "-=100", 3.01604928132, 8e-05)
static const Species He_6("He-6", "He", 2, 4, 2, 6, 4878.5199, "B-", 3505.2147, 806.92, "+", "-=100;B-d=0.000278 18", 6.018885889, 0.057)
static const Species H_4("H-4", "H", 2, 3, 1, 4, 1720.4491, "B-", 22196.2131, 139.0, "-", "=100", 4.026431867, 107.354)
Utilities and types for representing and manipulating chemical compositions.
std::ostream & operator<<(std::ostream &os, const Composition &composition)
OVERLOADS.
Represents an atomic species (isotope) with its fundamental physical properties.
std::string_view name() const
Gets the name of the species.
Represents the canonical (X, Y, Z) composition of stellar material.
double Y
Mass fraction of Helium.
double X
Mass fraction of Hydrogen.
double Z
Mass fraction of Metals.