fourdst::libcomposition v1.5.2
Robust atomic species information library
|
Manages a collection of chemical species and their abundances. More...
#include <composition.h>
Public Member Functions | |
Composition ()=default | |
Default constructor. | |
~Composition ()=default | |
Default destructor. | |
bool | finalize (bool norm=false) |
Finalizes the composition, making it ready for querying. | |
Composition (const std::vector< std::string > &symbols) | |
Constructs a Composition and registers the given symbols. | |
Composition (const std::set< std::string > &symbols) | |
Constructs a Composition and registers the given symbols from a set. | |
Composition (const std::vector< std::string > &symbols, const std::vector< double > &fractions, bool massFracMode=true) | |
Constructs and finalizes a Composition with the given symbols and fractions. | |
Composition (const Composition &composition) | |
Constructs a Composition from another Composition. | |
Composition & | operator= (Composition const &other) |
Assignment operator. | |
void | registerSymbol (const std::string &symbol, bool massFracMode=true) |
Registers a new symbol for inclusion in the composition. | |
void | registerSymbol (const std::vector< std::string > &symbols, bool massFracMode=true) |
Registers multiple new symbols. | |
void | registerSpecies (const fourdst::atomic::Species &species, bool massFracMode=true) |
Registers a new species by extracting its symbol. | |
void | registerSpecies (const std::vector< fourdst::atomic::Species > &species, bool massFracMode=true) |
Registers a vector of new species. | |
std::set< std::string > | getRegisteredSymbols () const |
Gets the registered symbols. | |
std::set< fourdst::atomic::Species > | getRegisteredSpecies () const |
Get a set of all species that are registered in the composition. | |
double | setMassFraction (const std::string &symbol, const double &mass_fraction) |
Sets the mass fraction for a given symbol. | |
std::vector< double > | setMassFraction (const std::vector< std::string > &symbols, const std::vector< double > &mass_fractions) |
Sets the mass fraction for multiple symbols. | |
double | setMassFraction (const fourdst::atomic::Species &species, const double &mass_fraction) |
Sets the mass fraction for a given species. | |
std::vector< double > | setMassFraction (const std::vector< fourdst::atomic::Species > &species, const std::vector< double > &mass_fractions) |
Sets the mass fraction for multiple species. | |
double | setNumberFraction (const std::string &symbol, const double &number_fraction) |
Sets the number fraction for a given symbol. | |
std::vector< double > | setNumberFraction (const std::vector< std::string > &symbols, const std::vector< double > &number_fractions) |
Sets the number fraction for multiple symbols. | |
double | setNumberFraction (const fourdst::atomic::Species &species, const double &number_fraction) |
Sets the number fraction for a given species. | |
std::vector< double > | setNumberFraction (const std::vector< fourdst::atomic::Species > &species, const std::vector< double > &number_fractions) |
Sets the number fraction for multiple species. | |
Composition | mix (const Composition &other, double fraction) const |
Mixes this composition with another to produce a new composition. | |
std::unordered_map< std::string, double > | getMassFraction () const |
Gets the mass fractions of all species in the composition. | |
double | getMassFraction (const std::string &symbol) const |
Gets the mass fraction for a given symbol. | |
double | getMassFraction (const fourdst::atomic::Species &species) const |
Gets the mass fraction for a given isotope. | |
double | getNumberFraction (const std::string &symbol) const |
Gets the number fraction for a given symbol. | |
double | getNumberFraction (const fourdst::atomic::Species &species) const |
Gets the number fraction for a given isotope. | |
std::unordered_map< std::string, double > | getNumberFraction () const |
Gets the number fractions of all species in the composition. | |
double | getMolarAbundance (const std::string &symbol) const |
Gets the molar abundance (X_i / A_i) for a given symbol. | |
double | getMolarAbundance (const fourdst::atomic::Species &species) const |
Gets the molar abundance for a given isotope. | |
std::pair< CompositionEntry, GlobalComposition > | getComposition (const std::string &symbol) const |
Gets the composition entry and global composition data for a given symbol. | |
std::pair< CompositionEntry, GlobalComposition > | getComposition (const fourdst::atomic::Species &species) const |
Gets the composition entry and global composition data for a given species. | |
std::pair< std::unordered_map< std::string, CompositionEntry >, GlobalComposition > | getComposition () const |
Gets all composition entries and the global composition data. | |
double | getMeanParticleMass () const |
Compute the mean particle mass of the composition. | |
double | getMeanAtomicNumber () const |
Compute the mean atomic number of the composition. | |
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. | |
bool | hasSymbol (const std::string &symbol) const |
Checks if a symbol is registered in the composition. | |
bool | contains (const fourdst::atomic::Species &isotope) const |
Checks if a given isotope is present in the composition. | |
void | setCompositionMode (bool massFracMode) |
Sets the composition mode (mass fraction vs. number fraction). | |
CanonicalComposition | getCanonicalComposition (bool harsh=false) const |
Gets the current canonical composition (X, Y, Z). | |
Composition | operator+ (const Composition &other) const |
Overloads the + operator to mix two compositions with a 50/50 fraction. | |
auto | begin () |
Returns an iterator to the beginning of the composition map. | |
auto | begin () const |
Returns a const iterator to the beginning of the composition map. | |
auto | end () |
Returns an iterator to the end of the composition map. | |
auto | end () const |
Returns a const iterator to the end of the composition map. | |
Private Member Functions | |
bool | isValidComposition (const std::vector< double > &fractions) const |
Checks if the given fractions are valid (sum to ~1.0). | |
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. | |
bool | finalizeNumberFracMode (bool norm) |
Finalizes the composition in number fraction mode. | |
Static Private Member Functions | |
static bool | isValidSymbol (const std::string &symbol) |
Checks if the given symbol is valid by checking against the global species database. | |
Private Attributes | |
fourdst::config::Config & | m_config = fourdst::config::Config::getInstance() |
fourdst::logging::LogManager & | m_logManager = fourdst::logging::LogManager::getInstance() |
quill::Logger * | m_logger = m_logManager.getLogger("log") |
bool | m_finalized = false |
True if the composition is finalized. | |
double | m_specificNumberDensity = 0.0 |
The specific number density of the composition (\sum_{i} X_i m_i. Where X_i is the number fraction of the ith species and m_i is the mass of the ith species). | |
double | m_meanParticleMass = 0.0 |
The mean particle mass of the composition (\sum_{i} \frac{n_i}{m_i}. where n_i is the number fraction of the ith species and m_i is the mass of the ith species). | |
bool | m_massFracMode = true |
True if mass fraction mode, false if number fraction mode. | |
std::set< std::string > | m_registeredSymbols |
The registered symbols. | |
std::unordered_map< std::string, CompositionEntry > | m_compositions |
The compositions. | |
Friends | |
std::ostream & | operator<< (std::ostream &os, const Composition &composition) |
Overloaded output stream operator for Composition. | |
Manages a collection of chemical species and their abundances.
This class is a primary interface for defining and manipulating material compositions. It can operate in two modes: mass fraction or number fraction.
Key Rules and Workflow:
registerSymbol()
or registerSpecies()
. All registered species must conform to the same abundance mode (mass or number fraction).setMassFraction()
or setNumberFraction()
to define the composition.getMassFraction()
, getMeanParticleMass()
), the composition must be finalized by calling finalize()
. This step validates the composition (abundances sum to ~1.0) and computes global properties.finalize()
before data can be retrieved again.This | class throws various exceptions from fourdst::composition::exceptions for invalid operations, such as using unregistered symbols, providing invalid abundances, or accessing data from a non-finalized composition. |
Definition at line 258 of file composition.h.
|
default |
Default constructor.
|
default |
Default destructor.
|
explicit |
Constructs a Composition and registers the given symbols.
symbols | The symbols to register. The composition will be in mass fraction mode by default. |
exceptions::InvalidSymbolError | if any symbol is invalid. |
Definition at line 151 of file composition.cpp.
|
explicit |
Constructs a Composition and registers the given symbols from a set.
symbols | The symbols to register. The composition will be in mass fraction mode by default. |
exceptions::InvalidSymbolError | if any symbol is invalid. |
Definition at line 157 of file composition.cpp.
fourdst::composition::Composition::Composition | ( | const std::vector< std::string > & | symbols, |
const std::vector< double > & | fractions, | ||
bool | massFracMode = true ) |
Constructs and finalizes a Composition with the given symbols and fractions.
This constructor provides a convenient way to create a fully-formed, finalized composition in one step. The provided fractions must be valid and sum to 1.0.
symbols | The symbols to initialize the composition with. |
fractions | The fractions (mass or number) corresponding to the symbols. |
massFracMode | True if fractions are mass fractions, false if they are number fractions. [Default: true] |
exceptions::InvalidCompositionError | if the number of symbols and fractions do not match, or if the fractions do not sum to ~1.0. |
exceptions::InvalidSymbolError | if any symbol is invalid. |
Definition at line 163 of file composition.cpp.
fourdst::composition::Composition::Composition | ( | const Composition & | composition | ) |
Constructs a Composition from another Composition.
composition | The Composition to copy. |
Definition at line 185 of file composition.cpp.
|
inline |
Returns an iterator to the beginning of the composition map.
Definition at line 759 of file composition.h.
|
inline |
Returns a const iterator to the beginning of the composition map.
Definition at line 767 of file composition.h.
|
nodiscard |
Checks if a given isotope is present in the composition.
isotope | The isotope to check for. |
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
Definition at line 741 of file composition.cpp.
|
inline |
Returns an iterator to the end of the composition map.
Definition at line 775 of file composition.h.
|
inline |
Returns a const iterator to the end of the composition map.
Definition at line 783 of file composition.h.
bool fourdst::composition::Composition::finalize | ( | bool | norm = false | ) |
Finalizes the composition, making it ready for querying.
This method checks if the sum of all fractions (mass or number) is approximately 1.0. It also computes global properties like mean particle mass. This must be called before any get...
method can be used.
norm | If true, the composition will be normalized to sum to 1 before validation. [Default: false] |
m_finalized
is true and global properties are computed. Definition at line 389 of file composition.cpp.
|
private |
Finalizes the composition in mass fraction mode.
norm | If true, the composition will be normalized to sum to 1. |
Definition at line 402 of file composition.cpp.
|
private |
Finalizes the composition in number fraction mode.
norm | If true, the composition will be normalized to sum to 1. |
Definition at line 439 of file composition.cpp.
|
nodiscard |
Gets the current canonical composition (X, Y, Z).
Calculates the total mass fractions for H, He, and metals.
harsh | If true, this will throw an error if 1 - (X + Y) is not equal to the directly summed Z (within a tolerance). If false, it will only log a warning. |
CanonicalComposition
struct. exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
std::runtime_error | if harsh is true and the canonical composition is not self-consistent. |
Definition at line 690 of file composition.cpp.
|
nodiscard |
Gets all composition entries and the global composition data.
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
Definition at line 605 of file composition.cpp.
|
nodiscard |
Gets the composition entry and global composition data for a given species.
species | The species to get the composition for. |
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
exceptions::UnregisteredSymbolError | if the species is not in the composition. |
Definition at line 600 of file composition.cpp.
|
nodiscard |
Gets the composition entry and global composition data for a given symbol.
symbol | The symbol to get the composition for. |
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
exceptions::UnregisteredSymbolError | if the symbol is not in the composition. |
Definition at line 588 of file composition.cpp.
|
nodiscard |
Gets the mass fractions of all species in the composition.
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
Definition at line 534 of file composition.cpp.
|
nodiscard |
Gets the mass fraction for a given isotope.
species | The isotope to get the mass fraction for. |
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
exceptions::UnregisteredSymbolError | if the isotope is not registered in the composition. |
Definition at line 530 of file composition.cpp.
|
nodiscard |
Gets the mass fraction for a given symbol.
symbol | The symbol to get the mass fraction for. |
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
exceptions::UnregisteredSymbolError | if the symbol is not in the composition. |
Definition at line 503 of file composition.cpp.
|
nodiscard |
Compute the mean atomic number of the composition.
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
Definition at line 621 of file composition.cpp.
|
nodiscard |
Compute the mean particle mass of the composition.
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
Definition at line 613 of file composition.cpp.
|
nodiscard |
Gets the molar abundance for a given isotope.
species | The isotope to get the molar abundance for. |
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
exceptions::UnregisteredSymbolError | if the isotope is not registered in the composition. |
Definition at line 584 of file composition.cpp.
|
nodiscard |
Gets the molar abundance (X_i / A_i) for a given symbol.
symbol | The symbol to get the molar abundance for. |
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
exceptions::UnregisteredSymbolError | if the symbol is not in the composition. |
Definition at line 571 of file composition.cpp.
|
nodiscard |
Gets the number fractions of all species in the composition.
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
Definition at line 563 of file composition.cpp.
|
nodiscard |
Gets the number fraction for a given isotope.
species | The isotope to get the number fraction for. |
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
exceptions::UnregisteredSymbolError | if the isotope is not registered in the composition. |
Definition at line 559 of file composition.cpp.
|
nodiscard |
Gets the number fraction for a given symbol.
symbol | The symbol to get the number fraction for. |
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
exceptions::UnregisteredSymbolError | if the symbol is not in the composition. |
Definition at line 543 of file composition.cpp.
|
nodiscard |
Get a set of all species that are registered in the composition.
atomic::Species
objects registered in the composition. Definition at line 255 of file composition.cpp.
|
nodiscard |
Gets the registered symbols.
Definition at line 251 of file composition.cpp.
|
nodiscard |
Checks if a symbol is registered in the composition.
symbol | The symbol to check. |
Definition at line 737 of file composition.cpp.
|
nodiscardprivate |
Checks if the given fractions are valid (sum to ~1.0).
fractions | The fractions to check. |
Definition at line 270 of file composition.cpp.
|
staticprivate |
Checks if the given symbol is valid by checking against the global species database.
symbol | The symbol to check. |
Definition at line 283 of file composition.cpp.
|
nodiscard |
Mixes this composition with another to produce a new composition.
The mixing is performed linearly on the mass fractions. The formula for each species is: new_X_i = fraction * this_X_i + (1 - fraction) * other_X_i
. The resulting composition is automatically finalized.
other | The other composition to mix with. |
fraction | The mixing fraction. A value of 1.0 means the new composition is 100% this , 0.0 means 100% other . |
Composition
object representing the mixture. this
and other
compositions must be finalized. exceptions::CompositionNotFinalizedError | if either composition is not finalized. |
exceptions::InvalidCompositionError | if the fraction is not between 0 and 1. |
Definition at line 473 of file composition.cpp.
Composition fourdst::composition::Composition::operator+ | ( | const Composition & | other | ) | const |
Overloads the + operator to mix two compositions with a 50/50 fraction.
OVERLOADS.
This is a convenience operator that calls mix(other, 0.5)
.
other | The other composition to mix with. |
See | mix() for exceptions. |
Definition at line 756 of file composition.cpp.
Composition & fourdst::composition::Composition::operator= | ( | Composition const & | other | ) |
Assignment operator.
other | The Composition to assign from. |
Definition at line 194 of file composition.cpp.
void fourdst::composition::Composition::registerSpecies | ( | const fourdst::atomic::Species & | species, |
bool | massFracMode = true ) |
Registers a new species by extracting its symbol.
species | The species to register. |
massFracMode | True for mass fraction mode, false for number fraction mode. |
exceptions::InvalidSymbolError | if the species' symbol is invalid. |
exceptions::CompositionModeError | if the mode conflicts. |
Definition at line 241 of file composition.cpp.
void fourdst::composition::Composition::registerSpecies | ( | const std::vector< fourdst::atomic::Species > & | species, |
bool | massFracMode = true ) |
Registers a vector of new species.
species | The vector of species to register. |
massFracMode | True for mass fraction mode, false for number fraction mode. |
exceptions::InvalidSymbolError | if any species' symbol is invalid. |
exceptions::CompositionModeError | if the mode conflicts. |
Definition at line 245 of file composition.cpp.
void fourdst::composition::Composition::registerSymbol | ( | const std::string & | symbol, |
bool | massFracMode = true ) |
Registers a new symbol for inclusion in the composition.
A symbol must be registered before its abundance can be set. The first registration sets the mode (mass/number fraction) for the entire composition.
symbol | The symbol to register (e.g., "Fe-56"). |
massFracMode | True for mass fraction mode, false for number fraction mode. This is only effective for the first symbol registered. |
exceptions::InvalidSymbolError | if the symbol is not in the atomic species database. |
exceptions::CompositionModeError | if attempting to register with a mode that conflicts with the existing mode. |
Definition at line 208 of file composition.cpp.
void fourdst::composition::Composition::registerSymbol | ( | const std::vector< std::string > & | symbols, |
bool | massFracMode = true ) |
Registers multiple new symbols.
symbols | The symbols to register. |
massFracMode | True for mass fraction mode, false for number fraction mode. |
exceptions::InvalidSymbolError | if any symbol is invalid. |
exceptions::CompositionModeError | if the mode conflicts with an already set mode. |
Definition at line 235 of file composition.cpp.
void fourdst::composition::Composition::setCompositionMode | ( | bool | massFracMode | ) |
Sets the composition mode (mass fraction vs. number fraction).
This function converts all entries in the composition to the specified mode.
massFracMode | True to switch to mass fraction mode, false for number fraction mode. |
exceptions::CompositionNotFinalizedError | if the composition is not finalized. |
std::runtime_error | if the conversion fails for an unknown reason. |
Definition at line 669 of file composition.cpp.
double fourdst::composition::Composition::setMassFraction | ( | const fourdst::atomic::Species & | species, |
const double & | mass_fraction ) |
Sets the mass fraction for a given species.
species | The species to set the mass fraction for. |
mass_fraction | The mass fraction to set. |
exceptions::UnregisteredSymbolError | if the species is not registered. |
exceptions::CompositionModeError | if the composition is in number fraction mode. |
exceptions::InvalidCompositionError | if the mass fraction is not between 0 and 1. |
Definition at line 324 of file composition.cpp.
double fourdst::composition::Composition::setMassFraction | ( | const std::string & | symbol, |
const double & | mass_fraction ) |
Sets the mass fraction for a given symbol.
symbol | The symbol to set the mass fraction for. |
mass_fraction | The mass fraction to set (must be in [0, 1]). |
exceptions::UnregisteredSymbolError | if the symbol is not registered. |
exceptions::CompositionModeError | if the composition is in number fraction mode. |
exceptions::InvalidCompositionError | if the mass fraction is not between 0 and 1. |
Definition at line 287 of file composition.cpp.
std::vector< double > fourdst::composition::Composition::setMassFraction | ( | const std::vector< fourdst::atomic::Species > & | species, |
const std::vector< double > & | mass_fractions ) |
Sets the mass fraction for multiple species.
species | The vector of species to set the mass fractions for. |
mass_fractions | The vector of mass fractions corresponding to the species. |
See | setMassFraction(const std::vector<std::string>&, const std::vector<double>&) for exceptions. |
Definition at line 328 of file composition.cpp.
std::vector< double > fourdst::composition::Composition::setMassFraction | ( | const std::vector< std::string > & | symbols, |
const std::vector< double > & | mass_fractions ) |
Sets the mass fraction for multiple symbols.
symbols | The symbols to set the mass fractions for. |
mass_fractions | The mass fractions corresponding to the symbols. |
exceptions::InvalidCompositionError | if symbol and fraction counts differ. |
See | setMassFraction(const std::string&, const double&) for other exceptions. |
Definition at line 310 of file composition.cpp.
double fourdst::composition::Composition::setNumberFraction | ( | const fourdst::atomic::Species & | species, |
const double & | number_fraction ) |
Sets the number fraction for a given species.
species | The species to set the number fraction for. |
number_fraction | The number fraction to set for the species. |
exceptions::UnregisteredSymbolError | if the species is not registered. |
exceptions::CompositionModeError | if the composition is in mass fraction mode. |
exceptions::InvalidCompositionError | if the number fraction is not between 0 and 1. |
Definition at line 375 of file composition.cpp.
double fourdst::composition::Composition::setNumberFraction | ( | const std::string & | symbol, |
const double & | number_fraction ) |
Sets the number fraction for a given symbol.
symbol | The symbol to set the number fraction for. |
number_fraction | The number fraction to set (must be in [0, 1]). |
exceptions::UnregisteredSymbolError | if the symbol is not registered. |
exceptions::CompositionModeError | if the composition is in mass fraction mode. |
exceptions::InvalidCompositionError | if the number fraction is not between 0 and 1. |
Definition at line 338 of file composition.cpp.
std::vector< double > fourdst::composition::Composition::setNumberFraction | ( | const std::vector< fourdst::atomic::Species > & | species, |
const std::vector< double > & | number_fractions ) |
Sets the number fraction for multiple species.
species | The vector of species to set the number fractions for. |
number_fractions | The vector of number fractions corresponding to the species. |
See | setNumberFraction(const std::vector<std::string>&, const std::vector<double>&) for exceptions. |
Definition at line 379 of file composition.cpp.
std::vector< double > fourdst::composition::Composition::setNumberFraction | ( | const std::vector< std::string > & | symbols, |
const std::vector< double > & | number_fractions ) |
Sets the number fraction for multiple symbols.
symbols | The symbols to set the number fractions for. |
number_fractions | The number fractions corresponding to the symbols. |
exceptions::InvalidCompositionError | if symbol and fraction counts differ. |
See | setNumberFraction(const std::string&, const double&) for other exceptions. |
Definition at line 361 of file composition.cpp.
|
nodiscard |
Creates a new Composition object containing a subset of species from this one.
symbols | The symbols to include in the subset. |
method | The method for handling the abundances of the new subset. Can be "norm" (normalize abundances to sum to 1) or "none" (keep original abundances). |
Composition
object containing the subset. exceptions::UnregisteredSymbolError | if any requested symbol is not in the original composition. |
exceptions::InvalidMixingMode | if an invalid method is provided. |
exceptions::FailedToFinalizeCompositionError | if normalization fails. |
Definition at line 640 of file composition.cpp.
|
private |
Validates the given fractions, throwing an exception on failure.
fractions | The fractions to validate. |
exceptions::InvalidCompositionError | if the fractions are invalid. |
Definition at line 263 of file composition.cpp.
|
friend |
Overloaded output stream operator for Composition.
os | The output stream. |
composition | The Composition to output. |
Definition at line 772 of file composition.cpp.
|
private |
The compositions.
Definition at line 270 of file composition.h.
|
private |
Definition at line 260 of file composition.h.
|
private |
True if the composition is finalized.
Definition at line 264 of file composition.h.
|
private |
Definition at line 262 of file composition.h.
|
private |
Definition at line 261 of file composition.h.
|
private |
True if mass fraction mode, false if number fraction mode.
Definition at line 267 of file composition.h.
|
private |
The mean particle mass of the composition (\sum_{i} \frac{n_i}{m_i}. where n_i is the number fraction of the ith species and m_i is the mass of the ith species).
Definition at line 266 of file composition.h.
|
private |
The registered symbols.
Definition at line 269 of file composition.h.
|
private |
The specific number density of the composition (\sum_{i} X_i m_i. Where X_i is the number fraction of the ith species and m_i is the mass of the ith species).
Definition at line 265 of file composition.h.