MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_GeneralGeometricPFactory_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_GENERALGEOMETRICPFACTORY_DEF_HPP
47#define MUELU_GENERALGEOMETRICPFACTORY_DEF_HPP
48
49#include <stdlib.h>
50#include <iomanip>
51
52
53// #include <Teuchos_LAPACK.hpp>
54#include <Teuchos_SerialDenseMatrix.hpp>
55#include <Teuchos_SerialDenseVector.hpp>
56#include <Teuchos_SerialDenseSolver.hpp>
57
58#include <Xpetra_CrsMatrixWrap.hpp>
59#include <Xpetra_ImportFactory.hpp>
60#include <Xpetra_Matrix.hpp>
61#include <Xpetra_MapFactory.hpp>
62#include <Xpetra_MultiVectorFactory.hpp>
63#include <Xpetra_VectorFactory.hpp>
64
65#include <Xpetra_IO.hpp>
66
68
69#include "MueLu_Monitor.hpp"
70
71namespace MueLu {
72
73 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
75 RCP<ParameterList> validParamList = rcp(new ParameterList());
76
77 // Coarsen can come in two forms, either a single char that will be interpreted as an integer
78 // which is used as the coarsening rate in every spatial dimentions, or it can be a longer
79 // string that will then be interpreted as an array of integers.
80 // By default coarsen is set as "{2}", hence a coarsening rate of 2 in every spatial dimension
81 // is the default setting!
82 validParamList->set<std::string > ("Coarsen", "{2}",
83 "Coarsening rate in all spatial dimensions");
84 validParamList->set<int> ("order", 1,
85 "Order of the interpolation scheme used");
86 validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null,
87 "Generating factory of the matrix A");
88 validParamList->set<RCP<const FactoryBase> >("Nullspace", Teuchos::null,
89 "Generating factory of the nullspace");
90 validParamList->set<RCP<const FactoryBase> >("Coordinates", Teuchos::null,
91 "Generating factory for coorindates");
92 validParamList->set<RCP<const FactoryBase> >("gNodesPerDim", Teuchos::null,
93 "Number of nodes per spatial dimmension provided by CoordinatesTransferFactory.");
94 validParamList->set<RCP<const FactoryBase> >("lNodesPerDim", Teuchos::null,
95 "Number of nodes per spatial dimmension provided by CoordinatesTransferFactory.");
96 validParamList->set<std::string > ("meshLayout", "Global Lexicographic",
97 "Type of mesh ordering");
98 validParamList->set<RCP<const FactoryBase> >("meshData", Teuchos::null,
99 "Mesh ordering associated data");
100
101 return validParamList;
102 }
103
104 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
106 DeclareInput(Level& fineLevel, Level& /* coarseLevel */) const {
107 Input(fineLevel, "A");
108 Input(fineLevel, "Nullspace");
109 Input(fineLevel, "Coordinates");
110 // Request the global number of nodes per dimensions
111 if(fineLevel.GetLevelID() == 0) {
112 if(fineLevel.IsAvailable("gNodesPerDim", NoFactory::get())) {
113 fineLevel.DeclareInput("gNodesPerDim", NoFactory::get(), this);
114 } else {
115 TEUCHOS_TEST_FOR_EXCEPTION(fineLevel.IsAvailable("gNodesPerDim", NoFactory::get()),
117 "gNodesPerDim was not provided by the user on level0!");
118 }
119 } else {
120 Input(fineLevel, "gNodesPerDim");
121 }
122
123 // Request the local number of nodes per dimensions
124 if(fineLevel.GetLevelID() == 0) {
125 if(fineLevel.IsAvailable("lNodesPerDim", NoFactory::get())) {
126 fineLevel.DeclareInput("lNodesPerDim", NoFactory::get(), this);
127 } else {
128 TEUCHOS_TEST_FOR_EXCEPTION(fineLevel.IsAvailable("lNodesPerDim", NoFactory::get()),
130 "lNodesPerDim was not provided by the user on level0!");
131 }
132 } else {
133 Input(fineLevel, "lNodesPerDim");
134 }
135 }
136
137 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
139 Level& coarseLevel) const {
140 return BuildP(fineLevel, coarseLevel);
141 }
142
143 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
145 Level& coarseLevel) const {
146 FactoryMonitor m(*this, "Build", coarseLevel);
147
148 // Obtain general variables
149 using xdMV = Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType,LO,GO,NO>;
150 RCP<Matrix> A = Get< RCP<Matrix> > (fineLevel, "A");
151 RCP<MultiVector> fineNullspace = Get< RCP<MultiVector> > (fineLevel, "Nullspace");
152 RCP<xdMV> fineCoords = Get< RCP<xdMV> >(fineLevel, "Coordinates");
153 RCP<xdMV> coarseCoords;
154
155 // Get user-provided coarsening rate parameter (constant over all levels)
156 const ParameterList& pL = GetParameterList();
157
158 // collect general input data
159 const LO blkSize = A->GetFixedBlockSize();
160 RCP<const Map> rowMap = A->getRowMap();
161 RCP<GeometricData> myGeometry = rcp(new GeometricData{});
162
163 // Load the mesh layout type and the associated mesh data
164 myGeometry->meshLayout = pL.get<std::string>("meshLayout");
165 if(fineLevel.GetLevelID() == 0) {
166 if(myGeometry->meshLayout == "Local Lexicographic") {
167 Array<GO> meshData;
168 meshData = fineLevel.Get<Array<GO> >("meshData", NoFactory::get());
169 TEUCHOS_TEST_FOR_EXCEPTION(meshData.empty() == true, Exceptions::RuntimeError,
170 "The meshData array is empty, somehow the input for geometric"
171 " multigrid are not captured correctly.");
172 myGeometry->meshData.resize(rowMap->getComm()->getSize());
173 for(int i = 0; i < rowMap->getComm()->getSize(); ++i) {
174 myGeometry->meshData[i].resize(10);
175 for(int j = 0; j < 10; ++j) {
176 myGeometry->meshData[i][j] = meshData[10*i + j];
177 }
178 }
179 }
180 }
181
182 TEUCHOS_TEST_FOR_EXCEPTION(fineCoords == Teuchos::null, Exceptions::RuntimeError,
183 "Coordinates cannot be accessed from fine level!");
184 myGeometry->numDimensions = fineCoords->getNumVectors();
185
186 // Get the number of points in each direction
187 if(fineLevel.GetLevelID() == 0) {
188 myGeometry->gFineNodesPerDir = fineLevel.Get<Array<GO> >("gNodesPerDim", NoFactory::get());
189 myGeometry->lFineNodesPerDir = fineLevel.Get<Array<LO> >("lNodesPerDim", NoFactory::get());
190 } else {
191 // Loading global number of nodes per diretions
192 myGeometry->gFineNodesPerDir = Get<Array<GO> >(fineLevel, "gNodesPerDim");
193
194 // Loading local number of nodes per diretions
195 myGeometry->lFineNodesPerDir = Get<Array<LO> >(fineLevel, "lNodesPerDim");
196 }
197 myGeometry->gNumFineNodes10 = myGeometry->gFineNodesPerDir[1]*myGeometry->gFineNodesPerDir[0];
198 myGeometry->gNumFineNodes = myGeometry->gFineNodesPerDir[2]*myGeometry->gNumFineNodes10;
199 myGeometry->lNumFineNodes10 = myGeometry->lFineNodesPerDir[1]*myGeometry->lFineNodesPerDir[0];
200 myGeometry->lNumFineNodes = myGeometry->lFineNodesPerDir[2]*myGeometry->lNumFineNodes10;
201
202 TEUCHOS_TEST_FOR_EXCEPTION(fineCoords->getLocalLength()
203 != static_cast<size_t>(myGeometry->lNumFineNodes),
205 "The local number of elements in Coordinates is not equal to the"
206 " number of nodes given by: lNodesPerDim!");
207 TEUCHOS_TEST_FOR_EXCEPTION(fineCoords->getGlobalLength()
208 != static_cast<size_t>(myGeometry->gNumFineNodes),
210 "The global number of elements in Coordinates is not equal to the"
211 " number of nodes given by: gNodesPerDim!");
212
213 // Get the coarsening rate
214 std::string coarsenRate = pL.get<std::string>("Coarsen");
215 Teuchos::Array<LO> crates;
216 try {
217 crates = Teuchos::fromStringToArray<LO>(coarsenRate);
218 } catch(const Teuchos::InvalidArrayStringRepresentation& e) {
219 GetOStream(Errors,-1) << " *** Coarsen must be a string convertible into an array! *** "
220 << std::endl;
221 throw e;
222 }
223 TEUCHOS_TEST_FOR_EXCEPTION((crates.size() > 1) && (crates.size() < myGeometry->numDimensions),
225 "Coarsen must have at least as many components as the number of"
226 " spatial dimensions in the problem.");
227
228 for(LO i = 0; i < 3; ++i) {
229 if(i < myGeometry->numDimensions) {
230 if(crates.size()==1) {
231 myGeometry->coarseRate[i] = crates[0];
232 } else if(crates.size() == myGeometry->numDimensions) {
233 myGeometry->coarseRate[i] = crates[i];
234 }
235 } else {
236 myGeometry->coarseRate[i] = 1;
237 }
238 }
239
240 int interpolationOrder = pL.get<int>("order");
241 TEUCHOS_TEST_FOR_EXCEPTION((interpolationOrder < 0) || (interpolationOrder > 1),
243 "The interpolation order can only be set to 0 or 1.");
244
245 // Get the axis permutation from Global axis to Local axis
246 Array<LO> mapDirG2L(3), mapDirL2G(3);
247 for(LO i = 0; i < myGeometry->numDimensions; ++i) {
248 mapDirG2L[i] = i;
249 }
250 for(LO i = 0; i < myGeometry->numDimensions; ++i) {
251 TEUCHOS_TEST_FOR_EXCEPTION(mapDirG2L[i] > myGeometry->numDimensions,
253 "axis permutation values must all be less than"
254 " the number of spatial dimensions.");
255 mapDirL2G[mapDirG2L[i]] = i;
256 }
257 RCP<const Map> fineCoordsMap = fineCoords->getMap();
258
259 // This struct stores PIDs, LIDs and GIDs on the fine mesh and GIDs on the coarse mesh.
260 RCP<NodesIDs> ghostedCoarseNodes = rcp(new NodesIDs{});
261 Array<Array<GO> > lCoarseNodesGIDs(2);
262 if((fineLevel.GetLevelID() == 0) && (myGeometry->meshLayout != "Global Lexicographic")) {
263 MeshLayoutInterface(interpolationOrder, blkSize, fineCoordsMap, myGeometry,
264 ghostedCoarseNodes, lCoarseNodesGIDs);
265 } else {
266 // This function expects perfect global lexicographic ordering of nodes and will not process
267 // data correctly otherwise. These restrictions allow for the simplest and most efficient
268 // processing of the levels (hopefully at least).
269 GetCoarsePoints(interpolationOrder, blkSize, fineCoordsMap, myGeometry, ghostedCoarseNodes,
270 lCoarseNodesGIDs);
271 }
272
273 // All that is left to do is loop over NCpts and:
274 // - extract coarse points coordiate for coarseCoords
275 // - get coordinates for current stencil computation
276 // - compute current stencil
277 // - compute row and column indices for stencil entries
278 RCP<const Map> stridedDomainMapP;
279 RCP<Matrix> P;
280 // Fancy formula for the number of non-zero terms
281 // All coarse points are injected, other points are using polynomial interpolation
282 // and have contribution from (interpolationOrder + 1)^numDimensions
283 // Noticebly this leads to 1 when the order is zero, hence fancy MatMatMatMult can be used.
284 GO lnnzP = Teuchos::as<LO>(std::pow(interpolationOrder + 1, myGeometry->numDimensions))
285 *(myGeometry->lNumFineNodes - myGeometry->lNumCoarseNodes) + myGeometry->lNumCoarseNodes;
286 lnnzP = lnnzP*blkSize;
287 GO gnnzP = Teuchos::as<LO>(std::pow(interpolationOrder + 1, myGeometry->numDimensions))
288 *(myGeometry->gNumFineNodes - myGeometry->gNumCoarseNodes) + myGeometry->gNumCoarseNodes;
289 gnnzP = gnnzP*blkSize;
290
291 GetOStream(Runtime1) << "P size = " << blkSize*myGeometry->gNumFineNodes
292 << " x " << blkSize*myGeometry->gNumCoarseNodes << std::endl;
293 GetOStream(Runtime1) << "P Fine grid : " << myGeometry->gFineNodesPerDir[0] << " -- "
294 << myGeometry->gFineNodesPerDir[1] << " -- "
295 << myGeometry->gFineNodesPerDir[2] << std::endl;
296 GetOStream(Runtime1) << "P Coarse grid : " << myGeometry->gCoarseNodesPerDir[0] << " -- "
297 << myGeometry->gCoarseNodesPerDir[1] << " -- "
298 << myGeometry->gCoarseNodesPerDir[2] << std::endl;
299 GetOStream(Runtime1) << "P nnz estimate: " << gnnzP << std::endl;
300
301 MakeGeneralGeometricP(myGeometry, fineCoords, lnnzP, blkSize, stridedDomainMapP,
302 A, P, coarseCoords, ghostedCoarseNodes, lCoarseNodesGIDs,
303 interpolationOrder);
304
305 // set StridingInformation of P
306 if (A->IsView("stridedMaps") == true) {
307 P->CreateView("stridedMaps", A->getRowMap("stridedMaps"), stridedDomainMapP);
308 } else {
309 P->CreateView("stridedMaps", P->getRangeMap(), stridedDomainMapP);
310 }
311
312 // store the transfer operator and node coordinates on coarse level
313 Set(coarseLevel, "P", P);
314 Set(coarseLevel, "coarseCoordinates", coarseCoords);
315 Set<Array<GO> >(coarseLevel, "gCoarseNodesPerDim", myGeometry->gCoarseNodesPerDir);
316 Set<Array<LO> >(coarseLevel, "lCoarseNodesPerDim", myGeometry->lCoarseNodesPerDir);
317
318 // rst: null space might get scaled here ... do we care. We could just inject at the cpoints,
319 // but I don't feel that this is needed.
320 RCP<MultiVector> coarseNullspace = MultiVectorFactory::Build(P->getDomainMap(),
321 fineNullspace->getNumVectors());
322 P->apply(*fineNullspace, *coarseNullspace, Teuchos::TRANS, Teuchos::ScalarTraits<SC>::one(),
323 Teuchos::ScalarTraits<SC>::zero());
324 Set(coarseLevel, "Nullspace", coarseNullspace);
325
326 }
327
328 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
330 MeshLayoutInterface(const int /* interpolationOrder */, const LO /* blkSize */, RCP<const Map> fineCoordsMap,
331 RCP<GeometricData> myGeo, RCP<NodesIDs> ghostedCoarseNodes,
332 Array<Array<GO> >& lCoarseNodesGIDs) const{
333 // The goal here is to produce maps that globally labels the mesh lexicographically.
334 // These maps will replace the current maps of A, the coordinate vector and the nullspace.
335 // Ideally if the user provides the necessary maps then nothing needs to be done, otherwise
336 // it could be advantageous to allow the user to register a re-labeling function. Ultimately
337 // for common ordering schemes, some re-labeling can be directly implemented here.
338
339 int numRanks = fineCoordsMap->getComm()->getSize();
340 int myRank = fineCoordsMap->getComm()->getRank();
341
342 myGeo->myBlock = myGeo->meshData[myRank][2];
343 myGeo->startIndices[0] = myGeo->meshData[myRank][3];
344 myGeo->startIndices[1] = myGeo->meshData[myRank][5];
345 myGeo->startIndices[2] = myGeo->meshData[myRank][7];
346 myGeo->startIndices[3] = myGeo->meshData[myRank][4];
347 myGeo->startIndices[4] = myGeo->meshData[myRank][6];
348 myGeo->startIndices[5] = myGeo->meshData[myRank][8];
349 std::sort(myGeo->meshData.begin(), myGeo->meshData.end(),
350 [](const std::vector<GO>& a, const std::vector<GO>& b)->bool {
351 // The below function sorts ranks by blockID, kmin, jmin and imin
352 if(a[2] < b[2]) {
353 return true;
354 } else if(a[2] == b[2]) {
355 if(a[7] < b[7]) {
356 return true;
357 } else if(a[7] == b[7]) {
358 if(a[5] < b[5]) {
359 return true;
360 } else if(a[5] == b[5]) {
361 if(a[3] < b[3]) {return true;}
362 }
363 }
364 }
365 return false;
366 });
367
368 myGeo->numBlocks = myGeo->meshData[numRanks - 1][2] + 1;
369 // Find the range of the current block
370 auto myBlockStart = std::lower_bound(myGeo->meshData.begin(), myGeo->meshData.end(),
371 myGeo->myBlock - 1,
372 [](const std::vector<GO>& vec, const GO val)->bool{
373 return (vec[2] < val) ? true : false;
374 });
375 auto myBlockEnd = std::upper_bound(myGeo->meshData.begin(), myGeo->meshData.end(),
376 myGeo->myBlock,
377 [](const GO val, const std::vector<GO>& vec)->bool{
378 return (val < vec[2]) ? true : false;
379 });
380 // Assuming that i,j,k and ranges are split in pi, pj and pk processors
381 // we search for these numbers as they will allow us to find quickly the PID of processors
382 // owning ghost nodes.
383 auto myKEnd = std::upper_bound(myBlockStart, myBlockEnd, (*myBlockStart)[3],
384 [](const GO val, const std::vector<GO>& vec)->bool{
385 return (val < vec[7]) ? true : false;
386 });
387 auto myJEnd = std::upper_bound(myBlockStart, myKEnd, (*myBlockStart)[3],
388 [](const GO val, const std::vector<GO>& vec)->bool{
389 return (val < vec[5]) ? true : false;
390 });
391 LO pi = std::distance(myBlockStart, myJEnd);
392 LO pj = std::distance(myBlockStart, myKEnd) / pi;
393 LO pk = std::distance(myBlockStart, myBlockEnd) / (pj*pi);
394
395 // We also look for the index of the local rank in the current block.
396 LO myRankIndex = std::distance(myGeo->meshData.begin(),
397 std::find_if(myBlockStart, myBlockEnd,
398 [myRank](const std::vector<GO>& vec)->bool{
399 return (vec[0] == myRank) ? true : false;
400 })
401 );
402
403 for(int dim = 0; dim < 3; ++dim) {
404 if(dim < myGeo->numDimensions) {
405 myGeo->offsets[dim]= Teuchos::as<LO>(myGeo->startIndices[dim]) % myGeo->coarseRate[dim];
406 myGeo->offsets[dim + 3]= Teuchos::as<LO>(myGeo->startIndices[dim]) % myGeo->coarseRate[dim];
407 }
408 }
409
410 // Check if the partition contains nodes on a boundary, if so that boundary (face, line or
411 // point) will not require ghost nodes.
412 for(int dim = 0; dim < 3; ++dim) {
413 if(dim < myGeo->numDimensions && (myGeo->startIndices[dim] % myGeo->coarseRate[dim] != 0 ||
414 myGeo->startIndices[dim] == myGeo->gFineNodesPerDir[dim]-1)) {
415 myGeo->ghostInterface[2*dim] = true;
416 }
417 if(dim < myGeo->numDimensions
418 && myGeo->startIndices[dim + 3] != myGeo->gFineNodesPerDir[dim] - 1
419 && (myGeo->lFineNodesPerDir[dim] == 1 ||
420 myGeo->startIndices[dim + 3] % myGeo->coarseRate[dim] != 0)) {
421 myGeo->ghostInterface[2*dim+1] = true;
422 }
423 }
424
425 // Here one element can represent either the degenerate case of one node or the more general
426 // case of two nodes, i.e. x---x is a 1D element with two nodes and x is a 1D element with one
427 // node. This helps generating a 3D space from tensorial products...
428 // A good way to handle this would be to generalize the algorithm to take into account the
429 // discretization order used in each direction, at least in the FEM sense, since a 0 degree
430 // discretization will have a unique node per element. This way 1D discretization can be viewed
431 // as a 3D problem with one 0 degree element in the y direction and one 0 degre element in the z
432 // direction.
433 // !!! Operations below are aftecting both local and global values that have two different !!!
434 // orientations. Orientations can be interchanged using mapDirG2L and mapDirL2G. coarseRate,
435 // endRate and offsets are in the global basis, as well as all the variables starting with a g,
436 // !!! while the variables starting with an l are in the local basis. !!!
437 for(int i = 0; i < 3; ++i) {
438 if(i < myGeo->numDimensions) {
439 // This array is passed to the RAPFactory and eventually becomes gFineNodePerDir on the next
440 // level.
441 myGeo->gCoarseNodesPerDir[i] = (myGeo->gFineNodesPerDir[i] - 1) / myGeo->coarseRate[i];
442 myGeo->endRate[i] = Teuchos::as<LO>((myGeo->gFineNodesPerDir[i] - 1) %myGeo->coarseRate[i]);
443 if(myGeo->endRate[i] == 0) {
444 myGeo->endRate[i] = myGeo->coarseRate[i];
445 ++myGeo->gCoarseNodesPerDir[i];
446 } else {
447 myGeo->gCoarseNodesPerDir[i] += 2;
448 }
449 } else {
450 myGeo->endRate[i] = 1;
451 myGeo->gCoarseNodesPerDir[i] = 1;
452 }
453 }
454
455 myGeo->gNumCoarseNodes = myGeo->gCoarseNodesPerDir[0]*myGeo->gCoarseNodesPerDir[1]
456 *myGeo->gCoarseNodesPerDir[2];
457
458 for(LO i = 0; i < 3; ++i) {
459 if(i < myGeo->numDimensions) {
460 // Check whether the partition includes the "end" of the mesh which means that endRate will
461 // apply. Also make sure that endRate is not 0 which means that the mesh does not require a
462 // particular treatment at the boundaries.
463 if( (myGeo->startIndices[i] + myGeo->lFineNodesPerDir[i]) == myGeo->gFineNodesPerDir[i] ) {
464 myGeo->lCoarseNodesPerDir[i] = (myGeo->lFineNodesPerDir[i] - myGeo->endRate[i]
465 + myGeo->offsets[i] - 1) / myGeo->coarseRate[i] + 1;
466 if(myGeo->offsets[i] == 0) {++myGeo->lCoarseNodesPerDir[i];}
467 } else {
468 myGeo->lCoarseNodesPerDir[i] = (myGeo->lFineNodesPerDir[i] + myGeo->offsets[i] - 1)
469 / myGeo->coarseRate[i];
470 if(myGeo->offsets[i] == 0) {++myGeo->lCoarseNodesPerDir[i];}
471 }
472 } else {
473 myGeo->lCoarseNodesPerDir[i] = 1;
474 }
475 // This would happen if the rank does not own any nodes but in that case a subcommunicator
476 // should be used so this should really not be a concern.
477 if(myGeo->lFineNodesPerDir[i] < 1) {myGeo->lCoarseNodesPerDir[i] = 0;}
478 }
479
480 // Assuming linear interpolation, each fine point has contribution from 8 coarse points
481 // and each coarse point value gets injected.
482 // For systems of PDEs we assume that all dofs have the same P operator.
483 myGeo->lNumCoarseNodes = myGeo->lCoarseNodesPerDir[0]*myGeo->lCoarseNodesPerDir[1]
484 *myGeo->lCoarseNodesPerDir[2];
485
486 // For each direction, determine how many points (including ghosts) are required.
487 for(int dim = 0; dim < 3; ++dim) {
488 // The first branch of this if-statement will be used if the rank contains only one layer
489 // of nodes in direction i, that layer must also coincide with the boundary of the mesh
490 // and coarseRate[i] == endRate[i]...
491 if(myGeo->startIndices[dim] == myGeo->gFineNodesPerDir[dim] - 1 &&
492 myGeo->startIndices[dim] % myGeo->coarseRate[dim] == 0) {
493 myGeo->startGhostedCoarseNode[dim] = myGeo->startIndices[dim] / myGeo->coarseRate[dim] - 1;
494 } else {
495 myGeo->startGhostedCoarseNode[dim] = myGeo->startIndices[dim] / myGeo->coarseRate[dim];
496 }
497 myGeo->ghostedCoarseNodesPerDir[dim] = myGeo->lCoarseNodesPerDir[dim];
498 // Check whether face *low needs ghost nodes
499 if(myGeo->ghostInterface[2*dim]) {myGeo->ghostedCoarseNodesPerDir[dim] += 1;}
500 // Check whether face *hi needs ghost nodes
501 if(myGeo->ghostInterface[2*dim + 1]) {myGeo->ghostedCoarseNodesPerDir[dim] += 1;}
502 }
503 myGeo->lNumGhostedNodes = myGeo->ghostedCoarseNodesPerDir[2]*myGeo->ghostedCoarseNodesPerDir[1]
504 *myGeo->ghostedCoarseNodesPerDir[0];
505 myGeo->lNumGhostNodes = myGeo->lNumGhostedNodes - myGeo->lNumCoarseNodes;
506 ghostedCoarseNodes->PIDs.resize(myGeo->lNumGhostedNodes);
507 ghostedCoarseNodes->LIDs.resize(myGeo->lNumGhostedNodes);
508 ghostedCoarseNodes->GIDs.resize(myGeo->lNumGhostedNodes);
509 ghostedCoarseNodes->coarseGIDs.resize(myGeo->lNumGhostedNodes);
510 ghostedCoarseNodes->colInds.resize(myGeo->lNumGhostedNodes);
511 lCoarseNodesGIDs[0].resize(myGeo->lNumCoarseNodes);
512 lCoarseNodesGIDs[1].resize(myGeo->lNumCoarseNodes);
513
514 // Now the tricky part starts, the coarse nodes / ghosted coarse nodes need to be imported.
515 // This requires finding what their GID on the fine mesh is. They need to be ordered
516 // lexicographically to allow for fast sweeps through the mesh.
517
518 // We loop over all ghosted coarse nodes by increasing global lexicographic order
519 Array<LO> coarseNodeCoarseIndices(3), coarseNodeFineIndices(3);
520 LO currentIndex = -1, countCoarseNodes = 0;
521 for(int k = 0; k < myGeo->ghostedCoarseNodesPerDir[2]; ++k) {
522 for(int j = 0; j < myGeo->ghostedCoarseNodesPerDir[1]; ++j) {
523 for(int i = 0; i < myGeo->ghostedCoarseNodesPerDir[0]; ++i) {
524 currentIndex = k*myGeo->ghostedCoarseNodesPerDir[1]*myGeo->ghostedCoarseNodesPerDir[0]
525 + j*myGeo->ghostedCoarseNodesPerDir[0] + i;
526 coarseNodeCoarseIndices[0] = myGeo->startGhostedCoarseNode[0] + i;
527 coarseNodeFineIndices[0] = coarseNodeCoarseIndices[0]*myGeo->coarseRate[0];
528 if(coarseNodeFineIndices[0] > myGeo->gFineNodesPerDir[0] - 1) {
529 coarseNodeFineIndices[0] = myGeo->gFineNodesPerDir[0] - 1;
530 }
531 coarseNodeCoarseIndices[1] = myGeo->startGhostedCoarseNode[1] + j;
532 coarseNodeFineIndices[1] = coarseNodeCoarseIndices[1]*myGeo->coarseRate[1];
533 if(coarseNodeFineIndices[1] > myGeo->gFineNodesPerDir[1] - 1) {
534 coarseNodeFineIndices[1] = myGeo->gFineNodesPerDir[1] - 1;
535 }
536 coarseNodeCoarseIndices[2] = myGeo->startGhostedCoarseNode[2] + k;
537 coarseNodeFineIndices[2] = coarseNodeCoarseIndices[2]*myGeo->coarseRate[2];
538 if(coarseNodeFineIndices[2] > myGeo->gFineNodesPerDir[2] - 1) {
539 coarseNodeFineIndices[2] = myGeo->gFineNodesPerDir[2] - 1;
540 }
541 GO myGID = -1, myCoarseGID = -1;
542 LO myLID = -1, myPID = -1;
543 GetGIDLocalLexicographic(i, j, k, coarseNodeFineIndices, myGeo, myRankIndex, pi, pj, pk,
544 myBlockStart, myBlockEnd, myGID, myPID, myLID);
545 myCoarseGID = coarseNodeCoarseIndices[0]
546 + coarseNodeCoarseIndices[1]*myGeo->gCoarseNodesPerDir[0]
547 + coarseNodeCoarseIndices[2]*myGeo->gCoarseNodesPerDir[1]*myGeo->gCoarseNodesPerDir[0];
548 ghostedCoarseNodes->PIDs[currentIndex] = myPID;
549 ghostedCoarseNodes->LIDs[currentIndex] = myLID;
550 ghostedCoarseNodes->GIDs[currentIndex] = myGID;
551 ghostedCoarseNodes->coarseGIDs[currentIndex] = myCoarseGID;
552 if(myPID == myRank){
553 lCoarseNodesGIDs[0][countCoarseNodes] = myCoarseGID;
554 lCoarseNodesGIDs[1][countCoarseNodes] = myGID;
555 ++countCoarseNodes;
556 }
557 }
558 }
559 }
560
561 } // End MeshLayoutInterface
562
563 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
565 GetCoarsePoints(const int /* interpolationOrder */, const LO /* blkSize */, RCP<const Map> fineCoordsMap,
566 RCP<GeometricData> myGeo, RCP<NodesIDs> ghostedCoarseNodes,
567 Array<Array<GO> >& lCoarseNodesGIDs) const{
568 // Assuming perfect global lexicographic ordering of the mesh, produce two arrays:
569 // 1) lGhostNodesIDs that stores PID, LID, GID and coarseGID associated with the coarse nodes
570 // need to compute the local part of the prolongator.
571 // 2) lCoarseNodesGIDs that stores the GIDs associated with the local nodes needed to create
572 // the map of the MultiVector of coarse node coordinates.
573
574 {
575 GO tmp = 0;
576 myGeo->startIndices[2] = fineCoordsMap->getMinGlobalIndex()
577 / (myGeo->gFineNodesPerDir[1]*myGeo->gFineNodesPerDir[0]);
578 tmp = fineCoordsMap->getMinGlobalIndex()
579 % (myGeo->gFineNodesPerDir[1]*myGeo->gFineNodesPerDir[0]);
580 myGeo->startIndices[1] = tmp / myGeo->gFineNodesPerDir[0];
581 myGeo->startIndices[0] = tmp % myGeo->gFineNodesPerDir[0];
582 } // End of scope for tmp
583 for(int dim = 0; dim < 3; ++dim) {
584 myGeo->startIndices[dim + 3] = myGeo->startIndices[dim] + myGeo->lFineNodesPerDir[dim] - 1;
585 }
586
587 for(int dim = 0; dim < 3; ++dim) {
588 if(dim < myGeo->numDimensions) {
589 myGeo->offsets[dim]= Teuchos::as<LO>(myGeo->startIndices[dim]) % myGeo->coarseRate[dim];
590 myGeo->offsets[dim + 3]= Teuchos::as<LO>(myGeo->startIndices[dim]) % myGeo->coarseRate[dim];
591 }
592 }
593
594 // Check if the partition contains nodes on a boundary, if so that boundary (face, line or
595 // point) will not require ghost nodes, unless there is only one node in that direction.
596 for(int dim = 0; dim < 3; ++dim) {
597 if(dim < myGeo->numDimensions && (myGeo->startIndices[dim] % myGeo->coarseRate[dim] != 0 ||
598 myGeo->startIndices[dim] == myGeo->gFineNodesPerDir[dim]-1)) {
599 myGeo->ghostInterface[2*dim] = true;
600 }
601 if(dim < myGeo->numDimensions
602 && myGeo->startIndices[dim + 3] != myGeo->gFineNodesPerDir[dim] - 1
603 && (myGeo->lFineNodesPerDir[dim] == 1 ||
604 myGeo->startIndices[dim + 3] % myGeo->coarseRate[dim] != 0)) {
605 myGeo->ghostInterface[2*dim + 1] = true;
606 }
607 }
608
609 // Here one element can represent either the degenerate case of one node or the more general
610 // case of two nodes, i.e. x---x is a 1D element with two nodes and x is a 1D element with one
611 // node. This helps generating a 3D space from tensorial products...
612 // A good way to handle this would be to generalize the algorithm to take into account the
613 // discretization order used in each direction, at least in the FEM sense, since a 0 degree
614 // discretization will have a unique node per element. This way 1D discretization can be viewed
615 // as a 3D problem with one 0 degree element in the y direction and one 0 degre element in the z
616 // direction.
617 // !!! Operations below are aftecting both local and global values that have two different !!!
618 // orientations. Orientations can be interchanged using mapDirG2L and mapDirL2G. coarseRate,
619 // endRate and offsets are in the global basis, as well as all the variables starting with a g,
620 // !!! while the variables starting with an l are in the local basis. !!!
621 for(int i = 0; i < 3; ++i) {
622 if(i < myGeo->numDimensions) {
623 // This array is passed to the RAPFactory and eventually becomes gFineNodePerDir on the next
624 // level.
625 myGeo->gCoarseNodesPerDir[i] = (myGeo->gFineNodesPerDir[i] - 1) / myGeo->coarseRate[i];
626 myGeo->endRate[i] = Teuchos::as<LO>((myGeo->gFineNodesPerDir[i] - 1) %myGeo->coarseRate[i]);
627 if(myGeo->endRate[i] == 0) {
628 myGeo->endRate[i] = myGeo->coarseRate[i];
629 ++myGeo->gCoarseNodesPerDir[i];
630 } else {
631 myGeo->gCoarseNodesPerDir[i] += 2;
632 }
633 } else {
634 myGeo->endRate[i] = 1;
635 myGeo->gCoarseNodesPerDir[i] = 1;
636 }
637 }
638
639 myGeo->gNumCoarseNodes = myGeo->gCoarseNodesPerDir[0]*myGeo->gCoarseNodesPerDir[1]
640 *myGeo->gCoarseNodesPerDir[2];
641
642 for(LO i = 0; i < 3; ++i) {
643 if(i < myGeo->numDimensions) {
644 // Check whether the partition includes the "end" of the mesh which means that endRate will
645 // apply. Also make sure that endRate is not 0 which means that the mesh does not require a
646 // particular treatment at the boundaries.
647 if( (myGeo->startIndices[i] + myGeo->lFineNodesPerDir[i]) == myGeo->gFineNodesPerDir[i] ) {
648 myGeo->lCoarseNodesPerDir[i] = (myGeo->lFineNodesPerDir[i] - myGeo->endRate[i]
649 + myGeo->offsets[i] - 1) / myGeo->coarseRate[i] + 1;
650 if(myGeo->offsets[i] == 0) {++myGeo->lCoarseNodesPerDir[i];}
651 } else {
652 myGeo->lCoarseNodesPerDir[i] = (myGeo->lFineNodesPerDir[i] + myGeo->offsets[i] - 1)
653 / myGeo->coarseRate[i];
654 if(myGeo->offsets[i] == 0) {++myGeo->lCoarseNodesPerDir[i];}
655 }
656 } else {
657 myGeo->lCoarseNodesPerDir[i] = 1;
658 }
659 // This would happen if the rank does not own any nodes but in that case a subcommunicator
660 // should be used so this should really not be a concern.
661 if(myGeo->lFineNodesPerDir[i] < 1) {myGeo->lCoarseNodesPerDir[i] = 0;}
662 }
663
664 // Assuming linear interpolation, each fine point has contribution from 8 coarse points
665 // and each coarse point value gets injected.
666 // For systems of PDEs we assume that all dofs have the same P operator.
667 myGeo->lNumCoarseNodes = myGeo->lCoarseNodesPerDir[0]*myGeo->lCoarseNodesPerDir[1]
668 *myGeo->lCoarseNodesPerDir[2];
669
670 // For each direction, determine how many points (including ghosts) are required.
671 bool ghostedDir[6] = {false};
672 for(int dim = 0; dim < 3; ++dim) {
673 // The first branch of this if-statement will be used if the rank contains only one layer
674 // of nodes in direction i, that layer must also coincide with the boundary of the mesh
675 // and coarseRate[i] == endRate[i]...
676 if(myGeo->startIndices[dim] == myGeo->gFineNodesPerDir[dim] - 1 &&
677 myGeo->startIndices[dim] % myGeo->coarseRate[dim] == 0) {
678 myGeo->startGhostedCoarseNode[dim] = myGeo->startIndices[dim] / myGeo->coarseRate[dim] - 1;
679 } else {
680 myGeo->startGhostedCoarseNode[dim] = myGeo->startIndices[dim] / myGeo->coarseRate[dim];
681 }
682 myGeo->ghostedCoarseNodesPerDir[dim] = myGeo->lCoarseNodesPerDir[dim];
683 // Check whether face *low needs ghost nodes
684 if(myGeo->ghostInterface[2*dim]) {
685 myGeo->ghostedCoarseNodesPerDir[dim] += 1;
686 ghostedDir[2*dim] = true;
687 }
688 // Check whether face *hi needs ghost nodes
689 if(myGeo->ghostInterface[2*dim + 1]) {
690 myGeo->ghostedCoarseNodesPerDir[dim] += 1;
691 ghostedDir[2*dim + 1] = true;
692 }
693 }
694 myGeo->lNumGhostedNodes = myGeo->ghostedCoarseNodesPerDir[2]*myGeo->ghostedCoarseNodesPerDir[1]
695 *myGeo->ghostedCoarseNodesPerDir[0];
696 myGeo->lNumGhostNodes = myGeo->lNumGhostedNodes - myGeo->lNumCoarseNodes;
697 ghostedCoarseNodes->PIDs.resize(myGeo->lNumGhostedNodes);
698 ghostedCoarseNodes->LIDs.resize(myGeo->lNumGhostedNodes);
699 ghostedCoarseNodes->GIDs.resize(myGeo->lNumGhostedNodes);
700 ghostedCoarseNodes->coarseGIDs.resize(myGeo->lNumGhostedNodes);
701 ghostedCoarseNodes->colInds.resize(myGeo->lNumGhostedNodes);
702 lCoarseNodesGIDs[0].resize(myGeo->lNumCoarseNodes);
703 lCoarseNodesGIDs[1].resize(myGeo->lNumCoarseNodes);
704
705 // Now the tricky part starts, the coarse nodes / ghosted coarse nodes need to be imported.
706 // This requires finding what their GID on the fine mesh is. They need to be ordered
707 // lexicographically to allow for fast sweeps through the mesh.
708
709 // We loop over all ghosted coarse nodes by increasing global lexicographic order
710 Array<LO> coarseNodeCoarseIndices(3), coarseNodeFineIndices(3), ijk(3);
711 LO currentIndex = -1, countCoarseNodes = 0;
712 for(ijk[2] = 0; ijk[2] < myGeo->ghostedCoarseNodesPerDir[2]; ++ijk[2]) {
713 for(ijk[1] = 0; ijk[1] < myGeo->ghostedCoarseNodesPerDir[1]; ++ijk[1]) {
714 for(ijk[0] = 0; ijk[0] < myGeo->ghostedCoarseNodesPerDir[0]; ++ijk[0]) {
715 currentIndex = ijk[2]*myGeo->ghostedCoarseNodesPerDir[1]*myGeo->ghostedCoarseNodesPerDir[0]
716 + ijk[1]*myGeo->ghostedCoarseNodesPerDir[0] + ijk[0];
717 coarseNodeCoarseIndices[0] = myGeo->startGhostedCoarseNode[0] + ijk[0];
718 coarseNodeFineIndices[0] = coarseNodeCoarseIndices[0]*myGeo->coarseRate[0];
719 if(coarseNodeFineIndices[0] > myGeo->gFineNodesPerDir[0] - 1) {
720 coarseNodeFineIndices[0] = myGeo->gFineNodesPerDir[0] - 1;
721 }
722 coarseNodeCoarseIndices[1] = myGeo->startGhostedCoarseNode[1] + ijk[1];
723 coarseNodeFineIndices[1] = coarseNodeCoarseIndices[1]*myGeo->coarseRate[1];
724 if(coarseNodeFineIndices[1] > myGeo->gFineNodesPerDir[1] - 1) {
725 coarseNodeFineIndices[1] = myGeo->gFineNodesPerDir[1] - 1;
726 }
727 coarseNodeCoarseIndices[2] = myGeo->startGhostedCoarseNode[2] + ijk[2];
728 coarseNodeFineIndices[2] = coarseNodeCoarseIndices[2]*myGeo->coarseRate[2];
729 if(coarseNodeFineIndices[2] > myGeo->gFineNodesPerDir[2] - 1) {
730 coarseNodeFineIndices[2] = myGeo->gFineNodesPerDir[2] - 1;
731 }
732 GO myGID = 0, myCoarseGID = -1;
733 GO factor[3] = {};
734 factor[2] = myGeo->gNumFineNodes10;
735 factor[1] = myGeo->gFineNodesPerDir[0];
736 factor[0] = 1;
737 for(int dim = 0; dim < 3; ++dim) {
738 if(dim < myGeo->numDimensions) {
739 if(myGeo->startIndices[dim] - myGeo->offsets[dim] + ijk[dim]*myGeo->coarseRate[dim]
740 < myGeo->gFineNodesPerDir[dim] - 1) {
741 myGID += (myGeo->startIndices[dim] - myGeo->offsets[dim]
742 + ijk[dim]*myGeo->coarseRate[dim])*factor[dim];
743 } else {
744 myGID += (myGeo->startIndices[dim] - myGeo->offsets[dim]
745 + (ijk[dim] - 1)*myGeo->coarseRate[dim] + myGeo->endRate[dim])*factor[dim];
746 }
747 }
748 }
749 myCoarseGID = coarseNodeCoarseIndices[0]
750 + coarseNodeCoarseIndices[1]*myGeo->gCoarseNodesPerDir[0]
751 + coarseNodeCoarseIndices[2]*myGeo->gCoarseNodesPerDir[1]*myGeo->gCoarseNodesPerDir[0];
752 ghostedCoarseNodes->GIDs[currentIndex] = myGID;
753 ghostedCoarseNodes->coarseGIDs[currentIndex] = myCoarseGID;
754 if((!ghostedDir[0] || ijk[0] != 0)
755 && (!ghostedDir[2] || ijk[1] != 0)
756 && (!ghostedDir[4] || ijk[2] != 0)
757 && (!ghostedDir[1] || ijk[0] != myGeo->ghostedCoarseNodesPerDir[0] - 1)
758 && (!ghostedDir[3] || ijk[1] != myGeo->ghostedCoarseNodesPerDir[1] - 1)
759 && (!ghostedDir[5] || ijk[2] != myGeo->ghostedCoarseNodesPerDir[2] - 1)){
760 lCoarseNodesGIDs[0][countCoarseNodes] = myCoarseGID;
761 lCoarseNodesGIDs[1][countCoarseNodes] = myGID;
762 ++countCoarseNodes;
763 }
764 }
765 }
766 }
767 Array<int> ghostsPIDs(myGeo->lNumGhostedNodes);
768 Array<LO> ghostsLIDs(myGeo->lNumGhostedNodes);
769 fineCoordsMap->getRemoteIndexList(ghostedCoarseNodes->GIDs(),
770 ghostedCoarseNodes->PIDs(),
771 ghostedCoarseNodes->LIDs());
772 } // End GetCoarsePoint
773
774 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
776 MakeGeneralGeometricP(RCP<GeometricData> myGeo,
777 const RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType,LO,GO,Node> >& fineCoords,
778 const LO nnzP, const LO dofsPerNode,
779 RCP<const Map>& stridedDomainMapP,RCP<Matrix> & Amat, RCP<Matrix>& P,
780 RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType,LO,GO,Node> >& coarseCoords,
781 RCP<NodesIDs> ghostedCoarseNodes, Array<Array<GO> > coarseNodesGIDs,
782 int interpolationOrder) const {
783
784 /* On termination, return the number of local prolongator columns owned by
785 * this processor.
786 *
787 * Input
788 * =====
789 * nNodes Number of fine level Blk Rows owned by this processor
790 * coarseRate Rate of coarsening in each spatial direction.
791 * endRate Rate of coarsening in each spatial direction for the last
792 * nodes in the mesh where an adaptive coarsening rate is
793 * required.
794 * nTerms Number of nonzero entries in the prolongation matrix.
795 * dofsPerNode Number of degrees-of-freedom per mesh node.
796 *
797 * Output
798 * =====
799 * So far nothing...
800 */
801
802 using xdMV = Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType,LO,GO,NO>;
803 Xpetra::global_size_t OTI = Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid();
804
805 LO myRank = Amat->getRowMap()->getComm()->getRank();
806 GO numGloCols = dofsPerNode*myGeo->gNumCoarseNodes;
807
808 // Build maps necessary to create and fill complete the prolongator
809 // note: rowMapP == rangeMapP and colMapP != domainMapP
810 RCP<const Map> rowMapP = Amat->getDomainMap();
811
812 RCP<const Map> domainMapP;
813
814 RCP<const Map> colMapP;
815
816 // At this point we need to create the column map which is a delicate operation.
817 // The entries in that map need to be ordered as follows:
818 // 1) first owned entries ordered by LID
819 // 2) second order the remaining entries by PID
820 // 3) entries with the same remote PID are ordered by GID.
821 // One piece of good news: myGeo->lNumCoarseNodes is the number of ownedNodes and
822 // myGeo->lNumGhostNodes is the number of remote nodes. The sorting can be limited to remote
823 // nodes as the owned ones are alreaded ordered by LID!
824
825 {
826 std::vector<NodeID> colMapOrdering(myGeo->lNumGhostedNodes);
827 for(LO ind = 0; ind < myGeo->lNumGhostedNodes; ++ind) {
828 colMapOrdering[ind].GID = ghostedCoarseNodes->GIDs[ind];
829 if(ghostedCoarseNodes->PIDs[ind] == myRank) {
830 colMapOrdering[ind].PID = -1;
831 } else {
832 colMapOrdering[ind].PID = ghostedCoarseNodes->PIDs[ind];
833 }
834 colMapOrdering[ind].LID = ghostedCoarseNodes->LIDs[ind];
835 colMapOrdering[ind].lexiInd = ind;
836 }
837 std::sort(colMapOrdering.begin(), colMapOrdering.end(),
838 [](NodeID a, NodeID b)->bool{
839 return ( (a.PID < b.PID) || ((a.PID == b.PID) && (a.GID < b.GID)) );
840 });
841
842 Array<GO> colGIDs(dofsPerNode*myGeo->lNumGhostedNodes);
843 for(LO ind = 0; ind < myGeo->lNumGhostedNodes; ++ind) {
844 // Save the permutation calculated to go from Lexicographic indexing to column map indexing
845 ghostedCoarseNodes->colInds[colMapOrdering[ind].lexiInd] = ind;
846 for(LO dof = 0; dof < dofsPerNode; ++dof) {
847 colGIDs[dofsPerNode*ind + dof] = dofsPerNode*colMapOrdering[ind].GID + dof;
848 }
849 }
850 domainMapP = Xpetra::MapFactory<LO,GO,NO>::Build(rowMapP->lib(),
851 numGloCols,
852 colGIDs.view(0,dofsPerNode*
853 myGeo->lNumCoarseNodes),
854 rowMapP->getIndexBase(),
855 rowMapP->getComm());
856 colMapP = Xpetra::MapFactory<LO,GO,NO>::Build(rowMapP->lib(),
857 OTI,
858 colGIDs.view(0, colGIDs.size()),
859 rowMapP->getIndexBase(),
860 rowMapP->getComm());
861 } // End of scope for colMapOrdering and colGIDs
862
863 std::vector<size_t> strideInfo(1);
864 strideInfo[0] = dofsPerNode;
865 stridedDomainMapP = Xpetra::StridedMapFactory<LO,GO,NO>::Build(domainMapP, strideInfo);
866
867 // Build the map for the coarse level coordinates, create the associated MultiVector and fill it
868 // with an import from the fine coordinates MultiVector. As data is local this should not create
869 // communications during the importer creation.
870 RCP<const Map> coarseCoordsMap = MapFactory::Build (fineCoords->getMap()->lib(),
871 myGeo->gNumCoarseNodes,
872 coarseNodesGIDs[0](),
873 fineCoords->getMap()->getIndexBase(),
874 fineCoords->getMap()->getComm());
875 RCP<const Map> coarseCoordsFineMap = MapFactory::Build (fineCoords->getMap()->lib(),
876 myGeo->gNumCoarseNodes,
877 coarseNodesGIDs[1](),
878 fineCoords->getMap()->getIndexBase(),
879 fineCoords->getMap()->getComm());
880
881 RCP<const Import> coarseImporter = ImportFactory::Build(fineCoords->getMap(),
882 coarseCoordsFineMap);
883 coarseCoords = Xpetra::MultiVectorFactory<typename Teuchos::ScalarTraits<Scalar>::coordinateType,LO,GO,NO>::Build(coarseCoordsFineMap,
884 myGeo->numDimensions);
885 coarseCoords->doImport(*fineCoords, *coarseImporter, Xpetra::INSERT);
886 coarseCoords->replaceMap(coarseCoordsMap);
887
888 // Do the actual import using the fineCoords->getMap()
889 RCP<const Map> ghostMap = Xpetra::MapFactory<LO,GO,NO>::Build(fineCoords->getMap()->lib(),
890 OTI,
891 ghostedCoarseNodes->GIDs(),
892 fineCoords->getMap()->getIndexBase(),
893 fineCoords->getMap()->getComm());
894 RCP<const Import> ghostImporter = ImportFactory::Build(fineCoords->getMap(), ghostMap);
895 RCP<xdMV> ghostCoords = Xpetra::MultiVectorFactory<typename Teuchos::ScalarTraits<Scalar>::coordinateType,LO,GO,NO>::Build(ghostMap,
896 myGeo->numDimensions);
897 ghostCoords->doImport(*fineCoords, *ghostImporter, Xpetra::INSERT);
898
899 P = rcp(new CrsMatrixWrap(rowMapP, colMapP, 0));
900 RCP<CrsMatrix> PCrs = rcp_dynamic_cast<CrsMatrixWrap>(P)->getCrsMatrix();
901
902 ArrayRCP<size_t> iaP;
903 ArrayRCP<LO> jaP;
904 ArrayRCP<SC> valP;
905
906 PCrs->allocateAllValues(nnzP, iaP, jaP, valP);
907
908 ArrayView<size_t> ia = iaP();
909 ArrayView<LO> ja = jaP();
910 ArrayView<SC> val = valP();
911 ia[0] = 0;
912
913 Array<ArrayRCP<typename Teuchos::ScalarTraits<Scalar>::coordinateType> > ghostedCoords(3);
914 {
915 ArrayRCP<typename Teuchos::ScalarTraits<Scalar>::coordinateType> tmp(ghostCoords->getLocalLength(), 0.0);
916 for(int dim = 0; dim < 3; ++dim) {
917 if(dim < myGeo->numDimensions) {
918 ghostedCoords[dim] = ghostCoords->getDataNonConst(dim);
919 } else {
920 ghostedCoords[dim] = tmp;
921 }
922 }
923 }
924
925 // Declaration and assignment of fineCoords which holds the coordinates of the fine nodes in 3D.
926 // To do so we pull the nD coordinates from fineCoords and pad the rest with zero vectors...
927 RCP<Xpetra::Vector<typename Teuchos::ScalarTraits<Scalar>::coordinateType,LO,GO,NO> > zeros
928 = Xpetra::VectorFactory<typename Teuchos::ScalarTraits<Scalar>::coordinateType,LO,GO,NO>::Build(fineCoords->getMap(), true);
929 ArrayRCP< ArrayRCP<typename Teuchos::ScalarTraits<Scalar>::coordinateType> > lFineCoords(3);
930 for(int dim = 0; dim < 3; ++dim) {
931 if(dim < myGeo->numDimensions) {
932 lFineCoords[dim] = fineCoords->getDataNonConst(dim);
933 } else {
934 lFineCoords[dim] = zeros->getDataNonConst(0);
935 }
936 }
937
938 GO tStencil = 0;
939 for(int currentIndex = 0; currentIndex < myGeo->lNumFineNodes; ++currentIndex) {
940 Array<GO> ghostedIndices(3), firstInterpolationIndices(3);
941 Array<LO> interpolationPIDs(8), interpolationLIDs(8), interpolationGIDs(8);
942 Array<Array<typename Teuchos::ScalarTraits<Scalar>::coordinateType> > interpolationCoords(9);
943 interpolationCoords[0].resize(3);
944 GO firstInterpolationNodeIndex;
945 int nStencil = 0;
946 for(int dim = 0; dim < 3; ++dim) {
947 interpolationCoords[0][dim] = lFineCoords[dim][currentIndex];
948 }
949
950 // Compute the ghosted (i,j,k) of the current node, that assumes (I,J,K)=(0,0,0) to be
951 // associated with the first node in ghostCoords
952 {// Scope for tmp
953 ghostedIndices[2] = currentIndex / (myGeo->lFineNodesPerDir[1]*myGeo->lFineNodesPerDir[0]);
954 LO tmp = currentIndex % (myGeo->lFineNodesPerDir[1]*myGeo->lFineNodesPerDir[0]);
955 ghostedIndices[1] = tmp / myGeo->lFineNodesPerDir[0];
956 ghostedIndices[0] = tmp % myGeo->lFineNodesPerDir[0];
957 for(int dim = 0; dim < 3; ++dim) {
958 ghostedIndices[dim] += myGeo->offsets[dim];
959 }
960 // A special case appears when the mesh is really coarse: it is possible for a rank to
961 // have a single coarse node in a given direction. If this happens on the highest i, j or k
962 // we need to "grab" a coarse node with a lower i, j, or k which leads us to add to the
963 // value of ghostedIndices
964 }
965 // No we find the indices of the coarse nodes used for interpolation simply by integer
966 // division.
967 for(int dim = 0; dim < 3; ++dim) {
968 firstInterpolationIndices[dim] = ghostedIndices[dim] / myGeo->coarseRate[dim];
969 // If you are on the edge of the local domain go back one coarse node, unless there is only
970 // one node on the local domain...
971 if(firstInterpolationIndices[dim] == myGeo->ghostedCoarseNodesPerDir[dim] - 1
972 && myGeo->ghostedCoarseNodesPerDir[dim] > 1) {
973 firstInterpolationIndices[dim] -= 1;
974 }
975 }
976 firstInterpolationNodeIndex =
977 firstInterpolationIndices[2]*myGeo->ghostedCoarseNodesPerDir[1]*myGeo->ghostedCoarseNodesPerDir[0]
978 + firstInterpolationIndices[1]*myGeo->ghostedCoarseNodesPerDir[0]
979 + firstInterpolationIndices[0];
980
981 // We extract the coordinates and PIDs associated with each coarse node used during
982 // inteprolation in order to fill the prolongator correctly
983 {
984 LO ind = -1;
985 for(int k = 0; k < 2; ++k) {
986 for(int j = 0; j < 2; ++j) {
987 for(int i = 0; i < 2; ++i) {
988 ind = k*4 + j*2 + i;
989 interpolationLIDs[ind] = firstInterpolationNodeIndex
990 + k*myGeo->ghostedCoarseNodesPerDir[1]*myGeo->ghostedCoarseNodesPerDir[0]
991 + j*myGeo->ghostedCoarseNodesPerDir[0] + i;
992 if(ghostedCoarseNodes->PIDs[interpolationLIDs[ind]] == rowMapP->getComm()->getRank()){
993 interpolationPIDs[ind] = -1;
994 } else {
995 interpolationPIDs[ind] = ghostedCoarseNodes->PIDs[interpolationLIDs[ind]];
996 }
997 interpolationGIDs[ind] = ghostedCoarseNodes->coarseGIDs[interpolationLIDs[ind]];
998
999 interpolationCoords[ind + 1].resize(3);
1000 for(int dim = 0; dim < 3; ++dim) {
1001 interpolationCoords[ind + 1][dim]
1002 = ghostedCoords[dim][interpolationLIDs[ind]];
1003 }
1004 }
1005 }
1006 }
1007 } // End of ind scope
1008
1009 // Compute the actual geometric interpolation stencil
1010 // LO stencilLength = static_cast<LO>(std::pow(interpolationOrder + 1, 3));
1011 std::vector<double> stencil(8);
1012 Array<GO> firstCoarseNodeFineIndices(3);
1013 int rate[3] = {};
1014 for(int dim = 0; dim < 3; ++dim) {
1015 firstCoarseNodeFineIndices[dim] = firstInterpolationIndices[dim]*myGeo->coarseRate[dim];
1016 if((myGeo->startIndices[dim + 3] == myGeo->gFineNodesPerDir[dim] - 1)
1017 && (ghostedIndices[dim] >=
1018 (myGeo->ghostedCoarseNodesPerDir[dim] - 2)*myGeo->coarseRate[dim])) {
1019 rate[dim] = myGeo->endRate[dim];
1020 } else {
1021 rate[dim] = myGeo->coarseRate[dim];
1022 }
1023 }
1024 Array<GO> trueGhostedIndices(3);
1025 // This handles the case of a rank having a single node that also happens to be the last node
1026 // in any direction. It might be more efficient to re-write the algo so that this is
1027 // incorporated in the definition of ghostedIndices directly...
1028 for(int dim = 0; dim < 3; ++dim) {
1029 if (myGeo->startIndices[dim] == myGeo->gFineNodesPerDir[dim] - 1) {
1030 trueGhostedIndices[dim] = ghostedIndices[dim] + rate[dim];
1031 } else {
1032 trueGhostedIndices[dim] = ghostedIndices[dim];
1033 }
1034 }
1035 ComputeStencil(myGeo->numDimensions, trueGhostedIndices, firstCoarseNodeFineIndices, rate,
1036 interpolationCoords, interpolationOrder, stencil);
1037
1038 // Finally check whether the fine node is on a coarse: node, edge or face
1039 // and select accordingly the non-zero values from the stencil and the
1040 // corresponding column indices
1041 Array<LO> nzIndStencil(8), permutation(8);
1042 for(LO k = 0; k < 8; ++k) {permutation[k] = k;}
1043 if(interpolationOrder == 0) {
1044 nStencil = 1;
1045 for(LO k = 0; k < 8; ++k) {
1046 nzIndStencil[k] = static_cast<LO>(stencil[0]);
1047 }
1048 stencil[0] = 0.0;
1049 stencil[nzIndStencil[0]] = 1.0;
1050 } else if(interpolationOrder == 1) {
1051 Array<GO> currentNodeGlobalFineIndices(3);
1052 for(int dim = 0; dim < 3; ++dim) {
1053 currentNodeGlobalFineIndices[dim] = ghostedIndices[dim] - myGeo->offsets[dim]
1054 + myGeo->startIndices[dim];
1055 }
1056 if( ((ghostedIndices[0] % myGeo->coarseRate[0] == 0)
1057 || currentNodeGlobalFineIndices[0] == myGeo->gFineNodesPerDir[0] - 1)
1058 && ((ghostedIndices[1] % myGeo->coarseRate[1] == 0)
1059 || currentNodeGlobalFineIndices[1] == myGeo->gFineNodesPerDir[1] - 1)
1060 && ((ghostedIndices[2] % myGeo->coarseRate[2] == 0)
1061 || currentNodeGlobalFineIndices[2] == myGeo->gFineNodesPerDir[2] - 1) ) {
1062 if((currentNodeGlobalFineIndices[0] == myGeo->gFineNodesPerDir[0] - 1) ||
1063 (ghostedIndices[0] / myGeo->coarseRate[0] == myGeo->ghostedCoarseNodesPerDir[0] - 1)) {
1064 nzIndStencil[0] += 1;
1065 }
1066 if(((currentNodeGlobalFineIndices[1] == myGeo->gFineNodesPerDir[1] - 1) ||
1067 (ghostedIndices[1] / myGeo->coarseRate[1] == myGeo->ghostedCoarseNodesPerDir[1] - 1))
1068 && (myGeo->numDimensions > 1)){
1069 nzIndStencil[0] += 2;
1070 }
1071 if(((currentNodeGlobalFineIndices[2] == myGeo->gFineNodesPerDir[2] - 1) ||
1072 (ghostedIndices[2] / myGeo->coarseRate[2] == myGeo->ghostedCoarseNodesPerDir[2] - 1))
1073 && myGeo->numDimensions > 2) {
1074 nzIndStencil[0] += 4;
1075 }
1076 nStencil = 1;
1077 for(LO k = 0; k < 8; ++k) {
1078 nzIndStencil[k] = nzIndStencil[0];
1079 }
1080 } else {
1081 nStencil = 8;
1082 for(LO k = 0; k < 8; ++k) {
1083 nzIndStencil[k] = k;
1084 }
1085 }
1086 }
1087
1088 // Here the values are filled in the Crs matrix arrays
1089 // This is basically the only place these variables are modified
1090 // Hopefully this makes handling system of PDEs easy!
1091
1092 // Loop on dofsPerNode and process each row for the current Node
1093
1094
1095 // Sort nodes by PIDs using stable sort to keep sublist ordered by LIDs and GIDs
1096 sh_sort2(interpolationPIDs.begin(),interpolationPIDs.end(),
1097 permutation.begin(), permutation.end());
1098
1099 GO colInd;
1100 for(LO j = 0; j < dofsPerNode; ++j) {
1101 ia[currentIndex*dofsPerNode + j + 1] = ia[currentIndex*dofsPerNode + j] + nStencil;
1102 for(LO k = 0; k < nStencil; ++k) {
1103 colInd = ghostedCoarseNodes->colInds[interpolationLIDs[nzIndStencil[permutation[k]]]];
1104 ja[ia[currentIndex*dofsPerNode + j] + k] = colInd*dofsPerNode + j;
1105 val[ia[currentIndex*dofsPerNode + j] + k] = stencil[nzIndStencil[permutation[k]]];
1106 }
1107 // Add the stencil for each degree of freedom.
1108 tStencil += nStencil;
1109 }
1110 } // End loop over fine nodes
1111
1112 if (rowMapP->lib() == Xpetra::UseTpetra) {
1113 // - Cannot resize for Epetra, as it checks for same pointers
1114 // - Need to resize for Tpetra, as it check ().size() == ia[numRows]
1115 // NOTE: these invalidate ja and val views
1116 jaP .resize(tStencil);
1117 valP.resize(tStencil);
1118 }
1119
1120 // Set the values of the prolongation operators into the CrsMatrix P and call FillComplete
1121 PCrs->setAllValues(iaP, jaP, valP);
1122 PCrs->expertStaticFillComplete(domainMapP,rowMapP);
1123 } // End MakeGeneralGeometricP
1124
1125 // template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1126 // void BlackBoxPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetGeometricData(
1127 // RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType,LO,GO,NO> >& coordinates, const Array<LO> coarseRate,
1128 // const Array<GO> gFineNodesPerDir, const Array<LO> lFineNodesPerDir, const LO BlkSize,
1129 // Array<GO>& gIndices, Array<LO>& myOffset, Array<bool>& ghostInterface, Array<LO>& endRate,
1130 // Array<GO>& gCoarseNodesPerDir, Array<LO>& lCoarseNodesPerDir, Array<GO>& ghostGIDs,
1131 // Array<GO>& coarseNodesGIDs, Array<GO>& colGIDs, GO& gNumCoarseNodes, LO& lNumCoarseNodes,
1132 // ArrayRCP<Array<double> > coarseNodes) const {
1133
1134 // RCP<const Map> coordinatesMap = coordinates->getMap();
1135 // LO numDimensions = coordinates->getNumVectors();
1136
1137 // // Using the coarsening rate and the fine level data,
1138 // // compute coarse level data
1139
1140 // // Phase 1 //
1141 // // ------------------------------------------------------------------ //
1142 // // We first start by finding small informations on the mesh such as //
1143 // // the number of coarse nodes (local and global) and the number of //
1144 // // ghost nodes / the end rate of coarsening. //
1145 // // ------------------------------------------------------------------ //
1146 // GO minGlobalIndex = coordinatesMap->getMinGlobalIndex();
1147 // {
1148 // GO tmp;
1149 // gIndices[2] = minGlobalIndex / (gFineNodesPerDir[1]*gFineNodesPerDir[0]);
1150 // tmp = minGlobalIndex % (gFineNodesPerDir[1]*gFineNodesPerDir[0]);
1151 // gIndices[1] = tmp / gFineNodesPerDir[0];
1152 // gIndices[0] = tmp % gFineNodesPerDir[0];
1153
1154 // myOffset[2] = gIndices[2] % coarseRate[2];
1155 // myOffset[1] = gIndices[1] % coarseRate[1];
1156 // myOffset[0] = gIndices[0] % coarseRate[0];
1157 // }
1158
1159 // // Check whether ghost nodes are needed in each direction
1160 // for(LO i=0; i < numDimensions; ++i) {
1161 // if((gIndices[i] != 0) && (gIndices[i] % coarseRate[i] > 0)) {
1162 // ghostInterface[2*i] = true;
1163 // }
1164 // if(((gIndices[i] + lFineNodesPerDir[i]) != gFineNodesPerDir[i]) && ((gIndices[i] + lFineNodesPerDir[i] - 1) % coarseRate[i] > 0)) {
1165 // ghostInterface[2*i + 1] = true;
1166 // }
1167 // }
1168
1169 // for(LO i = 0; i < 3; ++i) {
1170 // if(i < numDimensions) {
1171 // lCoarseNodesPerDir[i] = (lFineNodesPerDir[i] + myOffset[i] - 1) / coarseRate[i];
1172 // if(myOffset[i] == 0) { ++lCoarseNodesPerDir[i]; }
1173 // gCoarseNodesPerDir[i] = (gFineNodesPerDir[i] - 1) / coarseRate[i];
1174 // endRate[i] = (gFineNodesPerDir[i] - 1) % coarseRate[i];
1175 // if(endRate[i] == 0) {
1176 // ++gCoarseNodesPerDir[i];
1177 // endRate[i] = coarseRate[i];
1178 // }
1179 // } else {
1180 // // Most quantities need to be set to 1 for extra dimensions
1181 // // this is rather logical, an x-y plane is like a single layer
1182 // // of nodes in the z direction...
1183 // gCoarseNodesPerDir[i] = 1;
1184 // lCoarseNodesPerDir[i] = 1;
1185 // endRate[i] = 1;
1186 // }
1187 // }
1188
1189 // gNumCoarseNodes = gCoarseNodesPerDir[0]*gCoarseNodesPerDir[1]*gCoarseNodesPerDir[2];
1190 // lNumCoarseNodes = lCoarseNodesPerDir[0]*lCoarseNodesPerDir[1]*lCoarseNodesPerDir[2];
1191
1192 // // For each direction, determine how many ghost points are required.
1193 // LO lNumGhostNodes = 0;
1194 // {
1195 // const int complementaryIndices[3][2] = {{1,2}, {0,2}, {0,1}};
1196 // LO tmp = 0;
1197 // for(LO i = 0; i < 3; ++i) {
1198 // // Check whether a face in direction i needs ghost nodes
1199 // if(ghostInterface[2*i] || ghostInterface[2*i+1]) {
1200 // if(i == 0) {tmp = lCoarseNodesPerDir[1]*lCoarseNodesPerDir[2];}
1201 // if(i == 1) {tmp = lCoarseNodesPerDir[0]*lCoarseNodesPerDir[2];}
1202 // if(i == 2) {tmp = lCoarseNodesPerDir[0]*lCoarseNodesPerDir[1];}
1203 // }
1204 // // If both faces in direction i need nodes, double the number of ghost nodes
1205 // if(ghostInterface[2*i] && ghostInterface[2*i+1]) {tmp = 2*tmp;}
1206 // lNumGhostNodes += tmp;
1207
1208 // // The corners and edges need to be checked in 2D / 3D to add more ghosts nodes
1209 // for(LO j = 0; j < 2; ++j) {
1210 // for(LO k = 0; k < 2; ++k) {
1211 // // Check if two adjoining faces need ghost nodes and then add their common edge
1212 // if(ghostInterface[2*complementaryIndices[i][0]+j] && ghostInterface[2*complementaryIndices[i][1]+k]) {
1213 // lNumGhostNodes += lCoarseNodesPerDir[i];
1214 // // Add corners if three adjoining faces need ghost nodes,
1215 // // but add them only once! Hence when i == 0.
1216 // if(ghostInterface[2*i] && (i == 0)) { lNumGhostNodes += 1; }
1217 // if(ghostInterface[2*i+1] && (i == 0)) { lNumGhostNodes += 1; }
1218 // }
1219 // }
1220 // }
1221 // tmp = 0;
1222 // }
1223 // } // end of scope for tmp and complementaryIndices
1224
1225 // // Phase 2 //
1226 // // ------------------------------------------------------------------ //
1227 // // Build a list of GIDs to import the required ghost nodes. //
1228 // // The ordering of the ghosts nodes will be as natural as possible, //
1229 // // i.e. it should follow the GID ordering of the mesh. //
1230 // // ------------------------------------------------------------------ //
1231
1232 // // Saddly we have to more or less redo what was just done to figure out the value of lNumGhostNodes,
1233 // // there might be some optimization possibility here...
1234 // ghostGIDs.resize(lNumGhostNodes);
1235 // LO countGhosts = 0;
1236 // // Get the GID of the first point on the current partition.
1237 // GO startingGID = minGlobalIndex;
1238 // Array<GO> startingIndices(3);
1239 // // We still want ghost nodes even if have with a 0 offset,
1240 // // except when we are on a boundary
1241 // if(ghostInterface[4] && (myOffset[2] == 0)) {
1242 // if(gIndices[2] + coarseRate[2] > gFineNodesPerDir[2]) {
1243 // startingGID -= endRate[2]*gFineNodesPerDir[1]*gFineNodesPerDir[0];
1244 // } else {
1245 // startingGID -= coarseRate[2]*gFineNodesPerDir[1]*gFineNodesPerDir[0];
1246 // }
1247 // } else {
1248 // startingGID -= myOffset[2]*gFineNodesPerDir[1]*gFineNodesPerDir[0];
1249 // }
1250 // if(ghostInterface[2] && (myOffset[1] == 0)) {
1251 // if(gIndices[1] + coarseRate[1] > gFineNodesPerDir[1]) {
1252 // startingGID -= endRate[1]*gFineNodesPerDir[0];
1253 // } else {
1254 // startingGID -= coarseRate[1]*gFineNodesPerDir[0];
1255 // }
1256 // } else {
1257 // startingGID -= myOffset[1]*gFineNodesPerDir[0];
1258 // }
1259 // if(ghostInterface[0] && (myOffset[0] == 0)) {
1260 // if(gIndices[0] + coarseRate[0] > gFineNodesPerDir[0]) {
1261 // startingGID -= endRate[0];
1262 // } else {
1263 // startingGID -= coarseRate[0];
1264 // }
1265 // } else {
1266 // startingGID -= myOffset[0];
1267 // }
1268
1269 // { // scope for tmp
1270 // GO tmp;
1271 // startingIndices[2] = startingGID / (gFineNodesPerDir[1]*gFineNodesPerDir[0]);
1272 // tmp = startingGID % (gFineNodesPerDir[1]*gFineNodesPerDir[0]);
1273 // startingIndices[1] = tmp / gFineNodesPerDir[0];
1274 // startingIndices[0] = tmp % gFineNodesPerDir[0];
1275 // }
1276
1277 // GO ghostOffset[3] = {0, 0, 0};
1278 // LO lengthZero = lCoarseNodesPerDir[0];
1279 // LO lengthOne = lCoarseNodesPerDir[1];
1280 // LO lengthTwo = lCoarseNodesPerDir[2];
1281 // if(ghostInterface[0]) {++lengthZero;}
1282 // if(ghostInterface[1]) {++lengthZero;}
1283 // if(ghostInterface[2]) {++lengthOne;}
1284 // if(ghostInterface[3]) {++lengthOne;}
1285 // if(ghostInterface[4]) {++lengthTwo;}
1286 // if(ghostInterface[5]) {++lengthTwo;}
1287
1288 // // First check the bottom face as it will have the lowest GIDs
1289 // if(ghostInterface[4]) {
1290 // ghostOffset[2] = startingGID;
1291 // for(LO j = 0; j < lengthOne; ++j) {
1292 // if( (j == lengthOne-1) && (startingIndices[1] + j*coarseRate[1] + 1 > gFineNodesPerDir[1]) ) {
1293 // ghostOffset[1] = ((j-1)*coarseRate[1] + endRate[1])*gFineNodesPerDir[0];
1294 // } else {
1295 // ghostOffset[1] = j*coarseRate[1]*gFineNodesPerDir[0];
1296 // }
1297 // for(LO k = 0; k < lengthZero; ++k) {
1298 // if( (k == lengthZero-1) && (startingIndices[0] + k*coarseRate[0] + 1 > gFineNodesPerDir[0]) ) {
1299 // ghostOffset[0] = (k-1)*coarseRate[0] + endRate[0];
1300 // } else {
1301 // ghostOffset[0] = k*coarseRate[0];
1302 // }
1303 // // If the partition includes a changed rate at the edge, ghost nodes need to be picked carefully.
1304 // // This if statement is repeated each time a ghost node is selected.
1305 // ghostGIDs[countGhosts] = ghostOffset[2] + ghostOffset[1] + ghostOffset[0];
1306 // ++countGhosts;
1307 // }
1308 // }
1309 // }
1310
1311 // // Sweep over the lCoarseNodesPerDir[2] coarse layers in direction 2 and gather necessary ghost nodes
1312 // // located on these layers.
1313 // for(LO i = 0; i < lengthTwo; ++i) {
1314 // // Exclude the cases where ghost nodes exists on the faces in directions 2, these faces are swept
1315 // // seperatly for efficiency.
1316 // if( !((i == lengthTwo-1) && ghostInterface[5]) && !((i == 0) && ghostInterface[4]) ) {
1317 // // Set the ghostOffset in direction 2 taking into account a possible endRate different
1318 // // from the regular coarseRate.
1319 // if( (i == lengthTwo-1) && !ghostInterface[5] ) {
1320 // ghostOffset[2] = startingGID + ((i-1)*coarseRate[2] + endRate[2])*gFineNodesPerDir[1]*gFineNodesPerDir[0];
1321 // } else {
1322 // ghostOffset[2] = startingGID + i*coarseRate[2]*gFineNodesPerDir[1]*gFineNodesPerDir[0];
1323 // }
1324 // for(LO j = 0; j < lengthOne; ++j) {
1325 // if( (j == 0) && ghostInterface[2] ) {
1326 // for(LO k = 0; k < lengthZero; ++k) {
1327 // if( (k == lengthZero-1) && (startingIndices[0] + k*coarseRate[0] + 1 > gFineNodesPerDir[0]) ) {
1328 // if(k == 0) {
1329 // ghostOffset[0] = endRate[0];
1330 // } else {
1331 // ghostOffset[0] = (k-1)*coarseRate[0] + endRate[0];
1332 // }
1333 // } else {
1334 // ghostOffset[0] = k*coarseRate[0];
1335 // }
1336 // // In this case j == 0 so ghostOffset[1] is zero and can be ignored
1337 // ghostGIDs[countGhosts] = ghostOffset[2] + ghostOffset[0];
1338 // ++countGhosts;
1339 // }
1340 // } else if( (j == lengthOne-1) && ghostInterface[3] ) {
1341 // // Set the ghostOffset in direction 1 taking into account a possible endRate different
1342 // // from the regular coarseRate.
1343 // if( (j == lengthOne-1) && (startingIndices[1] + j*coarseRate[1] + 1 > gFineNodesPerDir[1]) ) {
1344 // ghostOffset[1] = ((j-1)*coarseRate[1] + endRate[1])*gFineNodesPerDir[0];
1345 // } else {
1346 // ghostOffset[1] = j*coarseRate[1]*gFineNodesPerDir[0];
1347 // }
1348 // for(LO k = 0; k < lengthZero; ++k) {
1349 // if( (k == lengthZero-1) && (startingIndices[0] + k*coarseRate[0] + 1 > gFineNodesPerDir[0]) ) {
1350 // ghostOffset[0] = (k-1)*coarseRate[0] + endRate[0];
1351 // } else {
1352 // ghostOffset[0] = k*coarseRate[0];
1353 // }
1354 // ghostGIDs[countGhosts] = ghostOffset[2] + ghostOffset[1] + ghostOffset[0];
1355 // ++countGhosts;
1356 // }
1357 // } else {
1358 // // Set ghostOffset[1] for side faces sweep
1359 // if( (j == lengthOne-1) && (startingIndices[1] + j*coarseRate[1] + 1 > gFineNodesPerDir[1]) ) {
1360 // ghostOffset[1] = ( (j-1)*coarseRate[1] + endRate[1] )*gFineNodesPerDir[0];
1361 // } else {
1362 // ghostOffset[1] = j*coarseRate[1]*gFineNodesPerDir[0];
1363 // }
1364
1365 // // Set ghostOffset[0], ghostGIDs and countGhosts
1366 // if(ghostInterface[0]) { // In that case ghostOffset[0]==0, so we can ignore it
1367 // ghostGIDs[countGhosts] = ghostOffset[2] + ghostOffset[1];
1368 // ++countGhosts;
1369 // }
1370 // if(ghostInterface[1]) { // Grab ghost point at the end of direction 0.
1371 // if( (startingIndices[0] + (lengthZero-1)*coarseRate[0]) > gFineNodesPerDir[0] - 1 ) {
1372 // if(lengthZero > 2) {
1373 // ghostOffset[0] = (lengthZero-2)*coarseRate[0] + endRate[0];
1374 // } else {
1375 // ghostOffset[0] = endRate[0];
1376 // }
1377 // } else {
1378 // ghostOffset[0] = (lengthZero-1)*coarseRate[0];
1379 // }
1380 // ghostGIDs[countGhosts] = ghostOffset[2] + ghostOffset[1] + ghostOffset[0];
1381 // ++countGhosts;
1382 // }
1383 // }
1384 // }
1385 // }
1386 // }
1387
1388 // // Finally check the top face
1389 // if(ghostInterface[5]) {
1390 // if( startingIndices[2] + (lengthTwo-1)*coarseRate[2] + 1 > gFineNodesPerDir[2] ) {
1391 // ghostOffset[2] = startingGID + ((lengthTwo-2)*coarseRate[2] + endRate[2])*gFineNodesPerDir[1]*gFineNodesPerDir[0];
1392 // } else {
1393 // ghostOffset[2] = startingGID + (lengthTwo-1)*coarseRate[2]*gFineNodesPerDir[1]*gFineNodesPerDir[0];
1394 // }
1395 // for(LO j = 0; j < lengthOne; ++j) {
1396 // if( (j == lengthOne-1) && (startingIndices[1] + j*coarseRate[1] + 1 > gFineNodesPerDir[1]) ) { // && !ghostInterface[3] ) {
1397 // ghostOffset[1] = ( (j-1)*coarseRate[1] + endRate[1] )*gFineNodesPerDir[0];
1398 // } else {
1399 // ghostOffset[1] = j*coarseRate[1]*gFineNodesPerDir[0];
1400 // }
1401 // for(LO k = 0; k < lengthZero; ++k) {
1402 // if( (k == lengthZero-1) && (startingIndices[0] + k*coarseRate[0] + 1 > gFineNodesPerDir[0]) ) {
1403 // ghostOffset[0] = (k-1)*coarseRate[0] + endRate[0];
1404 // } else {
1405 // ghostOffset[0] = k*coarseRate[0];
1406 // }
1407 // ghostGIDs[countGhosts] = ghostOffset[2] + ghostOffset[1] + ghostOffset[0];
1408 // ++countGhosts;
1409 // }
1410 // }
1411 // }
1412
1413 // // Phase 3 //
1414 // // ------------------------------------------------------------------ //
1415 // // Final phase of this function, lists are being built to create the //
1416 // // column and domain maps of the projection as well as the map and //
1417 // // arrays for the coarse coordinates multivector. //
1418 // // ------------------------------------------------------------------ //
1419
1420 // Array<GO> gCoarseNodesGIDs(lNumCoarseNodes);
1421 // LO currentNode, offset2, offset1, offset0;
1422 // // Find the GIDs of the coarse nodes on the partition.
1423 // for(LO ind2 = 0; ind2 < lCoarseNodesPerDir[2]; ++ind2) {
1424 // if(myOffset[2] == 0) {
1425 // offset2 = startingIndices[2] + myOffset[2];
1426 // } else {
1427 // if(startingIndices[2] + endRate[2] == gFineNodesPerDir[2] - 1) {
1428 // offset2 = startingIndices[2] + endRate[2];
1429 // } else {
1430 // offset2 = startingIndices[2] + coarseRate[2];
1431 // }
1432 // }
1433 // if(offset2 + ind2*coarseRate[2] > gFineNodesPerDir[2] - 1) {
1434 // offset2 += (ind2 - 1)*coarseRate[2] + endRate[2];
1435 // } else {
1436 // offset2 += ind2*coarseRate[2];
1437 // }
1438 // offset2 = offset2*gFineNodesPerDir[1]*gFineNodesPerDir[0];
1439
1440 // for(LO ind1 = 0; ind1 < lCoarseNodesPerDir[1]; ++ind1) {
1441 // if(myOffset[1] == 0) {
1442 // offset1 = startingIndices[1] + myOffset[1];
1443 // } else {
1444 // if(startingIndices[1] + endRate[1] == gFineNodesPerDir[1] - 1) {
1445 // offset1 = startingIndices[1] + endRate[1];
1446 // } else {
1447 // offset1 = startingIndices[1] + coarseRate[1];
1448 // }
1449 // }
1450 // if(offset1 + ind1*coarseRate[1] > gFineNodesPerDir[1] - 1) {
1451 // offset1 += (ind1 - 1)*coarseRate[1] + endRate[1];
1452 // } else {
1453 // offset1 += ind1*coarseRate[1];
1454 // }
1455 // offset1 = offset1*gFineNodesPerDir[0];
1456 // for(LO ind0 = 0; ind0 < lCoarseNodesPerDir[0]; ++ind0) {
1457 // offset0 = startingIndices[0];
1458 // if(myOffset[0] == 0) {
1459 // offset0 += ind0*coarseRate[0];
1460 // } else {
1461 // offset0 += (ind0 + 1)*coarseRate[0];
1462 // }
1463 // if(offset0 > gFineNodesPerDir[0] - 1) {offset0 += endRate[0] - coarseRate[0];}
1464
1465 // currentNode = ind2*lCoarseNodesPerDir[1]*lCoarseNodesPerDir[0]
1466 // + ind1*lCoarseNodesPerDir[0]
1467 // + ind0;
1468 // gCoarseNodesGIDs[currentNode] = offset2 + offset1 + offset0;
1469 // }
1470 // }
1471 // }
1472
1473 // // Actual loop over all the coarse/ghost nodes to find their index on the coarse mesh
1474 // // and the corresponding dofs that will need to be added to colMapP.
1475 // colGIDs.resize(BlkSize*(lNumCoarseNodes+lNumGhostNodes));
1476 // coarseNodesGIDs.resize(lNumCoarseNodes);
1477 // for(LO i = 0; i < numDimensions; ++i) {coarseNodes[i].resize(lNumCoarseNodes);}
1478 // GO fineNodesPerCoarseSlab = coarseRate[2]*gFineNodesPerDir[1]*gFineNodesPerDir[0];
1479 // GO fineNodesEndCoarseSlab = endRate[2]*gFineNodesPerDir[1]*gFineNodesPerDir[0];
1480 // GO fineNodesPerCoarsePlane = coarseRate[1]*gFineNodesPerDir[0];
1481 // GO fineNodesEndCoarsePlane = endRate[1]*gFineNodesPerDir[0];
1482 // GO coarseNodesPerCoarseLayer = gCoarseNodesPerDir[1]*gCoarseNodesPerDir[0];
1483 // GO gCoarseNodeOnCoarseGridGID;
1484 // LO gInd[3], lCol;
1485 // Array<int> ghostPIDs (lNumGhostNodes);
1486 // Array<LO> ghostLIDs (lNumGhostNodes);
1487 // Array<LO> ghostPermut(lNumGhostNodes);
1488 // for(LO k = 0; k < lNumGhostNodes; ++k) {ghostPermut[k] = k;}
1489 // coordinatesMap->getRemoteIndexList(ghostGIDs, ghostPIDs, ghostLIDs);
1490 // sh_sort_permute(ghostPIDs.begin(),ghostPIDs.end(), ghostPermut.begin(),ghostPermut.end());
1491
1492 // { // scope for tmpInds, tmpVars and tmp.
1493 // GO tmpInds[3], tmpVars[2];
1494 // LO tmp;
1495 // // Loop over the coarse nodes of the partition and add them to colGIDs
1496 // // that will be used to construct the column and domain maps of P as well
1497 // // as to construct the coarse coordinates map.
1498 // // for(LO col = 0; col < lNumCoarseNodes; ++col) { // This should most likely be replaced by loops of lCoarseNodesPerDir[] to simplify arithmetics
1499 // LO col = 0;
1500 // LO firstCoarseNodeInds[3], currentCoarseNode;
1501 // for(LO dim = 0; dim < 3; ++dim) {
1502 // if(myOffset[dim] == 0) {
1503 // firstCoarseNodeInds[dim] = 0;
1504 // } else {
1505 // firstCoarseNodeInds[dim] = coarseRate[dim] - myOffset[dim];
1506 // }
1507 // }
1508 // Array<ArrayRCP<const typename Teuchos::ScalarTraits<Scalar>::coordinateType> > fineNodes(numDimensions);
1509 // for(LO dim = 0; dim < numDimensions; ++dim) {fineNodes[dim] = coordinates->getData(dim);}
1510 // for(LO k = 0; k < lCoarseNodesPerDir[2]; ++k) {
1511 // for(LO j = 0; j < lCoarseNodesPerDir[1]; ++j) {
1512 // for(LO i = 0; i < lCoarseNodesPerDir[0]; ++i) {
1513 // col = k*lCoarseNodesPerDir[1]*lCoarseNodesPerDir[0] + j*lCoarseNodesPerDir[0] + i;
1514
1515 // // Check for endRate
1516 // currentCoarseNode = 0;
1517 // if(firstCoarseNodeInds[0] + i*coarseRate[0] > lFineNodesPerDir[0] - 1) {
1518 // currentCoarseNode += firstCoarseNodeInds[0] + (i-1)*coarseRate[0] + endRate[0];
1519 // } else {
1520 // currentCoarseNode += firstCoarseNodeInds[0] + i*coarseRate[0];
1521 // }
1522 // if(firstCoarseNodeInds[1] + j*coarseRate[1] > lFineNodesPerDir[1] - 1) {
1523 // currentCoarseNode += (firstCoarseNodeInds[1] + (j-1)*coarseRate[1] + endRate[1])*lFineNodesPerDir[0];
1524 // } else {
1525 // currentCoarseNode += (firstCoarseNodeInds[1] + j*coarseRate[1])*lFineNodesPerDir[0];
1526 // }
1527 // if(firstCoarseNodeInds[2] + k*coarseRate[2] > lFineNodesPerDir[2] - 1) {
1528 // currentCoarseNode += (firstCoarseNodeInds[2] + (k-1)*coarseRate[2] + endRate[2])*lFineNodesPerDir[1]*lFineNodesPerDir[0];
1529 // } else {
1530 // currentCoarseNode += (firstCoarseNodeInds[2] + k*coarseRate[2])*lFineNodesPerDir[1]*lFineNodesPerDir[0];
1531 // }
1532 // // Load coordinates
1533 // for(LO dim = 0; dim < numDimensions; ++dim) {
1534 // coarseNodes[dim][col] = fineNodes[dim][currentCoarseNode];
1535 // }
1536
1537 // if((endRate[2] != coarseRate[2]) && (gCoarseNodesGIDs[col] > (gCoarseNodesPerDir[2] - 2)*fineNodesPerCoarseSlab + fineNodesEndCoarseSlab - 1)) {
1538 // tmpInds[2] = gCoarseNodesGIDs[col] / fineNodesPerCoarseSlab + 1;
1539 // tmpVars[0] = gCoarseNodesGIDs[col] - (tmpInds[2] - 1)*fineNodesPerCoarseSlab - fineNodesEndCoarseSlab;
1540 // } else {
1541 // tmpInds[2] = gCoarseNodesGIDs[col] / fineNodesPerCoarseSlab;
1542 // tmpVars[0] = gCoarseNodesGIDs[col] % fineNodesPerCoarseSlab;
1543 // }
1544 // if((endRate[1] != coarseRate[1]) && (tmpVars[0] > (gCoarseNodesPerDir[1] - 2)*fineNodesPerCoarsePlane + fineNodesEndCoarsePlane - 1)) {
1545 // tmpInds[1] = tmpVars[0] / fineNodesPerCoarsePlane + 1;
1546 // tmpVars[1] = tmpVars[0] - (tmpInds[1] - 1)*fineNodesPerCoarsePlane - fineNodesEndCoarsePlane;
1547 // } else {
1548 // tmpInds[1] = tmpVars[0] / fineNodesPerCoarsePlane;
1549 // tmpVars[1] = tmpVars[0] % fineNodesPerCoarsePlane;
1550 // }
1551 // if(tmpVars[1] == gFineNodesPerDir[0] - 1) {
1552 // tmpInds[0] = gCoarseNodesPerDir[0] - 1;
1553 // } else {
1554 // tmpInds[0] = tmpVars[1] / coarseRate[0];
1555 // }
1556 // gInd[2] = col / (lCoarseNodesPerDir[1]*lCoarseNodesPerDir[0]);
1557 // tmp = col % (lCoarseNodesPerDir[1]*lCoarseNodesPerDir[0]);
1558 // gInd[1] = tmp / lCoarseNodesPerDir[0];
1559 // gInd[0] = tmp % lCoarseNodesPerDir[0];
1560 // lCol = gInd[2]*(lCoarseNodesPerDir[1]*lCoarseNodesPerDir[0]) + gInd[1]*lCoarseNodesPerDir[0] + gInd[0];
1561 // gCoarseNodeOnCoarseGridGID = tmpInds[2]*coarseNodesPerCoarseLayer + tmpInds[1]*gCoarseNodesPerDir[0] + tmpInds[0];
1562 // coarseNodesGIDs[lCol] = gCoarseNodeOnCoarseGridGID;
1563 // for(LO dof = 0; dof < BlkSize; ++dof) {
1564 // colGIDs[BlkSize*lCol + dof] = BlkSize*gCoarseNodeOnCoarseGridGID + dof;
1565 // }
1566 // }
1567 // }
1568 // }
1569 // // Now loop over the ghost nodes of the partition to add them to colGIDs
1570 // // since they will need to be included in the column map of P
1571 // for(col = lNumCoarseNodes; col < lNumCoarseNodes + lNumGhostNodes; ++col) {
1572 // if((endRate[2] != coarseRate[2]) && (ghostGIDs[ghostPermut[col - lNumCoarseNodes]] > (gCoarseNodesPerDir[2] - 2)*fineNodesPerCoarseSlab + fineNodesEndCoarseSlab - 1)) {
1573 // tmpInds[2] = ghostGIDs[ghostPermut[col - lNumCoarseNodes]] / fineNodesPerCoarseSlab + 1;
1574 // tmpVars[0] = ghostGIDs[ghostPermut[col - lNumCoarseNodes]] - (tmpInds[2] - 1)*fineNodesPerCoarseSlab - fineNodesEndCoarseSlab;
1575 // } else {
1576 // tmpInds[2] = ghostGIDs[ghostPermut[col - lNumCoarseNodes]] / fineNodesPerCoarseSlab;
1577 // tmpVars[0] = ghostGIDs[ghostPermut[col - lNumCoarseNodes]] % fineNodesPerCoarseSlab;
1578 // }
1579 // if((endRate[1] != coarseRate[1]) && (tmpVars[0] > (gCoarseNodesPerDir[1] - 2)*fineNodesPerCoarsePlane + fineNodesEndCoarsePlane - 1)) {
1580 // tmpInds[1] = tmpVars[0] / fineNodesPerCoarsePlane + 1;
1581 // tmpVars[1] = tmpVars[0] - (tmpInds[1] - 1)*fineNodesPerCoarsePlane - fineNodesEndCoarsePlane;
1582 // } else {
1583 // tmpInds[1] = tmpVars[0] / fineNodesPerCoarsePlane;
1584 // tmpVars[1] = tmpVars[0] % fineNodesPerCoarsePlane;
1585 // }
1586 // if(tmpVars[1] == gFineNodesPerDir[0] - 1) {
1587 // tmpInds[0] = gCoarseNodesPerDir[0] - 1;
1588 // } else {
1589 // tmpInds[0] = tmpVars[1] / coarseRate[0];
1590 // }
1591 // gCoarseNodeOnCoarseGridGID = tmpInds[2]*coarseNodesPerCoarseLayer + tmpInds[1]*gCoarseNodesPerDir[0] + tmpInds[0];
1592 // for(LO dof = 0; dof < BlkSize; ++dof) {
1593 // colGIDs[BlkSize*col + dof] = BlkSize*gCoarseNodeOnCoarseGridGID + dof;
1594 // }
1595 // }
1596 // } // End of scope for tmpInds, tmpVars and tmp
1597
1598 // } // GetGeometricData()
1599
1600 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1602 const LO numDimensions, const Array<GO> currentNodeIndices,
1603 const Array<GO> coarseNodeIndices, const LO rate[3],
1604 const Array<Array<typename Teuchos::ScalarTraits<Scalar>::coordinateType> > coord, const int interpolationOrder,
1605 std::vector<double>& stencil) const {
1606
1607 TEUCHOS_TEST_FOR_EXCEPTION((interpolationOrder > 1) || (interpolationOrder < 0),
1609 "The interpolation order can be set to 0 or 1 only.");
1610
1611 if(interpolationOrder == 0) {
1612 ComputeConstantInterpolationStencil(numDimensions, currentNodeIndices, coarseNodeIndices,
1613 rate, stencil);
1614 } else if(interpolationOrder == 1) {
1615 ComputeLinearInterpolationStencil(numDimensions, coord, stencil);
1616 }
1617
1618 } // End ComputeStencil
1619
1620 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1622 ComputeConstantInterpolationStencil(const LO numDimensions, const Array<GO> currentNodeIndices,
1623 const Array<GO> coarseNodeIndices, const LO rate[3],
1624 std::vector<double>& stencil) const {
1625
1626 LO coarseNode = 0;
1627 if(numDimensions > 2) {
1628 if((currentNodeIndices[2] - coarseNodeIndices[2]) > (rate[2] / 2)) {
1629 coarseNode += 4;
1630 }
1631 }
1632 if(numDimensions > 1) {
1633 if((currentNodeIndices[1] - coarseNodeIndices[1]) > (rate[1] / 2)) {
1634 coarseNode += 2;
1635 }
1636 }
1637 if((currentNodeIndices[0] - coarseNodeIndices[0]) > (rate[0] / 2)) {
1638 coarseNode += 1;
1639 }
1640 stencil[0] = coarseNode;
1641
1642 } // ComputeConstantInterpolationStencil
1643
1644 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1646 ComputeLinearInterpolationStencil(const LO numDimensions, const Array<Array<typename Teuchos::ScalarTraits<Scalar>::coordinateType> > coord,
1647 std::vector<double>& stencil)
1648 const {
1649
1650 // 7 8 Find xi, eta and zeta such that
1651 // x---------x
1652 // /| /| Rx = x_p - sum N_i(xi,eta,zeta)x_i = 0
1653 // 5/ | 6/ | Ry = y_p - sum N_i(xi,eta,zeta)y_i = 0
1654 // x---------x | Rz = z_p - sum N_i(xi,eta,zeta)z_i = 0
1655 // | | *P | |
1656 // | x------|--x We can do this with a Newton solver:
1657 // | /3 | /4 We will start with initial guess (xi,eta,zeta) = (0,0,0)
1658 // |/ |/ We compute the Jacobian and iterate until convergence...
1659 // z y x---------x
1660 // | / 1 2 Once we have (xi,eta,zeta), we can evaluate all N_i which
1661 // |/ give us the weights for the interpolation stencil!
1662 // o---x
1663 //
1664
1665 Teuchos::SerialDenseMatrix<LO,double> Jacobian(numDimensions, numDimensions);
1666 Teuchos::SerialDenseVector<LO,double> residual(numDimensions);
1667 Teuchos::SerialDenseVector<LO,double> solutionDirection(numDimensions);
1668 Teuchos::SerialDenseVector<LO,double> paramCoords(numDimensions);
1669 Teuchos::SerialDenseSolver<LO,double> problem;
1670 LO numTerms = std::pow(2,numDimensions), iter = 0, max_iter = 5;
1671 double functions[4][8], norm_ref = 1, norm2 = 1, tol = 1e-5;
1672 paramCoords.size(numDimensions);
1673
1674 while( (iter < max_iter) && (norm2 > tol*norm_ref) ) {
1675 ++iter;
1676 norm2 = 0;
1677 solutionDirection.size(numDimensions);
1678 residual.size(numDimensions);
1679 Jacobian = 0.0;
1680
1681 // Compute Jacobian and Residual
1682 GetInterpolationFunctions(numDimensions, paramCoords, functions);
1683 for(LO i = 0; i < numDimensions; ++i) {
1684 residual(i) = coord[0][i]; // Add coordinates from point of interest
1685 for(LO k = 0; k < numTerms; ++k) {
1686 residual(i) -= functions[0][k]*coord[k+1][i]; //Remove contribution from all coarse points
1687 }
1688 if(iter == 1) {
1689 norm_ref += residual(i)*residual(i);
1690 if(i == numDimensions - 1) {
1691 norm_ref = std::sqrt(norm_ref);
1692 }
1693 }
1694
1695 for(LO j = 0; j < numDimensions; ++j) {
1696 for(LO k = 0; k < numTerms; ++k) {
1697 Jacobian(i,j) += functions[j+1][k]*coord[k+1][i];
1698 }
1699 }
1700 }
1701
1702 // Set Jacobian, Vectors and solve problem
1703 problem.setMatrix(Teuchos::rcp(&Jacobian, false));
1704 problem.setVectors(Teuchos::rcp(&solutionDirection, false), Teuchos::rcp(&residual, false));
1705 problem.factorWithEquilibration(true);
1706 problem.solve();
1707 problem.unequilibrateLHS();
1708
1709 for(LO i = 0; i < numDimensions; ++i) {
1710 paramCoords(i) = paramCoords(i) + solutionDirection(i);
1711 }
1712
1713 // Recompute Residual norm
1714 GetInterpolationFunctions(numDimensions, paramCoords, functions);
1715 for(LO i = 0; i < numDimensions; ++i) {
1716 double tmp = coord[0][i];
1717 for(LO k = 0; k < numTerms; ++k) {
1718 tmp -= functions[0][k]*coord[k+1][i];
1719 }
1720 norm2 += tmp*tmp;
1721 tmp = 0;
1722 }
1723 norm2 = std::sqrt(norm2);
1724 }
1725
1726 // Load the interpolation values onto the stencil.
1727 for(LO i = 0; i < 8; ++i) {
1728 stencil[i] = functions[0][i];
1729 }
1730
1731 } // End ComputeLinearInterpolationStencil
1732
1733 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1735 GetInterpolationFunctions(const LO numDimensions,
1736 const Teuchos::SerialDenseVector<LO,double> parameters,
1737 double functions[4][8]) const {
1738 double xi = 0.0, eta = 0.0, zeta = 0.0, denominator = 0.0;
1739 if(numDimensions == 1) {
1740 xi = parameters[0];
1741 denominator = 2.0;
1742 } else if(numDimensions == 2) {
1743 xi = parameters[0];
1744 eta = parameters[1];
1745 denominator = 4.0;
1746 } else if(numDimensions == 3) {
1747 xi = parameters[0];
1748 eta = parameters[1];
1749 zeta = parameters[2];
1750 denominator = 8.0;
1751 }
1752
1753 functions[0][0] = (1.0 - xi)*(1.0 - eta)*(1.0 - zeta) / denominator;
1754 functions[0][1] = (1.0 + xi)*(1.0 - eta)*(1.0 - zeta) / denominator;
1755 functions[0][2] = (1.0 - xi)*(1.0 + eta)*(1.0 - zeta) / denominator;
1756 functions[0][3] = (1.0 + xi)*(1.0 + eta)*(1.0 - zeta) / denominator;
1757 functions[0][4] = (1.0 - xi)*(1.0 - eta)*(1.0 + zeta) / denominator;
1758 functions[0][5] = (1.0 + xi)*(1.0 - eta)*(1.0 + zeta) / denominator;
1759 functions[0][6] = (1.0 - xi)*(1.0 + eta)*(1.0 + zeta) / denominator;
1760 functions[0][7] = (1.0 + xi)*(1.0 + eta)*(1.0 + zeta) / denominator;
1761
1762 functions[1][0] = -(1.0 - eta)*(1.0 - zeta) / denominator;
1763 functions[1][1] = (1.0 - eta)*(1.0 - zeta) / denominator;
1764 functions[1][2] = -(1.0 + eta)*(1.0 - zeta) / denominator;
1765 functions[1][3] = (1.0 + eta)*(1.0 - zeta) / denominator;
1766 functions[1][4] = -(1.0 - eta)*(1.0 + zeta) / denominator;
1767 functions[1][5] = (1.0 - eta)*(1.0 + zeta) / denominator;
1768 functions[1][6] = -(1.0 + eta)*(1.0 + zeta) / denominator;
1769 functions[1][7] = (1.0 + eta)*(1.0 + zeta) / denominator;
1770
1771 functions[2][0] = -(1.0 - xi)*(1.0 - zeta) / denominator;
1772 functions[2][1] = -(1.0 + xi)*(1.0 - zeta) / denominator;
1773 functions[2][2] = (1.0 - xi)*(1.0 - zeta) / denominator;
1774 functions[2][3] = (1.0 + xi)*(1.0 - zeta) / denominator;
1775 functions[2][4] = -(1.0 - xi)*(1.0 + zeta) / denominator;
1776 functions[2][5] = -(1.0 + xi)*(1.0 + zeta) / denominator;
1777 functions[2][6] = (1.0 - xi)*(1.0 + zeta) / denominator;
1778 functions[2][7] = (1.0 + xi)*(1.0 + zeta) / denominator;
1779
1780 functions[3][0] = -(1.0 - xi)*(1.0 - eta) / denominator;
1781 functions[3][1] = -(1.0 + xi)*(1.0 - eta) / denominator;
1782 functions[3][2] = -(1.0 - xi)*(1.0 + eta) / denominator;
1783 functions[3][3] = -(1.0 + xi)*(1.0 + eta) / denominator;
1784 functions[3][4] = (1.0 - xi)*(1.0 - eta) / denominator;
1785 functions[3][5] = (1.0 + xi)*(1.0 - eta) / denominator;
1786 functions[3][6] = (1.0 - xi)*(1.0 + eta) / denominator;
1787 functions[3][7] = (1.0 + xi)*(1.0 + eta) / denominator;
1788
1789 } // End GetInterpolationFunctions
1790
1791 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1793 const typename Teuchos::Array<LocalOrdinal>::iterator& first1,
1794 const typename Teuchos::Array<LocalOrdinal>::iterator& last1,
1795 const typename Teuchos::Array<LocalOrdinal>::iterator& first2,
1796 const typename Teuchos::Array<LocalOrdinal>::iterator& /* last2 */) const
1797 {
1798 typedef typename std::iterator_traits<typename Teuchos::Array<LocalOrdinal>::iterator>::difference_type DT;
1799 DT n = last1 - first1;
1800 DT m = n / 2;
1801 DT z = Teuchos::OrdinalTraits<DT>::zero();
1802 while (m > z)
1803 {
1804 DT max = n - m;
1805 for (DT j = 0; j < max; j++)
1806 {
1807 for (DT k = j; k >= 0; k-=m)
1808 {
1809 if (first1[first2[k+m]] >= first1[first2[k]])
1810 break;
1811 std::swap(first2[k+m], first2[k]);
1812 }
1813 }
1814 m = m/2;
1815 }
1816 } // End sh_sort_permute
1817
1818 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1820 const typename Teuchos::Array<LocalOrdinal>::iterator& first1,
1821 const typename Teuchos::Array<LocalOrdinal>::iterator& last1,
1822 const typename Teuchos::Array<LocalOrdinal>::iterator& first2,
1823 const typename Teuchos::Array<LocalOrdinal>::iterator& /* last2 */) const
1824 {
1825 typedef typename std::iterator_traits<typename Teuchos::Array<LocalOrdinal>::iterator>::difference_type DT;
1826 DT n = last1 - first1;
1827 DT m = n / 2;
1828 DT z = Teuchos::OrdinalTraits<DT>::zero();
1829 while (m > z)
1830 {
1831 DT max = n - m;
1832 for (DT j = 0; j < max; j++)
1833 {
1834 for (DT k = j; k >= 0; k-=m)
1835 {
1836 if (first1[k+m] >= first1[k])
1837 break;
1838 std::swap(first1[k+m], first1[k]);
1839 std::swap(first2[k+m], first2[k]);
1840 }
1841 }
1842 m = m/2;
1843 }
1844 } // End sh_sort2
1845
1846 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1848 GetGIDLocalLexicographic(const GO i, const GO j, const GO k,
1849 const Array<LO> coarseNodeFineIndices, const RCP<GeometricData> myGeo,
1850 const LO myRankIndex, const LO pi, const LO pj, const LO /* pk */,
1851 const typename std::vector<std::vector<GO> >::iterator blockStart,
1852 const typename std::vector<std::vector<GO> >::iterator blockEnd,
1853 GO& myGID, LO& myPID, LO& myLID) const {
1854
1855 LO ni = -1, nj = -1, li = -1, lj = -1, lk = -1;
1856 LO myRankGuess = myRankIndex;
1857 // We try to make a logical guess as to which PID owns the current coarse node
1858 if(i == 0 && myGeo->ghostInterface[0]) {
1859 --myRankGuess;
1860 } else if((i == myGeo->ghostedCoarseNodesPerDir[0] - 1) && myGeo->ghostInterface[1]) {
1861 ++myRankGuess;
1862 }
1863 if(j == 0 && myGeo->ghostInterface[2]) {
1864 myRankGuess -= pi;
1865 } else if((j == myGeo->ghostedCoarseNodesPerDir[1] - 1) && myGeo->ghostInterface[3]) {
1866 myRankGuess += pi;
1867 }
1868 if(k == 0 && myGeo->ghostInterface[4]) {
1869 myRankGuess -= pj*pi;
1870 } else if((k == myGeo->ghostedCoarseNodesPerDir[2] - 1) && myGeo->ghostInterface[5]) {
1871 myRankGuess += pj*pi;
1872 }
1873 if(coarseNodeFineIndices[0] >= myGeo->meshData[myRankGuess][3]
1874 && coarseNodeFineIndices[0] <= myGeo->meshData[myRankGuess][4]
1875 && coarseNodeFineIndices[1] >= myGeo->meshData[myRankGuess][5]
1876 && coarseNodeFineIndices[1] <= myGeo->meshData[myRankGuess][6]
1877 && coarseNodeFineIndices[2] >= myGeo->meshData[myRankGuess][7]
1878 && coarseNodeFineIndices[2] <= myGeo->meshData[myRankGuess][8]) {
1879 myPID = myGeo->meshData[myRankGuess][0];
1880 ni = myGeo->meshData[myRankGuess][4] - myGeo->meshData[myRankGuess][3] + 1;
1881 nj = myGeo->meshData[myRankGuess][6] - myGeo->meshData[myRankGuess][5] + 1;
1882 li = coarseNodeFineIndices[0] - myGeo->meshData[myRankGuess][3];
1883 lj = coarseNodeFineIndices[1] - myGeo->meshData[myRankGuess][5];
1884 lk = coarseNodeFineIndices[2] - myGeo->meshData[myRankGuess][7];
1885 myLID = lk*nj*ni + lj*ni + li;
1886 myGID = myGeo->meshData[myRankGuess][9] + myLID;
1887 } else { // The guess failed, let us use the heavy artilery: std::find_if()
1888 // It could be interesting to monitor how many times this branch of the code gets
1889 // used as it is far more expensive than the above one...
1890 auto nodeRank = std::find_if(blockStart, blockEnd,
1891 [coarseNodeFineIndices](const std::vector<GO>& vec){
1892 if(coarseNodeFineIndices[0] >= vec[3]
1893 && coarseNodeFineIndices[0] <= vec[4]
1894 && coarseNodeFineIndices[1] >= vec[5]
1895 && coarseNodeFineIndices[1] <= vec[6]
1896 && coarseNodeFineIndices[2] >= vec[7]
1897 && coarseNodeFineIndices[2] <= vec[8]) {
1898 return true;
1899 } else {
1900 return false;
1901 }
1902 });
1903 myPID = (*nodeRank)[0];
1904 ni = (*nodeRank)[4] - (*nodeRank)[3] + 1;
1905 nj = (*nodeRank)[6] - (*nodeRank)[5] + 1;
1906 li = coarseNodeFineIndices[0] - (*nodeRank)[3];
1907 lj = coarseNodeFineIndices[1] - (*nodeRank)[5];
1908 lk = coarseNodeFineIndices[2] - (*nodeRank)[7];
1909 myLID = lk*nj*ni + lj*ni + li;
1910 myGID = (*nodeRank)[9] + myLID;
1911 }
1912 } // End GetGIDLocalLexicographic
1913
1914} //namespace MueLu
1915
1916#define MUELU_GENERALGEOMETRICPFACTORY_SHORT
1917#endif // MUELU_GENERALGEOMETRICPFACTORY_DEF_HPP
MueLu::DefaultNode Node
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.
Prolongator factory performing geometric coarsening.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
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 MeshLayoutInterface(const int interpolationOrder, const LO blkSize, RCP< const Map > fineCoordsMap, RCP< GeometricData > myGeometry, RCP< NodesIDs > ghostedCoarseNodes, Array< Array< GO > > &lCoarseNodesGIDs) const
Class that holds all level-specific information.
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need's value has been saved.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
int GetLevelID() const
Return level number.
T & Get(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Get data without decrementing associated storage counter (i.e., read-only access)....
static const NoFactory * get()
Namespace for MueLu classes and methods.
@ Runtime1
Description of what is happening (more verbose)