Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_HierarchicalGaussSeidelPreconditionerFactory.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Teko: A package for block and physics based preconditioning
4 //
5 // Copyright 2010 NTESS and the Teko contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef __Teko_HierarchicalGaussSeidelPreconditionerFactory_hpp__
11 #define __Teko_HierarchicalGaussSeidelPreconditionerFactory_hpp__
12 
13 #include "Teuchos_RCP.hpp"
14 
15 #include "Teko_BlockPreconditionerFactory.hpp"
16 #include "Teko_BlockImplicitLinearOp.hpp"
17 #include "Teko_Utilities.hpp"
18 #include <map>
19 #include <vector>
20 
21 namespace Teko {
22 
23 class NestedBlockGS : public BlockImplicitLinearOp {
24  public:
25  NestedBlockGS(const std::map<int, std::vector<int>>& blockToRow_,
26  const std::map<int, LinearOp>& blockToInvOp_, BlockedLinearOp& A_,
27  bool useLowerTriangle_ = false);
28 
29  VectorSpace range() const override { return productRange_; }
30  VectorSpace domain() const override { return productDomain_; }
31 
32  void implicitApply(const BlockedMultiVector& r, BlockedMultiVector& z, const double alpha = 1.0,
33  const double beta = 0.0) const override;
34 
35  private:
36  void upperTriangularImplicitApply(std::vector<BlockedMultiVector>& r,
37  std::vector<BlockedMultiVector>& z, const double alpha = 1.0,
38  const double beta = 0.0) const;
39 
40  void lowerTriangularImplicitApply(std::vector<BlockedMultiVector>& r,
41  std::vector<BlockedMultiVector>& z, const double alpha = 1.0,
42  const double beta = 0.0) const;
43 
44  // block operators
45  std::map<int, std::vector<int>> blockToRow;
46  std::map<int, LinearOp> blockToInvOp;
47  std::vector<LinearOp> invOps;
48  BlockedLinearOp A;
49  std::vector<BlockedLinearOp> Ab;
50  bool useLowerTriangle = false;
51 
52  Teuchos::RCP<const Thyra::ProductVectorSpaceBase<double>> productRange_;
53  Teuchos::RCP<const Thyra::ProductVectorSpaceBase<double>>
54  productDomain_;
55 };
56 
57 class HierarchicalGaussSeidelPreconditionerFactory : public BlockPreconditionerFactory {
58  public:
59  ~HierarchicalGaussSeidelPreconditionerFactory() override = default;
60  HierarchicalGaussSeidelPreconditionerFactory();
61 
62  LinearOp buildPreconditionerOperator(BlockedLinearOp& blo,
63  BlockPreconditionerState& state) const override;
64 
65  protected:
66  void initializeFromParameterList(const Teuchos::ParameterList& pl) override;
68 
69  private:
70  LinearOp buildBlockInverse(const InverseFactory& invFact,
71  const Teuchos::RCP<InverseFactory>& precFact,
72  const BlockedLinearOp& matrix, BlockPreconditionerState& state,
73  int hierarchicalBlockNum) const;
74  template <typename LinearOpType>
75  LinearOp buildInverseImpl(const InverseFactory& invFact,
76  const Teuchos::RCP<InverseFactory>& precFact,
77  const LinearOpType& matrix, BlockPreconditionerState& state,
78  int hierarchicalBlockNum) const {
79  std::stringstream ss;
80  ss << "hierarchical_block_" << hierarchicalBlockNum;
81 
82  ModifiableLinearOp& invOp = state.getModifiableOp(ss.str());
83  ModifiableLinearOp& precOp = state.getModifiableOp("prec_" + ss.str());
84 
85  if (precFact != Teuchos::null) {
86  if (precOp == Teuchos::null) {
87  precOp = precFact->buildInverse(matrix);
88  state.addModifiableOp("prec_" + ss.str(), precOp);
89  } else {
90  Teko::rebuildInverse(*precFact, matrix, precOp);
91  }
92  }
93 
94  if (invOp == Teuchos::null)
95  if (precOp.is_null())
96  invOp = Teko::buildInverse(invFact, matrix);
97  else
98  invOp = Teko::buildInverse(invFact, matrix, precOp);
99  else {
100  if (precOp.is_null())
101  Teko::rebuildInverse(invFact, matrix, invOp);
102  else
103  Teko::rebuildInverse(invFact, matrix, precOp, invOp);
104  }
105 
106  return invOp;
107  }
108 
109  std::map<int, std::vector<int>> blockToRow;
110  std::map<int, Teuchos::RCP<InverseFactory>> blockToInverse;
111  std::map<int, Teuchos::RCP<InverseFactory>> blockToPreconditioner;
112  mutable std::map<int, LinearOp> blockToInvOp;
113 
114  bool useLowerTriangle = false;
115 };
116 } // end namespace Teko
117 
118 #endif
void rebuildInverse(const InverseFactory &factory, const LinearOp &A, InverseLinearOp &invA)
virtual LinearOp buildPreconditionerOperator(BlockedLinearOp &blo, BlockPreconditionerState &state) const =0
Function that is called to build the preconditioner for the linear operator that is passed in...
virtual LinearOp buildPreconditionerOperator(LinearOp &blo, PreconditionerState &state) const
Function that is called to build the preconditioner for the linear operator that is passed in...
virtual void implicitApply(const Thyra::EOpTransp M_trans, const BlockedMultiVector &x, BlockedMultiVector &y, const double alpha=1.0, const double beta=0.0) const
Perform a matrix vector multiply with this implicitly defined blocked operator.
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A)
Build an inverse operator using a factory and a linear operator.