Debian Codegraph @

demo/undocumented/multimesh-poisson/cpp/main.cpp

raw
1 // Copyright (C) 2013-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 // First added: 2013-06-26
19 // Last changed: 2016-03-02
20 //
21 // This demo program solves Poisson's equation on a domain defined by
22 // three overlapping and non-matching meshes. The solution is computed
23 // on a sequence of rotating meshes to test the multimesh
24 // functionality.
25
26 #include <cmath>
27 #include <dolfin.h>
28 #include "MultiMeshPoisson.h"
29
30 using namespace dolfin;
31 using std::make_shared;
32
33 // Sub domain for Dirichlet boundary condition
34 class DirichletBoundary : public SubDomain
35 {
36 bool inside(const Array<double>& x, bool on_boundary) const
37 {
38 return on_boundary;
39 }
40 };
41
42 // Compute solution for given mesh configuration
43 std::shared_ptr<MultiMeshFunction> solve_poisson(double t,
44 double x1, double y1,
45 double x2, double y2)
46 {
47 // Create meshes
48 double r = 0.5;
49 auto mesh_0 = make_shared<RectangleMesh>(Point(-r, -r), Point(r, r), 16, 16);
50 auto mesh_1 = make_shared<RectangleMesh>(Point(x1 - r, y1 - r), Point(x1 + r, y1 + r), 8, 8);
51 auto mesh_2 = make_shared<RectangleMesh>(Point(x2 - r, y2 - r), Point(x2 + r, y2 + r), 8, 8);
52 mesh_1->rotate(70*t);
53 mesh_2->rotate(-70*t);
54
55 // Build multimesh
56 auto multimesh = make_shared<MultiMesh>();
57 multimesh->add(mesh_0);
58 multimesh->add(mesh_1);
59 multimesh->add(mesh_2);
60 multimesh->build();
61
62 // Create function space
63 auto V = make_shared<MultiMeshPoisson::MultiMeshFunctionSpace>(multimesh);
64
65 // Create forms
66 auto a = make_shared<MultiMeshPoisson::MultiMeshBilinearForm>(V, V);
67 auto L = make_shared<MultiMeshPoisson::MultiMeshLinearForm>(V);
68
69 // Attach coefficients
70 auto f = make_shared<Constant>(1);
71 L->f = f;
72
73 // Assemble linear system
74 auto A = make_shared<Matrix>();
75 auto b = make_shared<Vector>();
76 assemble_multimesh(*A, *a);
77 assemble_multimesh(*b, *L);
78
79 // Apply boundary condition
80 auto zero = make_shared<Constant>(0);
81 auto boundary = make_shared<DirichletBoundary>();
82 auto bc = make_shared<MultiMeshDirichletBC>(V, zero, boundary);
83 bc->apply(*A, *b);
84
85 // Lock inactive dofs
86 V->lock_inactive_dofs(*A, *b);
87
88 // Compute solution
89 auto u = make_shared<MultiMeshFunction>(V);
90 solve(*A, *u->vector(), *b);
91
92 return u;
93 }
94
95 int main(int argc, char* argv[])
96 {
97 if (dolfin::MPI::size(MPI_COMM_WORLD) > 1)
98 {
99 info("Sorry, this demo does not (yet) run in parallel.");
100 return 0;
101 }
102
103 // Parameters
104 const double T = 10.0;
105 const std::size_t N = 100;
106 const double dt = T / N;
107
108 // Create files for output
109 XDMFFile f0("output/u0.xdmf");
110 XDMFFile f1("output/u1.xdmf");
111 XDMFFile f2("output/u2.xdmf");
112
113 // Iterate over configurations
114 for (std::size_t n = 0; n < N; n++)
115 {
116 info("Computing solution, step %d / %d.", n + 1, N);
117
118 // Compute coordinates for meshes
119 const double t = dt*n;
120 const double x1 = sin(t)*cos(2*t);
121 const double y1 = cos(t)*cos(2*t);
122 const double x2 = cos(t)*cos(2*t);
123 const double y2 = sin(t)*cos(2*t);
124
125 // Compute solution
126 std::shared_ptr<MultiMeshFunction> u = solve_poisson(t, x1, y1, x2, y2);
127
128 // Save to file
129 f0.write(*u->part(0), t);
130 f1.write(*u->part(1), t);
131 f2.write(*u->part(2), t);
132 }
133
134 // Close files
135 f0.close();
136 f1.close();
137 f2.close();
138
139 return 0;
140 }