### graph matrix

I have a finite element mesh with 4 element and 6 node;

the element connectivity is

element 1: 1 2 4
element 2: 1 4 5
element 3: 2 3 4
element 4: 5 4 6

I need a matrix 6*6 showing the connectivity between nodes. for example the first row shows me the connection between node 1 and others and so on;

node 1: 1 2 0 4 5 0
node 2: 1 2 3 5 0 0
node 3: 0 2 3 4 0 0
node 4: 1 2 3 4 5 6
node 5: 1 0 0 4 5 6
node 6: 0 0 0 4 5 6

I tried the below code but it doesn't work properly. anybody can help me please?

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````int node_0, node_1, node_2; int counter[n_node]; int node_connec[n_node][n_node] = { 0 }; for (int i = 0; i < n_node; i++) { counter[i] = 0; } for (int i = 0; i < local_N; i++) { node_0 = connectivity[i][1]; node_1 = connectivity[i][2]; node_2 = connectivity[i][3]; counter[node_0] = counter[node_0] + 1; node_connec[node_0][counter[node_0]] = node_0 + 1; counter[node_0] = counter[node_0] + 1; node_connec[node_0][counter[node_0]] = node_1 + 1; counter[node_0] = counter[node_0] + 1; node_connec[node_0][counter[node_0]] = node_2 + 1; counter[node_1] = counter[node_1] + 1; node_connec[node_1][counter[node_1]] = node_1 + 1; counter[node_1] = counter[node_1] + 1; node_connec[node_1][counter[node_1]] = node_2 + 1; counter[node_1] = counter[node_1] + 1; node_connec[node_1][counter[node_1]] = node_0 + 1; counter[node_2] = counter[node_2] + 1; node_connec[node_2][counter[node_2]] = node_2 + 1; counter[node_2] = counter[node_2] + 1; node_connec[node_2][counter[node_2]] = node_1 + 1; counter[node_2] = counter[node_2] + 1; node_connec[node_2][counter[node_2]] = node_0 + 1; } ``````
Last edited on
I'm not sure what your code is trying to do.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394`` ``````#include #include void displayElements(const std::vector>& elements, std::ostream& out); int getMaxElement(const std::vector>& elements); void sizeAdjacencyMatrix(std::vector>& adjacencies, int maxElement); void findAdjacencies(std::vector> adjacencies, const std::vector>& elements); void displayAdjacencies(const std::vector>& adjacencies, std::ostream& out); int main() { std::vector> elements{ {1, 2, 4}, {1, 4, 5}, {2, 3, 4}, {5, 4, 6} }; std::vector> adjacencies; displayElements(elements, std::cout); std::cout << std::endl; int maxElement = getMaxElement(elements); sizeAdjacencyMatrix(adjacencies, maxElement); findAdjacencies(adjacencies, elements); displayAdjacencies(adjacencies, std::cout); } void displayElements(const std::vector>& elements, std::ostream& out) { for (size_t i = 0; i < elements.size(); ++i) { out << "element " << (i + 1) << ":"; for (int e : elements[i]) { out << " " << e; } out << std::endl; } } int getMaxElement(const std::vector>& elements) { int maxElement = 0; for (auto element : elements) { for (int e : element) { if (e > maxElement) { maxElement = e; } } } return maxElement; } void sizeAdjacencyMatrix(std::vector>& adjacencies, int maxElement) { adjacencies.resize(maxElement); for (auto& adjacency : adjacencies) { adjacency.resize(maxElement); } } void findAdjacencies(std::vector> adjacencies, const std::vector>& elements) { for (auto element : elements) { for (size_t idx = 0; idx < element.size(); ++idx) { for (size_t other = idx; other < element.size(); ++other) { int node1 = element[idx]; int node2 = element[other]; int node1Idx = node1 - 1; int node2Idx = node2 - 1; adjacencies[node1Idx][node2Idx] = node2; adjacencies[node2Idx][node1Idx] = node1; } } } } void displayAdjacencies(const std::vector>& adjacencies, std::ostream& out) { for (size_t i = 0; i < adjacencies.size(); ++i) { out << "node " << (i + 1) << ":"; for (int n : adjacencies[i]) { out << " " << n; } out << std::endl; } }``````
I need to obtain the node connectivity for a big mesh. Then,in this case I need to obtain row_ptr and col_ind for csr format to assemble the global stiffness matrix for finite element. After that, I need to use MPI for this strategy. Imagine I have two MPI process. I broadcasted the element connectivity and coordinate to all processors. Then I should obtain row_ptr and col_ind in each process, for this I have to drive node connectivity in each process, assemble the matrix in each process and at the end with reduce command I can sum all them up into process zero.
@resabzr,

When I said "I'm not sure what your code is trying to do," I just meant that your code was confusing. I knew you were trying to find the connectivity, but your code was too convoluted, and I didn't want to take the time to figure out what it was really doing. I decided starting over was cleaner.

I can't tell from your last post if you are clear now or if you still have questions on creating an adjacency matrix. If you have more questions, go ahead and ask them.

If you get to questions specifically about FE and multiprocessor calculations I'll bow out because I am not experienced in those areas. But others here will be able to help you (I would expect).