Loading...
Searching...
No Matches
composition.h
Go to the documentation of this file.
/* ***********************************************************************
//
// Copyright (C) 2025 -- The 4D-STAR Collaboration
// File Author: Emily Boudreaux
// Last Modified: March 26, 2025
//
// 4DSSE is free software; you can use it and/or modify
// it under the terms and restrictions the GNU General Library Public
// License version 3 (GPLv3) as published by the Free Software Foundation.
//
// 4DSSE is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with this software; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// *********************************************************************** */
#pragma once
#include <string>
#include <unordered_map>
#include <set>
#include <optional>
#include <unordered_set>
#include <expected>
#include "fourdst/config/config.h"
#include "fourdst/logging/logging.h"
#include "fourdst/composition/composition_abstract.h"
#include "fourdst/atomic/atomicSpecies.h"
namespace fourdst::composition {
struct CanonicalComposition {
double X = 0.0;
double Y = 0.0;
double Z = 0.0;
friend std::ostream& operator<<(std::ostream& os, const CanonicalComposition& composition) {
os << "<CanonicalComposition: "
<< "X = " << composition.X << ", "
<< "Y = " << composition.Y << ", "
<< "Z = " << composition.Z << ">";
return os;
}
};
// ReSharper disable once CppClassCanBeFinal
class Composition final : public CompositionAbstract {
public:
using iterator = detail::CompositionIterator<false>;
using const_iterator = detail::CompositionIterator<true>;
private:
struct CompositionCache {
std::optional<CanonicalComposition> canonicalComp;
std::optional<std::vector<double>> massFractions;
std::optional<std::vector<double>> numberFractions;
std::optional<std::vector<double>> molarAbundances;
std::optional<std::vector<atomic::Species>> sortedSpecies;
std::optional<std::vector<std::string>> sortedSymbols;
std::optional<double> Ye;
std::optional<std::size_t> hash;
void clear() {
canonicalComp = std::nullopt;
massFractions = std::nullopt;
numberFractions = std::nullopt;
molarAbundances = std::nullopt;
sortedSymbols = std::nullopt;
sortedSpecies = std::nullopt;
Ye = std::nullopt;
hash = std::nullopt;
}
[[nodiscard]] bool is_clear() const {
return !canonicalComp.has_value() && !massFractions.has_value() &&
!numberFractions.has_value() && !molarAbundances.has_value() && !sortedSymbols.has_value() &&
!Ye.has_value() && !sortedSpecies.has_value() && !hash.has_value();
}
};
enum class SpeciesIndexLookupError : uint8_t {
NO_REGISTERED_SPECIES,
SPECIES_NOT_FOUND
};
private:
static quill::Logger* getLogger() {
static quill::Logger* logger = logging::LogManager::getInstance().getLogger("log");
return logger;
}
// std::set<atomic::Species> m_registeredSpecies; ///< Set of registered species in the composition.
// std::map<atomic::Species, double> m_molarAbundances; ///< Map of species to their molar abundances.
std::vector<atomic::Species> m_species;
std::vector<double> m_molarAbundances;
mutable CompositionCache m_cache;
private:
[[nodiscard]] std::expected<std::ptrdiff_t, SpeciesIndexLookupError> findSpeciesIndex(const atomic::Species &species) const noexcept;
[[nodiscard]] static std::vector<atomic::Species> symbolVectorToSpeciesVector(const std::vector<std::string>& symbols);
public:
Composition() = default;
~Composition() override = default;
explicit Composition(const std::vector<std::string>& symbols);
explicit Composition(const std::vector<atomic::Species>& species);
explicit Composition(const std::set<std::string>& symbols);
explicit Composition(const std::set<atomic::Species>& species);
explicit Composition(const std::unordered_set<std::string>& symbols);
explicit Composition(const std::unordered_set<atomic::Species>& species);
Composition(const std::vector<std::string>& symbols, const std::vector<double>& molarAbundances);
Composition(const std::vector<atomic::Species>& species, const std::vector<double>& molarAbundances);
Composition(const std::set<std::string>& symbols, const std::vector<double>& molarAbundances);
explicit Composition(const std::unordered_map<std::string, double>& symbolMolarAbundances);
explicit Composition(const std::map<std::string, double>& symbolMolarAbundances);
explicit Composition(const std::unordered_map<atomic::Species, double>& speciesMolarAbundances);
explicit Composition(const std::map<atomic::Species, double>& speciesMolarAbundances);
Composition(const Composition& composition);
explicit Composition(const CompositionAbstract& composition);
Composition& operator=(Composition const& other);
Composition& operator=(const CompositionAbstract& other);
void registerSymbol(const std::string& symbol);
void registerSymbol(const std::vector<std::string>& symbols);
void registerSpecies(const atomic::Species& species) noexcept;
void registerSpecies(const std::vector<atomic::Species>& species) noexcept;
[[nodiscard]] bool contains(const atomic::Species& species) const noexcept override;
[[nodiscard]] bool contains(const std::string& symbol) const override;
[[nodiscard]] size_t size() const noexcept override;
void setMolarAbundance(
const std::string& symbol,
const double& molar_abundance
);
void setMolarAbundance(
const atomic::Species& species,
const double& molar_abundance
);
void setMolarAbundance(
const std::vector<std::string>& symbols,
const std::vector<double>& molar_abundances
);
void setMolarAbundance(
const std::vector<atomic::Species>& species,
const std::vector<double>& molar_abundances
);
void setMolarAbundance(
const std::set<std::string>& symbols,
const std::vector<double>& molar_abundances
);
void setMolarAbundance(
const std::set<atomic::Species>& species,
const std::vector<double>& molar_abundances
);
[[nodiscard]] std::set<std::string> getRegisteredSymbols() const noexcept override;
[[nodiscard]] const std::vector<atomic::Species> &getRegisteredSpecies() const noexcept override;
[[nodiscard]] std::unordered_map<atomic::Species, double> getMassFraction() const noexcept override;
[[nodiscard]] double getMassFraction(const std::string& symbol) const override;
[[nodiscard]] double getMassFraction(const atomic::Species& species) const override;
[[nodiscard]] double getNumberFraction(const std::string& symbol) const override;
[[nodiscard]] double getNumberFraction(const atomic::Species& species) const override;
[[nodiscard]] std::unordered_map<atomic::Species, double> getNumberFraction() const noexcept override;
[[nodiscard]] double getMolarAbundance(const std::string& symbol) const override;
[[nodiscard]] double getMolarAbundance(const atomic::Species& species) const override;
[[nodiscard]] double getMeanParticleMass() const noexcept override;
[[nodiscard]] double getElectronAbundance() const noexcept override;
[[nodiscard]] CanonicalComposition getCanonicalComposition() const;
[[nodiscard]] std::vector<double> getMassFractionVector() const noexcept override;
[[nodiscard]] std::vector<double> getNumberFractionVector() const noexcept override;
[[nodiscard]] std::vector<double> getMolarAbundanceVector() const noexcept override;
[[nodiscard]] size_t getSpeciesIndex(const std::string& symbol) const override;
[[nodiscard]] size_t getSpeciesIndex(const atomic::Species& species) const override;
[[nodiscard]] atomic::Species getSpeciesAtIndex(size_t index) const override;
[[nodiscard]] std::unique_ptr<CompositionAbstract> clone() const override;
friend std::ostream& operator<<(std::ostream& os, const Composition& composition);
[[nodiscard]] iterator begin() override {
return {m_species.begin(), m_molarAbundances.begin()};
}
[[nodiscard]] const_iterator begin() const override {
return {m_species.cbegin(), m_molarAbundances.cbegin()};
}
[[nodiscard]] detail::CompositionIterator<false> end() override {
return {m_species.end(), m_molarAbundances.end()};
}
[[nodiscard]] detail::CompositionIterator<true> end() const override {
return {m_species.cend(), m_molarAbundances.cend()};
}
[[nodiscard]] std::size_t hash() const override;
};
inline bool operator==(const Composition& a, const Composition& b) noexcept {
if (a.size() != b.size()) return false;
if (a.getRegisteredSpecies() != b.getRegisteredSpecies())
return false;
return a.hash() == b.hash();
}
}; // namespace fourdst::composition
atomicSpecies.h
fourdst::composition::CompositionAbstract
Abstract base class for chemical composition representations.
Definition composition_abstract.h:36
fourdst::composition::Composition
Manages a collection of chemical species and their abundances.
Definition composition.h:99
fourdst::composition::Composition::m_cache
CompositionCache m_cache
Cache for computed properties to avoid redundant calculations.
Definition composition.h:167
fourdst::composition::Composition::~Composition
~Composition() override=default
Default destructor.
fourdst::composition::Composition::getSpeciesIndex
size_t getSpeciesIndex(const std::string &symbol) const override
get the index in the sorted vector representation for a given symbol
Definition composition.cpp:639
fourdst::composition::Composition::contains
bool contains(const atomic::Species &species) const noexcept override
Checks if a given species is present in the composition.
Definition composition.cpp:693
fourdst::composition::Composition::Composition
Composition()=default
Default constructor.
fourdst::composition::Composition::setMolarAbundance
void setMolarAbundance(const std::string &symbol, const double &molar_abundance)
Sets the molar abundance for a given symbol.
Definition composition.cpp:321
fourdst::composition::Composition::getNumberFraction
double getNumberFraction(const std::string &symbol) const override
Gets the number fraction for a given symbol. See the overload for species-based lookup for more detai...
Definition composition.cpp:469
fourdst::composition::Composition::const_iterator
detail::CompositionIterator< true > const_iterator
Definition composition.h:102
fourdst::composition::Composition::registerSpecies
void registerSpecies(const atomic::Species &species) noexcept
Registers a new species by extracting its symbol.
Definition composition.cpp:251
fourdst::composition::Composition::registerSymbol
void registerSymbol(const std::string &symbol)
Registers a new symbol for inclusion in the composition.
Definition composition.cpp:234
fourdst::composition::Composition::getRegisteredSymbols
std::set< std::string > getRegisteredSymbols() const noexcept override
Gets the registered symbols.
Definition composition.cpp:304
fourdst::composition::Composition::symbolVectorToSpeciesVector
static std::vector< atomic::Species > symbolVectorToSpeciesVector(const std::vector< std::string > &symbols)
Definition composition.cpp:725
fourdst::composition::Composition::getLogger
static quill::Logger * getLogger()
Gets the logger instance for the Composition class. This is static to ensure that all composition obj...
Definition composition.h:156
fourdst::composition::Composition::operator=
Composition & operator=(Composition const &other)
Assignment operator.
Definition composition.cpp:204
fourdst::composition::Composition::clone
std::unique_ptr< CompositionAbstract > clone() const override
Definition composition.cpp:226
fourdst::composition::Composition::hash
std::size_t hash() const override
Definition composition.cpp:684
fourdst::composition::Composition::begin
iterator begin() override
Returns an iterator to the beginning of the molar abundance map.
Definition composition.h:787
fourdst::composition::Composition::iterator
detail::CompositionIterator< false > iterator
Definition composition.h:101
fourdst::composition::Composition::getElectronAbundance
double getElectronAbundance() const noexcept override
Compute the electron abundance of the composition.
Definition composition.cpp:543
fourdst::composition::Composition::size
size_t size() const noexcept override
Gets the number of registered species in the composition.
Definition composition.cpp:709
fourdst::composition::Composition::getMassFraction
std::unordered_map< atomic::Species, double > getMassFraction() const noexcept override
Gets the mass fractions of all species in the composition.
Definition composition.cpp:460
fourdst::composition::Composition::m_species
std::vector< atomic::Species > m_species
Definition composition.h:164
fourdst::composition::Composition::end
detail::CompositionIterator< false > end() override
Returns an iterator to the end of the molar abundance map.
Definition composition.h:829
fourdst::composition::Composition::end
detail::CompositionIterator< true > end() const override
Returns a const iterator to the end of the molar abundance map.
Definition composition.h:850
fourdst::composition::Composition::getCanonicalComposition
CanonicalComposition getCanonicalComposition() const
Compute the canonical composition (X, Y, Z) of the composition.
Definition composition.cpp:552
fourdst::composition::Composition::m_molarAbundances
std::vector< double > m_molarAbundances
Definition composition.h:165
fourdst::composition::Composition::getMolarAbundanceVector
std::vector< double > getMolarAbundanceVector() const noexcept override
Get a uniform vector representation of the molar abundances stored in the composition object sorted s...
Definition composition.cpp:631
fourdst::composition::Composition::getMolarAbundance
double getMolarAbundance(const std::string &symbol) const override
Gets the molar abundances of all species in the composition.
Definition composition.cpp:504
fourdst::composition::Composition::findSpeciesIndex
std::expected< std::ptrdiff_t, SpeciesIndexLookupError > findSpeciesIndex(const atomic::Species &species) const noexcept
Definition composition.cpp:713
fourdst::composition::Composition::begin
const_iterator begin() const override
Returns a const iterator to the beginning of the molar abundance map.
Definition composition.h:808
fourdst::composition::Composition::getNumberFractionVector
std::vector< double > getNumberFractionVector() const noexcept override
Get a uniform vector representation of the number fractions stored in the composition object sorted s...
Definition composition.cpp:614
fourdst::composition::Composition::getSpeciesAtIndex
atomic::Species getSpeciesAtIndex(size_t index) const override
Get the species at a given index in the sorted vector representation.
Definition composition.cpp:668
fourdst::composition::Composition::SpeciesIndexLookupError
SpeciesIndexLookupError
Definition composition.h:146
fourdst::composition::Composition::SpeciesIndexLookupError::SPECIES_NOT_FOUND
@ SPECIES_NOT_FOUND
Definition composition.h:148
fourdst::composition::Composition::SpeciesIndexLookupError::NO_REGISTERED_SPECIES
@ NO_REGISTERED_SPECIES
Definition composition.h:147
fourdst::composition::Composition::getRegisteredSpecies
const std::vector< atomic::Species > & getRegisteredSpecies() const noexcept override
Get a set of all species that are registered in the composition.
Definition composition.cpp:312
fourdst::composition::Composition::getMassFractionVector
std::vector< double > getMassFractionVector() const noexcept override
Get a uniform vector representation of the mass fraction stored in the composition object sorted such...
Definition composition.cpp:596
fourdst::composition::Composition::getMeanParticleMass
double getMeanParticleMass() const noexcept override
Compute the mean particle mass of the composition.
Definition composition.cpp:530
fourdst::composition::detail::CompositionIterator
Definition composition_abstract_iterator.h:13
composition_abstract.h
fourdst::atomic
Contains canonical information about atomic species and elements used by 4D-STAR.
fourdst::composition
Utilities and types for representing and manipulating chemical compositions.
fourdst::composition::operator==
bool operator==(const Composition &a, const Composition &b) noexcept
Definition composition.h:858
fourdst::atomic::Species
Represents an atomic species (isotope) with its fundamental physical properties.
Definition atomicSpecies.h:52
fourdst::composition::CanonicalComposition
Represents the canonical (X, Y, Z) composition of stellar material.
Definition composition.h:46
fourdst::composition::CanonicalComposition::operator<<
friend std::ostream & operator<<(std::ostream &os, const CanonicalComposition &composition)
Overloads the stream insertion operator for easy printing.
Definition composition.h:57
fourdst::composition::CanonicalComposition::Y
double Y
Mass fraction of Helium.
Definition composition.h:48
fourdst::composition::CanonicalComposition::X
double X
Mass fraction of Hydrogen.
Definition composition.h:47
fourdst::composition::CanonicalComposition::Z
double Z
Mass fraction of Metals.
Definition composition.h:49
fourdst::composition::Composition::CompositionCache
Caches computed properties of the composition to avoid redundant calculations.
Definition composition.h:111
fourdst::composition::Composition::CompositionCache::sortedSpecies
std::optional< std::vector< atomic::Species > > sortedSpecies
Cached vector of sorted species (by mass).
Definition composition.h:116
fourdst::composition::Composition::CompositionCache::numberFractions
std::optional< std::vector< double > > numberFractions
Cached vector of number fractions.
Definition composition.h:114
fourdst::composition::Composition::CompositionCache::canonicalComp
std::optional< CanonicalComposition > canonicalComp
Cached canonical composition data.
Definition composition.h:112
fourdst::composition::Composition::CompositionCache::hash
std::optional< std::size_t > hash
Definition composition.h:119
fourdst::composition::Composition::CompositionCache::molarAbundances
std::optional< std::vector< double > > molarAbundances
Cached vector of molar abundances.
Definition composition.h:115
fourdst::composition::Composition::CompositionCache::sortedSymbols
std::optional< std::vector< std::string > > sortedSymbols
Cached vector of sorted species (by mass).
Definition composition.h:117
fourdst::composition::Composition::CompositionCache::clear
void clear()
Clears all cached values.
Definition composition.h:124
fourdst::composition::Composition::CompositionCache::massFractions
std::optional< std::vector< double > > massFractions
Cached vector of mass fractions.
Definition composition.h:113
fourdst::composition::Composition::CompositionCache::Ye
std::optional< double > Ye
Cached electron abundance.
Definition composition.h:118
fourdst::composition::Composition::CompositionCache::is_clear
bool is_clear() const
Checks if the cache is clear (i.e., all cached values are empty).
Definition composition.h:139