SERiF 0.0.1a
3+1D Stellar Structure and Evolution
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 <iostream>
24#include <string>
25#include <unordered_map>
26#include <set>
27
28#include <utility>
29
30#include "probe.h"
31#include "config.h"
32
33#include "atomicSpecies.h"
34
35namespace serif::composition {
37 double X = 0.0;
38 double Y = 0.0;
39 double Z = 0.0;
40
41 friend std::ostream& operator<<(std::ostream& os, const CanonicalComposition& composition) {
42 os << "<CanonicalComposition: "
43 << "X = " << composition.X << ", "
44 << "Y = " << composition.Y << ", "
45 << "Z = " << composition.Z << ">";
46 return os;
47 }
48 };
49
56
57 // Overload the output stream operator for GlobalComposition
58 friend std::ostream& operator<<(std::ostream& os, const GlobalComposition& comp);
59 };
60
65 std::string m_symbol;
66 serif::atomic::Species m_isotope;
67 bool m_massFracMode = true;
68
69 double m_massFraction = 0.0;
70 double m_numberFraction = 0.0;
71 double m_relAbundance = 0.0;
72
73 bool m_initialized = false;
74
79
89 CompositionEntry(const std::string& symbol, bool massFracMode=true);
90
96
101 void setSpecies(const std::string& symbol);
102
107 std::string symbol() const;
108
113 double mass_fraction() const;
114
120 double mass_fraction(double meanMolarMass) const;
121
126 double number_fraction() const;
127
133 double number_fraction(double totalMoles) const;
134
139 double rel_abundance() const;
140
145 serif::atomic::Species isotope() const;
146
151 bool getMassFracMode() const;
152
157 void setMassFraction(double mass_fraction);
158
164
170 bool setMassFracMode(double meanMolarMass);
171
177 bool setNumberFracMode(double totalMoles);
178
185 friend std::ostream& operator<<(std::ostream& os, const CompositionEntry& entry);
186 };
187
217 private:
218 serif::config::Config& m_config = serif::config::Config::getInstance();
220 quill::Logger* m_logger = m_logManager.getLogger("log");
221
222 bool m_finalized = false;
224 double m_meanParticleMass = 0.0;
225 bool m_massFracMode = true;
226
227 std::set<std::string> m_registeredSymbols;
228 std::unordered_map<std::string, CompositionEntry> m_compositions;
229
236 static bool isValidSymbol(const std::string& symbol);
237
243 bool isValidComposition(const std::vector<double>& fractions) const;
244
250 void validateComposition(const std::vector<double>& fractions) const;
251
257 bool finalizeMassFracMode(bool norm);
258
264 bool finalizeNumberFracMode(bool norm);
265
266 public:
270 Composition() = default;
271
275 ~Composition() = default;
276
282 bool finalize(bool norm=false);
283
293 explicit Composition(const std::vector<std::string>& symbols);
294
304 explicit Composition(const std::set<std::string>& symbols);
305
318 Composition(const std::vector<std::string>& symbols, const std::vector<double>& mass_fractions, bool massFracMode=true);
319
325
326 Composition& operator=(Composition const& other);
327
338 void registerSymbol(const std::string& symbol, bool massFracMode=true);
339
351 void registerSymbol(const std::vector<std::string>& symbols, bool massFracMode=true);
352
357 [[nodiscard]] std::set<std::string> getRegisteredSymbols() const;
358
370 double setMassFraction(const std::string& symbol, const double& mass_fraction);
371
385 std::vector<double> setMassFraction(const std::vector<std::string>& symbols, const std::vector<double>& mass_fractions);
386
393 double setNumberFraction(const std::string& symbol, const double& number_fraction);
394
401 std::vector<double> setNumberFraction(const std::vector<std::string>& symbols, const std::vector<double>& number_fractions);
402
408 Composition mix(const Composition& other, double fraction) const;
409
414 [[nodiscard]] std::unordered_map<std::string, double> getMassFraction() const;
415
421 [[nodiscard]] double getMassFraction(const std::string& symbol) const;
422
428 [[nodiscard]] double getNumberFraction(const std::string& symbol) const;
429
434 [[nodiscard]] std::unordered_map<std::string, double> getNumberFraction() const;
435
441 [[nodiscard]] std::pair<CompositionEntry, GlobalComposition> getComposition(const std::string& symbol) const;
442
447 [[nodiscard]] std::pair<std::unordered_map<std::string, CompositionEntry>, GlobalComposition> getComposition() const;
448
453 [[nodiscard]] double getMeanParticleMass() const;
454
459 [[nodiscard]] double getMeanAtomicNumber() const;
460
467 Composition subset(const std::vector<std::string>& symbols, std::string method="norm") const;
468
474 bool hasSymbol(const std::string& symbol) const;
475
476 bool contains(const serif::atomic::Species& isotope) const;
477
482 void setCompositionMode(bool massFracMode);
483
492 [[nodiscard]] CanonicalComposition getCanonicalComposition(bool harsh=false) const;
493
500 friend std::ostream& operator<<(std::ostream& os, const Composition& composition);
501
502 // Overload the + operator to call mix with a fraction of 0.5
508 Composition operator+(const Composition& other) const;
509
510 };
511}; // namespace serif::composition
bool hasSymbol(const std::string &symbol) const
Check if a symbol is registered.
std::unordered_map< std::string, double > getMassFraction() const
Gets the mass fractions of all compositions.
bool isValidComposition(const std::vector< double > &fractions) const
Checks if the given mass fractions are valid.
double getMeanAtomicNumber() const
Compute the mean atomic mass number of the composition.
bool contains(const serif::atomic::Species &isotope) const
friend std::ostream & operator<<(std::ostream &os, const Composition &composition)
Overloaded output stream operator for Composition.
double m_meanParticleMass
The mean particle mass of the composition (\sum_{i} \frac{n_i}{m_i}. where n_i is the number fraction...
bool finalizeMassFracMode(bool norm)
Finalizes the composition in mass fraction mode.
std::set< std::string > getRegisteredSymbols() const
Gets the registered symbols.
serif::config::Config & m_config
Composition()=default
Default constructor.
Composition mix(const Composition &other, double fraction) const
Mix two compositions together with a given fraction.
serif::probe::LogManager & m_logManager
~Composition()=default
Default destructor.
double m_specificNumberDensity
The specific number density of the composition (\sum_{i} X_i m_i. Where X_i is the number fraction of...
void registerSymbol(const std::string &symbol, bool massFracMode=true)
Registers a new symbol.
std::set< std::string > m_registeredSymbols
The registered symbols.
Composition subset(const std::vector< std::string > &symbols, std::string method="norm") const
Gets a subset of the composition.
std::unordered_map< std::string, double > getNumberFraction() const
Gets the number fractions of all compositions.
bool finalize(bool norm=false)
Finalizes the composition.
double setMassFraction(const std::string &symbol, const double &mass_fraction)
Sets the mass fraction for a given symbol.
bool m_finalized
True if the composition is finalized.
bool finalizeNumberFracMode(bool norm)
Finalizes the composition in number fraction mode.
std::pair< std::unordered_map< std::string, CompositionEntry >, GlobalComposition > getComposition() const
Gets all composition entries and the global composition.
Composition & operator=(Composition const &other)
Composition operator+(const Composition &other) const
Overloads the + operator to mix two compositions together with a fraction of 0.5.
bool m_massFracMode
True if mass fraction mode, false if number fraction mode.
CanonicalComposition getCanonicalComposition(bool harsh=false) const
Gets the current canonical composition (X, Y, Z).
double getMeanParticleMass() const
Compute the mean particle mass of the composition.
std::unordered_map< std::string, CompositionEntry > m_compositions
The compositions.
double setNumberFraction(const std::string &symbol, const double &number_fraction)
Sets the number fraction for a given symbol.
void validateComposition(const std::vector< double > &fractions) const
Validates the given mass fractions.
void setCompositionMode(bool massFracMode)
Sets the composition mode.
static bool isValidSymbol(const std::string &symbol)
Checks if the given symbol is valid.
Class to manage logging operations.
Definition probe.h:79
static LogManager & getInstance()
Get the singleton instance of LogManager.
Definition probe.h:103
double X
Mass fraction of Hydrogen.
Definition composition.h:37
friend std::ostream & operator<<(std::ostream &os, const CanonicalComposition &composition)
Definition composition.h:41
double Z
Mass fraction of Metals.
Definition composition.h:39
double Y
Mass fraction of Helium.
Definition composition.h:38
std::string symbol() const
Gets the chemical symbol of the species.
serif::atomic::Species isotope() const
Gets the isotope of the species.
double rel_abundance() const
Gets the relative abundance of the species.
bool setNumberFracMode(double totalMoles)
Sets the mode to number fraction mode.
void setNumberFraction(double number_fraction)
Sets the number fraction of the species.
bool m_massFracMode
The mode of the composition entry. True if mass fraction, false if number fraction.
Definition composition.h:67
void setMassFraction(double mass_fraction)
Sets the mass fraction of the species.
serif::atomic::Species m_isotope
The isotope of the species.
Definition composition.h:66
double m_numberFraction
The number fraction of the species.
Definition composition.h:70
void setSpecies(const std::string &symbol)
Sets the species for the composition entry.
double number_fraction() const
Gets the number fraction of the species.
double mass_fraction() const
Gets the mass fraction of the species.
double m_relAbundance
The relative abundance of the species for converting between mass and number fractions.
Definition composition.h:71
bool setMassFracMode(double meanMolarMass)
Sets the mode to mass fraction mode.
std::string m_symbol
The chemical symbol of the species.
Definition composition.h:65
CompositionEntry()
Default constructor.
double m_massFraction
The mass fraction of the species.
Definition composition.h:69
bool m_initialized
True if the composition entry has been initialized.
Definition composition.h:73
friend std::ostream & operator<<(std::ostream &os, const CompositionEntry &entry)
Overloaded output stream operator for CompositionEntry.
bool getMassFracMode() const
Gets the mode of the composition entry.
Represents the global composition of a system. This tends to be used after finalize and is primarily ...
Definition composition.h:53
friend std::ostream & operator<<(std::ostream &os, const GlobalComposition &comp)
double meanParticleMass
The mean particle mass of the composition (\sum_{i} \frac{n_i}{m_i}. where n_i is the number fraction...
Definition composition.h:55
double specificNumberDensity
The specific number density of the composition (\sum_{i} X_i m_i. Where X_i is the number fraction of...
Definition composition.h:54