linear_system_petsc.c 6.13 KB
Newer Older
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
#include "linear_system.h"
#include "petscksp.h"
#include "petscvec.h"

struct LinearSystemStruct {
  Mesh *mesh;
  int n_fields;
  int *map; //[inode*n_fields+ifield] >=0 -> dof_id, <0 -> fixed_id-1
  int n_dofs;
  double *fixed;
  KSP ksp;
  Mat a;
  Vec b,x;
  int assembly_needed;
};

#define CSIZE 10
typedef struct SparsityChunkStruct {
  int n[CSIZE];
  struct SparsityChunkStruct *next;
}SparsityChunk;

SparsityChunk *sparsity_chunk_new(void)
{
  SparsityChunk *s = malloc(sizeof(SparsityChunk));
  for (int i = 0; i < CSIZE; ++i)
    s->n[i] = -1;
  s->next = NULL;
  return s;
}

void sparsity_chunk_insert(SparsityChunk *s, int id)
{
  for (int i=0; i<CSIZE; ++i) {
    if (s->n[i] == -1) s->n[i] = id;
    if (s->n[i] == id) return;
  }
  if (s->next == NULL)
    s->next = sparsity_chunk_new();
  sparsity_chunk_insert(s->next, id);
}

void sparsity_chunk_free(SparsityChunk *s)
{
  if (s->next)
    sparsity_chunk_free(s->next);
  free(s);
}

static void linear_system_pre_allocate_matrix(LinearSystem *lsys)
{
  const Mesh *mesh = lsys->mesh;
  SparsityChunk **sparsity = malloc(sizeof(SparsityChunk*)*lsys->n_dofs);
  for (int i = 0; i < lsys->n_dofs; ++i)
    sparsity[i] = sparsity_chunk_new();
  for (int i = 0; i < mesh->n_triangles; ++i) {
    for (int k = 0; k < 3; ++k) {
      for (int kf = 0; kf < lsys->n_fields; ++kf) {
        int row = lsys->map[mesh->triangles[i*3+k]*lsys->n_fields+kf];
        if (row < 0)
          continue;
        for (int l = 0; l < 3; ++l){
          for (int lf = 0; lf < lsys->n_fields; ++lf){
            int col = lsys->map[mesh->triangles[i*3+l]*lsys->n_fields+lf];
            if (col >= 0)
              sparsity_chunk_insert(sparsity[row], col);
          }
        }
      }
    }
  }
  int *n_by_row = malloc(sizeof(int) * lsys->n_dofs);
  for (int i = 0; i < lsys->n_dofs; ++i){
    n_by_row[i] = 0;
    SparsityChunk *c = sparsity[i];
    while (c->next) {
      n_by_row[i] += CSIZE;
      c = c->next;
    }
    for (int j = 0; j < CSIZE; ++j)
      if(c->n[j] != -1)
        n_by_row[i]++;
    sparsity_chunk_free(sparsity[i]);
  }
  free(sparsity);
  MatSeqAIJSetPreallocation(lsys->a, 0, n_by_row);
  free(n_by_row);
}

90
static void linear_system_create_map(LinearSystem *lsys, int n_boundaries, const StrongBoundary *bnds)
91
92
93
94
95
96
97
{
  const Mesh *mesh = lsys->mesh;
  int n = mesh->n_nodes*lsys->n_fields;
  lsys->map = malloc(sizeof(int)*n);
  for (int i = 0; i < n; ++i)
    lsys->map[i] = 0;
  int n_fixed = 0;
98
99
  for (int ibnd = 0; ibnd < n_boundaries; ++ibnd) {
    const StrongBoundary *bnd = bnds + ibnd;
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    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)
      lsys->map[mesh->phys_nodes[iphys][i]*lsys->n_fields+bnd->field] = -(++n_fixed);
  }
  lsys->n_dofs = 0;
  for (int i = 0; i < n; ++i)
    if(lsys->map[i]==0)
      lsys->map[i] = lsys->n_dofs++;
  lsys->fixed = malloc(sizeof(double)*n_fixed);
}

117
LinearSystem *linear_system_new(Mesh *mesh, int n_fields, int n_boundaries, const StrongBoundary *boundaries)
118
119
120
121
{
  LinearSystem *lsys = malloc(sizeof(LinearSystem));
  lsys->mesh = mesh;
  lsys->n_fields = n_fields;
122
  linear_system_create_map(lsys, n_boundaries, boundaries);
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
  MatCreate(MPI_COMM_WORLD, &lsys->a);
  MatSetSizes(lsys->a, lsys->n_dofs, lsys->n_dofs, PETSC_DETERMINE, PETSC_DETERMINE);
  MatSetFromOptions(lsys->a);
  linear_system_pre_allocate_matrix(lsys);
  VecCreate(MPI_COMM_WORLD, &lsys->b);
  VecSetSizes(lsys->b, lsys->n_dofs, PETSC_DETERMINE);
  VecSetFromOptions(lsys->b);
  VecSetOption(lsys->b, VEC_IGNORE_NEGATIVE_INDICES,PETSC_TRUE);
  VecDuplicate(lsys->b, &lsys->x);
  KSPCreate(MPI_COMM_WORLD, &lsys->ksp);
  KSPSetFromOptions(lsys->ksp);
  lsys->assembly_needed = 0;
  return lsys;
}

void linear_system_free(LinearSystem *lsys)
{
  KSPDestroy(&lsys->ksp);
  VecDestroy(&lsys->b);
  VecDestroy(&lsys->x);
  MatDestroy(&lsys->a);
  free(lsys->fixed);
  free(lsys->map);
  free(lsys);
}

void linear_system_add_to_matrix(LinearSystem *lsys, int el0, int el1, const double *local_matrix)
{
  int *tri0 = &lsys->mesh->triangles[el0*3];
  int *tri1 = &lsys->mesh->triangles[el1*3];
  int nf = lsys->n_fields;
  int *map0 = malloc(sizeof(int)*3*nf);
  int *map1 = malloc(sizeof(int)*3*nf);
  for(int i=0; i<3; ++i){
    for (int j=0; j<nf; ++j){
      map0[j*3+i] = lsys->map[tri0[i]*nf+j];
      map1[j*3+i] = lsys->map[tri1[i]*nf+j];
    }
  }
  MatSetValues(lsys->a,3*nf,map0,3*nf,map1,local_matrix,ADD_VALUES);
  free(map0);
  free(map1);
  lsys->assembly_needed = 1;
}

void linear_system_add_to_rhs(LinearSystem *lsys, int el0, const double *local_vector)
{
  int *tri0 = &lsys->mesh->triangles[el0*3];
  int nf = lsys->n_fields;
  int *map0 = malloc(sizeof(int)*3*nf);
  for(int i=0; i<3; ++i){
    for (int j=0; j<nf; ++j){
      map0[j*3+i]= lsys->map[tri0[i]*nf+j];
    }
  }
  VecSetValues(lsys->b,3*nf,map0,local_vector,ADD_VALUES);
  free(map0);
}

void linear_system_zero_matrix_and_rhs(LinearSystem *lsys)
{
  if(lsys->assembly_needed) {
    MatAssemblyBegin(lsys->a, MAT_FINAL_ASSEMBLY);
    MatAssemblyEnd(lsys->a, MAT_FINAL_ASSEMBLY);
    lsys->assembly_needed = 0;
  }
  MatZeroEntries(lsys->a);
  VecZeroEntries(lsys->b);
}

void linear_system_solve(LinearSystem *lsys, double *solution)
{
  VecAssemblyBegin(lsys->b);
  VecAssemblyEnd(lsys->b);
  if(lsys->assembly_needed) {
    MatAssemblyBegin(lsys->a, MAT_FINAL_ASSEMBLY);
    MatAssemblyEnd(lsys->a, MAT_FINAL_ASSEMBLY);
    lsys->assembly_needed = 0;
  }
  KSPSetOperators(lsys->ksp, lsys->a, lsys->a);
  KSPSolve(lsys->ksp, lsys->b, lsys->x);
  double *x;
  VecGetArray(lsys->x, &x);
  int n_row = lsys->n_fields*lsys->mesh->n_nodes;
  for (int i=0; i<n_row; ++i) {
    if (lsys->map[i] >= 0)
      solution[i] = x[lsys->map[i]];
    else
      solution[i] = 0;
  }
  VecRestoreArray(lsys->x,&x);
}

double linear_system_get_rhs_norm(LinearSystem *lsys)
{
  double norm;
  VecNorm(lsys->b, NORM_2, &norm);
  return norm;
}

223
224
225
void initialize_linear_solver(int argc, char **argv) {
  MPI_Init(NULL, NULL);
  PetscInitialize(&argc, &argv, NULL, NULL);
226
}