Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
382 lines (299 loc) · 10.1 KB
/
Copy pathmain.cpp
File metadata and controls
382 lines (299 loc) · 10.1 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <cstdlib>
using namespace std;
#include <string.h>
#include <math.h>
#include <omp.h>
#include <chrono>
#include <vector>
#include <set>
#include <algorithm>
#include "topol.h"
#include "gmres.h"
#include "utilities.h"
// Main---------------------------------------------------------------------------------------
int main(int argc, const char* argv[]){
//Argument check using the terminal
if (argc < 6) {
printf("Too few arguments. \n Usage: [tetra file] [coord file] [np] [maxit] [restart] \n");
exit(1);
}
int np = atoi(argv[3]); // number of threads
int maxit = atoi(argv[4]); // max iteration GMRES
int restart = atoi(argv[5]); // Restart parameter GMRES
// Read tetrahedrons
ifstream tetra_file(argv[1]);
// Read header
int ntet, nodes_per_elem, base;
tetra_file >> ntet >> nodes_per_elem >> base; // number of tetra elements
int *tetra_buf = (int*) malloc(4*ntet*sizeof(int)); // Continue allocation of tetra elements
int **tetra = (int**) malloc(ntet*sizeof(int*)); // Puntatori ai nodi di ciascun tetraedro
int k = 0;
int junk;
for (int i=0; i<ntet; i++){
tetra[i] = &(tetra_buf[k]);
k += 4;
tetra_file >> junk;
for (int j=0; j < 4; j++){
tetra_file >> tetra[i][j];
}
tetra_file >> junk;
}
// Close the input file
tetra_file.close();
//Set C-style of tet: needed to convert file in base 1 (Fortran style) in base 0
for (int i = 0; i < ntet; i++)
for (int j = 0; j < 4; j++) tetra[i][j]--;
//Number of equations
int nn = 0;
for (int i = 0; i < ntet; i++)
for (int j = 0; j < 4; j++)
if (tetra[i][j] > nn) nn = tetra[i][j];
nn++;
printf("Number of DOF: %d \n", nn);
// Topology -----------------------------------------------------------------
int nterm;
int n1 = 600;
int *iat = nullptr;
int *ja = nullptr;
topol(nn, ntet, n1, tetra, nterm, iat, ja);
//topol2(nn, ntet, tetra, nterm, iat, ja);
printf("Topology created \n");
// Now we have iat and ja ready to be used
// -----------------------------------------
ifstream coord_file(argv[2]);
// Read header
int nnodes, dim, junk1, junk2;
coord_file >> nnodes >> dim >> junk1 >> junk2;
double *coord_buff = (double*) malloc(3*nnodes*sizeof(double)); // we are in 3D
double **coord = (double**) malloc(nnodes*sizeof(double*));
for (int i = 0; i < nn; i++) coord[i] = nullptr;
for (int i = 0; i < nnodes; i++) {
int id;
coord_file >> id;
id--;
coord[id] = &coord_buff[3 * id];
coord_file >> coord[id][0]
>> coord[id][1]
>> coord[id][2];
}
coord_file.close();
for (int k = 0; k < ntet; k++) {
for (int i = 0; i < 4; i++) {
if (tetra[k][i] >= nnodes) {
printf("Tetra %d use nodo %d ma coor has only %d nodi\n", k, tetra[k][i], nnodes);
exit(1);
}
}
}
for (int i = 0; i < nn; i++) {
if (coord[i] == nullptr) {
printf("Node FEM %d NO coordinates\n", i);
exit(1);
}
}
//------------------------------------------
// Diffusion and flow velocity
double* D = (double*) malloc(3*sizeof(double));
D[0] = 0.1; D[1] = 0.1; D[2] = 0.6;
double* v = (double*) malloc(3*sizeof(double));
v[0]=1.0; v[1] = 3.0; v[2] = 1.0;
printf("D: ");
printf("%f %f %f\n", D[0],D[1],D[2]);
printf("v: ");
printf("%f %f %f\n", v[0],v[1],v[2]);
//-----------------------------------------
// Assembly: we have to build H B P csr matrix -> coefH, coefB, coefP
double start;
double end;
start = omp_get_wtime();
double* coefH = (double*) malloc (nterm *sizeof(double));
double* coefB = (double*) malloc (nterm *sizeof(double));
double* coefP = (double*) malloc (nterm *sizeof(double));
for (int i = 0; i<nterm; i++){
coefH[i]= 0;
coefB[i] = 0;
coefP[i] = 0;
}
# pragma omp parallel num_threads(np)
{
double* a = (double*) malloc(4*sizeof(double));
double* b = (double*) malloc(4*sizeof(double));
double* c = (double*) malloc(4*sizeof(double));
double* d = (double*) malloc(4*sizeof(double));
double *Hloc_buf = (double*) malloc(4*4*sizeof(double));
double **Hloc = (double**) malloc(4*sizeof(double*));
double *Ploc_buf = (double*) malloc(4*4*sizeof(double));
double **Ploc =(double**) malloc(4*sizeof(double*));
double *Bloc_buf = (double*) malloc(4*4*sizeof(double));
double **Bloc = (double**) malloc(4*sizeof(double*));
int kk =0;
for (int i = 0; i<4; i++){
Hloc[i] = &(Hloc_buf[kk]);
Ploc[i] = &(Ploc_buf[kk]);
Bloc[i] = &(Bloc_buf[kk]);
kk += 4;
}
# pragma omp for
for (int jj=0; jj < ntet; jj++){
int* tet = tetra[jj];
double* n0 = coord[tet[0]];
double* n1 = coord[tet[1]];
double* n2 = coord[tet[2]];
double* n3 = coord[tet[3]];
double vol = (det3(n1,n2,n3) -
det3(n0,n2,n3) +
det3(n0,n1,n3) -
det3(n0,n1,n2))/6;
for (int ii=0; ii < 4; ii++){
int idx[3]; int cnt = 0;
for (int jj = 0; jj < 4; jj++){
if (ii == jj){
continue;
}
idx[cnt] = jj;
cnt += 1;
}
double segno = pow(-1, ii);
double* nj = coord[tet[idx[0]]];
double* nk = coord[tet[idx[1]]];
double* nm = coord[tet[idx[2]]];
a[ii] = segno * det3(nj,nk,nm);
b[ii] = segno * (-1) * (nk[1]*nm[2] -nm[1]*nk[2] - (nj[1]*nm[2]-nj[2]*nm[1]) + nj[1]*nk[2]-nj[2]*nk[1]);
c[ii] = segno * (nk[0]*nm[2]-nm[0]*nk[2] - (nj[0]*nm[2]-nj[2]*nm[0])+nj[0]*nk[2]-nj[2]*nk[0]);
d[ii] = segno * (nk[1]*nm[0]-nm[1]*nk[0] - (nj[1]*nm[0]-nj[0]*nm[1])+nj[1]*nk[0]-nj[0]*nk[1]); // I swap col 1 with col 2 so the det change sign
}
for (int i = 0; i<4; i++){
double* Hi = Hloc[i];
double* Pi = Ploc[i];
double* Bi = Bloc[i];
for (int j=0; j<4; j++){
Hi[j] = (D[0]*b[i]*b[j] + D[1]*c[i]*c[j] + D[2]*d[i]*d[j])/ (36 * abs(vol));
Pi[j] = abs(vol)/20;
Bi[j] = sign(vol)/24 * (v[0]*b[j]+v[1]*c[j]+v[2]*d[j]);
}
Pi[i] *=2;
}
// locate them inside coefH, coefP, coefB
for (int i=0; i<4; i++) {
// tet[i] gives the row index
double* Hi = Hloc[i];
double* Pi = Ploc[i];
double* Bi = Bloc[i];
for (int j=0; j<4; j++){
// in ja from iat[tet[i]] look for tet[j]
int curr_node = tet[i];
for (int ii=iat[curr_node]; ii < iat[curr_node+1]; ii++){
int jjj = tet[j];
if (ja[ii] == jjj){
#pragma omp atomic
coefH[ii] += Hi[j];
#pragma omp atomic
coefP[ii] += Pi[j];
#pragma omp atomic
coefB[ii] += Bi[j];
}
}
}
}
}
free(a);
free(b);
free(c);
free(d);
free(Hloc);
free(Bloc);
free(Ploc);
free(Hloc_buf);
free(Ploc_buf);
free(Bloc_buf);
}
end = omp_get_wtime();
printf("Assembly time: %f\n", end - start);
// GMRES-----------------------------------------------------------------------------------
double* coefA = (double*) malloc (nterm * sizeof(double));
double* coefAmod = (double*) malloc(nterm *sizeof(double)); //matrix to modify in order to impose the DC bc.
double dt = 1;
for (int i = 0; i < nterm; i++){
coefA[i] = coefB[i] + coefH[i] + coefP[i]/dt; //
coefAmod[i] = coefA[i];
}
double* bdval = (double*) malloc(nnodes*sizeof(double));
for (int i = 0; i < nnodes; i++){
if ((coord[i][0] < 1e-10)&&(coord[i][1] < .5 + 1e-10)&&(coord[i][2]< 1e-10)){
bdval[i] = 1.0;
}
else {
bdval[i] = 0.0;
}
}
double* Abdval = (double*) calloc(nnodes, sizeof(double));
matcsrvecprod(nn, iat, ja, coefA, bdval, Abdval, np);
// Impose BC----------------------------------------------------------------------------
for (int iii =0; iii < nn; iii++){
if (bdval[iii]){ // Set the row equal to zero except for the diagonal element
for (int j = iat[iii]; j < iat[iii+1]; j++){
if (ja[j] == iii){
coefAmod[j] = 1.0;
}
else {
coefAmod[j] = 0.0;
}
}
}
else { // Set the columns where q[j] = 1 equal to 0
for (int j = iat[iii]; j < iat[iii+1]; j++){
if (bdval[ja[j]]){
coefAmod[j] = 0.0;
}
}
}
}
// RHS assembly-----------------------------------------------------------------------------
double* rhs = (double*) calloc(nnodes, sizeof(double));
matcsrvecprod(nn, iat, ja, coefP, bdval, rhs, np);
for (int i = 0; i < nnodes; i++){
if (bdval[i]){
rhs[i] = bdval[i];
}
else {
rhs[i] = rhs[i]/dt - Abdval[i]; // (P/dt)u_prev - Abdval
}
}
// Solution vector---------------------------------------------------------------------------
double* S = (double*) calloc(nnodes, sizeof(double));
double tol = 1e-9;
start = omp_get_wtime();
gmres(nnodes, iat, ja, coefAmod, rhs, tol, maxit, restart, np, S);
end = omp_get_wtime();
printf("GMRES time: %f\n", end - start);
// Relative error---------------------------------------------------------------------------
double* AS = (double*) malloc(nnodes*sizeof(double));
double* rtrue = (double*) calloc(nnodes, sizeof(double));
matcsrvecprod(nn, iat, ja, coefAmod, S, AS, np);
double num = 0.0, den = 0.0;
for (int i=0; i<nnodes; i++) rtrue[i] = rhs[i] - AS[i];
printf("Final residual ||b - A*x|| = %1.4e\n", norm(rtrue, nnodes, np));
// free memory
free(coord);
free(coord_buff);
free(tetra);
free(tetra_buf);
free(iat);
free(ja);
free(D);
free(v);
free(coefH);
free(coefP);
free(coefB);
free(coefA);
free(coefAmod);
free(bdval);
free(Abdval);
free(S);
free(rhs);
free(AS);
free(rtrue);
}