Debian Codegraph @

demo/undocumented/meshview-3D3D/cpp/main.cpp

raw
1 #include <dolfin.h>
2 #include "MeshView_3D3D.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 double dz = x[2] - 0.5;
39 values[0] = 10*exp(-(dx*dx + dy*dy + dz*dz) / 0.02);
40 }
41 };
42
43 int main()
44 {
45 // Create mesh and function space
46 auto mesh = std::make_shared<UnitCubeMesh>(32, 32, 32);
47 auto V = std::make_shared<MeshView_3D3D::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_3D3D::Form_a00::TestSpace>(submesh1);
63 auto V2 = std::make_shared<MeshView_3D3D::Form_a11::TestSpace>(submesh2);
64
65
66 // Bilinear and linear forms
67 MeshView_3D3D::Form_a a(V, V);
68 MeshView_3D3D::Form_L L(V);
69 MeshView_3D3D::Form_a00 a1(V1, V1);
70 MeshView_3D3D::Form_a11 a2(V2, V2);
71 MeshView_3D3D::Form_L0 L1(V1);
72 MeshView_3D3D::Form_L1 L2(V2);
73
74 // Define boundary conditions
75 auto zero = std::make_shared<Constant>(0.0);
76 auto boundary = std::make_shared<DirichletBoundary>();
77 DirichletBC bc(V, zero, boundary);
78
79 auto boundarySubdomain1 = std::make_shared<DirichletBoundarySubdomain1>();
80 auto boundarySubdomain2 = std::make_shared<DirichletBoundarySubdomain2>();
81 DirichletBC bc1(V1, zero, boundarySubdomain1);
82 DirichletBC bc2(V2, zero, boundarySubdomain2);
83
84 // Define RHS
85 auto f = std::make_shared<Source>();
86 L.f = f;
87 L1.f1 = f;
88 L2.f2 = f;
89
90 // Compute solution
91 // Global problem
92 Function u(V);
93 solve(a == L, u, bc);
94 // Subproblem 1
95 Function u1(V1);
96 solve(a1 == L1, u1, bc1);
97 // Subproblem 1
98 Function u2(V2);
99 solve(a2 == L2, u2, bc2);
100
101 // Save solution in vtk format
102 File out_global("meshview-mapping-3D3D-global.pvd");
103 out_global << u;
104 File out_sub1("meshview-mapping-3D3D-subdomain1.pvd");
105 out_sub1 << u1;
106 File out_sub2("meshview-mapping-3D3D-subdomain2.pvd");
107 out_sub2 << u2;
108 }
109
110