10 #ifndef MUELU_INTERFACEAGGREGATIONFACTORY_DEF_HPP_
11 #define MUELU_INTERFACEAGGREGATIONFACTORY_DEF_HPP_
13 #include <Xpetra_Map.hpp>
14 #include <Xpetra_MapFactory.hpp>
15 #include <Xpetra_StridedMap.hpp>
17 #include "MueLu_Aggregates.hpp"
18 #include "MueLu_AmalgamationFactory.hpp"
19 #include "MueLu_AmalgamationInfo.hpp"
27 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
31 validParamList->
set<
RCP<const FactoryBase>>(
"A", Teuchos::null,
"Generating factory of A (matrix block related to dual DOFs)");
32 validParamList->
set<
RCP<const FactoryBase>>(
"Aggregates", Teuchos::null,
"Generating factory of the Aggregates (for block 0,0)");
34 validParamList->
set<std::string>(
"Dual/primal mapping strategy",
"vague",
35 "Strategy to represent mapping between dual and primal quantities [node-based, dof-based]");
38 "Generating factory of the DualNodeID2PrimalNodeID map as input data in a Moertel-compatible std::map<LO,LO> to map local IDs of dual nodes to local IDs of primal nodes");
40 "Number of DOFs per dual node");
43 "Generating factory of the primal DOF row map of slave side of the coupling surface");
45 return validParamList;
48 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
50 Input(currentLevel,
"A");
51 Input(currentLevel,
"Aggregates");
55 "Strategy for dual/primal mapping not selected. Please select one of the available strategies.")
56 if (pL.
get<std::string>(
"Dual/primal mapping strategy") ==
"node-based") {
63 Input(currentLevel,
"DualNodeID2PrimalNodeID");
65 }
else if (pL.
get<std::string>(
"Dual/primal mapping strategy") ==
"dof-based") {
69 Input(currentLevel,
"Primal interface DOF map");
75 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
77 const std::string prefix =
"MueLu::InterfaceAggregationFactory::Build: ";
83 const std::string parameterName =
"Dual/primal mapping strategy";
84 if (pL.
get<std::string>(parameterName) ==
"node-based")
85 BuildBasedOnNodeMapping(prefix, currentLevel);
86 else if (pL.
get<std::string>(parameterName) ==
"dof-based")
87 BuildBasedOnPrimalInterfaceDofMap(prefix, currentLevel);
90 "MueLu::InterfaceAggregationFactory::Builld(): Unknown strategy for dual/primal mapping. Set a valid value for the parameter \"" << parameterName <<
"\".")
93 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
95 Level ¤tLevel)
const {
96 using Dual2Primal_type = std::map<LocalOrdinal, LocalOrdinal>;
103 "Number of dual DOFs per node < 0 (default value). Specify a valid \"number of DOFs per dual node\" in the parameter list for the InterfaceAggregationFactory.");
110 if (currentLevel.GetLevelID() == 0)
113 mapNodesDualToPrimal = Get<RCP<Dual2Primal_type>>(currentLevel,
"DualNodeID2PrimalNodeID");
116 const size_t myRank = operatorRangeMap->getComm()->getRank();
118 LocalOrdinal globalNumDualNodes = operatorRangeMap->getGlobalNumElements() / numDofsPerDualNode;
119 LocalOrdinal localNumDualNodes = operatorRangeMap->getLocalNumElements() / numDofsPerDualNode;
121 TEUCHOS_TEST_FOR_EXCEPTION(localNumDualNodes != Teuchos::as<LocalOrdinal>(mapNodesDualToPrimal->size()),
122 std::runtime_error, prefix <<
" MueLu requires the range map and the DualNodeID2PrimalNodeID map to be compatible.");
125 if (numDofsPerDualNode == 1)
126 dualNodeMap = operatorRangeMap;
129 auto comm = operatorRangeMap->getComm();
130 std::vector<GlobalOrdinal> myDualNodes = {};
132 for (
size_t i = 0; i < operatorRangeMap->getLocalNumElements(); i += numDofsPerDualNode)
133 myDualNodes.push_back((operatorRangeMap->getGlobalElement(i) - indexBase) / numDofsPerDualNode + indexBase);
135 dualNodeMap = MapFactory::Build(operatorRangeMap->lib(), globalNumDualNodes, myDualNodes, indexBase, comm);
137 TEUCHOS_TEST_FOR_EXCEPTION(localNumDualNodes != Teuchos::as<LocalOrdinal>(dualNodeMap->getLocalNumElements()),
138 std::runtime_error, prefix <<
" Local number of dual nodes given by user is incompatible to the dual node map.");
161 for (
LocalOrdinal localDualNodeID = 0; localDualNodeID < localNumDualNodes; ++localDualNodeID) {
163 localPrimalNodeID = (*mapNodesDualToPrimal)[localDualNodeID];
166 currentPrimalAggId = primalVertex2AggId[localPrimalNodeID];
170 if (coarseMapNodesPrimalToDual->count(currentPrimalAggId) == 0) {
172 (*coarseMapNodesPrimalToDual)[currentPrimalAggId] = numLocalDualAggregates;
173 (*coarseMapNodesDualToPrimal)[numLocalDualAggregates] = currentPrimalAggId;
174 ++numLocalDualAggregates;
178 dualVertex2AggId[localDualNodeID] = (*coarseMapNodesPrimalToDual)[currentPrimalAggId];
179 dualProcWinner[localDualNodeID] = myRank;
184 Set(currentLevel,
"Aggregates", dualAggregates);
185 Set(currentLevel,
"CoarseDualNodeID2PrimalNodeID", coarseMapNodesDualToPrimal);
189 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
191 const std::string &prefix,
Level ¤tLevel)
const {
204 auto comm = A01->getRowMap()->getComm();
205 const int myRank = comm->getRank();
208 if (currentLevel.GetLevelID() == 0) {
212 primalInterfaceDofRowMap = Get<RCP<const Map>>(currentLevel,
"Primal interface DOF map");
215 if (A01->IsView(
"stridedMaps") && rcp_dynamic_cast<
const StridedMap>(A01->getRowMap(
"stridedMaps")) != Teuchos::null) {
216 auto stridedRowMap = rcp_dynamic_cast<
const StridedMap>(A01->getRowMap(
"stridedMaps"));
217 auto stridedColMap = rcp_dynamic_cast<
const StridedMap>(A01->getColMap(
"stridedMaps"));
218 numDofsPerPrimalNode = Teuchos::as<LocalOrdinal>(stridedRowMap->getFixedBlockSize());
219 numDofsPerDualNode = Teuchos::as<LocalOrdinal>(stridedColMap->getFixedBlockSize());
221 if (numDofsPerPrimalNode != numDofsPerDualNode) {
222 GetOStream(
Warnings) <<
"InterfaceAggregation attempts to work with "
223 << numDofsPerPrimalNode <<
" primal DOFs per node and " << numDofsPerDualNode <<
" dual DOFs per node."
224 <<
"Be careful! Algorithm is not well-tested, if number of primal and dual DOFs per node differ." << std::endl;
229 "InterfaceAggregationFactory could not extract the number of primal DOFs per node from striding information. At least, make sure that StridedMap information has actually been provided.");
231 "InterfaceAggregationFactory could not extract the number of dual DOFs per node from striding information. At least, make sure that StridedMap information has actually been provided.");
252 GlobalOrdinal dualDofOffset = A01->getRowMap()->getMaxAllGlobalIndex() + 1;
257 dualDofMap->getMaxAllGlobalIndex(), dualBlockDim, dualDofOffset, dualDofMap->getIndexBase());
259 dualDofMap->getMinAllGlobalIndex(), dualBlockDim, dualDofOffset, dualDofMap->getIndexBase());
261 GetOStream(
Runtime1) <<
" Dual DOF map: index base = " << dualDofMap->getIndexBase()
262 <<
", block dim = " << dualBlockDim
263 <<
", gid offset = " << dualDofOffset
266 GetOStream(
Runtime1) <<
" [primal / dual] DOFs per node = [" << numDofsPerPrimalNode
267 <<
"/" << numDofsPerDualNode <<
"]" << std::endl;
271 Array<GlobalOrdinal> local_dualNodeId2primalNodeId(gMaxDualNodeId - gMinDualNodeId + 1, -GO_ONE);
275 Array<GlobalOrdinal> local_dualNodeId2primalAggId(gMaxDualNodeId - gMinDualNodeId + 1, -GO_ONE);
277 Array<GlobalOrdinal> dualDofId2primalDofId(primalInterfaceDofRowMap->getGlobalNumElements(), -GO_ONE);
278 Array<GlobalOrdinal> local_dualDofId2primalDofId(primalInterfaceDofRowMap->getGlobalNumElements(), -GO_ONE);
281 const size_t numMyPrimalInterfaceDOFs = primalInterfaceDofRowMap->getLocalNumElements();
282 for (
size_t r = 0; r < numMyPrimalInterfaceDOFs; r += numDofsPerPrimalNode) {
283 GlobalOrdinal gPrimalRowId = primalInterfaceDofRowMap->getGlobalElement(r);
285 if (A01->getRowMap()->isNodeGlobalElement(gPrimalRowId))
287 const LocalOrdinal lPrimalRowId = A01->getRowMap()->getLocalElement(gPrimalRowId);
289 const LocalOrdinal lPrimalNodeId = lPrimalRowId / numDofsPerPrimalNode;
290 const LocalOrdinal primalAggId = primalVertex2AggId[lPrimalNodeId];
291 const GlobalOrdinal gDualDofId = A01->getDomainMap()->getGlobalElement(r);
296 "PROC: " << myRank <<
" gDualNodeId " << gDualNodeId
297 <<
" is already connected to primal nodeId "
298 << local_dualNodeId2primalNodeId[gDualNodeId - gMinDualNodeId]
299 <<
". This shouldn't be. A possible reason might be: "
300 "Check if parallel distribution of primalInterfaceDofRowMap corresponds "
301 "to the parallel distribution of subblock matrix A01.");
303 local_dualNodeId2primalNodeId[gDualNodeId - gMinDualNodeId] = gPrimalNodeId;
304 local_dualNodeId2primalAggId[gDualNodeId - gMinDualNodeId] = primalAggId;
307 const int dualNodeId2primalNodeIdSize = Teuchos::as<int>(local_dualNodeId2primalNodeId.
size());
309 &local_dualNodeId2primalNodeId[0], &dualNodeId2primalNodeId[0]);
311 &local_dualNodeId2primalAggId[0], &dualNodeId2primalAggId[0]);
316 std::vector<GlobalOrdinal> dualNodes;
317 for (
size_t r = 0; r < A01->getDomainMap()->getLocalNumElements(); r++) {
321 GlobalOrdinal gDualDofId = A01->getDomainMap()->getGlobalElement(r);
323 dualNodes.push_back(gDualNodeId);
327 dualNodes.erase(std::unique(dualNodes.begin(), dualNodes.end()), dualNodes.end());
335 dualAggregates->setObjectLabel(
"UC (dual variables)");
343 std::map<GlobalOrdinal, LocalOrdinal> primalAggId2localDualAggId;
344 for (
size_t lDualNodeID = 0; lDualNodeID < dualNodeMap->getLocalNumElements(); ++lDualNodeID) {
345 const GlobalOrdinal gDualNodeId = dualNodeMap->getGlobalElement(lDualNodeID);
346 const GlobalOrdinal primalAggId = dualNodeId2primalAggId[gDualNodeId - gMinDualNodeId];
347 if (primalAggId2localDualAggId.count(primalAggId) == 0)
348 primalAggId2localDualAggId[primalAggId] = nLocalAggregates++;
349 dualVertex2AggId[lDualNodeID] = primalAggId2localDualAggId[primalAggId];
350 dualProcWinner[lDualNodeID] = myRank;
360 const size_t numMyDualNodes = dualNodeMap->getLocalNumElements();
361 for (
size_t lDualNodeID = 0; lDualNodeID < numMyDualNodes; ++lDualNodeID) {
362 for (
LocalOrdinal dof = 0; dof < numDofsPerDualNode; ++dof) {
363 rowTranslation->push_back(lDualNodeID);
364 colTranslation->push_back(lDualNodeID);
371 A01->getDomainMap(), A01->getDomainMap(), A01->getDomainMap(),
372 fullblocksize, dualDofOffset, blockid, nStridedOffset, stridedblocksize));
374 dualAggregates->SetNumAggregates(nLocalAggregates);
375 dualAggregates->AggregatesCrossProcessors(primalAggregates->AggregatesCrossProcessors());
377 if (dualAggregates->AggregatesCrossProcessors())
378 GetOStream(
Runtime1) <<
"Interface aggregates cross processor boundaries." << std::endl;
380 GetOStream(
Runtime1) <<
"Interface aggregates do not cross processor boundaries." << std::endl;
382 currentLevel.Set(
"Aggregates", dualAggregates,
this);
383 currentLevel.Set(
"UnAmalgamationInfo", dualAmalgamationInfo,
this);
MueLu::DefaultLocalOrdinal LocalOrdinal
const RCP< LOVector > & GetProcWinner() const
Returns constant vector that maps local node IDs to owning processor IDs.
Container class for aggregation information.
void BuildBasedOnNodeMapping(const std::string &prefix, Level ¤tLevel) const
Build dual aggregates based on a given dual-to-primal node mapping.
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)
static const NoFactory * get()
static const GlobalOrdinal DOFGid2NodeId(GlobalOrdinal gid, LocalOrdinal blockSize, const GlobalOrdinal offset, const GlobalOrdinal indexBase)
Translate global (row/column) id to global amalgamation block id.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Class that holds all level-specific information.
void Build(Level ¤tLevel) const override
Build aggregates.
virtual void setObjectLabel(const std::string &objectLabel)
const RCP< LOMultiVector > & GetVertex2AggId() const
Returns constant vector that maps local node IDs to local aggregates IDs.
void DeclareInput(Level ¤tLevel) const override
Specifies the data that this class needs, and the factories that generate that data.
RCP< const ParameterList > GetValidParameterList() const override
Input.
KOKKOS_INLINE_FUNCTION void AggregatesCrossProcessors(const bool &flag)
Record whether aggregates include DOFs from other processes.
int GetLevelID() const
Return level number.
Exception throws to report errors in the internal logical of the program.
Print all warning messages.
#define TEUCHOS_ASSERT(assertion_test)
Description of what is happening (more verbose)
void BuildBasedOnPrimalInterfaceDofMap(const std::string &prefix, Level ¤tLevel) const
Build dual aggregates based on a given interface row map of the primal and dual problem.
std::string description() const
Return a simple one-line description of this object.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
minimal container class for storing amalgamation information
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need's value has been saved.
void SetNumAggregates(LO nAggregates)
Set number of local aggregates on current processor.
Exception throws to report invalid user entry.