fix(python-bindings): Updated python bindings to new interface

The python bindings now work with the polymorphic reaction class and the CVODE solver
This commit is contained in:
2025-10-30 15:05:08 -04:00
parent 23df87f915
commit 7fded59814
27 changed files with 962 additions and 255 deletions

View File

@@ -176,7 +176,7 @@ namespace gridfire {
recordADTape(); // Record the AD tape for the RHS of the ODE (dY/di and dEps/di) for all independent variables i
const size_t inputSize = m_rhsADFun.Domain();
[[maybe_unused]] const size_t inputSize = m_rhsADFun.Domain();
const size_t outputSize = m_rhsADFun.Range();
// Create a range x range identity pattern
@@ -584,6 +584,26 @@ namespace gridfire {
}
}
fourdst::composition::Composition GraphEngine::collectComposition(
fourdst::composition::Composition &comp
) const {
for (const auto &speciesName: comp | std::views::keys) {
if (!m_networkSpeciesMap.contains(speciesName)) {
throw exceptions::BadCollectionError("Cannot collect composition from GraphEngine as " + speciesName + " present in input composition does not exist in the network species map");
}
}
fourdst::composition::Composition result;
for (const auto& species : m_networkSpecies ) {
result.registerSpecies(species);
if (comp.hasSpecies(species)) {
result.setMassFraction(species, comp.getMassFraction(species));
} else {
result.setMassFraction(species, 0.0);
}
}
return result;
}
StepDerivatives<double> GraphEngine::calculateAllDerivativesUsingPrecomputation(
const fourdst::composition::Composition& comp,
const std::vector<double> &bare_rates,

View File

@@ -318,6 +318,21 @@ namespace gridfire {
return m_baseEngine.primeEngine(netIn);
}
fourdst::composition::Composition AdaptiveEngineView::collectComposition(
fourdst::composition::Composition &comp
) const {
fourdst::composition::Composition result = m_baseEngine.collectComposition(comp); // Step one is to bubble the results from lower levels of the engine chain up
for (const auto& species : m_activeSpecies) {
if (!result.hasSpecies(species)) {
result.registerSpecies(species);
result.setMassFraction(species, 0.0);
}
}
return result;
}
size_t AdaptiveEngineView::getSpeciesIndex(const fourdst::atomic::Species &species) const {
const auto it = std::ranges::find(m_activeSpecies, species);
if (it != m_activeSpecies.end()) {

View File

@@ -295,6 +295,20 @@ namespace gridfire {
return m_baseEngine.primeEngine(netIn);
}
fourdst::composition::Composition DefinedEngineView::collectComposition(
fourdst::composition::Composition &comp
) const {
fourdst::composition::Composition result = m_baseEngine.collectComposition(comp);
for (const auto& species : m_activeSpecies) {
if (!result.hasSpecies(species)) {
result.registerSpecies(species);
result.setMassFraction(species, 0.0);
}
}
return result;
}
std::vector<size_t> DefinedEngineView::constructSpeciesIndexMap() const {
LOG_TRACE_L3(m_logger, "Constructing species index map for DefinedEngineView...");
std::unordered_map<Species, size_t> fullSpeciesReverseMap;

View File

@@ -874,6 +874,43 @@ namespace gridfire {
return std::ranges::find(m_dynamic_species, species) != m_dynamic_species.end();
}
fourdst::composition::Composition MultiscalePartitioningEngineView::collectComposition(
fourdst::composition::Composition &comp
) const {
fourdst::composition::Composition result = m_baseEngine.collectComposition(comp);
bool didFinalize = result.finalize(false);
if (!didFinalize) {
std::string msg = "Failed to finalize collected composition from MultiscalePartitioningEngine view after calling base engines collectComposition method.";
LOG_ERROR(m_logger, "{}", msg);
throw exceptions::BadCollectionError(msg);
}
std::map<Species, double> Ym; // Use an ordered map here so that this is ordered by atomic mass (which is the </> comparator for Species)
for (const auto& [speciesName, entry] : result) {
Ym.emplace(entry.isotope(), result.getMolarAbundance(speciesName));
}
for (const auto& [species, Yi] : m_algebraic_abundances) {
if (!Ym.contains(species)) {
throw exceptions::BadCollectionError("MuiltiscalePartitioningEngineView failed to collect composition for species " + std::string(species.name()) + " as the base engine did not report that species present in its composition!");
}
Ym.at(species) = Yi;
}
std::vector<double> M;
std::vector<double> Y;
std::vector<std::string> speciesNames;
M.reserve(Ym.size());
Y.reserve(Ym.size());
for (const auto& [species, Yi] : Ym) {
M.emplace_back(species.mass());
Y.emplace_back(Yi);
speciesNames.emplace_back(species.name());
}
std::vector<double> X = utils::massFractionFromMolarAbundanceAndMolarMass(Y, M);
return fourdst::composition::Composition(speciesNames, X);
}
size_t MultiscalePartitioningEngineView::getSpeciesIndex(const Species &species) const {
return m_baseEngine.getSpeciesIndex(species);
}