24#include <unordered_map>
29#include "fourdst/config/config.h"
30#include "fourdst/logging/logging.h"
56 os <<
"<CanonicalComposition: "
134 [[nodiscard]] std::string
symbol()
const;
265 std::optional<double>
Ye;
285 config::Config&
m_config = config::Config::getInstance();
286 logging::LogManager&
m_logManager = logging::LogManager::getInstance();
355 [[nodiscard]]
bool finalize(
bool norm=
false);
370 explicit Composition(
const std::vector<std::string>& symbols);
382 explicit Composition(
const std::set<std::string>& symbols);
401 Composition(
const std::vector<std::string>& symbols,
const std::vector<double>& fractions,
bool massFracMode=
true);
430 void registerSymbol(
const std::string& symbol,
bool massFracMode=
true);
445 void registerSymbol(
const std::vector<std::string>& symbols,
bool massFracMode=
true);
477 void registerSpecies(
const std::vector<fourdst::atomic::Species>& species,
bool massFracMode=
true);
508 double setMassFraction(
const std::string& symbol,
const double& mass_fraction);
519 std::vector<double>
setMassFraction(
const std::vector<std::string>& symbols,
const std::vector<double>& mass_fractions);
539 std::vector<double>
setMassFraction(
const std::vector<fourdst::atomic::Species>& species,
const std::vector<double>& mass_fractions);
551 double setNumberFraction(
const std::string& symbol,
const double& number_fraction);
561 std::vector<double>
setNumberFraction(
const std::vector<std::string>& symbols,
const std::vector<double>& number_fractions);
581 std::vector<double>
setNumberFraction(
const std::vector<fourdst::atomic::Species>& species,
const std::vector<double>& number_fractions);
603 [[nodiscard]] std::unordered_map<std::string, double>
getMassFraction()
const override;
613 [[nodiscard]]
double getMassFraction(
const std::string& symbol)
const override;
633 [[nodiscard]]
double getNumberFraction(
const std::string& symbol)
const override;
651 [[nodiscard]] std::unordered_map<std::string, double>
getNumberFraction()
const override;
661 [[nodiscard]]
double getMolarAbundance(
const std::string& symbol)
const override;
681 [[nodiscard]] std::pair<CompositionEntry, GlobalComposition>
getComposition(
const std::string& symbol)
const;
734 [[nodiscard]]
Composition subset(
const std::vector<std::string>& symbols,
const std::string& method=
"norm")
const;
741 [[nodiscard]]
bool hasSymbol(
const std::string& symbol)
const override;
817 [[nodiscard]]
size_t getSpeciesIndex(
const std::string& symbol)
const override;
888 [[nodiscard]]
auto end()
const {
Abstract base class for chemical composition representations.
CompositionCache m_cache
Cache for computed properties to avoid redundant calculations.
~Composition() override=default
Default destructor.
void setCompositionMode(bool massFracMode)
Sets the composition mode (mass fraction vs. number fraction).
size_t getSpeciesIndex(const std::string &symbol) const override
get the index in the sorted vector representation for a given symbol
std::pair< std::unordered_map< std::string, CompositionEntry >, GlobalComposition > getComposition() const
Gets all composition entries and the global composition data.
Composition subset(const std::vector< std::string > &symbols, const std::string &method="norm") const
Creates a new Composition object containing a subset of species from this one.
void registerSymbol(const std::string &symbol, bool massFracMode=true)
Registers a new symbol for inclusion in the composition.
config::Config & m_config
Composition()=default
Default constructor.
Composition operator+(const Composition &other) const
Overloads the + operator to mix two compositions with a 50/50 fraction.
friend std::ostream & operator<<(std::ostream &os, const Composition &composition)
Overloaded output stream operator for Composition.
std::set< std::string > m_registeredSymbols
The registered symbols.
Composition mix(const Composition &other, double fraction) const
Mixes this composition with another to produce a new composition.
std::set< std::string > getRegisteredSymbols() const override
Gets the registered symbols.
logging::LogManager & m_logManager
bool finalizeNumberFracMode(bool norm)
Finalizes the composition in number fraction mode.
double setMassFraction(const std::string &symbol, const double &mass_fraction)
Sets the mass fraction for a given symbol.
std::vector< double > getNumberFractionVector() const override
Get a uniform vector representation of the number fractions stored in the composition object sorted s...
double m_meanParticleMass
The mean particle mass of the composition (\sum_{i} \frac{n_i}{m_i}. where n_i is the number fraction...
void registerSpecies(const fourdst::atomic::Species &species, bool massFracMode=true)
Registers a new species by extracting its symbol.
Composition & operator=(Composition const &other)
Assignment operator.
bool hasSpecies(const fourdst::atomic::Species &species) const override
Checks if a species is registered in the composition.
double getElectronAbundance() const override
Compute the electron abundance of the composition.
bool m_massFracMode
True if mass fraction mode, false if number fraction mode.
bool finalize(bool norm=false)
Finalizes the composition, making it ready for querying.
double getMeanParticleMass() const override
Compute the mean particle mass of the composition.
double setNumberFraction(const std::string &symbol, const double &number_fraction)
Sets the number fraction for a given symbol.
bool contains(const atomic::Species &isotope) const override
Checks if a given isotope is present in the composition.
std::vector< double > getMassFractionVector() const override
Get a uniform vector representation of the mass fraction stored in the composition object sorted such...
void validateComposition(const std::vector< double > &fractions) const
Validates the given fractions, throwing an exception on failure.
bool finalizeMassFracMode(bool norm)
Finalizes the composition in mass fraction mode.
static bool isValidSymbol(const std::string &symbol)
Checks if the given symbol is valid by checking against the global species database.
bool m_finalized
True if the composition is finalized.
std::unordered_map< std::string, CompositionEntry > m_compositions
The compositions.
std::unordered_map< std::string, double > getMassFraction() const override
Gets the mass fractions of all species in the composition.
auto begin()
Returns an iterator to the beginning of the composition map.
std::vector< double > getMolarAbundanceVector() const override
Get a uniform vector representation of the molar abundances stored in the composition object sorted s...
bool hasSymbol(const std::string &symbol) const override
Checks if a symbol is registered in the composition.
CanonicalComposition getCanonicalComposition(bool harsh=false) const
Gets the current canonical composition (X, Y, Z).
double getMolarAbundance(const std::string &symbol) const override
Gets the molar abundance (X_i / A_i) for a given symbol.
double m_specificNumberDensity
The specific number density of the composition (\sum_{i} X_i m_i. Where X_i is the number fraction of...
bool isValidComposition(const std::vector< double > &fractions) const
Checks if the given fractions are valid (sum to ~1.0).
auto end()
Returns an iterator to the end of the composition map.
auto begin() const
Returns a const iterator to the beginning of the composition map.
std::unordered_map< std::string, double > getNumberFraction() const override
Gets the number fractions of all species in the composition.
atomic::Species getSpeciesAtIndex(size_t index) const override
Get the species at a given index in the sorted vector representation.
auto end() const
Returns a const iterator to the end of the composition map.
std::set< fourdst::atomic::Species > getRegisteredSpecies() const override
Get a set of all species that are registered in the composition.
double getMeanAtomicNumber() const override
Compute the mean atomic number of the composition.
Represents an atomic species (isotope) with its fundamental physical properties.
Represents the canonical (X, Y, Z) composition of stellar material.
friend std::ostream & operator<<(std::ostream &os, const CanonicalComposition &composition)
Overloads the stream insertion operator for easy printing.
double Y
Mass fraction of Helium.
double X
Mass fraction of Hydrogen.
double Z
Mass fraction of Metals.
std::optional< std::vector< atomic::Species > > sortedSpecies
Cached vector of sorted species (by mass).
std::optional< std::vector< double > > numberFractions
Cached vector of number fractions.
std::optional< CanonicalComposition > canonicalComp
Cached canonical composition data.
std::optional< GlobalComposition > globalComp
Cached global composition data.
std::optional< std::vector< double > > molarAbundances
Cached vector of molar abundances.
std::optional< std::vector< std::string > > sortedSymbols
Cached vector of sorted species (by mass).
std::optional< std::vector< double > > massFractions
Cached vector of mass fractions.
std::optional< double > Ye
Cached electron abundance.
bool setNumberFracMode(double totalMolesPerMass)
Switches the mode to number fraction mode.
double m_relAbundance
The relative abundance, used internally for conversions. For mass fraction mode, this is X_i / A_i; f...
bool getMassFracMode() const
Gets the mode of the composition entry.
CompositionEntry()
Default constructor. Initializes a default entry (H-1), but in an uninitialized state.
bool m_massFracMode
The mode of the composition entry. True if mass fraction, false if number fraction.
double m_numberFraction
The number fraction (mole fraction) of the species. Valid only if m_massFracMode is false.
double number_fraction() const
Gets the number fraction of the species.
CompositionEntry(const CompositionEntry &entry)
Copy constructor.
bool m_initialized
True if the composition entry has been initialized with a valid species.
double m_cachedNumberFraction
Cached number fraction for conversions when in mass fraction mode.
bool setMassFracMode(double meanMolarMass)
Switches the mode to mass fraction mode.
void setMassFraction(double mass_fraction)
Sets the mass fraction of the species.
std::string symbol() const
Gets the chemical symbol of the species.
void setSpecies(const std::string &symbol)
Sets the species for the composition entry. This can only be done once.
double mass_fraction() const
Gets the mass fraction of the species.
atomic::Species m_isotope
The atomic::Species object containing detailed isotope data.
void setNumberFraction(double number_fraction)
Sets the number fraction of the species.
double rel_abundance() const
Gets the relative abundance of the species.
std::string m_symbol
The chemical symbol of the species (e.g., "H-1", "Fe-56").
double m_massFraction
The mass fraction of the species. Valid only if m_massFracMode is true.
friend std::ostream & operator<<(std::ostream &os, const CompositionEntry &entry)
Overloaded output stream operator for CompositionEntry.
atomic::Species isotope() const
Gets the isotope data for the species.
Represents global properties of a finalized composition.
double specificNumberDensity
The specific number density (moles per unit mass, sum of X_i/M_i), where X_i is mass fraction and M_i...
friend std::ostream & operator<<(std::ostream &os, const GlobalComposition &comp)
double meanParticleMass
The mean mass per particle (inverse of specific number density). Units: g/mol.