|
| bool | operator== (const Composition &a, const Composition &b) noexcept |
| |
| Composition | get_composition_record (const std::string &metal_fraction_scheme, const std::string &isotopic_percentage_scheme, double initial_z, double initial_y) |
| | Constructs a stellar Composition from a named solar metal-fraction scheme and isotopic-percentage table, scaled to the supplied bulk hydrogen and helium mass fractions.
|
| |
| Composition | get_composition_record (io::SolarCompositions metal_fraction_scheme, io::IsotopicPercentages isotopic_percentage_scheme, double initial_z, double initial_y) |
| | Enum-based overload of get_composition_record().
|
| |
| Composition | buildCompositionFromMassFractions (const std::vector< std::string > &symbols, const std::vector< double > &massFractions) |
| | Build a Composition object from symbols and their corresponding mass fractions.
|
| |
| Composition | buildCompositionFromMassFractions (const std::vector< atomic::Species > &species, const std::vector< double > &massFractions) |
| | Build a Composition object from species and their corresponding mass fractions.
|
| |
| Composition | buildCompositionFromMassFractions (const std::set< atomic::Species > &species, const std::vector< double > &massFractions) |
| | Build a Composition object from species in a set and their corresponding mass fractions.
|
| |
| Composition | buildCompositionFromMassFractions (const std::unordered_map< atomic::Species, double > &massFractionsMap) |
| | Build a Composition object from a map of species to mass fractions.
|
| |
| Composition | buildCompositionFromMassFractions (const std::unordered_map< std::string, double > &massFractions) |
| | Build a Composition object from a map of species to mass fractions.
|
| |
| Composition | buildCompositionFromMassFractions (std::map< atomic::Species, double > massFractions) |
| | Build a Composition object from a map of species to mass fractions.
|
| |
| Composition | buildCompositionFromMassFractions (std::map< std::string, double > massFractions) |
| | Build a Composition object from a map of species to mass fractions.
|
| |
| std::optional< fourdst::atomic::Species > | getSpecies (const std::string &symbol) |
| |
| std::ostream & | operator<< (std::ostream &os, const Composition &composition) |
| |
Utilities and types for representing and manipulating chemical compositions.
The composition module provides a small, but expressive, API for constructing and querying material compositions used throughout the 4D-STAR codebase. A Composition represents a collection of atomic species together with their molar abundances. From these molar abundances the module can compute derived quantities such as mass fractions, number fractions, canonical (X, Y, Z) composition, mean particle mass, and the electron abundance (Y_e).
Key concepts:
- Species and Symbols: Atomic isotopes are represented by the strongly-typed fourdst::atomic::Species values (see
fourdst/atomic/species.h). Each species also has a human-readable string symbol (e.g. "H-1", "He-4") used by some constructors and convenience overloads.
- Molar abundances: The Composition API accepts and stores molar abundances (absolute mole counts). Many derived quantities (mass fraction, number fraction, mean particle mass) are computed from these molar abundances.
- Canonical composition: A CanonicalComposition (X, Y, Z) is provided which groups mass fractions into hydrogen (X), helium (Y), and metals (Z). A lightweight struct
CanonicalComposition holds these values and provides an ostream operator for easy logging and testing.
- Caching: The concrete Composition implementation caches computed vectors and scalars to avoid repeated work. The cache is invalidated automatically when molar abundances or registered species are changed.
Main types and functions
Composition: The primary concrete class for building and interrogating compositions. It implements the CompositionAbstract interface and exposes methods to register symbols/species, set molar abundances, and query all commonly-needed derived quantities. Multiple constructors are provided for convenience (from vectors/sets of symbols or species, with optional molar-abundance initialization).
Important member functions include:
- registerSymbol / registerSpecies (single or many overloads)
- setMolarAbundance (many overloads accepting symbols or species)
- getMolarAbundance, getMassFraction, getNumberFraction (symbol and species overloads)
- getMassFractionVector, getNumberFractionVector, getMolarAbundanceVector
- getMeanParticleMass, getElectronAbundance
- getCanonicalComposition
- Iteration support (begin/end) which iterates species from lightest to heaviest because species ordering is defined by atomic mass.
- CompositionAbstract: A compact abstract interface implemented by Composition which guarantees the presence of all getter/query methods. This allows other components to accept composition-like objects without depending on the concrete implementation.
- Utilities (fourdst::composition::buildCompositionFromMassFractions): Convenience helpers exist to construct a Composition from mass fractions (instead of molar abundances). Those helpers validate that the provided mass fractions sum to unity within a tight tolerance and convert them into the corresponding molar abundances before returning a populated Composition.
- Exceptions (namespace fourdst::composition::exceptions): The module defines a small hierarchy of exceptions for error handling:
- CompositionError: Base class for composition-related errors.
- InvalidCompositionError: Thrown when the composition is inconsistent or when mass fractions fail validation.
- UnregisteredSymbolError: Thrown when an operation requires a symbol that hasn't been registered on the Composition object.
- UnknownSymbolError: Thrown when a provided string symbol does not map to any known atomic species in the atomic species database.
Usage examples
Example 1 – basic construction and queries:
std::cout << canon << std::endl;
Manages a collection of chemical species and their abundances.
void setMolarAbundance(const std::string &symbol, const double &molar_abundance)
Sets the molar abundance for a given symbol.
void registerSymbol(const std::string &symbol)
Registers a new symbol for inclusion in the composition.
std::unordered_map< atomic::Species, double > getMassFraction() const noexcept override
Gets the mass fractions of all species in the composition.
CanonicalComposition getCanonicalComposition() const
Compute the canonical composition (X, Y, Z) of the composition.
double getMeanParticleMass() const noexcept override
Compute the mean particle mass of the composition.
Utilities and types for representing and manipulating chemical compositions.
Represents the canonical (X, Y, Z) composition of stellar material.
Example 2 – constructing from mass fractions:
#include "fourdst/composition/utils.h"
std::vector<std::string> symbols = {"H-1", "He-4", "C-12"};
std::vector<double> massFractions = {0.70, 0.28, 0.02};
Composition buildCompositionFromMassFractions(const std::vector< std::string > &symbols, const std::vector< double > &massFractions)
Build a Composition object from symbols and their corresponding mass fractions.
Notes and remarks
- Molar abundances are the canonical input for the Composition class. When passing mass fractions, use the
buildCompositionFromMassFractions helper which performs the safe conversion and validation.
- Many methods throw exceptions from the
fourdst::composition::exceptions namespace on invalid usage (unknown symbols, unregistered species, or invalid abundance values). Callers should catch and handle these where appropriate.
- Floating point results (mass/number fractions, mean particle mass, Y_e) are computed as doubles and may have small numerical round-off. Callers comparing values in tests should use an appropriate tolerance.
See also
| Composition fourdst::composition::get_composition_record |
( |
const std::string & | metal_fraction_scheme, |
|
|
const std::string & | isotopic_percentage_scheme, |
|
|
double | initial_z, |
|
|
double | initial_y ) |
|
nodiscard |
Constructs a stellar Composition from a named solar metal-fraction scheme and isotopic-percentage table, scaled to the supplied bulk hydrogen and helium mass fractions.
Available metal fraction schemes (extracted from MESA chem_def.f90):
AG89 (Anders & Grevesse 1989)
GN93 (Grevesse & Noels 1993)
GS98 (Grevesse & Sauval 1998)
L03 (Lodders 2003)
AGS05 (Asplund, Grevesse & Sauval 2005)
AGSS09 (Asplund et al. 2009)
A09_Przybilla
MB22_photospheric
AAG21_photospheric
L09 (Lodders 2009)
Available isotopic percentage schemes:
L03_data (Lodders 2003)
L09_data (Lodders 2009)
Algorithm:
- Data loading — The embedded binary
StandardMetalFractions is copied into a std::vector<char> and parsed twice: once for metal_fraction_scheme and once for isotopic_percentage_scheme.
- Species list — The isotope table is iterated; any isotope whose element appears in the metals list or is
"H" / "He" is looked up in the global atomic::species registry by "<Element>-<A>" and added to the list.
- H and He mass fractions — Four entries are prepended (H-1, H-2, He-3, He-4) using Anders & Grevesse (1989) solar He3/He4 ratio:
- X(H-1) = clamp(1 - Z - Y, 0, 1)
- X(H-2) = 0
- X(He-3) = Y * xsol_He3 / (xsol_He3 + xsol_He4)
- X(He-4) = Y * xsol_He4 / (xsol_He3 + xsol_He4) where xsol_He3 = 2.9291e-5 and xsol_He4 = 2.7521e-1.
- Atomic-weight weighting — When
CompositionData::requires_atomic_weight is true, each metal's number-fraction abundance is multiplied by the atomic mass of its most-abundant isotope (determined from the isotopic table).
- Normalisation — Metal fractions are summed and normalised to unity.
- Isotope distribution — Per-isotope mass fractions are computed as: X_i = Z_total * f_E * (p_i * m_i) / sum_j(p_j * m_j) where f_E is the normalised metal fraction, p_i the isotopic percentage (0-100 scale), and m_i the isotope's atomic mass.
- Renormalisation — Metal mass fractions are rescaled so their sum equals Z_total exactly.
- Assembly —
buildCompositionFromMassFractions(species, massFracs) builds the final Composition object.
- Parameters
-
| [in] | metal_fraction_scheme | Block tag of the desired solar metal composition (e.g., "GS98", "AGSS09"). Case-sensitive; must match a BEGIN/END tag in the embedded data exactly. |
| [in] | isotopic_percentage_scheme | Block tag of the isotopic percentage table (e.g., "L03_data", "L09_data"). |
| [in] | initial_z | Total metal mass fraction Z (0 <= Z < 1). |
| [in] | initial_y | Total helium mass fraction Y (0 <= Y < 1, with X + Y + Z <= 1 recommended). X(H-1) is clamped to [0, 1] if the constraint is violated. |
- Returns
Composition Fully populated composition object with per-isotope mass fractions normalised to initial_z and initial_y.
- Exceptions
-
| std::out_of_range | If a species name derived from the isotopic table is absent from atomic::species, or if either scheme tag is not present in the embedded data. |
| std::invalid_argument | If numeric fields in the embedded data are malformed (propagated from std::stod / std::stoi). |
- Examples
double x_h1 = comp.massFraction("H-1");
std::println("X(H-1) = {:.6f}", x_h1);
Composition get_composition_record(const std::string &metal_fraction_scheme, const std::string &isotopic_percentage_scheme, double initial_z, double initial_y)
Constructs a stellar Composition from a named solar metal-fraction scheme and isotopic-percentage tab...
Definition at line 252 of file standard_compositions.cpp.