MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_GeometricInterpolationPFactory_def.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MueLu: A package for multigrid based preconditioning
4//
5// Copyright 2012 NTESS and the MueLu contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEF_HPP
11#define MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEF_HPP
12
13#include "Xpetra_CrsGraph.hpp"
15
16#include "MueLu_MasterList.hpp"
17#include "MueLu_Monitor.hpp"
18#include "MueLu_Aggregates.hpp"
19#include "Xpetra_TpetraCrsMatrix.hpp"
20
21// Including this one last ensure that the short names of the above headers are defined properly
23
24namespace MueLu {
25
26template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
28 RCP<ParameterList> validParamList = rcp(new ParameterList());
29
30#define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
31 SET_VALID_ENTRY("interp: build coarse coordinates");
32#undef SET_VALID_ENTRY
33
34 // general variables needed in GeometricInterpolationPFactory
35 validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null,
36 "Generating factory of the matrix A");
37 validParamList->set<RCP<const FactoryBase> >("Aggregates", Teuchos::null,
38 "Aggregates generated by StructuredAggregationFactory used to construct a piece-constant prolongator.");
39 validParamList->set<RCP<const FactoryBase> >("prolongatorGraph", Teuchos::null,
40 "Graph generated by StructuredAggregationFactory used to construct a piece-linear prolongator.");
41 validParamList->set<RCP<const FactoryBase> >("Coordinates", Teuchos::null,
42 "Fine level coordinates used to construct piece-wise linear prolongator and coarse level coordinates.");
43 validParamList->set<RCP<const FactoryBase> >("coarseCoordinatesFineMap", Teuchos::null,
44 "map of the coarse coordinates' GIDs as indexed on the fine mesh.");
45 validParamList->set<RCP<const FactoryBase> >("coarseCoordinatesMap", Teuchos::null,
46 "map of the coarse coordinates' GIDs as indexed on the coarse mesh.");
47 validParamList->set<RCP<const FactoryBase> >("Nullspace", Teuchos::null,
48 "Fine level nullspace used to construct the coarse level nullspace.");
49 validParamList->set<RCP<const FactoryBase> >("numDimensions", Teuchos::null,
50 "Number of spacial dimensions in the problem.");
51 validParamList->set<RCP<const FactoryBase> >("lCoarseNodesPerDim", Teuchos::null,
52 "Number of nodes per spatial dimension on the coarse grid.");
53 validParamList->set<RCP<const FactoryBase> >("structuredInterpolationOrder", Teuchos::null,
54 "Interpolation order for constructing the prolongator.");
55 validParamList->set<bool>("keep coarse coords", false, "Flag to keep coordinates for special coarse grid solve");
56 validParamList->set<bool>("interp: remove small entries", true, "Remove small interpolation coeficient from prolongator to reduce fill-in on coarse level");
57
58 return validParamList;
59}
60
61template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
63 DeclareInput(Level& fineLevel, Level& /* coarseLevel */) const {
64 const ParameterList& pL = GetParameterList();
65
66 Input(fineLevel, "A");
67 Input(fineLevel, "Nullspace");
68 Input(fineLevel, "numDimensions");
69 Input(fineLevel, "prolongatorGraph");
70 Input(fineLevel, "lCoarseNodesPerDim");
71 Input(fineLevel, "structuredInterpolationOrder");
72
73 if (pL.get<bool>("interp: build coarse coordinates") ||
74 Get<int>(fineLevel, "structuredInterpolationOrder") == 1) {
75 Input(fineLevel, "Coordinates");
76 Input(fineLevel, "coarseCoordinatesFineMap");
77 Input(fineLevel, "coarseCoordinatesMap");
78 }
79}
80
81template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
83 Build(Level& fineLevel, Level& coarseLevel) const {
84 return BuildP(fineLevel, coarseLevel);
85}
86
87template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
89 BuildP(Level& fineLevel, Level& coarseLevel) const {
90 FactoryMonitor m(*this, "BuildP", coarseLevel);
91
92 // Set debug outputs based on environment variable
93 RCP<Teuchos::FancyOStream> out;
94 if (const char* dbg = std::getenv("MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
95 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
96 out->setShowAllFrontMatter(false).setShowProcRank(true);
97 } else {
98 out = Teuchos::getFancyOStream(rcp(new Teuchos::oblackholestream()));
99 }
100
101 *out << "Starting GeometricInterpolationPFactory::BuildP." << std::endl;
102
103 // Get inputs from the parameter list
104 const ParameterList& pL = GetParameterList();
105 const bool removeSmallEntries = pL.get<bool>("interp: remove small entries");
106 const bool buildCoarseCoordinates = pL.get<bool>("interp: build coarse coordinates");
107 const int interpolationOrder = Get<int>(fineLevel, "structuredInterpolationOrder");
108 const int numDimensions = Get<int>(fineLevel, "numDimensions");
109
110 // Declared main input/outputs to be retrieved and placed on the fine resp. coarse level
111 RCP<Matrix> A = Get<RCP<Matrix> >(fineLevel, "A");
112 RCP<const CrsGraph> prolongatorGraph = Get<RCP<CrsGraph> >(fineLevel, "prolongatorGraph");
113 RCP<realvaluedmultivector_type> fineCoordinates, coarseCoordinates;
114 RCP<Matrix> P;
115
116 // Check if we need to build coarse coordinates as they are used if we construct
117 // a linear interpolation prolongator
118 if (buildCoarseCoordinates || (interpolationOrder == 1)) {
119 SubFactoryMonitor sfm(*this, "BuildCoordinates", coarseLevel);
120 RCP<const Map> coarseCoordsFineMap = Get<RCP<const Map> >(fineLevel, "coarseCoordinatesFineMap");
121 RCP<const Map> coarseCoordsMap = Get<RCP<const Map> >(fineLevel, "coarseCoordinatesMap");
122
123 fineCoordinates = Get<RCP<realvaluedmultivector_type> >(fineLevel, "Coordinates");
124 coarseCoordinates = Xpetra::MultiVectorFactory<real_type, LO, GO, Node>::Build(coarseCoordsFineMap,
125 fineCoordinates->getNumVectors());
126 RCP<const Import> coordsImporter = ImportFactory::Build(fineCoordinates->getMap(),
127 coarseCoordsFineMap);
128 coarseCoordinates->doImport(*fineCoordinates, *coordsImporter, Xpetra::INSERT);
129 coarseCoordinates->replaceMap(coarseCoordsMap);
130
131 Set(coarseLevel, "Coordinates", coarseCoordinates);
132
133 if (pL.get<bool>("keep coarse coords")) {
134 coarseLevel.Set<RCP<realvaluedmultivector_type> >("Coordinates2", coarseCoordinates, NoFactory::get());
135 }
136 }
137
138 *out << "Fine and coarse coordinates have been loaded from the fine level and set on the coarse level." << std::endl;
139
140 if (interpolationOrder == 0) {
141 SubFactoryMonitor sfm(*this, "BuildConstantP", coarseLevel);
142 // Compute the prolongator using piece-wise constant interpolation
143 BuildConstantP(P, prolongatorGraph, A);
144 } else if (interpolationOrder == 1) {
145 // Compute the prolongator using piece-wise linear interpolation
146 // First get all the required coordinates to compute the local part of P
147 RCP<const Map> prolongatorColMap = prolongatorGraph->getColMap();
148
149 const size_t dofsPerNode = static_cast<size_t>(A->GetFixedBlockSize());
150 const size_t numColIndices = prolongatorColMap->getLocalNumElements();
151 TEUCHOS_TEST_FOR_EXCEPTION((numColIndices % dofsPerNode) != 0,
153 "Something went wrong, the number of columns in the prolongator is not a multiple of dofsPerNode!");
154 const size_t numGhostCoords = numColIndices / dofsPerNode;
155 const GO indexBase = prolongatorColMap->getIndexBase();
156 const GO coordIndexBase = fineCoordinates->getMap()->getIndexBase();
157
158 ArrayView<const GO> prolongatorColIndices = prolongatorColMap->getLocalElementList();
159 Array<GO> ghostCoordIndices(numGhostCoords);
160 for (size_t ghostCoordIdx = 0; ghostCoordIdx < numGhostCoords; ++ghostCoordIdx) {
161 ghostCoordIndices[ghostCoordIdx] = (prolongatorColIndices[ghostCoordIdx * dofsPerNode] - indexBase) / dofsPerNode + coordIndexBase;
162 }
163 RCP<Map> ghostCoordMap = MapFactory::Build(fineCoordinates->getMap()->lib(),
164 prolongatorColMap->getGlobalNumElements() / dofsPerNode,
165 ghostCoordIndices(),
166 coordIndexBase,
167 fineCoordinates->getMap()->getComm());
168
169 RCP<realvaluedmultivector_type> ghostCoordinates = Xpetra::MultiVectorFactory<real_type, LO, GO, NO>::Build(ghostCoordMap,
170 fineCoordinates->getNumVectors());
171 RCP<const Import> ghostImporter = ImportFactory::Build(coarseCoordinates->getMap(),
172 ghostCoordMap);
173 ghostCoordinates->doImport(*coarseCoordinates, *ghostImporter, Xpetra::INSERT);
174
175 BuildLinearP(coarseLevel, A, prolongatorGraph, fineCoordinates,
176 ghostCoordinates, numDimensions, removeSmallEntries, P);
177 }
178
179 *out << "The prolongator matrix has been built." << std::endl;
180
181 {
182 SubFactoryMonitor sfm(*this, "BuildNullspace", coarseLevel);
183 // Build the coarse nullspace
184 RCP<MultiVector> fineNullspace = Get<RCP<MultiVector> >(fineLevel, "Nullspace");
185 RCP<MultiVector> coarseNullspace = MultiVectorFactory::Build(P->getDomainMap(),
186 fineNullspace->getNumVectors());
187
188 using helpers = Xpetra::Helpers<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
189 if (helpers::isTpetraBlockCrs(A)) {
190 // FIXME: BlockCrs doesn't currently support transpose apply, so we have to do this the hard way
191 RCP<Matrix> Ptrans = Utilities::Transpose(*P);
192 Ptrans->apply(*fineNullspace, *coarseNullspace, Teuchos::NO_TRANS, Teuchos::ScalarTraits<SC>::one(),
193 Teuchos::ScalarTraits<SC>::zero());
194 } else {
195 P->apply(*fineNullspace, *coarseNullspace, Teuchos::TRANS, Teuchos::ScalarTraits<SC>::one(),
196 Teuchos::ScalarTraits<SC>::zero());
197 }
198
199 Set(coarseLevel, "Nullspace", coarseNullspace);
200 }
201
202 *out << "The coarse nullspace is constructed and set on the coarse level." << std::endl;
203
204 Set(coarseLevel, "P", P);
205
206 *out << "GeometricInterpolationPFactory::BuildP has completed." << std::endl;
207
208} // BuildP
209
210template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
212 BuildConstantP(RCP<Matrix>& P, RCP<const CrsGraph>& prolongatorGraph, RCP<Matrix>& A) const {
213 // Set debug outputs based on environment variable
214 RCP<Teuchos::FancyOStream> out;
215 if (const char* dbg = std::getenv("MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
216 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
217 out->setShowAllFrontMatter(false).setShowProcRank(true);
218 } else {
219 out = Teuchos::getFancyOStream(rcp(new Teuchos::oblackholestream()));
220 }
221
222 *out << "BuildConstantP" << std::endl;
223
224 std::vector<size_t> strideInfo(1);
225 strideInfo[0] = A->GetFixedBlockSize();
226 RCP<const StridedMap> stridedDomainMap =
227 StridedMapFactory::Build(prolongatorGraph->getDomainMap(), strideInfo);
228
229 *out << "Call prolongator constructor" << std::endl;
230
231 using helpers = Xpetra::Helpers<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
232 if (helpers::isTpetraBlockCrs(A)) {
233 SC one = Teuchos::ScalarTraits<SC>::one();
234 SC zero = Teuchos::ScalarTraits<SC>::zero();
235 LO NSDim = A->GetStorageBlockSize();
236
237 // Build the exploded Map
238 RCP<const Map> BlockMap = prolongatorGraph->getDomainMap();
239 Teuchos::ArrayView<const GO> block_dofs = BlockMap->getLocalElementList();
240 Teuchos::Array<GO> point_dofs(block_dofs.size() * NSDim);
241 for (LO i = 0, ct = 0; i < block_dofs.size(); i++) {
242 for (LO j = 0; j < NSDim; j++) {
243 point_dofs[ct] = block_dofs[i] * NSDim + j;
244 ct++;
245 }
246 }
247
248 RCP<const Map> PointMap = MapFactory::Build(BlockMap->lib(),
249 BlockMap->getGlobalNumElements() * NSDim,
250 point_dofs(),
251 BlockMap->getIndexBase(),
252 BlockMap->getComm());
253 strideInfo[0] = A->GetFixedBlockSize();
254 RCP<const StridedMap> stridedPointMap = StridedMapFactory::Build(PointMap, strideInfo);
255
256 RCP<Xpetra::CrsMatrix<SC, LO, GO, NO> > P_xpetra = Xpetra::CrsMatrixFactory<SC, LO, GO, NO>::BuildBlock(prolongatorGraph, PointMap, A->getRangeMap(), NSDim);
257 RCP<Xpetra::TpetraBlockCrsMatrix<SC, LO, GO, NO> > P_tpetra = rcp_dynamic_cast<Xpetra::TpetraBlockCrsMatrix<SC, LO, GO, NO> >(P_xpetra);
258 if (P_tpetra.is_null()) throw std::runtime_error("BuildConstantP Matrix factory did not return a Tpetra::BlockCrsMatrix");
259 RCP<CrsMatrixWrap> P_wrap = rcp(new CrsMatrixWrap(P_xpetra));
260
261 // NOTE: Assumes block-diagonal prolongation
262 Teuchos::Array<LO> temp(1);
263 Teuchos::ArrayView<const LO> indices;
264 Teuchos::Array<Scalar> block(NSDim * NSDim, zero);
265 for (LO i = 0; i < NSDim; i++)
266 block[i * NSDim + i] = one;
267 for (LO i = 0; i < (int)prolongatorGraph->getLocalNumRows(); i++) {
268 prolongatorGraph->getLocalRowView(i, indices);
269 for (LO j = 0; j < (LO)indices.size(); j++) {
270 temp[0] = indices[j];
271 P_tpetra->replaceLocalValues(i, temp(), block());
272 }
273 }
274
275 P = P_wrap;
276 if (A->IsView("stridedMaps") == true) {
277 P->CreateView("stridedMaps", A->getRowMap("stridedMaps"), stridedPointMap);
278 }
279 } else {
280 // Create the prolongator matrix and its associated objects
281 RCP<ParameterList> dummyList = rcp(new ParameterList());
282 P = rcp(new CrsMatrixWrap(prolongatorGraph, dummyList));
283 RCP<CrsMatrix> PCrs = toCrsMatrix(P);
284 PCrs->setAllToScalar(1.0);
285 PCrs->fillComplete();
286
287 // set StridingInformation of P
288 if (A->IsView("stridedMaps") == true)
289 P->CreateView("stridedMaps", A->getRowMap("stridedMaps"), stridedDomainMap);
290 }
291
292} // BuildConstantP
293
294template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
296 BuildLinearP(Level& coarseLevel,
297 RCP<Matrix>& A, RCP<const CrsGraph>& prolongatorGraph,
298 RCP<realvaluedmultivector_type>& fineCoordinates,
299 RCP<realvaluedmultivector_type>& ghostCoordinates,
300 const int numDimensions, const bool removeSmallEntries,
301 RCP<Matrix>& P) const {
302 // Set debug outputs based on environment variable
303 RCP<Teuchos::FancyOStream> out;
304 if (const char* dbg = std::getenv("MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
305 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
306 out->setShowAllFrontMatter(false).setShowProcRank(true);
307 } else {
308 out = Teuchos::getFancyOStream(rcp(new Teuchos::oblackholestream()));
309 }
310
311 // Compute 2^numDimensions using bit logic to avoid round-off errors
312 const int numInterpolationPoints = 1 << numDimensions;
313 const int dofsPerNode = A->GetFixedBlockSize() / A->GetStorageBlockSize();
314 ;
315
316 RCP<ParameterList> dummyList = rcp(new ParameterList());
317 P = rcp(new CrsMatrixWrap(prolongatorGraph, dummyList));
318 RCP<CrsMatrix> PCrs = toCrsMatrix(P);
319 PCrs->resumeFill(); // The Epetra matrix is considered filled at this point.
320
321 {
322 *out << "Entering BuildLinearP" << std::endl;
323 SubFactoryMonitor sfm(*this, "BuildLinearP", coarseLevel);
324
325 // Extract coordinates for interpolation stencil calculations
326 const LO numFineNodes = fineCoordinates->getLocalLength();
327 const LO numGhostNodes = ghostCoordinates->getLocalLength();
328 Array<ArrayRCP<const real_type> > fineCoords(3);
329 Array<ArrayRCP<const real_type> > ghostCoords(3);
330 const real_type realZero = Teuchos::as<real_type>(0.0);
331 ArrayRCP<real_type> fineZero(numFineNodes, realZero);
332 ArrayRCP<real_type> ghostZero(numGhostNodes, realZero);
333 for (int dim = 0; dim < 3; ++dim) {
334 if (dim < numDimensions) {
335 fineCoords[dim] = fineCoordinates->getData(dim);
336 ghostCoords[dim] = ghostCoordinates->getData(dim);
337 } else {
338 fineCoords[dim] = fineZero;
339 ghostCoords[dim] = ghostZero;
340 }
341 }
342
343 *out << "Coordinates extracted from the multivectors!" << std::endl;
344
345 { // Construct the linear interpolation prolongator
346 LO interpolationNodeIdx = 0, rowIdx = 0;
347 ArrayView<const LO> colIndices;
348 Array<SC> values;
349 Array<Array<real_type> > coords(numInterpolationPoints + 1);
350 Array<real_type> stencil(numInterpolationPoints);
351 for (LO nodeIdx = 0; nodeIdx < numFineNodes; ++nodeIdx) {
352 if (PCrs->getNumEntriesInLocalRow(nodeIdx * dofsPerNode) == 1) {
353 values.resize(1);
354 values[0] = 1.0;
355 for (LO dof = 0; dof < dofsPerNode; ++dof) {
356 rowIdx = nodeIdx * dofsPerNode + dof;
357 prolongatorGraph->getLocalRowView(rowIdx, colIndices);
358 PCrs->replaceLocalValues(rowIdx, colIndices, values());
359 }
360 } else {
361 // Extract the coordinates associated with the current node
362 // and the neighboring coarse nodes
363 coords[0].resize(3);
364 for (int dim = 0; dim < 3; ++dim) {
365 coords[0][dim] = fineCoords[dim][nodeIdx];
366 }
367 prolongatorGraph->getLocalRowView(nodeIdx * dofsPerNode, colIndices);
368 for (int interpolationIdx = 0; interpolationIdx < numInterpolationPoints; ++interpolationIdx) {
369 coords[interpolationIdx + 1].resize(3);
370 interpolationNodeIdx = colIndices[interpolationIdx] / dofsPerNode;
371 for (int dim = 0; dim < 3; ++dim) {
372 coords[interpolationIdx + 1][dim] = ghostCoords[dim][interpolationNodeIdx];
373 }
374 }
375 RCP<Teuchos::TimeMonitor> tm = rcp(new Teuchos::TimeMonitor(*Teuchos::TimeMonitor::getNewTimer("Compute Linear Interpolation")));
376 ComputeLinearInterpolationStencil(numDimensions, numInterpolationPoints, coords, stencil);
377 tm = Teuchos::null;
378 values.resize(numInterpolationPoints);
379 for (LO valueIdx = 0; valueIdx < numInterpolationPoints; ++valueIdx) {
380 values[valueIdx] = Teuchos::as<SC>(stencil[valueIdx]);
381 }
382
383 // Set values in all the rows corresponding to nodeIdx
384 for (LO dof = 0; dof < dofsPerNode; ++dof) {
385 rowIdx = nodeIdx * dofsPerNode + dof;
386 prolongatorGraph->getLocalRowView(rowIdx, colIndices);
387 PCrs->replaceLocalValues(rowIdx, colIndices, values());
388 }
389 } // Check for Coarse vs. Fine point
390 } // Loop over fine nodes
391 } // Construct the linear interpolation prolongator
392
393 *out << "The calculation of the interpolation stencils has completed." << std::endl;
394
395 PCrs->fillComplete();
396 }
397
398 *out << "All values in P have been set and fillComplete has been performed." << std::endl;
399
400 // Note lbv Jan 29 2019: this should be handle at aggregation level
401 // if the user really does not want potential d2 neighbors on coarse grid
402 // that way we would avoid a new graph construction...
403
404 // Check if we want to remove small entries from P
405 // to reduce stencil growth on next level.
406 if (removeSmallEntries) {
407 *out << "Entering remove small entries" << std::endl;
408 SubFactoryMonitor sfm(*this, "remove small entries", coarseLevel);
409
410 ArrayRCP<const size_t> rowptrOrig;
411 ArrayRCP<const LO> colindOrig;
412 ArrayRCP<const Scalar> valuesOrig;
413 PCrs->getAllValues(rowptrOrig, colindOrig, valuesOrig);
414
415 const size_t numRows = static_cast<size_t>(rowptrOrig.size() - 1);
416 ArrayRCP<size_t> rowPtr(numRows + 1);
417 ArrayRCP<size_t> nnzOnRows(numRows);
418 rowPtr[0] = 0;
419 size_t countRemovedEntries = 0;
420 for (size_t rowIdx = 0; rowIdx < numRows; ++rowIdx) {
421 for (size_t entryIdx = rowptrOrig[rowIdx]; entryIdx < rowptrOrig[rowIdx + 1]; ++entryIdx) {
422 if (Teuchos::ScalarTraits<Scalar>::magnitude(valuesOrig[entryIdx]) < 1e-6) {
423 ++countRemovedEntries;
424 }
425 }
426 rowPtr[rowIdx + 1] = rowptrOrig[rowIdx + 1] - countRemovedEntries;
427 nnzOnRows[rowIdx] = rowPtr[rowIdx + 1] - rowPtr[rowIdx];
428 }
429 GetOStream(Statistics1) << "interp: number of small entries removed= " << countRemovedEntries << " / " << rowptrOrig[numRows] << std::endl;
430
431 size_t countKeptEntries = 0;
432 ArrayRCP<LO> colInd(rowPtr[numRows]);
433 ArrayRCP<SC> values(rowPtr[numRows]);
434 for (size_t entryIdx = 0; entryIdx < rowptrOrig[numRows]; ++entryIdx) {
435 if (Teuchos::ScalarTraits<Scalar>::magnitude(valuesOrig[entryIdx]) > 1e-6) {
436 colInd[countKeptEntries] = colindOrig[entryIdx];
437 values[countKeptEntries] = valuesOrig[entryIdx];
438 ++countKeptEntries;
439 }
440 }
441
442 P = rcp(new CrsMatrixWrap(prolongatorGraph->getRowMap(),
443 prolongatorGraph->getColMap(),
444 nnzOnRows));
445 RCP<CrsMatrix> PCrsSqueezed = toCrsMatrix(P);
446 PCrsSqueezed->resumeFill(); // The Epetra matrix is considered filled at this point.
447 PCrsSqueezed->setAllValues(rowPtr, colInd, values);
448 PCrsSqueezed->expertStaticFillComplete(prolongatorGraph->getDomainMap(),
449 prolongatorGraph->getRangeMap());
450 }
451
452 std::vector<size_t> strideInfo(1);
453 strideInfo[0] = dofsPerNode;
454 RCP<const StridedMap> stridedDomainMap =
455 StridedMapFactory::Build(prolongatorGraph->getDomainMap(), strideInfo);
456
457 *out << "The strided maps of P have been computed" << std::endl;
458
459 // set StridingInformation of P
460 if (A->IsView("stridedMaps") == true) {
461 P->CreateView("stridedMaps", A->getRowMap("stridedMaps"), stridedDomainMap);
462 }
463
464} // BuildLinearP
465
466template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
468 ComputeLinearInterpolationStencil(const int numDimensions, const int numInterpolationPoints,
469 const Array<Array<real_type> > coord,
470 Array<real_type>& stencil) const {
471 // 7 8 Find xi, eta and zeta such that
472 // x---------x
473 // /| /| Rx = x_p - sum N_i(xi,eta,zeta)x_i = 0
474 // 5/ | 6/ | Ry = y_p - sum N_i(xi,eta,zeta)y_i = 0
475 // x---------x | Rz = z_p - sum N_i(xi,eta,zeta)z_i = 0
476 // | | *P | |
477 // | x------|--x We can do this with a Newton solver:
478 // | /3 | /4 We will start with initial guess (xi,eta,zeta) = (0,0,0)
479 // |/ |/ We compute the Jacobian and iterate until convergence...
480 // z y x---------x
481 // | / 1 2 Once we have (xi,eta,zeta), we can evaluate all N_i which
482 // |/ give us the weights for the interpolation stencil!
483 // o---x
484 //
485
486 Teuchos::SerialDenseMatrix<LO, real_type> Jacobian(numDimensions, numDimensions);
487 Teuchos::SerialDenseVector<LO, real_type> residual(numDimensions);
488 Teuchos::SerialDenseVector<LO, real_type> solutionDirection(numDimensions);
489 Teuchos::SerialDenseVector<LO, real_type> paramCoords(numDimensions);
490 Teuchos::SerialDenseSolver<LO, real_type> problem;
491 int iter = 0, max_iter = 5;
492 real_type functions[4][8], norm_ref = 1.0, norm2 = 1.0, tol = 1.0e-5;
493 paramCoords.size(numDimensions);
494
495 while ((iter < max_iter) && (norm2 > tol * norm_ref)) {
496 ++iter;
497 norm2 = 0.0;
498 solutionDirection.size(numDimensions);
499 residual.size(numDimensions);
500 Jacobian = 0.0;
501
502 // Compute Jacobian and Residual
503 GetInterpolationFunctions(numDimensions, paramCoords, functions);
504 for (LO i = 0; i < numDimensions; ++i) {
505 residual(i) = coord[0][i]; // Add coordinates from point of interest
506 for (LO k = 0; k < numInterpolationPoints; ++k) {
507 residual(i) -= functions[0][k] * coord[k + 1][i]; // Remove contribution from all coarse points
508 }
509 if (iter == 1) {
510 norm_ref += residual(i) * residual(i);
511 if (i == numDimensions - 1) {
512 norm_ref = std::sqrt(norm_ref);
513 }
514 }
515
516 for (LO j = 0; j < numDimensions; ++j) {
517 for (LO k = 0; k < numInterpolationPoints; ++k) {
518 Jacobian(i, j) += functions[j + 1][k] * coord[k + 1][i];
519 }
520 }
521 }
522
523 // Set Jacobian, Vectors and solve problem
524 problem.setMatrix(Teuchos::rcp(&Jacobian, false));
525 problem.setVectors(Teuchos::rcp(&solutionDirection, false), Teuchos::rcp(&residual, false));
526 if (problem.shouldEquilibrate()) {
527 problem.factorWithEquilibration(true);
528 }
529 problem.solve();
530
531 for (LO i = 0; i < numDimensions; ++i) {
532 paramCoords(i) = paramCoords(i) + solutionDirection(i);
533 }
534
535 // Recompute Residual norm
536 GetInterpolationFunctions(numDimensions, paramCoords, functions);
537 for (LO i = 0; i < numDimensions; ++i) {
538 real_type tmp = coord[0][i];
539 for (LO k = 0; k < numInterpolationPoints; ++k) {
540 tmp -= functions[0][k] * coord[k + 1][i];
541 }
542 norm2 += tmp * tmp;
543 tmp = 0.0;
544 }
545 norm2 = std::sqrt(norm2);
546 }
547
548 // Load the interpolation values onto the stencil.
549 for (LO i = 0; i < numInterpolationPoints; ++i) {
550 stencil[i] = functions[0][i];
551 }
552
553} // End ComputeLinearInterpolationStencil
554
555template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
557 GetInterpolationFunctions(const LO numDimensions,
558 const Teuchos::SerialDenseVector<LO, real_type> parametricCoordinates,
559 real_type functions[4][8]) const {
560 real_type xi = 0.0, eta = 0.0, zeta = 0.0, denominator = 0.0;
561 if (numDimensions == 1) {
562 xi = parametricCoordinates[0];
563 denominator = 2.0;
564 } else if (numDimensions == 2) {
565 xi = parametricCoordinates[0];
566 eta = parametricCoordinates[1];
567 denominator = 4.0;
568 } else if (numDimensions == 3) {
569 xi = parametricCoordinates[0];
570 eta = parametricCoordinates[1];
571 zeta = parametricCoordinates[2];
572 denominator = 8.0;
573 }
574
575 functions[0][0] = (1.0 - xi) * (1.0 - eta) * (1.0 - zeta) / denominator;
576 functions[0][1] = (1.0 + xi) * (1.0 - eta) * (1.0 - zeta) / denominator;
577 functions[0][2] = (1.0 - xi) * (1.0 + eta) * (1.0 - zeta) / denominator;
578 functions[0][3] = (1.0 + xi) * (1.0 + eta) * (1.0 - zeta) / denominator;
579 functions[0][4] = (1.0 - xi) * (1.0 - eta) * (1.0 + zeta) / denominator;
580 functions[0][5] = (1.0 + xi) * (1.0 - eta) * (1.0 + zeta) / denominator;
581 functions[0][6] = (1.0 - xi) * (1.0 + eta) * (1.0 + zeta) / denominator;
582 functions[0][7] = (1.0 + xi) * (1.0 + eta) * (1.0 + zeta) / denominator;
583
584 functions[1][0] = -(1.0 - eta) * (1.0 - zeta) / denominator;
585 functions[1][1] = (1.0 - eta) * (1.0 - zeta) / denominator;
586 functions[1][2] = -(1.0 + eta) * (1.0 - zeta) / denominator;
587 functions[1][3] = (1.0 + eta) * (1.0 - zeta) / denominator;
588 functions[1][4] = -(1.0 - eta) * (1.0 + zeta) / denominator;
589 functions[1][5] = (1.0 - eta) * (1.0 + zeta) / denominator;
590 functions[1][6] = -(1.0 + eta) * (1.0 + zeta) / denominator;
591 functions[1][7] = (1.0 + eta) * (1.0 + zeta) / denominator;
592
593 functions[2][0] = -(1.0 - xi) * (1.0 - zeta) / denominator;
594 functions[2][1] = -(1.0 + xi) * (1.0 - zeta) / denominator;
595 functions[2][2] = (1.0 - xi) * (1.0 - zeta) / denominator;
596 functions[2][3] = (1.0 + xi) * (1.0 - zeta) / denominator;
597 functions[2][4] = -(1.0 - xi) * (1.0 + zeta) / denominator;
598 functions[2][5] = -(1.0 + xi) * (1.0 + zeta) / denominator;
599 functions[2][6] = (1.0 - xi) * (1.0 + zeta) / denominator;
600 functions[2][7] = (1.0 + xi) * (1.0 + zeta) / denominator;
601
602 functions[3][0] = -(1.0 - xi) * (1.0 - eta) / denominator;
603 functions[3][1] = -(1.0 + xi) * (1.0 - eta) / denominator;
604 functions[3][2] = -(1.0 - xi) * (1.0 + eta) / denominator;
605 functions[3][3] = -(1.0 + xi) * (1.0 + eta) / denominator;
606 functions[3][4] = (1.0 - xi) * (1.0 - eta) / denominator;
607 functions[3][5] = (1.0 + xi) * (1.0 - eta) / denominator;
608 functions[3][6] = (1.0 - xi) * (1.0 + eta) / denominator;
609 functions[3][7] = (1.0 + xi) * (1.0 + eta) / denominator;
610
611} // End GetInterpolationFunctions
612
613} // namespace MueLu
614
615#endif // MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEF_HPP
#define SET_VALID_ENTRY(name)
Exception throws to report errors in the internal logical of the program.
Timer to be used in factories. Similar to Monitor but with additional timers.
typename Teuchos::ScalarTraits< SC >::coordinateType real_type
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
void BuildConstantP(RCP< Matrix > &P, RCP< const CrsGraph > &prolongatorGraph, RCP< Matrix > &A) const
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
void BuildP(Level &fineLevel, Level &coarseLevel) const
Abstract Build method.
void ComputeLinearInterpolationStencil(const int numDimensions, const int numInterpolationPoints, const Array< Array< real_type > > coord, Array< real_type > &stencil) const
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
void GetInterpolationFunctions(const LO numDimensions, const Teuchos::SerialDenseVector< LO, real_type > parametricCoordinates, real_type functions[4][8]) const
Class that holds all level-specific information.
void Set(const std::string &ename, const T &entry, const FactoryBase *factory=NoFactory::get())
static const NoFactory * get()
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
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 > &params=Teuchos::null)
Namespace for MueLu classes and methods.
@ Statistics1
Print more statistics.
Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > removeSmallEntries(Teuchos::RCP< Xpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A, const typename Teuchos::ScalarTraits< Scalar >::magnitudeType threshold, const bool keepDiagonal)