10 #ifndef PACKAGES_MUELU_SRC_GRAPH_MUELU_VARIABLEDOFLAPLACIANFACTORY_DEF_HPP_
11 #define PACKAGES_MUELU_SRC_GRAPH_MUELU_VARIABLEDOFLAPLACIANFACTORY_DEF_HPP_
19 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
23 validParamList->
set<
double>(
"Advanced Dirichlet: threshold", 1e-5,
"Drop tolerance for Dirichlet detection");
24 validParamList->
set<
double>(
"Variable DOF amalgamation: threshold", 1.8e-9,
"Drop tolerance for amalgamation process");
25 validParamList->
set<
int>(
"maxDofPerNode", 1,
"Maximum number of DOFs per node");
30 return validParamList;
33 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
36 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
38 Input(currentLevel,
"A");
39 Input(currentLevel,
"Coordinates");
47 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
54 RCP<Matrix> A = Get<RCP<Matrix> >(currentLevel,
"A");
60 RCP<dxMV> Coords = Get<RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO,
NO> > >(currentLevel,
"Coordinates");
62 int maxDofPerNode = pL.
get<
int>(
"maxDofPerNode");
63 Scalar dirDropTol = Teuchos::as<Scalar>(pL.
get<
double>(
"Advanced Dirichlet: threshold"));
64 Scalar amalgDropTol = Teuchos::as<Scalar>(pL.
get<
double>(
"Variable DOF amalgamation: threshold"));
66 bool bHasZeroDiagonal =
false;
84 std::vector<LocalOrdinal> map(A->getLocalNumRows());
85 this->buildPaddedMap(dofPresent, map, A->getLocalNumRows());
88 std::vector<LocalOrdinal> myLocalNodeIds(A->getColMap()->getLocalNumElements());
91 size_t nLocalNodes, nLocalPlusGhostNodes;
92 this->assignGhostLocalNodeIds(A->getRowMap(), A->getColMap(), myLocalNodeIds, map, maxDofPerNode, nLocalNodes, nLocalPlusGhostNodes, comm);
96 TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::as<size_t>(dofPresent.
size()) != Teuchos::as<size_t>(nLocalNodes * maxDofPerNode),
MueLu::Exceptions::RuntimeError,
"VariableDofLaplacianFactory: size of provided DofPresent array is " << dofPresent.
size() <<
" but should be " << nLocalNodes * maxDofPerNode <<
" on the current processor.");
106 size_t nLocalDofs = A->getRowMap()->getLocalNumElements();
107 size_t nLocalPlusGhostDofs = A->getColMap()->getLocalNumElements();
116 if (nLocalDofs > 0) {
117 amalgRowMapGIDs[count] = myGids[0];
118 amalgColMapGIDs[count] = myGids[0];
122 for (
size_t i = 1; i < nLocalDofs; i++) {
123 if (myLocalNodeIds[i] != myLocalNodeIds[i - 1]) {
124 amalgRowMapGIDs[count] = myGids[i];
125 amalgColMapGIDs[count] = myGids[i];
130 RCP<GOVector> tempAmalgColVec = GOVectorFactory::Build(A->getDomainMap());
133 for (
size_t i = 0; i < A->getDomainMap()->getLocalNumElements(); i++)
134 tempAmalgColVecData[i] = amalgColMapGIDs[myLocalNodeIds[i]];
137 RCP<GOVector> tempAmalgColVecTarget = GOVectorFactory::Build(A->getColMap());
139 tempAmalgColVecTarget->doImport(*tempAmalgColVec, *dofImporter,
Xpetra::INSERT);
144 for (
size_t i = 0; i < myLocalNodeIds.size(); i++)
145 amalgColMapGIDs[myLocalNodeIds[i]] = tempAmalgColVecBData[i];
151 A->getRowMap()->getIndexBase(),
157 A->getRangeMap()->getIndexBase(),
167 size_t nNonZeros = 0;
168 std::vector<bool> isNonZero(nLocalPlusGhostDofs,
false);
169 std::vector<size_t> nonZeroList(nLocalPlusGhostDofs);
174 A->getLocalDiagCopy(*diagVecUnique);
181 Acrs->getAllValues(rowptr, colind, values);
192 amalgRowPtr[0] = newNzs;
194 bool doNotDrop =
false;
196 if (values.size() == 0) doNotDrop =
true;
198 for (decltype(rowptr.size()) i = 0; i < rowptr.size() - 1; i++) {
199 blockRow = std::floor<LocalOrdinal>(map[i] / maxDofPerNode);
200 if (blockRow != oldBlockRow) {
202 for (
size_t j = 0; j < nNonZeros; j++) isNonZero[nonZeroList[j]] =
false;
204 amalgRowPtr[blockRow] = newNzs;
206 for (
size_t j = rowptr[i]; j < rowptr[i + 1]; j++) {
207 if (doNotDrop ==
true ||
208 (STS::magnitude(values[j] / STS::magnitude(sqrt(STS::magnitude(diagVecData[i]) * STS::magnitude(diagVecData[colind[j]])))) >= STS::magnitude(amalgDropTol))) {
209 blockColumn = myLocalNodeIds[colind[j]];
210 if (isNonZero[blockColumn] ==
false) {
211 isNonZero[blockColumn] =
true;
212 nonZeroList[nNonZeros++] = blockColumn;
213 amalgCols[newNzs++] = blockColumn;
217 oldBlockRow = blockRow;
219 amalgRowPtr[blockRow + 1] = newNzs;
221 TEUCHOS_TEST_FOR_EXCEPTION((blockRow + 1 != Teuchos::as<LO>(nLocalNodes)) && (nLocalNodes != 0), MueLu::Exceptions::RuntimeError,
"VariableDofsPerNodeAmalgamation: error, computed # block rows (" << blockRow + 1 <<
") != nLocalNodes (" << nLocalNodes <<
")");
223 amalgCols.resize(amalgRowPtr[nLocalNodes]);
243 std::vector<bool> keep(amalgRowPtr[amalgRowPtr.
size() - 1],
true);
246 for (decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size() - 1; i++) {
249 for (decltype(maxDofPerNode) j = 0; j < maxDofPerNode; j++) {
250 if (dofPresent[ii++]) uniqueId[i] += temp;
261 for (decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size() - 1; i++) {
262 nodeIdSrcData[i] = uniqueId[i];
265 nodeIdTarget->doImport(*nodeIdSrc, *nodeImporter,
Xpetra::INSERT);
268 for (decltype(uniqueId.
size()) i = 0; i < uniqueId.
size(); i++) {
269 uniqueId[i] = nodeIdTargetData[i];
276 for (decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size() - 1; i++) {
277 for (
size_t j = amalgRowPtr[i]; j < amalgRowPtr[i + 1]; j++) {
278 if (uniqueId[i] != uniqueId[amalgCols[j]]) keep[j] =
false;
284 this->squeezeOutNnzs(amalgRowPtr, amalgCols, amalgVals, keep);
287 RCP<dxMV> ghostedCoords = dxMVf::Build(amalgColMap, Coords->getNumVectors());
289 TEUCHOS_TEST_FOR_EXCEPTION(amalgRowMap->getLocalNumElements() != Coords->getMap()->getLocalNumElements(), MueLu::Exceptions::RuntimeError,
"MueLu::VariableDofLaplacianFactory: the number of Coordinates and amalgamated nodes is inconsistent.");
297 Coords->replaceMap(amalgRowMap);
301 this->buildLaplacian(amalgRowPtr, amalgCols, lapVals, Coords->getNumVectors(), ghostedCoords);
304 for (decltype(amalgRowPtr.
size()) i = 0; i < amalgRowPtr.
size() - 1; i++) {
305 size_t j = amalgRowPtr[i];
306 this->MueLu_az_sort<LocalOrdinal>(&(amalgCols[j]), amalgRowPtr[i + 1] - j, NULL, &(lapVals[j]));
313 for (decltype(status.
size()) i = 0; i < status.
size(); i++) status[i] =
's';
314 for (decltype(status.
size()) i = 0; i < status.
size(); i++) {
315 if (dofPresent[i] ==
false) status[i] =
'p';
317 if (dirOrNot.
size() > 0) {
318 for (decltype(map.size()) i = 0; i < map.size(); i++) {
319 if (dirOrNot[i] ==
true) {
320 status[map[i]] =
'd';
324 Set(currentLevel,
"DofStatus", status);
330 for (
size_t i = 0; i < nLocalNodes; i++) {
331 lapCrsMat->insertLocalValues(i, amalgCols.view(amalgRowPtr[i], amalgRowPtr[i + 1] - amalgRowPtr[i]),
332 lapVals.
view(amalgRowPtr[i], amalgRowPtr[i + 1] - amalgRowPtr[i]));
334 lapCrsMat->fillComplete(amalgRowMap, amalgRowMap);
339 Set(currentLevel,
"A", lapMat);
342 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
343 void VariableDofLaplacianFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::buildLaplacian(
const Teuchos::ArrayRCP<size_t>& rowPtr,
const Teuchos::ArrayRCP<LocalOrdinal>& cols,
Teuchos::ArrayRCP<Scalar>& vals,
const size_t& numdim,
const RCP<
Xpetra::MultiVector<
typename Teuchos::ScalarTraits<Scalar>::magnitudeType,
LocalOrdinal,
GlobalOrdinal,
Node> >& ghostedCoords)
const {
350 for (decltype(rowPtr.
size()) i = 0; i < rowPtr.
size() - 1; i++) {
353 for (
size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
354 if (cols[j] != Teuchos::as<LO>(i)) {
355 vals[j] = std::sqrt((x[i] - x[cols[j]]) * (x[i] - x[cols[j]]) +
356 (y[i] - y[cols[j]]) * (y[i] - y[cols[j]]));
373 for (decltype(rowPtr.
size()) i = 0; i < rowPtr.
size() - 1; i++) {
376 for (
size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
377 if (cols[j] != Teuchos::as<LO>(i)) {
378 vals[j] = std::sqrt((x[i] - x[cols[j]]) * (x[i] - x[cols[j]]) +
379 (y[i] - y[cols[j]]) * (y[i] - y[cols[j]]) +
380 (z[i] - z[cols[j]]) * (z[i] - z[cols[j]]));
397 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
405 size_t nRows = rowPtr.
size() - 1;
406 if (vals.
size() > 0) {
407 for (
size_t i = 0; i < nRows; i++) {
408 size_t newStart = count;
409 for (
size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
411 cols[count] = cols[j];
412 vals[count++] = vals[j];
415 rowPtr[i] = newStart;
418 for (
size_t i = 0; i < nRows; i++) {
419 size_t newStart = count;
420 for (
size_t j = rowPtr[i]; j < rowPtr[i + 1]; j++) {
421 if (keep[j] ==
true) {
422 cols[count++] = cols[j];
425 rowPtr[i] = newStart;
428 rowPtr[nRows] = count;
431 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
434 for (decltype(dofPresent.
size()) i = 0; i < dofPresent.
size(); i++)
435 if (dofPresent[i] == 1) map[count++] = Teuchos::as<LocalOrdinal>(i);
439 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
440 void VariableDofLaplacianFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::assignGhostLocalNodeIds(
const Teuchos::RCP<const Map>& rowDofMap,
const Teuchos::RCP<const Map>& colDofMap, std::vector<LocalOrdinal>& myLocalNodeIds,
const std::vector<LocalOrdinal>& dofMap,
size_t maxDofPerNode,
size_t& nLocalNodes,
size_t& nLocalPlusGhostNodes,
Teuchos::RCP<
const Teuchos::Comm<int> > comm)
const {
441 size_t nLocalDofs = rowDofMap->getLocalNumElements();
442 size_t nLocalPlusGhostDofs = colDofMap->getLocalNumElements();
454 for (
size_t i = 0; i < localNodeIdsTemp->getLocalLength(); i++)
455 localNodeIdsTempData[i] = std::floor<LocalOrdinal>(dofMap[i] / maxDofPerNode);
458 localNodeIds->doImport(*localNodeIdsTemp, *importer,
Xpetra::INSERT);
470 for (
size_t i = 0; i < myProcTemp->getLocalLength(); i++)
471 myProcTempData[i] = Teuchos::as<LocalOrdinal>(comm->getRank());
488 std::vector<size_t> location(nLocalPlusGhostDofs - nLocalDofs + 1);
489 std::vector<size_t> tempId(nLocalPlusGhostDofs - nLocalDofs + 1);
490 std::vector<size_t> tempProc(nLocalPlusGhostDofs - nLocalDofs + 1);
492 size_t notProcessed = nLocalDofs;
493 size_t tempIndex = 0;
494 size_t first = tempIndex;
497 while (notProcessed < nLocalPlusGhostDofs) {
498 neighbor = myProcData[notProcessed];
500 location[tempIndex] = notProcessed;
501 tempId[tempIndex++] = localNodeIdsData[notProcessed];
502 myProcData[notProcessed] = -1 - neighbor;
504 for (
size_t i = notProcessed + 1; i < nLocalPlusGhostDofs; i++) {
505 if (myProcData[i] == neighbor) {
506 location[tempIndex] = i;
507 tempId[tempIndex++] = localNodeIdsData[i];
511 this->MueLu_az_sort<size_t>(&(tempId[first]), tempIndex - first, &(location[first]), NULL);
512 for (
size_t i = first; i < tempIndex; i++) tempProc[i] = neighbor;
516 while ((notProcessed < nLocalPlusGhostDofs) && (myProcData[notProcessed] < 0))
526 if (nLocalDofs > 0) nLocalNodes = localNodeIdsData[nLocalDofs - 1] + 1;
528 nLocalPlusGhostNodes = nLocalNodes;
529 if (nLocalDofs < nLocalPlusGhostDofs) nLocalPlusGhostNodes++;
536 for (
size_t i = nLocalDofs + 1; i < nLocalPlusGhostDofs; i++) {
537 size_t lagged = nLocalPlusGhostNodes - 1;
540 if ((tempId[i - nLocalDofs] != tempId[i - 1 - nLocalDofs]) ||
541 (tempProc[i - nLocalDofs] != tempProc[i - 1 - nLocalDofs]))
542 nLocalPlusGhostNodes++;
543 tempId[i - 1 - nLocalDofs] = lagged;
545 if (nLocalPlusGhostDofs > nLocalDofs)
546 tempId[nLocalPlusGhostDofs - 1 - nLocalDofs] = nLocalPlusGhostNodes - 1;
549 for (
size_t i = 0; i < nLocalDofs; i++)
550 myLocalNodeIds[i] = std::floor<LocalOrdinal>(dofMap[i] / maxDofPerNode);
553 for (
size_t i = nLocalDofs; i < nLocalPlusGhostDofs; i++)
554 myLocalNodeIds[location[i - nLocalDofs]] = tempId[i - nLocalDofs];
VariableDofLaplacianFactory()
Constructor.
void buildPaddedMap(const Teuchos::ArrayRCP< const LocalOrdinal > &dofPresent, std::vector< LocalOrdinal > &map, size_t nDofs) const
MueLu::DefaultLocalOrdinal LocalOrdinal
void buildLaplacian(const Teuchos::ArrayRCP< size_t > &rowPtr, const Teuchos::ArrayRCP< LocalOrdinal > &cols, Teuchos::ArrayRCP< Scalar > &vals, const size_t &numdim, const RCP< Xpetra::MultiVector< typename Teuchos::ScalarTraits< Scalar >::magnitudeType, LocalOrdinal, GlobalOrdinal, Node > > &ghostedCoords) const
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void DeclareInput(Level ¤tLevel) const
Input.
void assignGhostLocalNodeIds(const Teuchos::RCP< const Map > &rowDofMap, const Teuchos::RCP< const Map > &colDofMap, std::vector< LocalOrdinal > &myLocalNodeIds, const std::vector< LocalOrdinal > &dofMap, size_t maxDofPerNode, size_t &nLocalNodes, size_t &nLocalPlusGhostNodes, Teuchos::RCP< const Teuchos::Comm< int > > comm) const
static const NoFactory * get()
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Class that holds all level-specific information.
void Build(Level ¤tLevel) const
Build an object with this factory.
static Teuchos::ArrayRCP< const bool > DetectDirichletRowsExt(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, bool &bHasZeroDiagonal, const Magnitude &tol=Teuchos::ScalarTraits< Scalar >::zero())
Detect Dirichlet rows (extended version)
static RCP< Vector > Build(const Teuchos::RCP< const Map > &map, bool zeroOut=true)
void squeezeOutNnzs(Teuchos::ArrayRCP< size_t > &rowPtr, Teuchos::ArrayRCP< LocalOrdinal > &cols, Teuchos::ArrayRCP< Scalar > &vals, const std::vector< bool > &keep) const
Exception throws to report errors in the internal logical of the program.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need's value has been saved.
ArrayView< T > view(size_type lowerOffset, size_type size) const