Debian Codegraph @

demo/undocumented/poisson1D/cpp/main.cpp

raw
1 // Copyright (C) 2007 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 // Modified by Anders Logg, 2011
19 //
20 // First added: 2007-11-23
21 // Last changed: 2012-11-12
22 //
23 // This demo program solves Poisson's equation,
24 //
25 // - div grad u(x) = f(x)
26 //
27 // on the unit interval with source f given by
28 //
29 // f(x) = 9.0*DOLFIN_PI*DOLFIN_PI*sin(3.0*DOLFIN_PI*x[0]);
30 //
31 // and boundary conditions given by
32 //
33 // u(x) = 0 for x = 0,
34 // du/dx = 0 for x = 1.
35
36 #include <dolfin.h>
37 #include "Poisson.h"
38
39 using namespace dolfin;
40
41 // Boundary condition
42 class DirichletBoundary : public SubDomain
43 {
44 bool inside(const Array<double>& x, bool on_boundary) const
45 {
46 return (std::abs(x[0]) < DOLFIN_EPS);
47 }
48 };
49
50 // Source term
51 class Source : public Expression
52 {
53 public:
54
55 void eval(Array<double>& values, const Array<double>& x) const
56 {
57 values[0] = 9.0*DOLFIN_PI*DOLFIN_PI*sin(3.0*DOLFIN_PI*x[0]);
58 }
59
60 };
61
62 // Neumann boundary condition
63 class Flux : public Expression
64 {
65 public:
66
67 void eval(Array<double>& values, const Array<double>& x) const
68 {
69 values[0] = 3.0*DOLFIN_PI*cos(3.0*DOLFIN_PI*x[0]);
70 }
71 };
72
73 int main()
74 {
75 // Create mesh
76 auto mesh = std::make_shared<UnitIntervalMesh>(50);
77
78 // Create function space
79 auto V = std::make_shared<Poisson::FunctionSpace>(mesh);
80
81 // Set up BCs
82 auto zero = std::make_shared<Constant>(0.0);
83 auto boundary = std::make_shared<DirichletBoundary>();
84 DirichletBC bc(V, zero, boundary);
85
86 // Create source and flux terms
87 auto f = std::make_shared<Source>();
88 auto g = std::make_shared<Flux>();
89
90 // Define variational problem
91 Poisson::BilinearForm a(V, V);
92 Poisson::LinearForm L(V);
93 L.f = f;
94 L.g = g;
95
96 // Solve PDE
97 Function u(V);
98 solve(a == L, u, bc);
99
100 // Save solution in VTK format
101 File file_u("poisson.pvd");
102 file_u << u;
103
104 return 0;
105 }