feat(poly): started work on penalty term in variational form

This commit is contained in:
2025-03-19 10:09:37 -04:00
parent f512f10096
commit b98f6b6ebd
4 changed files with 47 additions and 104 deletions

View File

@@ -25,17 +25,17 @@
namespace polycoeff{ namespace polycoeff{
double nonlinearSourceCoeff(const mfem::Vector &x) double nonlinearSourceCoeff(const mfem::Vector &x)
{ {
double r = x.Norml2(); return 1;
return std::pow(r, 2); // double r = x(0)*x(0) + x(1)*x(1) + x(2)*x(2);
// return std::pow(r, 2);
} }
void diffusionCoeff(const mfem::Vector &x, mfem::Vector &v) void diffusionCoeff(const mfem::Vector &x, mfem::Vector &v)
{ {
v.SetSize(3); v.SetSize(3);
double r = x.Norml2(); v = -1;
for (int i = 0; i < 3; i++) { // double r = x(0)*x(0) + x(1)*x(1) + x(2)*x(2);
v(i) = -std::pow(r, 2); // v = -std::pow(r, 2);
}
} }
double x1(const double n) double x1(const double n)

View File

@@ -21,6 +21,7 @@
#include "mfem.hpp" #include "mfem.hpp"
#include <string> #include <string>
#include <stdexcept>
#include "polySolver.h" #include "polySolver.h"
#include "polyMFEMUtils.h" #include "polyMFEMUtils.h"
@@ -75,6 +76,12 @@ PolySolver::PolySolver(double n, double order, mfem::Mesh& mesh_)
nonlinearForm(std::make_unique<mfem::NonlinearForm>(feSpace.get())), nonlinearForm(std::make_unique<mfem::NonlinearForm>(feSpace.get())),
u(std::make_unique<mfem::GridFunction>(feSpace.get())) { u(std::make_unique<mfem::GridFunction>(feSpace.get())) {
// --- Check the polytropic index ---
if (n > 4.99 || n < 0.0) {
LOG_ERROR(logger, "The polytropic index n must be less than 5.0 and greater than 0.0. Currently it is {}", n);
throw std::runtime_error("The polytropic index n must be less than 5.0 and greater than 0.0. Currently it is " + std::to_string(n));
}
diffusionCoeff = std::make_unique<mfem::VectorFunctionCoefficient>(mesh.SpaceDimension(), polycoeff::diffusionCoeff); diffusionCoeff = std::make_unique<mfem::VectorFunctionCoefficient>(mesh.SpaceDimension(), polycoeff::diffusionCoeff);
nonlinearSourceCoeff = std::make_unique<mfem::FunctionCoefficient>(polycoeff::nonlinearSourceCoeff); nonlinearSourceCoeff = std::make_unique<mfem::FunctionCoefficient>(polycoeff::nonlinearSourceCoeff);
@@ -103,11 +110,18 @@ void PolySolver::solve(){
mfem::FunctionCoefficient initCoeff ( mfem::FunctionCoefficient initCoeff (
[this](const mfem::Vector &x) { [this](const mfem::Vector &x) {
double r = x.Norml2(); double r = x.Norml2();
double theta = laneEmden::thetaSerieseExpansion(r, n, 10); double theta = laneEmden::thetaSerieseExpansion(r, n, 12);
return theta; return theta;
// double radius = Probe::getMeshRadius(mesh);
// double u = 1/radius;
// return -std::pow((u*r), 2)+1.0;
} }
); );
u->ProjectCoefficient(initCoeff); u->ProjectCoefficient(initCoeff);
if (config.get<bool>("Poly:Solver:ViewInitialGuess", false)) {
Probe::glVisView(*u, mesh, "initialGuess");
}
// mfem::DenseMatrix centerPoint(mesh.SpaceDimension(), 7); // mfem::DenseMatrix centerPoint(mesh.SpaceDimension(), 7);
mfem::DenseMatrix centerPoint(mesh.SpaceDimension(), 1); mfem::DenseMatrix centerPoint(mesh.SpaceDimension(), 1);
centerPoint(0, 0) = 0.0; centerPoint(0, 0) = 0.0;

View File

@@ -191,4 +191,17 @@ namespace polyMFEMUtils {
} }
} }
ConstraintIntegrator::ConstraintIntegrator(const double gamma): m_gamma(gamma) {
LOG_INFO(m_logger, "Initializing Constraint Integrator...");
m_originCoordinateMatrix.SetSize(3, 1);
m_originCoordinateMatrix(0, 0) = 0.0;
m_originCoordinateMatrix(1, 0) = 0.0;
m_originCoordinateMatrix(2, 0) = 0.0;
}
void ConstraintIntegrator::AssembleElementMatrix(const mfem::FiniteElement &el, mfem::ElementTransformation &Trans, mfem::DenseMatrix &elmat) {
}
} // namespace polyMFEMUtils } // namespace polyMFEMUtils

View File

@@ -166,107 +166,23 @@ namespace polyMFEMUtils {
virtual void AssembleElementGrad(const mfem::FiniteElement &el, mfem::ElementTransformation &Trans, const mfem::Vector &elfun, mfem::DenseMatrix &elmat) override; virtual void AssembleElementGrad(const mfem::FiniteElement &el, mfem::ElementTransformation &Trans, const mfem::Vector &elfun, mfem::DenseMatrix &elmat) override;
}; };
/** class ConstraintIntegrator: public mfem::BilinearFormIntegrator {
* @brief A class for constraint integrator. private:
*/ Config& m_config = Config::getInstance();
class ConstraintIntegrator: public mfem::NonlinearFormIntegrator { Probe::LogManager& logManager = Probe::LogManager::getInstance();
private: quill::Logger* m_logger = logManager.getLogger("log");
mfem::Coefficient &eta; const double m_gamma;
mfem::Array<int> m_originElementIDs;
mfem::Array<mfem::IntegrationPoint> m_originIntegrationPoints;
mfem::DenseMatrix m_originCoordinateMatrix;
public:
ConstraintIntegrator(const double gamma);
~ConstraintIntegrator() = default;
public: void AssembleElementMatrix(const mfem::FiniteElement &el, mfem::ElementTransformation &Trans, mfem::DenseMatrix &elmat) override;
/**
* @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:
Config& config = Config::getInstance();
mfem::NonlinearForm &nfl;
mfem::LinearForm &C;
double C_val;
int lambdaDofOffset;
mutable mfem::SparseMatrix *lastJacobian = nullptr;
public:
AugmentedOperator(mfem::NonlinearForm &nfl_, mfem::LinearForm &C_, int lambdaDofOffset_, double C_val_);
~AugmentedOperator();
virtual void Mult(const mfem::Vector &x, mfem::Vector &y) const override;
virtual mfem::Operator &GetGradient(const mfem::Vector &x) const override;
};
/**
* @brief Calculates the Gaussian integral.
*
* @param mesh The mesh.
* @param gaussianCoeff The Gaussian coefficient.
* @return The Gaussian integral.
*/
double calculateGaussianIntegral(mfem::Mesh &mesh, polyMFEMUtils::GaussianCoefficient &gaussianCoeff);
class ZeroSlopeNewtonSolver : public mfem::NewtonSolver {
private:
Config& config = Config::getInstance();
Probe::LogManager& logManager = Probe::LogManager::getInstance();
quill::Logger* logger = logManager.getLogger("log");
double alpha; // The penalty term for the flat slope at zero
std::vector<double> zeroSlopeCoordinate; // The coordinate of the zero slope point
int zeroSlopeElemID = -1;
double zeroIPReferenceCoord[4] = {0.0, 0.0, 0.0, 1.0};
mfem::IntegrationPoint zeroIP;
mfem::Array<int> zeroSlopeConnectedElements;
std::vector<mfem::IntegrationPoint> zeroSlopeIPs;
std::vector<mfem::Array<int>> zeroSlopeDofs;
std::unique_ptr<mfem::GridFunction> u_gf;
mutable mfem::SparseMatrix *grad = nullptr;
void ComputeConstrainedResidual(const mfem::Vector &x, mfem::Vector &r) const;
void ComputeConstrainedGradient(const mfem::Vector &x) const;
public:
ZeroSlopeNewtonSolver(double alpha_, std::vector<double> zeroSlopeCoordinate_);
~ZeroSlopeNewtonSolver();
// virtual void ProcessNewState(const mfem::Vector &x) const;
virtual void SetOperator(const mfem::Operator &op) override;
void Mult(const mfem::Vector &b, mfem::Vector &x) const override;
};
} // namespace polyMFEMUtils } // namespace polyMFEMUtils
#endif // POLYMFEMUTILS_H #endif // POLYMFEMUTILS_H