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