46 #ifndef MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEF_HPP
47 #define MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEF_HPP
54 #include "MueLu_Aggregates.hpp"
55 #include "Xpetra_TpetraCrsMatrix.hpp"
62 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
66 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
68 #undef SET_VALID_ENTRY
72 "Generating factory of the matrix A");
74 "Aggregates generated by StructuredAggregationFactory used to construct a piece-constant prolongator.");
76 "Graph generated by StructuredAggregationFactory used to construct a piece-linear prolongator.");
78 "Fine level coordinates used to construct piece-wise linear prolongator and coarse level coordinates.");
80 "map of the coarse coordinates' GIDs as indexed on the fine mesh.");
82 "map of the coarse coordinates' GIDs as indexed on the coarse mesh.");
84 "Fine level nullspace used to construct the coarse level nullspace.");
86 "Number of spacial dimensions in the problem.");
88 "Number of nodes per spatial dimension on the coarse grid.");
90 "Interpolation order for constructing the prolongator.");
91 validParamList->
set<
bool>(
"keep coarse coords",
false,
"Flag to keep coordinates for special coarse grid solve");
92 validParamList->
set<
bool>(
"interp: remove small entries",
true,
"Remove small interpolation coeficient from prolongator to reduce fill-in on coarse level");
94 return validParamList;
97 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
102 Input(fineLevel,
"A");
103 Input(fineLevel,
"Nullspace");
104 Input(fineLevel,
"numDimensions");
105 Input(fineLevel,
"prolongatorGraph");
106 Input(fineLevel,
"lCoarseNodesPerDim");
107 Input(fineLevel,
"structuredInterpolationOrder");
109 if (pL.
get<
bool>(
"interp: build coarse coordinates") ||
110 Get<int>(fineLevel,
"structuredInterpolationOrder") == 1) {
111 Input(fineLevel,
"Coordinates");
112 Input(fineLevel,
"coarseCoordinatesFineMap");
113 Input(fineLevel,
"coarseCoordinatesMap");
117 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
120 return BuildP(fineLevel, coarseLevel);
123 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
130 if (
const char* dbg = std::getenv(
"MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
131 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
137 *out <<
"Starting GeometricInterpolationPFactory::BuildP." << std::endl;
141 const bool removeSmallEntries = pL.
get<
bool>(
"interp: remove small entries");
142 const bool buildCoarseCoordinates = pL.
get<
bool>(
"interp: build coarse coordinates");
143 const int interpolationOrder = Get<int>(fineLevel,
"structuredInterpolationOrder");
144 const int numDimensions = Get<int>(fineLevel,
"numDimensions");
154 if (buildCoarseCoordinates || (interpolationOrder == 1)) {
156 RCP<const Map> coarseCoordsFineMap = Get<RCP<const Map> >(fineLevel,
"coarseCoordinatesFineMap");
157 RCP<const Map> coarseCoordsMap = Get<RCP<const Map> >(fineLevel,
"coarseCoordinatesMap");
159 fineCoordinates = Get<RCP<realvaluedmultivector_type> >(fineLevel,
"Coordinates");
161 fineCoordinates->getNumVectors());
162 RCP<const Import> coordsImporter = ImportFactory::Build(fineCoordinates->getMap(),
163 coarseCoordsFineMap);
164 coarseCoordinates->doImport(*fineCoordinates, *coordsImporter,
Xpetra::INSERT);
165 coarseCoordinates->replaceMap(coarseCoordsMap);
167 Set(coarseLevel,
"Coordinates", coarseCoordinates);
169 if (pL.
get<
bool>(
"keep coarse coords")) {
174 *out <<
"Fine and coarse coordinates have been loaded from the fine level and set on the coarse level." << std::endl;
176 if (interpolationOrder == 0) {
179 BuildConstantP(P, prolongatorGraph, A);
180 }
else if (interpolationOrder == 1) {
185 const size_t dofsPerNode =
static_cast<size_t>(A->GetFixedBlockSize());
186 const size_t numColIndices = prolongatorColMap->getLocalNumElements();
189 "Something went wrong, the number of columns in the prolongator is not a multiple of dofsPerNode!");
190 const size_t numGhostCoords = numColIndices / dofsPerNode;
191 const GO indexBase = prolongatorColMap->getIndexBase();
192 const GO coordIndexBase = fineCoordinates->getMap()->getIndexBase();
195 Array<GO> ghostCoordIndices(numGhostCoords);
196 for (
size_t ghostCoordIdx = 0; ghostCoordIdx < numGhostCoords; ++ghostCoordIdx) {
197 ghostCoordIndices[ghostCoordIdx] = (prolongatorColIndices[ghostCoordIdx * dofsPerNode] - indexBase) / dofsPerNode + coordIndexBase;
199 RCP<Map> ghostCoordMap = MapFactory::Build(fineCoordinates->getMap()->lib(),
200 prolongatorColMap->getGlobalNumElements() / dofsPerNode,
203 fineCoordinates->getMap()->getComm());
206 fineCoordinates->getNumVectors());
207 RCP<const Import> ghostImporter = ImportFactory::Build(coarseCoordinates->getMap(),
209 ghostCoordinates->doImport(*coarseCoordinates, *ghostImporter,
Xpetra::INSERT);
211 BuildLinearP(coarseLevel, A, prolongatorGraph, fineCoordinates,
212 ghostCoordinates, numDimensions, removeSmallEntries, P);
215 *out <<
"The prolongator matrix has been built." << std::endl;
220 RCP<MultiVector> fineNullspace = Get<RCP<MultiVector> >(fineLevel,
"Nullspace");
221 RCP<MultiVector> coarseNullspace = MultiVectorFactory::Build(P->getDomainMap(),
222 fineNullspace->getNumVectors());
225 if (helpers::isTpetraBlockCrs(A)) {
235 Set(coarseLevel,
"Nullspace", coarseNullspace);
238 *out <<
"The coarse nullspace is constructed and set on the coarse level." << std::endl;
240 Set(coarseLevel,
"P", P);
242 *out <<
"GeometricInterpolationPFactory::BuildP has completed." << std::endl;
246 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
251 if (
const char* dbg = std::getenv(
"MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
252 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
258 *out <<
"BuildConstantP" << std::endl;
260 std::vector<size_t> strideInfo(1);
261 strideInfo[0] = A->GetFixedBlockSize();
263 StridedMapFactory::Build(prolongatorGraph->getDomainMap(), strideInfo);
265 *out <<
"Call prolongator constructor" << std::endl;
268 if (helpers::isTpetraBlockCrs(A)) {
271 LO NSDim = A->GetStorageBlockSize();
277 for (
LO i = 0, ct = 0; i < block_dofs.
size(); i++) {
278 for (
LO j = 0; j < NSDim; j++) {
279 point_dofs[ct] = block_dofs[i] * NSDim + j;
285 BlockMap->getGlobalNumElements() * NSDim,
287 BlockMap->getIndexBase(),
288 BlockMap->getComm());
289 strideInfo[0] = A->GetFixedBlockSize();
294 if (P_tpetra.is_null())
throw std::runtime_error(
"BuildConstantP Matrix factory did not return a Tpetra::BlockCrsMatrix");
301 for (
LO i = 0; i < NSDim; i++)
302 block[i * NSDim + i] = one;
303 for (
LO i = 0; i < (int)prolongatorGraph->getLocalNumRows(); i++) {
304 prolongatorGraph->getLocalRowView(i, indices);
305 for (
LO j = 0; j < (
LO)indices.
size(); j++) {
306 temp[0] = indices[j];
307 P_tpetra->replaceLocalValues(i, temp(), block());
312 if (A->IsView(
"stridedMaps") ==
true) {
313 P->CreateView(
"stridedMaps", A->getRowMap(
"stridedMaps"), stridedPointMap);
315 P->CreateView(
"stridedMaps", P->getRangeMap(), PointMap);
320 P =
rcp(
new CrsMatrixWrap(prolongatorGraph, dummyList));
321 RCP<CrsMatrix> PCrs = rcp_dynamic_cast<CrsMatrixWrap>(P)->getCrsMatrix();
322 PCrs->setAllToScalar(1.0);
323 PCrs->fillComplete();
326 if (A->IsView(
"stridedMaps") ==
true)
327 P->CreateView(
"stridedMaps", A->getRowMap(
"stridedMaps"), stridedDomainMap);
329 P->CreateView(
"stridedMaps", P->getRangeMap(), stridedDomainMap);
334 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
340 const int numDimensions,
const bool removeSmallEntries,
344 if (
const char* dbg = std::getenv(
"MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
345 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
352 const int numInterpolationPoints = 1 << numDimensions;
353 const int dofsPerNode = A->GetFixedBlockSize() / A->GetStorageBlockSize();
357 P =
rcp(
new CrsMatrixWrap(prolongatorGraph, dummyList));
358 RCP<CrsMatrix> PCrs = rcp_dynamic_cast<CrsMatrixWrap>(P)->getCrsMatrix();
362 *out <<
"Entering BuildLinearP" << std::endl;
366 const LO numFineNodes = fineCoordinates->getLocalLength();
367 const LO numGhostNodes = ghostCoordinates->getLocalLength();
370 const real_type realZero = Teuchos::as<real_type>(0.0);
373 for (
int dim = 0; dim < 3; ++dim) {
374 if (dim < numDimensions) {
375 fineCoords[dim] = fineCoordinates->getData(dim);
376 ghostCoords[dim] = ghostCoordinates->getData(dim);
378 fineCoords[dim] = fineZero;
379 ghostCoords[dim] = ghostZero;
383 *out <<
"Coordinates extracted from the multivectors!" << std::endl;
386 LO interpolationNodeIdx = 0, rowIdx = 0;
391 for (
LO nodeIdx = 0; nodeIdx < numFineNodes; ++nodeIdx) {
392 if (PCrs->getNumEntriesInLocalRow(nodeIdx * dofsPerNode) == 1) {
395 for (
LO dof = 0; dof < dofsPerNode; ++dof) {
396 rowIdx = nodeIdx * dofsPerNode + dof;
397 prolongatorGraph->getLocalRowView(rowIdx, colIndices);
398 PCrs->replaceLocalValues(rowIdx, colIndices, values());
404 for (
int dim = 0; dim < 3; ++dim) {
405 coords[0][dim] = fineCoords[dim][nodeIdx];
407 prolongatorGraph->getLocalRowView(nodeIdx * dofsPerNode, colIndices);
408 for (
int interpolationIdx = 0; interpolationIdx < numInterpolationPoints; ++interpolationIdx) {
409 coords[interpolationIdx + 1].
resize(3);
410 interpolationNodeIdx = colIndices[interpolationIdx] / dofsPerNode;
411 for (
int dim = 0; dim < 3; ++dim) {
412 coords[interpolationIdx + 1][dim] = ghostCoords[dim][interpolationNodeIdx];
416 ComputeLinearInterpolationStencil(numDimensions, numInterpolationPoints, coords, stencil);
418 values.
resize(numInterpolationPoints);
419 for (
LO valueIdx = 0; valueIdx < numInterpolationPoints; ++valueIdx) {
420 values[valueIdx] = Teuchos::as<SC>(stencil[valueIdx]);
424 for (
LO dof = 0; dof < dofsPerNode; ++dof) {
425 rowIdx = nodeIdx * dofsPerNode + dof;
426 prolongatorGraph->getLocalRowView(rowIdx, colIndices);
427 PCrs->replaceLocalValues(rowIdx, colIndices, values());
433 *out <<
"The calculation of the interpolation stencils has completed." << std::endl;
435 PCrs->fillComplete();
438 *out <<
"All values in P have been set and fillComplete has been performed." << std::endl;
446 if (removeSmallEntries) {
447 *out <<
"Entering remove small entries" << std::endl;
453 PCrs->getAllValues(rowptrOrig, colindOrig, valuesOrig);
455 const size_t numRows =
static_cast<size_t>(rowptrOrig.
size() - 1);
459 size_t countRemovedEntries = 0;
460 for (
size_t rowIdx = 0; rowIdx < numRows; ++rowIdx) {
461 for (
size_t entryIdx = rowptrOrig[rowIdx]; entryIdx < rowptrOrig[rowIdx + 1]; ++entryIdx) {
463 ++countRemovedEntries;
466 rowPtr[rowIdx + 1] = rowptrOrig[rowIdx + 1] - countRemovedEntries;
467 nnzOnRows[rowIdx] = rowPtr[rowIdx + 1] - rowPtr[rowIdx];
469 GetOStream(
Statistics1) <<
"interp: number of small entries removed= " << countRemovedEntries <<
" / " << rowptrOrig[numRows] << std::endl;
471 size_t countKeptEntries = 0;
474 for (
size_t entryIdx = 0; entryIdx < rowptrOrig[numRows]; ++entryIdx) {
476 colInd[countKeptEntries] = colindOrig[entryIdx];
477 values[countKeptEntries] = valuesOrig[entryIdx];
482 P =
rcp(
new CrsMatrixWrap(prolongatorGraph->getRowMap(),
483 prolongatorGraph->getColMap(),
485 RCP<CrsMatrix> PCrsSqueezed = rcp_dynamic_cast<CrsMatrixWrap>(P)->getCrsMatrix();
486 PCrsSqueezed->resumeFill();
487 PCrsSqueezed->setAllValues(rowPtr, colInd, values);
488 PCrsSqueezed->expertStaticFillComplete(prolongatorGraph->getDomainMap(),
489 prolongatorGraph->getRangeMap());
492 std::vector<size_t> strideInfo(1);
493 strideInfo[0] = dofsPerNode;
495 StridedMapFactory::Build(prolongatorGraph->getDomainMap(), strideInfo);
497 *out <<
"The strided maps of P have been computed" << std::endl;
500 if (A->IsView(
"stridedMaps") ==
true) {
501 P->CreateView(
"stridedMaps", A->getRowMap(
"stridedMaps"), stridedDomainMap);
503 P->CreateView(
"stridedMaps", P->getRangeMap(), stridedDomainMap);
508 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
533 int iter = 0, max_iter = 5;
534 real_type functions[4][8], norm_ref = 1.0, norm2 = 1.0, tol = 1.0e-5;
535 paramCoords.
size(numDimensions);
537 while ((iter < max_iter) && (norm2 > tol * norm_ref)) {
540 solutionDirection.
size(numDimensions);
541 residual.
size(numDimensions);
545 GetInterpolationFunctions(numDimensions, paramCoords, functions);
546 for (
LO i = 0; i < numDimensions; ++i) {
547 residual(i) = coord[0][i];
548 for (
LO k = 0; k < numInterpolationPoints; ++k) {
549 residual(i) -= functions[0][k] * coord[k + 1][i];
552 norm_ref += residual(i) * residual(i);
553 if (i == numDimensions - 1) {
554 norm_ref = std::sqrt(norm_ref);
558 for (
LO j = 0; j < numDimensions; ++j) {
559 for (
LO k = 0; k < numInterpolationPoints; ++k) {
560 Jacobian(i, j) += functions[j + 1][k] * coord[k + 1][i];
573 for (
LO i = 0; i < numDimensions; ++i) {
574 paramCoords(i) = paramCoords(i) + solutionDirection(i);
578 GetInterpolationFunctions(numDimensions, paramCoords, functions);
579 for (
LO i = 0; i < numDimensions; ++i) {
581 for (
LO k = 0; k < numInterpolationPoints; ++k) {
582 tmp -= functions[0][k] * coord[k + 1][i];
587 norm2 = std::sqrt(norm2);
591 for (
LO i = 0; i < numInterpolationPoints; ++i) {
592 stencil[i] = functions[0][i];
597 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
602 real_type xi = 0.0, eta = 0.0, zeta = 0.0, denominator = 0.0;
603 if (numDimensions == 1) {
604 xi = parametricCoordinates[0];
606 }
else if (numDimensions == 2) {
607 xi = parametricCoordinates[0];
608 eta = parametricCoordinates[1];
610 }
else if (numDimensions == 3) {
611 xi = parametricCoordinates[0];
612 eta = parametricCoordinates[1];
613 zeta = parametricCoordinates[2];
617 functions[0][0] = (1.0 - xi) * (1.0 - eta) * (1.0 - zeta) / denominator;
618 functions[0][1] = (1.0 + xi) * (1.0 - eta) * (1.0 - zeta) / denominator;
619 functions[0][2] = (1.0 - xi) * (1.0 + eta) * (1.0 - zeta) / denominator;
620 functions[0][3] = (1.0 + xi) * (1.0 + eta) * (1.0 - zeta) / denominator;
621 functions[0][4] = (1.0 - xi) * (1.0 - eta) * (1.0 + zeta) / denominator;
622 functions[0][5] = (1.0 + xi) * (1.0 - eta) * (1.0 + zeta) / denominator;
623 functions[0][6] = (1.0 - xi) * (1.0 + eta) * (1.0 + zeta) / denominator;
624 functions[0][7] = (1.0 + xi) * (1.0 + eta) * (1.0 + zeta) / denominator;
626 functions[1][0] = -(1.0 - eta) * (1.0 - zeta) / denominator;
627 functions[1][1] = (1.0 - eta) * (1.0 - zeta) / denominator;
628 functions[1][2] = -(1.0 + eta) * (1.0 - zeta) / denominator;
629 functions[1][3] = (1.0 + eta) * (1.0 - zeta) / denominator;
630 functions[1][4] = -(1.0 - eta) * (1.0 + zeta) / denominator;
631 functions[1][5] = (1.0 - eta) * (1.0 + zeta) / denominator;
632 functions[1][6] = -(1.0 + eta) * (1.0 + zeta) / denominator;
633 functions[1][7] = (1.0 + eta) * (1.0 + zeta) / denominator;
635 functions[2][0] = -(1.0 - xi) * (1.0 - zeta) / denominator;
636 functions[2][1] = -(1.0 + xi) * (1.0 - zeta) / denominator;
637 functions[2][2] = (1.0 - xi) * (1.0 - zeta) / denominator;
638 functions[2][3] = (1.0 + xi) * (1.0 - zeta) / denominator;
639 functions[2][4] = -(1.0 - xi) * (1.0 + zeta) / denominator;
640 functions[2][5] = -(1.0 + xi) * (1.0 + zeta) / denominator;
641 functions[2][6] = (1.0 - xi) * (1.0 + zeta) / denominator;
642 functions[2][7] = (1.0 + xi) * (1.0 + zeta) / denominator;
644 functions[3][0] = -(1.0 - xi) * (1.0 - eta) / denominator;
645 functions[3][1] = -(1.0 + xi) * (1.0 - eta) / denominator;
646 functions[3][2] = -(1.0 - xi) * (1.0 + eta) / denominator;
647 functions[3][3] = -(1.0 + xi) * (1.0 + eta) / denominator;
648 functions[3][4] = (1.0 - xi) * (1.0 - eta) / denominator;
649 functions[3][5] = (1.0 + xi) * (1.0 - eta) / denominator;
650 functions[3][6] = (1.0 - xi) * (1.0 + eta) / denominator;
651 functions[3][7] = (1.0 + xi) * (1.0 + eta) / denominator;
657 #endif // MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEF_HPP
static Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Build(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node >> &map, size_t NumVectors, bool zeroOut=true)
basic_FancyOStream & setShowProcRank(const bool showProcRank)
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void BuildLinearP(Level &coarseLevel, RCP< Matrix > &A, RCP< const CrsGraph > &prolongatorGraph, RCP< realvaluedmultivector_type > &fineCoordinates, RCP< realvaluedmultivector_type > &ghostCoordinates, const int numDimensions, const bool keepD2, RCP< Matrix > &P) const
static const NoFactory * get()
void GetInterpolationFunctions(const LO numDimensions, const Teuchos::SerialDenseVector< LO, real_type > parametricCoordinates, real_type functions[4][8]) const
static RCP< Time > getNewTimer(const std::string &name)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
Class that holds all level-specific information.
void ComputeLinearInterpolationStencil(const int numDimensions, const int numInterpolationPoints, const Array< Array< real_type > > coord, Array< real_type > &stencil) const
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
void factorWithEquilibration(bool flag)
#define SET_VALID_ENTRY(name)
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)
basic_FancyOStream & setShowAllFrontMatter(const bool showAllFrontMatter)
void resize(size_type new_size, const value_type &x=value_type())
void Set(const std::string &ename, const T &entry, const FactoryBase *factory=NoFactory::get())
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
int size(OrdinalType length_in)
int setVectors(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &X, const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &B)
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
void residual(const Operator< SC, LO, GO, NO > &Aop, const MultiVector< SC, LO, GO, NO > &X_in, const MultiVector< SC, LO, GO, NO > &B_in, MultiVector< SC, LO, GO, NO > &R_in)
Exception throws to report errors in the internal logical of the program.
void BuildConstantP(RCP< Matrix > &P, RCP< const CrsGraph > &prolongatorGraph, RCP< Matrix > &A) const
typename Teuchos::ScalarTraits< SC >::coordinateType real_type
void BuildP(Level &fineLevel, Level &coarseLevel) const
Abstract Build method.
int setMatrix(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &A)