| 1 |
// Copyright (C) 2012-2016 Garth N. Wells |
| 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 illustrates how to solve Poisson's equation |
| 19 |
// |
| 20 |
// - div grad u(x, y) = f(x, y) |
| 21 |
// |
| 22 |
// on the unit square with pure Neumann boundary conditions: |
| 23 |
// |
| 24 |
// du/dn(x, y) = -sin(5*x) |
| 25 |
// |
| 26 |
// and source f given by |
| 27 |
// |
| 28 |
// f(x, y) = 10*exp(-((x - 0.5)^2 + (y - 0.5)^2) / 0.02) |
| 29 |
// |
| 30 |
// Since only Neumann conditions are applied, u is only determined up to |
| 31 |
// a constant c by the above equations. An addition constraint is thus |
| 32 |
// required, for instance |
| 33 |
// |
| 34 |
// \int u = 0 |
| 35 |
// |
| 36 |
// This is accomplished in this demo by using a Krylov iterative solver |
| 37 |
// that removes the component in the null space from the solution vector. |
| 38 |
|
| 39 |
#include <dolfin.h> |
| 40 |
#include "Poisson.h" |
| 41 |
|
| 42 |
using namespace dolfin; |
| 43 |
|
| 44 |
// Source term (right-hand side) |
| 45 |
class Source : public Expression |
| 46 |
{ |
| 47 |
void eval(Array<double>& values, const Array<double>& x) const |
| 48 |
{ |
| 49 |
double dx = x[0] - 0.5; |
| 50 |
double dy = x[1] - 0.5; |
| 51 |
values[0] = 10*exp(-(dx*dx + dy*dy) / 0.02); |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
// Boundary flux (Neumann boundary condition) |
| 56 |
class Flux : public Expression |
| 57 |
{ |
| 58 |
void eval(Array<double>& values, const Array<double>& x) const |
| 59 |
{ |
| 60 |
values[0] = -sin(5*x[0]); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
int main() |
| 65 |
{ |
| 66 |
#ifdef HAS_PETSC |
| 67 |
// Create mesh and function space |
| 68 |
auto mesh = std::make_shared<UnitSquareMesh>(128, 128); |
| 69 |
auto V = std::make_shared<Poisson::FunctionSpace>(mesh); |
| 70 |
|
| 71 |
// Define variational problem |
| 72 |
Poisson::BilinearForm a(V, V); |
| 73 |
Poisson::LinearForm L(V); |
| 74 |
auto f = std::make_shared<Source>(); |
| 75 |
auto g = std::make_shared<Flux>(); |
| 76 |
L.f = f; |
| 77 |
L.g = g; |
| 78 |
|
| 79 |
// Assemble system |
| 80 |
auto A = std::make_shared<PETScMatrix>(); |
| 81 |
PETScVector b; |
| 82 |
assemble(*A, a); |
| 83 |
assemble(b, L); |
| 84 |
|
| 85 |
// Create constant vector that spans null space (normalised) |
| 86 |
auto null_space_vector = b.copy(); |
| 87 |
*null_space_vector = sqrt(1.0/null_space_vector->size()); |
| 88 |
|
| 89 |
// Create null space basis object and attach to PETSc matrix |
| 90 |
VectorSpaceBasis null_space({null_space_vector}); |
| 91 |
A->set_nullspace(null_space); |
| 92 |
|
| 93 |
// Orthogonalize b with respect to the null space (this gurantees |
| 94 |
// that a solution exists) |
| 95 |
null_space.orthogonalize(b); |
| 96 |
|
| 97 |
// Set PETSc solve type (conjugate gradient) and preconditioner |
| 98 |
// (algebraic multigrid) |
| 99 |
PETScOptions::set("ksp_type", "cg"); |
| 100 |
PETScOptions::set("pc_type", "gamg"); |
| 101 |
|
| 102 |
// Since we have a singular problem, use SVD solver on the multigrid |
| 103 |
// 'coarse grid' |
| 104 |
PETScOptions::set("mg_coarse_ksp_type", "preonly"); |
| 105 |
PETScOptions::set("mg_coarse_pc_type", "svd"); |
| 106 |
|
| 107 |
// Set the solver tolerance |
| 108 |
PETScOptions::set("ksp_rtol", 1.0e-8); |
| 109 |
|
| 110 |
// Print PETSc solver configuration |
| 111 |
PETScOptions::set("ksp_view"); |
| 112 |
PETScOptions::set("ksp_monitor"); |
| 113 |
|
| 114 |
// Create PETSc Krylov solver and attach operator |
| 115 |
PETScKrylovSolver solver; |
| 116 |
solver.set_operator(A); |
| 117 |
|
| 118 |
// Set PETSc options on the solver |
| 119 |
solver.set_from_options(); |
| 120 |
|
| 121 |
// Create solution Function |
| 122 |
Function u(V); |
| 123 |
|
| 124 |
// Solve |
| 125 |
solver.solve(*u.vector(), b); |
| 126 |
|
| 127 |
// Check residual |
| 128 |
Vector residual(*u.vector()); |
| 129 |
A->mult(*u.vector(), residual); |
| 130 |
residual.axpy(-1.0, b); |
| 131 |
info("Norm of residual: %lf", residual.norm("l2")); |
| 132 |
|
| 133 |
// Write out solution to XDMF file. |
| 134 |
XDMFFile("u.xdmf").write(u); |
| 135 |
|
| 136 |
#else |
| 137 |
cout << "This demo requires DOLFIN to be confugured with PETSc." << endl; |
| 138 |
#endif |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |