hxt_linear_system_petsc.c 12.7 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
2
/*
 * Marblesbag - Copyright (C) <2010-2018>
3
4
 * <Universite catholique de Louvain (UCL), Belgium>
 * <Universite de Montpellier, France>
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * 	
 * List of the contributors to the development of SLIM: see AUTHORS file.
 * Description and complete License: see LICENSE file.
 * 	
 * This program (Marblesbag) is free software: 
 * you can redistribute it and/or modify it under the terms of the GNU General 
 * Public License as published by the Free Software Foundation, either version
 * 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program (see COPYING file).  If not, 
 * see <http://www.gnu.org/licenses/>.
 */

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifdef HXT_HAVE_PETSC
#include "hxt_linear_system_petsc.h"
#include "petscksp.h"
#include "petscvec.h"
#include "hxt_message.h"
#define HXT_PETSC_CHECK(status) HXT_CHECK_MSG(status == 0 ? HXT_STATUS_OK : HXT_STATUS_FAILED, "PETSc error")

#if PETSC_VERSION_MAJOR != 3 || PETSC_VERSION_MINOR < 6
#error PETSc version >= 3.6 is required
#endif


struct HXTLinearSystemPETScStruct {
  int nElements;
  int nNodesByElement;
  uint32_t nMappedNodes;
  uint32_t nNodes;
  uint32_t *elements;
  uint32_t nDofs;
  int nFields;
  uint32_t *nodeMap;
  KSP ksp;
  Mat a;
  Vec x;
48
49
  uint32_t nIdentRows, nIdentRowsAlloc;
  PetscInt *identRows;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
  int assemblyNeeded;
};

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

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

static void sparsityChunkInsert(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 = sparsityChunkNew();
  sparsityChunkInsert(s->next, id);
}

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

static HXTStatus assembleIfNeeded(HXTLinearSystemPETSc *system)
{
  if(system->assemblyNeeded) {
    HXT_PETSC_CHECK(MatAssemblyBegin(system->a, MAT_FINAL_ASSEMBLY));
    HXT_PETSC_CHECK(MatAssemblyEnd(system->a, MAT_FINAL_ASSEMBLY));
    system->assemblyNeeded = 0;
  }
93
94
95
96
97
  if (system->nIdentRows > 0){
    HXT_PETSC_CHECK(MatZeroRows(system->a,
          system->nIdentRows, system->identRows,1., NULL, NULL));
  }
  system->nIdentRows = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  return HXT_STATUS_OK;
}

static HXTStatus linearSystemPreAllocateMatrix(HXTLinearSystemPETSc *system)
{
  SparsityChunk **sparsity = malloc(sizeof(SparsityChunk*)*system->nMappedNodes);
  for (int i = 0; i < system->nMappedNodes; ++i)
    sparsity[i] = sparsityChunkNew();
  for (int i = 0; i < system->nElements; ++i) {
    uint32_t *el = system->elements + system->nNodesByElement * i;
    for (int k = 0; k < system->nNodesByElement; ++k) {
      uint32_t nk = system->nodeMap[el[k]];
      if (nk == -1)
        continue;
      for (int l = 0; l < system->nNodesByElement; ++l) {
        uint32_t nl = system->nodeMap[el[l]];
        if (nl == -1)
          continue;
        sparsityChunkInsert(sparsity[nk], nl);
      }
    }
  }
120
  int *nByRow = malloc(sizeof(int) * system->nMappedNodes);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
121
122
123
124
125
126
127
128
129
130
131
  for (uint32_t i = 0; i < system->nMappedNodes; ++i){
    int n = 0;
    SparsityChunk *c = sparsity[i];
    while (c->next) {
      n += CSIZE;
      c = c->next;
    }
    for (int j = 0; j < CSIZE; ++j)
      if(c->n[j] != -1)
        n++;
    sparsityChunkFree(sparsity[i]);
132
      nByRow[i] = n;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
133
134
  }
  free(sparsity);
135
  HXT_PETSC_CHECK(MatSeqBAIJSetPreallocation(system->a, system->nFields,0, nByRow));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
136
137
138
139
140
141
142
143
144
  free(nByRow);
  return HXT_STATUS_OK;
}


HXTStatus hxtLinearSystemPETScAddToMatrix(HXTLinearSystemPETSc *system, int el0, int el1, const double *localMatrix){
  int nn = system->nNodesByElement;
  uint32_t *e0 = system->elements + el0*nn;
  uint32_t *e1 = system->elements + el1*nn;
145
146
  int *map0 = malloc(sizeof(int)*nn);
  int *map1 = malloc(sizeof(int)*nn);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
147
  for(int i=0; i<nn; ++i){
148
149
    map0[i] = system->nodeMap[e0[i]];
    map1[i] = system->nodeMap[e1[i]];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
150
  }
151
  HXT_PETSC_CHECK(MatSetValuesBlocked(system->a,nn,map0,nn,map1,localMatrix,ADD_VALUES));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
  free(map0);
  free(map1);
  system->assemblyNeeded = 1;
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScAddToRhs(HXTLinearSystemPETSc *system, double *rhs, int el0, const double *localVector)
{
  int nf = system->nFields;
  int nn = system->nNodesByElement;
  uint32_t *e0 = system->elements + el0*nn;
  for(int i=0; i<nn; ++i){
    uint32_t n = system->nodeMap[e0[i]];
    for (int j=0; j<nf; ++j){
      rhs[n*nf+j] += localVector[j*nn+i];
    }
  }
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScZeroMatrix(HXTLinearSystemPETSc *system)
{
  HXT_CHECK(assembleIfNeeded(system));
  HXT_PETSC_CHECK(MatZeroEntries(system->a));
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScSetMatrixRowIdentity(HXTLinearSystemPETSc *system, int node, int field)
{
  if (node >= system->nMappedNodes || system->nodeMap[node] == -1) {
    HXT_WARNING("ignoring boundary condition on node %i", node);
    return HXT_STATUS_OK;
  }
  int row = system->nodeMap[node]*system->nFields + field;
186
187
188
  if(system->nIdentRows == system->nIdentRowsAlloc) {
    system->nIdentRowsAlloc *= 2;
    system->identRows = realloc(system->identRows, sizeof(PetscInt)*system->nIdentRowsAlloc);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
189
  }
190
  system->identRows[system->nIdentRows++] = row;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
  return HXT_STATUS_OK;
}


HXTStatus hxtLinearSystemPETScSetMatrixRowFieldCombinaison(HXTLinearSystemPETSc *system, int node, int field, double *coeff)
{
  if (node >= system->nMappedNodes || system->nodeMap[node] == -1) {
    HXT_WARNING("ignoring boundary condition on node %i", node);
    return HXT_STATUS_OK;
  }
  HXT_CHECK(assembleIfNeeded(system));
  int row = system->nodeMap[node]*system->nFields + field;
  HXT_PETSC_CHECK(MatZeroRows(system->a, 1, &row, 0., NULL, NULL));
  row = system->nodeMap[node]*system->nFields;
  for (int i = 0; i < system->nFields; ++i) {
    HXT_PETSC_CHECK(MatSetValue(system->a, row+field, row+i, coeff[i], ADD_VALUES));
  }
  system->assemblyNeeded = 1;
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScSetRhsEntry(HXTLinearSystemPETSc *system, double *rhs, int node, int field, double v)
{
  if (node >= system->nMappedNodes || system->nodeMap[node] == -1) {
    HXT_WARNING("ignoring boundary condition on node %i", node);
    return HXT_STATUS_OK;
  }
  int row = system->nodeMap[node]*system->nFields + field;
  rhs[row] = v;
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScAddMatrixEntry(HXTLinearSystemPETSc *system, int node0, int field0, int node1, int field1, double v)
{
  if (system->nodeMap[node0] == -1 || system->nodeMap[node1] == -1)
    HXT_ERROR_MSG(HXT_STATUS_FAILED, "node %i or %i not in the domain", node0, node1);
  int row0 = system->nodeMap[node0]*system->nFields + field0;
  int col1 = system->nodeMap[node1]*system->nFields + field1;
  
  HXT_PETSC_CHECK(MatSetValue(system->a, row0, col1, v, ADD_VALUES));
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScAddRhsEntry(HXTLinearSystemPETSc *system, double *rhs, int node, int field, double v)
{
  if (system->nodeMap[node] == -1)
    return HXT_STATUS_OK;
  int row = system->nodeMap[node]*system->nFields + field;
  rhs[row] += v;
  return HXT_STATUS_OK;
}


HXTStatus hxtLinearSystemPETScSolve(HXTLinearSystemPETSc *system, double *rhs, double *solution){
  Vec b;
  HXT_PETSC_CHECK(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, system->nDofs, rhs, &b));
247
  assembleIfNeeded(system);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
248
249
250
251
252
253
254
  HXT_PETSC_CHECK(KSPSetOperators(system->ksp, system->a, system->a));
  HXT_PETSC_CHECK(KSPSolve(system->ksp, b, system->x));
  HXT_PETSC_CHECK(VecDestroy(&b));
  HXT_CHECK(hxtLinearSystemPETScMapFromVec(system, system->x, solution));
  return HXT_STATUS_OK;
}

255
256

HXTStatus hxtPETScInsertOptions(const char *options, const char *prefix)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
257
258
{
  #if PETSC_VERSION_MINOR < 7
259
260
261
262
263
  if(prefix != NULL)
    HXT_PETSC_CHECK(PetscOptionsPrefixPush(prefix));
  HXT_PETSC_CHECK(PetscOptionsInsertString(options));
  if(prefix != NULL)
    HXT_PETSC_CHECK(PetscOptionsPrefixPop());
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
264
  #else
265
266
267
268
269
  if(prefix != NULL)
    HXT_PETSC_CHECK(PetscOptionsPrefixPush(NULL, prefix));
  HXT_PETSC_CHECK(PetscOptionsInsertString(NULL, options));
  if(prefix != NULL)
    HXT_PETSC_CHECK(PetscOptionsPrefixPop(NULL));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
270
  #endif
271
272
273
274
}

HXTStatus hxtLinearSystemPETScCreate(HXTLinearSystemPETSc **pSystem, int nElements, int nNodesByElement, int nFields, uint32_t *elements, const char *prefix)
{
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
275
276
  HXTLinearSystemPETSc *system = malloc(sizeof(HXTLinearSystemPETSc));
  *pSystem = system;
277
278
279
  system->nIdentRows = 0;
  system->nIdentRowsAlloc = 256;
  system->identRows = malloc (sizeof(uint32_t)*system->nIdentRowsAlloc);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  system->nElements = nElements;
  system->nNodesByElement = nNodesByElement;
  system->nFields = nFields;
  system->elements = elements;
  uint32_t nNodes = 0;
  for (int i = 0; i < nElements*nNodesByElement; ++i) {
    if (elements[i] >= nNodes)
      nNodes = elements[i]+1;
  }
  system->nNodes = nNodes;
  uint32_t *nodeMap = malloc((nNodes)*sizeof(uint32_t));
  for (uint32_t i = 0; i < nNodes; ++i) {
    nodeMap[i] = -1;
  }
  uint32_t nMappedNodes = 0;
  for (int i = 0; i < nElements*nNodesByElement; ++i) {
    if (nodeMap[elements[i]] == -1)
      nodeMap[elements[i]] = nMappedNodes++;
  }
  system->nodeMap = nodeMap;
  system->nMappedNodes = nMappedNodes;
  uint32_t nDofs = nMappedNodes * nFields;
  system->nDofs = nDofs;
  HXT_PETSC_CHECK(MatCreate(MPI_COMM_WORLD, &system->a));
304
305
  HXT_PETSC_CHECK(MatSetType(system->a,MATBAIJ));

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
306
  HXT_PETSC_CHECK(MatSetSizes(system->a, nDofs, nDofs, PETSC_DETERMINE, PETSC_DETERMINE));
307
308
  if (prefix != NULL)
    HXT_PETSC_CHECK(MatSetOptionsPrefix(system->a, prefix));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
309
310
311
312
313
314
  HXT_PETSC_CHECK(MatSetFromOptions(system->a));
  HXT_PETSC_CHECK(MatSetOption(system->a,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE));
  HXT_PETSC_CHECK(MatSetOption(system->a,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE));
  HXT_CHECK(linearSystemPreAllocateMatrix(system));
  HXT_PETSC_CHECK(VecCreate(MPI_COMM_WORLD, &system->x));
  HXT_PETSC_CHECK(VecSetSizes(system->x, nDofs, PETSC_DETERMINE));
315
316
  if (prefix != NULL)
    HXT_PETSC_CHECK(VecSetOptionsPrefix(system->x, prefix));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
317
318
  HXT_PETSC_CHECK(VecSetFromOptions(system->x));
  HXT_PETSC_CHECK(KSPCreate(MPI_COMM_WORLD, &system->ksp));
319
320
  if (prefix != NULL)
    HXT_PETSC_CHECK(KSPSetOptionsPrefix(system->ksp, prefix));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
321
322
323
324
325
326
327
328
329
330
331
332
333
  HXT_PETSC_CHECK(KSPSetFromOptions(system->ksp));
  system->assemblyNeeded = 0;
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScDelete(HXTLinearSystemPETSc **pSystem)
{
  HXTLinearSystemPETSc *system = *pSystem;
  if (system == NULL)
    return HXT_STATUS_OK;
  HXT_PETSC_CHECK(KSPDestroy(&system->ksp));
  HXT_PETSC_CHECK(VecDestroy(&system->x));
  HXT_PETSC_CHECK(MatDestroy(&system->a));
334
  free(system->identRows);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
  free(system->nodeMap);
  free(system);
  *pSystem = NULL;
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScGetRhsNorm(HXTLinearSystemPETSc *system, double *rhs, double *pNorm)
{
  uint32_t nn = system->nNodes * system->nFields;
  double norm = 0;
  for (uint32_t i = 0; i < nn ; i++)
    norm += rhs[i]*rhs[i];
  *pNorm = sqrt(norm);
  return HXT_STATUS_OK;
}

HXTStatus hxtInitializePETSc(int *argc, char ***argv) {
  HXT_PETSC_CHECK(PetscInitialize(argc, argv, NULL, NULL));
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScHasConverged(HXTLinearSystemPETSc *lsys, int *converged)
{
  KSPConvergedReason reason;
  HXT_PETSC_CHECK(KSPGetConvergedReason(lsys->ksp,&reason));
  *converged = (reason>0);
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScSize(HXTLinearSystemPETSc *lsys, int *size)
{
  *size = lsys->nDofs;
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScGetMat(HXTLinearSystemPETSc *lsys, Mat *mat)
{
  *mat = lsys->a;
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScMapToVec(HXTLinearSystemPETSc *system, double *v, Vec vec)
{
  double *vv;
  HXT_PETSC_CHECK(VecGetArray(vec, &vv));
  int nf = system->nFields;
  for (int i=0; i< system->nNodes; ++i) {
    uint32_t n= system->nodeMap[i];
    for (int j = 0; j < nf; ++j)
      vv[n*nf+j] = v[i*nf+j];
  }
  HXT_PETSC_CHECK(VecRestoreArray(vec, &vv));
  return HXT_STATUS_OK;
}

HXTStatus hxtLinearSystemPETScMapFromVec(HXTLinearSystemPETSc *system, Vec vec, double *v)
{
  const double *vv;
  HXT_PETSC_CHECK(VecGetArrayRead(vec, &vv));
  int nf = system->nFields;
  for (int i=0; i< system->nNodes; ++i) {
    uint32_t n= system->nodeMap[i];
Matthieu Constant's avatar
Matthieu Constant committed
397
    if(n==-1) continue;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
398
399
400
401
402
403
404
405
    for (int j = 0; j < nf; ++j)
      v[i*nf+j] = vv[n*nf+j];
  }
  HXT_PETSC_CHECK(VecRestoreArrayRead(vec, &vv));
  return HXT_STATUS_OK;
}

#endif