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:
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