Debian Codegraph @

demo/undocumented/gmg-poisson/cpp/main.cpp

raw
1 // Copyright (C) 2016 Patrick E. Farrell 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 // This is a highly experimental demo of a geometric multigrid solver
19 // using PETSc. It is very likely to change.
20 //
21 // This demo requires DOLFIN to be configured with PETSc enabled.
22
23 #include <dolfin.h>
24 #include "Poisson.h"
25
26 using namespace dolfin;
27
28 // Source term (right-hand side)
29 class Source : public Expression
30 {
31 void eval(Array<double>& values, const Array<double>& x) const
32 { values[0] = 1.0; }
33 };
34
35 // Normal derivative (Neumann boundary condition)
36 class dUdN : public Expression
37 {
38 void eval(Array<double>& values, const Array<double>& x) const
39 { values[0] = sin(5*x[0]); }
40 };
41
42 // Sub domain for Dirichlet boundary condition
43 class DirichletBoundary : public SubDomain
44 {
45 bool inside(const Array<double>& x, bool on_boundary) const
46 { return on_boundary; }
47 };
48
49 int main()
50 {
51 #ifdef HAS_PETSC
52
53 // Create hierarchy of meshes
54 std::vector<std::shared_ptr<Mesh>> meshes
55 = {std::make_shared<UnitSquareMesh>(16, 16),
56 std::make_shared<UnitSquareMesh>(32, 32),
57 std::make_shared<UnitSquareMesh>(64, 64)};
58
59 // Create hierarchy of funcrion spaces
60 std::vector<std::shared_ptr<const FunctionSpace>> V;
61 for (auto mesh : meshes)
62 V.push_back(std::make_shared<Poisson::FunctionSpace>(mesh));
63
64 // Define boundary condition on fine grid
65 auto ubc = std::make_shared<Constant>(0.0);
66 auto boundary = std::make_shared<DirichletBoundary>();
67 auto bc = std::make_shared<DirichletBC>(V.back(), ubc, boundary);
68
69 // Define variational forms on fine grid
70 Poisson::BilinearForm a(V.back(), V.back());
71 Poisson::LinearForm L(V.back());
72 auto f = std::make_shared<Source>();
73
74 // Assemble system
75 auto A = std::make_shared<PETScMatrix>();
76 PETScVector b;
77 assemble_system(*A, b, a, L, {bc});
78
79 // Create Krylove solver
80 PETScKrylovSolver solver;
81 solver.set_operator(A);
82
83 // Set PETSc solver type
84 PETScOptions::set("ksp_type", "richardson");
85 PETScOptions::set("pc_type", "mg");
86
87 // Set PETSc MG type and levels
88 PETScOptions::set("pc_mg_levels", V.size());
89 PETScOptions::set("pc_mg_galerkin");
90
91 // Set smoother
92 PETScOptions::set("mg_levels_ksp_type", "chebyshev");
93 PETScOptions::set("mg_levels_pc_type", "jacobi");
94
95 //Set tolerance and monitor residual
96 PETScOptions::set("ksp_monitor_true_residual");
97 PETScOptions::set("ksp_atol", 1.0e-12);
98 PETScOptions::set("ksp_rtol", 1.0e-12);
99 solver.set_from_options();
100
101 // Create PETSc DM objects
102 PETScDMCollection dm_collection(V);
103
104 // Get fine grid DM and attach fine grid DM to solver
105 solver.set_dm(dm_collection.get_dm(-1));
106 solver.set_dm_active(false);
107
108 Function u(V.back());
109 solver.solve(*u.vector(), b);
110
111 #else
112 info("This demo requires DOLFIN to be configured with PETSc");
113 #endif
114
115 return 0;
116 }