Debian Codegraph @

demo/documented/navier-stokes/cpp/main.cpp

raw
1 // Copyright (C) 2010-2011 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: 2010-08-30
19 // Last changed: 2013-03-21
20 //
21 // This demo program solves the incompressible Navier-Stokes equations
22 // on an L-shaped domain using Chorin's splitting method.
23
24 // Begin demo
25
26 #include <dolfin.h>
27 #include "TentativeVelocity.h"
28 #include "PressureUpdate.h"
29 #include "VelocityUpdate.h"
30
31 using namespace dolfin;
32
33 // Define noslip domain
34 class NoslipDomain : public SubDomain
35 {
36 bool inside(const Array<double>& x, bool on_boundary) const
37 {
38 return (on_boundary &&
39 (x[0] < DOLFIN_EPS || x[1] < DOLFIN_EPS ||
40 (x[0] > 0.5 - DOLFIN_EPS && x[1] > 0.5 - DOLFIN_EPS)));
41 }
42 };
43
44 // Define inflow domain
45 class InflowDomain : public SubDomain
46 {
47 bool inside(const Array<double>& x, bool on_boundary) const
48 { return x[1] > 1.0 - DOLFIN_EPS; }
49 };
50
51 // Define inflow domain
52 class OutflowDomain : public SubDomain
53 {
54 bool inside(const Array<double>& x, bool on_boundary) const
55 { return x[0] > 1.0 - DOLFIN_EPS; }
56 };
57
58 // Define pressure boundary value at inflow
59 class InflowPressure : public Expression
60 {
61 public:
62
63 // Constructor
64 InflowPressure() : t(0) {}
65
66 // Evaluate pressure at inflow
67 void eval(Array<double>& values, const Array<double>& x) const
68 { values[0] = sin(3.0*t); }
69
70 // Current time
71 double t;
72
73 };
74
75 int main()
76 {
77 // Print log messages only from the root process in parallel
78 parameters["std_out_all_processes"] = false;
79
80 // Load mesh from file
81 auto mesh = std::make_shared<Mesh>("../lshape.xml.gz");
82
83 // Create function spaces
84 auto V = std::make_shared<VelocityUpdate::FunctionSpace>(mesh);
85 auto Q = std::make_shared<PressureUpdate::FunctionSpace>(mesh);
86
87 // Set parameter values
88 double dt = 0.01;
89 double T = 3;
90
91 // Define values for boundary conditions
92 auto p_in = std::make_shared<InflowPressure>();
93 auto zero = std::make_shared<Constant>(0.0);
94 auto zero_vector = std::make_shared<Constant>(0.0, 0.0);
95
96 // Define subdomains for boundary conditions
97 auto noslip_domain = std::make_shared<NoslipDomain>();
98 auto inflow_domain = std::make_shared<InflowDomain>();
99 auto outflow_domain = std::make_shared<OutflowDomain>() ;
100
101 // Define boundary conditions
102 DirichletBC noslip(V, zero_vector, noslip_domain);
103 DirichletBC inflow(Q, p_in, inflow_domain);
104 DirichletBC outflow(Q, zero, outflow_domain);
105 std::vector<DirichletBC*> bcu = {&noslip};
106 std::vector<DirichletBC*> bcp = {{&inflow, &outflow}};
107
108 // Create functions
109 auto u0 = std::make_shared<Function>(V);
110 auto u1 = std::make_shared<Function>(V);
111 auto p1 = std::make_shared<Function>(Q);
112
113 // Create coefficients
114 auto k = std::make_shared<Constant>(dt);
115 auto f = std::make_shared<Constant>(0, 0);
116
117 // Create forms
118 TentativeVelocity::BilinearForm a1(V, V);
119 TentativeVelocity::LinearForm L1(V);
120 PressureUpdate::BilinearForm a2(Q, Q);
121 PressureUpdate::LinearForm L2(Q);
122 VelocityUpdate::BilinearForm a3(V, V);
123 VelocityUpdate::LinearForm L3(V);
124
125 // Set coefficients
126 a1.k = k; L1.k = k; L1.u0 = u0; L1.f = f;
127 L2.k = k; L2.u1 = u1;
128 L3.k = k; L3.u1 = u1; L3.p1 = p1;
129
130 // Assemble matrices
131 Matrix A1, A2, A3;
132 assemble(A1, a1);
133 assemble(A2, a2);
134 assemble(A3, a3);
135
136 // Create vectors
137 Vector b1, b2, b3;
138
139 // Use amg preconditioner if available
140 const std::string prec(has_krylov_solver_preconditioner("amg") ? "amg" : "default");
141
142 // Create files for storing solution
143 File ufile("results/velocity.pvd");
144 File pfile("results/pressure.pvd");
145
146 // Time-stepping
147 double t = dt;
148 while (t < T + DOLFIN_EPS)
149 {
150 // Update pressure boundary condition
151 p_in->t = t;
152
153 // Compute tentative velocity step
154 begin("Computing tentative velocity");
155 assemble(b1, L1);
156 for (std::size_t i = 0; i < bcu.size(); i++)
157 bcu[i]->apply(A1, b1);
158 solve(A1, *u1->vector(), b1, "gmres", "default");
159 end();
160
161 // Pressure correction
162 begin("Computing pressure correction");
163 assemble(b2, L2);
164 for (std::size_t i = 0; i < bcp.size(); i++)
165 {
166 bcp[i]->apply(A2, b2);
167 bcp[i]->apply(*p1->vector());
168 }
169 solve(A2, *p1->vector(), b2, "bicgstab", prec);
170 end();
171
172 // Velocity correction
173 begin("Computing velocity correction");
174 assemble(b3, L3);
175 for (std::size_t i = 0; i < bcu.size(); i++)
176 bcu[i]->apply(A3, b3);
177 solve(A3, *u1->vector(), b3, "gmres", "default");
178 end();
179
180 // Save to file
181 ufile << *u1;
182 pfile << *p1;
183
184 // Move to next time step
185 *u0 = *u1;
186 t += dt;
187 cout << "t = " << t << endl;
188 }
189
190 return 0;
191 }