Debian Codegraph @

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

raw
1 // Copyright (C) 2007-2011 Kristian B. Oelgaard, Anders Logg and Garth N. Wells
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: 2007-06-29
19 // Last changed: 2013-03-21
20 //
21 // Steady state advection-diffusion equation, discontinuous
22 // formulation using full upwinding.
23
24 #include <dolfin.h>
25
26 #include "AdvectionDiffusion.h"
27 #include "Projection.h"
28 #include "Velocity.h"
29
30 using namespace dolfin;
31
32 // Dirichlet boundary condition
33 class BC : public Expression
34 {
35 public:
36
37 void eval(Array<double>& values, const Array<double>& x) const
38 {
39 values[0] = sin(DOLFIN_PI*5.0*x[1]);
40 }
41
42 };
43
44 // Sub domain for Dirichlet boundary condition
45 class DirichletBoundary : public SubDomain
46 {
47 bool inside(const Array<double>& x, bool on_boundary) const
48 {
49 return std::abs(x[0] - 1.0) < DOLFIN_EPS && on_boundary;
50 }
51 };
52
53 int main(int argc, char *argv[])
54 {
55 // FIXME: Make mesh ghosted
56 parameters["ghost_mode"] = "shared_facet";
57
58 // Read simple velocity field (-1.0, -0.4) defined on a 64x64 unit square
59 // mesh and a quadratic vector Lagrange element
60
61 // Read mesh
62 auto mesh = std::make_shared<Mesh>("../unitsquare_64_64.xml.gz");
63
64 // Create velocity FunctionSpace
65 auto V_u = std::make_shared<Velocity::FunctionSpace>(mesh);
66
67 // Create velocity function
68 auto u = std::make_shared<Function>(V_u, "../unitsquare_64_64_velocity.xml.gz");
69
70 // Diffusivity
71 auto c = std::make_shared<Constant>(0.0);
72
73 //Source term
74 auto f = std::make_shared<Constant>(0.0);
75
76 // Penalty parameter
77 auto alpha = std::make_shared<Constant>(5.0);
78
79 // Create function space
80 auto V = std::make_shared<AdvectionDiffusion::FunctionSpace>(mesh);
81
82 // Create forms and attach functions
83 AdvectionDiffusion::BilinearForm a(V, V);
84 a.u = u; a.kappa = c; a.alpha = alpha;
85 AdvectionDiffusion::LinearForm L(V);
86 L.f = f;
87
88 // Set up boundary condition (apply strong BCs)
89 auto g = std::make_shared<BC>();
90 auto boundary = std::make_shared<DirichletBoundary>();
91 DirichletBC bc(V, g, boundary, "geometric");
92
93 // Solution function
94 auto phi_h = std::make_shared<Function>(V);
95
96 // Assemble and apply boundary conditions
97 Matrix A;
98 Vector b;
99 assemble(A, a);
100 assemble(b, L);
101 bc.apply(A, b);
102
103 // Solve system
104 solve(A, *phi_h->vector(), b);
105
106 // Define variational problem
107 auto Vp = std::make_shared<Projection::FunctionSpace>(mesh);
108 Projection::BilinearForm ap(Vp, Vp);
109 Projection::LinearForm Lp(Vp);
110 Lp.phi0 = phi_h;
111
112 // Compute solution
113 Function phi_p(Vp);
114 solve(ap == Lp, phi_p);
115
116 // Save projected solution in VTK format
117 File file("temperature.pvd");
118 file << phi_p;
119
120 return 0;
121 }