87 RCP<Teuchos::FancyOStream> out;
88 if (
const char* dbg = std::getenv(
"MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
89 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
90 out->setShowAllFrontMatter(
false).setShowProcRank(
true);
92 out = Teuchos::getFancyOStream(rcp(
new Teuchos::oblackholestream()));
95 *out <<
"Starting GeometricInterpolationPFactory_kokkos::BuildP." << std::endl;
98 const ParameterList& pL = GetParameterList();
99 const bool buildCoarseCoordinates = pL.get<
bool>(
"interp: build coarse coordinates");
100 const int interpolationOrder = Get<int>(fineLevel,
"structuredInterpolationOrder");
101 const int numDimensions = Get<int>(fineLevel,
"numDimensions");
104 RCP<Matrix> A = Get<RCP<Matrix> >(fineLevel,
"A");
105 RCP<const CrsGraph> prolongatorGraph = Get<RCP<CrsGraph> >(fineLevel,
"prolongatorGraph");
106 RCP<realvaluedmultivector_type> fineCoordinates, coarseCoordinates;
111 if (buildCoarseCoordinates || (interpolationOrder == 1)) {
115 RCP<IndexManager_kokkos> geoData = Get<RCP<IndexManager_kokkos> >(fineLevel,
"indexManager");
116 fineCoordinates = Get<RCP<realvaluedmultivector_type> >(fineLevel,
"Coordinates");
119 RCP<const Map> coarseCoordsMap = MapFactory::Build(fineCoordinates->getMap()->lib(),
120 Teuchos::OrdinalTraits<GO>::invalid(),
121 geoData->getNumCoarseNodes(),
122 fineCoordinates->getMap()->getIndexBase(),
123 fineCoordinates->getMap()->getComm());
124 coarseCoordinates = Xpetra::MultiVectorFactory<real_type, LO, GO, Node>::
125 Build(coarseCoordsMap, fineCoordinates->getNumVectors());
130 fineCoordinates->getLocalViewDevice(Tpetra::Access::ReadWrite),
131 coarseCoordinates->getLocalViewDevice(Tpetra::Access::OverwriteAll));
132 Kokkos::parallel_for(
"GeometricInterpolation: build coarse coordinates",
133 Kokkos::RangePolicy<execution_space>(0, geoData->getNumCoarseNodes()),
134 myCoarseCoordinatesBuilder);
136 Set(coarseLevel,
"Coordinates", coarseCoordinates);
139 *out <<
"Fine and coarse coordinates have been loaded from the fine level and set on the coarse level." << std::endl;
141 if (interpolationOrder == 0) {
144 BuildConstantP(P, prolongatorGraph, A);
145 }
else if (interpolationOrder == 1) {
148 RCP<realvaluedmultivector_type> ghostCoordinates = Xpetra::MultiVectorFactory<real_type, LO, GO, NO>::Build(prolongatorGraph->getColMap(),
149 fineCoordinates->getNumVectors());
150 RCP<const Import> ghostImporter = ImportFactory::Build(coarseCoordinates->getMap(),
151 prolongatorGraph->getColMap());
152 ghostCoordinates->doImport(*coarseCoordinates, *ghostImporter, Xpetra::INSERT);
155 BuildLinearP(A, prolongatorGraph, fineCoordinates, ghostCoordinates, numDimensions, P);
158 *out <<
"The prolongator matrix has been built." << std::endl;
163 RCP<MultiVector> fineNullspace = Get<RCP<MultiVector> >(fineLevel,
"Nullspace");
164 RCP<MultiVector> coarseNullspace = MultiVectorFactory::Build(P->getDomainMap(),
165 fineNullspace->getNumVectors());
166 P->apply(*fineNullspace, *coarseNullspace, Teuchos::TRANS, Teuchos::ScalarTraits<SC>::one(),
167 Teuchos::ScalarTraits<SC>::zero());
168 Set(coarseLevel,
"Nullspace", coarseNullspace);
171 *out <<
"The coarse nullspace is constructed and set on the coarse level." << std::endl;
173 Array<LO> lNodesPerDir = Get<Array<LO> >(fineLevel,
"lCoarseNodesPerDim");
174 Set(coarseLevel,
"numDimensions", numDimensions);
175 Set(coarseLevel,
"lNodesPerDim", lNodesPerDir);
176 Set(coarseLevel,
"P", P);
178 *out <<
"GeometricInterpolationPFactory_kokkos::BuildP has completed." << std::endl;
184 BuildConstantP(RCP<Matrix>& P, RCP<const CrsGraph>& prolongatorGraph, RCP<Matrix>& A)
const {
186 RCP<Teuchos::FancyOStream> out;
187 if (
const char* dbg = std::getenv(
"MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
188 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
189 out->setShowAllFrontMatter(
false).setShowProcRank(
true);
191 out = Teuchos::getFancyOStream(rcp(
new Teuchos::oblackholestream()));
194 *out <<
"BuildConstantP" << std::endl;
196 std::vector<size_t> strideInfo(1);
197 strideInfo[0] = A->GetFixedBlockSize();
198 RCP<const StridedMap> stridedDomainMap =
199 StridedMapFactory::Build(prolongatorGraph->getDomainMap(), strideInfo);
201 *out <<
"Call prolongator constructor" << std::endl;
202 using helpers = Xpetra::Helpers<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
203 if (helpers::isTpetraBlockCrs(A)) {
204 LO NSDim = A->GetStorageBlockSize();
208 RCP<const Map> BlockMap = prolongatorGraph->getDomainMap();
209 Teuchos::ArrayView<const GO> block_dofs = BlockMap->getLocalElementList();
210 Teuchos::Array<GO> point_dofs(block_dofs.size() * NSDim);
211 for (LO i = 0, ct = 0; i < block_dofs.size(); i++) {
212 for (LO j = 0; j < NSDim; j++) {
213 point_dofs[ct] = block_dofs[i] * NSDim + j;
218 RCP<const Map> PointMap = MapFactory::Build(BlockMap->lib(),
219 BlockMap->getGlobalNumElements() * NSDim,
221 BlockMap->getIndexBase(),
222 BlockMap->getComm());
223 strideInfo[0] = A->GetFixedBlockSize();
224 RCP<const StridedMap> stridedPointMap = StridedMapFactory::Build(PointMap, strideInfo);
226 RCP<Xpetra::CrsMatrix<SC, LO, GO, NO> > P_xpetra = Xpetra::CrsMatrixFactory<SC, LO, GO, NO>::BuildBlock(prolongatorGraph, PointMap, A->getRangeMap(), NSDim);
227 RCP<Xpetra::TpetraBlockCrsMatrix<SC, LO, GO, NO> > P_tpetra = rcp_dynamic_cast<Xpetra::TpetraBlockCrsMatrix<SC, LO, GO, NO> >(P_xpetra);
228 if (P_tpetra.is_null())
throw std::runtime_error(
"BuildConstantP: Matrix factory did not return a Tpetra::BlockCrsMatrix");
229 RCP<CrsMatrixWrap> P_wrap = rcp(
new CrsMatrixWrap(P_xpetra));
231 const LO stride = strideInfo[0] * strideInfo[0];
232 const LO in_stride = strideInfo[0];
233 typename CrsMatrix::local_graph_device_type localGraph = prolongatorGraph->getLocalGraphDevice();
234 auto rowptr = localGraph.row_map;
235 auto indices = localGraph.entries;
236 auto values = P_tpetra->getTpetra_BlockCrsMatrix()->getValuesDeviceNonConst();
238 using ISC =
typename Tpetra::BlockCrsMatrix<SC, LO, GO, NO>::impl_scalar_type;
239 ISC one = Teuchos::ScalarTraits<ISC>::one();
241 const Kokkos::TeamPolicy<execution_space> policy(prolongatorGraph->getLocalNumRows(), 1);
243 Kokkos::parallel_for(
244 "MueLu:GeoInterpFact::BuildConstantP::fill", policy,
245 KOKKOS_LAMBDA(
const typename Kokkos::TeamPolicy<execution_space>::member_type& thread) {
246 auto row = thread.league_rank();
247 for (LO j = (LO)rowptr[row]; j < (LO)rowptr[row + 1]; j++) {
248 LO block_offset = j * stride;
249 for (LO k = 0; k < in_stride; k++)
250 values[block_offset + k * (in_stride + 1)] = one;
255 if (A->IsView(
"stridedMaps") ==
true) {
256 P->CreateView(
"stridedMaps", A->getRowMap(
"stridedMaps"), stridedPointMap);
258 P->CreateView(
"stridedMaps", P->getRangeMap(), PointMap);
262 RCP<ParameterList> dummyList = rcp(
new ParameterList());
263 P = rcp(
new CrsMatrixWrap(prolongatorGraph, dummyList));
264 RCP<CrsMatrix> PCrs = toCrsMatrix(P);
265 PCrs->setAllToScalar(1.0);
266 PCrs->fillComplete();
269 if (A->IsView(
"stridedMaps") ==
true) {
270 P->CreateView(
"stridedMaps", A->getRowMap(
"stridedMaps"), stridedDomainMap);
272 P->CreateView(
"stridedMaps", P->getRangeMap(), stridedDomainMap);
280 BuildLinearP(RCP<Matrix>& A, RCP<const CrsGraph>& prolongatorGraph,
281 RCP<realvaluedmultivector_type>& fineCoordinates,
282 RCP<realvaluedmultivector_type>& ghostCoordinates,
283 const int numDimensions, RCP<Matrix>& P)
const {
285 RCP<Teuchos::FancyOStream> out;
286 if (
const char* dbg = std::getenv(
"MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
287 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
288 out->setShowAllFrontMatter(
false).setShowProcRank(
true);
290 out = Teuchos::getFancyOStream(rcp(
new Teuchos::oblackholestream()));
293 *out <<
"Entering BuildLinearP" << std::endl;
296 const LO numFineNodes = fineCoordinates->getLocalLength();
297 const LO numGhostNodes = ghostCoordinates->getLocalLength();
298 Array<ArrayRCP<const real_type> > fineCoords(3);
299 Array<ArrayRCP<const real_type> > ghostCoords(3);
300 const real_type realZero = Teuchos::as<real_type>(0.0);
301 ArrayRCP<real_type> fineZero(numFineNodes, realZero);
302 ArrayRCP<real_type> ghostZero(numGhostNodes, realZero);
303 for (
int dim = 0; dim < 3; ++dim) {
304 if (dim < numDimensions) {
305 fineCoords[dim] = fineCoordinates->getData(dim);
306 ghostCoords[dim] = ghostCoordinates->getData(dim);
308 fineCoords[dim] = fineZero;
309 ghostCoords[dim] = ghostZero;
313 *out <<
"Coordinates extracted from the multivectors!" << std::endl;
316 const int numInterpolationPoints = 1 << numDimensions;
317 const int dofsPerNode = A->GetFixedBlockSize();
319 std::vector<size_t> strideInfo(1);
320 strideInfo[0] = dofsPerNode;
321 RCP<const StridedMap> stridedDomainMap =
322 StridedMapFactory::Build(prolongatorGraph->getDomainMap(), strideInfo);
324 *out <<
"The maps of P have been computed" << std::endl;
326 RCP<ParameterList> dummyList = rcp(
new ParameterList());
327 P = rcp(
new CrsMatrixWrap(prolongatorGraph, dummyList));
328 RCP<CrsMatrix> PCrs = toCrsMatrix(P);
331 LO interpolationNodeIdx = 0, rowIdx = 0;
332 ArrayView<const LO> colIndices;
334 Array<Array<real_type> > coords(numInterpolationPoints + 1);
335 Array<real_type> stencil(numInterpolationPoints);
336 for (LO nodeIdx = 0; nodeIdx < numFineNodes; ++nodeIdx) {
337 if (PCrs->getNumEntriesInLocalRow(nodeIdx * dofsPerNode) == 1) {
340 for (LO dof = 0; dof < dofsPerNode; ++dof) {
341 rowIdx = nodeIdx * dofsPerNode + dof;
342 prolongatorGraph->getLocalRowView(rowIdx, colIndices);
343 PCrs->replaceLocalValues(rowIdx, colIndices, values());
349 for (
int dim = 0; dim < 3; ++dim) {
350 coords[0][dim] = fineCoords[dim][nodeIdx];
352 prolongatorGraph->getLocalRowView(nodeIdx * dofsPerNode, colIndices);
353 for (
int interpolationIdx = 0; interpolationIdx < numInterpolationPoints; ++interpolationIdx) {
354 coords[interpolationIdx + 1].resize(3);
355 interpolationNodeIdx = colIndices[interpolationIdx] / dofsPerNode;
356 for (
int dim = 0; dim < 3; ++dim) {
357 coords[interpolationIdx + 1][dim] = ghostCoords[dim][interpolationNodeIdx];
360 ComputeLinearInterpolationStencil(numDimensions, numInterpolationPoints, coords, stencil);
361 values.resize(numInterpolationPoints);
362 for (LO valueIdx = 0; valueIdx < numInterpolationPoints; ++valueIdx) {
363 values[valueIdx] = Teuchos::as<SC>(stencil[valueIdx]);
367 for (LO dof = 0; dof < dofsPerNode; ++dof) {
368 rowIdx = nodeIdx * dofsPerNode + dof;
369 prolongatorGraph->getLocalRowView(rowIdx, colIndices);
370 PCrs->replaceLocalValues(rowIdx, colIndices, values());
375 *out <<
"The calculation of the interpolation stencils has completed." << std::endl;
377 PCrs->fillComplete();
379 *out <<
"All values in P have been set and expertStaticFillComplete has been performed." << std::endl;
382 if (A->IsView(
"stridedMaps") ==
true) {
383 P->CreateView(
"stridedMaps", A->getRowMap(
"stridedMaps"), stridedDomainMap);
385 P->CreateView(
"stridedMaps", P->getRangeMap(), stridedDomainMap);
393 const Array<Array<real_type> > coord,
394 Array<real_type>& stencil)
const {
410 Teuchos::SerialDenseMatrix<LO, real_type> Jacobian(numDimensions, numDimensions);
411 Teuchos::SerialDenseVector<LO, real_type> residual(numDimensions);
412 Teuchos::SerialDenseVector<LO, real_type> solutionDirection(numDimensions);
413 Teuchos::SerialDenseVector<LO, real_type> paramCoords(numDimensions);
414 Teuchos::SerialDenseSolver<LO, real_type> problem;
415 int iter = 0, max_iter = 5;
416 real_type functions[4][8], norm_ref = 1.0, norm2 = 1.0, tol = 1.0e-5;
417 paramCoords.size(numDimensions);
419 while ((iter < max_iter) && (norm2 > tol * norm_ref)) {
422 solutionDirection.size(numDimensions);
423 residual.size(numDimensions);
427 GetInterpolationFunctions(numDimensions, paramCoords, functions);
428 for (LO i = 0; i < numDimensions; ++i) {
429 residual(i) = coord[0][i];
430 for (LO k = 0; k < numInterpolationPoints; ++k) {
431 residual(i) -= functions[0][k] * coord[k + 1][i];
434 norm_ref += residual(i) * residual(i);
435 if (i == numDimensions - 1) {
436 norm_ref = std::sqrt(norm_ref);
440 for (LO j = 0; j < numDimensions; ++j) {
441 for (LO k = 0; k < numInterpolationPoints; ++k) {
442 Jacobian(i, j) += functions[j + 1][k] * coord[k + 1][i];
448 problem.setMatrix(Teuchos::rcp(&Jacobian,
false));
449 problem.setVectors(Teuchos::rcp(&solutionDirection,
false), Teuchos::rcp(&residual,
false));
450 if (problem.shouldEquilibrate()) {
451 problem.factorWithEquilibration(
true);
455 for (LO i = 0; i < numDimensions; ++i) {
456 paramCoords(i) = paramCoords(i) + solutionDirection(i);
460 GetInterpolationFunctions(numDimensions, paramCoords, functions);
461 for (LO i = 0; i < numDimensions; ++i) {
463 for (LO k = 0; k < numInterpolationPoints; ++k) {
464 tmp -= functions[0][k] * coord[k + 1][i];
469 norm2 = std::sqrt(norm2);
473 for (LO i = 0; i < numInterpolationPoints; ++i) {
474 stencil[i] = functions[0][i];
482 const Teuchos::SerialDenseVector<LO, real_type> parametricCoordinates,
484 real_type xi = 0.0, eta = 0.0, zeta = 0.0, denominator = 0.0;
485 if (numDimensions == 1) {
486 xi = parametricCoordinates[0];
488 }
else if (numDimensions == 2) {
489 xi = parametricCoordinates[0];
490 eta = parametricCoordinates[1];
492 }
else if (numDimensions == 3) {
493 xi = parametricCoordinates[0];
494 eta = parametricCoordinates[1];
495 zeta = parametricCoordinates[2];
499 functions[0][0] = (1.0 - xi) * (1.0 - eta) * (1.0 - zeta) / denominator;
500 functions[0][1] = (1.0 + xi) * (1.0 - eta) * (1.0 - zeta) / denominator;
501 functions[0][2] = (1.0 - xi) * (1.0 + eta) * (1.0 - zeta) / denominator;
502 functions[0][3] = (1.0 + xi) * (1.0 + eta) * (1.0 - zeta) / denominator;
503 functions[0][4] = (1.0 - xi) * (1.0 - eta) * (1.0 + zeta) / denominator;
504 functions[0][5] = (1.0 + xi) * (1.0 - eta) * (1.0 + zeta) / denominator;
505 functions[0][6] = (1.0 - xi) * (1.0 + eta) * (1.0 + zeta) / denominator;
506 functions[0][7] = (1.0 + xi) * (1.0 + eta) * (1.0 + zeta) / denominator;
508 functions[1][0] = -(1.0 - eta) * (1.0 - zeta) / denominator;
509 functions[1][1] = (1.0 - eta) * (1.0 - zeta) / denominator;
510 functions[1][2] = -(1.0 + eta) * (1.0 - zeta) / denominator;
511 functions[1][3] = (1.0 + eta) * (1.0 - zeta) / denominator;
512 functions[1][4] = -(1.0 - eta) * (1.0 + zeta) / denominator;
513 functions[1][5] = (1.0 - eta) * (1.0 + zeta) / denominator;
514 functions[1][6] = -(1.0 + eta) * (1.0 + zeta) / denominator;
515 functions[1][7] = (1.0 + eta) * (1.0 + zeta) / denominator;
517 functions[2][0] = -(1.0 - xi) * (1.0 - zeta) / denominator;
518 functions[2][1] = -(1.0 + xi) * (1.0 - zeta) / denominator;
519 functions[2][2] = (1.0 - xi) * (1.0 - zeta) / denominator;
520 functions[2][3] = (1.0 + xi) * (1.0 - zeta) / denominator;
521 functions[2][4] = -(1.0 - xi) * (1.0 + zeta) / denominator;
522 functions[2][5] = -(1.0 + xi) * (1.0 + zeta) / denominator;
523 functions[2][6] = (1.0 - xi) * (1.0 + zeta) / denominator;
524 functions[2][7] = (1.0 + xi) * (1.0 + zeta) / denominator;
526 functions[3][0] = -(1.0 - xi) * (1.0 - eta) / denominator;
527 functions[3][1] = -(1.0 + xi) * (1.0 - eta) / denominator;
528 functions[3][2] = -(1.0 - xi) * (1.0 + eta) / denominator;
529 functions[3][3] = -(1.0 + xi) * (1.0 + eta) / denominator;
530 functions[3][4] = (1.0 - xi) * (1.0 - eta) / denominator;
531 functions[3][5] = (1.0 + xi) * (1.0 - eta) / denominator;
532 functions[3][6] = (1.0 - xi) * (1.0 + eta) / denominator;
533 functions[3][7] = (1.0 + xi) * (1.0 + eta) / denominator;
551 LO nodeCoarseTuple[3] = {0, 0, 0};
552 LO nodeFineTuple[3] = {0, 0, 0};
553 auto coarseningRate = geoData_.getCoarseningRates();
554 auto fineNodesPerDir = geoData_.getLocalFineNodesPerDir();
555 auto coarseNodesPerDir = geoData_.getCoarseNodesPerDir();
556 geoData_.getCoarseLID2CoarseTuple(coarseNodeIdx, nodeCoarseTuple);
557 for (
int dim = 0; dim < 3; ++dim) {
558 if (nodeCoarseTuple[dim] == coarseNodesPerDir(dim) - 1) {
559 nodeFineTuple[dim] = fineNodesPerDir(dim) - 1;
561 nodeFineTuple[dim] = nodeCoarseTuple[dim] * coarseningRate(dim);
565 fineNodeIdx = nodeFineTuple[2] * fineNodesPerDir(1) * fineNodesPerDir(0) + nodeFineTuple[1] * fineNodesPerDir(0) + nodeFineTuple[0];
567 for (
int dim = 0; dim < fineCoordView_.extent_int(1); ++dim) {
568 coarseCoordView_(coarseNodeIdx, dim) = fineCoordView_(fineNodeIdx, dim);