| 1 |
// Copyright (C) 2011 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 |
// First added: 2011-01-05 |
| 19 |
// Last changed: 2012-07-05 |
| 20 |
// |
| 21 |
// This demo solves the Stokes equations using an iterative linear solver. |
| 22 |
// Its also demonstrates how to use a precontioner matrix that is different |
| 23 |
// from the matrix being solved. |
| 24 |
// |
| 25 |
// Note that the sign for the pressure has been flipped for symmetry. |
| 26 |
// |
| 27 |
|
| 28 |
#include <dolfin.h> |
| 29 |
#include "Stokes.h" |
| 30 |
#include "StokesPreconditioner.h" |
| 31 |
|
| 32 |
using namespace dolfin; |
| 33 |
|
| 34 |
#if defined(HAS_PETSC) || defined(HAS_TRILINOS) |
| 35 |
|
| 36 |
int main() |
| 37 |
{ |
| 38 |
// Sub domain for left-hand side |
| 39 |
class Left : public SubDomain |
| 40 |
{ |
| 41 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 42 |
{ |
| 43 |
return x[0] < DOLFIN_EPS; |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
// Sub domain for right-hand side |
| 48 |
class Right : public SubDomain |
| 49 |
{ |
| 50 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 51 |
{ |
| 52 |
return std::abs(1.0 - x[0]) < DOLFIN_EPS; |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
// Sub domain for top and bottom |
| 57 |
class TopBottom : public SubDomain |
| 58 |
{ |
| 59 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 60 |
{ |
| 61 |
return std::abs(1.0 - x[1]) < DOLFIN_EPS || std::abs(x[1]) < DOLFIN_EPS; |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
// Function for inflow boundary condition for velocity |
| 66 |
class Inflow : public Expression |
| 67 |
{ |
| 68 |
public: |
| 69 |
|
| 70 |
Inflow() : Expression(3) {} |
| 71 |
|
| 72 |
void eval(Array<double>& values, const Array<double>& x) const |
| 73 |
{ |
| 74 |
values[0] = -sin(x[1]*DOLFIN_PI); |
| 75 |
values[1] = 0.0; |
| 76 |
values[2] = 0.0; |
| 77 |
} |
| 78 |
|
| 79 |
}; |
| 80 |
|
| 81 |
// Check for available preconditioners |
| 82 |
if (!has_krylov_solver_preconditioner("amg")) |
| 83 |
{ |
| 84 |
info("Sorry, this demo is only available when DOLFIN is compiled with AMG " |
| 85 |
"preconditioner, Hypre or ML."); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
// Check for available Krylov solvers |
| 90 |
std::string krylov_method; |
| 91 |
if (has_krylov_solver_method("minres")) |
| 92 |
krylov_method = "minres"; |
| 93 |
else if (has_krylov_solver_method("tfqmr")) |
| 94 |
krylov_method = "tfqmr"; |
| 95 |
else |
| 96 |
{ |
| 97 |
info("Default linear algebra backend was not compiled with MINRES or TFQMR " |
| 98 |
"Krylov subspace method. Terminating."); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
// Create mesh |
| 103 |
auto mesh = std::make_shared<Mesh>( |
| 104 |
UnitCubeMesh::create({{16, 16, 16}}, CellType::Type::hexahedron)); |
| 105 |
|
| 106 |
// Create function space and subspaces |
| 107 |
auto W = std::make_shared<Stokes::FunctionSpace>(mesh); |
| 108 |
|
| 109 |
// Set-up infow boundary condition |
| 110 |
auto inflow_prfofile = std::make_shared<Inflow>(); |
| 111 |
auto right = std::make_shared<Right>(); |
| 112 |
auto inflow = std::make_shared<DirichletBC>(W->sub(0), inflow_prfofile, |
| 113 |
right); |
| 114 |
|
| 115 |
// Set-up no-slip boundary condition |
| 116 |
auto zero_vector = std::make_shared<Constant>(0.0, 0.0, 0.0); |
| 117 |
auto top_bottom = std::make_shared<TopBottom>(); |
| 118 |
auto noslip = std::make_shared<DirichletBC>(W->sub(0), zero_vector, |
| 119 |
top_bottom); |
| 120 |
|
| 121 |
// Create forms for the Stokes problem |
| 122 |
auto f = std::make_shared<Constant>(0.0, 0.0, 0.0); |
| 123 |
Stokes::BilinearForm a(W, W); |
| 124 |
Stokes::LinearForm L(W); |
| 125 |
L.f = f; |
| 126 |
|
| 127 |
// Create solution function |
| 128 |
Function w(W); |
| 129 |
|
| 130 |
// Create form for the Stokes preconditioner |
| 131 |
StokesPreconditioner::BilinearForm a_P(W, W); |
| 132 |
|
| 133 |
// Assemble precondtioner system (P, b_dummy) |
| 134 |
auto P = std::make_shared<Matrix>(); |
| 135 |
Vector b; |
| 136 |
assemble_system(*P, b, a_P, L, {inflow, noslip}); |
| 137 |
|
| 138 |
// Assemble Stokes system (A, b) |
| 139 |
auto A = std::make_shared<Matrix>(); |
| 140 |
assemble_system(*A, b, a, L, {inflow, noslip}); |
| 141 |
|
| 142 |
// Create Krylov solver with specified method and preconditioner |
| 143 |
KrylovSolver solver(krylov_method, "amg"); |
| 144 |
|
| 145 |
// Set operator (A) and precondtioner matrix (P) |
| 146 |
solver.set_operators(A, P); |
| 147 |
|
| 148 |
// Solve system |
| 149 |
solver.solve(*w.vector(), b); |
| 150 |
cout << "Soln norm: " << w.vector()->norm("l2") << endl; |
| 151 |
|
| 152 |
// Split solution |
| 153 |
Function u = w[0]; |
| 154 |
Function p = w[1]; |
| 155 |
|
| 156 |
// Save solution in VTK format |
| 157 |
File ufile_pvd("velocity.pvd"); |
| 158 |
ufile_pvd << u; |
| 159 |
File pfile_pvd("pressure.pvd"); |
| 160 |
pfile_pvd << p; |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
#else |
| 166 |
|
| 167 |
int main() |
| 168 |
{ |
| 169 |
info("DOLFIN has not been configured with Trilinos or PETSc. Exiting."); |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
#endif |