Debian Codegraph @

demo/undocumented/advection-diffusion/cpp/main.cpp

raw
1 // Copyright (C) 2006-2008 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: 2006-02-09
19 // Last changed: 2013-03-21
20 //
21 // This demo solves the time-dependent advection-diffusion equation
22 // by a least-squares stabilized cG(1)cG(1) method. The velocity field
23 // used in the simulation is the output from the Stokes (Taylor-Hood)
24 // demo. The sub domains for the different boundary conditions are
25 // computed by the demo program in src/demo/subdomains.
26
27 #include <dolfin.h>
28 #include "AdvectionDiffusion.h"
29 #include "Velocity.h"
30
31 using namespace dolfin;
32
33 int main(int argc, char *argv[])
34 {
35 // Read mesh
36 auto mesh = std::make_shared<Mesh>("../dolfin_fine.xml.gz");
37
38 // Create velocity FunctionSpace
39 auto V_u = std::make_shared<Velocity::FunctionSpace>(mesh);
40
41 // Create velocity function
42 auto velocity = std::make_shared<Function>(V_u);
43 File file_u(mesh->mpi_comm(), "../dolfin_fine_velocity.xml.gz");
44 file_u >> *velocity;
45
46 // Read sub domain markers
47 auto sub_domains = std::make_shared<MeshFunction<std::size_t>>(mesh,
48 "../dolfin_fine_subdomains.xml.gz");
49
50 // Create function space
51 auto V = std::make_shared<AdvectionDiffusion::FunctionSpace>(mesh);
52
53 // Source term and initial condition
54 auto f = std::make_shared<Constant>(0.0);
55 auto u = std::make_shared<Function>(V);
56
57 // Set up forms
58 AdvectionDiffusion::BilinearForm a(V, V);
59 a.b = velocity;
60 AdvectionDiffusion::LinearForm L(V);
61 L.u0 = u; L.b = velocity; L.f = f;
62
63 // Set up boundary condition
64 auto g = std::make_shared<Constant>(1.0);
65 DirichletBC bc(V, g, sub_domains, 1);
66
67 // Linear system
68 std::shared_ptr<Matrix> A(new Matrix);
69 Vector b;
70
71 // Assemble matrix
72 assemble(*A, a);
73 bc.apply(*A);
74
75 // LU solver
76 LUSolver lu(A);
77
78 // Parameters for time-stepping
79 const double T = 2.0;
80 const double k = 0.05;
81 double t = k;
82
83 // Output file
84 File file("results/temperature.pvd");
85
86 // Time-stepping
87 Progress p("Time-stepping");
88 while (t < T)
89 {
90 // Assemble vector and apply boundary conditions
91 assemble(b, L);
92 bc.apply(b);
93
94 // Solve the linear system (re-use the already factorized matrix A)
95 lu.solve(*u->vector(), b);
96
97 // Save solution in VTK format
98 file << std::pair<const Function*, double>(u.get(), t);
99
100 // Move to next interval
101 p = t/T;
102 t += k;
103 }
104
105 return 0;
106 }