| 1 |
Interpolation from a non-matching mesh |
| 2 |
====================================== |
| 3 |
|
| 4 |
This example demonstrates how to interpolate functions between |
| 5 |
finite element spaces on non-matching meshes. |
| 6 |
|
| 7 |
.. note:: |
| 8 |
|
| 9 |
Interpolation on non-matching meshes is not presently support in |
| 10 |
parallel. See |
| 11 |
https://bitbucket.org/fenics-project/dolfin/issues/162. |
| 12 |
|
| 13 |
|
| 14 |
Implementation |
| 15 |
-------------- |
| 16 |
|
| 17 |
The implementation is split in three files: two UFL form files |
| 18 |
containing the definition of finite elements, and a C++ file |
| 19 |
containing the runtime code. |
| 20 |
|
| 21 |
Running this demo requires the files: :download:`main.cpp`, |
| 22 |
:download:`P1.ufl`, :download:`P3.ufl` and :download:`CMakeLists.txt`. |
| 23 |
|
| 24 |
|
| 25 |
UFL form file |
| 26 |
^^^^^^^^^^^^^ |
| 27 |
|
| 28 |
The UFL files are implemented in :download:`P1.ufl` and |
| 29 |
:download:`P1.ufl`, and the explanations of the UFL files can be found |
| 30 |
at :doc:`here (P1) <P1.ufl>` and :doc:`here (P3) <P3.ufl>`. |
| 31 |
|
| 32 |
At the top we include the DOLFIN header file and the generated header |
| 33 |
files "P1.h" and "P2.h". For convenience we also include the DOLFIN |
| 34 |
namespace. |
| 35 |
|
| 36 |
.. code-block:: cpp |
| 37 |
|
| 38 |
#include <dolfin.h> |
| 39 |
#include "P1.h" |
| 40 |
#include "P3.h" |
| 41 |
|
| 42 |
using namespace dolfin; |
| 43 |
|
| 44 |
We then define an ``Expression``: |
| 45 |
|
| 46 |
.. code-block:: cpp |
| 47 |
|
| 48 |
class MyExpression : public Expression |
| 49 |
{ |
| 50 |
public: |
| 51 |
|
| 52 |
void eval(Array<double>& values, const Array<double>& x) const |
| 53 |
{ |
| 54 |
values[0] = sin(10.0*x[0])*sin(10.0*x[1]); |
| 55 |
} |
| 56 |
|
| 57 |
}; |
| 58 |
|
| 59 |
Next, the ``main`` function is started and we create two unit square |
| 60 |
meshes with a differing number of vertices in each direction: |
| 61 |
|
| 62 |
.. code-block:: cpp |
| 63 |
|
| 64 |
int main() |
| 65 |
{ |
| 66 |
if (dolfin::MPI::size(MPI_COMM_WORLD) > 1) |
| 67 |
{ |
| 68 |
std::cout << "This demo does not work in parallel" << std::endl; |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
// Create meshes |
| 73 |
auto mesh0 = std::make_shared<UnitSquareMesh>(16, 16); |
| 74 |
auto mesh1 = std::make_shared<UnitSquareMesh>(64, 64); |
| 75 |
|
| 76 |
We create a linear Lagrange finite element space on the coarser mesh, |
| 77 |
and a cubic Lagrange space on the finer mesh: |
| 78 |
|
| 79 |
.. code-block:: cpp |
| 80 |
|
| 81 |
// Create function spaces |
| 82 |
auto P1 = std::make_shared<P1::FunctionSpace>(mesh0); |
| 83 |
auto P3 = std::make_shared<P3::FunctionSpace>(mesh1); |
| 84 |
|
| 85 |
One each space we create a finite element function: |
| 86 |
|
| 87 |
.. code-block:: cpp |
| 88 |
|
| 89 |
// Create functions |
| 90 |
Function v1(P1); |
| 91 |
Function v3(P3); |
| 92 |
|
| 93 |
We create an instantiation of ``MyExpression``, and interpolate it |
| 94 |
into ``P3``: |
| 95 |
|
| 96 |
.. code-block:: cpp |
| 97 |
|
| 98 |
// Interpolate expression into P3 |
| 99 |
MyExpression e; |
| 100 |
v3.interpolate(e); |
| 101 |
|
| 102 |
Now, we interpolate ``v3`` into the linear finite element space on a |
| 103 |
coarser grid: |
| 104 |
|
| 105 |
.. code-block:: cpp |
| 106 |
|
| 107 |
v1.interpolate(v3); |
| 108 |
|
| 109 |
Finally, we can save the output of each function on each mesh to XDMF: |
| 110 |
|
| 111 |
.. code-block:: cpp |
| 112 |
|
| 113 |
XDMFFile("v1.xdmf").write(v1); |
| 114 |
XDMFFile("v3.xdmf").write(v3); |
| 115 |
|
| 116 |
return 0; |
| 117 |
} |