Repository navigation
Expand file tree
/
Copy pathtopol.cpp
More file actions
144 lines (128 loc) · 3.64 KB
/
Copy pathtopol.cpp
File metadata and controls
144 lines (128 loc) · 3.64 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdlib.h>
#include <stdio.h>
void topol(const int n, const int nt, const int n1, const int * const *tetra, int &nterm,
int *&iat, int *&ja){
int I1[4];
// Overestimate number of non-zeroes
nterm = n1*n;
// Init ja_tmp
int *ja_tmp = (int*) malloc(nterm*sizeof(int));
for (int i = 0; i< nterm; i++) ja_tmp[i] = -1;
for (int k = 0; k < nterm; k++){
if ( (k/n1)*n1 == k ) ja_tmp[k]=k/n1;
}
// Analize all tetrahedrons ( nt )
for (int k = 0; k < nt; k++){
for (int i = 0; i < 4; i++) I1[i] = tetra[k][i];
// Sort tet nodes
for (int j = 0; j < 3; j++){
for (int i = 0; i < 3 - j; i++){
if ( I1[i] > I1[i+1] ){
int aux=I1[i];
I1[i]=I1[i+1];
I1[i+1]=aux;
}
}
}
// Build ja_tmp array
for (int i = 0; i < 3; i++){
int j = i+1;
for (int l = j; l < 4; l++){
int m = n1*I1[i]+l-i;
int MCONTR = n1*(I1[i]+1);
bool continua = true;
while ( ((I1[l]-ja_tmp[m]) < 0) || ((I1[l]-ja_tmp[m]) > 0) ){
if ((I1[l]-ja_tmp[m]) > 0){
if (ja_tmp[m] == -1 ){
ja_tmp[m] = I1[l];
} else {
m++;
if ( (m-MCONTR)>=0 ){
printf("1) Error for tetra %d unknown %d\n",k,I1[l]);
printf("Increase n1\n");
int ind = n1*I1[i];
for (int ii = ind; ii < ind+n1; ii++) printf("%d\n",ja_tmp[ii]);
fflush(stdin);
fprintf(stderr, "topol failed\n");
exit(1);
}
}
} else if ( (I1[l]-ja_tmp[m]) < 0 ){
int mm = m + 1;
while (ja_tmp[mm] > -1){
mm++;
if ( m-MCONTR>=0){
printf("2) Error for tetra %d unknown %d\n",k,I1[l]);
printf("Increase n1\n");
int ind = n1*I1[i];
for (int ii = ind; ii < ind+n1; ii++) printf("%d\n",ja_tmp[ii]);
fflush(stdin);
fprintf(stderr, "topol failed\n");
exit(1);
}
}
while (ja_tmp[mm] == -1 ){
ja_tmp[mm] = ja_tmp[mm-1];
mm--;
while ( mm > m ){
ja_tmp[mm] = ja_tmp[mm-1];
mm--;
}
}
ja_tmp[m]=I1[l];
}
} // while loop
} // l loop
} // i loop
} // k loop
// Build iat array
int *iat_tmp = (int*) malloc((n+1)*sizeof(int));
iat_tmp[0] = 0;
int m = 0;
int j = 0;
for (int k = 0; k < nterm; k += n1){
for (int i = 0; i < n1; i++){
if ( ja_tmp[k+i] >= 0){
m++;
}
}
j++;
iat_tmp[j] = m;
}
// Compact ja_tmp
m = 0;
for (int k = 0; k < nterm; k++){
if (ja_tmp[k] >= 0){
ja_tmp[m] = ja_tmp[k];
m++;
}
}
// Complete the matrix
nterm = 2*m-n;
iat = (int*) malloc((n+1)*sizeof(int));
ja = (int*) malloc(nterm*sizeof(int));
int *ptrow = (int*) malloc(n*sizeof(int));
for (int i = 0; i < n; i++) ptrow[i] = 0;
for (int i = 0; i < n; i++){
for (int j = iat_tmp[i]; j < iat_tmp[i+1]; j++){
int jcol = ja_tmp[j];
ptrow[i]++;
if (jcol != i) ptrow[jcol]++;
}
}
iat[0] = 0;
for (int i = 0; i < n; i++){
iat[i+1] = iat[i] + ptrow[i];
ptrow[i] = iat[i];
}
for (int i = 0; i < n; i++){
for (int j = iat_tmp[i]; j < iat_tmp[i+1]; j++){
int jcol = ja_tmp[j];
ja[ptrow[i]++] = jcol;
if (i != jcol) ja[ptrow[jcol]++] = i;
}
}
free(iat_tmp);
free(ja_tmp);
free(ptrow);
}