| 1 |
// Copyright (C) 2006-2015 Anders Logg, Martin Sandve Alnæs |
| 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 disk with source f given by |
| 23 |
// |
| 24 |
// f(x, y) = 1.0 |
| 25 |
// |
| 26 |
// and boundary conditions given by |
| 27 |
// |
| 28 |
// u(x, y) = 0 for x^2 + y^2 = 1 |
| 29 |
|
| 30 |
#include <dolfin.h> |
| 31 |
#include "PoissonDisc.h" |
| 32 |
|
| 33 |
using namespace dolfin; |
| 34 |
|
| 35 |
// Source term (right-hand side) |
| 36 |
class Source : public Expression |
| 37 |
{ |
| 38 |
void eval(Array<double>& values, const Array<double>& x) const |
| 39 |
{ |
| 40 |
values[0] = 1.0; |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
// Sub domain for Dirichlet boundary condition |
| 45 |
class DirichletBoundary : public SubDomain |
| 46 |
{ |
| 47 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 48 |
{ |
| 49 |
return on_boundary; |
| 50 |
} |
| 51 |
}; |
| 52 |
|
| 53 |
int main() |
| 54 |
{ |
| 55 |
if (dolfin::MPI::size(MPI_COMM_WORLD) > 1) |
| 56 |
{ |
| 57 |
std::cout << "This demo does not work in parallel" << std::endl; |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
// Create mesh and function space |
| 62 |
int degree = 2; |
| 63 |
int gdim = 2; |
| 64 |
auto mesh = std::make_shared<Mesh>(UnitDiscMesh::create(MPI_COMM_WORLD, 32, degree, gdim)); |
| 65 |
|
| 66 |
auto V = std::make_shared<PoissonDisc::FunctionSpace>(mesh); |
| 67 |
|
| 68 |
// Define boundary condition |
| 69 |
auto u0 = std::make_shared<Constant>(0.0); |
| 70 |
auto boundary = std::make_shared<DirichletBoundary>(); |
| 71 |
DirichletBC bc(V, u0, boundary); |
| 72 |
|
| 73 |
// Define variational forms |
| 74 |
PoissonDisc::BilinearForm a(V, V); |
| 75 |
PoissonDisc::LinearForm L(V); |
| 76 |
|
| 77 |
auto f = std::make_shared<Source>(); |
| 78 |
L.f = f; |
| 79 |
|
| 80 |
// Compute solution |
| 81 |
auto u = std::make_shared<Function>(V); |
| 82 |
solve(a == L, *u, bc); |
| 83 |
|
| 84 |
// Error norm functional |
| 85 |
PoissonDisc::Functional M(mesh); |
| 86 |
M.uh = u; |
| 87 |
double uerror = assemble(M); |
| 88 |
std::cout << uerror << std::endl; |
| 89 |
|
| 90 |
// Save solution in VTK format |
| 91 |
//File file("poisson.pvd"); |
| 92 |
//file << u; |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |