Debian Codegraph @

demo/documented/periodic/cpp/main.cpp

raw
1 // Copyright (C) 2007-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: 2007-07-11
19 // Last changed: 2012-11-12
20 //
21 // This demo program solves Poisson's equation,
22 //
23 // - div grad u(x, y) = f(x, y)
24 //
25 // on the unit square with homogeneous Dirichlet boundary conditions
26 // at y = 0, 1 and periodic boundary conditions at x = 0, 1.
27
28 #include <dolfin.h>
29 #include "Poisson.h"
30
31 using namespace dolfin;
32
33 int main()
34 {
35 // Source term
36 class Source : public Expression
37 {
38 public:
39
40 void eval(Array<double>& values, const Array<double>& x) const
41 {
42 const double dx = x[0] - 0.5;
43 const double dy = x[1] - 0.5;
44 values[0] = x[0]*sin(5.0*DOLFIN_PI*x[1]) + 1.0*exp(-(dx*dx + dy*dy)/0.02);
45 }
46
47 };
48
49 // Sub domain for Dirichlet boundary condition
50 class DirichletBoundary : public SubDomain
51 {
52 bool inside(const Array<double>& x, bool on_boundary) const
53 { return (x[1] < DOLFIN_EPS || x[1] > (1.0 - DOLFIN_EPS)) && on_boundary; }
54 };
55
56 // Sub domain for Periodic boundary condition
57 class PeriodicBoundary : public SubDomain
58 {
59 // Left boundary is "target domain" G
60 bool inside(const Array<double>& x, bool on_boundary) const
61 { return (std::abs(x[0]) < DOLFIN_EPS); }
62
63 // Map right boundary (H) to left boundary (G)
64 void map(const Array<double>& x, Array<double>& y) const
65 {
66 y[0] = x[0] - 1.0;
67 y[1] = x[1];
68 }
69 };
70
71 // Create mesh
72 auto mesh = std::make_shared<UnitSquareMesh>(32, 32);
73
74 // Create periodic boundary condition
75 auto periodic_boundary = std::make_shared<PeriodicBoundary>();
76
77 // Create functions
78 auto f = std::make_shared<Source>();
79
80 // Define PDE
81 auto V = std::make_shared<Poisson::FunctionSpace>(mesh, periodic_boundary);
82 Poisson::BilinearForm a(V, V);
83 Poisson::LinearForm L(V);
84 L.f = f;
85
86 // Create Dirichlet boundary condition
87 auto u0 = std::make_shared<Constant>(0.0);
88 auto dirichlet_boundary = std::make_shared<DirichletBoundary>();
89 DirichletBC bc0(V, u0, dirichlet_boundary);
90
91 // Collect boundary conditions
92 std::vector<const DirichletBC*> bcs = {&bc0};
93
94 // Compute solution
95 Function u(V);
96 solve(a == L, u, bcs);
97
98 // Save solution in VTK format
99 File file_u("periodic.pvd");
100 file_u << u;
101
102 return 0;
103 }