Debian Codegraph @

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

raw
1 // Copyright (C) 2006-2011 Anders Logg and Kristian B. Oelgaard
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 // This demo program solves Poisson's equation,
19 //
20 // - div grad u(x, y) = f(x, y)
21 //
22 // on the unit square with source f given by
23 //
24 // f(x, y) = -100*exp(-((x - 0.5)^2 + (y - 0.5)^2)/0.02)
25 //
26 // and boundary conditions given by
27 //
28 // u(x, y) = u0 on x = 0 and x = 1
29 // du/dn(x, y) = g on y = 0 and y = 1
30 //
31 // where
32 //
33 // u0 = x + 0.25*sin(2*pi*x)
34 // g = (y - 0.5)**2
35 //
36 // using a discontinuous Galerkin formulation (interior penalty method).
37
38 #include <dolfin.h>
39 #include "Poisson.h"
40
41 using namespace dolfin;
42
43 int main()
44 {
45 // Source term
46 class Source : public Expression
47 {
48 void eval(Array<double>& values, const Array<double>& x) const
49 {
50 const double dx = x[0] - 0.5;
51 const double dy = x[1] - 0.5;
52 values[0] = -100.0*exp(-(dx*dx + dy*dy)/0.02);
53 }
54 };
55
56 // Dirichlet term
57 class BoundaryValue : public Expression
58 {
59 void eval(Array<double>& values, const Array<double>& x) const
60 {
61 values[0] = x[0] + 0.25*sin(2*M_PI*x[1]);
62 }
63 };
64
65 // Neumann term
66 class BoundaryDerivative : public Expression
67 {
68 void eval(Array<double>& values, const Array<double>& x) const
69 {
70 const double dx = x[0] - 0.5;
71 values[0] = dx*dx;
72 }
73 };
74
75 // Sub domain for Dirichlet boundary condition, x = 1 and x = 0
76 class DirichletBoundary : public SubDomain
77 {
78 bool inside(const Array<double>& x, bool on_boundary) const
79 { return on_boundary and near(x[0]*(1 - x[0]), 0); }
80 };
81
82 // Sub domain for Neumann boundary condition, y = 1 and y = 0
83 class NeumannBoundary : public SubDomain
84 {
85 bool inside(const Array<double>& x, bool on_boundary) const
86 { return on_boundary and near(x[1]*(1 - x[1]), 0); }
87 };
88
89
90 // FIXME: Make mesh ghosted
91 parameters["ghost_mode"] = "shared_facet";
92
93 // Create mesh
94 auto mesh = std::make_shared<UnitSquareMesh>(24, 24);
95
96 // Create functions
97 auto f = std::make_shared<Source>();
98 auto u0 = std::make_shared<BoundaryValue>();
99 auto g = std::make_shared<BoundaryDerivative>();
100
101 // Create funtion space
102 auto V = std::make_shared<Poisson::FunctionSpace>(mesh);
103
104 // Mark facets of the mesh
105 NeumannBoundary neumann_boundary;
106 DirichletBoundary dirichlet_boundary;
107
108 auto boundaries = std::make_shared<MeshFunction<std::size_t>>(mesh, mesh->topology().dim()-1, 0);
109 neumann_boundary.mark(*boundaries, 2);
110 dirichlet_boundary.mark(*boundaries, 1);
111
112 // Define variational problem
113 Poisson::BilinearForm a(V, V);
114 Poisson::LinearForm L(V);
115 L.f = f;
116 L.u0 = u0;
117 L.g = g;
118
119 // Attach marked facets to bilinear and linear form
120 a.ds = boundaries;
121 L.ds = boundaries;
122
123 // Compute solution
124 Function u(V);
125 solve(a == L, u);
126
127 // Save solution in VTK format
128 File file("poisson.pvd");
129 file << u;
130
131 return 0;
132 }