- 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:
}
#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.
MoFEMErrorCode loadFile(const std::string options, const std::string mesh_file_name, LoadFileFunc loadFunc=defaultLoadFileFunc)
Load mesh file.
MoFEMErrorCode getOptions()
get options
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:
}
constexpr char FIELD_NAME[]
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
MoFEMErrorCode setupProblem()
[Run problem]
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.
MoFEMErrorCode setFieldOrder(const std::string field_name, const int order, const Range *ents=NULL)
Set field order.
MoFEMErrorCode setUp(const PetscBool is_partitioned=PETSC_TRUE)
Setup problem.
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:
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:
auto solver = pipeline_mng->createKSP();
CHKERR KSPSetFromOptions(solver);
CHKERR VecGhostUpdateBegin(
D, INSERT_VALUES, SCATTER_FORWARD);
CHKERR VecGhostUpdateEnd(
D, INSERT_VALUES, SCATTER_FORWARD);
}
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
auto createDMVector(DM dm, RowColData rc=RowColData::COL)
Get smart vector from DM.
SmartPetscObj< Vec > vectorDuplicate(Vec vec)
Create duplicate vector of smart vector.
MoFEMErrorCode solveSystem()
[Solve]
MoFEMErrorCode getDM(DM *dm)
Get DM.
Checking the results
Finally, to compare the approximated and the analyitcal function, we use the following code:
pipeline_mng->getOpDomainPostProcPipeline().clear();
pipeline_mng->getOpDomainPostProcPipeline().push_back(
pipeline_mng->getOpDomainPostProcPipeline().push_back(
CHKERR pipeline_mng->loopFiniteElementsPostProc();
double nrm2;
const double *array;
PetscPrintf(PETSC_COMM_SELF, "Error %6.4e Vec norm %6.4e\n",
std::sqrt(array[0]), nrm2);
constexpr double eps = 1e-8;
"Not converged solution");
}
@ MOFEM_DATA_INCONSISTENCY
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:
OpError(boost::shared_ptr<CommonData> &common_data_ptr)
if (
const size_t nb_dofs = data.
getIndices().size()) {
const int nb_integration_pts = getGaussPts().size2();
auto t_w = getFTensor0IntegrationWeight();
auto t_coords = getFTensor1CoordsAtGaussPts();
nf.clear();
const double volume = getMeasure();
double error = 0;
for (int gg = 0; gg != nb_integration_pts; ++gg) {
const double alpha = t_w * volume;
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;
}
}
};
FTensor::Index< 'i', SPACE_DIM > i
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
UBlasVector< double > VectorDouble
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
static char help[] =
"...\n\n";
};
};
double operator()(const double x, const double y, const double z) {
return sin(x * 10.) * cos(y * 10.);
}
};
private:
};
template <
int FIELD_DIM>
struct OpError;
};
OpError(boost::shared_ptr<CommonData> &common_data_ptr)
if (
const size_t nb_dofs = data.
getIndices().size()) {
const int nb_integration_pts = getGaussPts().size2();
auto t_w = getFTensor0IntegrationWeight();
auto t_coords = getFTensor1CoordsAtGaussPts();
nf.clear();
const double volume = getMeasure();
double error = 0;
for (int gg = 0; gg != nb_integration_pts; ++gg) {
const double alpha = t_w * volume;
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;
}
}
};
}
}
}
auto rule = [](
int,
int,
int p) ->
int {
return 2 * p; };
}
}
}
CHKERR KSPSetFromOptions(solver);
CHKERR VecGhostUpdateBegin(
D, INSERT_VALUES, SCATTER_FORWARD);
CHKERR VecGhostUpdateEnd(
D, INSERT_VALUES, SCATTER_FORWARD);
}
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(
post_proc_fe->getPostProcMesh(), post_proc_fe->getMapGaussPts(),
{{FIELD_NAME, u_ptr}},
{},
{},
{})
);
CHKERR post_proc_fe->writeFile(
"out_approx.h5m");
}
double nrm2;
const double *array;
PetscPrintf(PETSC_COMM_SELF, "Error %6.4e Vec norm %6.4e\n",
std::sqrt(array[0]), nrm2);
constexpr double eps = 1e-8;
"Not converged solution");
}
int main(
int argc,
char *argv[]) {
const char param_file[] = "param_file.petsc";
try {
DMType dm_name = "DMMOFEM";
moab::Core mb_instance;
moab::Interface &moab = mb_instance;
}
}
ElementsAndOps< SPACE_DIM >::DomainEle DomainEle
#define CATCH_ERRORS
Catch errors.
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
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.
implementation of Data Operators for Forces and Sources
auto createVectorMPI(MPI_Comm comm, PetscInt n, PetscInt N)
Create MPI Vector.
OpPostProcMapInMoab< SPACE_DIM, SPACE_DIM > OpPPMap
SmartPetscObj< Vec > L2Vec
boost::shared_ptr< VectorDouble > approxVals
SmartPetscObj< Vec > resVec
MoFEMErrorCode boundaryCondition()
[Set up problem]
MoFEMErrorCode setIntegrationRules()
[Set up problem]
MoFEMErrorCode createCommonData()
[Set up problem]
MoFEMErrorCode runProblem()
[Run problem]
MoFEMErrorCode outputResults()
[Solve]
virtual MPI_Comm & get_comm() const =0
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
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.
intrusive_ptr for managing petsc objects
Volume finite element base.