fourdst::libcomposition v2.0.1
Robust atomic species information library
Loading...
Searching...
No Matches
composition.h
Go to the documentation of this file.
1/* ***********************************************************************
2//
3// Copyright (C) 2025 -- The 4D-STAR Collaboration
4// File Author: Emily Boudreaux
5// Last Modified: March 26, 2025
6//
7// 4DSSE is free software; you can use it and/or modify
8// it under the terms and restrictions the GNU General Library Public
9// License version 3 (GPLv3) as published by the Free Software Foundation.
10//
11// 4DSSE is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14// See the GNU Library General Public License for more details.
15//
16// You should have received a copy of the GNU Library General Public License
17// along with this software; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19//
20// *********************************************************************** */
21#pragma once
22
23#include <string>
24#include <unordered_map>
25#include <set>
26
27#include <optional>
28
29#include "fourdst/config/config.h"
30#include "fourdst/logging/logging.h"
33
34namespace fourdst::composition {
45 double X = 0.0;
46 double Y = 0.0;
47 double Z = 0.0;
48
55 friend std::ostream& operator<<(std::ostream& os, const CanonicalComposition& composition) {
56 os << "<CanonicalComposition: "
57 << "X = " << composition.X << ", "
58 << "Y = " << composition.Y << ", "
59 << "Z = " << composition.Z << ">";
60 return os;
61 }
62 };
63
96 // ReSharper disable once CppClassCanBeFinal
98 private:
107 std::optional<CanonicalComposition> canonicalComp;
108 std::optional<std::vector<double>> massFractions;
109 std::optional<std::vector<double>> numberFractions;
110 std::optional<std::vector<double>> molarAbundances;
111 std::optional<std::vector<atomic::Species>> sortedSpecies;
112 std::optional<std::vector<std::string>> sortedSymbols;
113 std::optional<double> Ye;
114
118 void clear() {
119 canonicalComp = std::nullopt;
120 massFractions = std::nullopt;
121 numberFractions = std::nullopt;
122 molarAbundances = std::nullopt;
123 sortedSymbols = std::nullopt;
124 sortedSpecies = std::nullopt;
125 Ye = std::nullopt;
126 }
127
132 [[nodiscard]] bool is_clear() const {
133 return !canonicalComp.has_value() && !massFractions.has_value() &&
134 !numberFractions.has_value() && !molarAbundances.has_value() && !sortedSymbols.has_value() &&
135 !Ye.has_value() && !sortedSpecies.has_value();
136 }
137 };
138 private:
144 static quill::Logger* getLogger() {
145 static quill::Logger* logger = logging::LogManager::getInstance().getLogger("log");
146 return logger;
147 }
148
149 std::set<atomic::Species> m_registeredSpecies;
150 std::map<atomic::Species, double> m_molarAbundances;
151
153
154 public:
160 Composition() = default;
161
165 ~Composition() override = default;
166
177 explicit Composition(const std::vector<std::string>& symbols);
178
192 explicit Composition(const std::vector<atomic::Species>& species);
193
204 explicit Composition(const std::set<std::string>& symbols);
205
219 explicit Composition(const std::set<atomic::Species>& species);
220
236 Composition(const std::vector<std::string>& symbols, const std::vector<double>& molarAbundances);
237
252 Composition(const std::vector<atomic::Species>& species, const std::vector<double>& molarAbundances);
253
269 Composition(const std::set<std::string>& symbols, const std::vector<double>& molarAbundances);
270
276
282 Composition& operator=(Composition const& other);
283
298 void registerSymbol(const std::string& symbol);
299
314 void registerSymbol(const std::vector<std::string>& symbols);
315
338 void registerSpecies(const atomic::Species& species) noexcept;
339
340
360 void registerSpecies(const std::vector<atomic::Species>& species) noexcept;
361
367 [[nodiscard]] bool contains(const atomic::Species& species) const noexcept override;
368
375 [[nodiscard]] bool contains(const std::string& symbol) const override;
376
381 [[nodiscard]] size_t size() const noexcept override;
382
400 const std::string& symbol,
401 const double& molar_abundance
402 );
403
424 const atomic::Species& species,
425 const double& molar_abundance
426 );
427
444 const std::vector<std::string>& symbols,
445 const std::vector<double>& molar_abundances
446 );
447
467 const std::vector<atomic::Species>& species,
468 const std::vector<double>& molar_abundances
469 );
470
488 const std::set<std::string>& symbols,
489 const std::vector<double>& molar_abundances
490 );
491
512 const std::set<atomic::Species>& species,
513 const std::vector<double>& molar_abundances
514 );
515
524 [[nodiscard]] std::set<std::string> getRegisteredSymbols() const noexcept override;
525
534 [[nodiscard]] const std::set<atomic::Species> &getRegisteredSpecies() const noexcept override;
535
542 [[nodiscard]] std::unordered_map<atomic::Species, double> getMassFraction() const noexcept override;
543
552 [[nodiscard]] double getMassFraction(const std::string& symbol) const override;
553
571 [[nodiscard]] double getMassFraction(const atomic::Species& species) const override;
572
581 [[nodiscard]] double getNumberFraction(const std::string& symbol) const override;
582
599 [[nodiscard]] double getNumberFraction(const atomic::Species& species) const override;
600
607 [[nodiscard]] std::unordered_map<atomic::Species, double> getNumberFraction() const noexcept override;
608
619 [[nodiscard]] double getMolarAbundance(const std::string& symbol) const override;
620
630 [[nodiscard]] double getMolarAbundance(const atomic::Species& species) const override;
631
645 [[nodiscard]] double getMeanParticleMass() const noexcept override;
646
660 [[nodiscard]] double getElectronAbundance() const noexcept override;
661
662
677 [[nodiscard]] CanonicalComposition getCanonicalComposition() const;
678
684 [[nodiscard]] std::vector<double> getMassFractionVector() const noexcept override;
685
691 [[nodiscard]] std::vector<double> getNumberFractionVector() const noexcept override;
692
698 [[nodiscard]] std::vector<double> getMolarAbundanceVector() const noexcept override;
699
708 [[nodiscard]] size_t getSpeciesIndex(const std::string& symbol) const override;
709
717 [[nodiscard]] size_t getSpeciesIndex(const atomic::Species& species) const override;
718
726 [[nodiscard]] atomic::Species getSpeciesAtIndex(size_t index) const override;
727
734 friend std::ostream& operator<<(std::ostream& os, const Composition& composition);
735
753 auto begin() {
754 return m_molarAbundances.begin();
755 }
756
774 [[nodiscard]] auto begin() const {
775 return m_molarAbundances.cbegin();
776 }
777
795 auto end() {
796 return m_molarAbundances.end();
797 }
798
816 [[nodiscard]] auto end() const {
817 return m_molarAbundances.cend();
818 }
819
820 };
821}; // namespace fourdst::composition
Abstract base class for chemical composition representations.
CompositionCache m_cache
Cache for computed properties to avoid redundant calculations.
~Composition() override=default
Default destructor.
size_t getSpeciesIndex(const std::string &symbol) const override
get the index in the sorted vector representation for a given symbol
bool contains(const atomic::Species &species) const noexcept override
Checks if a given species is present in the composition.
Composition()=default
Default constructor.
void setMolarAbundance(const std::string &symbol, const double &molar_abundance)
Sets the molar abundance for a given symbol.
const std::set< atomic::Species > & getRegisteredSpecies() const noexcept override
Get a set of all species that are registered in the composition.
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...
void registerSpecies(const atomic::Species &species) noexcept
Registers a new species by extracting its symbol.
void registerSymbol(const std::string &symbol)
Registers a new symbol for inclusion in the composition.
std::set< std::string > getRegisteredSymbols() const noexcept override
Gets the registered symbols.
std::set< atomic::Species > m_registeredSpecies
Set of registered species in the composition.
static quill::Logger * getLogger()
Gets the logger instance for the Composition class. This is static to ensure that all composition obj...
Composition & operator=(Composition const &other)
Assignment operator.
double getElectronAbundance() const noexcept override
Compute the electron abundance of the composition.
size_t size() const noexcept override
Gets the number of registered species in the composition.
std::unordered_map< atomic::Species, double > getMassFraction() const noexcept override
Gets the mass fractions of all species in the composition.
std::map< atomic::Species, double > m_molarAbundances
Map of species to their molar abundances.
CanonicalComposition getCanonicalComposition() const
Compute the canonical composition (X, Y, Z) of the composition.
auto begin()
Returns an iterator to the beginning of the molar abundance map.
std::vector< double > getMolarAbundanceVector() const noexcept override
Get a uniform vector representation of the molar abundances stored in the composition object sorted s...
double getMolarAbundance(const std::string &symbol) const override
Gets the molar abundances of all species in the composition.
auto end()
Returns an iterator to the end of the molar abundance map.
auto begin() const
Returns a const iterator to the beginning of the molar abundance map.
std::vector< double > getNumberFractionVector() const noexcept override
Get a uniform vector representation of the number fractions stored in the composition object sorted s...
atomic::Species getSpeciesAtIndex(size_t index) const override
Get the species at a given index in the sorted vector representation.
auto end() const
Returns a const iterator to the end of the molar abundance map.
std::vector< double > getMassFractionVector() const noexcept override
Get a uniform vector representation of the mass fraction stored in the composition object sorted such...
double getMeanParticleMass() const noexcept override
Compute the mean particle mass of the composition.
Contains canonical information about atomic species and elements used by 4D-STAR.
Utilities and types for representing and manipulating chemical compositions.
Represents an atomic species (isotope) with its fundamental physical properties.
Represents the canonical (X, Y, Z) composition of stellar material.
Definition composition.h:44
friend std::ostream & operator<<(std::ostream &os, const CanonicalComposition &composition)
Overloads the stream insertion operator for easy printing.
Definition composition.h:55
double Y
Mass fraction of Helium.
Definition composition.h:46
double X
Mass fraction of Hydrogen.
Definition composition.h:45
double Z
Mass fraction of Metals.
Definition composition.h:47
Caches computed properties of the composition to avoid redundant calculations.
std::optional< std::vector< atomic::Species > > sortedSpecies
Cached vector of sorted species (by mass).
std::optional< std::vector< double > > numberFractions
Cached vector of number fractions.
std::optional< CanonicalComposition > canonicalComp
Cached canonical composition data.
std::optional< std::vector< double > > molarAbundances
Cached vector of molar abundances.
std::optional< std::vector< std::string > > sortedSymbols
Cached vector of sorted species (by mass).
std::optional< std::vector< double > > massFractions
Cached vector of mass fractions.
std::optional< double > Ye
Cached electron abundance.
bool is_clear() const
Checks if the cache is clear (i.e., all cached values are empty).