feat(composition): added ability to change composition modes
This commit is contained in:
@@ -459,4 +459,25 @@ Composition Composition::subset(const std::vector<std::string>& symbols, std::st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return subsetComposition;
|
return subsetComposition;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Composition::setCompositionMode(bool massFracMode) {
|
||||||
|
if (!m_finalized) {
|
||||||
|
LOG_ERROR(m_logger, "Composition has not been finalized. Mode cannot be set unless composition is finalized.");
|
||||||
|
throw std::runtime_error("Composition has not been finalized (Consider running .finalize()). The mode cannot be set unless the composition is finalized.");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool okay = true;
|
||||||
|
for (auto& [_, entry] : m_compositions) {
|
||||||
|
if (massFracMode) {
|
||||||
|
okay = entry.setMassFracMode(m_meanParticleMass);
|
||||||
|
} else {
|
||||||
|
okay = entry.setNumberFracMode(m_specificNumberDensity);
|
||||||
|
}
|
||||||
|
if (!okay) {
|
||||||
|
LOG_ERROR(m_logger, "Composition mode could not be set.");
|
||||||
|
throw std::runtime_error("Composition mode could not be set.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_massFracMode = massFracMode;
|
||||||
}
|
}
|
||||||
@@ -403,6 +403,12 @@ namespace composition{
|
|||||||
*/
|
*/
|
||||||
Composition subset(const std::vector<std::string>& symbols, std::string method="norm") const;
|
Composition subset(const std::vector<std::string>& symbols, std::string method="norm") const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets the composition mode.
|
||||||
|
* @param massFracMode True if mass fraction mode, false if number fraction mode.
|
||||||
|
*/
|
||||||
|
void setCompositionMode(bool massFracMode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Overloaded output stream operator for Composition.
|
* @brief Overloaded output stream operator for Composition.
|
||||||
* @param os The output stream.
|
* @param os The output stream.
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
@@ -138,4 +139,26 @@ TEST_F(compositionTest, getComposition) {
|
|||||||
EXPECT_DOUBLE_EQ(compositionEntry.first.mass_fraction(), 0.6);
|
EXPECT_DOUBLE_EQ(compositionEntry.first.mass_fraction(), 0.6);
|
||||||
EXPECT_DOUBLE_EQ(compositionEntry.second.meanParticleMass, 1.4382769310381101);
|
EXPECT_DOUBLE_EQ(compositionEntry.second.meanParticleMass, 1.4382769310381101);
|
||||||
EXPECT_DOUBLE_EQ(compositionEntry.second.specificNumberDensity, 1.0/1.4382769310381101);
|
EXPECT_DOUBLE_EQ(compositionEntry.second.specificNumberDensity, 1.0/1.4382769310381101);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(compositionTest, setCompositionMode) {
|
||||||
|
Config::getInstance().loadConfig(EXAMPLE_FILENAME);
|
||||||
|
composition::Composition comp;
|
||||||
|
comp.registerSymbol("H-1");
|
||||||
|
comp.registerSymbol("He-4");
|
||||||
|
comp.setMassFraction("H-1", 0.6);
|
||||||
|
comp.setMassFraction("He-4", 0.4);
|
||||||
|
EXPECT_NO_THROW(comp.finalize());
|
||||||
|
|
||||||
|
EXPECT_DOUBLE_EQ(comp.getMassFraction("H-1"), 0.6);
|
||||||
|
EXPECT_DOUBLE_EQ(comp.getMassFraction("He-4"), 0.4);
|
||||||
|
|
||||||
|
EXPECT_NO_THROW(comp.setCompositionMode(false));
|
||||||
|
|
||||||
|
EXPECT_NO_THROW(comp.setNumberFraction("H-1", 0.9));
|
||||||
|
EXPECT_NO_THROW(comp.setNumberFraction("He-4", 0.1));
|
||||||
|
|
||||||
|
EXPECT_THROW(comp.setCompositionMode(true), std::runtime_error);
|
||||||
|
EXPECT_NO_THROW(comp.finalize());
|
||||||
|
EXPECT_NO_THROW(comp.setCompositionMode(true));
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user