SERiF 0.0.1a
3+1D Stellar Structure and Evolution
Loading...
Searching...
No Matches
serif::composition::Composition Class Reference

Manages the composition of elements. More...

#include <composition.h>

Public Member Functions

 Composition ()=default
 Default constructor.
 
 ~Composition ()=default
 Default destructor.
 
bool finalize (bool norm=false)
 Finalizes the composition.
 
 Composition (const std::vector< std::string > &symbols)
 Constructs a Composition with the given symbols.
 
 Composition (const std::set< std::string > &symbols)
 Constructs a Composition with the given symbols as a set.
 
 Composition (const std::vector< std::string > &symbols, const std::vector< double > &mass_fractions, bool massFracMode=true)
 Constructs a Composition with the given symbols and mass fractions.
 
 Composition (const Composition &composition)
 Constructs a Composition from another Composition.
 
Compositionoperator= (Composition const &other)
 
void registerSymbol (const std::string &symbol, bool massFracMode=true)
 Registers a new symbol.
 
void registerSymbol (const std::vector< std::string > &symbols, bool massFracMode=true)
 Registers multiple new symbols.
 
std::set< std::string > getRegisteredSymbols () const
 Gets the registered symbols.
 
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 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.
 
Composition mix (const Composition &other, double fraction) const
 Mix two compositions together with a given fraction.
 
std::unordered_map< std::string, double > getMassFraction () const
 Gets the mass fractions of all compositions.
 
double getMassFraction (const std::string &symbol) const
 Gets the mass fraction for a given symbol.
 
double getNumberFraction (const std::string &symbol) const
 Gets the number fraction for a given symbol.
 
std::unordered_map< std::string, double > getNumberFraction () const
 Gets the number fractions of all compositions.
 
std::pair< CompositionEntry, GlobalCompositiongetComposition (const std::string &symbol) const
 Gets the composition entry and global composition for a given symbol.
 
std::pair< std::unordered_map< std::string, CompositionEntry >, GlobalCompositiongetComposition () const
 Gets all composition entries and the global composition.
 
double getMeanParticleMass () const
 Compute the mean particle mass of the composition.
 
double getMeanAtomicNumber () const
 Compute the mean atomic mass number of the composition.
 
Composition subset (const std::vector< std::string > &symbols, std::string method="norm") const
 Gets a subset of the composition.
 
bool hasSymbol (const std::string &symbol) const
 Check if a symbol is registered.
 
bool contains (const serif::atomic::Species &isotope) const
 
void setCompositionMode (bool massFracMode)
 Sets the composition mode.
 
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 together with a fraction of 0.5.
 

Private Member Functions

bool isValidComposition (const std::vector< double > &fractions) const
 Checks if the given mass fractions are valid.
 
void validateComposition (const std::vector< double > &fractions) const
 Validates the given mass fractions.
 
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.
 

Private Attributes

serif::config::Config & m_config = serif::config::Config::getInstance()
 
serif::probe::LogManagerm_logManager = serif::probe::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, CompositionEntrym_compositions
 The compositions.
 

Friends

std::ostream & operator<< (std::ostream &os, const Composition &composition)
 Overloaded output stream operator for Composition.
 

Detailed Description

Manages the composition of elements.

The composition is a collection of elements with their respective mass fractions. The general purpose of this class is to provide a standardized interface for managing the composition of any part of 4DSSE. There are a few rules when using this class.

  • Only species in the atomicSpecies.h database can be used. There are 1000s (All species from AME2020) in there so it should not be a problem.
  • Before a mass fraction can be set with a particular instance of Composition, the symbol must be registered. (i.e. register He-3 before setting its mass fraction)
  • Before any composition information can be retrived (e.g. getComposition), the composition must be finalized (call to .finalize()). This checks if the total mass fraction sums to approximatly 1 (within 1 part in 10^8)
  • Any changes made to the composition after finalization will "unfinalize" the composition. This means that the composition must be finalized again before any information can be retrived.
  • The mass fraction of any individual species must be no more than 1 and no less than 0.
  • The only exception to the finalize rule is if the compositon was constructed with symbols and mass fractions at instantiation time. In this case, the composition is automatically finalized.
    however, this means that the composition passed to the constructor must be valid.

Example Usage: Constructing a finalized composition with symbols and mass fractions:

std::vector<std::string> symbols = {"H", "He"};
std::vector<double> mass_fractions = {0.7, 0.3};
Composition comp(symbols, mass_fractions);
Composition()=default
Default constructor.

Example Usage: Constructing a composition with symbols and finalizing it later:

std::vector<std::string> symbols = {"H", "He"};
Composition comp(symbols);
comp.setComposition("H", 0.7);
comp.setComposition("He", 0.3);
comp.finalize();
Manages the composition of elements.
Examples
/Users/tboudreaux/Programming/SERiF/src/eos/public/EOS.h.

Definition at line 216 of file composition.h.

Constructor & Destructor Documentation

◆ Composition() [1/5]

serif::composition::Composition::Composition ( )
default

Default constructor.

◆ ~Composition()

serif::composition::Composition::~Composition ( )
default

Default destructor.

◆ Composition() [2/5]

serif::composition::Composition::Composition ( const std::vector< std::string > & symbols)
explicit

Constructs a Composition with the given symbols.

Parameters
symbolsThe symbols to initialize the composition with. Example Usage:
std::vector<std::string> symbols = {"H", "O"};
Composition comp(symbols);

Definition at line 146 of file composition.cpp.

◆ Composition() [3/5]

serif::composition::Composition::Composition ( const std::set< std::string > & symbols)
explicit

Constructs a Composition with the given symbols as a set.

Parameters
symbolsThe symbols to initialize the composition with. Example Usage:
std::set<std::string> symbols = {"H", "O"};
Composition comp(symbols);

Definition at line 152 of file composition.cpp.

◆ Composition() [4/5]

serif::composition::Composition::Composition ( const std::vector< std::string > & symbols,
const std::vector< double > & mass_fractions,
bool massFracMode = true )

Constructs a Composition with the given symbols and mass fractions.

Parameters
symbolsThe symbols to initialize the composition with.
mass_fractionsThe mass fractions corresponding to the symbols.
massFracModeTrue if mass fraction mode, false if number fraction mode. Example Usage:
std::vector<std::string> symbols = {"H", "O"};
std::vector<double> mass_fractions = {0.1, 0.9};
Composition comp(symbols, mass_fractions);

Definition at line 158 of file composition.cpp.

◆ Composition() [5/5]

serif::composition::Composition::Composition ( const Composition & composition)

Constructs a Composition from another Composition.

Parameters
compositionThe Composition to copy.

Definition at line 180 of file composition.cpp.

Member Function Documentation

◆ contains()

bool serif::composition::Composition::contains ( const serif::atomic::Species & isotope) const

Definition at line 644 of file composition.cpp.

◆ finalize()

bool serif::composition::Composition::finalize ( bool norm = false)

Finalizes the composition.

Parameters
normIf true, the composition will be normalized to sum to 1 [Default False]
Returns
True if the composition is successfully finalized, false otherwise.

Definition at line 338 of file composition.cpp.

◆ finalizeMassFracMode()

bool serif::composition::Composition::finalizeMassFracMode ( bool norm)
private

Finalizes the composition in mass fraction mode.

Parameters
normIf true, the composition will be normalized to sum to 1.
Returns
True if the composition is successfully finalized, false otherwise.

Definition at line 351 of file composition.cpp.

◆ finalizeNumberFracMode()

bool serif::composition::Composition::finalizeNumberFracMode ( bool norm)
private

Finalizes the composition in number fraction mode.

Parameters
normIf true, the composition will be normalized to sum to 1.
Returns
True if the composition is successfully finalized, false otherwise.

Definition at line 387 of file composition.cpp.

◆ getCanonicalComposition()

CanonicalComposition serif::composition::Composition::getCanonicalComposition ( bool harsh = false) const
nodiscard

Gets the current canonical composition (X, Y, Z).

Parameters
harshIf true, this will throw an error if X-Y != Z where Z is computed as the sum of all other elements.
Returns
True if mass fraction mode, false if number fraction mode.
Exceptions
std::runtime_errorif the composition is not finalized or if the canonical composition cannot be computed.
std::runtime_errorif harsh is true and the canonical composition is not valid.

Definition at line 593 of file composition.cpp.

◆ getComposition() [1/2]

std::pair< std::unordered_map< std::string, CompositionEntry >, GlobalComposition > serif::composition::Composition::getComposition ( ) const
nodiscard

Gets all composition entries and the global composition.

Returns
A pair containing an unordered map of CompositionEntries and the GlobalComposition.

Definition at line 510 of file composition.cpp.

◆ getComposition() [2/2]

std::pair< CompositionEntry, GlobalComposition > serif::composition::Composition::getComposition ( const std::string & symbol) const
nodiscard

Gets the composition entry and global composition for a given symbol.

Parameters
symbolThe symbol to get the composition for.
Returns
A pair containing the CompositionEntry and GlobalComposition for the given symbol.

Definition at line 498 of file composition.cpp.

◆ getMassFraction() [1/2]

std::unordered_map< std::string, double > serif::composition::Composition::getMassFraction ( ) const
nodiscard

Gets the mass fractions of all compositions.

Returns
An unordered map of compositions with their mass fractions.

Definition at line 465 of file composition.cpp.

◆ getMassFraction() [2/2]

double serif::composition::Composition::getMassFraction ( const std::string & symbol) const
nodiscard

Gets the mass fraction for a given symbol.

Parameters
symbolThe symbol to get the mass fraction for.
Returns
The mass fraction for the given symbol.

Definition at line 449 of file composition.cpp.

◆ getMeanAtomicNumber()

double serif::composition::Composition::getMeanAtomicNumber ( ) const
nodiscard

Compute the mean atomic mass number of the composition.

Returns
Mean atomic mass number.

Definition at line 526 of file composition.cpp.

◆ getMeanParticleMass()

double serif::composition::Composition::getMeanParticleMass ( ) const
nodiscard

Compute the mean particle mass of the composition.

Returns
Mean particle mass in g.

Definition at line 518 of file composition.cpp.

◆ getNumberFraction() [1/2]

std::unordered_map< std::string, double > serif::composition::Composition::getNumberFraction ( ) const
nodiscard

Gets the number fractions of all compositions.

Returns
An unordered map of compositions with their number fractions.

Definition at line 490 of file composition.cpp.

◆ getNumberFraction() [2/2]

double serif::composition::Composition::getNumberFraction ( const std::string & symbol) const
nodiscard

Gets the number fraction for a given symbol.

Parameters
symbolThe symbol to get the number fraction for.
Returns
The number fraction for the given symbol.

Definition at line 474 of file composition.cpp.

◆ getRegisteredSymbols()

std::set< std::string > serif::composition::Composition::getRegisteredSymbols ( ) const
nodiscard

Gets the registered symbols.

Returns
A set of registered symbols.

Definition at line 236 of file composition.cpp.

◆ hasSymbol()

bool serif::composition::Composition::hasSymbol ( const std::string & symbol) const

Check if a symbol is registered.

Parameters
symbolThe symbol to check.
Returns
True if the symbol is registered, false otherwise.

Definition at line 640 of file composition.cpp.

◆ isValidComposition()

bool serif::composition::Composition::isValidComposition ( const std::vector< double > & fractions) const
private

Checks if the given mass fractions are valid.

Parameters
mass_fractionsThe mass fractions to check.
Returns
True if the mass fractions are valid, false otherwise.

Definition at line 247 of file composition.cpp.

◆ isValidSymbol()

bool serif::composition::Composition::isValidSymbol ( const std::string & symbol)
staticprivate

Checks if the given symbol is valid.

A symbol is valid if it is in the atomic species database (species in atomicSpecies.h). These include all the isotopes from AME2020.

Parameters
symbolThe symbol to check.
Returns
True if the symbol is valid, false otherwise.

Definition at line 260 of file composition.cpp.

◆ mix()

Composition serif::composition::Composition::mix ( const Composition & other,
double fraction ) const

Mix two compositions together with a given fraction.

Parameters
otherThe other composition to mix with.
fractionThe fraction of the other composition to mix with. This is the fraction of the other composition wrt. to the current. i.e. fraction=1 would mean that 50% of the new composition is from the other and 50% from the current).

Definition at line 420 of file composition.cpp.

◆ operator+()

Composition serif::composition::Composition::operator+ ( const Composition & other) const

Overloads the + operator to mix two compositions together with a fraction of 0.5.

OVERLOADS.

Parameters
otherThe other composition to mix with.
Returns
The mixed composition.

Definition at line 659 of file composition.cpp.

◆ operator=()

Composition & serif::composition::Composition::operator= ( Composition const & other)

Definition at line 189 of file composition.cpp.

◆ registerSymbol() [1/2]

void serif::composition::Composition::registerSymbol ( const std::string & symbol,
bool massFracMode = true )

Registers a new symbol.

Parameters
symbolThe symbol to register.
massFracModeTrue if mass fraction mode, false if number fraction mode. Example Usage:
comp.registerSymbol("H");

Definition at line 203 of file composition.cpp.

◆ registerSymbol() [2/2]

void serif::composition::Composition::registerSymbol ( const std::vector< std::string > & symbols,
bool massFracMode = true )

Registers multiple new symbols.

Parameters
symbolsThe symbols to register.
massFracModeTrue if mass fraction mode, false if number fraction mode. Example Usage:
std::vector<std::string> symbols = {"H", "O"};
comp.registerSymbol(symbols);

Definition at line 230 of file composition.cpp.

◆ setCompositionMode()

void serif::composition::Composition::setCompositionMode ( bool massFracMode)

Sets the composition mode.

Parameters
massFracModeTrue if mass fraction mode, false if number fraction mode.

Definition at line 572 of file composition.cpp.

◆ setMassFraction() [1/2]

double serif::composition::Composition::setMassFraction ( const std::string & symbol,
const double & mass_fraction )

Sets the mass fraction for a given symbol.

Parameters
symbolThe symbol to set the mass fraction for.
mass_fractionThe mass fraction to set.
Returns
The mass fraction that was set. Example Usage:
comp.setMassFraction("H", 0.1);

Definition at line 264 of file composition.cpp.

◆ setMassFraction() [2/2]

std::vector< double > serif::composition::Composition::setMassFraction ( const std::vector< std::string > & symbols,
const std::vector< double > & mass_fractions )

Sets the mass fraction for multiple symbols.

Parameters
symbolsThe symbols to set the mass fraction for.
mass_fractionsThe mass fractions corresponding to the symbols.
Returns
A vector of mass fractions that were set. Example Usage:
std::vector<std::string> symbols = {"H", "O"};
std::vector<double> mass_fractions = {0.1, 0.9};
comp.setMassFraction(symbols, mass_fractions);

Definition at line 287 of file composition.cpp.

◆ setNumberFraction() [1/2]

double serif::composition::Composition::setNumberFraction ( const std::string & symbol,
const double & number_fraction )

Sets the number fraction for a given symbol.

Parameters
symbolThe symbol to set the number fraction for.
number_fractionThe number fraction to set.
Returns
The number fraction that was set.

Definition at line 301 of file composition.cpp.

◆ setNumberFraction() [2/2]

std::vector< double > serif::composition::Composition::setNumberFraction ( const std::vector< std::string > & symbols,
const std::vector< double > & number_fractions )

Sets the number fraction for multiple symbols.

Parameters
symbolsThe symbols to set the number fraction for.
number_fractionsThe number fractions corresponding to the symbols.
Returns
A vector of number fractions that were set.

Definition at line 324 of file composition.cpp.

◆ subset()

Composition serif::composition::Composition::subset ( const std::vector< std::string > & symbols,
std::string method = "norm" ) const

Gets a subset of the composition.

Parameters
symbolsThe symbols to include in the subset.
methodThe method to use for the subset (default is "norm").
Returns
A Composition object containing the subset.

Definition at line 543 of file composition.cpp.

◆ validateComposition()

void serif::composition::Composition::validateComposition ( const std::vector< double > & fractions) const
private

Validates the given mass fractions.

Parameters
mass_fractionsThe mass fractions to validate.
Exceptions
std::invalid_argumentif the mass fractions are invalid.

Definition at line 240 of file composition.cpp.

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const Composition & composition )
friend

Overloaded output stream operator for Composition.

Parameters
osThe output stream.
compositionThe Composition to output.
Returns
The output stream.

Definition at line 675 of file composition.cpp.

Member Data Documentation

◆ m_compositions

std::unordered_map<std::string, CompositionEntry> serif::composition::Composition::m_compositions
private

The compositions.

Definition at line 228 of file composition.h.

◆ m_config

serif::config::Config& serif::composition::Composition::m_config = serif::config::Config::getInstance()
private

Definition at line 218 of file composition.h.

◆ m_finalized

bool serif::composition::Composition::m_finalized = false
private

True if the composition is finalized.

Definition at line 222 of file composition.h.

◆ m_logger

quill::Logger* serif::composition::Composition::m_logger = m_logManager.getLogger("log")
private

Definition at line 220 of file composition.h.

◆ m_logManager

serif::probe::LogManager& serif::composition::Composition::m_logManager = serif::probe::LogManager::getInstance()
private

Definition at line 219 of file composition.h.

◆ m_massFracMode

bool serif::composition::Composition::m_massFracMode = true
private

True if mass fraction mode, false if number fraction mode.

Definition at line 225 of file composition.h.

◆ m_meanParticleMass

double serif::composition::Composition::m_meanParticleMass = 0.0
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 224 of file composition.h.

◆ m_registeredSymbols

std::set<std::string> serif::composition::Composition::m_registeredSymbols
private

The registered symbols.

Definition at line 227 of file composition.h.

◆ m_specificNumberDensity

double serif::composition::Composition::m_specificNumberDensity = 0.0
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 223 of file composition.h.


The documentation for this class was generated from the following files: