linear_system_banded.c 8.51 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
#include <math.h>
#include "mesh.h"
#include "linear_system.h"
#include <stdlib.h>
#include <string.h>

#define CONMAX 12
void connectivity_insert(int *connectivity, int i, int j)
{
  for (int k = 0; k < CONMAX; ++k) {
    int *p = connectivity + CONMAX*i + k;
    if (*p == -1)
      *p = j;
    if (*p == j)
      return;
  }
  printf("ERROR : node %i has more than %i neighbours\n", i, CONMAX);
}

int reverse_cuthill_mckee(Mesh *mesh, int *ordering)
{
  int *node_connectivity = malloc(sizeof(int)*mesh->n_nodes*CONMAX);
  for (int i = 0; i < mesh->n_nodes*CONMAX; ++i) {
    node_connectivity[i] = -1;
  }
  for (int i = 0; i < mesh->n_triangles; ++i) {
    int *tri = mesh->triangles + i*3;
    connectivity_insert(node_connectivity, tri[0], tri[1]);
    connectivity_insert(node_connectivity, tri[1], tri[0]);
    connectivity_insert(node_connectivity, tri[0], tri[2]);
    connectivity_insert(node_connectivity, tri[2], tri[0]);
    connectivity_insert(node_connectivity, tri[1], tri[2]);
    connectivity_insert(node_connectivity, tri[2], tri[1]);
  }
  int *node_degree = malloc(sizeof(int)*mesh->n_nodes);
  for (int i = 0; i < mesh->n_nodes; ++i) {
    node_degree[i] = 0;
    for (int j = 0; j < CONMAX; ++j) {
      if (node_connectivity[CONMAX*i+j] == -1)
        break;
      node_degree[i] += 1;
    }
  }
  int *queue = malloc(sizeof(int)*mesh->n_nodes);
  queue[0] = 0;
  for (int i = 0; i < mesh->n_nodes; ++i){
    ordering[i] = -1;
    if (node_degree[queue[0]] > node_degree[i] )
      queue[0] = i;
  }
  int stage_start = 0;
  int stage_end = 1;
  int queue_end = 1;
  int id = 0;
  while(stage_start != stage_end) {
    for (int i = stage_start; i < stage_end; ++i) {
      int c = queue[i];
      ordering[c] = mesh->n_nodes-1 -(id++);
      for(int j = 0; j < node_degree[c]; ++j) {
        int o = node_connectivity[c*CONMAX+j];
        if (ordering[o] == -1) {
          ordering[o] = -2;
#if 1
          queue[queue_end++] = o;
#else
          int k = stage_end;
          while(k < queue_end && node_degree[queue[k]] < node_degree[o])
            k++;
          for (int l = queue_end; l > k; --l)
            queue[l] = queue[l-1];
          queue[k] = o;
          queue_end++;
#endif
        }
      }
    }
    stage_start = stage_end;
    stage_end = queue_end;
  }
  int final_bandwidth = 0;
  for (int i = 0; i < mesh->n_triangles; ++i) {
    int *tri = mesh->triangles + i*3;
    int m[3] = {ordering[tri[0]], ordering[tri[1]], ordering[tri[2]]};
    int d[3] = {abs(m[0]-m[1]), abs(m[0]-m[2]), abs(m[1]-m[2])};
    if (d[0] > final_bandwidth) final_bandwidth = d[0];
    if (d[1] > final_bandwidth) final_bandwidth = d[1];
    if (d[2] > final_bandwidth) final_bandwidth = d[2];
  }
  printf("n_nodes : %i final bandwidth : %i\n",mesh->n_nodes, final_bandwidth); 
  free(queue);
  free(node_degree);
  free(node_connectivity);
  return final_bandwidth;
}

//from wikipedia
//INPUT: A - array of pointers to rows of a square matrix having dimension N
//       Tol - small tolerance number to detect failure when the matrix is near degenerate
//OUTPUT: Matrix A is changed, it contains both matrices L-E and U as A=(L-E)+U such that P*A=L*U.
//        P - array of N+1 integers containing pivoting of A and P[N] is for determinant computation
//        (P[i] is an index of the row in A placed at the i-th row
static int LUPDecompose(double *__restrict__*__restrict A,int N,double Tol,int *P, int bw){
  int i,j,k;
  for(i=0;i<N;i++)
    P[i]=i; //integer vector corresponding to no pivoting 
  //int c0 = 0, c1 = 0;
  for(i=0;i<N;i++){
    /*double absA;
    double maxA=0.0;
    int imax=i;
    for(k=i;k<N;k++)
      if((absA=fabs(A[k][i]))>maxA){ maxA=absA; imax=k; }
    if(maxA<Tol)return(0); //failure, matrix is degenerate
    if(imax!=i){
      j=P[i]; P[i]=P[imax]; P[imax]=j; //pivoting P
      double *ptr=A[i]; A[i]=A[imax]; A[imax]=ptr; //pivoting rows of A
    }*/
    for(j=i+1;j<i+bw && j<N;j++){
      if(A[j][i] == 0.)
        continue;
      A[j][i]/=A[i][i];
      for(k=i+1;k<i+bw && k<N;k++){
        /*if(fabs(A[i][k]) < 1e-12){
          c1++;
          //continue;
        }*/
        A[j][k]-=A[j][i]*A[i][k];
        //c0++;
      }
    }
  }
  //printf("%g / %i\n", c1*100./c0, c0);
  return(1);  //decomposition done 
}

//INPUT: A,P filled in LUPDecompose; b - rhs vector; N - dimension
//OUTPUT: x - solution vector of A*x=b
static void LUPSolve(double **A,int *P,double *b,int N,double *x, int bw){
  for(int i=0;i<N;i++){
    x[i]=b[P[i]];
    for(int k=(i-bw<0?0:i-bw);k<i;k++)
      x[i]-=A[i][k]*x[k];
  }
  for(int i=N-1;i>=0;i--){
    for(int k=i+1;k<N && k<i+bw;k++)
      x[i]-=A[i][k]*x[k];
    x[i]=x[i]/A[i][i];
  }
}

struct LinearSystemStruct{
  double *A;
  double **rows;
  double *b;
  double *x;
  int *node_map;
  int band_width;
  int line_width;
  int n_fields;
  int n;
  Mesh *mesh;
  int *isfixed;
};


LinearSystem *linear_system_new(Mesh *mesh, int n_fields, int n_boundaries, const StrongBoundary *boundaries)
{
  LinearSystem *system = malloc(sizeof(LinearSystem));
  system->n_fields = n_fields;
  system->mesh = mesh;
  system->n = mesh->n_nodes*system->n_fields;
  system->node_map = malloc(sizeof(int)*mesh->n_nodes);
  int mbw = reverse_cuthill_mckee(mesh, system->node_map);
  system->band_width = system->n_fields*(1+mbw);
  system->line_width = system->band_width*2+1;
  system->A = malloc(sizeof(double)*system->n*system->line_width);
  system->rows = malloc(sizeof(double*)*system->n);
  for (int i = 0; i < system->n; ++i)
    system->rows[i] = system->A + i*(system->line_width-1) + system->band_width;
  system->b = malloc(sizeof(double)*system->n);
  system->x = malloc(sizeof(double)*system->n);
  system->isfixed = malloc(sizeof(int)*system->n);
  for (int i = 0; i < system->n; ++i) {
    system->isfixed[i] = 0;
  }
  for (int ibnd = 0; ibnd < n_boundaries; ++ibnd) {
    const StrongBoundary *bnd = boundaries + ibnd;
    int iphys;
    for (iphys = 0; iphys < mesh->n_phys; ++iphys) {
      if (strcmp(bnd->tag, mesh->phys_name[iphys]) == 0)
        break;
    }
    if (iphys == mesh->n_phys)
      printf("Boundary tag \"%s\" not found.", bnd->tag);
    for (int i = 0; i < mesh->phys_n_nodes[iphys]; ++i){
      system->isfixed[system->node_map[mesh->phys_nodes[iphys][i]]*system->n_fields+bnd->field] = 1;
    }
  }
  return system;
}

void linear_system_add_to_matrix(LinearSystem *system, int el0, int el1, const double *local_matrix){
  int *tri0 = &system->mesh->triangles[el0*3];
  int *tri1 = &system->mesh->triangles[el1*3];
  int nf = system->n_fields;
  for (int i = 0; i < 3; ++i) {
    for (int inf = 0; inf < 3; ++inf) {
      int ii = system->node_map[tri0[i]]*nf + inf;
      for (int j = 0; j < 3; ++j) {
        for (int jnf = 0; jnf < nf; ++jnf) {
          int jj = system->node_map[tri1[j]]*nf + jnf;
          system->rows[ii][jj] += local_matrix[(inf*3+i)*nf*3+jnf*3+j];
        }
      }
    }
  }
}

void linear_system_add_to_rhs(LinearSystem *system, int el0, const double *local_vector)
{
  const int *tri = system->mesh->triangles + el0*3;
  int n_fields = system->n_fields;
  for (int i = 0; i < n_fields; ++i) {
    for (int j = 0; j < 3; ++j) {
      int m = system->node_map[tri[j]]*n_fields+i;
      system->b[m] += local_vector[i*3+j];
    }
  }
}

void linear_system_zero_matrix_and_rhs(LinearSystem *system)
{
  for (int i = 0; i < system->n; ++i){
    system->b[i] = 0.;
    for (int j = 0; j < system->line_width; ++j) {
      system->A[i*system->line_width+j] = 0.;
    }
  }
}

void linear_system_solve(LinearSystem *system, double *solution){
  int *P = malloc(sizeof(int)*(system->n));
  double **rows = system->rows;
  for (int i = 0; i < system->n; ++i){
    if (system->isfixed[i]) {
      for (int j = i-system->band_width; j <= i+system->band_width; ++j)
        rows[i][j] = 0.;
      rows[i][i] = 1.;
      system->b[i] = 0;
    }
  }
  LUPDecompose(rows, system->n, 1e-8, P, system->band_width);
  LUPSolve(rows, P, system->b, system->n, system->x, system->band_width);
  for (int i = 0; i < system->mesh->n_nodes; ++i){
    int ii = system->node_map[i];
    for (int j = 0; j < system->n_fields; ++j)
      solution[i*system->n_fields+j] = system->x[ii*system->n_fields+j];
  }
}

void linear_system_free(LinearSystem *system)
{
  free(system->b);
  free(system->x);
  free(system->A);
  free(system->rows);
  free(system->isfixed);
  free(system->node_map);
  free(system);
}

double linear_system_get_rhs_norm(LinearSystem *system)
{
  double norm = 0;
  for (int i = 0; i < system->n;++i)
    if (!system->isfixed[i])
      norm += system->b[i]*system->b[i];
  return sqrt(norm);
}

void initialize_linear_solver(int argc, char **argv){}