| 1 |
// Copyright (C) 2009-2015 Bartosz Sawicki, Stefano Zampini and 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 |
// Eddy currents phenomena in low conducting body can be described |
| 19 |
// using electric vector potential and curl-curl operator: |
| 20 |
// |
| 21 |
// \nabla \times \nabla \times T = - \frac{\partial B}{\partial t} |
| 22 |
// |
| 23 |
// Electric vector potential defined as: |
| 24 |
// |
| 25 |
// \nabla \times T = J |
| 26 |
// |
| 27 |
// Boundary condition |
| 28 |
// |
| 29 |
// J_n = 0, |
| 30 |
// T_t = T_w = 0, \frac{\partial T_n}{\partial n} = 0 |
| 31 |
// |
| 32 |
// which is naturaly fulfilled for zero Dirichlet BC with Nedelec |
| 33 |
// (edge) elements. |
| 34 |
// |
| 35 |
// This demo uses the auxiliary space Maxwell AMG preconditioner from |
| 36 |
// Hypre, via PETSc. It therefore requires PETSc, configured with |
| 37 |
// Hypre. |
| 38 |
|
| 39 |
#include <dolfin.h> |
| 40 |
#include "EddyCurrents.h" |
| 41 |
#include "CurrentDensity.h" |
| 42 |
#include "P1Space.h" |
| 43 |
|
| 44 |
using namespace dolfin; |
| 45 |
|
| 46 |
#if defined(HAS_PETSC) and defined(PETSC_HAVE_HYPRE) |
| 47 |
|
| 48 |
int main() |
| 49 |
{ |
| 50 |
// Set PETSc as default linear algebra backend |
| 51 |
parameters["linear_algebra_backend"] = "PETSc"; |
| 52 |
|
| 53 |
// Everywhere on exterior surface |
| 54 |
class DirichletBoundary: public SubDomain |
| 55 |
{ |
| 56 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 57 |
{ return on_boundary; } |
| 58 |
}; |
| 59 |
|
| 60 |
// Load sphere mesh and refine uniformly |
| 61 |
auto mesh = std::make_shared<Mesh>("../sphere.xml.gz"); |
| 62 |
mesh = std::make_shared<Mesh>(refine(*mesh)); |
| 63 |
|
| 64 |
// Homogeneous external magnetic field (dB/dt) |
| 65 |
auto dbdt = std::make_shared<Constant>(0.0, 0.0, 1.0); |
| 66 |
|
| 67 |
// Dirichlet boundary condition |
| 68 |
auto zero = std::make_shared<Constant>(0.0, 0.0, 0.0); |
| 69 |
|
| 70 |
// Define function space and boundary condition |
| 71 |
auto V = std::make_shared<EddyCurrents::FunctionSpace>(mesh); |
| 72 |
auto boundary = std::make_shared<DirichletBoundary>(); |
| 73 |
auto bc = std::make_shared<DirichletBC>(V, zero, boundary); |
| 74 |
|
| 75 |
// Define variational problem for T |
| 76 |
EddyCurrents::BilinearForm a(V, V); |
| 77 |
EddyCurrents::LinearForm L(V); |
| 78 |
L.dbdt = dbdt; |
| 79 |
|
| 80 |
// Solution function |
| 81 |
auto T = std::make_shared<Function>(V); |
| 82 |
|
| 83 |
// Assemble system |
| 84 |
auto A = std::make_shared<PETScMatrix>(); |
| 85 |
PETScVector b; |
| 86 |
assemble_system(*A, b, a, L, {bc}); |
| 87 |
|
| 88 |
// Create PETSc Krylov solver |
| 89 |
PETScKrylovSolver solver("cg"); |
| 90 |
|
| 91 |
// Get pointer to PETSc KSP object and set tolerances |
| 92 |
KSP ksp = solver.ksp(); |
| 93 |
KSPSetTolerances(ksp, 1.0e-8, 1.0e-12, 1.0e10, 1000); |
| 94 |
|
| 95 |
// Set preconditioner to AMS from HYPRE |
| 96 |
PC pc; |
| 97 |
KSPGetPC(ksp, &pc); |
| 98 |
PCSetType(pc, PCHYPRE); |
| 99 |
PCHYPRESetType(pc, "ams"); |
| 100 |
|
| 101 |
// Build discrete gradient operator and attach to preconditioner |
| 102 |
P1Space::FunctionSpace P1(mesh); |
| 103 |
auto G = DiscreteOperators::build_gradient(*V, P1); |
| 104 |
PCHYPRESetDiscreteGradient(pc, as_type<PETScMatrix>(*G).mat()); |
| 105 |
|
| 106 |
// Inform preconditioner of constants in the Nedelec space |
| 107 |
std::vector<Function> constants(3, Function(V)); |
| 108 |
constants[0] = Constant(1.0, 0.0, 0.0); |
| 109 |
constants[1] = Constant(0.0, 1.0, 0.0); |
| 110 |
constants[2] = Constant(0.0, 0.0, 1.0); |
| 111 |
|
| 112 |
PCHYPRESetEdgeConstantVectors(pc, |
| 113 |
as_type<PETScVector>(*constants[0].vector()).vec(), |
| 114 |
as_type<PETScVector>(*constants[1].vector()).vec(), |
| 115 |
as_type<PETScVector>(*constants[2].vector()).vec()); |
| 116 |
|
| 117 |
// We are dealing with a zero conductivity problem (no mass term), |
| 118 |
// so we need to tell the preconditioner |
| 119 |
PCHYPRESetBetaPoissonMatrix(pc, NULL); |
| 120 |
|
| 121 |
// Set PETSc operators |
| 122 |
solver.set_operator(A); |
| 123 |
|
| 124 |
// Set PETSc Krylov prefix solver and set some options |
| 125 |
KSPSetOptionsPrefix(ksp, "eddy_"); |
| 126 |
PETScOptions::set("eddy_ksp_monitor_true_residual"); |
| 127 |
PETScOptions::set("eddy_ksp_view"); |
| 128 |
KSPSetFromOptions(ksp); |
| 129 |
|
| 130 |
// Solve system |
| 131 |
KSPSolve(ksp, b.vec(), as_type<PETScVector>(*T->vector()).vec()); |
| 132 |
|
| 133 |
// Update ghost values in solution vector |
| 134 |
T->vector()->apply("insert"); |
| 135 |
|
| 136 |
// Define variational problem for J |
| 137 |
auto V1 = std::make_shared<CurrentDensity::FunctionSpace>(mesh); |
| 138 |
CurrentDensity::BilinearForm a1(V1,V1); |
| 139 |
CurrentDensity::LinearForm L1(V1); |
| 140 |
L1.T = T; |
| 141 |
|
| 142 |
// Solve problem using an iterative linear solver |
| 143 |
Function J(V1); |
| 144 |
Parameters p; |
| 145 |
p.add("linear_solver", "cg"); |
| 146 |
p.add("symmetric", true); |
| 147 |
solve(a1 == L1, J, p); |
| 148 |
|
| 149 |
File file("current_density.pvd"); |
| 150 |
file << J; |
| 151 |
|
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
#else |
| 156 |
|
| 157 |
int main() |
| 158 |
{ |
| 159 |
info("This demo requires DOLFIN to be configured with PETSc (with Hypre)."); |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
#endif |