Debian Codegraph @

demo/documented/nonlinear-poisson/cpp/main.cpp

raw
1 // Copyright (C) 2006-2007 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 // Modified by Anders Logg, 2005-2011.
19 //
20 // First added: 2005
21 // Last changed: 2012-11-12
22 //
23 // This demo illustrates how to use of DOLFIN for solving a nonlinear
24 // PDE, in this case a nonlinear variant of Poisson's equation,
25 //
26 // - div (1 + u^2) grad u(x, y) = f(x, y)
27 //
28 // on the unit square with source f given by
29 //
30 // f(x, y) = x*sin(y)
31 //
32 // and boundary conditions given by
33 //
34 // u(x, y) = 1 for x = 0
35 // du/dn(x, y) = 0 otherwise
36 //
37 // This is equivalent to solving the variational problem
38 //
39 // F(u) = ((1 + u^2)*grad(u), grad(v)) - (f, v) = 0
40
41 #include <dolfin.h>
42 #include "NonlinearPoisson.h"
43
44 using namespace dolfin;
45
46 // Right-hand side
47 class Source : public Expression
48 {
49 public:
50
51 Source() : Expression() {}
52
53 void eval(Array<double>& values, const Array<double>& x) const
54 {
55 values[0] = x[0]*sin(x[1]);
56 }
57
58 };
59
60 // Sub domain for Dirichlet boundary condition
61 class DirichletBoundary : public SubDomain
62 {
63 bool inside(const Array<double>& x, bool on_boundary) const
64 {
65 return std::abs(x[0] - 1.0) < DOLFIN_EPS && on_boundary;
66 }
67 };
68
69 int main()
70 {
71 // Create mesh and define function space
72 auto mesh = std::make_shared<UnitSquareMesh>(16, 16);
73 auto V = std::make_shared<NonlinearPoisson::FunctionSpace>(mesh);
74
75 // Define boundary condition
76 auto dirichlet_boundary = std::make_shared<DirichletBoundary>();
77 auto g = std::make_shared<Constant>(1.0);
78 DirichletBC bc(V, g, dirichlet_boundary);
79
80 // Define source and solution functions
81 auto f = std::make_shared<Source>();
82 auto u = std::make_shared<Function>(V);
83
84 // Create residual form defining (nonlinear) variational problem
85 NonlinearPoisson::LinearForm F(V);
86 F.u = u;
87 F.f = f;
88
89 // Create Jacobian form J = F' (for use in nonlinear solver).
90 NonlinearPoisson::JacobianForm J(V, V);
91 J.u = u;
92
93 // Create solver Parameters
94 Parameters params("nonlinear_variational_solver");
95 Parameters newton_params("newton_solver");
96 newton_params.add("relative_tolerance", 1e-6);
97 params.add(newton_params);
98
99 // Solve nonlinear variational problem
100 solve(F == 0, *u, bc, J, params);
101
102 // Save solution in VTK format
103 File file("nonlinear_poisson.pvd");
104 file << *u;
105
106 return 0;
107 }