136 const Teuchos::ParameterList &pL = GetParameterList();
138 bool M_is_diagonal =
false;
139 if (pL.isParameter(
"rap: shift diagonal M"))
140 M_is_diagonal = pL.get<
bool>(
"rap: shift diagonal M");
143 bool use_low_storage =
false;
144 if (pL.isParameter(
"rap: shift low storage")) {
145 use_low_storage = pL.get<
bool>(
"rap: shift low storage");
146 M_is_diagonal = use_low_storage ? true : M_is_diagonal;
149 Teuchos::ArrayView<const double> doubleShifts;
150 Teuchos::ArrayRCP<double> myshifts;
151 if (pL.isParameter(
"rap: shift array") && pL.get<Teuchos::Array<double> >(
"rap: shift array").size() > 0) {
153 doubleShifts = pL.get<Teuchos::Array<double> >(
"rap: shift array")();
155 if (pL.isParameter(
"rap: cfl array") && pL.get<Teuchos::Array<double> >(
"rap: cfl array").size() > 0) {
157 Teuchos::ArrayView<const double> CFLs = pL.get<Teuchos::Array<double> >(
"rap: cfl array")();
159 double dt = Get<double>(fineLevel,
"deltaT");
160 double cfl = Get<double>(fineLevel,
"cfl");
161 double ts_at_cfl1 = dt / cfl;
162 myshifts.resize(CFLs.size());
163 Teuchos::Array<double> myCFLs(CFLs.size());
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");
229 KP = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*K,
false, *P,
false, KP, GetOStream(
Statistics2));
230 if (!M_is_diagonal) {
231 MP = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*M,
false, *P,
false, MP, GetOStream(
Statistics2));
233 MP = Xpetra::MatrixFactory2<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildCopy(P);
234 MP->leftScale(*Mdiag);
237 Set(coarseLevel,
"AP Pattern", KP);
240 bool doOptimizedStorage =
true;
242 RCP<Matrix> Ac, Kc, Mc;
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();
265 Scalar shift = Teuchos::ScalarTraits<Scalar>::zero();
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];
278 Scalar prod1 = Teuchos::ScalarTraits<Scalar>::one();
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));
309 Teuchos::ArrayView<const double> relativeFloor = pL.get<Teuchos::Array<double> >(
"rap: relative diagonal floor")();
310 if (relativeFloor.size() > 0)
311 Xpetra::MatrixUtils<SC, LO, GO, NO>::RelativeDiagonalBoost(Ac, relativeFloor, GetOStream(
Statistics2));
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)
317 Xpetra::MatrixUtils<SC, LO, GO, NO>::CheckRepairMainDiagonal(Ac, repairZeroDiagonals, GetOStream(
Warnings1));
319 RCP<ParameterList> params = rcp(
new ParameterList());
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);
334 RCP<Vector> Mcv = Xpetra::VectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(Mc->getRowMap(),
false);
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) {
347 RCP<const FactoryBase> fac = *it;
348 GetOStream(
Runtime0) <<
"RAPShiftFactory: call transfer factory: " << fac->description() << std::endl;
349 fac->CallBuild(coarseLevel);