Debian Codegraph @

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

raw
1 // Copyright (C) 2014-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: 2014-06-10
19 // Last changed: 2017-09-28
20 //
21 // This demo program solves the Stokes equations on a domain defined
22 // by three overlapping and non-matching meshes.
23
24 #include <dolfin.h>
25 #include "MultiMeshStokes.h"
26
27 using namespace dolfin;
28 using std::make_shared;
29
30 // Value for inflow boundary condition for velocity
31 class InflowValue : public Expression
32 {
33 public:
34
35 InflowValue() : Expression(2) {}
36
37 void eval(Array<double>& values, const Array<double>& x) const
38 {
39 values[0] = sin(x[1]*DOLFIN_PI);
40 values[1] = 0.0;
41 }
42
43 };
44
45 // Subdomain for inflow boundary
46 class InflowBoundary : public SubDomain
47 {
48 bool inside(const Array<double>& x, bool on_boundary) const
49 {
50 return on_boundary && near(x[0], 0.0);
51 }
52 };
53
54 // Subdomain for outflow boundary
55 class OutflowBoundary : public SubDomain
56 {
57 bool inside(const Array<double>& x, bool on_boundary) const
58 {
59 return on_boundary && near(x[0], 1.0);
60 }
61 };
62
63 // Subdomain for no-slip boundary
64 class NoslipBoundary : public SubDomain
65 {
66 bool inside(const Array<double>& x, bool on_boundary) const
67 {
68 return on_boundary && (near(x[1], 0.0) || near(x[1], 1.0));
69 }
70 };
71
72 int main(int argc, char* argv[])
73 {
74 if (dolfin::MPI::size(MPI_COMM_WORLD) > 1)
75 {
76 info("Sorry, this demo does not (yet) run in parallel.");
77 return 0;
78 }
79
80 // Create meshes
81 auto mesh_0 = make_shared<UnitSquareMesh>(16, 16);
82 auto mesh_1 = make_shared<RectangleMesh>(Point(0.2, 0.2), Point(0.6, 0.6), 8, 8);
83 auto mesh_2 = make_shared<RectangleMesh>(Point(0.4, 0.4), Point(0.8, 0.8), 8, 8);
84
85 // Build multimesh
86 auto multimesh = make_shared<MultiMesh>();
87 multimesh->add(mesh_0);
88 multimesh->add(mesh_1);
89 multimesh->add(mesh_2);
90 multimesh->build();
91
92 // Create function space
93 auto W = make_shared<MultiMeshStokes::MultiMeshFunctionSpace>(multimesh);
94
95 // Create forms
96 auto a = make_shared<MultiMeshStokes::MultiMeshBilinearForm>(W, W);
97 auto L = make_shared<MultiMeshStokes::MultiMeshLinearForm>(W);
98
99 // Attach coefficients
100 auto f = make_shared<Constant>(0, 0);
101 L->f = f;
102
103 // Assemble linear system
104 auto A = make_shared<Matrix>();
105 auto b = make_shared<Vector>();
106 assemble_multimesh(*A, *a);
107 assemble_multimesh(*b, *L);
108
109 // Create boundary values
110 auto inflow_value = make_shared<InflowValue>();
111 auto outflow_value = make_shared<Constant>(0);
112 auto noslip_value = make_shared<Constant>(0, 0);
113
114 // Create subdomains for boundary conditions
115 auto inflow_boundary = make_shared<InflowBoundary>();
116 auto outflow_boundary = make_shared<OutflowBoundary>();
117 auto noslip_boundary = make_shared<NoslipBoundary>();
118
119 // Create subspaces for boundary conditions
120 auto V = make_shared<MultiMeshSubSpace>(*W, 0);
121 auto Q = make_shared<MultiMeshSubSpace>(*W, 1);
122
123 // Create boundary conditions
124 auto bc0 = make_shared<MultiMeshDirichletBC>(V, noslip_value, noslip_boundary);
125 auto bc1 = make_shared<MultiMeshDirichletBC>(V, inflow_value, inflow_boundary);
126 auto bc2 = make_shared<MultiMeshDirichletBC>(Q, outflow_value, outflow_boundary);
127
128 // Apply boundary conditions
129 bc0->apply(*A, *b);
130 bc1->apply(*A, *b);
131 bc2->apply(*A, *b);
132
133 // Remove inactive dofs
134 V->lock_inactive_dofs(*A, *b);
135
136 // Compute solution
137 auto w = make_shared<MultiMeshFunction>(W);
138 solve(*A, *w->vector(), *b);
139
140 // Save solution parts and components to file
141 for (int part = 0; part < 3; part++)
142 {
143 XDMFFile ufile("output/u" + std::to_string(part) + ".xdmf");
144 XDMFFile pfile("output/p" + std::to_string(part) + ".xdmf");
145 ufile.write((*w->part(part))[0]);
146 pfile.write((*w->part(part))[1]);
147 ufile.close();
148 pfile.close();
149 }
150
151 return 0;
152 }