v0.15.0
Loading...
Searching...
No Matches
mofem/atom_tests/dg_projection.cpp

Testing Discontinuous Galerkin (DG) projection operators.

Testing Discontinuous Galerkin (DG) projection operatorsThis test validates the accuracy of DG projection algorithms used to represent functions on finite element meshes. The projection process involves solving local least-squares problems to find the best approximation of a given function in the finite element space.

Mathematical background:

Test workflow:

  1. Setup finite element space with polynomial basis functions
  2. Assemble mass matrix and load vector for the projection problem
  3. Solve the projection system to get approximation coefficients
  4. Evaluate the projected function and compare with analytical values
/**
* @file dg_projection.cpp
* @example mofem/atom_tests/dg_projection.cpp
*
* @brief Testing Discontinuous Galerkin (DG) projection operators
*
* This test validates the accuracy of DG projection algorithms used to
* represent functions on finite element meshes. The projection process
* involves solving local least-squares problems to find the best
* approximation of a given function in the finite element space.
*
* Mathematical background:
* - DG projection finds coefficients c_i such that ∑c_i φ_i minimizes
* ||u - ∑c_i φ_i||_{L2} where u is the target function and φ_i are basis functions
* - This leads to the normal equation: M c = f, where M is the mass matrix
* and f is the load vector from projecting the target function
* - The test verifies that the projection accurately represents a polynomial
* function that should be exactly representable in the chosen finite element space
*
* Test workflow:
* 1. Setup finite element space with polynomial basis functions
* 2. Assemble mass matrix and load vector for the projection problem
* 3. Solve the projection system to get approximation coefficients
* 4. Evaluate the projected function and compare with analytical values
*/
#include <MoFEM.hpp>
using namespace MoFEM;
static char help[] = "DG Projection Test - validates discontinuous Galerkin "
"projection accuracy\n\n";
constexpr char FIELD_NAME[] = "U";
constexpr int BASE_DIM = 1;
constexpr int FIELD_DIM = 1;
constexpr int SPACE_DIM = 2;
constexpr int order = 2;
template <int DIM> struct ElementsAndOps {};
template <> struct ElementsAndOps<2> {
using DomainEleOp = DomainEle::UserDataOperator;
};
using DomainEleOp = DomainEle::UserDataOperator;
auto fun = [](const double x, const double y, const double z) {
return x + y + x * x + y * y;
};
struct AtomTest {
AtomTest(MoFEM::Interface &m_field) : mField(m_field) {}
private:
struct CommonData {
boost::shared_ptr<MatrixDouble> invJacPtr;
boost::shared_ptr<VectorDouble> approxVals;
boost::shared_ptr<MatrixDouble> approxGradVals;
boost::shared_ptr<MatrixDouble> approxHessianVals;
};
struct OpError;
};
struct AtomTest::OpError : public DomainEleOp {
boost::shared_ptr<CommonData> commonDataPtr;
OpError(boost::shared_ptr<MatrixDouble> data_ptr)
: DomainEleOp(NOSPACE, OPSPACE), dataPtr(data_ptr) {}
MoFEMErrorCode doWork(int side, EntityType type, EntData &data) {
const int nb_integration_pts = getGaussPts().size2();
auto t_val = getFTensor1FromMat<1>(*(dataPtr));
auto t_coords = getFTensor1CoordsAtGaussPts();
for (int gg = 0; gg != nb_integration_pts; ++gg) {
double projected_value = t_val(0);
double analytical_value = fun(t_coords(0), t_coords(1), t_coords(2));
double error = projected_value - analytical_value;
constexpr double eps = 1e-8;
if (std::abs(error) > eps) {
MOFEM_LOG("SELF", Sev::error)
<< "Projection error too large: " << error << " at point ("
<< t_coords(0) << ", " << t_coords(1) << ")"
<< " projected=" << projected_value
<< " analytical=" << analytical_value;
SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
"DG projection failed accuracy test");
}
++t_val;
++t_coords;
}
MOFEM_LOG("SELF", Sev::noisy) << "DG projection accuracy validation passed";
}
private:
boost::shared_ptr<MatrixDouble> dataPtr;
};
//! [Run programme]
}
//! [Run programme]
//! [Read mesh]
}
//! [Read mesh]
//! [Set up problem]
}
//! [Set up problem]
//! [Push operators to pipeline]
auto rule = [](int, int, int p) -> int { return 2 * p; };
CHKERR pipeline_mng->setDomainLhsIntegrationRule(rule);
CHKERR pipeline_mng->setDomainRhsIntegrationRule(rule);
auto beta = [](const double, const double, const double) { return 1; };
pipeline_mng->getOpDomainLhsPipeline().push_back(
pipeline_mng->getOpDomainRhsPipeline().push_back(
}
//! [Push operators to pipeline]
//! [Solve]
MOFEM_LOG("WORLD", Sev::inform) << "Solving DG projection system";
auto solver = pipeline_mng->createKSP();
CHKERR KSPSetFromOptions(solver);
CHKERR KSPSetUp(solver);
auto dm = simpleInterface->getDM();
auto D = createDMVector(dm);
auto F = vectorDuplicate(D);
CHKERR KSPSolve(solver, F, D);
CHKERR VecGhostUpdateBegin(D, INSERT_VALUES, SCATTER_FORWARD);
CHKERR VecGhostUpdateEnd(D, INSERT_VALUES, SCATTER_FORWARD);
CHKERR DMoFEMMeshToLocalVector(dm, D, INSERT_VALUES, SCATTER_REVERSE);
}
//! [Solve]
//! [Check results]
auto pipeline_mng = mField.getInterface<PipelineManager>();
pipeline_mng->getDomainLhsFE().reset();
pipeline_mng->getDomainRhsFE().reset();
pipeline_mng->getOpDomainRhsPipeline().clear();
auto rule = [](int, int, int p) -> int { return 2 * p + 1; };
CHKERR pipeline_mng->setDomainRhsIntegrationRule(rule);
auto entity_data_l2 = boost::make_shared<EntitiesFieldData>(MBENTITYSET);
auto mass_ptr = boost::make_shared<MatrixDouble>();
auto coeffs_ptr = boost::make_shared<MatrixDouble>();
auto data_ptr = boost::make_shared<MatrixDouble>();
auto op_this =
new OpLoopThis<DomainEle>(mField, simple->getDomainFEName(), Sev::noisy);
pipeline_mng->getOpDomainRhsPipeline().push_back(op_this);
pipeline_mng->getOpDomainRhsPipeline().push_back(new OpDGProjectionEvaluation(
data_ptr, coeffs_ptr, entity_data_l2, AINSWORTH_LEGENDRE_BASE, L2));
pipeline_mng->getOpDomainRhsPipeline().push_back(new OpError(data_ptr));
auto fe_physics_ptr = op_this->getThisFEPtr();
fe_physics_ptr->getRuleHook = [](int, int, int p) { return 2 * p; };
fe_physics_ptr->getOpPtrVector().push_back(new OpDGProjectionMassMatrix(
order, mass_ptr, entity_data_l2, AINSWORTH_LEGENDRE_BASE, L2));
fe_physics_ptr->getOpPtrVector().push_back(
fe_physics_ptr->getOpPtrVector().push_back(new OpDGProjectionCoefficients(
data_ptr, coeffs_ptr, mass_ptr, entity_data_l2, AINSWORTH_LEGENDRE_BASE,
L2));
CHKERR pipeline_mng->loopFiniteElements();
}
//! [Check results]
int main(int argc, char *argv[]) {
MoFEM::Core::Initialize(&argc, &argv, NULL, help);
try {
//! [Register MoFEM discrete manager in PETSc]
DMType dm_name = "DMMOFEM";
//! [Register MoFEM discrete manager in PETSc]
//! [Create MoAB]
moab::Core mb_instance;
moab::Interface &moab = mb_instance;
//! [Create MoAB]
//! [Create MoFEM]
MoFEM::Core core(moab);
MoFEM::Interface &m_field = core;
//! [Create MoFEM]
//! [Execute DG Projection Test]
AtomTest ex(m_field);
CHKERR ex.runProblem();
//! [Execute DG Projection Test]
}
}
void simple(double P1[], double P2[], double P3[], double c[], const int N)
Definition acoustic.cpp:69
static char help[]
int main()
static const double eps
ElementsAndOps< SPACE_DIM >::DomainEle DomainEle
constexpr char FIELD_NAME[]
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::LinearForm< GAUSS >::OpSource< 1, FIELD_DIM > OpDomainSource
constexpr int SPACE_DIM
[Define dimension]
constexpr int FIELD_DIM
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpMass< 1, FIELD_DIM > OpDomainMass
#define CATCH_ERRORS
Catch errors.
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition definitions.h:60
@ L2
field with C-1 continuity
Definition definitions.h:88
@ H1
continuous field
Definition definitions.h:85
@ NOSPACE
Definition definitions.h:83
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
auto fun
constexpr int BASE_DIM
constexpr int order
Order displacement.
@ F
PetscErrorCode DMoFEMMeshToLocalVector(DM dm, Vec l, InsertMode mode, ScatterMode scatter_mode)
set local (or ghosted) vector values on mesh for partition only
Definition DMMoFEM.cpp:514
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition DMMoFEM.cpp:43
auto createDMVector(DM dm)
Get smart vector from DM.
Definition DMMoFEM.hpp:1234
boost::ptr_deque< UserDataOperator > & getOpDomainLhsPipeline()
Get the Op Domain Lhs Pipeline object.
SmartPetscObj< KSP > createKSP(SmartPetscObj< DM > dm=nullptr)
Create KSP (linear) solver.
boost::ptr_deque< UserDataOperator > & getOpDomainRhsPipeline()
Get the Op Domain Rhs Pipeline object.
@ PETSC
Standard PETSc assembly.
#define MOFEM_LOG(channel, severity)
Log.
constexpr char FIELD_NAME[]
auto fun
Function to approximate.
constexpr int FIELD_DIM
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpMass< BASE_DIM, FIELD_DIM > OpDomainMass
OPerator to integrate mass matrix for least square approximation.
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::LinearForm< GAUSS >::OpSource< BASE_DIM, FIELD_DIM > OpDomainSource
Operator to integrate the right hand side matrix for the problem.
double D
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
SmartPetscObj< Vec > vectorDuplicate(Vec vec)
Create duplicate vector of smart vector.
boost::shared_ptr< VectorDouble > approxVals
SmartPetscObj< Vec > L2Vec
boost::shared_ptr< MatrixDouble > invJacPtr
boost::shared_ptr< MatrixDouble > approxHessianVals
boost::shared_ptr< MatrixDouble > approxGradVals
Operator to evaluate errors.
boost::shared_ptr< CommonData > commonDataPtr
boost::shared_ptr< MatrixDouble > dataPtr
MoFEMErrorCode doWork(int side, EntityType type, EntData &data)
Simple * simpleInterface
MoFEMErrorCode checkResults()
[Solve]
MoFEMErrorCode solveSystem()
[Push operators to pipeline]
MoFEMErrorCode setupProblem()
[Read mesh]
MoFEMErrorCode readMesh()
[Run programme]
MoFEMErrorCode assembleSystem()
[Push operators to pipeline]
MoFEM::Interface & mField
MoFEMErrorCode runProblem()
[Run programme]
Core (interface) class.
Definition Core.hpp:82
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:72
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:118
Deprecated interface functions.
Data on single entity (This is passed as argument to DataOperator::doWork)
Specialization for double precision vector field values calculation.
Execute "this" element in the operator.
PipelineManager interface.
MoFEMErrorCode setDomainRhsIntegrationRule(RuleHookFun rule)
Set integration rule for domain right-hand side finite element.
MoFEMErrorCode setDomainLhsIntegrationRule(RuleHookFun rule)
Set integration rule for domain left-hand side finite element.
Simple interface for fast problem set-up.
Definition Simple.hpp:27
MoFEMErrorCode addDomainField(const std::string name, const FieldSpace space, const FieldApproximationBase base, const FieldCoefficientsNumber nb_of_coefficients, const TagType tag_type=MB_TAG_SPARSE, const enum MoFEMTypes bh=MF_ZERO, int verb=-1)
Add field on domain.
Definition Simple.cpp:261
MoFEMErrorCode loadFile(const std::string options, const std::string mesh_file_name, LoadFileFunc loadFunc=defaultLoadFileFunc)
Load mesh file.
Definition Simple.cpp:191
MoFEMErrorCode getOptions()
get options
Definition Simple.cpp:180
MoFEMErrorCode getDM(DM *dm)
Get DM.
Definition Simple.cpp:800
MoFEMErrorCode setFieldOrder(const std::string field_name, const int order, const Range *ents=NULL)
Set field order.
Definition Simple.cpp:575
MoFEMErrorCode setUp(const PetscBool is_partitioned=PETSC_TRUE)
Setup problem.
Definition Simple.cpp:736
intrusive_ptr for managing petsc objects
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.