KASKADE 7 development version
Loading...
Searching...
No Matches
taylorHoodPreconditioner.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2023-2024 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef TAYLORHOODPRECONDITIONER_HH
14#define TAYLORHOODPRECONDITIONER_HH
15
16#include "dune/common/config.h"
17
18
19#include "fem/linearspace.hh"
20#include "fem/supports.hh"
23
24#include "dune/grid/common/rangegenerators.hh"
25#include "dune/istl/preconditioner.hh"
26
27#include <fstream>
28
29
30namespace Kaskade
31{
33 {
82 template <int dim, class Real=double>
84 {
85 using Entry = Dune::FieldMatrix<Real,1,1>;
86 using Matrix = DynamicMatrix<Entry>;
87
88 template <class R, int n>
90
92 using EntryB = Dune::FieldMatrix<Real,1,dim>;
93
94
95 public:
98
100
104 std::vector<int> const& constraintIndices() const;
105
119 template <class R>
120 void apply(Vector<R,dim>& u, Vector<R,1>& p,
121 Vector<R,dim> f, Vector<R,1> const& g) const;
122
123 Matrix const& getInvK() const { return invK; }
124
125 private:
126 Matrix invK; // inverse of local Stokes system
127 std::vector<EntryA> D; // square root of inverse diagonal of A
128 std::vector<int> idx; // row indices of given B that are retained.
129 };
130 }
131
132
143
210 template <class X>
212 : public Dune::Preconditioner<X,X>
213 {
214 public:
215
219 constexpr static int dim = X::template Component<0>::block_type::dimension;
220
224 using domain_type = X;
225
234
235 using field_type = typename X::field_type;
237
238 private:
240
241 public:
247 template <class AGOP, class VelocitySpace>
249 VelocitySpace const& velocitySpace)
250 {
251 abort();
252 }
253
261 template <class MatrixA, class MatrixB, class MatrixBt, class VelocitySpace>
262 TaylorHoodPreconditioner(MatrixA const& A, MatrixB const& B, MatrixBt const& Bt,
263 VelocitySpace const& velocitySpace)
264 {
265 auto const& gridView = velocitySpace.gridView();
266 static_assert(MatrixA::block_type::rows==dim,"matrix block row size should agree with domain");
267 static_assert(MatrixA::block_type::cols==dim,"matrix block col size should agree with domain");
268 assert(A.M() == A.N());
269 assert(A.M() == B.M());
270
271 // for each patch around a vertex, store the global velocity dof indices
272 vPatchDofs.resize(gridView.size(dim));
273 pPatchDofs.resize(gridView.size(dim));
274
275 auto patches = computePatches(gridView);
276 assert(patches.size()==vPatchDofs.size());
277
278 // for each dof, count the number of incident patches
279 velocityPatchCount.resize(A.N(),0);
280 pressurePatchCount.resize(B.N(),0);
281
282// std::ofstream out("out.m");
283// std::cout << "A = " << 2*A.N() << " x " << 2*A.M() << "\n";
284// std::cout << "B = " << B.N() << " x " << 2*B.M() << "\n";
285// out << "function [A,B] = out\n";
286// out << "A = [" << A << "];\n";
287// out << "B = [" << B << "];\n";
288// out.close();
289
290 for (size_t i=0; i<patches.size(); ++i) // TODO: parallelize as in domainDecompositionPreconditioner.hh
291 {
292 // Find all velocity degrees of freedom with support inside the patch
293 auto const& vDofs = algebraicPatch(velocitySpace,patches[i]);
294// std::cout << "vdofs = ["; for (int j: vDofs) std::cout << 2*j+1 << " " << 2*j+2 << " "; std::cout << "];\n";
295
296 // Find all pressure constraints which affect any of the velocity dofs on
297 // the patch. This is done by collecting all column indices of B' in rows
298 // given by the velocity dofs.
299 std::vector<size_t> pressureDofs;
300 for (size_t const i: vDofs)
301 for (auto ci=Bt[i].begin(); ci!=Bt[i].end(); ++ci)
302 pressureDofs.push_back(ci.index());
303
304 std::sort(pressureDofs.begin(),pressureDofs.end());
305 pressureDofs.erase(std::unique(pressureDofs.begin(),pressureDofs.end()),
306 pressureDofs.end());
307// std::cout << "pdofs = ["; for (int j: pressureDofs) std::cout << j+1 << " "; std::cout << "];\n";
308
309 // Create local solver. The local solver may have discarded some rows of B
310 // if they were linearly dependent. Get the retained local indices and map
311 // them back to global ones.
315 invK(submatrix<DenseMatrixA>(A,vDofs,vDofs),
316 submatrix<DenseMatrixB>(B,pressureDofs,vDofs),1e4);
317 std::vector<size_t> pDofs;
318 for (int k: invK.constraintIndices())
319 pDofs.push_back(pressureDofs[k]);
320 std::swap(pDofs,pressureDofs);
321
322 // update the patch count for incident dofs
323 // TODO: lock this if going parallel
324 for (size_t uIdx: vDofs)
325 ++velocityPatchCount[uIdx];
326 for (size_t pIdx: pressureDofs)
327 ++pressurePatchCount[pIdx];
328
329 // Move over to global data structure
330 vPatchDofs[i] = std::move(vDofs);
331 pPatchDofs[i] = std::move(pressureDofs);
332 localInverse.push_back(std::move(invK));
333 }
334
335 // Check if every constraint has found at least one patch caring for it explicitly
336 std::cout << "checking " << pressurePatchCount.size() << " pressure constraints:\n";
337 for (int i=0; i<pressurePatchCount.size(); ++i)
338 if (pressurePatchCount[i]==0)
339 std::cout << "pressure node " << i << " not covered!\n";
340
341 }
342
348 virtual void apply(domain_type& x, range_type const& y) override
349 {
351 }
352
361 {
362 using VectorU = typename domain_type::template Component<0>;
363 using VectorP = typename domain_type::template Component<1>;
364
365 // Initialize to zero, as we need to accumulate the solution contributions of
366 // all the patch systems.
367 x = 0;
368
369 // TODO: parallelize
370 for (int k=0; k<vPatchDofs.size(); ++k)
371 {
372 int const nu = vPatchDofs[k].size();
373 int const np = pPatchDofs[k].size();
374
375 // gather velocity and pressure rhs entries with appropriate weight
376 int prolongation = 0;
377
378 // gather velocity rhs
379 VectorU rf(nu);
380 for (int i=0; i<nu; ++i)
381 {
382 auto uIdx = vPatchDofs[k][i];
383 double w = weight(velocityPatchCount[uIdx],prolongation,flavour);
384 rf[i] = component<0>(y)[uIdx] * w;
385 }
386
387 // gather pressure rhs
388 VectorP rg(np);
389 for (int i=0; i<np; ++i)
390 {
391 auto pIdx = pPatchDofs[k][i];
392 double w = weight(pressurePatchCount[pIdx],prolongation,flavour);
393 rg[i] = component<1>(y)[pIdx] * w;
394 }
395
396// std::cout << "local rhs: f=[" << rf << "]; g=[" << rg << "];\n";
397
398
399 // solve system
400 VectorU zu(rf.N());
401 VectorP zp(rg.N());
402 localInverse[k].apply(zu,zp,rf,rg);
403
404// std::cout << "local solution: u=[" << zu << "]; p=[" << zp << "];\n";
405
406 // write velocity and pressure to solution, with appropriate scaling
407 prolongation = 1;
408
409 // scatter velocity
410 for (int i=0; i<nu; ++i)
411 {
412 auto uIdx = vPatchDofs[k][i];
413 double w = weight(velocityPatchCount[uIdx],prolongation,flavour);
414 component<0>(x)[uIdx] += zu[i] * w;
415 }
416
417 // scatter pressure
418 for (int i=0; i<np; ++i)
419 {
420 auto pIdx = pPatchDofs[k][i];
421 double w = weight(pressurePatchCount[pIdx],prolongation,flavour);
422 component<1>(x)[pIdx] += zp[i] * w;
423 }
424 }
425 }
426
427 virtual bool requiresInitializedInput() const
428 {
429 return true;
430 }
431
432 virtual void pre (domain_type& x, range_type&) override {}
433 virtual void post (domain_type& x) override {}
434 virtual Dune::SolverCategory::Category category() const override
435 {
436 return Dune::SolverCategory::sequential;
437 }
438
439 private:
440 // determines restriction (prolongation==0) or prolongation (prolongation==1). See class
441 // documentation for a detailed explanation.
442 double weight(int count, int prolongation, TaylorHoodPreconditionerFlavour flavour) const
443 {
444 assert(count > 0);
445 double w = 1.0/count;
446
448 w = std::sqrt(w);
450 w = 1;
452 w = 1;
453
454 return w;
455 }
456
457 // for each patch around a vertex, store the global velocity and pressure dof indices
458 std::vector<std::vector<size_t>> vPatchDofs;
459 std::vector<std::vector<size_t>> pPatchDofs;
460 std::vector<std::array<std::vector<size_t>,2>> patchDofs;
461
462 // for each patch store the local inverse
463 std::vector<LocalSolver> localInverse;
464
465 // for each global velocity and pressure dof, store the number of incident patches
466 std::vector<int> velocityPatchCount;
467 std::vector<int> pressurePatchCount;
468 };
469
470 // ----------------------------------------------------------------------------------------------
471
480 template <class AGOP>
482 : public SymmetricPreconditioner<typename AGOP::Scalar,
483 typename AGOP::Domain>
484 {
485 public:
486 using domain_type = typename AGOP::Domain;
487 using range_type = typename AGOP::Range;
488 static_assert(std::is_same_v<domain_type,range_type>,"need a symmetric saddle point operator");
489 using field_type = typename AGOP::Scalar;
490
498 : K(&K_)
499 , hatKinv(*K)
500 {
501 }
502
510 void init(domain_type& x, range_type const& y) const
511 {
513 }
514
515 virtual void apply(domain_type& x, range_type const& y) override
516 {
517 // apply presmoother
518 auto z = x;
519 z = 0;
521
522 // compute residual r = y - Kz
523 auto r = y;
524 K->applyscaleadd(-1.0,z,r);
525
526 // apply postsmoother
528 x += z;
529 }
530
531 private:
532 AGOP const* K;
533
535 };
536
537}
538
539#endif
A LAPACK-compatible dense matrix class with shape specified at runtime.
Interface for symmetric preconditioners.
SymmetricTaylorHoodPreconditioner(AGOP const &K_)
Constructor.
void init(domain_type &x, range_type const &y) const
Computes an approximate solution satisfying the constraints.
virtual void apply(domain_type &x, range_type const &y) override
Solves small Stokes systems even if incompressibility constraints are linearly dependent.
void apply(Vector< R, dim > &u, Vector< R, 1 > &p, Vector< R, dim > f, Vector< R, 1 > const &g) const
Applys the preconditioner to the right hand side .
std::vector< int > const & constraintIndices() const
the indices of B rows that are actually retained in the reduced system
Constraint preconditioner for Taylor-Hood discretizations of incompressibility problems.
virtual Dune::SolverCategory::Category category() const override
virtual void post(domain_type &x) override
TaylorHoodPreconditioner(AGOP const &K, VelocitySpace const &velocitySpace)
Constructor.
void apply(domain_type &x, range_type const &y, TaylorHoodPreconditionerFlavour flavour) const
Applies the preconditioner either as presmoother, postsmoother, or in its symmetric flavour.
virtual void pre(domain_type &x, range_type &) override
static constexpr int dim
The dimension of the velocity component.
X domain_type
The domain type of the saddle point system.
TaylorHoodPreconditioner(MatrixA const &A, MatrixB const &B, MatrixBt const &Bt, VelocitySpace const &velocitySpace)
Constructor.
virtual void apply(domain_type &x, range_type const &y) override
Appliess the symmetric preconditioner as .
domain_type range_type
The range type of the saddle point system.
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition fixdune.hh:667
std::vector< size_t > algebraicPatch(Space const &space, Range const &cells)
Computes the global indices of all ansatz functions whose support is a subset of the union of given c...
Definition supports.hh:42
auto computePatches(GridView const &gridview)
Computes the patches around vertices of the grid view.
Definition supports.hh:165
DynamicMatrix< MEntry > submatrix(DynamicMatrix< MEntry > const &A, int rowstart, int rowend, int colstart, int colend)
extracts contiguous submatrices, copying the data
NumaBCRSMatrix< Dune::FieldMatrix< typename FineSpace::Scalar, 1, 1 >, SparseIndex > prolongation(CoarseSpace const &coarseSpace, FineSpace const &fineSpace)
Computes an interpolation-based prolongation matrix from a (supposedly) coarser space to a finer spac...
TaylorHoodPreconditionerFlavour
tag for controlling the TaylorHoodPreconditioner flavour
auto & component(LinearProductSpace< Scalar, Sequence > &x)