38 RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& P,
39 Teuchos::FancyOStream& out) {
40 using MagnitudeType =
typename Teuchos::ScalarTraits<Scalar>::magnitudeType;
42 AP = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A,
false, *P,
false, AP, out,
true,
true);
44 PtAP = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*P,
true, *AP,
false, PtAP, out,
true,
true);
45 RCP<Vector> diag = Xpetra::VectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(PtAP->getRowMap());
46 PtAP->getLocalDiagCopy(*diag);
47 MagnitudeType norm = diag->norm1();
48 norm = Teuchos::ScalarTraits<MagnitudeType>::squareroot(norm);
54 RCP<ParameterList> validParamList = rcp(
new ParameterList());
56#define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
61 validParamList->getEntry(
"emin: iterative method").setValidator(rcp(
new Teuchos::StringValidator(Teuchos::tuple<std::string>(
"cg",
"sd",
"gmres"))));
65 validParamList->set<RCP<const FactoryBase>>(
"A", Teuchos::null,
"Generating factory for the matrix A used during internal iterations");
66 validParamList->set<RCP<const FactoryBase>>(
"P", Teuchos::null,
"Generating factory for the initial guess");
67 validParamList->set<RCP<const FactoryBase>>(
"Constraint", Teuchos::null,
"Generating factory for constraints");
69 validParamList->set<RCP<Matrix>>(
"P0", Teuchos::null,
"Initial guess at P");
70 validParamList->set<
bool>(
"Keep P0",
false,
"Keep an initial P0 (for reuse)");
72 validParamList->set<RCP<Constraint>>(
"Constraint0", Teuchos::null,
"Initial Constraint");
73 validParamList->set<
bool>(
"Keep Constraint0",
false,
"Keep an initial Constraint (for reuse)");
75 return validParamList;
122 const ParameterList& pL = GetParameterList();
125 RCP<Matrix> A = Get<RCP<Matrix>>(fineLevel,
"A");
127 if (restrictionMode_) {
135 if (coarseLevel.
IsAvailable(
"Constraint0",
this)) {
137 X = coarseLevel.
Get<RCP<Constraint>>(
"Constraint0",
this);
138 GetOStream(
Runtime0) <<
"Reusing Constraint0" << std::endl;
142 X = Get<RCP<Constraint>>(coarseLevel,
"Constraint");
151#ifdef externalSuppliedP0
157#ifdef externalSuppliedP0
158 P0 = coarseLevel.
Get<RCP<Matrix>>(
"P0");
160 P0 = coarseLevel.
Get<RCP<Matrix>>(
"P0",
this);
162 numIts = pL.get<
int>(
"emin: num reuse iterations");
163 GetOStream(
Runtime0) <<
"Reusing P0" << std::endl;
166 GetOStream(
Runtime0) <<
"Getting P from coarseLevel" << std::endl;
168 P0 = Get<RCP<Matrix>>(coarseLevel,
"P");
169 numIts = pL.get<
int>(
"emin: num iterations");
174 GetOStream(
Statistics1) <<
"Energy norm of P0: " << ComputeProlongatorEnergyNorm(A, P0, GetOStream(
Statistics0)) << std::endl;
175 GetOStream(
Statistics1) <<
"Constraint residual norm of P0: " << X->ResidualNorm(P0) << std::endl;
178 std::string solverType = pL.get<std::string>(
"emin: iterative method");
179 if (solverType ==
"cg")
180 solverType =
"Pseudo Block CG";
181 else if (solverType ==
"gmres")
182 solverType =
"Pseudo Block GMRES";
185#ifdef HAVE_MUELU_BELOS
189 auto invD = MatrixFactory::Build(invDiagonal);
197 std::vector<RCP<Operator>> ops = {X, flatA};
201 auto B = MultiVectorFactory::Build(projectedFlatA->getRangeMap(), 1);
202 auto vecP = MultiVectorFactory::Build(projectedFlatA->getDomainMap(), 1);
204 X->AssignMatrixEntriesToVector(*P0, *vecP);
206 auto belosProjectedFlatA = rcp(
new Belos::XpetraOp<Scalar, LocalOrdinal, GlobalOrdinal, Node>(projectedFlatA));
207 auto belosFlatInvDiag = rcp(
new Belos::XpetraOp<Scalar, LocalOrdinal, GlobalOrdinal, Node>(flatInvD));
210 using MV = Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
211 using OP = Belos::OperatorT<MV>;
213 auto problem = rcp(
new Belos::LinearProblem<Scalar, MV, OP>());
214 problem->setOperator(belosProjectedFlatA);
215 problem->setRightPrec(belosFlatInvDiag);
216 problem->setLHS(vecP);
218 problem->setLabel(
"Emin");
219 TEUCHOS_ASSERT(problem->setProblem());
221 auto belosList = rcp(
new Teuchos::ParameterList());
222 belosList->set(
"Timer Label",
"Emin");
223 belosList->set(
"Maximum Iterations", numIts);
224 belosList->set(
"Implicit Residual Scaling",
"None");
225 belosList->set(
"Verbosity", Belos::Errors + Belos::Warnings + Belos::StatusTestDetails);
226 belosList->set(
"Output Frequency", 1);
227 belosList->set(
"Output Style", Belos::Brief);
228 auto out = GetMueLuOStream();
229 belosList->set(
"Output Stream", out->getOStream());
231 Belos::SolverFactory<Scalar, MV, OP> solverFactory;
232 auto solver = solverFactory.create(solverType, belosList);
233 solver->setProblem(problem);
235 if (numIts > 0) solver->solve();
238 P = X->GetMatrixWithEntriesFromVector(*vecP);
240 P = Xpetra::MatrixFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildCopy(P0);
242 if (P0->IsView(
"stridedMaps"))
243 P->CreateView(
"stridedMaps", P0);
245 TEUCHOS_TEST_FOR_EXCEPTION(
true,
Exceptions::RuntimeError,
"Energy minimization multigrid requires Belos to be enabled.");
251 GetOStream(
Statistics1) <<
"Energy norm of P: " << ComputeProlongatorEnergyNorm(A, P, GetOStream(
Statistics0)) << std::endl;
252 GetOStream(
Statistics1) <<
"Norm of constraint residual: " << X->ResidualNorm(P) << std::endl;
256 if (!P->IsView(
"stridedMaps")) {
257 if (A->IsView(
"stridedMaps") ==
true) {
258 GetOStream(
Runtime1) <<
"Using A to fillComplete P" << std::endl;
264 std::vector<size_t> stridingInfo(1, 1);
265 RCP<const StridedMap> dMap = StridedMapFactory::Build(X->GetPattern()->getDomainMap(), stridingInfo);
267 P->CreateView(
"stridedMaps", A->getRowMap(
"stridedMaps"), dMap);
270 P->CreateView(
"stridedMaps", P->getRangeMap(), P->getDomainMap());
275 if (!restrictionMode_) {
277 Set(coarseLevel,
"P", P);
279 if (pL.get<
bool>(
"Keep P0")) {
283 coarseLevel.
Keep(
"P0",
this);
284 Set(coarseLevel,
"P0", P);
286 if (pL.get<
bool>(
"Keep Constraint0")) {
290 coarseLevel.
Keep(
"Constraint0",
this);
291 Set(coarseLevel,
"Constraint0", X);
295 RCP<ParameterList> params = rcp(
new ParameterList());
296 params->set(
"printLoadBalancingInfo",
true);
297 params->set(
"printCommInfo",
true);
310 Set(coarseLevel,
"R", R);
313 RCP<ParameterList> params = rcp(
new ParameterList());
314 params->set(
"printLoadBalancingInfo",
true);
315 params->set(
"printCommInfo",
true);
static Teuchos::ScalarTraits< Scalar >::magnitudeType ComputeProlongatorEnergyNorm(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A, RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &P, Teuchos::FancyOStream &out)
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Transpose(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, bool optimizeTranspose=false, const std::string &label=std::string(), const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)