Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
poisson-pq2.cc

Commented version of this example This example on gitlab

1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#include <config.h>
8
9#include <cmath>
10#include <cstddef>
11#include <iostream>
12#include <numbers>
13#include <string>
14#include <thread>
15
22
23#include <dune/geometry/type.hh>
24
25#include <dune/grid/common/gridfactory.hh>
26#include <dune/grid/uggrid.hh>
27
29#include <dune/istl/bvector.hh>
31#include <dune/istl/solvers.hh>
32
36
37#include <dune/vtk/datacollectors/lagrangedatacollector.hh>
38#include <dune/vtk/vtkwriter.hh>
39
40#include <dune/assembler/backends/istlmatrixbackend.hh>
41#include <dune/assembler/backends/istlvectorbackend.hh>
42#include <dune/assembler/defaultglobalassembler.hh>
43#include <dune/assembler/parallel/coloredrangeexecutor.hh>
44#include <dune/assembler/parallel/gridcoloring.hh>
45
49#include <dune/fufem/logger.hh>
50
51#include "utilities.hh"
52
53int main (int argc, char *argv[]) try
54{
55
56 auto log = Dune::Fufem::makeLogger(std::cout, "[% 8.3f] [% 8.3f] ");
57
58 Dune::MPIHelper::instance(argc, argv);
59
60 log("MPI initialized");
61 auto parameters = Dune::ParameterTree();
62 Dune::ParameterTreeParser::readOptions(argc, argv, parameters);
63
64 auto refinements = parameters.get<std::size_t>("refinements", 4);
65 auto threadCount = parameters.get<std::size_t>("threads", std::thread::hardware_concurrency());
66 auto tol = parameters.get<double>("tol", 1e-4);
67 auto maxIter = parameters.get<std::size_t>("maxIter", 1000);
68 auto verbosity = parameters.get<std::size_t>("verbosity", 2);
69
70 log("Command line processed");
71 auto rightHandSide = [] (const auto& x) { return 10;};
72 auto dirichletValues = [](const auto& x){ return std::sin(2*std::numbers::pi*x[0]); };
73 using Grid = Dune::UGGrid<2>;
74
75 auto factory = Dune::GridFactory<Grid>();
76 for(unsigned int k : Dune::range(9))
77 factory.insertVertex({0.5*(k%3), 0.5*(k/3)});
78 factory.insertElement(Dune::GeometryTypes::cube(2), {0, 1, 3, 4});
79 factory.insertElement(Dune::GeometryTypes::cube(2), {1, 2, 4, 5});
80 factory.insertElement(Dune::GeometryTypes::simplex(2), {3, 4, 6});
81 factory.insertElement(Dune::GeometryTypes::simplex(2), {4, 7, 6});
82 factory.insertElement(Dune::GeometryTypes::simplex(2), {4, 5, 7});
83 factory.insertElement(Dune::GeometryTypes::simplex(2), {5, 8, 7});
84
85 auto gridPtr = factory.createGrid();
86 auto& grid = *gridPtr;
87
88 log("Grid created");
89
90 grid.globalRefine(refinements);
91
92 log(Dune::formatString("Grid refined %d times", refinements));
93
94 auto gridView = grid.leafGridView();
95 auto coloredRange = Dune::Assembler::Experimental::coloredElementRange(gridView);
96
97 log("Grid coloring computed");
98
99 auto executor = Dune::Assembler::Experimental::ColoredRangeExecutor(coloredRange, threadCount);
100
101 log(Dune::formatString("Using parallelism with %d threads", threadCount));
102 using namespace Dune::Functions::BasisFactory;
103 auto basis = makeBasis(gridView, lagrange<2>());
104
105 log(Dune::formatString("Basis created with dimension %d", basis.dimension()));
108
109 auto sol = Vector();
110 auto rhs = Vector();
111 auto stiffnessMatrix = Matrix();
112 auto constraints = Dune::Fufem::makeAffineConstraints(basis);
113
114 Dune::Fufem::computeBoundaryConstraints(constraints, basis, dirichletValues);
115
116 log("Boundary constraints computed");
117 using namespace Dune::Fufem::Forms;
118
119 auto u = trialFunction(basis);
120 auto v = testFunction(basis);
121 auto f = Coefficient(Dune::Functions::makeGridViewFunction(rightHandSide, gridView));
122
123 auto a = integrate(dot(grad(u), grad(v)));
124 auto l = integrate(f*v);
125 auto matrixBackend = Dune::Assembler::ISTLMatrixBackend(stiffnessMatrix);
126 auto rhsBackend = Dune::Assembler::ISTLVectorBackend(rhs);
127 auto assembler = Dune::Assembler::Assembler(basis, basis, executor);
128
129 log("Assembler set up");
130 auto patternBuilder = matrixBackend.patternBuilder();
131
132 patternBuilder.resize(basis, basis);
133
134 assembler.assembleMatrixPattern(a, patternBuilder);
135
136 log("Matrix pattern assembled");
137 patternBuilder.setupMatrix();
138
139 log("Matrix set up");
140
141 assembler.assembleMatrixEntries(a, matrixBackend);
142
143 log("Matrix assembled");
144 rhsBackend.resize(basis);
145 rhsBackend.setZero();
146
147 assembler.assembleVectorEntries(l, rhsBackend);
148
149 log("RHS assembled");
150 constraints.constrainLinearSystem(stiffnessMatrix, rhs);
151
152 log("Boundary condition incorporated into system");
153 Dune::Assembler::ISTLVectorBackend(sol).resize(basis);
154 Dune::Assembler::ISTLVectorBackend(sol).setZero();
155 auto matrixOperator = Dune::MatrixAdapter<Matrix,Vector,Vector>(stiffnessMatrix);
156
157 auto preconditioner = makeAMGPreconditioner<Vector>(stiffnessMatrix);
158
159 log("Preconditioner created");
160
161 auto solver = Dune::CGSolver<Vector>(
162 matrixOperator,
163 preconditioner,
164 tol,
165 maxIter,
166 verbosity
167 );
168
169 log("Solver created");
170
171 auto solverStatistics = Dune::InverseOperatorResult();
172
173 solver.apply(sol, rhs, solverStatistics);
174
175 log("Linear system solved");
176 auto vtkWriter = Dune::Vtk::UnstructuredGridWriter(Dune::Vtk::LagrangeDataCollector(basis.gridView(), 2));
177 vtkWriter.addPointData(u|sol, Dune::VTK::FieldInfo("sol", Dune::VTK::FieldInfo::Type::scalar, 1));
178 vtkWriter.write("poisson-pq2");
179
180 log("Solution written to vtk file");
181}
182catch (Dune::Exception& e)
183{
184 std::cout << e.what() << std::endl;
185 return 1;
186}
int main(int argc, char **argv)
field_type dot(const type &newv) const
Y & rhs()
std::decay_t< F > makeGridViewFunction(F &&f, const GridView &gridView)
auto makeBasis(const GridView &gridView, PreBasisFactory &&preBasisFactory)
static std::string formatString(const std::string &s, const T &... args)
const char * what() const noexcept override
void computeBoundaryConstraints(AffineConstraints< BV, V, MI, C > &constraints, const Basis &basis, Function &&f, const IntersectionSet &intersectionSet)
Compute constraints for essential boundary conditions.
Definition boundaryconstraints.hh:51
auto makeAffineConstraints(const Basis &basis)
Create and initialize an AffineConstraints object for a basis.
Definition affineconstraints.hh:825
auto testFunction(const Basis &basis)
Create unary identity operator on test function space.
Definition userfunctions.hh:694
auto trialFunction(const Basis &basis)
Create unary identity operator on trial function space.
Definition userfunctions.hh:773
auto grad(const Op &op)
Obtain the gradient of an operator.
Definition userfunctions.hh:1186
::value auto integrate(MultilinearOperator op, const Domain &domain)
Integrate a k-linear operator to obtain a k-linear form.
Definition integrate.hh:53
auto makeLogger(std::ostream &stream, std::string format)
Create a simple logger callback.
Definition logger.hh:42
Definition baseclass.hh:22
const Grid & grid() const
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
static void readOptions(int argc, char *argv[], ParameterTree &pt)
LeafGridView leafGridView() const
void globalRefine(int refCount)
Coefficient function.
Definition coefficient.hh:42
T endl(T... args)
T hardware_concurrency(T... args)
T log(T... args)
T sin(T... args)