2025-02-20 15:28:00 -05:00
|
|
|
#ifndef POLYMFEMUTILS_H
|
|
|
|
|
#define POLYMFEMUTILS_H
|
|
|
|
|
|
2025-02-12 16:44:10 -05:00
|
|
|
#include "mfem.hpp"
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
|
2025-02-14 10:50:07 -05:00
|
|
|
/**
|
2025-02-19 14:35:15 -05:00
|
|
|
* @file polyMFEMUtils.h
|
|
|
|
|
* @brief A collection of utilities for working with MFEM and solving the lane-emden equation.
|
2025-02-14 10:50:07 -05:00
|
|
|
*/
|
2025-02-12 16:44:10 -05:00
|
|
|
|
|
|
|
|
|
2025-02-14 10:50:07 -05:00
|
|
|
/**
|
2025-02-19 14:35:15 -05:00
|
|
|
* @namespace polyMFEMUtils
|
|
|
|
|
* @brief A namespace for utilities for working with MFEM and solving the lane-emden equation.
|
2025-02-14 10:50:07 -05:00
|
|
|
*/
|
2025-02-19 14:35:15 -05:00
|
|
|
namespace polyMFEMUtils {
|
|
|
|
|
/**
|
|
|
|
|
* @brief A class for nonlinear power integrator.
|
|
|
|
|
*/
|
|
|
|
|
class NonlinearPowerIntegrator: public mfem::NonlinearFormIntegrator {
|
|
|
|
|
private:
|
|
|
|
|
mfem::Coefficient &coeff_;
|
|
|
|
|
double polytropicIndex;
|
2025-02-12 16:44:10 -05:00
|
|
|
public:
|
2025-02-14 10:50:07 -05:00
|
|
|
/**
|
2025-02-19 14:35:15 -05:00
|
|
|
* @brief Constructor for NonlinearPowerIntegrator.
|
2025-02-14 10:50:07 -05:00
|
|
|
*
|
2025-02-19 14:35:15 -05:00
|
|
|
* @param coeff The function coefficient.
|
|
|
|
|
* @param n The polytropic index.
|
2025-02-14 10:50:07 -05:00
|
|
|
*/
|
2025-02-19 14:35:15 -05:00
|
|
|
NonlinearPowerIntegrator(mfem::Coefficient &coeff, double n);
|
2025-02-12 16:44:10 -05:00
|
|
|
|
2025-02-14 10:50:07 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Assembles the element vector.
|
|
|
|
|
*
|
|
|
|
|
* @param el The finite element.
|
|
|
|
|
* @param Trans The element transformation.
|
|
|
|
|
* @param elfun The element function.
|
|
|
|
|
* @param elvect The element vector to be assembled.
|
|
|
|
|
*/
|
2025-02-12 16:44:10 -05:00
|
|
|
virtual void AssembleElementVector(const mfem::FiniteElement &el, mfem::ElementTransformation &Trans, const mfem::Vector &elfun, mfem::Vector &elvect) override;
|
2025-02-14 10:50:07 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Assembles the element gradient.
|
|
|
|
|
*
|
|
|
|
|
* @param el The finite element.
|
|
|
|
|
* @param Trans The element transformation.
|
|
|
|
|
* @param elfun The element function.
|
|
|
|
|
* @param elmat The element matrix to be assembled.
|
|
|
|
|
*/
|
2025-02-19 14:35:15 -05:00
|
|
|
virtual void AssembleElementGrad (const mfem::FiniteElement &el, mfem::ElementTransformation &Trans, const mfem::Vector &elfun, mfem::DenseMatrix &elmat) override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief A wrapper class for bilinear integrator.
|
|
|
|
|
*/
|
|
|
|
|
class BilinearIntegratorWrapper : public mfem::NonlinearFormIntegrator
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
mfem::BilinearFormIntegrator *integrator;
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Constructor for BilinearIntegratorWrapper.
|
|
|
|
|
*
|
|
|
|
|
* @param integratorInput The bilinear form integrator input.
|
|
|
|
|
*/
|
|
|
|
|
BilinearIntegratorWrapper(mfem::BilinearFormIntegrator *integratorInput);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Destructor for BilinearIntegratorWrapper.
|
|
|
|
|
*/
|
|
|
|
|
virtual ~BilinearIntegratorWrapper();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Assembles the element vector.
|
|
|
|
|
*
|
|
|
|
|
* @param el The finite element.
|
|
|
|
|
* @param Trans The element transformation.
|
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Assembles the element gradient.
|
|
|
|
|
*
|
|
|
|
|
* @param el The finite element.
|
|
|
|
|
* @param Trans The element transformation.
|
|
|
|
|
* @param elfun The element function.
|
|
|
|
|
* @param elmat The element matrix to be assembled.
|
|
|
|
|
*/
|
2025-02-12 16:44:10 -05:00
|
|
|
virtual void AssembleElementGrad(const mfem::FiniteElement &el, mfem::ElementTransformation &Trans, const mfem::Vector &elfun, mfem::DenseMatrix &elmat) override;
|
2025-02-19 14:35:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief A class for composite nonlinear integrator.
|
|
|
|
|
*/
|
|
|
|
|
class CompositeNonlinearIntegrator: public mfem::NonlinearFormIntegrator {
|
|
|
|
|
private:
|
|
|
|
|
std::vector<mfem::NonlinearFormIntegrator*> integrators;
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Constructor for CompositeNonlinearIntegrator.
|
|
|
|
|
*/
|
|
|
|
|
CompositeNonlinearIntegrator();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Destructor for CompositeNonlinearIntegrator.
|
|
|
|
|
*/
|
|
|
|
|
virtual ~CompositeNonlinearIntegrator();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Adds an integrator to the composite integrator.
|
|
|
|
|
*
|
|
|
|
|
* @param integrator The nonlinear form integrator to add.
|
|
|
|
|
*/
|
|
|
|
|
void add_integrator(mfem::NonlinearFormIntegrator *integrator);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Assembles the element vector.
|
|
|
|
|
*
|
|
|
|
|
* @param el The finite element.
|
|
|
|
|
* @param Trans The element transformation.
|
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Assembles the element gradient.
|
|
|
|
|
*
|
|
|
|
|
* @param el The finite element.
|
|
|
|
|
* @param Trans The element transformation.
|
|
|
|
|
* @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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief A class for constraint integrator.
|
|
|
|
|
*/
|
|
|
|
|
class ConstraintIntegrator: public mfem::NonlinearFormIntegrator {
|
|
|
|
|
private:
|
|
|
|
|
mfem::Coefficient η
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Constructor for ConstraintIntegrator.
|
|
|
|
|
*
|
|
|
|
|
* @param eta The coefficient.
|
|
|
|
|
*/
|
|
|
|
|
ConstraintIntegrator(mfem::Coefficient &eta_);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Assembles the element vector.
|
|
|
|
|
*
|
|
|
|
|
* @param el The finite element.
|
|
|
|
|
* @param Trans The element transformation.
|
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Assembles the element gradient.
|
|
|
|
|
*
|
|
|
|
|
* @param el The finite element.
|
|
|
|
|
* @param Trans The element transformation.
|
|
|
|
|
* @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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class GaussianCoefficient : public mfem::Coefficient {
|
|
|
|
|
private:
|
|
|
|
|
double stdDev;
|
|
|
|
|
double norm_coeff;
|
|
|
|
|
public:
|
|
|
|
|
GaussianCoefficient(double stdDev);
|
|
|
|
|
virtual double Eval(mfem::ElementTransformation &T, const mfem::IntegrationPoint &ip) override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AugmentedOperator : public mfem::Operator {
|
|
|
|
|
private:
|
2025-02-20 15:28:00 -05:00
|
|
|
mfem::NonlinearForm &nfl;
|
|
|
|
|
mfem::LinearForm &C;
|
2025-02-19 14:35:15 -05:00
|
|
|
int lambdaDofOffset;
|
2025-02-20 15:28:00 -05:00
|
|
|
mutable mfem::SparseMatrix *lastJacobian = nullptr;
|
2025-02-19 14:35:15 -05:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
AugmentedOperator(mfem::NonlinearForm &nfl_, mfem::LinearForm &C_, int lambdaDofOffset_);
|
|
|
|
|
~AugmentedOperator();
|
|
|
|
|
|
|
|
|
|
virtual void Mult(const mfem::Vector &x, mfem::Vector &y) const override;
|
|
|
|
|
|
|
|
|
|
virtual mfem::Operator &GetGradient(const mfem::Vector &x) const override;
|
|
|
|
|
};
|
2025-02-20 15:28:00 -05:00
|
|
|
} // namespace polyMFEMUtils
|
|
|
|
|
|
|
|
|
|
#endif // POLYMFEMUTILS_H
|