Loading...
Searching...
No Matches
composition_masked.cpp
Go to the documentation of this file.
#include "fourdst/composition/decorators/composition_masked.h"
#include "fourdst/composition/exceptions/exceptions_composition.h"
#include "fourdst/atomic/species.h"
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
#include <set>
#include <unordered_map>
#include "fourdst/composition/utils/composition_hash.h"
namespace fourdst::composition {
MaskedComposition::MaskedComposition(
const CompositionAbstract& baseComposition,
const std::vector<atomic::Species>& activeSpecies
) :
CompositionDecorator(baseComposition.clone()),
m_activeSpecies(activeSpecies) {
std::ranges::sort(m_activeSpecies, [](const auto &a, const auto &b) {
return a < b;
});
m_molarAbundances.reserve(m_activeSpecies.size());
for (const auto& species : m_activeSpecies) {
if (!CompositionDecorator::contains(species)) {
m_molarAbundances.push_back(0.0);
} else {
m_molarAbundances.push_back(CompositionDecorator::getMolarAbundance(species));
}
}
}
bool MaskedComposition::contains(const atomic::Species &species) const noexcept{
return std::ranges::contains(m_activeSpecies, species);
}
bool MaskedComposition::contains(const std::string &symbol) const {
if (!atomic::species.contains(symbol)) {
throw exceptions::UnknownSymbolError("Cannot find species '" + symbol + "' in base composition");
}
const atomic::Species& species = atomic::species.at(symbol);
return contains(species);
}
const std::vector<atomic::Species>& MaskedComposition::getRegisteredSpecies() const noexcept {
return m_activeSpecies;
}
std::set<std::string> MaskedComposition::getRegisteredSymbols() const noexcept {
std::set<std::string> symbols;
for (const auto& species : m_activeSpecies) {
symbols.insert(std::string(species.name()));
}
return symbols;
}
size_t MaskedComposition::size() const noexcept {
return m_activeSpecies.size();
}
std::unordered_map<atomic::Species, double> MaskedComposition::getMassFraction() const noexcept {
std::unordered_map<atomic::Species, double> massFractions;
for (const auto& species : m_activeSpecies) {
if (CompositionDecorator::contains(species)) {
massFractions[species] = CompositionDecorator::getMassFraction(species);
} else {
massFractions[species] = 0.0;
}
}
return massFractions;
}
std::unordered_map<atomic::Species, double> MaskedComposition::getNumberFraction() const noexcept {
std::unordered_map<atomic::Species, double> numberFractions;
for (const auto& species : m_activeSpecies) {
if (CompositionDecorator::contains(species)) {
numberFractions[species] = CompositionDecorator::getNumberFraction(species);
} else {
numberFractions[species] = 0.0;
}
}
return numberFractions;
}
double MaskedComposition::getMassFraction(const std::string &symbol) const {
if (!contains(symbol)) {
throw exceptions::UnregisteredSymbolError("Species '" + symbol + "' is not part of the active species in the MaskedComposition.");
}
if (CompositionDecorator::contains(symbol)) {
return CompositionDecorator::getMassFraction(symbol);
} return 0.0;
}
double MaskedComposition::getMassFraction(const atomic::Species &species) const {
if (!contains(species)) {
throw exceptions::UnregisteredSymbolError("Species '" + std::string(species.name()) + "' is not part of the active species in the MaskedComposition.");
}
if (CompositionDecorator::contains(species)) {
return CompositionDecorator::getMassFraction(species);
} return 0.0;
}
double MaskedComposition::getNumberFraction(const std::string &symbol) const {
if (!contains(symbol)) {
throw exceptions::UnregisteredSymbolError("Species '" + symbol + "' is not part of the active species in the MaskedComposition.");
}
if (CompositionDecorator::contains(symbol)) {
return CompositionDecorator::getNumberFraction(symbol);
} return 0.0;
}
double MaskedComposition::getNumberFraction(const atomic::Species &species) const {
if (!contains(species)) {
throw exceptions::UnregisteredSymbolError("Species '" + std::string(species.name()) + "' is not part of the active species in the MaskedComposition.");
}
if (CompositionDecorator::contains(species)) {
return CompositionDecorator::getNumberFraction(species);
} return 0.0;
}
double MaskedComposition::getMolarAbundance(const std::string &symbol) const {
if (!contains(symbol)) {
throw exceptions::UnregisteredSymbolError("Species '" + symbol + "' is not part of the active species in the MaskedComposition.");
}
if (CompositionDecorator::contains(symbol)) {
return CompositionDecorator::getMolarAbundance(symbol);
} return 0.0;
}
double MaskedComposition::getMolarAbundance(const atomic::Species &species) const {
if (!contains(species)) {
throw exceptions::UnregisteredSymbolError("Species '" + std::string(species.name()) + "' is not part of the active species in the MaskedComposition.");
}
if (CompositionDecorator::contains(species)) {
return CompositionDecorator::getMolarAbundance(species);
} return 0.0;
}
double MaskedComposition::getMeanParticleMass() const noexcept {
double meanParticleMass = 0.0;
for (const auto& species : m_activeSpecies) {
if (CompositionDecorator::contains(species)) {
const double numberFraction = CompositionDecorator::getNumberFraction(species);
const double atomicMass = species.mass();
meanParticleMass += numberFraction * atomicMass;
}
}
return meanParticleMass;
}
double MaskedComposition::getElectronAbundance() const noexcept {
double Ye = 0.0;
for (const auto& species : m_activeSpecies) {
if (CompositionDecorator::contains(species)) {
Ye += CompositionDecorator::getMolarAbundance(species) * species.z();
}
}
return Ye;
}
std::vector<double> MaskedComposition::getMassFractionVector() const noexcept {
std::vector<double> massFractions;
massFractions.reserve(m_activeSpecies.size());
for (const auto& species : m_activeSpecies) {
massFractions.push_back(getMassFraction(species));
}
return massFractions;
}
std::vector<double> MaskedComposition::getNumberFractionVector() const noexcept {
std::vector<double> numberFractions;
numberFractions.reserve(m_activeSpecies.size());
for (const auto& species : m_activeSpecies) {
numberFractions.push_back(getNumberFraction(species));
}
return numberFractions;
}
std::vector<double> MaskedComposition::getMolarAbundanceVector() const noexcept {
std::vector<double> molarAbundances;
molarAbundances.reserve(m_activeSpecies.size());
for (const auto& species : m_activeSpecies) {
molarAbundances.push_back(getMolarAbundance(species));
}
return molarAbundances;
}
size_t MaskedComposition::getSpeciesIndex(const std::string &symbol) const {
if (!contains(symbol)) {
throw exceptions::UnregisteredSymbolError("Species '" + symbol + "' is not part of the active species in the MaskedComposition.");
}
return std::distance(
m_activeSpecies.begin(),
std::ranges::find_if(m_activeSpecies,
[&symbol](const atomic::Species& sp) {
return std::string(sp.name()) == symbol;
}));
}
size_t MaskedComposition::getSpeciesIndex(const atomic::Species &species) const {
return std::distance(
m_activeSpecies.begin(),
std::ranges::find(m_activeSpecies, species)
);
}
atomic::Species MaskedComposition::getSpeciesAtIndex(const size_t index) const {
if (index >= m_activeSpecies.size()) {
throw std::out_of_range("Index " + std::to_string(index) + " is out of bounds for active species of size " + std::to_string(m_activeSpecies.size()) + ".");
}
auto it = m_activeSpecies.begin();
std::advance(it, index);
return *it;
}
std::unique_ptr<CompositionAbstract> MaskedComposition::clone() const {
return std::make_unique<MaskedComposition>(*m_base_composition, m_activeSpecies);
}
MaskedComposition::iterator MaskedComposition::begin() {
return {m_activeSpecies.begin(), m_molarAbundances.begin()};
}
MaskedComposition::iterator MaskedComposition::end() {
return {m_activeSpecies.end(), m_molarAbundances.end()};
}
MaskedComposition::const_iterator MaskedComposition::begin() const {
return {m_activeSpecies.cbegin(), m_molarAbundances.cbegin()};
}
MaskedComposition::const_iterator MaskedComposition::end() const {
return {m_activeSpecies.cend(), m_molarAbundances.cend()};
}
size_t MaskedComposition::hash() const {
return utils::CompositionHash::hash_exact<MaskedComposition>(*this);
}
};
fourdst::composition::CompositionAbstract
Abstract base class for chemical composition representations.
Definition composition_abstract.h:36
fourdst::composition::CompositionDecorator::getMolarAbundance
double getMolarAbundance(const std::string &symbol) const override
Get the molar abundance for a given symbol.
Definition composition_decorator_abstract.h:30
fourdst::composition::CompositionDecorator::CompositionDecorator
CompositionDecorator(std::unique_ptr< CompositionAbstract > decorator)
Definition composition_decorator_abstract.h:18
fourdst::composition::CompositionDecorator::getNumberFraction
std::unordered_map< atomic::Species, double > getNumberFraction() const noexcept override
Get the number fraction for all registered symbols.
Definition composition_decorator_abstract.h:25
fourdst::composition::CompositionDecorator::m_base_composition
std::unique_ptr< CompositionAbstract > m_base_composition
Definition composition_decorator_abstract.h:47
fourdst::composition::CompositionDecorator::contains
bool contains(const atomic::Species &species) const noexcept override
Check if the composition contains the given species.
Definition composition_decorator_abstract.h:19
fourdst::composition::CompositionDecorator::getMassFraction
std::unordered_map< atomic::Species, double > getMassFraction() const noexcept override
Get the mass fraction for all registered symbols.
Definition composition_decorator_abstract.h:24
fourdst::composition::MaskedComposition::contains
bool contains(const atomic::Species &species) const noexcept override
Check if the composition contains the given species.
Definition composition_masked.cpp:37
fourdst::composition::MaskedComposition::hash
size_t hash() const override
Definition composition_masked.cpp:234
fourdst::composition::MaskedComposition::m_activeSpecies
std::vector< atomic::Species > m_activeSpecies
Definition composition_masked.h:65
fourdst::composition::MaskedComposition::getMassFraction
std::unordered_map< atomic::Species, double > getMassFraction() const noexcept override
Get the mass fraction for all registered symbols.
Definition composition_masked.cpp:65
fourdst::composition::MaskedComposition::getNumberFraction
std::unordered_map< atomic::Species, double > getNumberFraction() const noexcept override
Get the number fraction for all registered symbols.
Definition composition_masked.cpp:77
fourdst::composition::MaskedComposition::getMolarAbundance
double getMolarAbundance(const std::string &symbol) const override
Get the molar abundance for a given symbol.
Definition composition_masked.cpp:121
fourdst::composition::MaskedComposition::clone
std::unique_ptr< CompositionAbstract > clone() const override
Definition composition_masked.cpp:214
fourdst::composition::MaskedComposition::getElectronAbundance
double getElectronAbundance() const noexcept override
Get the electron abundance of the composition.
Definition composition_masked.cpp:149
fourdst::composition::MaskedComposition::getMeanParticleMass
double getMeanParticleMass() const noexcept override
Get the mean particle mass of the composition.
Definition composition_masked.cpp:137
fourdst::composition::MaskedComposition::getRegisteredSpecies
const std::vector< atomic::Species > & getRegisteredSpecies() const noexcept override
Get all registered atomic species in the composition.
Definition composition_masked.cpp:49
fourdst::composition::MaskedComposition::size
size_t size() const noexcept override
Definition composition_masked.cpp:61
fourdst::composition::MaskedComposition::getSpeciesIndex
size_t getSpeciesIndex(const std::string &symbol) const override
Get the index of a species by symbol.
Definition composition_masked.cpp:186
fourdst::composition::MaskedComposition::getMassFractionVector
std::vector< double > getMassFractionVector() const noexcept override
Get the mass fraction as a vector.
Definition composition_masked.cpp:159
fourdst::composition::MaskedComposition::MaskedComposition
MaskedComposition(const CompositionAbstract &baseComposition, const std::vector< atomic::Species > &activeSpecies)
Definition composition_masked.cpp:15
fourdst::composition::MaskedComposition::getRegisteredSymbols
std::set< std::string > getRegisteredSymbols() const noexcept override
Get all registered chemical symbols in the composition.
Definition composition_masked.cpp:53
fourdst::composition::MaskedComposition::begin
iterator begin() override
Definition composition_masked.cpp:218
fourdst::composition::MaskedComposition::end
iterator end() override
Definition composition_masked.cpp:222
fourdst::composition::MaskedComposition::m_molarAbundances
std::vector< double > m_molarAbundances
Definition composition_masked.h:66
fourdst::composition::MaskedComposition::getMolarAbundanceVector
std::vector< double > getMolarAbundanceVector() const noexcept override
Get the molar abundance as a vector.
Definition composition_masked.cpp:177
fourdst::composition::MaskedComposition::getSpeciesAtIndex
atomic::Species getSpeciesAtIndex(size_t index) const override
Get the species at a given index.
Definition composition_masked.cpp:205
fourdst::composition::MaskedComposition::const_iterator
detail::CompositionIterator< true > const_iterator
Definition composition_masked.h:18
fourdst::composition::MaskedComposition::getNumberFractionVector
std::vector< double > getNumberFractionVector() const noexcept override
Get the number fraction as a vector.
Definition composition_masked.cpp:168
fourdst::composition::MaskedComposition::iterator
detail::CompositionIterator< false > iterator
Definition composition_masked.h:17
fourdst::composition::exceptions::UnknownSymbolError
Exception thrown when an unknown symbol is encountered.
Definition exceptions_composition.h:78
fourdst::composition::exceptions::UnregisteredSymbolError
Exception thrown when a symbol is used that has not been registered.
Definition exceptions_composition.h:52
composition_hash.h
composition_masked.h
exceptions_composition.h
fourdst::atomic::species
static const std::unordered_map< std::string, const Species & > species
Map of species names to their corresponding Species objects.
Definition species.h:3579
fourdst::composition
Utilities and types for representing and manipulating chemical compositions.
species.h
fourdst::atomic::Species
Represents an atomic species (isotope) with its fundamental physical properties.
Definition atomicSpecies.h:52
fourdst::atomic::Species::name
std::string_view name() const
Gets the name of the species.
Definition atomicSpecies.h:211
fourdst::composition::utils::CompositionHash::hash_exact
static uint64_t hash_exact(const CompositionT &comp)
Definition composition_hash.h:18