feat(pythonInterface/mfem): added loads of mfem bindings to make interacting through python easy

This commit is contained in:
2025-06-16 12:01:03 -04:00
parent 94a0fa947a
commit 79c585892f
24 changed files with 1467 additions and 55 deletions

View File

@@ -39,8 +39,8 @@
#include "utilities.h"
#include "quill/LogMacros.h"
namespace serif {
namespace polytrope {
namespace serif::polytrope {
namespace laneEmden {
@@ -382,7 +382,6 @@ void PolySolver::setInitialGuess() const {
serif::probe::glVisView(*m_theta, m_mesh, "θ init");
serif::probe::glVisView(*m_phi, m_mesh, "φ init");
}
std::cout << "HERE" << std::endl;
}
@@ -497,5 +496,5 @@ solverBundle PolySolver::setupNewtonSolver() const {
return solver;
}
} // namespace polytrope
} // namespace serif
} // namespace serif::polytrope

View File

@@ -271,7 +271,14 @@ public: // Public methods
* @return A reference to the `mfem::GridFunction` storing the \f$\theta\f$ solution.
* @note The solution is populated after `solve()` has been successfully called.
*/
mfem::GridFunction& getSolution() const { return *m_theta; }
mfem::GridFunction& getTheta() const { return *m_theta; }
/**
* @brief Gets a reference to the solution grid function for \f$\phi\f$.
* @return A reference to the `mfem::GridFunction` storing the \f$\phi\f$ solution.
* @note The solution is populated after `solve()` has been successfully called.
*/
mfem::GridFunction& getPhi() const { return *m_phi; }
private: // Private Attributes
// --- Configuration and Logging ---

View File

@@ -119,7 +119,6 @@ namespace serif::utilities {
mfem::IsoparametricTransformation T;
mesh->GetElementTransformation(ne, &T);
phi_gf.GetCurl(T, curl_mag_vec);
std::cout << "HERE" << std::endl;
}
mfem::L2_FECollection fac(order, dim);
mfem::FiniteElementSpace fs(mesh, &fac);

View File

@@ -31,27 +31,26 @@
* @brief A collection of utilities for working with MFEM and solving the lane-emden equation.
*/
namespace serif {
namespace polytrope {
/**
namespace serif::polytrope {
/**
* @namespace polyMFEMUtils
* @brief A namespace for utilities for working with MFEM and solving the lane-emden equation.
*/
namespace polyMFEMUtils {
/**
namespace polyMFEMUtils {
/**
* @brief A class for nonlinear power integrator.
*/
class NonlinearPowerIntegrator: public mfem::NonlinearFormIntegrator {
public:
/**
class NonlinearPowerIntegrator: public mfem::NonlinearFormIntegrator {
public:
/**
* @brief Constructor for NonlinearPowerIntegrator.
*
* @param coeff The function coefficient.
* @param n The polytropic index.
*/
NonlinearPowerIntegrator(double n);
NonlinearPowerIntegrator(double n);
/**
/**
* @brief Assembles the element vector.
*
* @param el The finite element.
@@ -59,8 +58,8 @@ namespace polyMFEMUtils {
* @param elfun The element function.
* @param elvect The element vector to be assembled.
*/
virtual void AssembleElementVector(const mfem::FiniteElement &el, mfem::ElementTransformation &Trans, const mfem::Vector &elfun, mfem::Vector &elvect) override;
/**
virtual void AssembleElementVector(const mfem::FiniteElement &el, mfem::ElementTransformation &Trans, const mfem::Vector &elfun, mfem::Vector &elvect) override;
/**
* @brief Assembles the element gradient.
*
* @param el The finite element.
@@ -68,40 +67,39 @@ namespace polyMFEMUtils {
* @param elfun The element function.
* @param elmat The element matrix to be assembled.
*/
virtual void AssembleElementGrad (const mfem::FiniteElement &el, mfem::ElementTransformation &Trans, const mfem::Vector &elfun, mfem::DenseMatrix &elmat) override;
private:
serif::config::Config& m_config = serif::config::Config::getInstance();
serif::probe::LogManager& m_logManager = serif::probe::LogManager::getInstance();
quill::Logger* m_logger = m_logManager.getLogger("log");
double m_polytropicIndex;
double m_epsilon;
static constexpr double m_regularizationRadius = 0.15; ///< Regularization radius for the epsilon function, used to avoid singularities in the power law.
static constexpr double m_regularizationCoeff = 1.0/6.0; ///< Coefficient for the regularization term, used to ensure smoothness in the power law.
};
virtual void AssembleElementGrad (const mfem::FiniteElement &el, mfem::ElementTransformation &Trans, const mfem::Vector &elfun, mfem::DenseMatrix &elmat) override;
private:
serif::config::Config& m_config = serif::config::Config::getInstance();
serif::probe::LogManager& m_logManager = serif::probe::LogManager::getInstance();
quill::Logger* m_logger = m_logManager.getLogger("log");
double m_polytropicIndex;
double m_epsilon;
static constexpr double m_regularizationRadius = 0.15; ///< Regularization radius for the epsilon function, used to avoid singularities in the power law.
static constexpr double m_regularizationCoeff = 1.0/6.0; ///< Coefficient for the regularization term, used to ensure smoothness in the power law.
};
inline double dfmod(const double epsilon, const double n) {
if (n == 0.0) {
return 0.0;
inline double dfmod(const double epsilon, const double n) {
if (n == 0.0) {
return 0.0;
}
if (n == 1.0) {
return 1.0;
}
return n * std::pow(epsilon, n - 1.0);
}
if (n == 1.0) {
return 1.0;
inline double fmod(const double theta, const double n, const double epsilon) {
if (n == 0.0) {
return 1.0;
}
// For n != 0
const double y_prime_at_epsilon = dfmod(epsilon, n); // Uses the robust dfmod
const double y_at_epsilon = std::pow(epsilon, n); // epsilon^n
// f_mod(theta) = y_at_epsilon + y_prime_at_epsilon * (theta - epsilon)
return y_at_epsilon + y_prime_at_epsilon * (theta - epsilon);
}
return n * std::pow(epsilon, n - 1.0);
}
inline double fmod(const double theta, const double n, const double epsilon) {
if (n == 0.0) {
return 1.0;
}
// For n != 0
const double y_prime_at_epsilon = dfmod(epsilon, n); // Uses the robust dfmod
const double y_at_epsilon = std::pow(epsilon, n); // epsilon^n
// f_mod(theta) = y_at_epsilon + y_prime_at_epsilon * (theta - epsilon)
return y_at_epsilon + y_prime_at_epsilon * (theta - epsilon);
}
} // namespace polyMFEMUtils
} // namespace polytrope
} // namespace serif
} // namespace polyMFEMUtils
}