10 #ifndef THYRA_MUELU_MAXWELL1_PRECONDITIONER_FACTORY_DEF_HPP
11 #define THYRA_MUELU_MAXWELL1_PRECONDITIONER_FACTORY_DEF_HPP
15 #include <Xpetra_CrsMatrixWrap.hpp>
19 #include <MueLu_Maxwell1.hpp>
20 #include <Xpetra_TpetraHalfPrecisionOperator.hpp>
22 #if defined(HAVE_MUELU_STRATIMIKOS) && defined(HAVE_MUELU_THYRA)
25 #if ((defined(HAVE_TPETRA_INST_DOUBLE) && defined(HAVE_TPETRA_INST_FLOAT) && !defined(HAVE_TPETRA_INST_COMPLEX_DOUBLE) && !defined(HAVE_TPETRA_INST_COMPLEX_FLOAT)) || \
26 (!defined(HAVE_TPETRA_INST_DOUBLE) && !defined(HAVE_TPETRA_INST_FLOAT) && defined(HAVE_TPETRA_INST_COMPLEX_DOUBLE) && defined(HAVE_TPETRA_INST_COMPLEX_FLOAT)) || \
27 (defined(HAVE_TPETRA_INST_DOUBLE) && defined(HAVE_TPETRA_INST_FLOAT) && defined(HAVE_TPETRA_INST_COMPLEX_DOUBLE) && defined(HAVE_TPETRA_INST_COMPLEX_FLOAT)))
28 #define MUELU_CAN_USE_MIXED_PRECISION
36 using Teuchos::rcp_const_cast;
37 using Teuchos::rcp_dynamic_cast;
41 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
42 MueLuMaxwell1PreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MueLuMaxwell1PreconditionerFactory()
43 : paramList_(
rcp(new ParameterList())) {}
47 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
48 bool MueLuMaxwell1PreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::isCompatible(
const LinearOpSourceBase<Scalar>& fwdOpSrc)
const {
49 const RCP<const LinearOpBase<Scalar>> fwdOp = fwdOpSrc.getOp();
51 if (Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::isTpetra(fwdOp))
return true;
53 #ifdef HAVE_MUELU_EPETRA
54 if (Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::isEpetra(fwdOp))
return true;
60 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
61 RCP<PreconditionerBase<Scalar>> MueLuMaxwell1PreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createPrec()
const {
65 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
66 void MueLuMaxwell1PreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
67 initializePrec(
const RCP<
const LinearOpSourceBase<Scalar>>& fwdOpSrc, PreconditionerBase<Scalar>* prec,
const ESupportSolveUse supportSolveUse)
const {
70 typedef Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node> XpThyUtils;
72 typedef Thyra::LinearOpBase<Scalar> ThyLinOpBase;
74 #if defined(MUELU_CAN_USE_MIXED_PRECISION)
75 typedef Xpetra::TpetraHalfPrecisionOperator<Scalar, LocalOrdinal, GlobalOrdinal, Node> XpHalfPrecOp;
77 typedef typename XpHalfPrecOp::HalfScalar HalfScalar;
93 ParameterList paramList = *paramList_;
96 const RCP<const ThyLinOpBase> fwdOp = fwdOpSrc->getOp();
100 bool bIsEpetra = XpThyUtils::isEpetra(fwdOp);
101 bool bIsTpetra = XpThyUtils::isTpetra(fwdOp);
106 RCP<XpMat> A = XpThyUtils::toXpetra(Teuchos::rcp_const_cast<ThyLinOpBase>(fwdOp));
114 RCP<ThyLinOpBase> thyra_precOp = Teuchos::null;
115 thyra_precOp = rcp_dynamic_cast<Thyra::LinearOpBase<Scalar>>(defaultPrec->getNonconstUnspecifiedPrecOp(),
true);
120 const bool startingOver = (thyra_precOp.is_null() || !paramList.isParameter(
"Maxwell1: enable reuse") || !paramList.get<
bool>(
"Maxwell1: enable reuse"));
121 const bool useHalfPrecision = paramList.get<
bool>(
"half precision",
false) && bIsTpetra;
124 if (startingOver ==
true) {
126 std::list<std::string> convertXpetra = {
"Coordinates",
"Nullspace",
"Kn",
"D0"};
127 for (
auto it = convertXpetra.begin(); it != convertXpetra.end(); ++it)
128 Converters<Scalar, LocalOrdinal, GlobalOrdinal, Node>::replaceWithXpetra(paramList, *it);
130 std::list<std::string> sublists = {
"maxwell1: 11list",
"maxwell1: 22list"};
131 for (
auto itSublist = sublists.begin(); itSublist != sublists.end(); ++itSublist)
132 if (paramList.isSublist(*itSublist)) {
133 ParameterList& sublist = paramList.sublist(*itSublist);
134 for (
int lvlNo = 0; lvlNo < 10; ++lvlNo) {
135 if (sublist.isSublist(
"level " + std::to_string(lvlNo) +
" user data")) {
136 ParameterList& lvlList = sublist.sublist(
"level " + std::to_string(lvlNo) +
" user data");
137 std::list<std::string> convertKeys;
138 for (
auto it = lvlList.begin(); it != lvlList.end(); ++it)
139 convertKeys.push_back(lvlList.name(it));
140 for (
auto it = convertKeys.begin(); it != convertKeys.end(); ++it)
141 Converters<Scalar, LocalOrdinal, GlobalOrdinal, Node>::replaceWithXpetra(lvlList, *it);
146 ParameterList& sublist = paramList.sublist(
"maxwell1: 11list");
147 if (sublist.isParameter(
"D0")) {
148 Converters<Scalar, LocalOrdinal, GlobalOrdinal, Node>::replaceWithXpetra(sublist,
"D0");
151 paramList.set<
bool>(
"Maxwell1: use as preconditioner",
true);
152 if (useHalfPrecision) {
153 #if defined(MUELU_CAN_USE_MIXED_PRECISION)
156 RCP<XphMat> halfA = Xpetra::convertToHalfPrecision(A);
157 if (paramList.isType<RCP<XpmMV>>(
"Coordinates")) {
158 RCP<XpmMV> coords = paramList.get<RCP<XpmMV>>(
"Coordinates");
159 paramList.remove(
"Coordinates");
160 RCP<XphmMV> halfCoords = Xpetra::convertToHalfPrecision(coords);
161 paramList.set(
"Coordinates", halfCoords);
163 if (paramList.isType<RCP<XpMV>>(
"Nullspace")) {
164 RCP<XpMV> nullspace = paramList.get<RCP<XpMV>>(
"Nullspace");
165 paramList.remove(
"Nullspace");
166 RCP<XphMV> halfNullspace = Xpetra::convertToHalfPrecision(nullspace);
167 paramList.set(
"Nullspace", halfNullspace);
169 std::list<std::string> convertMat = {
"Kn",
"D0"};
170 for (
auto it = convertMat.begin(); it != convertMat.end(); ++it) {
171 if (paramList.isType<RCP<XpMat>>(*it)) {
172 RCP<XpMat> M = paramList.get<RCP<XpMat>>(*it);
173 paramList.remove(*it);
174 RCP<XphMat> halfM = Xpetra::convertToHalfPrecision(M);
175 paramList.set(*it, halfM);
181 xpPrecOp =
rcp(
new XpHalfPrecOp(halfPrec));
188 xpPrecOp = rcp_dynamic_cast<XpOp>(preconditioner);
193 RCP<ThyXpOp> thyXpOp = rcp_dynamic_cast<ThyXpOp>(thyra_precOp,
true);
194 RCP<XpOp> xpOp = thyXpOp->getXpetraOperator();
195 #if defined(MUELU_CAN_USE_MIXED_PRECISION)
196 RCP<XpHalfPrecOp> xpHalfPrecOp = rcp_dynamic_cast<XpHalfPrecOp>(xpOp);
197 if (!xpHalfPrecOp.is_null()) {
199 RCP<XphMat> halfA = Xpetra::convertToHalfPrecision(A);
200 preconditioner->resetMatrix(halfA);
201 xpPrecOp = rcp_dynamic_cast<XpOp>(preconditioner);
207 xpPrecOp = rcp_dynamic_cast<XpOp>(preconditioner);
212 RCP<const VectorSpaceBase<Scalar>> thyraRangeSpace = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyra(xpPrecOp->getRangeMap());
213 RCP<const VectorSpaceBase<Scalar>> thyraDomainSpace = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyra(xpPrecOp->getDomainMap());
215 RCP<ThyLinOpBase> thyraPrecOp = Thyra::xpetraLinearOp<Scalar, LocalOrdinal, GlobalOrdinal, Node>(thyraRangeSpace, thyraDomainSpace, xpPrecOp);
218 defaultPrec->initializeUnspecified(thyraPrecOp);
221 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
222 void MueLuMaxwell1PreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
223 uninitializePrec(PreconditionerBase<Scalar>* prec, RCP<
const LinearOpSourceBase<Scalar>>* fwdOp, ESupportSolveUse* supportSolveUse)
const {
232 *fwdOp = Teuchos::null;
235 if (supportSolveUse) {
237 *supportSolveUse = Thyra::SUPPORT_SOLVE_UNSPECIFIED;
240 defaultPrec->uninitialize();
244 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
245 void MueLuMaxwell1PreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::setParameterList(RCP<ParameterList>
const& paramList) {
247 paramList_ = paramList;
250 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
251 RCP<ParameterList> MueLuMaxwell1PreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::getNonconstParameterList() {
255 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
256 RCP<ParameterList> MueLuMaxwell1PreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::unsetParameterList() {
257 RCP<ParameterList> savedParamList = paramList_;
258 paramList_ = Teuchos::null;
259 return savedParamList;
262 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
263 RCP<const ParameterList> MueLuMaxwell1PreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::getParameterList()
const {
267 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
268 RCP<const ParameterList> MueLuMaxwell1PreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::getValidParameters()
const {
269 static RCP<const ParameterList> validPL;
272 validPL =
rcp(
new ParameterList());
278 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
279 std::string MueLuMaxwell1PreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::description()
const {
280 return "Thyra::MueLuMaxwell1PreconditionerFactory";
284 #endif // HAVE_MUELU_STRATIMIKOS
286 #endif // ifdef THYRA_MUELU_MAXWELL1_PRECONDITIONER_FACTORY_DEF_HPP
void resetMatrix(Teuchos::RCP< Matrix > SM_Matrix_new, bool ComputePrec=true)
Reset system matrix.
bool nonnull(const std::shared_ptr< T > &p)
bool is_null(const std::shared_ptr< T > &p)
Preconditioner (wrapped as a Xpetra::Operator) for Maxwell's equations in curl-curl form...
static RCP< Time > getNewTimer(const std::string &name)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Concrete Thyra::LinearOpBase subclass for Xpetra::Operator.
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)