1
// Copyright (C) 2007 Anders Logg.
2
// Licensed under the GNU LGPL Version 2.1.
4
// First added: 2007-07-11
5
// Last changed: 2007-08-20
7
// This demo program solves Poisson's equation
9
// - div grad u(x, y) = f(x, y)
11
// on the unit square with homogeneous Dirichlet boundary conditions
12
// at y = 0, 1 and periodic boundary conditions at x = 0, 1.
17
using namespace dolfin;
22
class Source : public Function
26
Source(Mesh& mesh) : Function(mesh) {}
28
real eval(const real* x) const
32
return x[0]*sin(5.0*DOLFIN_PI*x[1]) + 1.0*exp(-(dx*dx + dy*dy)/0.02);
37
// Sub domain for Dirichlet boundary condition
38
class DirichletBoundary : public SubDomain
40
bool inside(const real* x, bool on_boundary) const
42
return x[1] < DOLFIN_EPS || x[1] > (1.0 - DOLFIN_EPS) && on_boundary;
46
// Sub domain for Periodic boundary condition
47
class PeriodicBoundary : public SubDomain
49
bool inside(const real* x, bool on_boundary) const
51
return x[0] < DOLFIN_EPS && x[0] > -DOLFIN_EPS && on_boundary;
54
void map(const real* x, real* y) const
62
UnitSquare mesh(32, 32);
67
// Create Dirichlet boundary condition
68
Function u0(mesh, 0.0);
69
DirichletBoundary dirichlet_boundary;
70
DirichletBC bc0(u0, mesh, dirichlet_boundary);
72
// Create periodic boundary condition
73
PeriodicBoundary periodic_boundary;
74
PeriodicBC bc1(mesh, periodic_boundary);
76
// Collect boundary conditions
77
Array<BoundaryCondition*> bcs(&bc0, &bc1);
80
PoissonBilinearForm a;
81
PoissonLinearForm L(f);
82
LinearPDE pde(a, L, mesh, bcs);
91
// Save solution to file
92
File file("poisson.pvd");