| 1 |
// Copyright (C) 2010 Marie E. Rognes |
| 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 Johannes Ring, 2011 |
| 19 |
// |
| 20 |
// First added: 2010 |
| 21 |
// Last changed: 2012-11-12 |
| 22 |
// |
| 23 |
// This demo program illustrates how to solve Poisson's equation |
| 24 |
// |
| 25 |
// - div grad u(x, y) = f(x, y) |
| 26 |
// |
| 27 |
// on the unit square with pure Neumann boundary conditions: |
| 28 |
// |
| 29 |
// du/dn(x, y) = -sin(5*x) |
| 30 |
// |
| 31 |
// and source f given by |
| 32 |
// |
| 33 |
// f(x, y) = 10*exp(-((x - 0.5)^2 + (y - 0.5)^2) / 0.02) |
| 34 |
// |
| 35 |
// Since only Neumann conditions are applied, u is only determined up to |
| 36 |
// a constant c by the above equations. An addition constraint is thus |
| 37 |
// required, for instance |
| 38 |
// |
| 39 |
// \int u = 0 |
| 40 |
// |
| 41 |
// This can be accomplished by introducing the constant c as an |
| 42 |
// additional unknown (to be sought in the space of real numbers) |
| 43 |
// and the above constraint. |
| 44 |
|
| 45 |
#include <dolfin.h> |
| 46 |
#include "Poisson.h" |
| 47 |
|
| 48 |
using namespace dolfin; |
| 49 |
|
| 50 |
// Source term (right-hand side) |
| 51 |
class Source : public Expression |
| 52 |
{ |
| 53 |
void eval(Array<double>& values, const Array<double>& x) const |
| 54 |
{ |
| 55 |
double dx = x[0] - 0.5; |
| 56 |
double dy = x[1] - 0.5; |
| 57 |
values[0] = 10*exp(-(dx*dx + dy*dy) / 0.02); |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
// Boundary flux (Neumann boundary condition) |
| 62 |
class Flux : public Expression |
| 63 |
{ |
| 64 |
void eval(Array<double>& values, const Array<double>& x) const |
| 65 |
{ |
| 66 |
values[0] = -sin(5*x[0]); |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
int main() |
| 71 |
{ |
| 72 |
// Create mesh and function space |
| 73 |
auto mesh = std::make_shared<Mesh>( |
| 74 |
UnitSquareMesh::create({{64, 64}}, CellType::Type::quadrilateral)); |
| 75 |
auto V = std::make_shared<Poisson::FunctionSpace>(mesh); |
| 76 |
|
| 77 |
// Define variational problem |
| 78 |
auto a = std::make_shared<Poisson::BilinearForm>(V, V); |
| 79 |
auto L = std::make_shared<Poisson::LinearForm>(V); |
| 80 |
auto f = std::make_shared<Source>(); |
| 81 |
auto g = std::make_shared<Flux>(); |
| 82 |
L->f = f; |
| 83 |
L->g = g; |
| 84 |
auto w = std::make_shared<Function>(V); |
| 85 |
std::vector<std::shared_ptr<const DirichletBC>> bcs; |
| 86 |
auto problem = std::make_shared<LinearVariationalProblem>(a, L, w, bcs); |
| 87 |
|
| 88 |
// Compute solution |
| 89 |
LinearVariationalSolver solver(problem); |
| 90 |
solver.solve(); |
| 91 |
|
| 92 |
// Extract subfunction |
| 93 |
Function u = (*w)[0]; |
| 94 |
|
| 95 |
// Write to file |
| 96 |
XDMFFile("u.xdmf").write(u); |
| 97 |
|
| 98 |
return 0; |
| 99 |
} |