Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_DiagonallyScaledPreconditionerFactory.cpp
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 // Teko includes
11 #include "Teko_DiagonallyScaledPreconditionerFactory.hpp"
12 
13 #include "Teko_PreconditionerInverseFactory.hpp"
14 
15 namespace Teko {
16 
19  : invFactory_(Teuchos::null), scalingType_(ROW_SCALING), diagonalType_(AbsRowSum) {}
20 
25  const Teuchos::RCP<Teko::InverseFactory>& invFactory, ScalingType scalingType,
26  DiagonalType diagonalType)
27  : invFactory_(invFactory), scalingType_(scalingType), diagonalType_(diagonalType) {}
28 
30 
35  LinearOp& lo, PreconditionerState& state) const {
36  using Teuchos::RCP;
37  using Teuchos::rcp_dynamic_cast;
38  Teko_DEBUG_SCOPE("DiagonallyScaledPreconditionerFactory::buildPreconditionerOperator", 10);
39 
40  TEUCHOS_TEST_FOR_EXCEPTION(
41  invFactory_ == Teuchos::null, std::runtime_error,
42  "ERROR: Teko::DiagonallyScaledPreconditionerFactory::buildPreconditionerOperator requires "
43  "that an "
44  << "inverse factory has been set. Currently it is null!");
45 
46  // get diagonal matrix
47  LinearOp invD = getInvDiagonalOp(lo, diagonalType_);
48 
49  // M = A * invD
50  ModifiableLinearOp& M = state.getModifiableOp("op_M");
51  if (scalingType_ == COLUMN_SCALING)
52  M = explicitMultiply(lo, invD, M);
53  else
54  M = explicitMultiply(invD, lo, M);
55 
56  // build inverse operator
57  ModifiableLinearOp& invM = state.getModifiableOp("op_invM");
58  if (invM == Teuchos::null)
59  invM = buildInverse(*invFactory_, M);
60  else
61  rebuildInverse(*invFactory_, M, invM);
62 
63  // return invD * invM
64  if (scalingType_ == COLUMN_SCALING)
65  return multiply(invD, invM.getConst());
66  else
67  return multiply(invM.getConst(), invD);
68 }
69 
74  const Teuchos::ParameterList& settings) {
75  TEUCHOS_TEST_FOR_EXCEPTION(
76  not settings.isParameter("Inverse Factory"), std::runtime_error,
77  "Parameter \"Inverse Factory\" is required by a Teko::DiagonallyScaledPreconditionerFactory");
78 
79  // grab library and preconditioner name
80  std::string invName = settings.get<std::string>("Inverse Factory");
81 
82  // build preconditioner factory
83  Teuchos::RCP<const InverseLibrary> il = getInverseLibrary();
84  invFactory_ = il->getInverseFactory(invName);
85  TEUCHOS_TEST_FOR_EXCEPTION(invFactory_ == Teuchos::null, std::runtime_error,
86  "ERROR: \"Inverse Factory\" = " << invName << " could not be found");
87 
88  // get scaling type specified by XML file
89  const std::string defaultScaleType = "Row";
90  const std::string scalingTypeString = "Scaling Type";
91  std::string scaleType = defaultScaleType;
92  if (settings.isParameter(scalingTypeString))
93  scaleType = settings.get<std::string>(scalingTypeString);
94 
95  if (defaultScaleType == scaleType)
96  scalingType_ = ROW_SCALING;
97  else
98  scalingType_ = COLUMN_SCALING;
99 
100  if (settings.isParameter("Diagonal Type"))
101  diagonalType_ = Teko::getDiagonalType(settings.get<std::string>("Diagonal Type"));
102 }
103 
104 } // end namespace Teko
void rebuildInverse(const InverseFactory &factory, const LinearOp &A, InverseLinearOp &invA)
virtual LinearOp buildPreconditionerOperator(LinearOp &lo, PreconditionerState &state) const
Function that is called to build the preconditioner for the linear operator that is passed in...
virtual void initializeFromParameterList(const Teuchos::ParameterList &settings)
This function builds the internals of the preconditioner factory from a parameter list...
virtual ~DiagonallyScaledPreconditionerFactory()
default destructor: prints out diagnostic string
DiagonallyScaledPreconditionerFactory()
Default constructor, for use with the AutoClone class.
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A)
Build an inverse operator using a factory and a linear operator.
Teuchos::RCP< const InverseLibrary > getInverseLibrary() const
Get the inverse library used by this preconditioner factory.
An implementation of a state object preconditioners.
virtual Teko::ModifiableLinearOp & getModifiableOp(const std::string &name)
Add a named operator to the state object.