fluid_problem.c 37 KB
Newer Older
1

2
#include "tools.h"
3
4
#include <stdlib.h>
#include <stdio.h>
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
5
#include <float.h>
6
7
#include <math.h>
#include "fluid_problem.h"
8
#include "mesh_find.h"
9

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
10
#define n_fields (DIMENSION+1)
11
12
13

#define newton_max_it 20
#define newton_atol 1e-12
14
#define newton_rtol 1e-5
15

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
16
#if DIMENSION == 2
17
18
19

#define N_SF 3
#define N_N 3
20
21
22
#define N_QP 3
static double QP[][2] = {{1./6,1./6},{1./6,2./3},{2./3,1./6}};
static double QW[] = {1./6,1./6,1./6};
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
static void shape_functions(double *uvw, double *sf)
{
  sf[0] = 1-uvw[0]-uvw[1];
  sf[1] = uvw[0];
  sf[2] = uvw[1];
}
static void grad_shape_functions(const double dxidx[2][2], double gsf[3][2])
{
  for (int i = 0;  i < 2; ++i) {
    gsf[0][i] = -dxidx[0][i]-dxidx[1][i];
    gsf[1][i] = dxidx[0][i];
    gsf[2][i] = dxidx[1][i];
  }
}
static double detDD(const double m[2][2])
{
  return m[0][0]*m[1][1] - m[0][1]*m[1][0];
}
static double invDD(const double m[2][2], double inv[2][2])
{
  double d = detDD(m);
  inv[0][0] = m[1][1]/d;
  inv[0][1] = -m[0][1]/d;
  inv[1][0] = -m[1][0]/d;
  inv[1][1] = m[0][0]/d;
  return d;
}
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
50
#else
51
52
53

#define N_SF 4
#define N_N 4
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
54
55
56
57
58
59
60
61
62
#define N_QP 5
static double QP[][3] = {
  {0.25, 0.25, 0.25},
  {0.5, 1./6, 1./6},
  {1./6, 0.5, 1./6},
  {1./6, 1./6, 0.5},
  {1./6, 1./6, 1./6}
};
static double QW[] = {-16./120, 9./120, 9./120, 9./120, 9./120};
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
static void shape_functions(double *uvw, double *sf)
{
  sf[0] = 1-uvw[0]-uvw[1]-uvw[2];
  sf[1] = uvw[0];
  sf[2] = uvw[1];
  sf[3] = uvw[2];
}
static void grad_shape_functions(const double dxidx[3][3], double gsf[4][3])
{
  for (int i = 0; i < 3; ++i) {
    gsf[0][i] = -dxidx[0][i]-dxidx[1][i]-dxidx[2][i];
    gsf[1][i] = dxidx[0][i];
    gsf[2][i] = dxidx[1][i];
    gsf[3][i] = dxidx[2][i];
  }
};
static double detDD(const double m[3][3])
{
  return m[0][0]*(m[1][1]*m[2][2]-m[2][1]*m[1][2])
        -m[1][0]*(m[0][1]*m[2][2]-m[2][1]*m[0][2])
        +m[2][0]*(m[0][1]*m[1][2]-m[1][1]*m[0][2]);
}
static double invDD(const double m[3][3], double inv[3][3]) {
  double d = detDD(m);
  inv[0][0] = (m[1][1]*m[2][2]-m[2][1]*m[1][2])/d;
  inv[0][1] = -(m[0][1]*m[2][2]-m[2][1]*m[0][2])/d;
  inv[0][2] = (m[0][1]*m[1][2]-m[1][1]*m[0][2])/d;
  inv[1][0] = -(m[1][0]*m[2][2]-m[2][0]*m[1][2])/d;
  inv[1][1] = (m[0][0]*m[2][2]-m[2][0]*m[0][2])/d;
  inv[1][2] = -(m[0][0]*m[1][2]-m[1][0]*m[0][2])/d;
  inv[2][0] = (m[1][0]*m[2][1]-m[2][0]*m[1][1])/d;
  inv[2][1] = -(m[0][0]*m[2][1]-m[2][0]*m[0][1])/d;
  inv[2][2] = (m[0][0]*m[1][1]-m[1][0]*m[0][1])/d;
  return d;
};

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
99
100
#endif

101
102
103
104
105
106
107
108
109
110
111
112
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;
}
113

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
114
int fluid_problem_export(const FluidProblem *problem, const char *output_dir, double t, int iter)
115
{
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
116
117
118
119
120
  char file_name[1024];
  sprintf(file_name,"%s/fluid_%05i.msh",output_dir, iter);
  FILE *f = fopen(file_name, "w");
  if (!f){
    printf("Cannot open file \"%s\" for writing.\n", file_name);
121
    return -1;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
122
123
124
125
  }
  if(mesh_write_msh(problem->mesh, f))
    return -1;
  if(mesh_write_msh_scalar(problem->mesh, f, "porosity", t, iter, problem->porosity, 1, 0))
126
    return -1;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
127
128
  int comp[]={0,1,2};
  if(mesh_write_msh_vector(problem->mesh, f, "uv", t, iter, problem->solution, n_fields, comp))
129
    return -1;
Matthieu Constant's avatar
Matthieu Constant committed
130
   if(mesh_write_msh_vector(problem->mesh, f, "force", t, iter, problem->node_force, DIMENSION, comp))
Matthieu Constant's avatar
Matthieu Constant committed
131
    return -1;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
132
  if(mesh_write_msh_scalar(problem->mesh, f, "p", t, iter, problem->solution, n_fields, DIMENSION))
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
133
134
    return -1;
  fclose(f);
135
136
137
  return 0;
}

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
int fluid_problem_export_vtk(const FluidProblem *problem, const char *output_dir, double t, int iter)
{
  Mesh *mesh = problem->mesh;
  char file_name[1024];
  sprintf(file_name,"%s/fluid_%05i.vtu",output_dir, iter);
  FILE *f = fopen(file_name, "w");
  if (!f){
    printf("Cannot open file \"%s\" for writing.\n", file_name);
    return -1;
  }
  fprintf(f,"<VTKFile type=\"UnstructuredGrid\" format=\"0.1\">\n");
  fprintf(f,"<UnstructuredGrid>\n");
  fprintf(f,"<Piece NumberOfPoints=\"%i\" NumberOfCells=\"%i\">\n", mesh->n_nodes, mesh->n_elements);
  fprintf(f,"<Points>\n");
  fprintf(f,"<DataArray NumberOfComponents=\"3\" type=\"Float64\" format=\"ascii\">\n");
  for (int i = 0; i < 3*mesh->n_nodes; ++i) {
    fprintf(f,"%g ", mesh->x[i]);
  }
  fprintf(f,"\n</DataArray>\n");
  fprintf(f,"</Points>\n");
  fprintf(f,"<Cells>\n");
  fprintf(f,"<DataArray  type=\"Int32\" Name=\"connectivity\" format=\"ascii\">\n");
Matthieu Constant's avatar
visu 3d    
Matthieu Constant committed
160
  for (int i = 0; i < mesh->n_elements*(DIMENSION+1); ++i) {
161
162
163
164
165
    fprintf(f,"%i ", mesh->elements[i]);
  }
  fprintf(f,"\n</DataArray>\n");
  fprintf(f,"<DataArray  type=\"Int32\" Name=\"offsets\" format=\"ascii\">\n");
  for(int i = 0; i < mesh->n_elements;++i) {
Matthieu Constant's avatar
visu 3d    
Matthieu Constant committed
166
    fprintf(f,"%i ", (i+1)*(DIMENSION+1));
167
  }
Matthieu Constant's avatar
visu 3d    
Matthieu Constant committed
168
#if DIMENSION == 2
169
170
171
172
173
  fprintf(f,"\n</DataArray>\n");
  fprintf(f,"<DataArray  type=\"Int32\" Name=\"types\" format=\"ascii\">\n");
  for (int i = 0; i < mesh->n_elements; ++i) {
    fprintf(f,"%i ", 5); // 10 for 3d
  }
Matthieu Constant's avatar
visu 3d    
Matthieu Constant committed
174
175
176
177
178
179
180
#else
  fprintf(f,"\n</DataArray>\n");
  fprintf(f,"<DataArray  type=\"Int32\" Name=\"types\" format=\"ascii\">\n");
  for (int i = 0; i < mesh->n_elements; ++i) {
    fprintf(f,"%i ", 10); // 10 for 3d
  }
#endif
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
  fprintf(f,"\n</DataArray>\n");
  fprintf(f,"</Cells>\n");
  //fprintf(f,"<PointData Scalars=\"Pressure Porosity\" Vectors=\"Velocity\">\n");
  fprintf(f,"<PointData Scalars=\"Porosity Pressure\" Vectors=\"Velocity\">\n");
  fprintf(f,"<DataArray Name=\"Porosity\" NumberOfComponents = \"1\" type=\"Float64\" format=\"ascii\">\n");
  for (int i = 0; i < mesh->n_nodes; ++i) {
    fprintf(f, "%g ", problem->porosity[i]);
  }
  fprintf(f,"\n</DataArray>\n");
  fprintf(f,"<DataArray Name=\"Pressure\" NumberOfComponents = \"1\" type=\"Float64\" format=\"ascii\">\n");
  for (int i = 0; i < mesh->n_nodes; ++i) {
    fprintf(f, "%g ", problem->solution[i*(DIMENSION+1)+DIMENSION]);
  }
  fprintf(f,"\n</DataArray>\n");
  fprintf(f,"<DataArray Name=\"Velocity\" NumberOfComponents = \"3\" type=\"Float64\" format=\"ascii\">\n");
  for (int i = 0; i < mesh->n_nodes; ++i) {
    for (int j = 0; j < DIMENSION; ++j)
      fprintf(f, "%g ", problem->solution[i*(DIMENSION+1)+j]);
    if(DIMENSION==2)
      fprintf(f, "0 ");
  }
  fprintf(f,"\n</DataArray>\n");
  fprintf(f,"</PointData>\n");
  fprintf(f,"</Piece>\n");
  fprintf(f,"</UnstructuredGrid>\n");
  fprintf(f,"</VTKFile>\n");

          //t[:] = 5 if dim == 2 else 10
/*
output = open("%s/f_%05i.vtu" %(odir, oiter), "w")
          vertices = np.array(self._mesh.vertices)[:,:3]
          elements = np.array([[v[3] for v in e.vertices] for e in self._dof.getElements(0)], np.int32) -1
            v = np.zeros((vertices.shape[0], 3))
            for d in range (dim) :
                v[elements, d] = self._jac.x[:,:,d]

            output.write("<Cells>\n")
            output.write("<DataArray  type=\"Int32\" Name=\"connectivity\" format=\"ascii\">\n")
            np.savetxt(output, elements, fmt="%i")
            output.write("</DataArray>\n")
            output.write("<DataArray  type=\"Int32\" Name=\"offsets\" format=\"ascii\">\n")
            np.savetxt(output, (np.array(range(elements.shape[0]), np.int32)+1)*(dim+1), fmt="%i", newline=" ")
            output.write("</DataArray>\n")
            output.write("<DataArray  type=\"Int32\" Name=\"types\" format=\"ascii\">\n")
            t = np.ndarray((elements.shape[0]), np.int32)
            np.savetxt(output, t, fmt="%i", newline=" ")
            output.write("</DataArray>\n")
            output.write("</Cells>\n")
            output.write("<PointData Scalars=\"Pressure Porosity\" Vectors=\"Velocity\">\n")
            output.write("<DataArray Name=\"Porosity\" NumberOfComponents = \"1\" type=\"Float64\" format=\"ascii\">")
            p = np.ndarray((vertices.shape[0]))
            p[np.transpose(elements)] = self._jac.dofManager.getField(0, self._pf.porosity)
            np.savetxt(output, p)
            output.write("</DataArray>\n")
            output.write("<DataArray Name=\"Pressure\" NumberOfComponents = \"1\" type=\"Float64\" format=\"ascii\">")
            p[np.transpose(elements)] = self._dof.getField(dim, self._sol)
            np.savetxt(output, p)
            output.write("</DataArray>\n")
            output.write("<DataArray Name=\"Velocity\" NumberOfComponents = \"3\" type=\"Float64\" format=\"ascii\">")
            v = np.zeros((vertices.shape[0], 3))
            for d in range (self._jac.dim) :
                v[np.transpose(elements), d] = self._dof.getField(d, self._sol)[:self._jac.dim+1,:]
            np.savetxt(output, v)
            output.write("</DataArray>\n")
            output.write("</PointData>\n")
            output.write("</Piece>\n")
            output.write("</UnstructuredGrid>\n")
            output.write("</VTKFile>\n")

    */
  fclose(f);
  return 0;
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
255
#if DIMENSION == 2
Matthieu Constant's avatar
Matthieu Constant committed
256
static void particle_drag(double u[2], double mu, double rho, double d, double c, double drag[2], double dt, double mass, double rhop, double gradp[2])
257
258
259
{
  double normu = hypot(u[0],u[1]);
  //Reynolds/|u_p-u_f|
Matthieu Constant's avatar
Matthieu Constant committed
260
  double Re_O_u = sqrt(d)*c/mu*rho;
261
262
263
  double Cd_u = 0.63*sqrt(normu)+4.8/sqrt(Re_O_u);
  Cd_u = Cd_u*Cd_u;
  double f = pow(c,-1.8);
Matthieu Constant's avatar
Matthieu Constant committed
264
  double r = d/2.;
Matthieu Constant's avatar
Matthieu Constant committed
265
  double GoU = f*Cd_u*rho/2*(2*r);
Matthieu Constant's avatar
Matthieu Constant committed
266
267
268
269
270
271
272

  double a = 0;
  double h = (1+(1-c)*rhop/(c*rho)+a)/(a+1);
  double F = (mass*GoU)/(mass+h*dt*GoU);

  drag[0] = (u[0]-gradp[0]*dt/rhop)*F;
  drag[1] = (u[1]-9.81*dt*(1-rho/rhop)-gradp[1]*dt/rhop)*F;
273
}
274
275
276
#else
static void particle_drag(double u[3], double mu, double rho, double d, double c, double drag[3], double dt, double mass, double rhop, double gradp[3])
{
Matthieu Constant's avatar
Matthieu Constant committed
277
  double normu = pow(u[0]*u[0]+u[1]*u[1]+u[2]*u[2],0.5);
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  //Reynolds/|u_p-u_f|
  double Re_O_u = d*c/mu*rho;
  double Cd_u = 0.63*sqrt(normu)+4.8/sqrt(Re_O_u);
  Cd_u = Cd_u*Cd_u;
  double f = pow(c,-1.8);
  double r = d/2.;
  double GoU = f*Cd_u*rho/2*(M_PI*r*r);

  double a = 0;
  double h = (1+(1-c)*rhop/(c*rho)+a)/(a+1);
  double F = (mass*GoU)/(mass+h*dt*GoU);

  drag[0] = (u[0]-gradp[0]*dt/rhop)*F;
  drag[1] = (u[1]-9.81*dt*(1-rho/rhop)-gradp[1]*dt/rhop)*F;
  drag[2] = (u[2]-gradp[2]*dt/rhop)*F;

}
#endif
296

297
#if DIMENSION == 2
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
298
void fluid_problem_compute_node_particle_force(FluidProblem *problem, double dt, double *particle_force) {
299
300
  double *porosity = problem->porosity;
  Mesh *mesh = problem->mesh;
Matthieu Constant's avatar
Matthieu Constant committed
301
  const double *solution = problem->solution;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
302
  for (int i = 0; i < mesh->n_nodes*DIMENSION; ++i) {
Matthieu Constant's avatar
Matthieu Constant committed
303
304
    problem->node_force[i] = 0.0;
  }
305
306
307
308
  for (int i = 0; i < problem->n_particles; ++i) {
    double *xi = problem->particle_uvw + 2*i;
    double phi[3] = {1-xi[0]-xi[1],xi[0],xi[1]};
    int iel = problem->particle_element_id[i];
309
310
311
    double vol = problem->particle_volume[i];
    double mass = problem->particle_mass[i];
    particle_force[i*2+0] = 0;
312
    particle_force[i*2+1] = problem->g*(mass-problem->rho*vol);
313
314
315
316
    if(iel < 0){
      continue;
    }

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
317
    int *tri = problem->mesh->elements+iel*3;
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
    double C[] = {porosity[tri[0]],porosity[tri[1]],porosity[tri[2]]};
    double U[] = {solution[tri[0]*3+0],solution[tri[1]*3+0],solution[tri[2]*3+0]};
    double V[] = {solution[tri[0]*3+1],solution[tri[1]*3+1],solution[tri[2]*3+1]};
    double P[] = {solution[tri[0]*3+2],solution[tri[1]*3+2],solution[tri[2]*3+2]};
    double vf[2] = {
      phi[0]*U[0]+phi[1]*U[1]+phi[2]*U[2],
      phi[0]*V[0]+phi[1]*V[1]+phi[2]*V[2]
    };
    double c = phi[0]*C[0]+phi[1]*C[1]+phi[2]*C[2];
    const double x[3][2] = {
      {mesh->x[tri[0]*3+0],mesh->x[tri[0]*3+1]},
      {mesh->x[tri[1]*3+0],mesh->x[tri[1]*3+1]},
      {mesh->x[tri[2]*3+0],mesh->x[tri[2]*3+1]}};
    const double dxdxi[2][2] = {
      {x[1][0]-x[0][0],x[2][0]-x[0][0]},
      {x[1][1]-x[0][1],x[2][1]-x[0][1]}};
    const double detj = dxdxi[0][0]*dxdxi[1][1]-dxdxi[0][1]*dxdxi[1][0];
    const double dxidx[2][2] = {
      {dxdxi[1][1]/detj,-dxdxi[0][1]/detj},
      {-dxdxi[1][0]/detj,dxdxi[0][0]/detj}
    };
    const double dphi[][2] = {
      {-dxidx[0][0]-dxidx[1][0],-dxidx[0][1]-dxidx[1][1]},
      {dxidx[0][0],dxidx[0][1]},
      {dxidx[1][0],dxidx[1][1]}
    };
    double gradp[2] = {
      dphi[0][0]*P[0]+dphi[1][0]*P[1]+dphi[2][0]*P[2],
      dphi[0][1]*P[0]+dphi[1][1]*P[1]+dphi[2][1]*P[2]
    };
    double du[2] = {
      problem->particle_velocity[i*2+0]-vf[0]/c,
      problem->particle_velocity[i*2+1]-vf[1]/c
    };
352
    double rhop = mass/vol;
353
354
    double d = 2*sqrt(vol/M_PI);
    double drag[2];
Matthieu Constant's avatar
Matthieu Constant committed
355
356
357
358
359
360
    particle_drag(du,problem->mu,problem->rho,d,c,drag, dt, mass, rhop, gradp);
    /*FILE *log = fopen("DragForce.txt", "at");
    if (!log) log = fopen("DragForce.txt", "wt");
    fprintf(log, "%d, %e, %e, %e, %e, %e, %e\n", i, du[0], du[1], drag[0], drag[1], mass, vol);
    fclose(log);*/

361
362
    particle_force[i*2+0] += -drag[0]-vol*gradp[0];
    particle_force[i*2+1] += -drag[1]-vol*gradp[1];
Matthieu Constant's avatar
Matthieu Constant committed
363
    //printf("%e,%e,%e,%e,%e,%e\n",particle_force[i*2+0],particle_force[i*2+1],-drag[0],-drag[1],0.,problem->g*(mass-problem->rho*vol));
364
    for (int iphi = 0; iphi < 3; ++iphi) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
365
366
      problem->node_force[tri[iphi]*2+0] += drag[0]*phi[iphi];
      problem->node_force[tri[iphi]*2+1] += drag[1]*phi[iphi];
367
368
    }
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
369
370
371
372
  for (int i = 0; i < mesh->n_nodes; ++i) {
    problem->node_force[i*2+0] /= problem->node_volume[i];
    problem->node_force[i*2+1] /= problem->node_volume[i];
  }
373
374
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#else
void fluid_problem_compute_node_particle_force(FluidProblem *problem, double dt, double *particle_force) {
  double *porosity = problem->porosity;
  Mesh *mesh = problem->mesh;
  const double *solution = problem->solution;
  for (int i = 0; i < mesh->n_nodes*3; ++i) {
    problem->node_force[i] = 0.0;
  }
  for (int i = 0; i < problem->n_particles; ++i) {
    double *xi = problem->particle_uvw + 3*i;
    double phi[4] = {1-xi[0]-xi[1]-xi[2],xi[0],xi[1],xi[2]};
    int iel = problem->particle_element_id[i];
    double vol = problem->particle_volume[i];
    double mass = problem->particle_mass[i];
    particle_force[i*3+0] = 0;
    particle_force[i*3+1] = problem->g*(mass-problem->rho*vol);
    particle_force[i*3+2] = 0;
    if(iel < 0){
      continue;
    }

    int *tet = problem->mesh->elements+iel*4;
    double C[] = {porosity[tet[0]],porosity[tet[1]],porosity[tet[2]],porosity[tet[3]]};
    double U[] = {solution[tet[0]*4+0],solution[tet[1]*4+0],solution[tet[2]*4+0],solution[tet[3]*4+0]};
    double V[] = {solution[tet[0]*4+1],solution[tet[1]*4+1],solution[tet[2]*4+1],solution[tet[3]*4+1]};
    double W[] = {solution[tet[0]*4+2],solution[tet[1]*4+2],solution[tet[2]*4+2],solution[tet[3]*4+2]};
    double P[] = {solution[tet[0]*4+3],solution[tet[1]*4+3],solution[tet[2]*4+3],solution[tet[3]*4+3]};
    double vf[3] = {
      phi[0]*U[0]+phi[1]*U[1]+phi[2]*U[2]+phi[3]*U[3],
      phi[0]*V[0]+phi[1]*V[1]+phi[2]*V[2]+phi[3]*V[3],
      phi[0]*W[0]+phi[1]*W[1]+phi[2]*W[2]+phi[3]*W[3]
    };
    double c = phi[0]*C[0]+phi[1]*C[1]+phi[2]*C[2]+phi[3]*C[3];
    /*printf("c=%e\n",c);
    printf("phi[0]=%e\n",phi[0]);
    printf("phi[1]=%e\n",phi[1]);
    printf("phi[2]=%e\n",phi[2]);
    printf("phi[3]=%e\n",phi[3]);*/
    const double x[4][3] = {
      {mesh->x[tet[0]*3+0],mesh->x[tet[0]*3+1],mesh->x[tet[0]*3+2]},
      {mesh->x[tet[1]*3+0],mesh->x[tet[1]*3+1],mesh->x[tet[1]*3+2]},
      {mesh->x[tet[2]*3+0],mesh->x[tet[2]*3+1],mesh->x[tet[2]*3+2]},
      {mesh->x[tet[3]*3+0],mesh->x[tet[3]*3+1],mesh->x[tet[3]*3+2]}};
    const double dxdxi[3][3] = {
      {x[1][0]-x[0][0],x[2][0]-x[0][0],x[3][0]-x[0][0]},
      {x[1][1]-x[0][1],x[2][1]-x[0][1],x[3][1]-x[0][1]},
      {x[1][2]-x[0][2],x[2][2]-x[0][2],x[3][2]-x[0][2]}};
    const double detj = dxdxi[0][0]*(dxdxi[1][1]*dxdxi[2][2]-dxdxi[2][1]*dxdxi[1][2])-dxdxi[1][0]*(dxdxi[0][1]*dxdxi[2][2]-dxdxi[2][1]*dxdxi[0][2])+dxdxi[2][0]*(dxdxi[0][1]*dxdxi[1][2]-dxdxi[1][1]*dxdxi[0][2]);
    const double dxidx[3][3] = {
      {(dxdxi[1][1]*dxdxi[2][2]-dxdxi[2][1]*dxdxi[1][2])/detj,-(dxdxi[0][1]*dxdxi[2][2]-dxdxi[2][1]*dxdxi[0][2])/detj, (dxdxi[0][1]*dxdxi[1][2]-dxdxi[1][1]*dxdxi[0][2])/detj},
      {-(dxdxi[1][0]*dxdxi[2][2]-dxdxi[2][0]*dxdxi[1][2])/detj,(dxdxi[0][0]*dxdxi[2][2]-dxdxi[2][0]*dxdxi[0][2])/detj,-(dxdxi[0][0]*dxdxi[1][2]-dxdxi[1][0]*dxdxi[0][2])/detj },
      {(dxdxi[1][0]*dxdxi[2][1]-dxdxi[2][0]*dxdxi[1][1])/detj,-(dxdxi[0][0]*dxdxi[2][1]-dxdxi[2][0]*dxdxi[0][1])/detj, (dxdxi[0][0]*dxdxi[1][1]-dxdxi[1][0]*dxdxi[0][1])/detj}
    };
    const double dphi[][3] = {
      {-dxidx[0][0]-dxidx[1][0]-dxidx[2][0],-dxidx[0][1]-dxidx[1][1]-dxidx[2][1],-dxidx[0][2]-dxidx[1][2]-dxidx[2][2]},
      {dxidx[0][0],dxidx[0][1],dxidx[0][2]},
      {dxidx[1][0],dxidx[1][1],dxidx[1][2]},
      {dxidx[2][0],dxidx[2][1],dxidx[2][2]}
    };
    double gradp[3] = {
      dphi[0][0]*P[0]+dphi[1][0]*P[1]+dphi[2][0]*P[2]+dphi[3][0]*P[3],
      dphi[0][1]*P[0]+dphi[1][1]*P[1]+dphi[2][1]*P[2]+dphi[3][1]*P[3],
      dphi[0][2]*P[0]+dphi[1][2]*P[1]+dphi[2][2]*P[2]+dphi[3][2]*P[3]
    };
    double du[3] = {
      problem->particle_velocity[i*3+0]-vf[0]/c,
      problem->particle_velocity[i*3+1]-vf[1]/c,
      problem->particle_velocity[i*3+2]-vf[2]/c
    };
    double rhop = mass/vol;
    double d = 2*pow(3./4.*vol/M_PI,1./3.);
    double drag[3];
    particle_drag(du,problem->mu,problem->rho,d,c,drag, dt, mass, rhop,gradp);
    particle_force[i*3+0] += -drag[0]-vol*gradp[0];
    particle_force[i*3+1] += -drag[1]-vol*gradp[1];
    particle_force[i*3+2] += -drag[2]-vol*gradp[2];
    //printf("drag[0]=%e\n",drag[0]);
    //printf("drag[1]=%e\n",drag[1]);
    //printf("drag[2]=%e\n",drag[2]);
    for (int iphi = 0; iphi < 4; ++iphi) {
      problem->node_force[tet[iphi]*3+0] += drag[0]*phi[iphi];
      problem->node_force[tet[iphi]*3+1] += drag[1]*phi[iphi];
      problem->node_force[tet[iphi]*3+2] += drag[2]*phi[iphi];
    }
  }
  for (int i = 0; i < mesh->n_nodes; ++i) {
    problem->node_force[i*3+0] /= problem->node_volume[i];
    problem->node_force[i*3+1] /= problem->node_volume[i];
    problem->node_force[i*3+2] /= problem->node_volume[i];
  }
}
466
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
467
468
469
470
471
472
473
474
475
476
477
478
479

static void fluid_problem_assemble_system(FluidProblem *problem, double *rhs, const double *solution_old, double dt)
{
  HXTLinearSystem *lsys = problem->linear_system;
  const Mesh *mesh = problem->mesh;
  const double *solution = problem->solution;
  const double *porosity = problem->porosity;
  const double *old_porosity = problem->old_porosity;
  const double *node_force = problem->node_force;
  const double mu = problem->mu;
  const double rho = problem->rho;
  const double epsilon = problem->epsilon;
  for (int iel=0; iel < mesh->n_elements; ++iel) {
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
    const unsigned int *el = &mesh->elements[iel*N_N];
    double dxdxi[DIMENSION][DIMENSION], dxidx[DIMENSION][DIMENSION], dphi[N_N][DIMENSION];
    for (int i = 0; i < DIMENSION; ++i)
      for (int j = 0; j < DIMENSION; ++j)
        dxdxi[i][j] = mesh->x[el[j+1]*3+i] - mesh->x[el[0]*3+i];
    const double detj = invDD(dxdxi, dxidx);
    grad_shape_functions(dxidx, dphi);
    double local_vector[n_fields*N_SF] = {0};
    double local_matrix[n_fields*n_fields*N_SF*N_SF] = {0};
    double dc[DIMENSION]={0}, du[DIMENSION][DIMENSION]={{0}}, dp[DIMENSION]={0};
    for (int i = 0; i< N_SF; ++i) {
      for (int j = 0; j < DIMENSION; ++j) {
        dc[j] += dphi[i][j]*porosity[el[i]];
        dp[j] += dphi[i][j]*solution[el[i]*n_fields+DIMENSION];
        for (int k = 0; k < DIMENSION; ++k)
          du[k][j] += dphi[i][j]*solution[el[i]*n_fields+k];
      }
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
498
    for (int iqp = 0; iqp< N_QP; ++iqp) {
499
500
501
502
503
504
505
506
507
508
509
510
511
      double phi[N_SF];
      shape_functions(QP[iqp],phi);
      double u[DIMENSION]={0}, dudt[DIMENSION]={0}, fu[DIMENSION]={0}, p=0, c=0, dcdt=0;
      for (int i = 0; i < N_SF; ++i) {
        for (int j = 0; j < DIMENSION; ++j){
          u[j] += solution[el[i]*n_fields+j]*phi[i];
          dudt[j] += (solution[el[i]*n_fields+j]-solution_old[el[i]*n_fields+j])/dt*phi[i];
          fu[j] += node_force[el[i]*DIMENSION+j]*phi[i];
        }
        p += solution[el[i]*n_fields+DIMENSION]*phi[i];
        c += porosity[el[i]]*phi[i];
        dcdt += (porosity[el[i]]-old_porosity[el[i]])/dt*phi[i];
      }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
512
      const double jw = QW[iqp]*detj;
513
      for (int iphi = 0; iphi < N_SF; ++iphi) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
514
        double phii = phi[iphi];
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
        const double *dphii = dphi[iphi];
        double tau[DIMENSION][DIMENSION];
        for (int i = 0; i < DIMENSION; ++i)
          for (int j = 0; j < DIMENSION; ++j)
            tau[i][j] = du[i][j]-u[i]*dc[j]/c;
        for (int j = 0; j < DIMENSION; ++j) {
          double utau = 0;
          double dphisigma=0;
          for (int k = 0; k < DIMENSION; ++k) {
            utau += u[k]*tau[j][k];
            dphisigma += 2*mu*dphii[k]*0.5*(tau[k][j]+tau[j][k]);
          }
          local_vector[iphi+N_SF*j] += jw*(
              dphisigma
              +rho*phii*(dudt[j]-u[j]/c*dcdt+utau/c)
              -p*dphii[j]-fu[j]*phii
              );
        }
        double dphidp = 0, divu = 0;
        for (int k = 0; k < DIMENSION; ++k){
          dphidp += dphii[k]*dp[k];
          divu += du[k][k];
        }
        local_vector[iphi+N_SF*DIMENSION] += jw*(epsilon*dphidp+phii*(divu+dcdt));
        for (int jphi = 0; jphi < N_SF; ++jphi) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
540
          double phij = phi[jphi];
541
          const double *dphij = dphi[jphi];
542
543
544
545
546
          int P = DIMENSION;
          //int N2 = N_SF*N_SF;
          //int IDX = iphi*N2+jphi;
          int N2 = n_fields*N_SF;
          int IDX = (iphi*N2+jphi)*n_fields;
547
548
549
550
551
552
553
554
555
556
557
558
#define LOCAL_MATRIX(a,b) local_matrix[IDX+N2*a+b]
          double dphiidphij = 0;
          double dtau[DIMENSION];
          double udtau = 0;
          double dphiidtau = 0;
          for (int j = 0; j < DIMENSION; ++j) {
            dtau[j] = dphij[j]-phij*dc[j]/c;
            dphiidphij += dphii[j]*dphij[j];
            udtau += u[j]*dtau[j];
            dphiidtau += dphii[j]*dtau[j];
          }
          for (int j = 0; j < DIMENSION; ++j) {
559
            int U = j;
560
561
562
            LOCAL_MATRIX(U,P) += -jw*dphii[j]*phij;
            LOCAL_MATRIX(P,U) +=  jw*phii*dphij[j];
            for (int k = 0; k < DIMENSION; ++k) {
563
              int V = k;
564
565
566
567
568
              LOCAL_MATRIX(U,V) += jw*rho*phii*phij*tau[j][k]/c+jw*2*mu*dphii[k]*dtau[j]*0.5;
            }
            LOCAL_MATRIX(U,U) += jw*mu*2*0.5*dphiidtau + jw*rho*phii*(phij/dt-phij/c*dcdt+udtau/c);
          }
          LOCAL_MATRIX(P,P) +=  jw*epsilon*dphiidphij;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
569
570
571
572
573
        }
      }
    }
    hxtLinearSystemAddToMatrix(lsys,iel,iel,local_matrix);
    hxtLinearSystemAddToRhs(lsys,rhs,iel,local_vector);
574
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
575

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
576
577
578
579
580
581
582
583
584
585
  for (int ibnd = 0; ibnd < problem->n_strong_boundaries; ++ibnd) {
    const StrongBoundary *bnd = problem->strong_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){
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
586
587
      hxtLinearSystemSetMatrixRowIdentity(lsys, mesh->phys_nodes[iphys][i], bnd->field);
      hxtLinearSystemSetRhsEntry(lsys, rhs,mesh->phys_nodes[iphys][i], bnd->field, 0.);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
588
589
    }
  }
590
}
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
591
592


593

594
static void apply_boundary_conditions(const Mesh *mesh, double *solution, int nBnd, StrongBoundary *bnds)
595
{
596
597
  for (int ibnd = 0; ibnd < nBnd; ++ibnd) {
    StrongBoundary *bnd = bnds + ibnd;
598
599
600
601
602
603
604
605
    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);
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
606
    double *x = malloc(mesh->phys_n_nodes[iphys]*sizeof(double)*DIMENSION);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
607
608
609
    double *v = malloc(mesh->phys_n_nodes[iphys]*sizeof(double));
    for (int i = 0; i < mesh->phys_n_nodes[iphys]; ++i){
      int j = mesh->phys_nodes[iphys][i];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
610
611
      for (int k = 0; k < DIMENSION; ++k)
        x[i*DIMENSION+k] = mesh->x[j*3+k];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
612
613
    }
    bnd->apply(mesh->phys_n_nodes[iphys], x, v);
614
    for (int i = 0; i < mesh->phys_n_nodes[iphys]; ++i)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
615
616
617
      solution[mesh->phys_nodes[iphys][i]*n_fields+bnd->field] = v[i];
    free(x);
    free(v);
618
619
620
  }
}

621
int fluid_problem_implicit_euler(FluidProblem *problem, double dt)
622
{
623
624
  static double totaltime = 0;
  struct timespec tic, toc;
625
  const Mesh *mesh = problem->mesh;
626
  apply_boundary_conditions(mesh, problem->solution, problem->n_strong_boundaries, problem->strong_boundaries);
627
  double *solution_old = malloc(mesh->n_nodes*n_fields*sizeof(double));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
628
  double *rhs = malloc(mesh->n_nodes*n_fields*sizeof(double));
629
630
631
632
  for (int i=0; i<mesh->n_nodes*n_fields; ++i)
    solution_old[i] = problem->solution[i];
  double *dx = malloc(sizeof(double)*mesh->n_nodes*n_fields);
  double norm0 = 0;
633
634
  int i;
  for (i=0; i<newton_max_it; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
635
636
637
638
639
640
    double norm = 0;
    hxtLinearSystemZeroMatrix(problem->linear_system);
    for (int i=0; i<mesh->n_nodes*n_fields; ++i)
      rhs[i] = 0;
    fluid_problem_assemble_system(problem, rhs, solution_old, dt);
    hxtLinearSystemGetRhsNorm(problem->linear_system,rhs,&norm);
641
642
643
644
645
646
647
    printf("iter %i norm = %g\n", i, norm);
    if (norm < newton_atol)
      break;
    if (i == 0)
      norm0 = norm;
    else if(norm/norm0 < newton_rtol)
      break;
648
  clock_get(&tic);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
649
    hxtLinearSystemSolve(problem->linear_system, rhs, dx);
650
651
  clock_get(&toc);
  totaltime += clock_diff(&tic, &toc);
652
653
654
655
    for (int j=0; j<mesh->n_nodes*n_fields; ++j) {
      problem->solution[j] -= dx[j];
    }
  }
656
  printf("total solve time : %g\n", totaltime);
657
  free(dx);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
658
  free(rhs);
659
  free(solution_old);
660
  return i;
661
662
}

Matthieu Constant's avatar
Matthieu Constant committed
663
664
665
666
667
668
669
static void fluid_problem_compute_node_volume(FluidProblem *problem) {
  free(problem->node_volume);
  Mesh *mesh = problem->mesh;
  problem->node_volume = malloc(sizeof(double)*mesh->n_nodes);
  for (int i = 0; i < problem->mesh->n_nodes; ++i){
    problem->node_volume[i] = 0;
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
670
  for (int iel = 0; iel < mesh->n_elements; ++iel) {
671
672
673
674
675
    double dxdxi[DIMENSION][DIMENSION];
    const int *el = &mesh->elements[iel*N_N];
    for (int i=0; i<DIMENSION; ++i)
      for (int j=0; j<DIMENSION; ++j)
        dxdxi[i][j] = mesh->x[el[j+1]*3+i]-mesh->x[el[0]*3+i];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
676
#if DIMENSION == 2
677
    const double vol = detDD(dxdxi)/6;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
678
#else
679
    const double vol = detDD(dxdxi)/24;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
680
#endif
681
682
    for (int i = 0; i < N_N; ++i)
      problem->node_volume[el[i]] += vol;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
683
684
685
686
687
688
689
690
691
692
693
694
  }
}

static void fluid_problem_compute_porosity(FluidProblem *problem)
{
  Mesh *mesh = problem->mesh;
  double *volume = problem->particle_volume;
  for (int i = 0; i < mesh->n_nodes; ++i){
    problem->porosity[i] = 0;
  }
  for (int i = 0; i < problem->n_particles; ++i) {
    if(problem->particle_element_id[i] == -1) continue;
695
696
    double sf[N_SF];
    shape_functions(&problem->particle_uvw[i*DIMENSION],sf);
697
    const uint32_t *el = &mesh->elements[problem->particle_element_id[i]*N_N];
698
699
    for (int j=0; j<N_N;++j)
      problem->porosity[el[j]] += sf[j]*volume[i];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
700
701
702
  }
  for (int i = 0; i < mesh->n_nodes; ++i){
    problem->porosity[i] = (1-problem->porosity[i]/problem->node_volume[i]);
Matthieu Constant's avatar
Matthieu Constant committed
703
704
705
  }
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
706
FluidProblem *fluid_problem_new(const char *mesh_file_name, double g, double mu, double rho, double epsilon, int n_strong_boundaries, StrongBoundary *strong_boundaries)
707
{
708
709
710
711
712
  static int initialized = 0;
  if (!initialized) {
    hxtInitializeLinearSystems(0, NULL);
    initialized = 1;
#ifdef HXT_HAVE_PETSC
Matthieu Constant's avatar
Matthieu Constant committed
713
    hxtPETScInsertOptions("-pc_type ilu", "fluid");
714
715
#endif
  }
716
  FluidProblem *problem = malloc(sizeof(FluidProblem));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
717
718
719
  Mesh *mesh = mesh_load_msh(mesh_file_name);
  if (!mesh)
    return NULL;
720
  problem->rho = rho;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
721
  problem->epsilon = epsilon;
722
  problem->mu = mu;
723
  problem->g = g;
724
  problem->mesh = mesh;
725
726
727
728
  problem->strong_boundaries = malloc(sizeof(StrongBoundary)*n_strong_boundaries);
  problem->n_strong_boundaries = n_strong_boundaries;
  for (int i = 0; i < n_strong_boundaries; ++i) {
    problem->strong_boundaries[i].tag = strdup(strong_boundaries[i].tag);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
729
    problem->strong_boundaries[i].apply = strong_boundaries[i].apply;
730
731
    problem->strong_boundaries[i].field = strong_boundaries[i].field;
  }
732
  problem->porosity = malloc(mesh->n_nodes*sizeof(double));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
733
734
  problem->node_force = malloc(mesh->n_nodes*DIMENSION*sizeof(double));
  for (int i = 0; i < mesh->n_nodes*DIMENSION; ++i) {
Matthieu Constant's avatar
Matthieu Constant committed
735
736
    problem->node_force[i] = 0.0;
  }
737
  problem->old_porosity = malloc(mesh->n_nodes*sizeof(double));
738
739
  problem->solution = malloc(mesh->n_nodes*n_fields*sizeof(double));
  // begin to remove
740
  for (int i = 0; i < problem->mesh->n_nodes*N_N; ++i){
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
741
    problem->solution[i] = 0.;
742
  }
Matthieu Constant's avatar
Matthieu Constant committed
743
744
  problem->node_volume = malloc(0);
  fluid_problem_compute_node_volume(problem);
745
  // end to remove
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
746
  problem->linear_system = NULL;
747
#ifdef HXT_HAVE_PETSC
Matthieu Constant's avatar
Matthieu Constant committed
748
    hxtLinearSystemCreatePETSc(&problem->linear_system,mesh->n_elements,N_N,n_fields,mesh->elements,"fluid");
749
#else
750
    hxtLinearSystemCreateLU(&problem->linear_system,mesh->n_elements,N_N,n_fields,mesh->elements);
751
#endif
752
753
754
755
756
757
758
  problem->n_particles = 0;
  problem->particle_uvw = malloc(0);
  problem->particle_element_id = malloc(0);
  problem->particle_position = malloc(0);
  problem->particle_mass = malloc(0);
  problem->particle_volume = malloc(0);
  problem->particle_velocity = malloc(0);
759
760
761
762
763
764
  return problem;
}

void fluid_problem_free(FluidProblem *problem)
{
  free(problem->solution);
Matthieu Constant's avatar
Matthieu Constant committed
765
  free(problem->node_force);
766
  free(problem->porosity);
767
  free(problem->old_porosity);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
768
  hxtLinearSystemDelete(&problem->linear_system);
769
770
771
772
773
774
775
776
777
  free(problem->particle_uvw);
  free(problem->particle_element_id);
  free(problem->particle_position);
  free(problem->particle_mass);
  free(problem->particle_volume);
  free(problem->particle_velocity);
  for (int i = 0; i < problem->n_strong_boundaries; ++i)
    free(problem->strong_boundaries[i].tag);
  free(problem->strong_boundaries);
778
779
780
  mesh_free(problem->mesh);
}

Matthieu Constant's avatar
Matthieu Constant committed
781
void fluid_problem_adapt_gen_mesh(FluidProblem *problem, double gradmin, double gradmax, double gradPmin, double gradPmax, double lcmin, double n_el)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
782
{
Matthieu Constant's avatar
Matthieu Constant committed
783
  double ngradM = 0.;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
784
785
786
  Mesh *mesh = problem->mesh;
  double *solution = problem->solution;
  double *new_mesh_size = malloc(sizeof(double)*mesh->n_nodes);
Matthieu Constant's avatar
Matthieu Constant committed
787
788
789
790
791
  double *num_lc = malloc(sizeof(double)*mesh->n_nodes);
  for (int i = 0; i < mesh->n_nodes; ++i){
    new_mesh_size[i] = 0.;
    num_lc[i] = 0.;
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
792
793
  gradmax = sqrt(gradmax);
  gradmin = sqrt(gradmin);
Matthieu Constant's avatar
Matthieu Constant committed
794
  double *porosity = problem->porosity;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
795
  for (int iel = 0; iel < mesh->n_elements; ++iel) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
796
    const unsigned int *el = &mesh->elements[iel*N_N];
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
    double C[N_N],U[DIMENSION][N_N], P[N_N];
    for (int i = 0; i < N_N; ++i){
      C[i] = porosity[el[i]];
      P[i] = solution[el[i]*n_fields+DIMENSION];
      for (int j=0; j<DIMENSION; ++j) {
        U[j][i] = solution[el[i]*n_fields+j];
      }
    }
    double dxdxi[DIMENSION][DIMENSION],dxidx[DIMENSION][DIMENSION];
    for (int i=0; i<DIMENSION; ++i)
      for (int j=0; j<DIMENSION; ++j)
        dxdxi[i][j] = mesh->x[el[j+1]*3+i]-mesh->x[el[0]*3+i];
    invDD(dxdxi, dxidx);
    double dphi[N_SF][DIMENSION];
    grad_shape_functions(dxidx, dphi);
    double ngrad2 = 0;
Matthieu Constant's avatar
Matthieu Constant committed
813
    for (int i=0; i<DIMENSION; ++i){
814
815
816
817
818
819
820
821
      for (int j=0; j<DIMENSION; ++j){
        double gradU = 0;
        for (int k=0; k<N_SF; ++k){
          gradU += dphi[k][j]*U[i][k]/C[k];
        }
        ngrad2 += gradU*gradU;
      }
    }
Matthieu Constant's avatar
Matthieu Constant committed
822
823
824
825
826
827
828
829
/*    double ngrad = pow(ngrad2, 0.25); */
/*    for (int j=0; j<DIMENSION; ++j){*/
/*      double gradP = 0;*/
/*      for (int k=0; k<N_SF; ++k){*/
/*        gradP += dphi[k][j]*P[k];*/
/*      }*/
/*      ngrad2 += gradP*gradP;*/
/*    }*/
Matthieu Constant's avatar
Matthieu Constant committed
830
    double ngrad = sqrt(ngrad2);
Matthieu Constant's avatar
Matthieu Constant committed
831
    double lc = lcmin /fmin(1,fmax(ngrad/(gradmax),gradmin/(gradmax)));
Matthieu Constant's avatar
Matthieu Constant committed
832
833
834
835
836
837
838
839
840
841
842
843
844
    
    ngrad = pow(ngrad2, 0.25); 
    for (int j=0; j<DIMENSION; ++j){
      double gradP = 0;
      for (int k=0; k<N_SF; ++k){
        gradP += dphi[k][j]*P[k];
      }
      ngrad2 += gradP*gradP;
    }
    ngrad = sqrt(ngrad2);
    lc = fmin(lcmin /fmin(1,fmax(ngrad/(gradPmax),gradPmin/(gradPmax))),lc);
    
    
Matthieu Constant's avatar
Matthieu Constant committed
845
846
847
848
849
    for (int j = 0; j < N_N; ++j){
      new_mesh_size[el[j]] += lc;
      num_lc[el[j]] += 1.;
    }
  }
Matthieu Constant's avatar
Matthieu Constant committed
850
  double nOfEl = 0.0;
Matthieu Constant's avatar
Matthieu Constant committed
851
#if DIMENSION==2
Matthieu Constant's avatar
Matthieu Constant committed
852
853
  for (int i = 0; i < mesh->n_nodes; ++i){
    new_mesh_size[i] /= num_lc[i];
Matthieu Constant's avatar
Matthieu Constant committed
854
855
856
857
858
859
860
    double nOfElByNode = 4*problem->node_volume[i]/(new_mesh_size[i]*new_mesh_size[i]*sqrt(3));
    nOfEl += nOfElByNode;
  }
  double ratioEl = n_el/nOfEl;
  for (int i = 0; i < mesh->n_nodes; ++i){
    new_mesh_size[i] = fmax(new_mesh_size[i]/sqrt(ratioEl),lcmin);
  }
Matthieu Constant's avatar
Matthieu Constant committed
861
#else
Matthieu Constant's avatar
Matthieu Constant committed
862
  for (int i = 0; i < mesh->n_nodes; ++i){
Matthieu Constant's avatar
Matthieu Constant committed
863
864
    new_mesh_size[i] /= num_lc[i];
    double nOfElByNode = 12*problem->node_volume[i]/(new_mesh_size[i]*new_mesh_size[i]*new_mesh_size[i]*sqrt(2));
Matthieu Constant's avatar
Matthieu Constant committed
865
    nOfEl += nOfElByNode;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
866
  }
Matthieu Constant's avatar
Matthieu Constant committed
867
868
869
870
871
  double ratioEl = n_el/nOfEl;
  for (int i = 0; i < mesh->n_nodes; ++i){
    new_mesh_size[i] = fmax(new_mesh_size[i]/cbrt(ratioEl),lcmin);
  }
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
872
  FILE *f = fopen("adapt/lc.pos", "w");
873
874
  if(!f)
    printf("cannot open file adapt/lc.pos for writing\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
875
  fprintf(f, "View \"lc\" {\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
876
  for (int iel = 0; iel < mesh->n_elements; ++iel) {
877
878
879
880
881
882
    unsigned int *el = problem->mesh->elements+iel*N_N;
    fprintf(f, DIMENSION == 2 ? "ST(" : "SS(");
    for (int i = 0; i < N_N; ++i)
      fprintf(f, "%.8g, %.8g, %.8g%s", mesh->x[el[i]*3+0], mesh->x[el[i]*3+1],mesh->x[el[i]*3+2], i != N_N-1 ? "," : "){");
    for (int i = 0; i < N_N; ++i)
      fprintf(f, "%.8g%s", new_mesh_size[el[i]],i != N_N-1?",":"};\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
883
  }
884
  fprintf(f,"};\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
885
886
  fclose(f);
  free(new_mesh_size);
Matthieu Constant's avatar
Matthieu Constant committed
887
  free(num_lc);
888
  system("gmsh -"xstr(DIMENSION)" adapt/mesh.geo");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
889
890
891
}


Matthieu Constant's avatar
Matthieu Constant committed
892
void fluid_problem_adapt_mesh(FluidProblem *problem, double gradmin, double gradmax, double gradPmin, double gradPmax, double lcmin, double n_el)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
893
{
Matthieu Constant's avatar
Matthieu Constant committed
894
  fluid_problem_adapt_gen_mesh(problem, gradmin, gradmax, gradPmin, gradPmax, lcmin, n_el);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
895
  Mesh *new_mesh = mesh_load_msh("adapt/mesh.msh");
896
  double *new_solution = malloc(sizeof(double)*new_mesh->n_nodes*N_N);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
897
  double *new_xi = malloc(sizeof(double)*new_mesh->n_nodes*DIMENSION);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
898
  int *new_eid = malloc(sizeof(int)*new_mesh->n_nodes);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
899
900
901
902
903
904
  for (int i = 0; i < new_mesh->n_nodes; ++i)
    new_eid[i] = -1;
  double *newx = malloc(sizeof(double)*DIMENSION*new_mesh->n_nodes);
  for (int i = 0;i < new_mesh->n_nodes;++i) 
    for (int k = 0; k < DIMENSION; ++k)
      newx[i*DIMENSION+k] = new_mesh->x[i*3+k];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
905
  mesh_particles_to_mesh(problem->mesh, new_mesh->n_nodes, newx, new_eid, new_xi);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
906
  for (int i = 0; i < new_mesh->n_nodes; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
907
908
909
910
911
    unsigned int *el = problem->mesh->elements+new_eid[i]*N_N;
    if(new_eid[i] < 0) {
      printf("new mesh point not found in old mesh\n");
      exit(1);
    }
912
913
914
915
916
    double phi[N_SF];
    shape_functions(new_xi+i*DIMENSION, phi);
    for (int j=0; j<n_fields; ++j) {
      new_solution[i*n_fields+j] = 0;
      for (int k = 0; k < N_SF; ++k)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
917
        new_solution[i*n_fields+j] += problem->solution[el[k]*n_fields+j]*phi[k];
918
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
919
920
921
922
923
924
925
926
927
928
  }
  free(new_eid);
  free(new_xi);
  free(newx);
  free(problem->solution);
  problem->solution = new_solution;
  free(problem->porosity);
  problem->porosity = malloc(sizeof(double)*new_mesh->n_nodes);
  free(problem->old_porosity);
  problem->old_porosity = malloc(sizeof(double)*new_mesh->n_nodes);
Matthieu Constant's avatar
Matthieu Constant committed
929
  free(problem->node_force);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
930
  problem->node_force = malloc(sizeof(double)*DIMENSION*new_mesh->n_nodes);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
931
932
  mesh_free(problem->mesh);
  problem->mesh = new_mesh;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
933
934
  for (int i = 0; i < problem->n_particles; ++i)
    problem->particle_element_id[i] = -1;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
935
  mesh_particles_to_mesh(problem->mesh, problem->n_particles, problem->particle_position, problem->particle_element_id, problem->particle_uvw);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
936
  fluid_problem_compute_node_volume(problem);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
937
  fluid_problem_compute_porosity(problem);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
938
939
  hxtLinearSystemDelete(&problem->linear_system);
  #ifdef HXT_HAVE_PETSC
Matthieu Constant's avatar
Matthieu Constant committed
940
    hxtLinearSystemCreatePETSc(&problem->linear_system,new_mesh->n_elements,N_N,n_fields,new_mesh->elements,"fluid");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
941
  #else
942
    hxtLinearSystemCreateLU(&problem->linear_system,new_mesh->n_elements,N_N,n_fields,new_mesh->elements);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
943
  #endif 
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
944
945
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
946
947

void fluid_problem_set_particles(FluidProblem *problem, int n, double *mass, double *volume, double *position, double *velocity, long int *elid)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
948
{
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
949
950
951
  
  struct timespec tic, toc;
  
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
952
953
954
955
956
957
958
959
  if(problem->n_particles != n) {
    problem->n_particles = n;
    free(problem->particle_uvw);
    free(problem->particle_element_id);
    free(problem->particle_position);
    free(problem->particle_mass);
    free(problem->particle_volume);
    free(problem->particle_velocity);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
960
    problem->particle_uvw = malloc(sizeof(double)*DIMENSION*n);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
961
    problem->particle_element_id = malloc(sizeof(int)*n);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
962
963
964
965
    for (int i = 0; i < n; ++i) {
      problem->particle_element_id[i]=-1;
    }
    problem->particle_position = malloc(sizeof(double)*DIMENSION*n);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
966
967
    problem->particle_mass = malloc(sizeof(double)*n);
    problem->particle_volume = malloc(sizeof(double)*n);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
968
969
970
971
972
973
    problem->particle_velocity = malloc(sizeof(double)*n*DIMENSION);
  }
  if (elid) {
    for (int i = 0; i < n; ++i) {
      problem->particle_element_id[i]=elid[i];
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
974
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
975
  clock_get(&tic);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
976
  mesh_particles_to_mesh(problem->mesh, n, position, problem->particle_element_id, problem->particle_uvw);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
977
978
  clock_get(&toc);
  printf("Time mesh_particles_to_mesh : %.6e \n",clock_diff(&tic,&toc));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
979
980
981
  for (int i = 0; i < n; ++i) {
    problem->particle_mass[i] = mass[i];
    problem->particle_volume[i] = volume[i];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
982
983
984
985
    for (int k = 0; k < DIMENSION; ++k) {
      problem->particle_position[i*DIMENSION+k] = position[i*DIMENSION+k];
      problem->particle_velocity[i*DIMENSION+k] = velocity[i*DIMENSION+k];
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
986
987
988
989
990
  }
  for (int i = 0; i < problem->mesh->n_nodes; ++i){
    problem->old_porosity[i] = problem->porosity[i];
  }
  fluid_problem_compute_porosity(problem);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
991
992
993
994
995
996
997
998
999
1000
}

int *fluid_problem_particle_element_id(FluidProblem *problem)
{
  return problem->particle_element_id;
}

int fluid_problem_n_particles(FluidProblem *problem)
{
  return problem->n_particles;
For faster browsing, not all history is shown. View entire blame