fourdst::libcomposition v2.3.1
Robust atomic species information library
Loading...
Searching...
No Matches
composition.cpp
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: October 6, 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#include "quill/LogMacros.h"
22
23#include <stdexcept>
24#include <unordered_map>
25#include <vector>
26#include <ranges>
27#include <algorithm>
28#include <set>
29#include <string>
30
31
32#include <utility>
33
37
38#include <numeric>
39
42
44
45namespace {
46 void throw_unknown_symbol(quill::Logger* logger, const std::string& symbol) {
47 LOG_ERROR(logger, "Symbol {} is not a valid species symbol (not in the species database)", symbol);
48 throw fourdst::composition::exceptions::UnknownSymbolError("Symbol " + symbol + " is not a valid species symbol (not in the species database)");
49 }
50
51 void throw_unregistered_symbol(quill::Logger* logger, const std::string& symbol) {
52 LOG_ERROR(logger, "Symbol {} is not registered in the composition.", symbol);
53 throw fourdst::composition::exceptions::UnregisteredSymbolError("Symbol " + symbol + " is not registered in the composition.");
54 }
55}
56
57namespace fourdst::composition {
58
64
66 const std::set<std::string>& symbols
67 ) : Composition(symbols | std::ranges::to<std::vector>()) {}
68
70 const std::set<atomic::Species> &species
71 ) : Composition(species | std::ranges::to<std::vector>()) {}
72
74 const std::unordered_set<std::string> &symbols
75 ) : Composition(symbols | std::ranges::to<std::vector>()) {}
76
78 const std::unordered_set<atomic::Species> &species
79 ) : Composition(species | std::ranges::to<std::vector>()) {}
80
82 const std::vector<std::string>& symbols
84
86 const std::vector<atomic::Species> &species
87 ) {
88 m_species = species;
89 std::ranges::sort(m_species, [&](const atomic::Species& a, const atomic::Species& b) {
90 return a < b;
91 });
92
93 const auto last = std::ranges::unique(m_species).begin();
94 m_species.erase(last, m_species.end());
95
96 m_molarAbundances.resize(m_species.size(), 0.0);
97 }
98
104
106 const std::vector<std::string>& symbols,
107 const std::vector<double>& molarAbundances
108 ) : Composition(symbolVectorToSpeciesVector(symbols), molarAbundances) {}
109
111 const std::set<std::string> &symbols,
112 const std::vector<double> &molarAbundances
113 ) : Composition(symbolVectorToSpeciesVector(symbols | std::ranges::to<std::vector>()), molarAbundances) {}
114
115
117 const std::unordered_map<std::string, double> &symbolMolarAbundances
118 ) : Composition(
119 symbolMolarAbundances | std::views::keys | std::ranges::to<std::vector>(),
120 symbolMolarAbundances | std::views::values | std::ranges::to<std::vector>()
121 ) {}
122
124 const std::map<std::string, double> &symbolMolarAbundances
125 ) : Composition(
126 symbolMolarAbundances | std::views::keys | std::ranges::to<std::vector>(),
127 symbolMolarAbundances | std::views::values | std::ranges::to<std::vector>()
128 ) {}
129
131 const std::unordered_map<atomic::Species, double> &speciesMolarAbundances
132 ) : Composition(
133 speciesMolarAbundances | std::views::keys | std::ranges::to<std::vector>(),
134 speciesMolarAbundances | std::views::values | std::ranges::to<std::vector>()
135 ) {}
136
138 const std::map<atomic::Species, double> &speciesMolarAbundances
139 ) : Composition(
140 speciesMolarAbundances | std::views::keys | std::ranges::to<std::vector>(),
141 speciesMolarAbundances | std::views::values | std::ranges::to<std::vector>()
142 ) {}
143
145 const std::vector<atomic::Species> &species,
146 const std::vector<double> &molarAbundances
147 ) {
148 if (__builtin_expect(species.size() != molarAbundances.size(), 0)) {
149 LOG_CRITICAL(getLogger(), "The number of species and molarAbundances must be equal (got {} species and {} molarAbundances).", species.size(), molarAbundances.size());
150 throw exceptions::InvalidCompositionError("The number of species and fractions must be equal. Got " + std::to_string(species.size()) + " species and " + std::to_string(molarAbundances.size()) + " fractions.");
151 }
152
153 const size_t numSpecies = species.size();
154 m_species.reserve(numSpecies);
155 m_molarAbundances.reserve(numSpecies);
156
157 for (size_t i = 0; i < numSpecies; ++i) {
158 m_species.push_back(species[i]);
159 if (__builtin_expect(molarAbundances[i] < 0.0, 0)) {
160 LOG_CRITICAL(getLogger(), "Molar abundance for species {} is negative (y = {}). Molar abundances must be non-negative.", species[i].name(), molarAbundances[i]);
161 throw exceptions::InvalidCompositionError("Molar abundance for species " + std::string(species[i].name()) + " is negative (y = " + std::to_string(molarAbundances[i]) + "). Molar abundances must be non-negative.");
162 }
163 m_molarAbundances.push_back(molarAbundances[i]);
164 }
165
166 auto combined = std::views::zip(m_species, m_molarAbundances);
167
168 std::ranges::sort(combined, [](const auto& a, const auto& b) -> bool {
169 const auto& spA = std::get<0>(a);
170 const auto& spB = std::get<0>(b);
171
172 if (spA != spB) {
173 return spA < spB;
174 }
175
176 return std::get<1>(a) > std::get<1>(b);
177 });
178
179 auto [first, last] = std::ranges::unique(combined, [](const auto& a, const auto& b) {
180 return std::get<0>(a) == std::get<0>(b);
181 });
182
183 const auto newEndIndex = std::distance(combined.begin(), first);
184 m_species.erase(m_species.begin() + newEndIndex, m_species.end());
185 m_molarAbundances.erase(m_molarAbundances.begin() + newEndIndex, m_molarAbundances.end());
186 }
187
191
193 m_species = composition.m_species;
194 m_molarAbundances = composition.m_molarAbundances;
195 }
196
198 for (const auto& species : composition.getRegisteredSpecies()) {
199 registerSpecies(species);
200 setMolarAbundance(species, composition.getMolarAbundance(species));
201 }
202 }
203
205 const Composition &other
206 ) {
207 if (this != &other) {
208 m_species = other.m_species;
210 }
211 m_cache.clear();
212 return *this;
213 }
214
216 m_species.clear();
217 m_molarAbundances.clear();
218 m_cache.clear();
219 for (const auto& species : other.getRegisteredSpecies()) {
220 registerSpecies(species);
221 setMolarAbundance(species, other.getMolarAbundance(species));
222 }
223 return *this;
224 }
225
226 std::unique_ptr<CompositionAbstract> Composition::clone() const {
227 return std::make_unique<Composition>(*this);
228 }
229
230 //------------------------------------------
231 // Registration methods
232 //------------------------------------------
233
235 const std::string& symbol
236 ) {
237 const auto result = getSpecies(symbol);
238 if (!result) {
239 throw_unknown_symbol(getLogger(), symbol);
240 }
241
242 registerSpecies(result.value());
243 }
244
246 const std::vector<std::string>& symbols
247 ) {
249 }
250
252 const atomic::Species &species
253 ) noexcept {
254 if (const auto it = std::ranges::lower_bound(m_species, species); it == m_species.end() || *it != species) {
255 const auto index = std::distance(m_species.begin(), it);
256 m_species.insert(it, species);
257 m_molarAbundances.insert(m_molarAbundances.begin() + index, 0.0);
258 m_cache.clear();
259 }
260 }
261
263 const std::vector<atomic::Species> &species
264 ) noexcept {
265 // We do not simply call registerSpecies(species) here as that would have a complexity of O(n^2) due to constantly
266 // reinserting into the vector. Rather we build the vector once and then sort it
267
268 if (species.empty()) return;
269
270 const size_t total_size = m_species.size() + species.size();
271 m_species.reserve(total_size);
272 m_molarAbundances.reserve(total_size);
273
274 for (const auto& sp : species) {
275 m_species.push_back(sp);
276 m_molarAbundances.push_back(0.0);
277 }
278
279 auto combined = std::views::zip(m_species, m_molarAbundances);
280
281 std::ranges::sort(combined, [](const auto& a, const auto& b) {
282 const auto& speciesA = std::get<0>(a);
283 const auto& speciesB = std::get<0>(b);
284
285 if (speciesA != speciesB) {
286 return speciesA < speciesB;
287 }
288
289 return std::get<1>(a) > std::get<1>(b);
290 });
291
292 auto [first, last] = std::ranges::unique(combined, [](const auto& a, const auto& b) {
293 return std::get<0>(a) == std::get<0>(b);
294 });
295
296 const auto newEndIndex = std::distance(combined.begin(), first);
297
298 m_species.erase(m_species.begin() + newEndIndex, m_species.end());
299 m_molarAbundances.erase(m_molarAbundances.begin() + newEndIndex, m_molarAbundances.end());
300
301 m_cache.clear();
302 }
303
304 std::set<std::string> Composition::getRegisteredSymbols() const noexcept {
305 std::set<std::string> symbols;
306 for (const auto& species : m_species) {
307 symbols.insert(std::string(species.name()));
308 }
309 return symbols;
310 }
311
312 const std::vector<atomic::Species> &Composition::getRegisteredSpecies() const noexcept {
313 return m_species;
314 }
315
316
317 //------------------------------------------
318 // Molar abundance setters
319 //------------------------------------------
320
322 const std::string &symbol,
323 const double &molar_abundance
324 ) {
325 const auto species = getSpecies(symbol);
326 if (__builtin_expect(!species, 0)) {
327 throw_unknown_symbol(getLogger(), symbol);
328 }
329
330 setMolarAbundance(species.value(), molar_abundance);
331 }
332
334 const atomic::Species &species,
335 const double &molar_abundance
336 ) {
337 if (__builtin_expect(molar_abundance < 0.0, 0)) {
338 LOG_ERROR(getLogger(), "Molar abundance must be non-negative for symbol {}. Currently it is {}.", species.name(), molar_abundance);
339 throw exceptions::InvalidCompositionError("Molar abundance must be non-negative, got " + std::to_string(molar_abundance) + " for symbol " + std::string(species.name()) + ".");
340 }
341
342 const std::expected<std::ptrdiff_t, SpeciesIndexLookupError> speciesIndexResult = findSpeciesIndex(species);
343 if (__builtin_expect(!speciesIndexResult, 0)) {
344 throw_unregistered_symbol(getLogger(), std::string(species.name()));
345 }
346
347 assert(static_cast<size_t>(speciesIndexResult.value()) < m_molarAbundances.size());
348
349 m_molarAbundances[speciesIndexResult.value()] = molar_abundance;
350 m_cache.clear();
351 }
352
353
358
360 const std::vector<std::string> &symbols,
361 const std::vector<double> &molar_abundances
362 ) {
363 setMolarAbundance(symbolVectorToSpeciesVector(symbols), molar_abundances);
364 }
365
367 const std::set<std::string> &symbols,
368 const std::vector<double> &molar_abundances
369 ) {
370 setMolarAbundance(symbolVectorToSpeciesVector(symbols | std::ranges::to<std::vector>()), molar_abundances);
371 }
372
374 const std::set<atomic::Species> &species,
375 const std::vector<double> &molar_abundances
376 ) {
377 setMolarAbundance(species | std::ranges::to<std::vector>(), molar_abundances);
378 }
379
381 const std::vector<atomic::Species> &species,
382 const std::vector<double> &molar_abundances
383 ) {
384 if (__builtin_expect(species.size() != molar_abundances.size(), 0)) {
385 LOG_CRITICAL(getLogger(), "The number of species and molar_abundances must be equal (got {} species and {} molar_abundances).", species.size(), molar_abundances.size());
386 throw exceptions::InvalidCompositionError("The number of species and fractions must be equal. Got " + std::to_string(species.size()) + " species and " + std::to_string(molar_abundances.size()) + " fractions.");
387 }
388
389 if (species.empty()) return;
390
391 if (species.size() == m_species.size()) {
392 if (species == m_species) {
393 for (const auto& [sp, y] : std::views::zip(species, molar_abundances)) {
394 if (__builtin_expect(y < 0.0, 0)) {
395 LOG_ERROR(getLogger(), "Molar abundance must be non-negative. Instead got {} for species {}.", y, sp.name());
396 throw exceptions::InvalidCompositionError("Molar abundance must be non-negative. Instead got " + std::to_string(y) + " for species " + std::string(sp.name()) + ".");
397 }
398 }
399
400 m_molarAbundances = molar_abundances;
401 m_cache.clear();
402 return;
403 }
404 }
405
406 for (size_t i = 0; i < species.size(); ++i) {
407 const double y = molar_abundances[i];
408 const auto& sp = species[i];
409 if (__builtin_expect(y < 0.0, 0)) {
410 LOG_CRITICAL(getLogger(), "Molar abundance must be non-negative. Instead got {} for species {}.", y, sp.name());
411 throw exceptions::InvalidCompositionError("Molar abundance must be non-negative. Instead got " + std::to_string(y) + " for species " + std::string(sp.name()) + ".");
412 }
413
414 const std::expected<std::ptrdiff_t, SpeciesIndexLookupError> speciesIndexResult = findSpeciesIndex(sp);
415 if (__builtin_expect(!speciesIndexResult, 0)) {
416 throw_unregistered_symbol(getLogger(), std::string(sp.name()));
417 }
418
419 const std::ptrdiff_t speciesIndex = speciesIndexResult.value();
420
421 m_molarAbundances[speciesIndex] = y;
422 }
423
424 m_cache.clear();
425 }
426
427
428 //------------------------------------------
429 // Fraction and abundance getters
430 //------------------------------------------
431
432 double Composition::getMassFraction(const std::string& symbol) const {
433 const auto species = getSpecies(symbol);
434 if (!species) {
435 throw_unknown_symbol(getLogger(), symbol);
436 }
437 return getMassFraction(species.value());
438 }
439
441 const atomic::Species &species
442 ) const {
443 const std::expected<std::ptrdiff_t, SpeciesIndexLookupError> speciesIndexResult = findSpeciesIndex(species);
444 if (!speciesIndexResult) {
445 throw_unregistered_symbol(getLogger(), std::string(species.name()));
446 }
447
448 double totalMass = 0;
449 double speciesMass = 0;
450 for (const auto& [sp, y] : *this) {
451 const double contrib = y * sp.mass();
452 totalMass += contrib;
453 if (sp == species) {
454 speciesMass = contrib;
455 }
456 }
457 return speciesMass / totalMass;
458 }
459
460 std::unordered_map<atomic::Species, double> Composition::getMassFraction() const noexcept {
461 std::unordered_map<atomic::Species, double> mass_fractions;
462 for (const auto &species: *this | std::views::keys) {
463 mass_fractions.emplace(species, getMassFraction(species));
464 }
465 return mass_fractions;
466 }
467
468
470 const std::string& symbol
471 ) const {
472 const auto species = getSpecies(symbol);
473 if (!species) {
474 throw_unknown_symbol(getLogger(), symbol);
475 }
476 return getNumberFraction(species.value());
477 }
478
480 const atomic::Species &species
481 ) const {
482 const std::expected<std::ptrdiff_t, SpeciesIndexLookupError> speciesIndexResult = findSpeciesIndex(species);
483 if (!speciesIndexResult) {
484 throw_unregistered_symbol(getLogger(), std::string(species.name()));
485 }
486 const std::ptrdiff_t speciesIndex = speciesIndexResult.value();
487
488 const double total_moles_per_gram = std::accumulate(
489 m_molarAbundances.begin(),
490 m_molarAbundances.end(),
491 0.0
492 );
493 return m_molarAbundances[speciesIndex] / total_moles_per_gram;
494 }
495
496 std::unordered_map<atomic::Species, double> Composition::getNumberFraction() const noexcept {
497 std::unordered_map<atomic::Species, double> number_fractions;
498 for (const auto &species: m_species) {
499 number_fractions.emplace(species, getNumberFraction(species));
500 }
501 return number_fractions;
502 }
503
505 const std::string &symbol
506 ) const {
507 const auto species = getSpecies(symbol);
508 if (!species) {
509 throw_unknown_symbol(getLogger(), symbol);
510 }
511 return getMolarAbundance(species.value());
512
513 }
514
516 const atomic::Species &species
517 ) const {
518 const std::expected<std::ptrdiff_t, SpeciesIndexLookupError> speciesIndexResult = findSpeciesIndex(species);
519 if (!speciesIndexResult) {
520 throw_unregistered_symbol(getLogger(), std::string(species.name()));
521 }
522 const std::ptrdiff_t speciesIndex = speciesIndexResult.value();
523 return m_molarAbundances[speciesIndex];
524 }
525
526 //------------------------------------------
527 // Derived property getters
528 //------------------------------------------
529
530 double Composition::getMeanParticleMass() const noexcept {
531 double totalMass = 0.0;
532 double totalMoles = 0.0;
533
534 for (size_t i = 0; i < m_species.size(); ++i) {
535 totalMoles += m_molarAbundances[i];
536 totalMass += m_molarAbundances[i] * m_species[i].mass();
537 }
538
539 return totalMass / totalMoles;
540 }
541
542
543 double Composition::getElectronAbundance() const noexcept {
544 double Ye = 0.0;
545 for (const auto& [species, y] : *this) {
546 Ye += species.z() * y;
547 }
548 return Ye;
549 }
550
551
553 ) const {
554 using namespace fourdst::atomic;
555
556 if (m_cache.canonicalComp.has_value()) {
557 return m_cache.canonicalComp.value(); // Short circuit if we have cached the canonical composition
558 }
559 CanonicalComposition canonicalComposition;
560 static const std::unordered_set<Species> canonicalH = {H_1, H_2, H_3, H_4, H_5, H_6, H_7};
561 static const std::unordered_set<Species> canonicalHe = {He_3, He_4, He_5, He_6, He_7, He_8, He_9, He_10};
562
563 for (const auto& symbol : canonicalH) {
564 if (contains(symbol)) {
565 canonicalComposition.X += getMassFraction(symbol);
566 }
567 }
568 for (const auto& symbol : canonicalHe) {
569 if (contains(symbol)) {
570 canonicalComposition.Y += getMassFraction(symbol);
571 }
572 }
573
574 for (const auto& species : m_species) {
575 if (canonicalH.contains(species) || canonicalHe.contains(species)) {
576 continue; // Skip canonical H and He symbols
577 }
578
579 canonicalComposition.Z += getMassFraction(species);
580 }
581
582 // ReSharper disable once CppTooWideScopeInitStatement
583 const double Z = 1.0 - (canonicalComposition.X + canonicalComposition.Y);
584 if (std::abs(Z - canonicalComposition.Z) > 1e-16) {
585 LOG_ERROR(getLogger(), "Validation composition Z (X-Y = {}) is different than canonical composition Z ({}) (∑a_i where a_i != H/He).", Z, canonicalComposition.Z);
586 throw exceptions::InvalidCompositionError("Validation composition Z (X-Y = " + std::to_string(Z) + ") is different than canonical composition Z (" + std::to_string(canonicalComposition.Z) + ") (∑a_i where a_i != H/He).");
587 }
588 m_cache.canonicalComp = canonicalComposition;
589 return canonicalComposition;
590 }
591
592 //------------------------------------------
593 // Vector getters
594 //------------------------------------------
595
596 std::vector<double> Composition::getMassFractionVector() const noexcept {
597 if (m_cache.massFractions.has_value()) {
598 return m_cache.massFractions.value(); // Short circuit if we have cached the mass fractions
599 }
600
601 std::vector<double> massFractionVector;
602
603 massFractionVector.reserve(m_molarAbundances.size());
604
605 for (const auto &species: m_species) {
606 massFractionVector.push_back(getMassFraction(species));
607 }
608
609 m_cache.massFractions = massFractionVector; // Cache the result
610 return massFractionVector;
611
612 }
613
614 std::vector<double> Composition::getNumberFractionVector() const noexcept {
615 if (m_cache.numberFractions.has_value()) {
616 return m_cache.numberFractions.value(); // Short circuit if we have cached the number fractions
617 }
618
619 std::vector<double> numberFractionVector;
620
621 numberFractionVector.reserve(m_molarAbundances.size());
622
623 for (const auto &species: m_species) {
624 numberFractionVector.push_back(getNumberFraction(species));
625 }
626
627 m_cache.numberFractions = numberFractionVector; // Cache the result
628 return numberFractionVector;
629 }
630
631 std::vector<double> Composition::getMolarAbundanceVector() const noexcept {
632 return m_molarAbundances;
633 }
634
635 //------------------------------------------
636 // Species index getters and lookups
637 //------------------------------------------
638
640 const std::string &symbol
641 ) const {
642 const auto species = getSpecies(symbol);
643 if (!species) {
644 throw_unknown_symbol(getLogger(), symbol);
645 }
646
647 return getSpeciesIndex(species.value());
648 }
649
652 ) const {
653 std::expected<std::ptrdiff_t, SpeciesIndexLookupError> speciesIndexResult = findSpeciesIndex(species);
654 if (!speciesIndexResult) {
655 switch (speciesIndexResult.error()) {
657 [[fallthrough]];
659 throw_unregistered_symbol(getLogger(), std::string(species.name()));
660 default:
661 throw std::logic_error("Unhandled SpeciesIndexLookupError in Composition::getSpeciesIndex");
662 }
663 }
664
665 return static_cast<size_t>(speciesIndexResult.value());
666 }
667
669 const size_t index
670 ) const {
671 if (index >= m_species.size()) {
672 LOG_ERROR(getLogger(), "Index {} is out of bounds for registered species (size {}).", index, m_species.size());
673 throw std::out_of_range("Index " + std::to_string(index) + " is out of bounds for registered species (size " + std::to_string(m_species.size()) + ").");
674 }
675
676 return m_species[index];
677 }
678
679
680 //------------------------------------------
681 // Utility methods
682 //------------------------------------------
683
684 std::size_t Composition::hash() const {
685 if (m_cache.hash.has_value()) {
686 return m_cache.hash.value();
687 }
688 std::size_t hash = utils::CompositionHash::hash_exact(*this);
689 m_cache.hash = hash;
690 return hash;
691 }
692
695 ) const noexcept {
696 return std::ranges::binary_search(m_species, species);
697 }
698
700 const std::string &symbol
701 ) const {
702 const auto species = getSpecies(symbol);
703 if (!species) {
704 throw_unknown_symbol(getLogger(), symbol);
705 }
706 return contains(species.value());
707 }
708
709 size_t Composition::size() const noexcept {
710 return m_species.size();
711 }
712
713 std::expected<std::ptrdiff_t, Composition::SpeciesIndexLookupError> Composition::findSpeciesIndex(const atomic::Species &species) const noexcept {
714 if (m_species.empty()) return std::unexpected(SpeciesIndexLookupError::NO_REGISTERED_SPECIES);
715
716 const auto it = std::ranges::lower_bound(m_species, species);
717
718 if (it == m_species.end() || *it != species) {
719 return std::unexpected(SpeciesIndexLookupError::SPECIES_NOT_FOUND);
720 }
721
722 return std::distance(m_species.begin(), it);
723 }
724
725 std::vector<atomic::Species> Composition::symbolVectorToSpeciesVector(const std::vector<std::string> &symbols) {
726 std::vector<atomic::Species> species;
727 species.reserve(symbols.size());
728
729
730 for (const auto& symbol : symbols) {
731 const auto speciesResult = getSpecies(symbol);
732 if (!speciesResult) {
733 throw_unknown_symbol(getLogger(), symbol);
734 }
735 species.push_back(speciesResult.value());
736 }
737
738 return species;
739 }
740
741
742 //------------------------------------------
743 // Stream operator
744 //------------------------------------------
745
746 std::ostream& operator<<(
747 std::ostream& os,
749 ) {
750 os << "Composition(Mass Fractions => [";
751 size_t count = 0;
752 for (const auto &species : composition.m_species) {
753 os << species << ": " << composition.getMassFraction(species);
754 if (count < composition.size() - 1) {
755 os << ", ";
756 }
757 count++;
758 }
759 os << "])";
760 return os;
761 }
762
763} // namespace fourdst::composition
Abstract base class for chemical composition representations.
virtual double getMolarAbundance(const std::string &symbol) const =0
Get the molar abundance for a given symbol.
virtual const std::vector< atomic::Species > & getRegisteredSpecies() const noexcept=0
Get all registered atomic species in the composition.
CompositionCache m_cache
Cache for computed properties to avoid redundant calculations.
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.
std::unordered_map< atomic::Species, double > getNumberFraction() const noexcept override
Gets the number fractions of all species 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.
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.
static std::vector< atomic::Species > symbolVectorToSpeciesVector(const std::vector< std::string > &symbols)
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.
std::unique_ptr< CompositionAbstract > clone() const override
std::size_t hash() const override
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::vector< atomic::Species > m_species
CanonicalComposition getCanonicalComposition() const
Compute the canonical composition (X, Y, Z) of the composition.
std::vector< double > m_molarAbundances
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.
std::expected< std::ptrdiff_t, SpeciesIndexLookupError > findSpeciesIndex(const atomic::Species &species) const noexcept
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.
const std::vector< atomic::Species > & getRegisteredSpecies() const noexcept override
Get a set of all species that are registered in the composition.
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.
Exception thrown when a composition is in an invalid or inconsistent state.
Exception thrown when an unknown symbol is encountered.
Exception thrown when a symbol is used that has not been registered.
Contains canonical information about atomic species and elements used by 4D-STAR.
static const Species H_7("H-7", "H", 5, 6, 1, 7, 940.0, "B-", 23062.0, 652.0, "/2+#", "n ?", 7.052749, 1078.0)
static const Species H_2("H-2", "H", 0, 1, 1, 2, 1112.2831, "B-", std::numeric_limits< double >::quiet_NaN(), std::numeric_limits< double >::infinity(), "+*", "S=0.0145 78", 2.014101777844, 1.5e-05)
static const Species H_6("H-6", "H", 4, 5, 1, 6, 961.6395, "B-", 24283.6294, 294.0, "-#", "?;3n ?", 6.044955437, 272.816)
static const Species He_10("He-10", "He", 6, 8, 2, 10, 2995.134, "B-", 16144.5191, 260.0, "+", "n=100", 10.052815306, 99.676)
static const Species He_9("He-9", "He", 5, 7, 2, 9, 3349.038, "B-", 15980.9213, 2.5, "/2(+)", "=100", 9.043946414, 50.259)
static const std::unordered_map< std::string, const Species & > species
Map of species names to their corresponding Species objects.
Definition species.h:3579
static const Species He_8("He-8", "He", 4, 6, 2, 8, 3924.521, "B-", 10663.8784, 119.5, "+", "-=100;B-n=16 1;B-t=0.9 1", 8.033934388, 0.095)
static const Species He_3("He-3", "He", -1, 1, 2, 3, 2572.68044, "B-", -13736.0, std::numeric_limits< double >::infinity(), "/2+*", "S=0.0002 2", 3.01602932197, 6e-05)
static const Species H_1("H-1", "H", -1, 0, 1, 1, 0.0, "B-", std::numeric_limits< double >::quiet_NaN(), std::numeric_limits< double >::infinity(), "/2+*", "S=99.9855 78", 1.007825031898, 1.4e-05)
static const Species H_5("H-5", "H", 3, 4, 1, 5, 1336.3592, "B-", 21661.2131, 86.0, "1/2+)", "n=100", 5.035311492, 96.02)
static const Species He_7("He-7", "He", 3, 5, 2, 7, 4123.0578, "B-", 11166.0229, 2.51, "3/2)-", "=100", 7.027990652, 8.115)
static const Species He_4("He-4", "He", 0, 2, 2, 4, 7073.9156, "B-", -22898.274, std::numeric_limits< double >::infinity(), "+", "S=99.9998 2", 4.00260325413, 0.00016)
static const Species He_5("He-5", "He", 1, 3, 2, 5, 5512.1325, "B-", -447.6529, 602.0, "/2-", "=100", 5.012057224, 21.47)
static const Species H_3("H-3", "H", 1, 2, 1, 3, 2827.2654, "B-", 18.59202, 388789632.0, "/2+*", "-=100", 3.01604928132, 8e-05)
static const Species He_6("He-6", "He", 2, 4, 2, 6, 4878.5199, "B-", 3505.2147, 806.92, "+", "-=100;B-d=0.000278 18", 6.018885889, 0.057)
static const Species H_4("H-4", "H", 2, 3, 1, 4, 1720.4491, "B-", 22196.2131, 139.0, "-", "=100", 4.026431867, 107.354)
Utilities and types for representing and manipulating chemical compositions.
std::ostream & operator<<(std::ostream &os, const Composition &composition)
std::optional< fourdst::atomic::Species > getSpecies(const std::string &symbol)
Definition utils.cpp:189
Represents an atomic species (isotope) with its fundamental physical properties.
std::string_view name() const
Gets the name of the species.
Represents the canonical (X, Y, Z) composition of stellar material.
Definition composition.h:46
double Y
Mass fraction of Helium.
Definition composition.h:48
double X
Mass fraction of Hydrogen.
Definition composition.h:47
double Z
Mass fraction of Metals.
Definition composition.h:49
static uint64_t hash_exact(const CompositionT &comp)