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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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.

How about something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <vector>

void displayElements(const std::vector<std::vector<int>>& elements, std::ostream& out);
int getMaxElement(const std::vector<std::vector<int>>& elements);
void sizeAdjacencyMatrix(std::vector<std::vector<int>>& adjacencies, int maxElement);
void findAdjacencies(std::vector<std::vector<int>> adjacencies, const std::vector<std::vector<int>>& elements);
void displayAdjacencies(const std::vector<std::vector<int>>& adjacencies, std::ostream& out);

int main()
{
    std::vector<std::vector<int>> elements{ {1, 2, 4}, {1, 4, 5}, {2, 3, 4}, {5, 4, 6} };
    std::vector<std::vector<int>> 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<std::vector<int>>& 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<std::vector<int>>& elements)
{
    int maxElement = 0;
    for (auto element : elements)
    {
        for (int e : element)
        {
            if (e > maxElement)
            {
                maxElement = e;
            }
        }
    }

    return maxElement;
}

void sizeAdjacencyMatrix(std::vector<std::vector<int>>& adjacencies, int maxElement)
{
    adjacencies.resize(maxElement);
    for (auto& adjacency : adjacencies)
    {
        adjacency.resize(maxElement);
    }
}


void findAdjacencies(std::vector<std::vector<int>> adjacencies, const std::vector<std::vector<int>>& 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<std::vector<int>>& 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;
    }
}
Thanks for your answer.
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).
Thanks for your answer.
Registered users can post here. Sign in or register to post.