feat(reverse-reactions): major work towrds detailed balance calculations

This commit is contained in:
2025-07-03 09:55:10 -04:00
parent e5ad284778
commit 1ac6b451b8
14 changed files with 365 additions and 31 deletions

View File

@@ -10,6 +10,8 @@
#include "gridfire/engine/engine_abstract.h"
#include "gridfire/screening/screening_abstract.h"
#include "gridfire/screening/screening_types.h"
#include "gridfire/partition/partition_abstract.h"
#include "gridfire/partition/partition_ground.h"
#include <string>
#include <unordered_map>
@@ -101,6 +103,10 @@ namespace gridfire {
*/
explicit GraphEngine(const fourdst::composition::Composition &composition);
explicit GraphEngine(const fourdst::composition::Composition &composition,
const partition::PartitionFunction& partitionFunction
);
/**
* @brief Constructs a GraphEngine from a set of reactions.
*
@@ -307,6 +313,14 @@ namespace gridfire {
[[nodiscard]] bool isPrecomputationEnabled() const;
[[nodiscard]] const partition::PartitionFunction& getPartitionFunction() const;
[[nodiscard]] double calculateReverseRate(
const reaction::Reaction &reaction,
double T9,
double expFactor
) const;
private:
struct PrecomputedReaction {
size_t reaction_index;
@@ -321,6 +335,7 @@ namespace gridfire {
const double u = Constants::getInstance().get("u").value; ///< Atomic mass unit in g.
const double Na = Constants::getInstance().get("N_a").value; ///< Avogadro's number.
const double c = Constants::getInstance().get("c").value; ///< Speed of light in cm/s.
const double kB = Constants::getInstance().get("kB").value; ///< Boltzmann constant in erg/K.
};
private:
@@ -347,6 +362,7 @@ namespace gridfire {
bool m_usePrecomputation = true; ///< Flag to enable or disable using precomputed reactions for efficiency. Mathematically, this should not change the results. Generally end users should not need to change this.
std::vector<PrecomputedReaction> m_precomputedReactions; ///< Precomputed reactions for efficiency.
std::unique_ptr<partition::PartitionFunction> m_partitionFunction; ///< Partition function for the network.
private:
/**
@@ -432,6 +448,20 @@ namespace gridfire {
double T9
);
double calculateReverseRateTwoBody(
const reaction::Reaction &reaction,
const double T9,
const double forwardRate,
const double expFactor
) const;
double GraphEngine::calculateReverseRateTwoBodyDerivative(
const reaction::Reaction &reaction,
const double T9,
const double reverseRate
) const;
[[nodiscard]] StepDerivatives<double> calculateAllDerivativesUsingPrecomputation(
const std::vector<double> &Y_in,
const std::vector<double>& bare_rates,
@@ -647,4 +677,36 @@ namespace gridfire {
// the entire network.
return molar_concentration_product * k_reaction * threshold_flag;
}
class AtomicReverseRate final : public CppAD::atomic_base<double> {
public:
AtomicReverseRate(
const reaction::Reaction& reaction,
const GraphEngine& engine
):
atomic_base<double>("AtomicReverseRate"),
m_reaction(reaction),
m_engine(engine) {}
bool forward(
size_t p,
size_t q,
const CppAD::vector<bool>& vx,
CppAD::vector<bool>& vy,
const CppAD::vector<double>& tx,
CppAD::vector<double>& ty
) override;
bool reverse(
size_t id,
size_t an,
const CppAD::vector<double>& tx,
const CppAD::vector<double>& ty,
CppAD::vector<double>& px,
const CppAD::vector<double>& py
);
private:
const double m_kB = Constants::getInstance().get("k_b").value; ///< Boltzmann constant in erg/K.
const reaction::Reaction& m_reaction;
const GraphEngine& m_engine;
};
};

View File

@@ -1,6 +1,7 @@
#pragma once
#include "gridfire/partition/partition_abstract.h"
#include "gridfire/partition/partition_types.h"
#include "fourdst/logging/logging.h"
@@ -12,28 +13,17 @@
namespace gridfire::partition {
enum BasePartitionType {
RauscherThielemann, ///< Rauscher-Thielemann partition function
GroundState, ///< Ground state partition function
};
inline std::unordered_map<BasePartitionType, std::string> basePartitionTypeToString = {
{RauscherThielemann, "RauscherThielemann"},
{GroundState, "GroundState"}
};
inline std::unordered_map<std::string, BasePartitionType> stringToBasePartitionType = {
{"RauscherThielemann", RauscherThielemann},
{"GroundState", GroundState}
};
class CompositePartitionFunction final : public PartitionFunction {
public:
explicit CompositePartitionFunction(const std::vector<BasePartitionType>& partitionFunctions);
double evaluate(int z, int a, double T9) const override;
double evaluateDerivative(int z, int a, double T9) const override;
bool supports(int z, int a) const override;
std::string type() const override;
CompositePartitionFunction(const CompositePartitionFunction& other);
[[nodiscard]] double evaluate(int z, int a, double T9) const override;
[[nodiscard]] double evaluateDerivative(int z, int a, double T9) const override;
[[nodiscard]] bool supports(int z, int a) const override;
[[nodiscard]] std::string type() const override;
[[nodiscard]] std::unique_ptr<PartitionFunction> clone() const override {
return std::make_unique<CompositePartitionFunction>(*this);
}
private:
quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log");
std::vector<std::unique_ptr<PartitionFunction>> m_partitionFunctions; ///< Set of partition functions to use in the composite partition function.

View File

@@ -1,14 +1,16 @@
#pragma once
#include <string>
#include <memory>
namespace gridfire::partition {
class PartitionFunction {
public:
virtual ~PartitionFunction() = default;
virtual double evaluate(int z, int a, double T9) const = 0;
virtual double evaluateDerivative(int z, int a, double T9) const = 0;
virtual bool supports(int z, int a) const = 0;
virtual std::string type() const = 0;
[[nodiscard]] virtual double evaluate(int z, int a, double T9) const = 0;
[[nodiscard]] virtual double evaluateDerivative(int z, int a, double T9) const = 0;
[[nodiscard]] virtual bool supports(int z, int a) const = 0;
[[nodiscard]] virtual std::string type() const = 0;
[[nodiscard]] virtual std::unique_ptr<PartitionFunction> clone() const = 0;
};
}

View File

@@ -5,6 +5,7 @@
#include "fourdst/logging/logging.h"
#include <unordered_map>
#include <memory>
#include "quill/Logger.h"
@@ -27,6 +28,9 @@ namespace gridfire::partition {
const int a
) const override;
std::string type() const override { return "GroundState"; }
std::unique_ptr<PartitionFunction> clone() const override {
return std::make_unique<GroundStatePartitionFunction>(*this);
}
private:
quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log");
std::unordered_map<int, double> m_ground_state_spin;

View File

@@ -9,6 +9,7 @@
#include <unordered_map>
#include <array>
#include <utility>
#include <memory>
namespace gridfire::partition {
class RauscherThielemannPartitionFunction final : public PartitionFunction {
@@ -41,6 +42,9 @@ namespace gridfire::partition {
size_t upperIndex;
size_t lowerIndex;
};
std::unique_ptr<PartitionFunction> clone() const override {
return std::make_unique<RauscherThielemannPartitionFunction>(*this);
}
private:
quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log");
std::unordered_map<int, IsotopeData> m_partitionData;

View File

@@ -0,0 +1,21 @@
#pragma once
#include <unordered_map>
#include <string>
namespace gridfire::partition {
enum BasePartitionType {
RauscherThielemann, ///< Rauscher-Thielemann partition function
GroundState, ///< Ground state partition function
};
inline std::unordered_map<BasePartitionType, std::string> basePartitionTypeToString = {
{RauscherThielemann, "RauscherThielemann"},
{GroundState, "GroundState"}
};
inline std::unordered_map<std::string, BasePartitionType> stringToBasePartitionType = {
{"RauscherThielemann", RauscherThielemann},
{"GroundState", GroundState}
};
}

View File

@@ -8,7 +8,6 @@ namespace gridfire::partition::record {
uint32_t z; ///< Atomic number
uint32_t a; ///< Mass number
double ground_state_spin; ///< Ground state spin
double partition_function; ///< Partition function value
double normalized_g_values[24]; ///< Normalized g-values for the first 24 energy levels
};
#pragma pack(pop)

View File

@@ -113,6 +113,8 @@ namespace gridfire::reaction {
*/
[[nodiscard]] virtual CppAD::AD<double> calculate_rate(const CppAD::AD<double> T9) const;
[[nodiscard]] virtual double calculate_forward_rate_log_derivative(const double T9) const;
/**
* @brief Gets the reaction name in (projectile, ejectile) notation.
* @return The reaction name (e.g., "p(p,g)d").
@@ -341,6 +343,8 @@ namespace gridfire::reaction {
*/
[[nodiscard]] double calculate_rate(const double T9) const override;
[[nodiscard]] virtual double calculate_forward_rate_log_derivative(const double T9) const override;
/**
* @brief Calculates the total reaction rate using CppAD types.
* @param T9 The temperature in units of 10^9 K, as a CppAD::AD<double>.