1
2
3
4
5
6
7#include <config.h>
8
9#include <vector>
10#include <cmath>
11
12#include <dune/common/bitsetvector.hh>
13#include <dune/common/rangeutilities.hh>
14
15#include <dune/geometry/quadraturerules.hh>
16
17#include <dune/grid/yaspgrid.hh>
18#include <dune/grid/uggrid.hh>
19#include <dune/grid/io/file/vtk/subsamplingvtkwriter.hh>
20
21#include <dune/istl/matrix.hh>
22#include <dune/istl/bcrsmatrix.hh>
23#include <dune/istl/matrixindexset.hh>
24#include <dune/istl/preconditioners.hh>
25#include <dune/istl/solvers.hh>
26
32
34
36
37template <class LocalView, class MatrixType>
38void getLocalMatrix(const LocalView& localView, MatrixType& elementMatrix)
39{
40
41 typedef typename LocalView::Element Element;
42 const Element& element = localView.element();
43
44 const int dim = Element::dimension;
45 auto geometry = element.geometry();
46
47
48 const auto& localFiniteElement = localView.tree().finiteElement();
49
50
51 elementMatrix.setSize(localFiniteElement.localBasis().size(),localFiniteElement.localBasis().size());
52 elementMatrix = 0;
53
54
55 int order = 2*(dim*localFiniteElement.localBasis().order()-1);
56 const QuadratureRule<double, dim>& quad = QuadratureRules<double, dim>::rule(element.type(), order);
57
58
59 for (size_t pt=0; pt < quad.size(); pt++) {
60
61
62 const FieldVector<double,dim>& quadPos = quad[pt].position();
63
64
65 const auto& jacobianInverse = geometry.jacobianInverse(quadPos);
66
67
68 const double integrationElement = geometry.integrationElement(quadPos);
69
70
71 std::vector<FieldMatrix<double,1,dim> > referenceJacobians;
72 localFiniteElement.localBasis().evaluateJacobian(quadPos, referenceJacobians);
73
74
75 std::vector<FieldMatrix<double,1,dim> > jacobians(referenceJacobians.size());
76 for (size_t i=0; i<jacobians.size(); i++)
77 jacobians[i] = referenceJacobians[i] * jacobianInverse;
78
79
80 for (size_t i=0; i<elementMatrix.N(); i++)
81 for (size_t j=0; j<elementMatrix.M(); j++ )
82 elementMatrix[i][j] += (jacobians[i] * transpose(jacobians[j])) * quad[pt].weight() * integrationElement;
83
84 }
85
86}
88
90
91template <class LocalView, class LocalVolumeTerm>
92void getVolumeTerm( const LocalView& localView,
93 BlockVector<double>& localRhs,
94 LocalVolumeTerm&& localVolumeTerm)
95{
96
97 typedef typename LocalView::Element Element;
98 const Element& element = localView.element();
99
100 const int dim = Element::dimension;
101
102
103 const auto& localFiniteElement = localView.tree().finiteElement();
104
105
106 localRhs.resize(localFiniteElement.localBasis().size());
107 localRhs = 0;
108
109
110 int order = dim*localFiniteElement.localBasis().order();
111 const QuadratureRule<double, dim>& quad = QuadratureRules<double, dim>::rule(element.type(), order);
112
113
114 for ( size_t pt=0; pt < quad.size(); pt++ ) {
115
116
117 const FieldVector<double,dim>& quadPos = quad[pt].position();
118
119
120 const double integrationElement = element.geometry().integrationElement(quadPos);
121
122 double functionValue = localVolumeTerm(quadPos);
123
124
125 std::vector<FieldVector<double,1> > shapeFunctionValues;
126 localFiniteElement.localBasis().evaluateFunction(quadPos, shapeFunctionValues);
127
128
129 for (size_t i=0; i<localRhs.size(); i++)
130 localRhs[i] += shapeFunctionValues[i] * functionValue * quad[pt].weight() * integrationElement;
131
132 }
133
134}
136
138
139template <class FEBasis>
140void getOccupationPattern(const FEBasis& feBasis, MatrixIndexSet& nb)
141{
142
143 auto n = feBasis.size();
144
145 nb.resize(n, n);
146
147
148 auto localView = feBasis.localView();
149
150
151 for(
const auto& e :
elements(feBasis.gridView()))
152 {
153
154 localView.bind(e);
155
156
157 for (size_t i=0; i<localView.tree().size(); i++) {
158
159 for (size_t j=0; j<localView.tree().size(); j++) {
160
161 auto iIdx = localView.index(i);
162 auto jIdx = localView.index(j);
163
164
165 nb.add(iIdx, jIdx);
166
167 }
168
169 }
170
171 }
172
173}
175
177
178template <class FEBasis, class VolumeTerm>
179void assembleLaplaceMatrix(const FEBasis& feBasis,
180 BCRSMatrix<double>& matrix,
181 BlockVector<double>& rhs,
182 VolumeTerm&& volumeTerm)
183{
184
185 typedef typename FEBasis::GridView GridView;
186 GridView gridView = feBasis.gridView();
187
189
190
191
192
193
194
195 MatrixIndexSet occupationPattern;
196 getOccupationPattern(feBasis, occupationPattern);
197
198
199 occupationPattern.exportIdx(matrix);
200
201
202 rhs.resize(feBasis.size());
203
204
205 matrix = 0;
206 rhs = 0;
207
208
209 auto localView = feBasis.localView();
210
211
212 for(
const auto& e :
elements(gridView))
213 {
214
215
216 localView.bind(e);
217
218
219
220 Matrix<double> elementMatrix;
221 getLocalMatrix(localView, elementMatrix);
222
223
224 for (size_t i=0; i<elementMatrix.N(); i++) {
225
226
227 auto row = localView.index(i);
228
229 for (size_t j=0; j<elementMatrix.M(); j++ ) {
230
231
232 auto col = localView.index(j);
233 matrix[row][col] += elementMatrix[i][j];
234
235 }
236
237 }
238
239
240 BlockVector<double> localRhs;
241 localVolumeTerm.bind(e);
242 getVolumeTerm(localView, localRhs, localVolumeTerm);
243
244 for (size_t i=0; i<localRhs.size(); i++) {
245
246
247 auto row = localView.index(i);
248 rhs[row] += localRhs[i];
249
250 }
251
252 }
253
254}
256
257
258
259
260
261
262
263
264
266template <class FEBasis>
267void boundaryTreatment (const FEBasis& feBasis, std::vector<char>& dirichletNodes )
268{
269 dirichletNodes.clear();
270 dirichletNodes.resize(feBasis.size(), false);
271
273 dirichletNodes[index] = true;
274 });
275}
277
279auto createMixedGrid()
280{
281 using Grid = Dune::UGGrid<2>;
282 Dune::GridFactory<Grid> factory;
283 for(unsigned int k : Dune::range(9))
284 factory.insertVertex({0.5*(k%3), 0.5*(k/3)});
285 factory.insertElement(Dune::GeometryTypes::cube(2), {0, 1, 3, 4});
286 factory.insertElement(Dune::GeometryTypes::cube(2), {1, 2, 4, 5});
287 factory.insertElement(Dune::GeometryTypes::simplex(2), {3, 4, 6});
288 factory.insertElement(Dune::GeometryTypes::simplex(2), {4, 7, 6});
289 factory.insertElement(Dune::GeometryTypes::simplex(2), {4, 5, 7});
290 factory.insertElement(Dune::GeometryTypes::simplex(2), {5, 8, 7});
291 return factory.createGrid();
292}
294
296int main (int argc, char *argv[]) try
297{
298
299 MPIHelper::instance(argc, argv);
301
303
305
307 auto grid = createMixedGrid();
308
309 grid->globalRefine(4);
310
311 auto gridView = grid->leafGridView();
312 using GridView = decltype(gridView);
314
316
318
321 FEBasis feBasis(gridView);
323
325
327
329 using VectorType = BlockVector<double>;
330 using MatrixType = BCRSMatrix<double>;
331
332 VectorType rhs;
333 MatrixType stiffnessMatrix;
334
336
338
339 std::cout << "Number of DOFs is " << feBasis.dimension() << std::endl;
340
341 auto rightHandSide = [] (const auto& x) { return 10;};
342
343 Dune::Timer timer;
344 assembleLaplaceMatrix(feBasis, stiffnessMatrix, rhs, rightHandSide);
345 std::cout << "Assembling the problem took " << timer.elapsed() << "s" << std::endl;
346
348
350 VectorType x(feBasis.size());
351 x = 0;
352
353
354 std::vector<char> dirichletNodes;
355 boundaryTreatment(feBasis, dirichletNodes);
356
357
358
359
360 auto pi = std::acos(-1.0);
361 auto dirichletValueFunction = [pi](const auto& x){ return std::sin(2*pi*x[0]); };
362
363
364 interpolate(feBasis, x, dirichletValueFunction, dirichletNodes);
365
367
369
370
371
372
373 stiffnessMatrix.mmv(x, rhs);
374
375
376 for (size_t i=0; i<stiffnessMatrix.N(); i++) {
377 if (dirichletNodes[i]) {
378 rhs[i] = x[i];
379
380 for (auto&& [entry, idx] : sparseRange(stiffnessMatrix[i]))
381 entry = (i==idx) ? 1.0 : 0.0;
382 }
383 else {
384
385 for (auto&& [entry, idx] : sparseRange(stiffnessMatrix[i]))
386 if (dirichletNodes[idx])
387 entry = 0.0;
388 }
389 }
390
392
394
395
396 MatrixAdapter<MatrixType,VectorType,VectorType> op(stiffnessMatrix);
397
398
399 SeqILDL<MatrixType,VectorType,VectorType> ildl(stiffnessMatrix,1.0);
400
401
402 CGSolver<VectorType> cg(op,
403 ildl,
404 1e-4,
405 100,
406 2);
407
408
409 InverseOperatorResult statistics;
410
411
412 cg.apply(x, rhs, statistics);
413
415
417
419
421
422
424 SubsamplingVTKWriter<GridView> vtkWriter(gridView, Dune::refinementLevels(2));
425 vtkWriter.addVertexData(xFunction, VTK::FieldInfo("x", VTK::FieldInfo::Type::scalar, 1));
426 vtkWriter.write("poisson-pq2");
427
428 }
429
430 catch (Exception& e) {
431 std::cout << e.what() << std::endl;
432 }
auto makeDiscreteGlobalBasisFunction(B &&basis, V &&vector)
Generate a DiscreteGlobalBasisFunction.
Definition discreteglobalbasisfunction.hh:458
DefaultGlobalBasis< LagrangePreBasis< GV, k, R > > LagrangeBasis
Nodal basis of a scalar k-th-order Lagrangean finite element space.
Definition lagrangebasis.hh:532
void forEachBoundaryDOF(const Basis &basis, F &&f)
Loop over all DOFs on the boundary.
Definition boundarydofs.hh:40
auto elements(const SubDomainGridView< HostGridView > &subDomainGridView)
ADL findable access to element range for a SubDomainGridView.
Definition subdomain.hh:487
Definition monomialset.hh:19
void interpolate(const B &basis, C &&coeff, const F &f, const BV &bv, const NTRE &nodeToRangeEntry)
Interpolate given function in discrete function space.
Definition interpolate.hh:205
std::decay_t< F > makeGridViewFunction(F &&f, const GridView &gridView)
Construct a function modeling GridViewFunction from function and grid view.
Definition gridviewfunction.hh:72