1
// Copyright (C) 2005-2007 Anders Logg.
2
// Licensed under the GNU LGPL Version 2.1.
5
// Last changed: 2007-08-20
8
#include "Poisson2D_1.h"
9
#include "Poisson2D_2.h"
10
#include "Poisson2D_3.h"
11
#include "Poisson2D_4.h"
12
#include "Poisson2D_5.h"
13
#include "Poisson3D_1.h"
14
#include "Poisson3D_2.h"
15
#include "Poisson3D_3.h"
16
#include "Poisson3D_4.h"
17
#include "Poisson3D_5.h"
19
using namespace dolfin;
22
class DirichletBoundary : public SubDomain
24
bool inside(const real* x, bool on_boundary) const
30
// Right-hand side, 2D
31
class Source2D : public Function
35
Source2D(Mesh& mesh) : Function(mesh) {}
37
real eval(const real* x) const
39
return 2.0*DOLFIN_PI*DOLFIN_PI*sin(DOLFIN_PI*x[0])*sin(DOLFIN_PI*x[1]);
44
// Right-hand side, 3D
45
class Source3D : public Function
49
Source3D(Mesh& mesh) : Function(mesh) {}
51
real eval(const real* x) const
53
return 3.0*DOLFIN_PI*DOLFIN_PI*sin(DOLFIN_PI*x[0])*sin(DOLFIN_PI*x[1])*sin(DOLFIN_PI*x[2]);
58
// Solve equation and compute error, 2D
59
real solve2D(int q, int n)
61
message("--------------------------------------------------");
62
message("Solving Poisson's equation in 2D for q = %d, n = %d.", q, n);
63
message("--------------------------------------------------");
66
UnitSquare mesh(n, n);
68
Function zero(mesh, 0.0);
69
DirichletBoundary boundary;
70
DirichletBC bc(zero, mesh, boundary);
78
a = new Poisson2D_1BilinearForm();
79
L = new Poisson2D_1LinearForm(f);
82
a = new Poisson2D_2BilinearForm();
83
L = new Poisson2D_2LinearForm(f);
86
a = new Poisson2D_3BilinearForm();
87
L = new Poisson2D_3LinearForm(f);
90
a = new Poisson2D_4BilinearForm();
91
L = new Poisson2D_4LinearForm(f);
94
a = new Poisson2D_5BilinearForm();
95
L = new Poisson2D_5LinearForm(f);
98
error("Forms not compiled for q = %d.", q);
101
// Discretize equation
104
assemble(A, *a, mesh);
105
assemble(b, *L, mesh);
108
// Solve the linear system
109
KrylovSolver solver(gmres);
110
solver.set("Krylov relative tolerance", 1e-14);
111
solver.solve(A, x, b);
113
// Compute maximum norm of error
115
real* U = new real[x.size()];
117
for (VertexIterator v(mesh); !v.end(); ++v)
119
const Point p = v->point();
120
const real u = sin(DOLFIN_PI*p.x())*sin(DOLFIN_PI*p.y());
121
const real e = std::abs(U[v->index()] - u);
122
emax = std::max(emax, e);
132
// Solve equation and compute error, 3D
133
real solve3D(int q, int n)
135
message("--------------------------------------------------");
136
message("Solving Poisson's equation in 3D for q = %d, n = %d.", q, n);
137
message("--------------------------------------------------");
140
UnitCube mesh(n, n, n);
142
Function zero(mesh, 0.0);
143
DirichletBoundary boundary;
144
DirichletBC bc(zero, mesh, boundary);
152
a = new Poisson3D_1BilinearForm();
153
L = new Poisson3D_1LinearForm(f);
156
a = new Poisson3D_2BilinearForm();
157
L = new Poisson3D_2LinearForm(f);
160
a = new Poisson3D_3BilinearForm();
161
L = new Poisson3D_3LinearForm(f);
164
a = new Poisson3D_4BilinearForm();
165
L = new Poisson3D_4LinearForm(f);
168
a = new Poisson3D_5BilinearForm();
169
L = new Poisson3D_5LinearForm(f);
172
error("Forms not compiled for q = %d.", q);
175
// Discretize equation
178
assemble(A, *a, mesh);
179
assemble(b, *L, mesh);
182
// Solve the linear system
183
KrylovSolver solver(gmres);
184
solver.set("Krylov relative tolerance", 1e-14);
185
solver.solve(A, x, b);
187
// Compute maximum norm of error
189
real* U = new real[x.size()];
191
for (VertexIterator v(mesh); !v.end(); ++v)
193
const Point p = v->point();
194
const real u = sin(DOLFIN_PI*p.x())*sin(DOLFIN_PI*p.y())*sin(DOLFIN_PI*p.z());
195
const real e = std::abs(U[v->index()] - u);
196
emax = std::max(emax, e);
209
const int num_meshes = 3;
210
real e2D[qmax][num_meshes];
211
real e3D[qmax][num_meshes];
213
// Compute errors in 2D
214
for (int q = 1; q <= qmax; q++)
217
for (int i = 0; i < num_meshes; i++)
219
e2D[q - 1][i] = solve2D(q, n);
224
// Compute errors in 3D
225
for (int q = 1; q <= qmax; q++)
228
for (int i = 0; i < num_meshes; i++)
230
e3D[q - 1][i] = solve3D(q, n);
235
// Write errors in 2D
236
printf("\nMaximum norm error in 2D:\n");
237
printf("-------------------------\n");
238
for (int q = 1; q <= qmax; q++)
240
printf("q = %d:", q);
241
for (int i = 0; i < num_meshes; i++)
242
printf(" %.3e", e2D[q - 1][i]);
246
// Write errors in 3D
247
printf("\nMaximum norm error in 3D:\n");
248
printf("-------------------------\n");
249
for (int q = 1; q <= qmax; q++)
251
printf("q = %d:", q);
252
for (int i = 0; i < num_meshes; i++)
253
printf(" %.3e", e3D[q - 1][i]);