v0.16.0
Loading...
Searching...
No Matches
SCL-0: Least square approximation
Note
Prerequisites of this tutorial include MSH-1: Create a 2D mesh from Gmsh


Note
Intended learning outcome:
  • general structure of a program developed using MoFEM
  • idea of Simple Interface in MoFEM and how to use it
  • idea of Domain element in MoFEM and how to use it
  • Use default Forms Integrators
  • how to push the developed UDOs to the Pipeline
  • utilisation of tools to convert outputs and visualise them in Paraview

Introduction

The aim of this tutorial is to demonstrate how MoFEM solves a simple least squares problem using a PETSc Krylov Subspace Solver (KSP). In this case, we approximate a simple analytical sinusoidal function, Eq. \eqref{eq:problemstatement}, and calculate the \(L^2\)-norm of the difference between the MoFEM approximation, \(u\), and the analytical function over a 2D domain \(\Omega\).

\[ \begin{equation} \label{eq:problemstatement} f(x,y) = \sin (10x) * \cos (10y) \end{equation} \]

The numerical approximation of the function, \(u\), is computed via the following weak form:

\[ \begin{equation} \label{eq:least_squares} \int_\Omega \delta u \, u \, \textrm{d} \Omega = \int_\Omega \delta u \, f \, \textrm{d} \Omega \quad \forall \, \delta u \in H^1(\Omega), \end{equation} \]

where \(\delta u\) is the test function and we approximate both \(u\) and \(\delta u\) in the Sobolev space \(H^1(\Omega)\), the space of square integrable functions with square integrable derivatives. There are different ways to arrive at Eq \eqref{eq:least_squares}. For example, we can minimise the square of the \(L^2\)-norm of the difference of the analytical and approximated functions:

\[ \begin{equation} \label{eq:sqnormdiff} \frac{1}{2} ||u-f||^2_{L^2} = \int_\Omega \frac{1}{2} (u - f)^2 d\Omega \end{equation} \]

To minimise it, we take the variation of this with respect to the numerical appriximation, \(u\), and set it to \(0\). Simple manipulation then suffices to recover our weak form.

Then, we check the correctness of the solution by requiring that the \(L^2\)-norm of the difference of the analytical and approximated functions be 0:

\[ \begin{equation} \label{eq:normdiff} ||u-f||_{L^2} = \left(\int_\Omega (u - f)^2 d\Omega \right)^{1/2} = 0. \end{equation} \]

Simple Interface

When reading the mesh, we use a Simple Interface. This interface will allow us to access and manipulate the mesh. Through it, we can assemble and push the operators we require for our problem. The Simple Interface also allows for mesh manipulation, so that it is possible to refine, remove degrees of freedom, and perform other operations directly through it (this tutorial does not cover these uses). The interface is accessed as follows:

MoFEMErrorCode Example::readMesh() {
}
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
MoFEMErrorCode readMesh()
[Run problem]
MoFEM::Interface & mField
Reference to MoFEM interface.
Definition plastic.cpp:227
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 getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.

The Simple Interface can then be used to add the field to the mesh, set the order of the fields, and set the problem up:

MoFEMErrorCode Example::setupProblem() {
// Add field
constexpr int order = 4;
}
constexpr char FIELD_NAME[]
constexpr int FIELD_DIM
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition definitions.h:60
@ H1
continuous field
Definition definitions.h:85
constexpr int order
MoFEMErrorCode setupProblem()
[Run problem]
Definition plastic.cpp:274
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 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:735

Further examples of using this interface will be seen at different points of the tutorial.

Discretisation and Operators

To solve the problem, it is necessary to discretise Eq. \eqref{eq:least_squares}. We approximate the fields as follows:

\[ \begin{align} \label{eq:fields_discret} \delta u^h = \sum_{i=0}^n \delta u_i N_i, \\ u^h = \sum_{j=0}^n u_j N_j, \end{align} \]

where the superscript \(h\) denotes the approximated function, \(n\) is the number of degres of freedom, and the \(N\)s are the Hierarchical shape functions. Now, we can re-write Eq. \eqref{eq:least_squares} as:

\[ \begin{equation} \label{eq:discr_weak} \sum_{i=0}^n \sum_{j=0}^n \delta u_i \int_\Omega N_i \, N_j \, \textrm{d} \Omega \, u_j = \sum_{i=0}^n \delta u_i \int_\Omega N_i \, f \, \textrm{d} \Omega \quad \forall \, \delta u \in H^1(\Omega). \end{equation} \]

We then recast this to matrix form:

\[ \begin{equation} \label{eq:discr_mat} \textbf{K} \textbf{u} = \textbf{b}, \end{equation} \]

where the matrix and vector elements are:

\[ \begin{align} \label{eq:mat_vec} [\textrm{K}_{ij}] = \int_\Omega N_i \, N_j \, \textrm{d} \Omega, \\ [\textrm{b}_i] = \int_\Omega N_i \, f \, \textrm{d} \Omega. \end{align} \]

Now that we have our LHS and RHS, we can turn to how they are pushed to the pipeline:

MoFEMErrorCode Example::assembleSystem() {
PipelineManager *pipeline_mng = mField.getInterface<PipelineManager>();
auto beta = [](const double, const double, const double) { return 1; };
pipeline_mng->getOpDomainLhsPipeline().push_back(
pipeline_mng->getOpDomainRhsPipeline().push_back(
}
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::LinearForm< GAUSS >::OpSource< 1, FIELD_DIM > OpDomainSource
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpMass< 1, FIELD_DIM > OpDomainMass
MoFEMErrorCode assembleSystem()
[Push operators to pipeline]
static ApproxFieldFunction< FIELD_DIM > approxFunction

In this case, we are simply pushing the OpDomainMass bilinear operator to the LHS domain pipeline and the OpDomainSource linear operator to the RHS domain pipeline. We use the beta lambda function to specify constant coefficients on the Mass term in the matrix. In this case, there is no coefficient that needs to be taken into account, so we just set beta to return 1. For the RHS, approxfunction stands for the analytical function.

Solving the problem

We are now ready to solve the problem in Eq. \eqref{eq:discr_mat}. We use a KSP solver and store the solution in vector D. To create vector D, we first need a discrete manager, which we call dm and obtain directly from the Simple interface we created before. The discrete manager is a PETSc object that manages communication between the algebraic structures and the data in the mesh. In this way, we ensure that D has the correct size for our problem without actually having to specify any of its parameters ourselves. This is implemented by:

MoFEMErrorCode Example::solveSystem() {
PipelineManager *pipeline_mng = mField.getInterface<PipelineManager>();
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);
}
@ F
PetscErrorCode DMoFEMMeshToLocalVector(DM dm, Vec l, InsertMode mode, ScatterMode scatter_mode, RowColData rc=RowColData::COL)
set local (or ghosted) vector values on mesh for partition only
Definition DMMoFEM.cpp:514
auto createDMVector(DM dm, RowColData rc=RowColData::COL)
Get smart vector from DM.
Definition DMMoFEM.hpp:1237
double D
SmartPetscObj< Vec > vectorDuplicate(Vec vec)
Create duplicate vector of smart vector.
MoFEMErrorCode solveSystem()
[Solve]
MoFEMErrorCode getDM(DM *dm)
Get DM.
Definition Simple.cpp:799

Checking the results

Finally, to compare the approximated and the analyitcal function, we use the following code:

MoFEMErrorCode Example::checkResults() {
PipelineManager *pipeline_mng = mField.getInterface<PipelineManager>();
pipeline_mng->getOpDomainPostProcPipeline().clear();
pipeline_mng->getOpDomainPostProcPipeline().push_back(
new OpCalculateScalarFieldValues(FIELD_NAME, commonDataPtr->approxVals));
pipeline_mng->getOpDomainPostProcPipeline().push_back(
CHKERR pipeline_mng->loopFiniteElementsPostProc();
CHKERR VecAssemblyBegin(commonDataPtr->L2Vec);
CHKERR VecAssemblyEnd(commonDataPtr->L2Vec);
CHKERR VecAssemblyBegin(commonDataPtr->resVec);
CHKERR VecAssemblyEnd(commonDataPtr->resVec);
double nrm2;
CHKERR VecNorm(commonDataPtr->resVec, NORM_2, &nrm2);
const double *array;
CHKERR VecGetArrayRead(commonDataPtr->L2Vec, &array);
if (mField.get_comm_rank() == 0)
PetscPrintf(PETSC_COMM_SELF, "Error %6.4e Vec norm %6.4e\n",
std::sqrt(array[0]), nrm2);
CHKERR VecRestoreArrayRead(commonDataPtr->L2Vec, &array);
constexpr double eps = 1e-8;
if (nrm2 > eps)
SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
"Not converged solution");
}
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
const double eps
Definition HenckyOps.hpp:13
boost::shared_ptr< CommonData > commonDataPtr
MoFEMErrorCode checkResults()
[Postprocess results]
virtual int get_comm_rank() const =0

Here, we reset the pipeline and push two new operators to the RHS. OpCalculateScalarFieldValues stores the values of the approximated solution in the commonDataPtr. OpError is an operator that calculates the \(L^2\) error and the norm (the square root of the error) of the difference between the approximation and analytical function and stores both values in the commonDataPtr. It is defined as follows:

template <> struct Example::OpError<1> : public DomainEleOp {
boost::shared_ptr<CommonData> commonDataPtr;
OpError(boost::shared_ptr<CommonData> &common_data_ptr)
: DomainEleOp(FIELD_NAME, OPROW), commonDataPtr(common_data_ptr) {}
MoFEMErrorCode doWork(int side, EntityType type, EntData &data) {
if (const size_t nb_dofs = data.getIndices().size()) {
const int nb_integration_pts = getGaussPts().size2();
auto t_w = getFTensor0IntegrationWeight();
auto t_val = getFTensor0FromVec(*(commonDataPtr->approxVals));
auto t_coords = getFTensor1CoordsAtGaussPts();
VectorDouble nf(nb_dofs, false);
nf.clear();
FTensor::Index<'i', 3> i;
const double volume = getMeasure();
auto t_row_base = data.getFTensor0N();
double error = 0;
for (int gg = 0; gg != nb_integration_pts; ++gg) {
const double alpha = t_w * volume;
double diff = t_val - Example::approxFunction(t_coords(0), t_coords(1),
t_coords(2));
error += alpha * pow(diff, 2);
for (size_t r = 0; r != nb_dofs; ++r) {
nf[r] += alpha * t_row_base * diff;
++t_row_base;
}
++t_w;
++t_val;
++t_coords;
}
const int index = 0;
CHKERR VecSetValue(commonDataPtr->L2Vec, index, error, ADD_VALUES);
CHKERR VecSetValues(commonDataPtr->resVec, data, &nf[0], ADD_VALUES);
}
}
};
std::string type
FTensor::Index< 'i', SPACE_DIM > i
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
UBlasVector< double > VectorDouble
Definition Types.hpp:68
static auto getFTensor0FromVec(V &data)
Get tensor rank 0 (scalar) form data vector.
MoFEMErrorCode VecSetValues(Vec V, const EntitiesFieldData::EntData &data, const double *ptr, InsertMode iora)
Assemble PETSc vector.
MoFEMErrorCode doWork(int side, EntityType type, EntData &data)
boost::shared_ptr< CommonData > commonDataPtr
Data on single entity (This is passed as argument to DataOperator::doWork)
FTensor::Tensor0< FTensor::PackPtr< double *, 1 > > getFTensor0N(const FieldApproximationBase base)
Get base function as Tensor0.
const VectorInt & getIndices() const
Get global indices of degrees of freedom on entity.

Finally, the code checks that the norm is smaller than a tolerance and throws an error otherwise.

Visualising the results

We can run the problem by running, from the build directory,:

./approximation -json_config example.json

This will run the code and produce an output mesh called out_approx.h5m. Calling convert.py on this mesh to convert it to a vtk file, we can then visualise it in paraview as below.

Figure 1: Output of the code.

Plain Code

/**
* \file approximation.cpp
* \example mofem/tutorials/scl-0_least_squares/approximation.cpp
*
* Using Simple interface and PipelineManager to solve least squares approximation problem.
*/
#include <MoFEM.hpp>
using namespace MoFEM;
static char help[] = "...\n\n";
#include <MoFEM.hpp>
constexpr char FIELD_NAME[] = "U";
constexpr int FIELD_DIM = 1;
constexpr int SPACE_DIM = 2;
template <int DIM> struct ElementsAndOps {};
template <> struct ElementsAndOps<2> {
};
template <> struct ElementsAndOps<3> {
};
using DomainEleOp = DomainEle::UserDataOperator;
template <int FIELD_DIM> struct ApproxFieldFunction;
template <> struct ApproxFieldFunction<1> {
double operator()(const double x, const double y, const double z) {
return sin(x * 10.) * cos(y * 10.);
}
};
struct Example {
Example(MoFEM::Interface &m_field) : mField(m_field) {}
private:
struct CommonData {
boost::shared_ptr<VectorDouble> approxVals;
};
boost::shared_ptr<CommonData> commonDataPtr;
template <int FIELD_DIM> struct OpError;
};
//! [OpError def]
template <> struct Example::OpError<1> : public DomainEleOp {
boost::shared_ptr<CommonData> commonDataPtr;
OpError(boost::shared_ptr<CommonData> &common_data_ptr)
: DomainEleOp(FIELD_NAME, OPROW), commonDataPtr(common_data_ptr) {}
MoFEMErrorCode doWork(int side, EntityType type, EntData &data) {
if (const size_t nb_dofs = data.getIndices().size()) {
const int nb_integration_pts = getGaussPts().size2();
auto t_w = getFTensor0IntegrationWeight();
auto t_val = getFTensor0FromVec(*(commonDataPtr->approxVals));
auto t_coords = getFTensor1CoordsAtGaussPts();
VectorDouble nf(nb_dofs, false);
nf.clear();
FTensor::Index<'i', 3> i;
const double volume = getMeasure();
auto t_row_base = data.getFTensor0N();
double error = 0;
for (int gg = 0; gg != nb_integration_pts; ++gg) {
const double alpha = t_w * volume;
double diff = t_val - Example::approxFunction(t_coords(0), t_coords(1),
t_coords(2));
error += alpha * pow(diff, 2);
for (size_t r = 0; r != nb_dofs; ++r) {
nf[r] += alpha * t_row_base * diff;
++t_row_base;
}
++t_w;
++t_val;
++t_coords;
}
const int index = 0;
CHKERR VecSetValue(commonDataPtr->L2Vec, index, error, ADD_VALUES);
CHKERR VecSetValues(commonDataPtr->resVec, data, &nf[0], ADD_VALUES);
}
}
};
//! [OpError def]
//! [Run programme]
}
//! [Run programme]
//! [Read mesh]
}
//! [Read mesh]
//! [Set up problem]
// Add field
constexpr int order = 4;
}
//! [Set up problem]
//! [Set integration rule]
auto rule = [](int, int, int p) -> int { return 2 * p; };
CHKERR pipeline_mng->setDomainLhsIntegrationRule(rule);
CHKERR pipeline_mng->setDomainRhsIntegrationRule(rule);
}
//! [Set integration rule]
//! [Create common data]
commonDataPtr = boost::make_shared<CommonData>();
commonDataPtr->L2Vec =
commonDataPtr->approxVals = boost::make_shared<VectorDouble>();
}
//! [Create common data]
//! [Boundary condition]
//! [Boundary condition]
//! [Push operators to pipeline]
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]
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]
auto post_proc_fe = boost::make_shared<PostProcEle>(mField);
auto u_ptr = boost::make_shared<VectorDouble>();
post_proc_fe->getOpPtrVector().push_back(
post_proc_fe->getOpPtrVector().push_back(
new OpPPMap(
post_proc_fe->getPostProcMesh(), post_proc_fe->getMapGaussPts(),
{{FIELD_NAME, u_ptr}},
{},
{},
{})
);
pipeline_mng->getDomainPostProcFE() = post_proc_fe;
CHKERR post_proc_fe->writeFile("out_approx.h5m");
}
//! [Postprocess results]
//! [Check results]
pipeline_mng->getOpDomainPostProcPipeline().clear();
pipeline_mng->getOpDomainPostProcPipeline().push_back(
pipeline_mng->getOpDomainPostProcPipeline().push_back(
CHKERR VecAssemblyBegin(commonDataPtr->L2Vec);
CHKERR VecAssemblyEnd(commonDataPtr->L2Vec);
CHKERR VecAssemblyBegin(commonDataPtr->resVec);
CHKERR VecAssemblyEnd(commonDataPtr->resVec);
double nrm2;
CHKERR VecNorm(commonDataPtr->resVec, NORM_2, &nrm2);
const double *array;
CHKERR VecGetArrayRead(commonDataPtr->L2Vec, &array);
if (mField.get_comm_rank() == 0)
PetscPrintf(PETSC_COMM_SELF, "Error %6.4e Vec norm %6.4e\n",
std::sqrt(array[0]), nrm2);
CHKERR VecRestoreArrayRead(commonDataPtr->L2Vec, &array);
constexpr double eps = 1e-8;
if (nrm2 > eps)
SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
"Not converged solution");
}
//! [Check results]
int main(int argc, char *argv[]) {
// Initialisation of MoFEM/PETSc and MOAB data structures
const char param_file[] = "param_file.petsc";
MoFEM::Core::Initialize(&argc, &argv, param_file, help);
try {
//! [Register MoFEM discrete manager in PETSc]
DMType dm_name = "DMMOFEM";
//! [Register MoFEM discrete manager in PETSc
//! [Create MoAB]
moab::Core mb_instance; ///< mesh database
moab::Interface &moab = mb_instance; ///< mesh database interface
//! [Create MoAB]
//! [Create MoFEM]
MoFEM::Core core(moab); ///< finite element database
MoFEM::Interface &m_field = core; ///< finite element database insterface
//! [Create MoFEM]
//! [Example]
Example ex(m_field);
CHKERR ex.runProblem();
//! [Example]
}
}
static char help[]
int main()
constexpr int SPACE_DIM
ElementsAndOps< SPACE_DIM >::DomainEle DomainEle
#define CATCH_ERRORS
Catch errors.
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition DMMoFEM.cpp:43
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 > & getOpDomainPostProcPipeline()
Get the Op Domain PostProc Pipeline object.
MoFEMErrorCode loopFiniteElementsPostProc(SmartPetscObj< DM > dm=nullptr)
Iterate postprocessing finite elements.
boost::ptr_deque< UserDataOperator > & getOpDomainRhsPipeline()
Get the Op Domain Rhs Pipeline object.
@ PETSC
Standard PETSc assembly.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
auto createVectorMPI(MPI_Comm comm, PetscInt n, PetscInt N)
Create MPI Vector.
int r
Definition sdf.py:205
OpPostProcMapInMoab< SPACE_DIM, SPACE_DIM > OpPPMap
[Operators_definition]
boost::shared_ptr< VectorDouble > approxVals
SmartPetscObj< Vec > resVec
[Example]
Definition plastic.cpp:217
MoFEMErrorCode boundaryCondition()
[Set up problem]
MoFEMErrorCode setIntegrationRules()
[Set up problem]
MoFEMErrorCode createCommonData()
[Set up problem]
Definition plastic.cpp:479
MoFEMErrorCode runProblem()
[Run problem]
Definition plastic.cpp:255
MoFEMErrorCode outputResults()
[Solve]
virtual MPI_Comm & get_comm() const =0
Core (interface) class.
Definition Core.hpp:83
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:68
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:123
Deprecated interface functions.
Specialization for double precision scalar field values calculation.
Post post-proc data at points from hash maps.
PipelineManager interface.
boost::shared_ptr< FEMethod > & getDomainPostProcFE()
Get domain postprocessing finite element.
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
intrusive_ptr for managing petsc objects