scontact.c 47 KB
Newer Older
Matthieu Constant's avatar
Matthieu Constant committed
1
2
3
4
5
6
7
8
#include "scontact.h" 
#include "quadtree.h"
#include "vector.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <string.h>
9
#include <time.h>
Matthieu Constant's avatar
Matthieu Constant committed
10
11
12
13
14
15
16

typedef struct _Particle Particle;
typedef struct _Contact Contact;
typedef struct _Disk Disk;
typedef struct _Segment Segment;
typedef struct _Triangle Triangle;

17
18
19
20
21
22
23
24
25
26
27
28
29
static int check_word_in_file(FILE *f, const char *w) {
  char word[256];
  if(fscanf(f,"%255s", word) != 1){
    printf("Invalid mesh file (\"%s\" expected).\n", w);
    return -1;
  }
  if(strcmp(word, w) != 0){
    printf("Invalid mesh file (\"%s\" expected).\n", w);
    return -1;
  }
  return 0;
}

Matthieu Constant's avatar
Matthieu Constant committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
struct _ParticleProblem{
  int jacobi;
  double relax;
  Particle *particles;
  Contact *contacts;
  double *position, *velocity, *externalForces;
  Disk *disks;
  char **tagname;
  int *diskTag, *segmentTag;
  Segment *segments;
  #if DIMENSION == 3
  Triangle *triangles;
  int *triangleTag;
  #endif
};

Matthieu Constant's avatar
Matthieu Constant committed
46
47
48
49
50
51
#if DIMENSION == 3
typedef enum {PARTICLE_PARTICLE=0, PARTICLE_DISK, PARTICLE_SEGMENT, PARTICLE_TRIANGLE} ContactType;
#else
typedef enum {PARTICLE_PARTICLE=0, PARTICLE_DISK, PARTICLE_SEGMENT} ContactType;
#endif

Matthieu Constant's avatar
Matthieu Constant committed
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
static double dot(const double *x, const double *y) {
  #if DIMENSION == 3
  return x[0] * y[0] +  x[1] * y[1] + x[2] * y[2];
  #else
  return x[0] * y[0] +  x[1] * y[1];
  #endif
}

void coordRead(FILE *f, double *x) {
  for (int i = 0; i < DIMENSION; ++i)
    fscanf(f, "%le", &x[i]);
}

void coordWrite(FILE *f, double *x) {
  for (int i = 0; i < DIMENSION; ++i)
    fprintf(f, " %.16g", x[i]);
}

/* Particle */
struct _Particle{
  double r;
  double m;
};

struct _Contact {
  size_t o0, o1;
  double a0, a1;
  double D;
  double dv;
Matthieu Constant's avatar
Matthieu Constant committed
81
  ContactType type;
Matthieu Constant's avatar
Matthieu Constant committed
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
  double n[DIMENSION];
};

static void particleBoundingBox(const Particle *p, const double x[DIMENSION], double pmin[DIMENSION], double pmax[DIMENSION]){
  for (int i = 0; i < DIMENSION; ++i) {
    pmin[i] = x[i] - p->r;
    pmax[i] = x[i] + p->r;
  }
}

static int particleInitContact(size_t id0, Particle *p0, double *x0, size_t id1, Particle *p1, double *x1, double alert, Contact *c) {
  double nnorm = 0;
  c->dv = 0;
  c->o0 = id0;
  c->o1 = id1;
  for (int k = 0; k < DIMENSION; ++k) {
    c->n[k] = x0[k] - x1[k];
    nnorm += c->n[k] * c->n[k];
  }
  nnorm = sqrt(nnorm);
  for (int i = 0; i < DIMENSION; ++i)
    c->n[i] /= -nnorm;
  c->D = fmax(0., nnorm - (p0->r + p1->r));
  c->a0 = p1->m / (p0->m + p1->m);
  c->a1 = p0->m / (p0->m + p1->m);
Matthieu Constant's avatar
Matthieu Constant committed
107
  c->type = PARTICLE_PARTICLE;
Matthieu Constant's avatar
Matthieu Constant committed
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
  return c->D < alert;
}

struct _Disk {
  double x[DIMENSION];
  double v[DIMENSION];
  double r;
};

static void diskBoundingBox(const Disk *d, double *pmin, double *pmax) {
  const double r = fabs(d->r);
  for (int i = 0; i < DIMENSION; ++i) {
    pmin[i] = d->x[i] - r;
    pmax[i] = d->x[i] + r;
  }
}

static size_t particleProblemAddBoundaryDiskTagId(ParticleProblem *p, const double x[DIMENSION], double r, size_t tag) {
  Disk *d = vectorPush(&p->disks);
  *vectorPush(&p->diskTag) = tag;
  d->r = r;
  for (int i = 0; i < DIMENSION; ++i) {
    d->x[i] = x[i];
    d->v[i] = 0.;
  }
  return vectorSize(p->disks) - 1;
}

size_t particleProblemAddBoundaryDisk(ParticleProblem *p, const double x[DIMENSION], double r, const char *tag) {
  return particleProblemAddBoundaryDiskTagId(p, x, r, particleProblemGetTagId(p, tag));
}

static int diskInitContact(size_t id, const Disk *d, size_t particle, const Particle *p, double *x, double alert, Contact *c) {
  double nnorm = 0;
  c->dv = 0;
  c->o1 = particle;
  c->o0 = id;
  for (int i = 0; i < DIMENSION; ++i) {
    c->n[i] = d->x[i] - x[i];
    nnorm += c->n[i] * c->n[i];
  }
  nnorm = sqrt(nnorm);
  c->D = (nnorm - fabs(p->r + d->r)) * (d->r < 0 ? -1 : 1);
  if (c->D < 0) c->D = 0;
  for (int i = 0; i < DIMENSION; ++i) {
    c->n[i] /= nnorm * (d->r < 0 ? -1 : 1);
  }
  c->a0 = 0.;
  c->a1 = 1.;
Matthieu Constant's avatar
Matthieu Constant committed
157
  c->type = PARTICLE_DISK;
Matthieu Constant's avatar
Matthieu Constant committed
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
  return c->D < alert;
}

struct _Segment{
  double p[2][DIMENSION];
  double v[DIMENSION];
};

static void segmentBoundingBox(const Segment *s, double *pmin, double *pmax) {
  for (int i = 0; i < DIMENSION; ++i) {
    pmin[i] = fmin(s->p[0][i], s->p[1][i]);
    pmax[i] = fmax(s->p[0][i], s->p[1][i]);
  }
}

static int segmentProjectionIsInside(const Segment *s, double *x) {
  double alpha = 0, beta = 0;
  for (int i = 0; i < DIMENSION; ++i) {
    const double d = s->p[0][i] - s->p[1][i];
    alpha += (x[i] - s->p[0][i]) * d;
    beta  += (x[i] - s->p[1][i]) * d;
  }
  return (alpha < 0 && beta > 0);
}

static int segmentInitContact(size_t id, const Segment *s, size_t particle, const Particle *p, double *x, double alert, Contact *c) {
  c->o0 = id;
  c->o1 = particle;
  c->dv = 0;
  double t[DIMENSION];
  double nt2 = 0;
  double dt = 0;
  for (int i = 0; i <DIMENSION; ++i){
    t[i] = s->p[1][i] - s->p[0][i];
    dt += t[i] * (s->p[0][i] - x[i]);
    nt2 += t[i] * t[i];
  }
  double nn2 = 0;
  for (int i = 0; i < DIMENSION; ++i) {
    c->n[i] = s->p[0][i] - x[i] - t[i] / nt2 *  dt;
    nn2 += c->n[i] * c->n[i];
  }
  const double nnorm = sqrt(nn2);
  for (int i = 0; i < DIMENSION; ++i) {
    c->n[i] /= nnorm;
  }
  c->D = nnorm - p->r;
  if (c->D < 0 && segmentProjectionIsInside(s, x)) c->D = 0;
  c->a0 = 0.;
  c->a1 = 1.;
Matthieu Constant's avatar
Matthieu Constant committed
208
  c->type = PARTICLE_SEGMENT;
Matthieu Constant's avatar
Matthieu Constant committed
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
  return c->D >= 0  && c->D < alert;
}

#if DIMENSION == 3
struct _Triangle {
  double p[3][DIMENSION];
  double v[DIMENSION];
};

static void particleProblemAddBoundaryTriangleTagId(ParticleProblem *p, const double x0[3], const double x1[3], const double x2[3], size_t tag) {
  Triangle *t = vectorPush(&p->triangles);
  *vectorPush(&p->triangleTag) = tag;
  for (int i = 0; i < DIMENSION; ++i) {
    t->p[0][i] = x0[i];
    t->p[1][i] = x1[i];
    t->p[2][i] = x2[i];
    t->v[i] = 0.;
  }
}

void particleProblemAddBoundaryTriangle(ParticleProblem *p, const double x0[3], const double x1[3], const double x2[3], const char *tag) {
  return particleProblemAddBoundaryTriangleTagId(p, x0, x1, x2, particleProblemGetTagId(p, tag));
}

static void triangleBoundingBox(const Triangle *t, double *pmin, double *pmax) {
  for (int i = 0; i < 3; ++i) {
    pmin[i] = fmin(fmin(t->p[0][i], t->p[1][i]), t->p[2][i]);
    pmax[i] = fmax(fmax(t->p[0][i], t->p[1][i]), t->p[2][i]);
  }
}

static void _cross (const double *a, const double *b, double *c) {
  c[0] = a[1] * b[2] - a[2] * b[1];
  c[1] = a[2] * b[0] - a[0] * b[2];
  c[2] = a[0] * b[1] - a[1] * b[0];
}

static int triangleProjectionIsInside(const Triangle *t, const double *x) {
  double d0[3] = {t->p[1][0] - t->p[0][0], t->p[1][1] - t->p[0][1], t->p[1][2] - t->p[0][2]};
  double d1[3] = {t->p[2][0] - t->p[0][0], t->p[2][1] - t->p[0][1], t->p[2][2] - t->p[0][2]};
  double n[3];
  _cross(d0, d1, n);
  double xp[3];
  double dp[3] = {t->p[0][0] - x[0], t->p[0][1] - x[1], t->p[0][2] - x[2]};
  const double nd = dot(n, dp);
  for (int i = 0; i < 3; ++i) 
    xp[i] = x[i] + n[i] * nd;
  double dx[3][3] = 
  {{xp[0] - t->p[0][0], xp[1] - t->p[0][1], xp[2] - t->p[0][2]},
    {xp[0] - t->p[1][0], xp[1] - t->p[1][1], xp[2] - t->p[1][2]},
    {xp[0] - t->p[2][0], xp[1] - t->p[2][1], xp[2] - t->p[2][2]}};
  double alpha[3];
  for (int i = 0; i < 3; ++i) {
    double d[3];
    _cross(dx[(i + 1) % 3], dx[(i + 2) %3], d);
    alpha[i] = 0.5 * dot(n, d);
  }
  return ((alpha[0] <= 0 && alpha[1] <= 0 &&  alpha[2]<= 0) || (alpha[0] >= 0 && alpha[1] >= 0 && alpha[2] >= 0));
}

static int triangleInitContact(size_t id, const Triangle *t, size_t particle, const Particle *p, double *x, double alert, Contact *c) {
  c->o0 = id;
  c->o1 = particle;
  c->dv = 0;
  double d0[3] = {t->p[1][0] - t->p[0][0], t->p[1][1] - t->p[0][1], t->p[1][2] - t->p[0][2]};
  double d1[3] = {t->p[2][0] - t->p[0][0], t->p[2][1] - t->p[0][1], t->p[2][2] - t->p[0][2]};
  double N[3];
  _cross(d0, d1, N);
  const double nn = sqrt(dot(N, N));
  for (int i = 0; i < 3; ++i)
    c->n[i] = N[i] / nn;
  double dd[3] = {t->p[0][0] - x[0], t->p[0][1] - x[1], t->p[0][2] - x[2]};
  c->D = dot(c->n, dd);
  if (c->D < 0) {
    for (int i = 0; i <3; ++i)
      c->n[i] = -c->n[i];
    c->D = -c->D;
  }
  c->D -= p->r;
  c->a0 = 0.;
  c->a1 = 1.;
  if (c->D < 0)
    c->D = 0;
Matthieu Constant's avatar
Matthieu Constant committed
292
  c->type = PARTICLE_TRIANGLE;
Matthieu Constant's avatar
Matthieu Constant committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
  return (c->D > 0 || triangleProjectionIsInside(t, x)) && c->D < alert;
}

#endif

static double contactParticleParticleSolve(Contact *c, const double *v0, const double *v1, double dt) {
  #if DIMENSION == 3
  double vn = (v0[0] - v1[0]) * c->n[0] + (v0[1] - v1[1]) * c->n[1] + (v0[2] - v1[2]) * c->n[2] + c->dv;
  #else 
  double vn = (v0[0] - v1[0]) * c->n[0] + (v0[1] - v1[1]) * c->n[1] + c->dv;
  #endif
  return fmax(0., vn - c->D/dt);
}

static double contactParticleBndSolve(Contact *c, const double *v, double vlocfree[DIMENSION], double *vn, double dt) {
  for (int i = 0; i < DIMENSION; ++i)
    vlocfree[i] = v[i] + c->n[i] * c->dv;
  *vn = dot(vlocfree, c->n);
  return fmax(0, *vn - c->D/dt);
}

Matthieu Constant's avatar
Matthieu Constant committed
314
315
316
317
318
static double contactParticleDiskSolve(Contact *c, const double *v, Disk *d, double dt) {

  double vlocfree[DIMENSION], vn;
  double vloc[DIMENSION];
  for (int i= 0; i < DIMENSION; ++i)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
319
    vloc[i] = v[i] - d->v[i];
Matthieu Constant's avatar
Matthieu Constant committed
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
  return contactParticleBndSolve(c, vloc, vlocfree, &vn, dt);
}

static double contactParticleSegmentSolve(Contact *c, const double *v, const double *x, double r, Segment *s, double dt) {
  double vlocfree[DIMENSION], vn, xn[DIMENSION], vloc[DIMENSION];
  for (int i= 0; i < DIMENSION; ++i)
    vloc[i] = v[i] - s->v[i];
  double dp = contactParticleBndSolve(c, vloc, vlocfree, &vn, dt);
  for (int i = 0; i < DIMENSION; ++i)
    xn[i] = x[i] + r*c->n[i] + vlocfree[i]*c->D / vn;
  if (!segmentProjectionIsInside(s, xn))
    dp = 0;
  return dp;
}

#if DIMENSION == 3
static double contactParticleTriangleSolve(Contact *c, const double *v, const double *x, double r, Triangle *t, double dt) {
  double vlocfree[DIMENSION], vn, xn[DIMENSION], vloc[DIMENSION];
  for (int i= 0; i < DIMENSION; ++i)
    vloc[i] = v[i] - t->v[i];
  double dp = contactParticleBndSolve(c, vloc, vlocfree, &vn, dt);
  for (int i = 0; i < DIMENSION; ++i)
    xn[i] = x[i] + r*c->n[i] + vlocfree[i]*c->D/vn;
  if (!triangleProjectionIsInside(t, xn))
    dp = 0;
  return dp;
}
#endif

Matthieu Constant's avatar
Matthieu Constant committed
349
350
351
352
353
354
355
356
357
static void coordAdd(double *x, double a, const double *y) { // x += a * y
  x[0] += a * y[0];
  x[1] += a * y[1];
  #if DIMENSION == 3
  x[2] += a * y[2];
  #endif
}

Contact *findContactSorted(Contact *c, Contact *v, size_t *i) {
Matthieu Constant's avatar
Matthieu Constant committed
358
  while (*i < vectorSize(v) && (v[*i].type < c->type || v[*i].o0 < c->o0 || (v[*i].o0 == c->o0 && v[*i].o1 < c->o1)))
Matthieu Constant's avatar
Matthieu Constant committed
359
    (*i)++;
Matthieu Constant's avatar
Matthieu Constant committed
360
  return (*i < vectorSize(v) && v[*i].type == c->type && v[*i].o0 == c->o0 && v[*i].o1 == c->o1) ? &v[*i] : NULL;
Matthieu Constant's avatar
Matthieu Constant committed
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
}

static void addAlert(double alert, double *rmin, double *rmax) {
  rmin[0] -= alert;
  rmin[1] -= alert;
  rmax[0] += alert;
  rmax[1] += alert;
  #if DIMENSION == 3
  rmin[2] -= alert;
  rmax[2] += alert;
  #endif
}

static void bbadd(double *amin, double *amax, const double *bmin, const double *bmax)
{
  for (int i = 0; i < DIMENSION; ++i) {
    if (bmin[i] < amin[i])
      amin[i] = bmin[i];
    if (bmax[i] > amax[i])
      amax[i] = bmax[i];
  }
}

static void _particleProblemBoundingBox(ParticleProblem *p, double *bbmin, double *bbmax) {
  double amin[DIMENSION], amax[DIMENSION];
  for (int i = 0; i < DIMENSION; ++i) {
    bbmin[i] = DBL_MAX;
    bbmax[i] = -DBL_MAX;
  }
  for (size_t i = 0; i < vectorSize(p->particles); ++i) {
    particleBoundingBox(&p->particles[i], &p->position[i * DIMENSION], amin, amax);
    bbadd(bbmin, bbmax, amin, amax);
  }
  for (size_t i = 0; i < vectorSize(p->disks); ++i) {
    diskBoundingBox(&p->disks[i], amin, amax);
    bbadd(bbmin, bbmax, amin, amax);
  }
  for (size_t i = 0; i < vectorSize(p->segments); ++i) {
    segmentBoundingBox(&p->segments[i], amin, amax);
    bbadd(bbmin, bbmax, amin, amax);
  }
  #if DIMENSION == 3
  for (size_t i = 0; i < vectorSize(p->triangles); ++i) {
    triangleBoundingBox(&p->triangles[i], amin, amax);
    bbadd(bbmin, bbmax, amin, amax);
  }
  #endif
}

static void _bboxtol(double *bbmin, double *bbmax) {
  double lmax = 0;
  for (int i = 0; i < DIMENSION; ++i) {
    lmax = fmax(lmax, bbmax[i] - bbmin[i]);
  }
  double tol = 1e-8 * lmax;
  for (int i = 0; i < DIMENSION; ++i) {
    bbmax[i] += tol;
    bbmin[i] -= tol;
  }
}


double volTet(double x0[3], double x1[3], double x2[3], double x3[3]) {
  // x1 * (y2 * (z3 - z4) - z2 * (y3 - y4) + y3 * z4 - y4 * z3)
  //- y1 * (x2 * (z3 - z4) - z2 * (x3 - x4) + x3 * z4 - x4 * z3)
  //+ z1 * (x2 * (y3 - y4) - y2 * (x3 - x4) + x3 * y4 - x4 * y3)
  //- (x2 * (y3 * z4 - y4 * z3) - y2 * (x3 * z4 - x4 * z3) + z2 * (x3 * y4 - x4 * y3))
  return  x0[0] * (x1[1] * (x2[2] - x3[2]) - x1[2] * (x2[1] - x3[1]) + x2[1] * x3[2] - x3[1] * x2[2])
    - x0[1] * (x1[0] * (x2[2] - x3[2]) - x1[2] * (x2[0] - x3[0]) + x2[0] * x3[2] - x3[0] * x2[2])
    + x0[2] * (x1[0] * (x2[1] - x3[1]) - x1[1] * (x2[0] - x3[0]) + x2[0] * x3[1] - x3[0] * x2[1])
    - (x1[0] * (x2[1] * x3[2] - x3[1] * x2[2]) - x1[1] * (x2[0] * x3[2] - x3[0] * x2[2]) + x1[2] * (x2[0] * x3[1] - x3[0] * x2[1]));
}

void ParticleToMesh(size_t nParticles, double *particles, int nElements, double *elements, int *elid, double *Xi)
{
  double bbmin[DIMENSION], bbmax[DIMENSION];
  int N = DIMENSION + 1;
  for (int i = 0; i < DIMENSION; ++i) {
    bbmin[i] = elements[i];
    bbmax[i] = elements[i];
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
442
  for (int i = 0; i < N * nElements; ++i) {
Matthieu Constant's avatar
Matthieu Constant committed
443
444
445
446
447
448
449
450
    for (int d = 0; d < DIMENSION; ++d) {
      bbmin[d] = fmin(bbmin[d], elements[DIMENSION * i + d]);
      bbmax[d] = fmax(bbmax[d], elements[DIMENSION * i + d]);
    }
  }
  _bboxtol(bbmin, bbmax);
  Cell *tree = cellNew(bbmin, bbmax);
  double amin[DIMENSION], amax[DIMENSION];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
451
  for (int i = 0; i < nElements; ++i) {
Matthieu Constant's avatar
Matthieu Constant committed
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
    double *el = elements + (DIMENSION * N) * i;
    for (int d = 0; d < DIMENSION; ++d) {
      amin[d] = el[d];
      amax[d] = el[d];
    }
    for (int v = 1; v < N; ++v) {//only simplices
      for (int d = 0; d < DIMENSION; ++d) {
        amin[d] = fmin(amin[d], el[v * DIMENSION + d]);
        amax[d] = fmax(amax[d], el[v * DIMENSION + d]);
      }
    }
    _bboxtol(amin, amax);
    cellAdd(tree, amin, amax, i, NULL);
  }
  int *found = NULL;
  vectorPushN(&found, 100);
  vectorClear(found);
  for (size_t i = 0; i < nParticles; ++i) {
    double *x = &particles[i * DIMENSION];
    elid[i] = -1;
    cellSearch(tree, x, x, &found);
    for (size_t j = 0; j < vectorSize(found); ++j) {
      double *el = &elements[found[j] * N * DIMENSION];
      if (DIMENSION == 2)  {
        double *X[3] = {el, el + DIMENSION, el + DIMENSION * 2};
        double dx = x[0] - X[0][0], dy = x[1] - X[0][1];
        double DX[2] = {X[1][0] - X[0][0], X[2][0] - X[0][0]};
        double DY[2] = {X[1][1] - X[0][1], X[2][1] - X[0][1]};
        double det = DX[1] * DY[0] - DX[0] * DY[1];
        double xi  = (DX[1] * dy - DY[1] * dx) / det;
        double eta = -(DX[0] * dy - DY[0] * dx) / det;
        if (xi > -1e-8 && eta > -1e-8 && 1 - xi - eta > -1e-8) {
          Xi[i * DIMENSION + 0] = xi;
          Xi[i * DIMENSION + 1] = eta;
          elid[i] = found[j];
          break;
        }
      }
      else {
        double *X[4] = {el, el + DIMENSION, el + DIMENSION * 2, el + DIMENSION * 3};
        double v = volTet(X[0], X[1], X[2], X[3]);
        double v0 = volTet(x, X[1], X[2], X[3]);
        if (v0 * v < -1e-8)
          continue;
        double v1 = volTet(X[0], x, X[2], X[3]);
        if (v1 * v < -1e-8)
          continue;
        double v2 = volTet(X[0], X[1], x, X[3]);
        if (v2 * v < -1e-8)
          continue;
        double v3 = volTet(X[0], X[1], X[2], x);
        if (v3 * v < -1e-8)
          continue;
        Xi[i * 3 + 0] = v1 / v;
        Xi[i * 3 + 1] = v2 / v;
        Xi[i * 3 + 2] = v3 / v;
        elid[i] = found[j];
        break;
      }
    }
    /*if (elid[i] == -1) {
      printf(" PARTICLE %i OUTSIDE DOMAIN N2 search!!!\n", i);
      double toll = -1000;
      for (size_t j = 0; j < nElements; ++j) {
        double *el = &elements[j * N * DIMENSION];
        double *X[3] = {el, el + DIMENSION, el + DIMENSION * 2};
        double dx = x[0] - X[0][0], dy = x[1] - X[0][1];
        double DX[2] = {X[1][0] - X[0][0], X[2][0] - X[0][0]};
        double DY[2] = {X[1][1] - X[0][1], X[2][1] - X[0][1]};
        double det = DX[1] * DY[0] - DX[0] * DY[1];
        double xi  = (DX[1] * dy - DY[1] * dx) / det;
        double eta = -(DX[0] * dy - DY[0] * dx) / det;
        toll = fmax(toll, fmin(-xi, fmin(-eta, xi + eta -1)));
        if (xi > -1e-8 && eta > -1e-8 && 1 - xi - eta > -1e-8) {
          Xi[i * DIMENSION + 0] = xi;
          Xi[i * DIMENSION + 1] = eta;
          elid[i] = j;
          break;
        }
      }*/
      if (elid[i] == -1) {
        //printf(" PARTICLE %lu OUTSIDE DOMAIN!!! %g %g\n", i, x[0], x[1]);
        //exit(1);
      }
    //}
  }
  vectorFree(found);
  cellFree(tree);
}

542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
int listcmp(const void *i0, const void *i1) {
  const u_int32_t j0 = *(u_int32_t*)i0, j1 = *(u_int32_t*)i1;
  return j0 - j1;
}

static int intersect(const double *amin, const double *amax, const double *bmin, const double *bmax) {
  if (amin[0] > bmax[0] || amax[0] < bmin[0]) return 0;
  if (amin[1] > bmax[1] || amax[1] < bmin[1]) return 0;
  #if DIMENSION == 3
  if (amin[2] > bmax[2] || amax[2] < bmin[2]) return 0;
  #endif
  return 1;
}

static void _particleProblemGenContacts2(ParticleProblem *p, const double alert)
{
  clock_t tic = clock();
  u_int32_t *list = NULL;
  double bbmin[DIMENSION], bbmax[DIMENSION];
  _particleProblemBoundingBox(p,bbmin,bbmax);
  addAlert(alert,bbmin, bbmax);
  u_int16_t N[DIMENSION];
  double delta = alert*6;
  for (int i = 0; i < DIMENSION; ++i) {
    N[i] = (bbmax[i]-bbmin[i])/delta;
  }

  for (size_t i = 0; i < vectorSize(p->particles); ++i) {
    double amin[DIMENSION], amax[DIMENSION];
    particleBoundingBox(&p->particles[i], &p->position[i*DIMENSION],amin,amax);
    addAlert(alert/2,amin,amax);
    u_int16_t from[DIMENSION],to[DIMENSION];
    for (int i = 0; i < DIMENSION; ++i){
      from[i] = (amin[i]-bbmin[i])/delta;
      to[i] = (amax[i]-bbmin[i])/delta;
    }
#if DIMENSION == 2
    for (int j = from[0]; j <= to[0]; ++j) {
      for (int k = from[1]; k <= to[1]; ++k) {
        u_int32_t bid = j*N[1]+k;
        *vectorPush(&list) = bid;
        *vectorPush(&list) = i;
      }
    }
#else
    for (int j = from[0]; j <= to[0]; ++j) {
      for (int k = from[1]; k <= to[1]; ++k) {
        for (int l = from[2]; l <= to[2]; ++l) {
          u_int32_t bid = (j*N[1]+k)*N[2]+l;
          *vectorPush(&list) = bid;
          *vectorPush(&list) = i;
        }
      }
    }
#endif
  }
  clock_t tic1 = clock();
  qsort(list, vectorSize(list)/2, 2*sizeof(u_int32_t), listcmp);
  clock_t tic2 = clock();
  int ntot = 0;
  Contact *cc=NULL;
  vectorPushN(&cc, 10000000);
  vectorClear(cc);
  double amin[DIMENSION], amax[DIMENSION];
  double bmin[DIMENSION], bmax[DIMENSION];
  for (int start = 0; start < vectorSize(list);) {
    int end = start;
    for (; end< vectorSize(list);end+=2) {
      if (list[end] != list[start])
        break;
    }
    for (int i = start; i < end; i+= 2) {
      int ii = list[i+1];
      Particle *p0 = &p->particles[ii];
      double *x_i = &p->position[ii*DIMENSION];
      for (int j = i; j < end; j+= 2) {
        int jj = list[j+1];
        Particle *p1 = &p->particles[jj];
        double *x_j = &p->position[jj*DIMENSION];
        Contact c;
        c.dv = 0;
        c.o0 = ii; c.o1 = jj;
        double nnorm = 0;
        for (int k = 0; k < DIMENSION; ++k) {
          c.n[k] = x_i[k] - x_j[k];
          nnorm += c.n[k]*c.n[k];
        }
        double dd =p0->r+p1->r+alert;
        if (nnorm > dd*dd)
          continue;
        nnorm = sqrt(nnorm);
        for (int i = 0; i < DIMENSION; ++i)
          c.n[i] /= -nnorm;
        c.D = fmax(0., nnorm - (p0->r + p1->r));
        c.a0 = p1->m / (p0->m + p1->m);
        c.a1 = p0->m / (p0->m + p1->m);
        c.type = PARTICLE_PARTICLE;
        ntot++;
        *vectorPush(&cc) = c;
        /*else if ((cold = findContactSorted(c, oldContacts, &iold))) {
          coordAdd(&p->velocity[c->o0 * DIMENSION], -cold->dv * c->a0, c->n);
          coordAdd(&p->velocity[c->o1 * DIMENSION], cold->dv * c->a1, c->n);
          c->dv = cold->dv;
        }*/
      }
    }
    start = end;
  }
  clock_t tic3 = clock();
  printf("time new %i : %i %i %i\n", tic3-tic, tic3-tic2,tic2-tic1, tic1-tic);
  printf("%i / %i (%i%%)\n", vectorSize(cc), ntot, vectorSize(cc)*100/ntot);
  vectorFree(cc);
}

Matthieu Constant's avatar
Matthieu Constant committed
656
657
static void _particleProblemGenContacts(ParticleProblem *p, const double alert)
{
658
659
  //_particleProblemGenContacts2(p,alert);
  clock_t tic = clock();
Matthieu Constant's avatar
Matthieu Constant committed
660
661
662
663
664
665
666
667
668
669
670
  size_t iold = 0;
  double bbmin[DIMENSION], bbmax[DIMENSION];
  _particleProblemBoundingBox(p, bbmin, bbmax);
  Cell *tree = cellNew(bbmin, bbmax);
  double amin[DIMENSION], amax[DIMENSION];
  int *found = NULL;
  vectorPushN(&found, 100);
  vectorClear(found);
  // Particles
  Contact *oldContacts = vectorDup(p->contacts), *cold;
  vectorClear(p->contacts);
671
  int ntot = 0;
Matthieu Constant's avatar
Matthieu Constant committed
672
673
674
675
676
  for (size_t i = 0; i < vectorSize(p->particles); ++i) {
    particleBoundingBox(&p->particles[i], &p->position[i * DIMENSION], amin, amax);
    addAlert(alert/2, amin, amax);
    vectorClear(found);
    cellAdd(tree, amin, amax, i, &found);
Matthieu Constant's avatar
Matthieu Constant committed
677
    for (size_t j = 0; j < vectorSize(found); ++j) {
678
      ntot++;
Matthieu Constant's avatar
Matthieu Constant committed
679
680
681
682
683
684
685
686
687
688
      Contact *c= vectorPush(&p->contacts);
      if(!particleInitContact(i, &p->particles[i], &p->position[i * DIMENSION], found[j], &p->particles[found[j]], &p->position[found[j] * DIMENSION], alert, c))
        vectorPop(p->contacts);
      else if ((cold = findContactSorted(c, oldContacts, &iold))) {
        coordAdd(&p->velocity[c->o0 * DIMENSION], -cold->dv * c->a0, c->n);
        coordAdd(&p->velocity[c->o1 * DIMENSION], cold->dv * c->a1, c->n);
        c->dv = cold->dv;
      }
    }
  }
689
  //exit(0);
Matthieu Constant's avatar
Matthieu Constant committed
690
691
692
693
694
695
  // Disks
  for (size_t i = 0; i < vectorSize(p->disks); ++i) {
    diskBoundingBox(&p->disks[i], amin, amax);
    addAlert(alert/2, amin, amax);
    vectorClear(found);
    cellSearch(tree, amin, amax, &found);
Matthieu Constant's avatar
Matthieu Constant committed
696
697
    for (size_t j = 0; j < vectorSize(found); ++j) {
      Contact *c = vectorPush(&p->contacts);
Matthieu Constant's avatar
Matthieu Constant committed
698
      if(!diskInitContact(i, &p->disks[i], found[j], &p->particles[found[j]], &p->position[found[j] * DIMENSION], alert, c))
Matthieu Constant's avatar
Matthieu Constant committed
699
        vectorPop(p->contacts);
Matthieu Constant's avatar
Matthieu Constant committed
700
701
702
703
704
705
706
707
708
709
710
711
      else if ((cold = findContactSorted(c, oldContacts, &iold))) {
        coordAdd(&p->velocity[c->o1 * DIMENSION], -cold->dv, c->n);
       c->dv = cold->dv;
      }
    }
  }
  // Segments
  for (size_t i = 0; i < vectorSize(p->segments); ++i) {
    segmentBoundingBox(&p->segments[i], amin, amax);
    addAlert(alert/2, amin, amax);
    vectorClear(found);
    cellSearch(tree, amin, amax, &found);
Matthieu Constant's avatar
Matthieu Constant committed
712
713
    for (size_t j = 0; j < vectorSize(found); ++j) {
      Contact *c = vectorPush(&p->contacts);
Matthieu Constant's avatar
Matthieu Constant committed
714
      if (!segmentInitContact(i, &p->segments[i], found[j], &p->particles[found[j]], &p->position[found[j] * DIMENSION], alert, c))
Matthieu Constant's avatar
Matthieu Constant committed
715
        vectorPop(p->contacts);
Matthieu Constant's avatar
Matthieu Constant committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
      else if ((cold = findContactSorted(c, oldContacts, &iold))) {
        coordAdd(&p->velocity[c->o1 * DIMENSION], -cold->dv, c->n);
        c->dv = cold->dv;
      }
    }
  }
  // Triangles 
  #if DIMENSION == 3
  for (size_t i = 0; i < vectorSize(p->triangles); ++i) {
    triangleBoundingBox(&p->triangles[i], amin, amax);
    addAlert(alert/2, amin, amax);
    vectorClear(found);
    cellSearch(tree, amin, amax, &found);
    for (int j = 0; j < vectorSize(found); ++j) {
Matthieu Constant's avatar
Matthieu Constant committed
730
      Contact *c =  vectorPush(&p->contacts);
Matthieu Constant's avatar
Matthieu Constant committed
731
      if (!triangleInitContact(i, &p->triangles[i], found[j], &p->particles[found[j]], &p->position[found[j] * DIMENSION], alert, c))
Matthieu Constant's avatar
Matthieu Constant committed
732
        vectorPop(p->contacts);
Matthieu Constant's avatar
Matthieu Constant committed
733
734
735
736
737
738
739
      else if ((cold = findContactSorted(c, oldContacts, &iold))) {
        coordAdd(&p->velocity[c->o1 * DIMENSION], -cold->dv, c->n);
        c->dv = cold->dv;
      }
    }
  }
  #endif
Matthieu Constant's avatar
Matthieu Constant committed
740
  vectorFree(oldContacts);
Matthieu Constant's avatar
Matthieu Constant committed
741
742
743
744
  vectorFree(found);
  cellFree(tree);
}

Matthieu Constant's avatar
Matthieu Constant committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
typedef struct {
  size_t *data;
  size_t *b, *e;
  size_t size;
}Fifo;

static Fifo *fifoNew(size_t maxSize) {
  Fifo *f = malloc(sizeof(Fifo));
  f->b = f->e = f->data = malloc(sizeof(size_t)*(maxSize+1));
  f->size = maxSize+1;
  return f;
}

static void fifoPush(Fifo *f, size_t v) {
  *(f->e++) = v;
  if (f->e == f->data + f->size) f->e = f->data;
}

#define FIFO_EMPTY ((size_t)-1)

static size_t fifoPop(Fifo *f) {
  if (f->b == f->e) return FIFO_EMPTY;
  size_t v = *(f->b++);
  if (f->b == f->data + f->size) f->b = f->data;
  return v;
}

static void fifoFree(Fifo *f) {
  free(f->data);
  free(f);
}

static void _particleProblemSolveContactsQueue(ParticleProblem *p, double dt, double tol) {
  size_t *nContactByParticle = malloc(sizeof(size_t)*(vectorSize(p->particles)));
  for (size_t i = 0; i < vectorSize(p->particles); ++i) {
    nContactByParticle[i] = 0;
  }
  for (size_t i = 0; i < vectorSize(p->contacts); ++i) {
    Contact *c = &p->contacts[i];
    if(c->type == PARTICLE_PARTICLE)
      nContactByParticle[c->o0]++;
    nContactByParticle[c->o1]++;
  }
  size_t *contactByParticleP = malloc(sizeof(size_t)*(vectorSize(p->particles)+1));
  contactByParticleP[0] = 0;
  for (size_t i = 0; i < vectorSize(p->particles); ++i){
    contactByParticleP[i+1] = contactByParticleP[i] + nContactByParticle[i];
    nContactByParticle[i] = 0;
  }
  size_t *contactByParticle = malloc(contactByParticleP[vectorSize(p->particles)]*sizeof(size_t));
  for (size_t i = 0; i < vectorSize(p->contacts); ++i) {
    Contact *c = &p->contacts[i];
    if (c->type == PARTICLE_PARTICLE)
      contactByParticle[contactByParticleP[c->o0]+(nContactByParticle[c->o0]++)] = i;
    contactByParticle[contactByParticleP[c->o1]+(nContactByParticle[c->o1]++)] = i;
  }
  free(nContactByParticle);

  Fifo *queue = fifoNew(vectorSize(p->contacts));
  int *activeContact = malloc(sizeof(int)*vectorSize(p->contacts));
  for (size_t i = 0; i < vectorSize(p->contacts); ++i) {
    fifoPush(queue, i);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
807
    activeContact[i] = 1;
Matthieu Constant's avatar
Matthieu Constant committed
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
  }
  for (size_t ic = fifoPop(queue); ic != FIFO_EMPTY; ic=fifoPop(queue)){
    Contact *c = &p->contacts[ic];
    double dp;
    switch (c->type) {
      case PARTICLE_PARTICLE :
        dp = contactParticleParticleSolve(c, &p->velocity[c->o0 * DIMENSION], &p->velocity[c->o1 * DIMENSION], dt);
        break;
      case PARTICLE_DISK :
        dp = contactParticleDiskSolve(c, &p->velocity[c->o1*DIMENSION], &p->disks[c->o0], dt);
        break;
      case PARTICLE_SEGMENT :
        dp = contactParticleSegmentSolve(c, &p->velocity[c->o1*DIMENSION], &p->position[c->o1*DIMENSION], p->particles[c->o1].r,&p->segments[c->o0], dt);
        break;
#if DIMENSION == 3
      case PARTICLE_TRIANGLE :
        dp = contactParticleTriangleSolve(c, &p->velocity[c->o1*DIMENSION], &p->position[c->o1*DIMENSION], p->particles[c->o1].r,&p->triangles[c->o0], dt);
        break;
#endif
Matthieu Constant's avatar
Matthieu Constant committed
827
    }
Matthieu Constant's avatar
Matthieu Constant committed
828
829
830
831
    dp -= c->dv;
    if (c->type == PARTICLE_PARTICLE) {
      coordAdd(&p->velocity[c->o0 * DIMENSION], -dp*c->a0, c->n);
      coordAdd(&p->velocity[c->o1 * DIMENSION], dp*c->a1, c->n);
Matthieu Constant's avatar
Matthieu Constant committed
832
    }
Matthieu Constant's avatar
Matthieu Constant committed
833
834
    else {
      coordAdd(&p->velocity[c->o1 * DIMENSION], -dp, c->n);
Matthieu Constant's avatar
Matthieu Constant committed
835
    }
Matthieu Constant's avatar
Matthieu Constant committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
    if (fabs(dp) > tol/dt) {
      if(c->type == PARTICLE_PARTICLE) {
        for (size_t j = contactByParticleP[c->o0]; j < contactByParticleP[c->o0+1]; ++j) {
          if (!activeContact[contactByParticle[j]]){
            activeContact[contactByParticle[j]] = 1;
            fifoPush(queue, contactByParticle[j]);
          }
        }
      }
      for (size_t j = contactByParticleP[c->o1]; j < contactByParticleP[c->o1+1]; ++j) {
        if (!activeContact[contactByParticle[j]]){
          activeContact[contactByParticle[j]] = 1;
          fifoPush(queue, contactByParticle[j]);
        }
      }
Matthieu Constant's avatar
Matthieu Constant committed
851
    }
Matthieu Constant's avatar
Matthieu Constant committed
852
853
    c->dv += dp;
    activeContact[ic] = 0;
Matthieu Constant's avatar
Matthieu Constant committed
854
  }
Matthieu Constant's avatar
Matthieu Constant committed
855
856
857
858
859

  fifoFree(queue);
  free(activeContact);
  free(contactByParticleP);
  free(contactByParticle);
Matthieu Constant's avatar
Matthieu Constant committed
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
}

double particleProblemMaxDt(const ParticleProblem *p, double alert) {
  double VM2 = 0;
  double FM2 = 0;
  for (size_t j = 0; j < vectorSize(p->particles); ++j) {
    VM2 = fmax(VM2, dot(&p->velocity[j * DIMENSION], &p->velocity[j * DIMENSION]));
    FM2 = fmax(FM2, dot(&p->externalForces[j * DIMENSION], &p->externalForces[j * DIMENSION]) / (p->particles[j].m * p->particles[j].m));
  }
  const double VM = sqrt(VM2);
  const double FM = sqrt(FM2);
  const double q = VM + sqrt(VM2 + 4 * FM * alert);
  return alert / q;
}

void particleProblemSolve(ParticleProblem *p, double alert, double dt, double tol, int maxit)
{
  _particleProblemGenContacts(p, alert);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
878
  _particleProblemSolveContactsQueue(p,dt,tol);
Matthieu Constant's avatar
Matthieu Constant committed
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
}

void particleProblemIterate(ParticleProblem *p, double alert, double dt, double tol, int maxit)
{
  for (size_t j = 0; j < vectorSize(p->particles); ++j)
    for (size_t i = 0; i < DIMENSION; ++i)
      p->velocity[j * DIMENSION + i] += p->externalForces[j * DIMENSION + i] * dt / p->particles[j].m;
  particleProblemSolve(p, alert, dt, tol, maxit);
  for (size_t i = 0; i < vectorSize(p->position); ++i)
    p->position[i] += p->velocity[i] * dt;
  for (size_t i = 0; i < vectorSize(p->disks); ++i)
    for (int j = 0; j < DIMENSION; ++j)
      p->disks[i].x[j] += p->disks[i].v[j] * dt;
  for (size_t i = 0; i < vectorSize(p->segments); ++i)
    for (int j = 0; j < DIMENSION; ++j) {
      p->segments[i].p[0][j] += p->segments[i].v[j] * dt;
      p->segments[i].p[1][j] += p->segments[i].v[j] * dt;
    }
  #if DIMENSION == 3
  for (size_t i = 0; i < vectorSize(p->triangles); ++i)
    for (int j = 0; j < DIMENSION; ++j) {
      p->triangles[i].p[0][j] += p->triangles[i].v[j] * dt;
      p->triangles[i].p[1][j] += p->triangles[i].v[j] * dt;
      p->triangles[i].p[2][j] += p->triangles[i].v[j] * dt;
    }
  #endif
}

static size_t particleProblemAddBoundarySegmentTagId(ParticleProblem *p, const double x0[DIMENSION], const double x1[DIMENSION], size_t tag) {
  Segment *s = vectorPush(&p->segments);
  *vectorPush(&p->segmentTag) = tag;
  for (int i = 0; i < DIMENSION; ++i) {
    s->p[0][i] = x0[i];
    s->p[1][i] = x1[i];
    s->v[i] = 0.;
  }
  return vectorSize(p->segments) - 1;
}

size_t particleProblemAddBoundarySegment(ParticleProblem *p, const double x0[DIMENSION], const double x1[DIMENSION], const char *tag) {
  return particleProblemAddBoundarySegmentTagId(p, x0, x1, particleProblemGetTagId(p, tag));
}

void particleProblemAddParticle(ParticleProblem *p, const double x[DIMENSION], double r, double m) {
  size_t n = vectorSize(p->particles);
  Particle *particle = vectorPush(&p->particles);
  particle->r = r;
  particle->m = m;
  vectorPushN(&p->position, DIMENSION);
  vectorPushN(&p->velocity, DIMENSION);
  vectorPushN(&p->externalForces, DIMENSION);
  for (int i = 0; i < DIMENSION; ++i) {
    p->position[n * DIMENSION +  i] = x[i];
    p->velocity[n * DIMENSION +  i] = 0;
    p->externalForces[n * DIMENSION + i] = 0;
  }
}

937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
static void _writeContactVtkSingle(ParticleProblem *p, FILE *f, int ctype, const char *cname)
{
  size_t n = 0;
  for (size_t i = 0; i < vectorSize(p->contacts); ++i) {
    if (p->contacts[i].type == ctype)
      n++;
  }
  fprintf(f, "<DataArray Name=\"%s\" NumberOfTuples=\"%zu\" NumberOfComponents=\"1\" type=\"Float32\" format=\"ascii\">\n",cname, n);
  for (size_t i = 0; i < vectorSize(p->contacts); ++i) {
    if (p->contacts[i].type == ctype) {
      fprintf(f, "%.2g ", p->contacts[i].dv);
    }
  }
  fprintf(f, "\n</DataArray>\n");
  fprintf(f, "<DataArray Name=\"%s_idx\" NumberOfTuples=\"%zu\" NumberOfComponents=\"2\" type=\"UInt64\" format=\"ascii\">\n",cname,n);
  for (size_t i = 0; i < vectorSize(p->contacts); ++i) {
    if (p->contacts[i].type == ctype) {
      fprintf(f, "%zu %zu ", p->contacts[i].o0, p->contacts[i].o1);
    }
  }
  fprintf(f, "\n</DataArray>\n");
}

static int particleProblemWriteContactVtp(ParticleProblem *p, const char *dirname, int id) {
  char *filename = malloc((strlen(dirname)+50)*sizeof(char));
  sprintf(filename,"%s/contacts_%05d.vtp",dirname, id);
  FILE *f = fopen(filename, "w");
  if (!f){
    printf("cannot open file %s\n", filename);
    return -1;
  }
968
969
970
971
/*  size_t n_contacts[DIMENSION+2] = {0};*/
/*  for (size_t i = 0; i < vectorSize(p->contacts); ++i) {*/
/*    n_contacts[p->contacts[i].type]++;*/
/*  }*/
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
  fprintf(f, "<VTKFile type=\"PolyData\">\n");
  fprintf(f, "<PolyData>\n");
  fprintf(f, "<FieldData>\n");
  _writeContactVtkSingle(p,f,PARTICLE_PARTICLE,"particle_particle");
  _writeContactVtkSingle(p,f,PARTICLE_DISK,"particle_disk");
  _writeContactVtkSingle(p,f,PARTICLE_SEGMENT,"particle_segment");
#if DIMENSION == 3
  _writeContactVtkSingle(p,f,PARTICLE_TRIANGLE,"particle_triangle");
#endif
  fprintf(f, "</FieldData>\n");
  fprintf(f, "<Piece NumberOfPoints=\"1\" NumberOfCells=\"0\">\n");
  fprintf(f, "<Points>\n<DataArray NumberOfComponents=\"3\" type=\"Float32\" format=\"ascii\">\n");
  fprintf(f, "0 0 0\n");
  fprintf(f, "</DataArray>\n");
  fprintf(f, "</Points>\n");
  fprintf(f, "</Piece>\n");
  fprintf(f, "</PolyData>\n");
  fprintf(f, "</VTKFile>\n");
  fclose(f);
  free(filename);
  return 0;
}

static int particleProblemWriteParticleVtp(ParticleProblem *p, const char *dirname, int id) {
  char *filename = malloc((strlen(dirname)+50)*sizeof(char));
  sprintf(filename,"%s/particles_%05d.vtp",dirname, id);
Matthieu Constant's avatar
Matthieu Constant committed
998
  FILE *f = fopen(filename, "w");
999
1000
1001
1002
  if (!f){
    printf("cannot open file %s\n", filename);
    return -1;
  }
1003
1004
1005
1006
1007
  size_t n_pp_contacts = 0;
  for (size_t i = 0; i < vectorSize(p->contacts); ++i) {
    if (p->contacts[i].type == PARTICLE_PARTICLE)
      n_pp_contacts ++;
  }
1008
1009
1010
  fprintf(f, "<VTKFile type=\"PolyData\">\n");
  fprintf(f, "<PolyData>\n");
  fprintf(f, "<Piece NumberOfPoints=\"%lu\" NumberOfCells=\"0\">\n", vectorSize(p->particles));
Matthieu Constant's avatar
Matthieu Constant committed
1011
1012
  fprintf(f, "<PointData Scalars=\"Radius\" Vectors=\"Velocity\">\n");
  fprintf(f, "<DataArray Name=\"Radius\" NumberOfComponents = \"1\" type=\"Float32\" format=\"ascii\">\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1013
  for (size_t i = 0; i < vectorSize(p->particles); ++i) {
Matthieu Constant's avatar
Matthieu Constant committed
1014
1015
1016
    fprintf(f, "%.16g ", p->particles[i].r);
  }
  fprintf(f, "\n</DataArray>\n");
1017
1018
1019
1020
1021
  fprintf(f, "<DataArray Name=\"Mass\" NumberOfComponents = \"1\" type=\"Float32\" format=\"ascii\">\n");
  for (size_t i = 0; i < vectorSize(p->particles); ++i) {
    fprintf(f, "%.16g ", p->particles[i].m);
  }
  fprintf(f, "\n</DataArray>\n");
Matthieu Constant's avatar
Matthieu Constant committed
1022
  fprintf(f, "<DataArray Name=\"Velocity\" NumberOfComponents = \"3\" type=\"Float32\" format=\"ascii\">\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1023
  for (size_t i = 0; i < vectorSize(p->particles); ++i) {
Matthieu Constant's avatar
Matthieu Constant committed
1024
1025
1026
1027
1028
1029
1030
1031
1032
#if DIMENSION == 2
    fprintf(f, "%.16g %.16g 0 ", p->velocity[i * 2], p->velocity[i * 2 + 1]);
#else
    fprintf(f, "%.16g %.16g %.16g ", p->velocity[i * 3], p->velocity[i * 3 + 1], p->velocity[i * 3 + 2]);
#endif
  }
  fprintf(f, "\n</DataArray>\n");
  fprintf(f, "</PointData>\n");
  fprintf(f, "<Points>\n<DataArray NumberOfComponents=\"3\" type=\"Float32\" format=\"ascii\">\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1033
  for (size_t i = 0; i < vectorSize(p->particles); ++i) {
Matthieu Constant's avatar
Matthieu Constant committed
1034
1035
1036
1037
1038
1039
#if DIMENSION == 2
    fprintf(f, "%.16g %.16g 0 ", p->position[i * 2], p->position[i * 2 + 1]);
#else
    fprintf(f, "%.16g %.16g %.16g ", p->position[i * 3], p->position[i * 3 + 1], p->position[i * 3 + 2]);
#endif
  }
1040
1041
  fprintf(f, "\n</DataArray>\n");
  fprintf(f, "</Points>\n");
Matthieu Constant's avatar
Matthieu Constant committed
1042
  fprintf(f, "</Piece>\n");
1043
  fprintf(f, "</PolyData>\n");
Matthieu Constant's avatar
Matthieu Constant committed
1044
1045
  fprintf(f, "</VTKFile>\n");
  fclose(f);
1046
  free(filename);
1047
  return 0;
Matthieu Constant's avatar
Matthieu Constant committed
1048
1049
}

1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
static int particleProblemWriteBndVtu(ParticleProblem *p, const char *dirname, int id) {
  char *filename = malloc((strlen(dirname)+50)*sizeof(char));
  sprintf(filename,"%s/boundaries_%05d.vtu",dirname, id);
  size_t nd = vectorSize(p->disks);
  size_t ns = vectorSize(p->segments);
#if DIMENSION == 3
  size_t nt = vectorSize(p->triangles);
#else
  size_t nt = 0;
#endif
Matthieu Constant's avatar
Matthieu Constant committed
1060
  FILE *f = fopen(filename, "w");
1061
1062
  fprintf(f, "<VTKFile type=\"UnstructuredGrid\">\n");
  fprintf(f, "<UnstructuredGrid>\n");
1063
1064
1065
1066
1067
1068
  fprintf(f, "<FieldData>\n");
  fprintf(f, "<DataArray Name=\"sizes\" NumberOfTuples=\"3\" NumberOfComponents=\"1\" type=\"Int32\" format=\"ascii\">\n");
  fprintf(f, "%i %i %i\n", (int)vectorSize(p->disks), (int)vectorSize(p->segments), 0);
  fprintf(f, "</DataArray>\n");
  fprintf(f, "</FieldData>\n");
  fprintf(f, "<Piece NumberOfPoints=\"%lu\" NumberOfCells=\"%lu\">\n", nt*3+ns*2+nd, nt+ns+nd);
Matthieu Constant's avatar
Matthieu Constant committed
1069
  fprintf(f, "<Points>\n<DataArray NumberOfComponents=\"3\" type=\"Float32\" format=\"ascii\">\n");
1070
1071
1072
1073
1074
  for (size_t i = 0; i < nd; ++i) {
    Disk *d = &p->disks[i];
    fprintf(f, "%.16g %.16g %.16g ", d->x[0], d->x[1], DIMENSION == 2 ? 0. : d->x[2]);
  }
  for (size_t i = 0; i < ns; ++i) {
Matthieu Constant's avatar
Matthieu Constant committed
1075
1076
1077
1078
1079
    Segment *s = &p->segments[i];
    fprintf(f, "%.16g %.16g %.16g ", s->p[0][0], s->p[0][1], DIMENSION == 2 ? 0. : s->p[0][2]);
    fprintf(f, "%.16g %.16g %.16g ", s->p[1][0], s->p[1][1], DIMENSION == 2 ? 0. : s->p[1][2]);
  }
#if DIMENSION == 3
1080
  for (int i = 0; i < nt; ++i) {
Matthieu Constant's avatar
Matthieu Constant committed
1081
1082
1083
1084
1085
    Triangle *t = &p->triangles[i];
    fprintf(f, "%.16g %.16g %.16g ", t->p[0][0], t->p[0][1], t->p[0][2]);
    fprintf(f, "%.16g %.16g %.16g ", t->p[1][0], t->p[1][1], t->p[1][2]);
    fprintf(f, "%.16g %.16g %.16g ", t->p[2][0], t->p[2][1], t->p[2][2]);
  }
1086
#endif
Matthieu Constant's avatar
Matthieu Constant committed
1087
1088
  fprintf(f, "\n</DataArray>\n");
  fprintf(f, "</Points>\n");
1089
  fprintf(f, "<Cells>\n");
Matthieu Constant's avatar
Matthieu Constant committed
1090
  fprintf(f, "<DataArray type=\"Int32\" Name=\"connectivity\">\n");
1091
1092
  for (size_t i = 0; i < nd; ++i) {
    fprintf(f, "%i ", (int)(i));
Matthieu Constant's avatar
Matthieu Constant committed
1093
  }
1094
1095
1096
1097
1098
  for (size_t i = 0; i < ns; ++i) {
    fprintf(f, "%i %i ", (int)(nd+i*2), (int)(nd+i*2+1));
  }
#if DIMENSION == 3
  for (size_t i = 0; i < nt; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1099
    fprintf(f, "%i %i %i ", (int)(nd+ns*2+i*3+0), (int)(nd+ns*2+i*3+1), (int)(nd+ns*2+i*3+2));
1100
1101
  }
#endif
Matthieu Constant's avatar
Matthieu Constant committed
1102
1103
  fprintf(f, "\n</DataArray>\n");
  fprintf(f, "<DataArray type=\"Int32\" Name=\"offsets\">\n");
1104
1105
1106
1107
1108
1109
1110
1111
  for (size_t i = 0; i < nd; ++i) {
    fprintf(f, "%i ", (int)(i+1));
  }
  for (size_t i = 0; i < ns; ++i) {
    fprintf(f, "%i ", (int)nd+(int)(i+1)*2);
  }
  for (size_t i = 0; i < nt; ++i) {
    fprintf(f, "%i ", (int)(nd+ns*2+(i+1)*3));
Matthieu Constant's avatar
Matthieu Constant committed
1112
1113
  }
  fprintf(f, "\n</DataArray>\n");
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
  fprintf(f, "<DataArray type=\"Int32\" Name=\"types\">\n");
  for (size_t i = 0; i < nd; ++i) {
    fprintf(f, "%i ", 1);
  }
  for (size_t i = 0; i < ns; ++i) {
    fprintf(f, "%i ", 3);
  }
  for (size_t i = 0; i < nt; ++i) {
    fprintf(f, "%i ", 5);
  }
  fprintf(f, "\n</DataArray>\n");
  fprintf(f, "</Cells>\n");
Matthieu Constant's avatar
Matthieu Constant committed
1126
  fprintf(f, "</Piece>\n");
1127
  fprintf(f, "</UnstructuredGrid>\n");
Matthieu Constant's avatar
Matthieu Constant committed
1128
1129
  fprintf(f, "</VTKFile>\n");
  fclose(f);
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
  free(filename);
  return 0;
}

int particleProblemWriteVtk(ParticleProblem *p, const char *dirname, int id) {
  if (particleProblemWriteParticleVtp(p,dirname,id) != 0)
    return -1;
  if (particleProblemWriteBndVtu(p,dirname,id) != 0)
    return -1;
  if (particleProblemWriteContactVtp(p,dirname,id) != 0)
    return -1;
  char *filename = malloc((strlen(dirname)+50)*sizeof(char));
  sprintf(filename,"%s/particles_problem_%05d.vtm",dirname, id);
  FILE *f = fopen(filename, "w");
  if (!f)
    return -1;
  fprintf(f, "<VTKFile type=\"vtkMultiBlockDataSet\" version=\"1.0\" byte_order=\"LittleEndian\">\n");
  fprintf(f, "<vtkMultiBlockDataSet>\n");
  fprintf(f, "<DataSet index=\"0\" file=\"particles_%05d.vtp\">\n",id);
  fprintf(f, "</DataSet>\n");
  fprintf(f, "<DataSet index=\"1\" file=\"boundaries_%05d.vtu\">\n",id);
  fprintf(f, "</DataSet>\n");
  fprintf(f, "<DataSet index=\"2\" file=\"contacts_%05d.vtp\">\n",id);
  fprintf(f, "</DataSet>\n");
  fprintf(f, "</vtkMultiBlockDataSet>\n");
  fprintf(f,"</VTKFile>\n");
  fclose(f);
  free(filename);

  return 0;
Matthieu Constant's avatar
Matthieu Constant committed
1160
1161
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
#include "mbxml.h"
int particleProblemImportVtk(ParticleProblem *p, const char *filename)
{
  MbXmlReader *r = mb_xml_reader_create(filename);
  MbXmlElement fileelement,gridelement,pieceelement;
  mb_xml_read_element(r,&fileelement);
  mb_xml_read_element(r,&gridelement);
  mb_xml_read_element(r,&pieceelement);
  MbXmlElement e;
  MBVtkDataArray a;
  size_t n_particles;
  for (size_t i = 0; i<pieceelement.n_attr; ++i) {
    if(!strcasecmp(pieceelement.attr[i].name,"NumberOfPoints")){
      sscanf(pieceelement.attr[i].value,"%lu", &n_particles);
      vectorClear(p->position);
      vectorClear(p->particles);
      vectorClear(p->externalForces);
      vectorClear(p->velocity);
      vectorPushN(&p->position,n_particles*DIMENSION);
      vectorPushN(&p->particles,n_particles);
      vectorPushN(&p->externalForces,n_particles*DIMENSION);
      vectorPushN(&p->velocity, n_particles*DIMENSION);
1184
1185
    }
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1186
1187
1188
  while(mb_xml_read_element(r,&e)!=1) {
    if (!strcasecmp(e.name,"Points")){
      mb_read_vtk_data_array(r,&a);
1189
      double *position = mb_vtk_data_array_to_double(&a,n_particles,3);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1190
1191
      for (size_t i = 0; i < n_particles; ++i)
        for (int j = 0; j < DIMENSION; ++j)
1192
1193
1194
          p->position[DIMENSION*i+j] = position[i*3+j];
      free(position);
      mb_vtk_data_array_destroy(&a);
1195
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1196
1197
    else if (!strcasecmp(e.name,"PointData")) {
      while(mb_read_vtk_data_array(r,&a) != 0){
1198
        double *value = mb_vtk_data_array_to_double(&a,n_particles,-1);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1199
1200
1201
        if(!strcasecmp(a.name,"Velocity")) {
          for (size_t i = 0; i < n_particles; ++i)
            for (int j = 0; j < DIMENSION; ++j)
1202
              p->velocity[DIMENSION*i+j] = value[i*3+j];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1203
1204
1205
        }
        else if(!strcasecmp(a.name,"Mass")) {
          for (size_t i = 0; i < n_particles; ++i)
1206
            p->particles[i].m = value[i];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1207
1208
1209
        }
        else if(!strcasecmp(a.name,"Radius")) {
          for (size_t i = 0; i < n_particles; ++i){
1210
            p->particles[i].r = value[i];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1211
1212
          }
        }
1213
1214
        free(value);
        mb_vtk_data_array_destroy(&a);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1215
      }
1216
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1217
1218
1219
    else {
      printf("unknown type in piece : \"%s\"\n", e.name);
      exit(1);
1220
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1221
    mb_xml_end_element(r,&e);
1222
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1223
1224
1225
1226
  mb_xml_end_element(r,&pieceelement);
  mb_xml_end_element(r,&gridelement);
  mb_xml_end_element(r,&fileelement);
  mb_xml_reader_destroy(r);
1227
1228
1229
  return 0;
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1230
int particleProblemImportBoundaryVtk(ParticleProblem *p, const char *filename)
1231
{
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1232
1233
1234
1235
1236
1237
1238
1239
1240
  MbXmlReader *r = mb_xml_reader_create(filename);
  MbXmlElement fileelement,gridelement;
  mb_xml_read_element(r,&fileelement);
  mb_xml_read_element(r,&gridelement);
  MbXmlElement e;
  MBVtkDataArray a;
  while(mb_xml_read_element(r,&e)!=1) {
    if (!strcasecmp(e.name, "FieldData")) {
      while(mb_read_vtk_data_array(r,&a)) {
1241
        mb_vtk_data_array_destroy(&a);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1242
      }
1243
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1244
    else if(!strcasecmp(e.name,"Piece")) {
1245
1246
1247
1248
1249
1250
1251
      int n_points, n_cells;
      for(size_t i = 0; i < e.n_attr; ++i) {
        if (!strcasecmp("NumberOfPoints",e.attr[i].name))
          sscanf(e.attr[i].value,"%i",&n_points);
        if (!strcasecmp("NumberOfCells",e.attr[i].name))
          sscanf(e.attr[i].value,"%i",&n_cells);
      }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
      MbXmlElement f;
      for (size_t i = 0; i<e.n_attr; ++i) {
        MBVtkDataArray points={0}, connectivity={0}, offsets={0}, types={0};
        while(mb_xml_read_element(r,&f)!=1) {
          if (!strcasecmp(f.name,"Points"))
            mb_read_vtk_data_array(r,&points);
          else if (!strcasecmp(f.name,"Cells")) {
            while(mb_read_vtk_data_array(r,&a) != 0){
              if(!strcasecmp(a.name,"connectivity"))
                connectivity = a;
              else if (!strcasecmp(a.name,"types"))
                types = a;
              else if (!strcasecmp(a.name,"offsets"))
                offsets = a;
              else {
                printf("unexpected data array in \"%s\" : \"%s\"\n",filename, a.name);
1268
                mb_vtk_data_array_destroy(&a);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1269
1270
1271
1272
              }
            }
            vectorClear(p->disks);
            vectorClear(p->segments);
1273
#if DIMENSION == 3
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1274
            vectorClear(p->triangles);
1275
#endif
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
            double *pts = mb_vtk_data_array_to_double(&points,n_points,3);
            int *off = mb_vtk_data_array_to_int(&offsets,n_cells,1);
            int *con = mb_vtk_data_array_to_int(&connectivity,off[n_cells-1],1);
            int *typ = mb_vtk_data_array_to_int(&types,n_cells,1);
            mb_vtk_data_array_destroy(&points);
            mb_vtk_data_array_destroy(&offsets);
            mb_vtk_data_array_destroy(&connectivity);
            mb_vtk_data_array_destroy(&types);
            for(size_t i = 0; i < n_cells; ++i) {
              int t = typ[i];
              int *nodes = con+(i==0 ? 0 : off[i-1]);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
              if (t == 1){
                particleProblemAddBoundaryDisk(p,pts+nodes[0]*3,0.,"");
              }
              else if (t == 3)
                particleProblemAddBoundarySegment(p, pts+nodes[0]*3,pts+nodes[1]*3,"");
#if DIMENSION == 3
              else if (t == 5)
                particleProblemAddBoundaryTriangle(p, pts+nodes[0]*3,pts+nodes[1]*3,pts+nodes[2]*3,"");
#endif
              else
                printf("Unknown boundary object : %i\n", t);
            }
          }
          else {
            printf("unexpected element in \"%s\" : \"%s\"\n",filename, f.name);
            exit(1);
          }
          mb_xml_end_element(r,&f);
Matthieu Constant's avatar
Matthieu Constant committed
1305
1306
1307
1308
        }
      }
    }
    else {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1309
1310
      printf("unexpected element in \"%s\" : \"%s\"\n",filename, e.name);
      exit(1);
Matthieu Constant's avatar
Matthieu Constant committed
1311
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1312
    mb_xml_end_element(r,&e);
Matthieu Constant's avatar
Matthieu Constant committed
1313
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1314
1315
1316
1317
  mb_xml_end_element(r,&gridelement);
  mb_xml_end_element(r,&fileelement);
  mb_xml_reader_destroy(r);
  return 0;
Matthieu Constant's avatar
Matthieu Constant committed
1318
1319
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
int particleProblemReadVtk(ParticleProblem *p, const char *dirname, int iter)
{
  char *f0 = malloc(sizeof(char)*(strlen(dirname)+30));
  char *f1 = malloc(sizeof(char)*(strlen(dirname)+30));
  sprintf(f0, "%s/particles_%05d.vtp",dirname,iter);
  sprintf(f1, "%s/boundaries_%05d.vtu",dirname,iter);
  particleProblemImportVtk(p,f0);
  particleProblemImportBoundaryVtk(p,f1);
  free(f0);
  free(f1);
  return 0;
}
Matthieu Constant's avatar
Matthieu Constant committed
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409

int *particleProblemDiskTag(ParticleProblem *p) {return p->diskTag;}
int *particleProblemSegmentTag(ParticleProblem *p) {return p->segmentTag;};
#if DIMENSION == 3
int *particleProblemTriangleTag(ParticleProblem *p) {return p->triangleTag;};
double *particleProblemTriangle(ParticleProblem *p) {return (double*)&p->triangles[0];}
#endif
double *particleProblemVelocity(ParticleProblem *p) {return &p->velocity[0];}
double *particleProblemDisk(ParticleProblem *p) {return (double*)&p->disks[0];}
double *particleProblemSegment(ParticleProblem *p) {return (double*)&p->segments[0];}
double *particleProblemParticle(ParticleProblem *p) {return (double*)&p->particles[0];}
double *particleProblemPosition(ParticleProblem *p){return &p->position[0];}
double *particleProblemExternalForces(ParticleProblem *p){return &p->externalForces[0];}
void particleProblemUseJacobi(ParticleProblem *p, int jacobi, double relax) {p->jacobi = jacobi; p->relax = relax;}
void particleProblemDelete(ParticleProblem *p) {
  vectorFree(p->particles);
  vectorFree(p->disks);
  vectorFree(p->diskTag);
  vectorFree(p->segments);
  vectorFree(p->segmentTag);
  vectorFree(p->contacts);
  vectorFree(p->position);
  vectorFree(p->velocity);
  vectorFree(p->externalForces);
  #if DIMENSION == 3
  vectorFree(p->triangles);
  vectorFree(p->triangleTag);
  #endif
  for (size_t i = 0; i < vectorSize(p->tagname); ++i)
    free(p->tagname[i]);
  vectorFree(p->tagname);
  free(p);
}

size_t particleProblemGetTagId(ParticleProblem *p, const char *tag) {
  for (size_t i = 0; i < vectorSize(p->tagname); ++i)
    if (!strcmp(tag, p->tagname[i]))
      return i;
  char *dup = malloc(sizeof(char) * (strlen(tag)+1));
  strcpy(dup, tag);
  *vectorPush(&p->tagname) = dup;
  return vectorSize(p->tagname) - 1;
}

const char *particleProblemGetTagName(ParticleProblem *p, size_t id) {
  if (id >= vectorSize(p->tagname))
    return NULL;
  return p->tagname[id];
}

ParticleProblem *particleProblemNew() {
  ParticleProblem *p = (ParticleProblem*)malloc(sizeof(ParticleProblem));
  p->jacobi = 0;
  p->relax = 1.;
  p->particles = NULL;
  p->contacts = NULL;
  p->tagname = NULL;
  p->disks = NULL;
  p->diskTag = NULL;
  p->segments = NULL;
  p->segmentTag = NULL;
  p->position = NULL;
  p->velocity = NULL;
  p->externalForces = NULL;
  #if DIMENSION == 3
  p->triangles = NULL;
  p->triangleTag = NULL;
  #endif
  return p;
}

unsigned long int particleProblemNParticle(ParticleProblem *p) {
  return vectorSize(p->particles);
}

double *particleProblemRadius(ParticleProblem *p) {
  return (double*)p->particles;
}