10 #ifndef MUELU_RAPSHIFTFACTORY_DEF_HPP
11 #define MUELU_RAPSHIFTFACTORY_DEF_HPP
20 #include <Xpetra_MatrixFactory2.hpp>
25 #include "MueLu_PerfUtils.hpp"
30 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
32 : implicitTranspose_(false) {}
35 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
39 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
48 #undef SET_VALID_ENTRY
50 validParamList->
set<
RCP<const FactoryBase> >(
"A", Teuchos::null,
"Generating factory of the matrix A used during the prolongator smoothing process");
51 validParamList->
set<
RCP<const FactoryBase> >(
"M", Teuchos::null,
"Generating factory of the matrix M used during the non-Galerkin RAP");
52 validParamList->
set<
RCP<const FactoryBase> >(
"Mdiag", Teuchos::null,
"Generating factory of the matrix Mdiag used during the non-Galerkin RAP");
53 validParamList->
set<
RCP<const FactoryBase> >(
"K", Teuchos::null,
"Generating factory of the matrix K used during the non-Galerkin RAP");
57 validParamList->
set<
bool>(
"CheckMainDiagonal",
false,
"Check main diagonal for zeros");
58 validParamList->
set<
bool>(
"RepairMainDiagonal",
false,
"Repair zeros on main diagonal");
62 validParamList->
set<
RCP<const FactoryBase> >(
"cfl-based shift array", Teuchos::null,
"MueLu-generated shift array for CFL-based shifting");
66 norecurse.disableRecursiveValidation();
67 validParamList->
set<
ParameterList>(
"matrixmatrix: kernel params", norecurse,
"MatrixMatrix kernel parameters");
69 return validParamList;
73 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
77 bool use_mdiag =
false;
79 use_mdiag = pL.
get<
bool>(
"rap: shift diagonal M");
82 bool use_low_storage =
false;
84 use_low_storage = pL.
get<
bool>(
"rap: shift low storage");
85 use_mdiag = use_low_storage ?
true : use_mdiag;
88 if (implicitTranspose_ ==
false) {
89 Input(coarseLevel,
"R");
93 Input(fineLevel,
"K");
95 Input(fineLevel,
"A");
96 Input(coarseLevel,
"P");
99 Input(fineLevel,
"M");
101 Input(fineLevel,
"Mdiag");
111 "deltaT was not provided by the user on level0!");
119 "cfl was not provided by the user on level0!");
122 Input(fineLevel,
"cfl-based shift array");
127 for (std::vector<
RCP<const FactoryBase> >::const_iterator it = transferFacts_.begin(); it != transferFacts_.end(); ++it) {
128 (*it)->CallDeclareInput(coarseLevel);
132 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
138 bool M_is_diagonal =
false;
140 M_is_diagonal = pL.
get<
bool>(
"rap: shift diagonal M");
143 bool use_low_storage =
false;
145 use_low_storage = pL.
get<
bool>(
"rap: shift low storage");
146 M_is_diagonal = use_low_storage ?
true : M_is_diagonal;
159 double dt = Get<double>(fineLevel,
"deltaT");
160 double cfl = Get<double>(fineLevel,
"cfl");
161 double ts_at_cfl1 = dt / cfl;
167 for (
int i = 1; i < (int)CFLs.
size(); i++)
168 myCFLs[i] = (CFLs[i] > cfl) ? cfl : CFLs[i];
171 std::ostringstream ofs;
172 ofs <<
"RAPShiftFactory: CFL schedule = ";
173 for (
int i = 0; i < (int)CFLs.
size(); i++)
174 ofs <<
" " << myCFLs[i];
177 GetOStream(
Statistics0) <<
"RAPShiftFactory: Timestep at CFL=1 is " << ts_at_cfl1 <<
" " << std::endl;
180 for (
int i = 0; i < (int)myshifts.
size(); i++)
181 myshifts[i] = 1.0 / (ts_at_cfl1 * myCFLs[i]);
182 doubleShifts = myshifts();
185 std::ostringstream ofs;
186 ofs <<
"RAPShiftFactory: shift schedule = ";
187 for (
int i = 0; i < (int)doubleShifts.
size(); i++)
188 ofs <<
" " << doubleShifts[i];
191 Set(coarseLevel,
"cfl-based shift array", myshifts);
193 myshifts = Get<Teuchos::ArrayRCP<double> >(fineLevel,
"cfl-based shift array");
194 doubleShifts = myshifts();
195 Set(coarseLevel,
"cfl-based shift array", myshifts);
207 K = Get<RCP<Matrix> >(fineLevel,
"A");
209 K = Get<RCP<Matrix> >(fineLevel,
"K");
211 M = Get<RCP<Matrix> >(fineLevel,
"M");
213 Mdiag = Get<RCP<Vector> >(fineLevel,
"Mdiag");
215 RCP<Matrix> P = Get<RCP<Matrix> >(coarseLevel,
"P");
230 if (!M_is_diagonal) {
234 MP->leftScale(*Mdiag);
237 Set(coarseLevel,
"AP Pattern", KP);
240 bool doOptimizedStorage =
true;
248 bool doFillComplete =
true;
249 if (implicitTranspose_) {
251 Kc =
Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*P,
true, *KP,
false, Kc, GetOStream(
Statistics2), doFillComplete, doOptimizedStorage);
252 Mc =
Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*P,
true, *MP,
false, Mc, GetOStream(
Statistics2), doFillComplete, doOptimizedStorage);
254 RCP<Matrix> R = Get<RCP<Matrix> >(coarseLevel,
"R");
256 Kc =
Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*R,
false, *KP,
false, Kc, GetOStream(
Statistics2), doFillComplete, doOptimizedStorage);
257 Mc =
Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*R,
false, *MP,
false, Mc, GetOStream(
Statistics2), doFillComplete, doOptimizedStorage);
264 int level = coarseLevel.GetLevelID();
266 if (!use_low_storage) {
268 if (level < (
int)shifts_.size())
269 shift = shifts_[level];
271 shift = Teuchos::as<Scalar>(pL.
get<
double>(
"rap: shift"));
274 if (level < (
int)shifts_.size()) {
276 shift = shifts_[level];
279 for (
int i = 1; i < level - 1; i++) {
282 shift = (prod1 * shifts_[level] - prod1);
284 }
else if (doubleShifts.
size() != 0) {
285 double d_shift = 0.0;
286 if (level < doubleShifts.
size())
287 d_shift = doubleShifts[level] - doubleShifts[level - 1];
290 GetOStream(
Warnings1) <<
"WARNING: RAPShiftFactory has detected a negative shift... This implies a less stable coarse grid." << std::endl;
291 shift = Teuchos::as<Scalar>(d_shift);
293 double base_shift = pL.
get<
double>(
"rap: shift");
295 shift = Teuchos::as<Scalar>(base_shift);
297 shift = Teuchos::as<Scalar>(pow(base_shift, level) - pow(base_shift, level - 1));
300 GetOStream(
Runtime0) <<
"RAPShiftFactory: Using shift " << shift << std::endl;
305 Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::TwoMatrixAdd(*Kc,
false,
Teuchos::ScalarTraits<Scalar>::one(), *Mc,
false, shift, Ac, GetOStream(
Statistics2));
310 if (relativeFloor.
size() > 0)
313 bool repairZeroDiagonals = pL.
get<
bool>(
"RepairMainDiagonal") || pL.
get<
bool>(
"rap: fix zero diagonals");
314 bool checkAc = pL.
get<
bool>(
"CheckMainDiagonal") || pL.
get<
bool>(
"rap: fix zero diagonals");
316 if (checkAc || repairZeroDiagonals)
321 params->
set(
"printLoadBalancingInfo",
true);
324 Set(coarseLevel,
"A", Ac);
326 if (!use_low_storage)
327 Set(coarseLevel,
"K", Kc);
329 if (!M_is_diagonal) {
330 Set(coarseLevel,
"M", Mc);
335 Mc->getLocalDiagCopy(*Mcv);
336 Set(coarseLevel,
"Mdiag", Mcv);
342 if (transferFacts_.begin() != transferFacts_.end()) {
346 for (std::vector<
RCP<const FactoryBase> >::const_iterator it = transferFacts_.begin(); it != transferFacts_.end(); ++it) {
348 GetOStream(
Runtime0) <<
"RAPShiftFactory: call transfer factory: " << fac->
description() << std::endl;
357 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
360 TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::rcp_dynamic_cast<const TwoLevelFactoryBase>(factory) == Teuchos::null,
Exceptions::BadCast,
"MueLu::RAPShiftFactory::AddTransferFactory: Transfer factory is not derived from TwoLevelFactoryBase. This is very strange. (Note: you can remove this exception if there's a good reason for)");
361 transferFacts_.push_back(factory);
366 #define MUELU_RAPSHIFTFACTORY_SHORT
367 #endif // MUELU_RAPSHIFTFACTORY_DEF_HPP
Exception indicating invalid cast attempted.
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
virtual void CallBuild(Level &requestedLevel) const =0
void Release(const FactoryBase &factory)
Decrement the storage counter for all the inputs of a factory.
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
One-liner description of what is happening.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
static void CheckRepairMainDiagonal(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> &Ac, bool const &repairZeroDiagonals, Teuchos::FancyOStream &fos, const typename Teuchos::ScalarTraits< Scalar >::magnitudeType threshold=Teuchos::ScalarTraits< typename Teuchos::ScalarTraits< Scalar >::magnitudeType >::zero())
static const NoFactory * get()
Print even more statistics.
void resize(const size_type n, const T &val=T())
bool isParameter(const std::string &name) const
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
Print statistics that do not involve significant additional computation.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultScalar Scalar
Class that holds all level-specific information.
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > BuildCopy(const RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > A)
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
static RCP< Vector > Build(const Teuchos::RCP< const Map > &map, bool zeroOut=true)
static void TwoMatrixAdd(const Matrix &A, bool transposeA, SC alpha, Matrix &B, SC beta)
void AddTransferFactory(const RCP< const FactoryBase > &factory)
Add transfer factory in the end of list of transfer factories in RepartitionAcFactory.
static void Multiply(const Matrix &A, bool transposeA, const Matrix &B, bool transposeB, Matrix &C, bool call_FillComplete_on_result=true, bool doOptimizeStorage=true, const std::string &label=std::string(), const RCP< ParameterList > ¶ms=null)
int GetLevelID() const
Return level number.
Exception throws to report errors in the internal logical of the program.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
virtual std::string description() const
Return a simple one-line description of this object.
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need's value has been saved.
#define SET_VALID_ENTRY(name)