| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// MueLu: A package for multigrid based preconditioning |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the MueLu contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef MUELU_NULLSPACEPRESMOOTHFACTORY_DEF_HPP |
| 11 |
#define MUELU_NULLSPACEPRESMOOTHFACTORY_DEF_HPP |
| 12 |
|
| 13 |
#include <Xpetra_Matrix.hpp> |
| 14 |
#include <Xpetra_MultiVector.hpp> |
| 15 |
#include <Xpetra_MultiVectorFactory.hpp> |
| 16 |
|
| 17 |
#include "MueLu_NullspacePresmoothFactory_decl.hpp" |
| 18 |
#include "MueLu_Utilities.hpp" |
| 19 |
|
| 20 |
#include "MueLu_Monitor.hpp" |
| 21 |
|
| 22 |
namespace MueLu { |
| 23 |
|
| 24 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 25 |
RCP<const ParameterList> NullspacePresmoothFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetValidParameterList() const { |
| 26 |
RCP<ParameterList> validParamList = rcp(new ParameterList()); |
| 27 |
|
| 28 |
validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory for A"); |
| 29 |
validParamList->set<RCP<const FactoryBase> >("Nullspace", Teuchos::null, "Generating factory for the nonsmoothed nullspace"); |
| 30 |
|
| 31 |
return validParamList; |
| 32 |
} |
| 33 |
|
| 34 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 35 |
void NullspacePresmoothFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::DeclareInput(Level ¤tLevel) const { |
| 36 |
Input(currentLevel, "Nullspace"); |
| 37 |
|
| 38 |
if (currentLevel.GetLevelID() == 0) |
| 39 |
Input(currentLevel, "A"); |
| 40 |
} |
| 41 |
|
| 42 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 43 |
void NullspacePresmoothFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(Level ¤tLevel) const { |
| 44 |
FactoryMonitor m(*this, "Nullspace presmooth factory", currentLevel); |
| 45 |
|
| 46 |
RCP<MultiVector> newB; |
| 47 |
if (currentLevel.GetLevelID() == 0) { |
| 48 |
RCP<Matrix> A = Get<RCP<Matrix> >(currentLevel, "A"); |
| 49 |
RCP<MultiVector> B = Get<RCP<MultiVector> >(currentLevel, "Nullspace"); |
| 50 |
newB = MultiVectorFactory::Build(B->getMap(), B->getNumVectors()); |
| 51 |
|
| 52 |
Teuchos::ArrayRCP<SC> D = Utilities::GetMatrixDiagonal_arcp(*A); |
| 53 |
|
| 54 |
SC damping = 4. / 3; |
| 55 |
damping /= Utilities::PowerMethod(*A, true, (LO)10, 1e-4); |
| 56 |
|
| 57 |
A->apply(*B, *newB, Teuchos::NO_TRANS); |
| 58 |
|
| 59 |
size_t numVec = newB->getNumVectors(); |
| 60 |
LO numElements = newB->getLocalLength(); |
| 61 |
for (size_t j = 0; j < numVec; j++) { |
| 62 |
Teuchos::ArrayRCP<const SC> Bj = B->getData(j); |
| 63 |
Teuchos::ArrayRCP<SC> newBj = newB->getDataNonConst(j); |
| 64 |
|
| 65 |
for (LO i = 0; i < numElements; i++) |
| 66 |
newBj[i] = Bj[i] - damping * newBj[i] / D[i]; |
| 67 |
} |
| 68 |
} else { |
| 69 |
newB = Get<RCP<MultiVector> >(currentLevel, "Nullspace"); |
| 70 |
} |
| 71 |
|
| 72 |
// provide "Nullspace" variable on current level |
| 73 |
Set(currentLevel, "Nullspace", newB); |
| 74 |
|
| 75 |
} // Build |
| 76 |
|
| 77 |
} // namespace MueLu |
| 78 |
|
| 79 |
#endif // MUELU_NULLSPACEPRESMOOTHFACTORY_DEF_HPP |