Debian Codegraph @

demo/undocumented/meshview-2D2D/cpp/main.cpp

raw
1 #include <dolfin.h>
2 #include "MeshView_2D2D.h"
3
4 using namespace dolfin;
5
6 // Sub domain for Dirichlet boundary condition
7 class DirichletBoundary : public SubDomain
8 {
9 bool inside(const Array<double>& x, bool on_boundary) const
10 {
11 return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS;
12 }
13 };
14
15 class DirichletBoundarySubdomain1 : public SubDomain
16 {
17 bool inside(const Array<double>& x, bool on_boundary) const
18 {
19 return x[0] < DOLFIN_EPS;
20 }
21 };
22
23 class DirichletBoundarySubdomain2 : public SubDomain
24 {
25 bool inside(const Array<double>& x, bool on_boundary) const
26 {
27 return x[0] > 1.0 - DOLFIN_EPS;
28 }
29 };
30
31 // Source term (right-hand side)
32 class Source : public Expression
33 {
34 void eval(Array<double>& values, const Array<double>& x) const
35 {
36 double dx = x[0] - 0.5;
37 double dy = x[1] - 0.5;
38 values[0] = 10*exp(-(dx*dx + dy*dy) / 0.02);
39 }
40 };
41
42
43 int main()
44 {
45 // Create mesh and function space
46 auto mesh = std::make_shared<UnitSquareMesh>(32, 32);
47 auto V = std::make_shared<MeshView_2D2D::Form_a::TestSpace>(mesh);
48
49 MeshFunction<std::size_t> marker(mesh, mesh->topology().dim(), 0);
50 for (CellIterator cell(*mesh); !cell.end(); ++cell)
51 {
52 auto x = cell->midpoint().coordinates();
53 marker[cell->index()] = x[0] < 0.5;
54 }
55
56 std::vector<std::size_t> vertex_map,cell_map;
57 auto mapping = std::make_shared<MeshView>(mesh,vertex_map,cell_map);
58 auto submesh1 = std::make_shared<Mesh>(mapping->create(marker, 1));
59 auto submesh2 = std::make_shared<Mesh>(mapping->create(marker, 0));
60
61 // Function spaces associated with each of the function spaces
62 auto V1 = std::make_shared<MeshView_2D2D::Form_a00::TestSpace>(submesh1);
63 auto V2 = std::make_shared<MeshView_2D2D::Form_a11::TestSpace>(submesh2);
64
65 // Bilinear and linear forms
66 MeshView_2D2D::Form_a a(V, V);
67 MeshView_2D2D::Form_L L(V);
68 MeshView_2D2D::Form_a00 a1(V1, V1);
69 MeshView_2D2D::Form_a11 a2(V2, V2);
70 MeshView_2D2D::Form_L0 L1(V1);
71 MeshView_2D2D::Form_L1 L2(V2);
72
73 // Define boundary conditions
74 auto zero = std::make_shared<Constant>(0.0);
75 auto boundary = std::make_shared<DirichletBoundary>();
76 DirichletBC bc(V, zero, boundary);
77
78 auto boundarySubdomain1 = std::make_shared<DirichletBoundarySubdomain1>();
79 auto boundarySubdomain2 = std::make_shared<DirichletBoundarySubdomain2>();
80 DirichletBC bc1(V1, zero, boundarySubdomain1);
81 DirichletBC bc2(V2, zero, boundarySubdomain2);
82
83 // Define RHS
84 auto f = std::make_shared<Source>();
85 L.f = f;
86 L1.f1 = f;
87 L2.f2 = f;
88
89 // Compute solution
90 // Global problem
91 Function u(V);
92 solve(a == L, u, bc);
93 // Subproblem 1
94 Function u1(V1);
95 solve(a1 == L1, u1, bc1);
96 // Subproblem 1
97 Function u2(V2);
98 solve(a2 == L2, u2, bc2);
99
100 // Save solution in vtk format
101 File out_global("meshview-mapping-2D2D-global.pvd");
102 out_global << u;
103 File out_sub1("meshview-mapping-2D2D-subdomain1.pvd");
104 out_sub1 << u1;
105 File out_sub2("meshview-mapping-2D2D-subdomain2.pvd");
106 out_sub2 << u2;
107 }