| 1 |
// Copyright (C) 2009, 2015 Anders Logg |
| 2 |
// |
| 3 |
// This file is part of DOLFIN. |
| 4 |
// |
| 5 |
// DOLFIN is free software: you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU Lesser General Public License as published by |
| 7 |
// the Free Software Foundation, either version 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// DOLFIN is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU Lesser General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// This demo program demonstrates how to extract matching sub meshes |
| 19 |
// from a common mesh. |
| 20 |
|
| 21 |
#include <dolfin.h> |
| 22 |
|
| 23 |
using namespace dolfin; |
| 24 |
|
| 25 |
int main() |
| 26 |
{ |
| 27 |
if (dolfin::MPI::size(MPI_COMM_WORLD) > 1) |
| 28 |
{ |
| 29 |
std::cout << "This demo does not work in parallel" << std::endl; |
| 30 |
return 0; |
| 31 |
} |
| 32 |
|
| 33 |
// Structure sub domain |
| 34 |
class Structure : public SubDomain |
| 35 |
{ |
| 36 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 37 |
{ |
| 38 |
return x[0] > 1.4 - DOLFIN_EPS and x[0] < 1.6 + DOLFIN_EPS |
| 39 |
and x[1] < 0.6 + DOLFIN_EPS; |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
// Create mesh |
| 44 |
auto mesh = std::make_shared<RectangleMesh>(Point(0.0, 0.0), |
| 45 |
Point(3.0, 1.0), 60, 20); |
| 46 |
|
| 47 |
// Create sub domain markers and mark everything as 0 |
| 48 |
MeshFunction<std::size_t> sub_domains(mesh, mesh->topology().dim()); |
| 49 |
sub_domains = 0; |
| 50 |
|
| 51 |
// Mark structure domain as 1 |
| 52 |
Structure structure; |
| 53 |
structure.mark(sub_domains, 1); |
| 54 |
|
| 55 |
// Extract sub meshes |
| 56 |
auto fluid_mesh = std::make_shared<SubMesh>(*mesh, sub_domains, 0); |
| 57 |
auto structure_mesh = std::make_shared<SubMesh>(*mesh, sub_domains, 1); |
| 58 |
|
| 59 |
// Move structure mesh |
| 60 |
MeshGeometry& geometry = structure_mesh->geometry(); |
| 61 |
|
| 62 |
for (VertexIterator v(*structure_mesh); !v.end(); ++v) |
| 63 |
{ |
| 64 |
std::array<double, 2> x = {{v->x()[0], v->x()[1]}}; |
| 65 |
x[0] += 0.1*x[0]*x[1]; |
| 66 |
geometry.set(v->index(), x.data()); |
| 67 |
} |
| 68 |
|
| 69 |
// Move fluid mesh according to structure mesh |
| 70 |
ALE::move(fluid_mesh, *structure_mesh); |
| 71 |
fluid_mesh->smooth(); |
| 72 |
|
| 73 |
// Write out meshes to files. |
| 74 |
XDMFFile("fluid_mesh.xdmf").write(*fluid_mesh); |
| 75 |
XDMFFile("structure_mesh.xdmf").write(*structure_mesh); |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |