Debian Codegraph @

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

raw
1 #include <dolfin.h>
2 #include "MeshView_3D1D.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 // Source term (right-hand side)
16 class Source : public Expression
17 {
18 void eval(Array<double>& values, const Array<double>& x) const
19 {
20 double dx = x[0] - 0.5;
21 values[0] = 10*exp(-(dx*dx) / 0.02);
22 }
23 };
24
25 int main()
26 {
27 // Create mesh and function space
28 auto mesh = std::make_shared<UnitCubeMesh>(32, 32, 32);
29
30 MeshFunction<std::size_t> marker(mesh, mesh->topology().dim() - 2, 0);
31 for (EdgeIterator edge(*mesh); !edge.end(); ++edge)
32 {
33 auto y = edge->midpoint().y();
34 auto z = edge->midpoint().z();
35 marker[edge->index()] = (0.5 - DOLFIN_EPS < z && z < 0.5 + DOLFIN_EPS) && (0.5 - DOLFIN_EPS < y && y < 0.5 + DOLFIN_EPS);
36 }
37
38 std::vector<std::size_t> vertex_map,cell_map;
39 auto mapping = std::make_shared<MeshView>(mesh,vertex_map,cell_map);
40 auto submesh = std::make_shared<Mesh>(mapping->create(marker, 1));
41
42 // Function spaces associated with each of the function spaces
43 auto V1 = std::make_shared<MeshView_3D1D::Form_a00::TestSpace>(mesh); // 3D
44 auto V2 = std::make_shared<MeshView_3D1D::Form_a11::TestSpace>(submesh); // 1D
45
46 // Bilinear and linear forms
47 MeshView_3D1D::Form_a00 a_3D(V1, V1);
48 MeshView_3D1D::Form_a11 a_1D(V2, V2);
49 MeshView_3D1D::Form_L0 L_3D(V1);
50 MeshView_3D1D::Form_L1 L_1D(V2);
51
52 // Define boundary conditions
53 auto zero = std::make_shared<Constant>(0.0);
54 auto boundary = std::make_shared<DirichletBoundary>();
55 DirichletBC bc_3D(V1, zero, boundary);
56 DirichletBC bc_1D(V2, zero, boundary);
57
58 // Define RHS
59 auto f = std::make_shared<Source>();
60 L_3D.f1 = f;
61 L_1D.f2 = f;
62
63 // Compute solution
64 // Subproblem 3D
65 Function u_3D(V1);
66 solve(a_3D == L_3D, u_3D, bc_3D);
67 // Subproblem 1D
68 Function u_1D(V2);
69 solve(a_1D == L_1D, u_1D, bc_1D);
70
71 // Save solution in vtk format
72 File out_3D("meshview-mapping-3D1D-3Dsol.pvd");
73 out_3D << u_3D;
74 File out_1D("meshview-mapping-3D1D-1Dsol.pvd");
75 out_1D << u_1D;
76 }
77
78