| 1 |
// Copyright (C) 2005-2010 Anders Logg |
| 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 |
#include <dolfin.h> |
| 19 |
#include "Poisson2D_1.h" |
| 20 |
#include "Poisson2D_2.h" |
| 21 |
#include "Poisson2D_3.h" |
| 22 |
#include "Poisson2D_4.h" |
| 23 |
#include "Poisson2D_5.h" |
| 24 |
#include "Poisson3D_1.h" |
| 25 |
#include "Poisson3D_2.h" |
| 26 |
#include "Poisson3D_3.h" |
| 27 |
#include "Poisson3D_4.h" |
| 28 |
|
| 29 |
using namespace dolfin; |
| 30 |
|
| 31 |
// Boundary condition |
| 32 |
class DirichletBoundary : public SubDomain |
| 33 |
{ |
| 34 |
public: |
| 35 |
|
| 36 |
DirichletBoundary() {} |
| 37 |
|
| 38 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 39 |
{ return on_boundary; } |
| 40 |
}; |
| 41 |
|
| 42 |
// Right-hand side, 2D |
| 43 |
class Source2D : public Expression |
| 44 |
{ |
| 45 |
void eval(Array<double>& values, const Array<double>& x) const |
| 46 |
{ |
| 47 |
values[0] = 2.0*DOLFIN_PI*DOLFIN_PI*sin(DOLFIN_PI*x[0])*sin(DOLFIN_PI*x[1]); |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
// Right-hand side, 3D |
| 52 |
class Source3D : public Expression |
| 53 |
{ |
| 54 |
void eval(Array<double>& values, const Array<double>& x) const |
| 55 |
{ |
| 56 |
values[0] = 3.0*DOLFIN_PI*DOLFIN_PI*sin(DOLFIN_PI*x[0])*sin(DOLFIN_PI*x[1])*sin(DOLFIN_PI*x[2]); |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
// Solve equation and compute error, 2D |
| 61 |
double solve2D(int q, int n) |
| 62 |
{ |
| 63 |
printf("Solving Poisson's equation in 2D for q = %d, n = %d.\n", q, n); |
| 64 |
|
| 65 |
// Set up problem |
| 66 |
auto mesh = std::make_shared<UnitSquareMesh>(n, n); |
| 67 |
auto f = std::make_shared<Source2D>(); |
| 68 |
auto zero = std::make_shared<const Constant>(0.0); |
| 69 |
|
| 70 |
// Choose forms |
| 71 |
Form* a = 0; |
| 72 |
Form* L = 0; |
| 73 |
std::shared_ptr<const FunctionSpace> V; |
| 74 |
switch (q) |
| 75 |
{ |
| 76 |
case 1: |
| 77 |
V = std::make_shared<Poisson2D_1::FunctionSpace>(mesh); |
| 78 |
a = new Poisson2D_1::BilinearForm(V, V); |
| 79 |
L = new Poisson2D_1::LinearForm(V, f); |
| 80 |
break; |
| 81 |
case 2: |
| 82 |
V = std::make_shared<Poisson2D_2::FunctionSpace>(mesh); |
| 83 |
a = new Poisson2D_2::BilinearForm(V, V); |
| 84 |
L = new Poisson2D_2::LinearForm(V, f); |
| 85 |
break; |
| 86 |
case 3: |
| 87 |
V = std::make_shared<Poisson2D_3::FunctionSpace>(mesh); |
| 88 |
a = new Poisson2D_3::BilinearForm(V, V); |
| 89 |
L = new Poisson2D_3::LinearForm(V, f); |
| 90 |
break; |
| 91 |
case 4: |
| 92 |
V = std::make_shared<Poisson2D_4::FunctionSpace>(mesh); |
| 93 |
a = new Poisson2D_4::BilinearForm(V, V); |
| 94 |
L = new Poisson2D_4::LinearForm(V, f); |
| 95 |
break; |
| 96 |
case 5: |
| 97 |
V = std::make_shared<Poisson2D_5::FunctionSpace>(mesh); |
| 98 |
a = new Poisson2D_5::BilinearForm(V, V); |
| 99 |
L = new Poisson2D_5::LinearForm(V, f); |
| 100 |
break; |
| 101 |
default: |
| 102 |
error("Forms not compiled for q = %d.", q); |
| 103 |
} |
| 104 |
|
| 105 |
// Set up boundary conditions |
| 106 |
auto boundary = std::make_shared<const DirichletBoundary>(); |
| 107 |
DirichletBC bc(V, zero, boundary); |
| 108 |
|
| 109 |
// Discretize equation |
| 110 |
Matrix A; |
| 111 |
Vector x, b; |
| 112 |
assemble(A, *a); |
| 113 |
assemble(b, *L); |
| 114 |
bc.apply(A, b); |
| 115 |
|
| 116 |
// Solve the linear system |
| 117 |
KrylovSolver solver("gmres"); |
| 118 |
solver.parameters["relative_tolerance"] = 1e-14; |
| 119 |
solver.solve(A, x, b); |
| 120 |
|
| 121 |
// Compute maximum norm of error |
| 122 |
double emax = 0.0; |
| 123 |
std::vector<double> U; |
| 124 |
x.get_local(U); |
| 125 |
for (VertexIterator v(*mesh); !v.end(); ++v) |
| 126 |
{ |
| 127 |
const Point p = v->point(); |
| 128 |
const double u = sin(DOLFIN_PI*p.x())*sin(DOLFIN_PI*p.y()); |
| 129 |
const double e = std::abs(U[v->index()] - u); |
| 130 |
emax = std::max(emax, e); |
| 131 |
} |
| 132 |
|
| 133 |
delete a; |
| 134 |
delete L; |
| 135 |
|
| 136 |
return emax; |
| 137 |
} |
| 138 |
|
| 139 |
// Solve equation and compute error, 3D |
| 140 |
double solve3D(int q, int n) |
| 141 |
{ |
| 142 |
printf("Solving Poisson's equation in 3D for q = %d, n = %d.\n", q, n); |
| 143 |
|
| 144 |
// Set up problem |
| 145 |
auto mesh = std::make_shared<UnitCubeMesh>(n, n, n); |
| 146 |
auto f = std::make_shared<Source3D>(); |
| 147 |
auto zero = std::make_shared<const Constant>(0.0); |
| 148 |
|
| 149 |
// Choose forms |
| 150 |
Form* a = 0; |
| 151 |
Form* L = 0; |
| 152 |
std::shared_ptr<const FunctionSpace> V; |
| 153 |
switch (q) |
| 154 |
{ |
| 155 |
case 1: |
| 156 |
V = std::make_shared<Poisson3D_1::FunctionSpace>(mesh); |
| 157 |
a = new Poisson3D_1::BilinearForm(V, V); |
| 158 |
L = new Poisson3D_1::LinearForm(V, f); |
| 159 |
break; |
| 160 |
case 2: |
| 161 |
V = std::make_shared<Poisson3D_2::FunctionSpace>(mesh); |
| 162 |
a = new Poisson3D_2::BilinearForm(V, V); |
| 163 |
L = new Poisson3D_2::LinearForm(V, f); |
| 164 |
break; |
| 165 |
case 3: |
| 166 |
V = std::make_shared<Poisson3D_3::FunctionSpace>(mesh); |
| 167 |
a = new Poisson3D_3::BilinearForm(V, V); |
| 168 |
L = new Poisson3D_3::LinearForm(V, f); |
| 169 |
break; |
| 170 |
case 4: |
| 171 |
V = std::make_shared<Poisson3D_4::FunctionSpace>(mesh); |
| 172 |
a = new Poisson3D_4::BilinearForm(V, V); |
| 173 |
L = new Poisson3D_4::LinearForm(V, f); |
| 174 |
break; |
| 175 |
default: |
| 176 |
error("Forms not compiled for q = %d.", q); |
| 177 |
} |
| 178 |
|
| 179 |
// Set up boundary conditions |
| 180 |
auto boundary = std::make_shared<const DirichletBoundary>(); |
| 181 |
DirichletBC bc(V, zero, boundary); |
| 182 |
|
| 183 |
// Discretize equation |
| 184 |
Matrix A; |
| 185 |
Vector x, b; |
| 186 |
assemble(A, *a); |
| 187 |
assemble(b, *L); |
| 188 |
bc.apply(A, b); |
| 189 |
|
| 190 |
// Solve the linear system |
| 191 |
KrylovSolver solver("gmres"); |
| 192 |
solver.parameters["relative_tolerance"] = 1e-14; |
| 193 |
solver.solve(A, x, b); |
| 194 |
|
| 195 |
// Compute maximum norm of error |
| 196 |
double emax = 0.0; |
| 197 |
std::vector<double> U; |
| 198 |
x.get_local(U); |
| 199 |
for (VertexIterator v(*mesh); !v.end(); ++v) |
| 200 |
{ |
| 201 |
const Point p = v->point(); |
| 202 |
const double u = sin(DOLFIN_PI*p.x())*sin(DOLFIN_PI*p.y())*sin(DOLFIN_PI*p.z()); |
| 203 |
const double e = std::abs(U[v->index()] - u); |
| 204 |
emax = std::max(emax, e); |
| 205 |
} |
| 206 |
|
| 207 |
delete a; |
| 208 |
delete L; |
| 209 |
|
| 210 |
return emax; |
| 211 |
} |
| 212 |
|
| 213 |
int main() |
| 214 |
{ |
| 215 |
info("Runtime of convergence benchmark"); |
| 216 |
|
| 217 |
set_log_active(false); |
| 218 |
|
| 219 |
const int qmax_2D = 5; |
| 220 |
const int qmax_3D = 4; |
| 221 |
const int num_meshes = 3; |
| 222 |
std::vector<std::vector<double>> e2D(qmax_2D, |
| 223 |
std::vector<double>(num_meshes)); |
| 224 |
std::vector<std::vector<double>> e3D(qmax_3D, |
| 225 |
std::vector<double>(num_meshes)); |
| 226 |
|
| 227 |
// Compute errors in 2D |
| 228 |
for (int q = 1; q <= qmax_2D; q++) |
| 229 |
{ |
| 230 |
int n = 2; |
| 231 |
for (int i = 0; i < num_meshes; i++) |
| 232 |
{ |
| 233 |
e2D[q - 1][i] = solve2D(q, n); |
| 234 |
n *= 2; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// Compute errors in 3D |
| 239 |
for (int q = 1; q <= qmax_3D; q++) |
| 240 |
{ |
| 241 |
int n = 2; |
| 242 |
for (int i = 0; i < num_meshes; i++) |
| 243 |
{ |
| 244 |
e3D[q - 1][i] = solve3D(q, n); |
| 245 |
n *= 2; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// Write errors in 2D |
| 250 |
printf("\nMaximum norm error in 2D:\n"); |
| 251 |
printf("-------------------------\n"); |
| 252 |
for (int q = 1; q <= qmax_2D; q++) |
| 253 |
{ |
| 254 |
printf("q = %d:", q); |
| 255 |
for (int i = 0; i < num_meshes; i++) |
| 256 |
printf(" %.3e", e2D[q - 1][i]); |
| 257 |
printf("\n"); |
| 258 |
} |
| 259 |
|
| 260 |
// Write errors in 3D |
| 261 |
printf("\nMaximum norm error in 3D:\n"); |
| 262 |
printf("-------------------------\n"); |
| 263 |
for (int q = 1; q <= qmax_3D; q++) |
| 264 |
{ |
| 265 |
printf("q = %d:", q); |
| 266 |
for (int i = 0; i < num_meshes; i++) |
| 267 |
printf(" %.3e", e3D[q - 1][i]); |
| 268 |
printf("\n"); |
| 269 |
} |
| 270 |
} |