Skip to content

Commit 4d4cbae

Browse files
committed
Refactor BuildSystemStructure to support row/column block connectivity and improve error handling
1 parent ed79031 commit 4d4cbae

2 files changed

Lines changed: 51 additions & 7 deletions

File tree

applications/TrilinosApplication/tests/cpp_tests/test_trilinos_space.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ KRATOS_TEST_CASE_IN_SUITE(TrilinosBuildSystemStructure, KratosTrilinosApplicatio
845845
pMap = Kratos::make_shared<Epetra_Map>(-1, local_size, local_ids.data(), 0, epetra_comm);
846846

847847
TrilinosSparseSpaceType::BuildSystemStructure(
848-
epetra_comm, local_size, first_my_id, 5, all_equation_ids,
848+
epetra_comm, local_size, first_my_id, 5, all_equation_ids, all_equation_ids,
849849
pA, pb, pDx, pReactions, system_size, pMap
850850
);
851851

@@ -856,6 +856,43 @@ KRATOS_TEST_CASE_IN_SUITE(TrilinosBuildSystemStructure, KratosTrilinosApplicatio
856856
KRATOS_EXPECT_EQ(static_cast<std::size_t>(system_size), TrilinosSparseSpaceType::Size1(*pA));
857857
}
858858

859+
KRATOS_TEST_CASE_IN_SUITE(TrilinosBuildSystemStructureRowColumnBlocks, KratosTrilinosApplicationMPITestSuite)
860+
{
861+
const auto& r_comm = Testing::GetDefaultDataCommunicator();
862+
auto raw_mpi_comm = MPIDataCommunicator::GetMPICommunicator(r_comm);
863+
Epetra_MpiComm epetra_comm(raw_mpi_comm);
864+
865+
const int local_size = 2;
866+
const int first_my_id = r_comm.Rank() * local_size;
867+
const int system_size = 2 * r_comm.Size();
868+
869+
// Rectangular row/column blocks: one diagonal and one cross entry per rank
870+
std::vector<std::vector<int>> all_row_equation_ids = {{first_my_id}, {first_my_id}};
871+
std::vector<std::vector<int>> all_col_equation_ids = {{first_my_id}, {first_my_id + 1}};
872+
873+
TrilinosSparseSpaceType::MatrixPointerType pA;
874+
TrilinosSparseSpaceType::VectorPointerType pb;
875+
TrilinosSparseSpaceType::VectorPointerType pDx;
876+
TrilinosSparseSpaceType::VectorPointerType pReactions;
877+
878+
TrilinosSparseSpaceType::MapPointerType pMap;
879+
std::vector<int> local_ids(local_size);
880+
for (int i = 0; i < local_size; i++) local_ids[i] = first_my_id + i;
881+
pMap = Kratos::make_shared<Epetra_Map>(-1, local_size, local_ids.data(), 0, epetra_comm);
882+
883+
TrilinosSparseSpaceType::BuildSystemStructure(
884+
epetra_comm, local_size, first_my_id, 5, all_row_equation_ids, all_col_equation_ids,
885+
pA, pb, pDx, pReactions, system_size, pMap
886+
);
887+
888+
KRATOS_EXPECT_FALSE(TrilinosSparseSpaceType::IsNull(pA));
889+
KRATOS_EXPECT_FALSE(TrilinosSparseSpaceType::IsNull(pb));
890+
KRATOS_EXPECT_FALSE(TrilinosSparseSpaceType::IsNull(pDx));
891+
KRATOS_EXPECT_FALSE(TrilinosSparseSpaceType::IsNull(pReactions));
892+
KRATOS_EXPECT_EQ(static_cast<std::size_t>(system_size), TrilinosSparseSpaceType::Size1(*pA));
893+
KRATOS_EXPECT_EQ(2 * r_comm.Size(), pA->NumGlobalNonzeros());
894+
}
895+
859896
KRATOS_TEST_CASE_IN_SUITE(TrilinosBuildConstraintsStructure, KratosTrilinosApplicationMPITestSuite)
860897
{
861898
const auto& r_comm = Testing::GetDefaultDataCommunicator();

applications/TrilinosApplication/trilinos_space.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -907,12 +907,13 @@ class TrilinosSpace
907907
}
908908

909909
/**
910-
* @brief Build Epetra FECrsGraph and create new system matrix + vectors.
910+
* @brief Build Epetra FECrsGraph and create new system matrix + vectors from row/column block connectivity.
911911
* @param rComm The communicator considered
912912
* @param LocalSize The local size of the system
913913
* @param FirstMyId The first global id owned by this rank
914914
* @param GuessRowSize The guess row size for the graph construction
915-
* @param rAllEquationIds The list of lists of equation ids for each local row
915+
* @param rAllRowEquationIds The list of row equation-id blocks
916+
* @param rAllColEquationIds The list of column equation-id blocks
916917
* @param rpA The pointer to the matrix to be created
917918
* @param rpb The pointer to the right-hand side vector to be created
918919
* @param rpDx The pointer to the solution vector to be created
@@ -925,7 +926,8 @@ class TrilinosSpace
925926
const IndexType LocalSize,
926927
const int FirstMyId,
927928
const int GuessRowSize,
928-
const std::vector<std::vector<int>>& rAllEquationIds,
929+
const std::vector<std::vector<int>>& rAllRowEquationIds,
930+
const std::vector<std::vector<int>>& rAllColEquationIds,
929931
MatrixPointerType& rpA,
930932
VectorPointerType& rpb,
931933
VectorPointerType& rpDx,
@@ -934,11 +936,16 @@ class TrilinosSpace
934936
MapPointerType pMap
935937
)
936938
{
939+
KRATOS_ERROR_IF(rAllRowEquationIds.size() != rAllColEquationIds.size())
940+
<< "BuildSystemStructure: row and column block lists must have the same size" << std::endl;
941+
937942
// Create graph
938943
Epetra_FECrsGraph graph(::Copy, *pMap, GuessRowSize);
939-
for (const auto& r_ids : rAllEquationIds) {
940-
if (r_ids.size() != 0) {
941-
graph.InsertGlobalIndices(r_ids.size(), r_ids.data(), r_ids.size(), r_ids.data());
944+
for (std::size_t i_block = 0; i_block < rAllRowEquationIds.size(); ++i_block) {
945+
const auto& r_row_ids = rAllRowEquationIds[i_block];
946+
const auto& r_col_ids = rAllColEquationIds[i_block];
947+
if (!r_row_ids.empty() && !r_col_ids.empty()) {
948+
graph.InsertGlobalIndices(r_row_ids.size(), r_row_ids.data(), r_col_ids.size(), r_col_ids.data());
942949
}
943950
}
944951
graph.GlobalAssemble();

0 commit comments

Comments
 (0)