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

24
#include "tools.h"
25
26
#include <stdlib.h>
#include <stdio.h>
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
27
#include <float.h>
28
29
#include <math.h>
#include "fluid_problem.h"
30
#include "mesh_find.h"
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
31
#include "fluid_problem_fem.h"
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
32

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
33
34
35
#define P  D
#define U 0

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
36
37
#define LOCAL_MATRIX(i,j,a,b) local_matrix[((i)*n_fields*N_SF+N_SF*(a)+(j))*n_fields+(b)]

38
39
40
41
42
43
44
static MeshTree *get_mesh_tree(FluidProblem *problem)
{
  if (problem->mesh_tree == NULL)
    problem->mesh_tree = mesh_tree_create(problem->mesh);
  return problem->mesh_tree;
}

45
static void particle_force_f(FluidProblem *problem, double *f, double *dfdu, double *dfddp, double *sol, double *dsol, const double *solold, const double c, const double *dc, double a, double dt, int iel, int ip);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
46
47
48
49
50
51
52
53
54
55
56
57
58
void fluid_problem_interpolate_rho_and_nu(const FluidProblem *problem, double a, double *rho, double *mu) {
  if (problem->n_fluids == 1){
    *rho = problem->rho[0];
    *mu = problem->mu[0];
  }
  else{
    //*rho = 1/(1/problem->rho[0]*a + 1/problem->rho[1]*(1-a));
    *rho = problem->rho[0]*a + problem->rho[1]*(1-a);
    //*mu = problem->mu[0]*a + problem->mu[1]*(1-a);
    *mu = (problem->mu[0]/problem->rho[0]*a + problem->mu[1]/problem->rho[1]*(1-a))*(*rho);
  }
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
59
60
61
62
63
64
65
66
67
68
69
70
inline static double mesh_dxidx(const Mesh *mesh, int iel, double dxidx[D][D]) {
  double dxdxi[D][D];
  const int *el = &mesh->elements[iel*N_N];
  for (int i = 0; i < D; ++i)
    for (int j = 0; j < D; ++j)
      dxdxi[i][j] = mesh->x[el[j+1]*3+i] - mesh->x[el[0]*3+i];
  return invDD(dxdxi, dxidx);
}

inline static double element_volume_from_detj(double detj) {
#if D == 2
    return detj/2;
Michel Henry's avatar
test    
Michel Henry committed
71
#else
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
72
    return detj/6;
Michel Henry's avatar
test    
Michel Henry committed
73
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
74
75
76
77
78
79
80
81
82
83
}

inline static double node_volume_from_detj(double detj) {
#if DIMENSION == 2
    return detj/6;
#else
    return detj/24;
#endif
}

Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
84
int fluid_problem_n_fields(const FluidProblem *p) {return D+1;}
85

86

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
87
void fluid_problem_node_force_volume(FluidProblem *problem, const double *solution_old, double dt, double *all_local_vector, double *all_local_matrix)
88
89
90
91
{
  const Mesh *mesh = problem->mesh;
  const double *porosity = problem->porosity;
  const double *solution = problem->solution;
92
  int drag_in_stab = problem->drag_in_stab;
93
94
95
96
97
98
99
  int n_fields = fluid_problem_n_fields(problem);
  size_t local_size = N_SF*n_fields;
  double *s = malloc(sizeof(double)*n_fields);
  double *sold = malloc(sizeof(double)*n_fields);
  double *ds = malloc(sizeof(double)*n_fields*D);
  for (int ip=0; ip < problem->n_particles; ++ip) {
    int iel = problem->particle_element_id[ip];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
100
    if(iel < 0){
101
      for (int d = 0; d < D; ++d)
102
        problem->particle_force[ip*D+d] = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
103
104
      continue;
    }
105
    double *xi = problem->particle_uvw + D*ip;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
106
107
    double phi[N_SF];
    shape_functions(xi,phi);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
108
    const int *el = &mesh->elements[iel*N_N];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
109
110
    double dxidx[D][D], dphi[N_SF][D];
    double detj = mesh_dxidx(mesh, iel, dxidx);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
111
    grad_shape_functions(dxidx, dphi);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
112
113
    double *local_vector = all_local_vector ? &all_local_vector[local_size*iel] : NULL;
    double *local_matrix = all_local_matrix ? &all_local_matrix[local_size*local_size*iel] : NULL;
114
115
116
117
118
    for (int i = 0; i < n_fields; ++i) {
      s[i] = 0;
      sold[i] = 0;
      for (int j = 0; j < D; ++j) {
        ds[i*D+j] = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
119
120
      }
    }
121
    double c = 0;
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
122
    double a = 0;
Nathan Coppin's avatar
Nathan Coppin committed
123
    double cold = 0;
124
125
126
    double dc[D] = {0};
    for (int i = 0; i < N_SF; ++i) {
      c += problem->porosity[el[i]]*phi[i];
Nathan Coppin's avatar
Nathan Coppin committed
127
      cold += problem->oldporosity[el[i]]*phi[i];
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
128
129
130
      if (problem->n_fluids == 2) {
        a += problem->concentration[iel*N_N+i]*phi[i];
      }
131
132
133
134
135
136
137
138
139
140
141
      for (int j = 0; j < n_fields; ++j) {
        double dof = solution[el[i]*n_fields+j];
        double dofold = solution_old[el[i]*n_fields+j];
        s[j] += phi[i]*dof;
        sold[j] += phi[i]*dofold;
        for (int k = 0; k < D; ++k)
          ds[j*D+k] += dphi[i][k]*dof;
      }
      for (int k=0; k<D; ++k){
        dc[k] += problem->porosity[el[i]]*dphi[i][k];
      }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
142
    }
143
    double f[D],dfdu,dfddp;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
144
    particle_force_f(problem,f,&dfdu,&dfddp,s,ds,sold,c,dc,a,dt,iel,ip);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
145
146
    if (!local_vector)
      continue;
147
148
    double rho,nu;
    fluid_problem_interpolate_rho_and_nu(problem,a, &rho,&nu);
149
    double pspg = drag_in_stab?problem->taup[iel]/rho:0;
150
    for (int iD = 0; iD < D; ++iD) {
151
      for (int iphi = 0; iphi < N_SF; ++iphi){
152
        local_vector[iphi+N_SF*(U+iD)] += phi[iphi]*f[iD];
153
154
        local_vector[iphi+N_SF*P] += pspg*dphi[iphi][iD]*f[iD];
        for (int jD = 0; jD < D; ++jD) {
155
          double supg = drag_in_stab?sold[U+jD]*problem->taup[iel]:0;
156
157
          local_vector[iphi+N_SF*(U+iD)] += supg*dphi[iphi][jD]*f[iD];
        }
158
      }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
159
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
160
161
    if (!local_matrix)
      continue;
162
163
164
165
166
    for (int iD = 0; iD < D; ++iD){
      for (int iphi = 0; iphi < N_SF; ++iphi){
        for (int jphi = 0; jphi < N_SF; ++jphi){
          LOCAL_MATRIX(iphi,jphi,U+iD,U+iD) += phi[iphi]*phi[jphi]*dfdu;
          LOCAL_MATRIX(iphi,jphi,U+iD,P) += phi[iphi]*dphi[jphi][iD]*dfddp;
167
168
169
          LOCAL_MATRIX(iphi,jphi,P,U+iD) += pspg*dphi[iphi][iD]*phi[jphi]*dfdu;
          LOCAL_MATRIX(iphi,jphi,P,P) += pspg*dphi[iphi][iD]*dphi[jphi][iD]*dfddp;
          for (int jD = 0; jD < D; ++jD) {
170
            double supg = drag_in_stab?sold[U+jD]*problem->taup[iel]:0;
171
172
173
            LOCAL_MATRIX(iphi,jphi,U+iD,U+iD) += supg*dphi[iphi][jD]*phi[jphi]*dfdu;
            LOCAL_MATRIX(iphi,jphi,U+iD,P) += supg*dphi[iphi][jD]*dphi[jphi][iD]*dfddp;
          }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
174
        }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
175
176
      }
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
177
  }
178
179
180
  free(s);
  free(ds);
  free(sold);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
181
182
}

Michel Henry's avatar
ALE    
Michel Henry committed
183
static void f_boundary(WeakBoundary *wbnd, FluidProblem *problem,const double *n, double *f0,const double *s,const double *ds, const double *sold, const double *dsold, const double *mesh_velocity, const double c, const double *dc, const double rho, double mu, const double dt, int eid, const double *data, double *f00, double *f01)
184
185
186
187
188
{
  const int vid = wbnd->vid;
  const int pid = wbnd->pid;
  const int cid = wbnd->cid;
  const int aid = wbnd->aid;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
189
  double p = s[P];
190
  const int n_fields = fluid_problem_n_fields(problem);
Michel Henry's avatar
Michel Henry committed
191
  double u[D], dp[D], uext[D];
192
  double s_c = 0;
193
  double unold = 0;
Michel Henry's avatar
Michel Henry committed
194
  double cext = cid<0?c:data[cid];
195
  double unext = 0;
Michel Henry's avatar
ALE    
Michel Henry committed
196
  double unmesh = 0;
197
198
199
200
  double dcn = 0;
  for (int iD = 0; iD < D; ++iD) {
    u[iD] = s[U+iD];
    dp[iD] = ds[P*D+iD];
201
    unold += sold[U+iD]*n[iD];
Michel Henry's avatar
ALE    
Michel Henry committed
202
    unmesh += mesh_velocity[iD]*n[iD];
Jonathan Lambrechts's avatar
usolid    
Jonathan Lambrechts committed
203
    uext[iD] = (vid<0?s[U+iD]:data[vid+iD]*c);
Michel Henry's avatar
Michel Henry committed
204
    unext += uext[iD]*n[iD];
205
    dcn += dc[iD]*n[iD];
Michel Henry's avatar
Michel Henry committed
206
    s_c += (u[iD]-uext[iD])*n[iD];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
207
  }
208
  double h = problem->element_size[eid];
209
210
211
  if (wbnd->type == BND_WALL && pid >= 0) {
      f0[P] = -(s[P]-data[pid])/h*problem->taup[eid];
      f00[P*n_fields+P] += -1/h*problem->taup[eid];
212
  }
213
  if(wbnd->type == BND_OPEN) {
214
    f0[P] = unext;
215
    for (int i = 0; i < D; ++i) {
216
      f00[P*n_fields+U+i] += vid<0?n[i]:0;
217
    }
218
219
  }
  for (int id = 0; id < D; ++id) {
220
    f0[U+id] = c*(pid<0?0:data[pid]-p)*n[id];
Jonathan Lambrechts's avatar
cdP    
Jonathan Lambrechts committed
221
    f00[(U+id)*n_fields+P] += c*(pid<0?0:-n[id]);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
222
223
  }
  double c_du_o_c[D][D];
Matthieu Constant's avatar
Matthieu Constant committed
224
  double sigma = problem->ip_factor*(1+D)/(D*h)*mu*N_N;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
225
226
227
228
  for (int iD = 0; iD < D; ++iD) {
    for (int jD = 0; jD < D; ++jD)
      c_du_o_c[iD][jD] = ds[(U+iD)*D+jD] -u[iD]/c*dc[jD];
  }
229
  if(wbnd->compute_viscous_term == 1){
Michel Henry's avatar
Michel Henry committed
230
    for (int id = 0; id < D; ++id) {
Michel Henry's avatar
Michel Henry committed
231
      f0[U+id] += sigma*(u[id]-uext[id]+s_c*n[id]);
Michel Henry's avatar
Michel Henry committed
232
233
234
235
236
237
238
239
      f00[(U+id)*n_fields+U+id] += (vid<0?0:sigma);
      for (int jd = 0; jd <D; ++jd) {
        f0[U+id] -= mu*(c_du_o_c[id][jd]+c_du_o_c[jd][id])*n[jd];
        f00[(U+id)*n_fields+U+id] += (vid<0?0:n[id]*n[jd]*sigma) + mu/c*dc[jd]*n[jd];
        f00[(U+id)*n_fields+U+jd] += mu/c*dc[id]*n[jd];
        f01[(U+id)*n_fields*D+(U+id)*D+jd] -= mu*n[jd];
        f01[(U+id)*n_fields*D+(U+jd)*D+id] -= mu*n[jd];
      }
240
241
242
243
244
245
    }
  }
  if (problem->advection) {
    double unbnd = unold;
    if (wbnd->type == BND_WALL) unbnd = unold/2;
    else if(vid>0) unbnd = (unold+unext)/2;
Michel Henry's avatar
ALE    
Michel Henry committed
246
    if (unbnd - unmesh < 0) {
247
      for (int id = 0; id < D; ++id) {
Michel Henry's avatar
Michel Henry committed
248
        f0[U+id] += ((unbnd - unmesh)*(vid<0?0:uext[id])-(unold - unmesh)*u[id])*rho/c;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
249
        f00[(U+id)*n_fields+U+id] -= (unold-unmesh)*rho/c;
250
      }
251
    }
Matthieu Constant's avatar
Matthieu Constant committed
252
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
253
254
255
}

static double pow2(double a) {return a*a;}
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
256
void fluid_problem_compute_stab_parameters(FluidProblem *problem, double dt) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
257
  const Mesh *mesh = problem->mesh;
258

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
259
  problem->element_size = realloc(problem->element_size,sizeof(double)*mesh->n_elements);
260
261
262
  problem->taup = realloc(problem->taup,sizeof(double)*mesh->n_elements);
  problem->tauc = realloc(problem->tauc,sizeof(double)*mesh->n_elements);

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
263
264
265
  double maxtaup = 0;
  double mintaup = DBL_MAX;
  const int n_fields = fluid_problem_n_fields(problem);
266
267
  double maxtaa = 0;
  double mintaa = DBL_MAX;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
268
269
  for (int iel = 0; iel < mesh->n_elements; ++iel) {
    double normu = {0.};
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
270
    const int *el = &mesh->elements[iel*N_N];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
271
    double a = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
272
273
    double amax = 0.5;
    double amin = 0.5;
Matthieu Constant's avatar
Matthieu Constant committed
274
    double c = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
275
276
    for (int i=0; i< N_N; ++i) {
      double normup = 0;
Matthieu Constant's avatar
Matthieu Constant committed
277
      c += problem->porosity[el[i]]/N_N;
278
      if(problem->n_fluids == 2){
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
279
280
281
        a += problem->concentration[iel*N_N+i];
        amax = fmax(amax,problem->concentration[iel*N_N+i]);
        amin = fmin(amin,problem->concentration[iel*N_N+i]);
Matthieu Constant's avatar
Matthieu Constant committed
282
      }
283
      for(int j = 0; j < DIMENSION; ++j) normup += pow2(problem->solution[el[i]*n_fields+j]);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
284
285
      normu = fmax(normu,sqrt(normup));
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
286
287
    double dxidx[DIMENSION][DIMENSION];
    double detj = mesh_dxidx(mesh,iel,dxidx);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
288
#if DIMENSION == 2
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
289
    const double h = 2*sqrt(detj/(2*M_PI));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
290
#else
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
291
    const double h = 2*cbrt(detj/(8*M_PI));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
292
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
293
    problem->element_size[iel] = h;
Matthieu Constant's avatar
Matthieu Constant committed
294
    double rho, mu;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
295
296
297
    a /= N_N;
    a = fmin(1.,fmax(0.,a));
    fluid_problem_interpolate_rho_and_nu(problem,a, &rho, &mu);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
298
    double nu = mu/rho;
299
300
301
302
303
    problem->taup[iel] = 1/sqrt(
        (problem->temporal?pow2(2/dt):0.)
        +(problem->advection ?pow2(2*normu/h):0.)
        +pow2(4*nu/pow2(h)));
    problem->tauc[iel] = problem->advection?h*normu*fmin(h*normu/(6*nu),0.5):0.;
Jonathan Lambrechts's avatar
weak bc    
Jonathan Lambrechts committed
304
305
  }
}
306

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#if D==2
static double particle_drag_coeff(double u[2], double mu, double rho, double vol, double c)
{
  double d = 2*sqrt(vol/M_PI);
  double normu = hypot(u[0],u[1]);
  //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-0.65*exp(-(1.5-log(Re_O_u*normu))*(1.5-log(Re_O_u*normu))/2.)));
  return f*Cd_u*rho/2*d;
}
#else
static double particle_drag_coeff(double u[3], double mu, double rho, double vol, double c)
{
  double d = 2*cbrt(3./4.*vol/M_PI);
  double normu = sqrt(u[0]*u[0]+u[1]*u[1]+u[2]*u[2]);
  //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.;
  return f*Cd_u*rho/2*(M_PI*r*r);
}
#endif

void fluid_problem_compute_node_particle_force(FluidProblem *problem, double dt, double *particle_force) {
  for (int i = 0; i < problem->n_particles*D; ++i) {
    particle_force[i] = problem->particle_force[i];
  }
}

static void particle_force_f(FluidProblem *problem, double *f, double *dfdu, double *dfddp, double *sol, double *dsol, const double *solold, const double c, const double *dc, double a, double dt, int iel, int ip) {
  const double *contact = &problem->particle_contact[ip*D];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
342
  double u[D], uold[D], dp[D];
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  for (int iD = 0; iD < D; ++iD) {
    u[iD] = sol[U+iD];
    uold[iD] = solold[U+iD];
    dp[iD] = dsol[P*D+iD];
  }
  double mu, rho;
  fluid_problem_interpolate_rho_and_nu(problem,a, &rho, &mu);
  double Due[D];
  const double *up = &problem->particle_velocity[ip*D];
  for (int j = 0; j < D; ++j) {
    Due[j] = problem->particle_velocity[ip*D+j]-uold[j]/c;
  }
  double mass = problem->particle_mass[ip];
  double vol = problem->particle_volume[ip];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
357
358
359
360
  double rhop = mass/vol;
  double massreduced = mass;
  if(problem->reduced_gravity) {
    massreduced = (rhop-problem->rho[0])*problem->particle_volume[ip];
361
362
  }
  double gamma;
Michel Henry's avatar
test    
Michel Henry committed
363

364
365
366
367
368
369
  if(problem->n_fluids == 2){
    double gamma0 = particle_drag_coeff(Due,problem->mu[0],problem->rho[0],vol,c);
    double gamma1 = particle_drag_coeff(Due,problem->mu[1],problem->rho[1],vol,c);
    gamma = gamma0*a + gamma1*(1-a);
  }
  else{
370
    gamma = particle_drag_coeff(Due,mu,rho,vol,c);
371
  }
Matthieu Constant's avatar
Matthieu Constant committed
372
373
  gamma = gamma*problem->drag_coeff_factor;
  double gammastar = gamma/(1+dt/mass*gamma);
374
  for (int i = 0; i < D; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
375
    double upstar = up[i]+dt/mass*(contact[i]+massreduced*problem->g[i]-vol*dp[i]);
376
    double drag = gammastar*(upstar-u[i]/c);
377
    problem->particle_force[ip*D+i] = -drag-vol*dp[i];
Jonathan Lambrechts's avatar
cdP    
Jonathan Lambrechts committed
378
    f[U+i] = -drag;//-vol*dp[i];
379
380
  }
  *dfdu = gammastar/c;
Jonathan Lambrechts's avatar
cdP    
Jonathan Lambrechts committed
381
  *dfddp = gammastar*dt/mass*vol;//-vol;
382
383
}

Jonathan Lambrechts's avatar
usolid    
Jonathan Lambrechts committed
384
static void fluid_problem_f(const FluidProblem *problem, const double *sol, double *dsol, const double *solold, const double *dsolold, const double* mesh_velocity, const double c, const double *dc, const double *bf, const double cold, const double dus[D][D], const double rho, const double mu, double dt, int iel, double *f0, double *f1, double *f00, double *f10, double *f01, double *f11) {
385
  double p = sol[P];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
386
387
  double taup = problem->taup[iel];
  double tauc = problem->tauc[iel];
388
  const int n_fields = fluid_problem_n_fields(problem);
Michel Henry's avatar
ALE    
Michel Henry committed
389
  double u[D], uold[D], du[D][D], duold[D][D], umesh[D], dp[D];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
390
391
  if(problem->stab_param != 0)
    taup = tauc = 0;
392
  double divu = 0;
Jonathan Lambrechts's avatar
usolid    
Jonathan Lambrechts committed
393
  double divus = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
394
  double nold = 0;
395
396
397
  for (int iD = 0; iD < D; ++iD) {
    u[iD] = sol[U+iD];
    uold[iD] = solold[U+iD];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
398
    nold += uold[iD]*uold[iD];
Michel Henry's avatar
ALE    
Michel Henry committed
399
    umesh[iD] = mesh_velocity[iD];
400
    dp[iD] = dsol[P*D+iD];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
401
402
403
404
    for (int jD = 0; jD < D; ++jD) {
      du[iD][jD] = dsol[(U+iD)*D+jD];
      duold[iD][jD] = dsolold[(U+iD)*D+jD];
    }
405
    divu += du[iD][iD];
406
  }
Jonathan Lambrechts's avatar
usolid    
Jonathan Lambrechts committed
407
408
409
410
411
412
413
  if (problem->usolid) {
    for (int iD = 0; iD < D; ++iD)
      divus += dus[iD][iD];
  }
  else {
    divus = (c-cold)/dt;
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
414
  nold = sqrt(nold);
Jonathan Lambrechts's avatar
usolid    
Jonathan Lambrechts committed
415
  f0[P] = divus;
Matthieu Constant's avatar
Matthieu Constant committed
416
  double *g = problem->g;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
417
  double rhoreduced = (problem->reduced_gravity ? (rho-problem->rho[0]) : rho);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
418
  double drag = problem->volume_drag + problem->quadratic_drag*nold;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
419
  for (int i = 0; i < D; ++i) {
Michel Henry's avatar
test    
Michel Henry committed
420
    f0[U+i] =
421
      + c*(dp[i]- g[i]*rhoreduced- bf[i])
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
422
423
424
      + drag*u[i];
      //+ 5.3e5*u[i];
    f00[(U+i)*n_fields+U+i] = drag;//5.3e5;
425
426
427
428
    if(problem->temporal) {
      f0[U+i] += rho*(u[i]-uold[i])/dt;
      f00[(U+i)*n_fields+U+i] += rho/dt;
    }
Jonathan Lambrechts's avatar
cdP    
Jonathan Lambrechts committed
429
    f01[(U+i)*n_fields*D+P*D+i] = c;
430
431
432
433
434
435
    if (problem->advection) {
      // advection :
      // dj(uj ui /c) = uj/c dj(ui) + ui/c dj(uj) - uj ui /(c*c) dj(c)
      //              = ui/c * (dj(uj)- uj/c*dj(c)) + uj/c*dj(ui)
      for (int j = 0; j < D; ++j) {
        f0[U+i] += rho/c*(uold[j]*du[i][j] + u[i]*(duold[j][j]-uold[j]/c*dc[j]));
Michel Henry's avatar
ALE    
Michel Henry committed
436
        f0[U+i] -= rho/c*(umesh[j]*du[i][j]);
437
438
        f00[(U+i)*n_fields+U+i] += rho/c*(duold[j][j]-uold[j]/c*dc[j]);
        f01[(U+i)*n_fields*D+(U+i)*D+j] += rho/c*uold[j];
Michel Henry's avatar
ALE    
Michel Henry committed
439
        f01[(U+i)*n_fields*D+(U+i)*D+j] -= rho/c*umesh[j];
440
      }
441
442
    }
    for (int j = 0; j < D; ++j) {
443
      f1[(U+i)*D+j] = mu*(du[i][j]-u[i]*dc[j]/c+ du[j][i]-u[j]*dc[i]/c);
444
445
      f10[((U+i)*D+j)*n_fields+U+j] += -mu*dc[i]/c;
      f10[((U+i)*D+j)*n_fields+U+i] += -mu*dc[j]/c;
Michel Henry's avatar
test    
Michel Henry committed
446
      f11[((U+i)*D+j)*n_fields*D+(U+j)*D+i] += mu;
447
448
449
      f11[((U+i)*D+j)*n_fields*D+(U+i)*D+j] += mu;
      // SUPG
      double supg = uold[j]*taup;
450
      f1[(U+i)*D+j] += supg*f0[U+i];
451
      f10[((U+i)*D+j)*n_fields+U+i] += supg*f00[(U+i)*n_fields+U+i];
Michel Henry's avatar
test    
Michel Henry committed
452
      f11[((U+i)*D+j)*(n_fields*D)+P*D+i] += supg*f01[(U+i)*n_fields*D+P*D+i];
453
454
455
      for (int k = 0; k < D; ++k)
        f11[((U+i)*D+j)*n_fields*D+(U+i)*D+k] += supg*f01[(U+i)*n_fields*D+(U+i)*D+k];
    }
456
    double lsic = tauc*rho;
Jonathan Lambrechts's avatar
usolid    
Jonathan Lambrechts committed
457
    f1[(U+i)*D+i] += (divus+divu)*lsic;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
458
    for (int j = 0; j < D; ++j) {
459
      f11[((U+i)*D+i)*(n_fields*D)+(U+j)*D+j] += lsic;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
460
    }
461
462
463
    f1[P*D+i] = -u[i];
    f10[(P*D+i)*n_fields+U+i] += -1;
    // PSPG
Jonathan Lambrechts's avatar
pp    
Jonathan Lambrechts committed
464
    double pspg = taup/rho;
465
466
    f1[P*D+i] += pspg*(f0[U+i]+(problem->drag_in_stab?0:(1-c)*dp[i])) + problem->stab_param*dp[i];
    f11[(P*D+i)*(n_fields*D)+P*D+i] += pspg*(f01[(U+i)*n_fields*D+P*D+i]+(problem->drag_in_stab?0:1-c))+problem->stab_param;
Jonathan Lambrechts's avatar
pp    
Jonathan Lambrechts committed
467
    f10[(P*D+i)*n_fields+U+i] += pspg*f00[(U+i)*n_fields+(U+i)];
468
    for (int j = 0; j < D; ++j) {
Jonathan Lambrechts's avatar
pp    
Jonathan Lambrechts committed
469
      f11[(P*D+i)*n_fields*D+(U+i)*D+j] = pspg*f01[(U+i)*n_fields*D+(U+i)*D+j];
470
    }
Matthieu Constant's avatar
Matthieu Constant committed
471
  }
472
473
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
474
static void compute_weak_boundary_conditions(FluidProblem *problem, const double *solution_old, double dt, double *all_local_vector, double *all_local_matrix)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
475
476
477
{
  const Mesh *mesh = problem->mesh;
  const double *solution = problem->solution;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
478
479
480
481
482
  const int n_fields = fluid_problem_n_fields(problem);
  const size_t local_size = N_SF*n_fields;
  double *s = malloc(sizeof(double)*n_fields);
  double *sold = malloc(sizeof(double)*n_fields);
  double *ds = malloc(sizeof(double)*n_fields*D);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
483
  double *dsold = malloc(sizeof(double)*n_fields*D);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
484
  double *f0 = malloc(sizeof(double)*n_fields);
485
  double *f00 = malloc(sizeof(double)*n_fields*n_fields);
Jonathan Lambrechts's avatar
tmp    
Jonathan Lambrechts committed
486
  double *f01 = malloc(sizeof(double)*n_fields*n_fields*D);
Matthieu Constant's avatar
Matthieu Constant committed
487
488
489
  double i_bnd = 0;
  double iv_bnd_up = 0;
  double iv_bnd_down = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
490
491
492
493
494
495
496
497
498
499
500
501
  for (int ibnd = 0; ibnd < problem->n_weak_boundaries; ++ibnd){
    WeakBoundary *wbnd = problem->weak_boundaries + ibnd;
    int bndid= -1;
    for (int i = 0; i < mesh->n_boundaries; ++i) {
      if (strcmp(mesh->boundary_names[i],wbnd->tag) == 0){
        bndid = i;
      }
    }
    if (bndid == -1) {
      printf("Mesh has no boundary with name \"%s\".", wbnd->tag);
    }
    MeshBoundary *bnd = &problem->boundaries[bndid];
502
503
504
505
    int n_value = weak_boundary_n_values(wbnd);
    double *data = malloc(n_value*bnd->n_interfaces*N_LQP*sizeof(double));
    weak_boundary_values(mesh, bnd, wbnd, data);

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
506
507
508
509
    for (int iinterface = 0; iinterface < bnd->n_interfaces; ++iinterface) {
      const int *interface = &mesh->interfaces[bnd->interfaces[iinterface]*4];
      const int iel = interface[0];
      const int icl = interface[1];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
510
511
512
      const int *cl = elbnd[icl];
      int nodes[N_LSF];
      double *x[N_LSF];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
513
      const int *el = &mesh->elements[iel*N_N];
514
      for (int i = 0; i < D; ++i){
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
515
        nodes[i] = el[cl[i]];
516
517
        x[i] = &mesh->x[nodes[i]*3];
      }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
518
519
      double dxidx[D][D], dphi[N_N][D];
      mesh_dxidx(mesh,iel,dxidx);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
520
521
      double n[D],detbnd;
      get_normal_and_det(x,n,&detbnd);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
522
      grad_shape_functions(dxidx, dphi);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
523
524
525
526
527
528
529
      double dc[D] = {0};
      for (int j = 0; j < n_fields*D; ++j) {
        ds[j] = 0;
      }
      for (int i = 0; i < N_SF; ++i) {
        for (int j = 0; j < n_fields; ++j) {
          double dof = solution[el[i]*n_fields+j];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
530
531
          double dofold = solution_old[el[i]*n_fields+j];
          for (int k = 0; k < D; ++k){
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
532
            ds[j*D+k] += dphi[i][k]*dof;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
533
534
            dsold[j*D+k] += dphi[i][k]*dofold;
          }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
535
536
537
538
539
        }
        for (int k=0; k<D; ++k){
          dc[k] += problem->porosity[el[i]]*dphi[i][k];
        }
      }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
540
      for (int iqp = 0; iqp < N_LQP; ++iqp) {
541
        double *dataqp = &data[n_value*(N_LQP*iinterface+iqp)];
Michel Henry's avatar
ALE    
Michel Henry committed
542
543
        double dp[D] = {0};
        double umesh[D] = {0};
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
544
545
        double *local_vector = &all_local_vector[local_size*iel];
        double *local_matrix = &all_local_matrix[local_size*local_size*iel];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
546
547
        double phi[DIMENSION];
        l_shape_functions(LQP[iqp],phi);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
548
549
550
        for (int i = 0; i < n_fields; ++i) {
          s[i] = 0;
          sold[i] = 0;
551
          f0[i] = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
552
        }
553
554
555
        for (int i = 0; i < n_fields*n_fields; ++i) {
          f00[i] = 0;
        }
Jonathan Lambrechts's avatar
tmp    
Jonathan Lambrechts committed
556
557
558
        for (int i = 0; i < n_fields*n_fields*D; ++i) {
          f01[i] = 0;
        }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
559
        double c = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
560
        double a = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
561
562
        for (int i = 0; i < DIMENSION; ++i) {
          c += problem->porosity[nodes[i]]*phi[i];
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
563
          if (problem->n_fluids == 2) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
564
            a += problem->concentration[iel*N_N+cl[i]]*phi[i];
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
565
          }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
566
          for (int j = 0; j < n_fields; ++j) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
567
            double dof = solution[nodes[i]*n_fields+j];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
568
            double dofold = solution_old[nodes[i]*n_fields+j];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
569
            s[j] += phi[i]*dof;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
570
            sold[j] += phi[i]*dofold;
Matthieu Constant's avatar
Matthieu Constant committed
571
          }
Michel Henry's avatar
ALE    
Michel Henry committed
572
573
574
575
          for(int j = 0; j < D; ++j){
            double dofmesh = problem->mesh_velocity[nodes[i]*D+j];
            umesh[j] += phi[i]*dofmesh;
          }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
576
        }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
577
578
        double rho, mu;
        fluid_problem_interpolate_rho_and_nu(problem,a, &rho, &mu);
579
        const double jw = LQW[iqp]*detbnd;
Matthieu Constant's avatar
Matthieu Constant committed
580
581
582
583
584
585
586
587
588
589
590
591
        if (wbnd->type != BND_WALL){
          for (int i = 0; i < D; ++i){
            if (wbnd->vid<0) {
              i_bnd -= a*s[U+i]*n[i]*jw*dt;
              iv_bnd_up -= s[U+i]*n[i]*jw;
            }
            else {
              iv_bnd_down -= dataqp[wbnd->vid+i]*n[i]*jw;
              i_bnd -= a*dataqp[wbnd->vid+i]*n[i]*jw*dt;
            }
          }
        }
Michel Henry's avatar
ALE    
Michel Henry committed
592
        f_boundary(wbnd,problem,n,f0,s,ds,sold,dsold,umesh,c,dc,rho,mu,dt,iel,dataqp,f00,f01);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
593
        for (int ifield = 0; ifield < n_fields; ++ifield) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
594
          for (int iphi = 0; iphi < D; ++iphi){
595
596
597
            if (ifield<D){
              problem->boundary_force[bndid*D + ifield] += phi[iphi]*f0[ifield]*jw;
            }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
598
            local_vector[cl[iphi]+N_SF*ifield] += phi[iphi]*f0[ifield]*jw;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
599
600
601
602
          }
        }
        for (int jfield = 0; jfield < n_fields; ++jfield) {
          for (int ifield = 0; ifield < n_fields; ++ifield){
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
603
604
            for (int iphi = 0; iphi < N_LSF; ++iphi){
              for (int jphi = 0; jphi < N_LSF; ++jphi){
Jonathan Lambrechts's avatar
tmp    
Jonathan Lambrechts committed
605
606
607
608
609
610
611
612
613
                double d = phi[jphi]*f00[ifield*n_fields+jfield];
                LOCAL_MATRIX(cl[iphi],cl[jphi],ifield,jfield) += jw*phi[iphi]*d;
              }
              for (int jphi = 0; jphi < N_SF; ++jphi){
                double d = 0;
                for (int jD = 0; jD < D; ++jD) {
                  d += dphi[jphi][jD]*f01[ifield*n_fields*D+jfield*D+jD];
                }
                LOCAL_MATRIX(cl[iphi],jphi,ifield,jfield) += jw*phi[iphi]*d;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
614
615
616
617
618
              }
            }
          }
        }
      }
Jonathan Lambrechts's avatar
weak bc    
Jonathan Lambrechts committed
619
    }
620
    free(data);
Jonathan Lambrechts's avatar
weak bc    
Jonathan Lambrechts committed
621
  }
622
623
624
  free(s);
  free(ds);
  free(sold);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
625
  free(dsold);
626
  free(f0);
627
  free(f00);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
628
}
Jonathan Lambrechts's avatar
weak bc    
Jonathan Lambrechts committed
629

Matthieu Constant's avatar
Matthieu Constant committed
630
631
632
633
634
635
636
637
638
double fluid_problem_a_integ_volume(FluidProblem *problem)
{
  const Mesh *mesh = problem->mesh;
  double I_a = 0;
  for (int iel=0; iel < mesh->n_elements; ++iel) {
    const int *el = &mesh->elements[iel*N_N];
    double dxidx[D][D];
    const double detj = mesh_dxidx(mesh,iel,dxidx);
    for (int i = 0; i < N_N; ++i)
Michel Henry's avatar
test    
Michel Henry committed
639
      for (int j = 0; j< N_N; ++j)
Matthieu Constant's avatar
Matthieu Constant committed
640
641
642
643
644
        I_a += detj * problem->porosity[el[i]]*problem->concentration[iel*N_N+j]*mass_matrix[i][j];
  }
  return I_a;
}

645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
double fluid_problem_volume_flux(FluidProblem *problem, const char *tagname)
{
  const Mesh *mesh = problem->mesh;
  const double *solution = problem->solution;
  double Q = 0;
  double wtotal = 0;
  int n_fields = D+1;
  int found = 0;
  for (int i = 0; i < N_LQP; ++i) wtotal += LQW[i];
  for (int ibnd = 0; ibnd < mesh->n_boundaries; ++ibnd) {
    if (strcmp(mesh->boundary_names[ibnd],tagname) != 0)
      continue;
    found = 1;
    MeshBoundary *bnd = &problem->boundaries[ibnd];
    for (int iinterface = 0; iinterface < bnd->n_interfaces; ++iinterface) {
      const int *interface = &mesh->interfaces[bnd->interfaces[iinterface]*4];
      const int iel = interface[0];
      const int icl = interface[1];
      const int *cl = elbnd[icl];
      int nodes[N_LSF];
      double *x[N_LSF];
      const int *el = &mesh->elements[iel*N_N];
      for (int i = 0; i < D; ++i){
        nodes[i] = el[cl[i]];
        x[i] = &mesh->x[nodes[i]*3];
      }
      double dxidx[D][D], dphi[N_N][D];
      mesh_dxidx(mesh,iel,dxidx);
      double n[D],detbnd;
      get_normal_and_det(x,n,&detbnd);
      double vnmean = 0;
      for (int i = 0; i < D; ++i) {
        for (int iD = 0; iD < D; ++iD) {
          vnmean += n[iD]*problem->solution[nodes[i]*n_fields+U+iD];
        }
      }
      Q += vnmean*detbnd*wtotal/D;
    }
  }
  if(found == 0) {
    printf("boundary '%s' not found\n", tagname);
    exit(1);
  }
  printf("Q = %g wtotal = %g\n", Q,wtotal);
  return Q;
}

Matthieu Constant's avatar
Matthieu Constant committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
double fluid_problem_a_integ_bnd(FluidProblem *problem, double dt)
{
  const Mesh *mesh = problem->mesh;
  const double *solution = problem->solution;
  const int n_fields = fluid_problem_n_fields(problem);
  const size_t local_size = N_SF*n_fields;
  double *s = malloc(sizeof(double)*n_fields);
  double i_bnd = 0;
  for (int ibnd = 0; ibnd < problem->n_weak_boundaries; ++ibnd){
    WeakBoundary *wbnd = problem->weak_boundaries + ibnd;
    int bndid= -1;
    for (int i = 0; i < mesh->n_boundaries; ++i) {
      if (strcmp(mesh->boundary_names[i],wbnd->tag) == 0){
        bndid = i;
      }
    }
    if (bndid == -1) {
      printf("Mesh has no boundary with name \"%s\".", wbnd->tag);
    }
    MeshBoundary *bnd = &problem->boundaries[bndid];
    int n_value = weak_boundary_n_values(wbnd);
    double *data = malloc(n_value*bnd->n_interfaces*N_LQP*sizeof(double));
    weak_boundary_values(mesh, bnd, wbnd, data);

    for (int iinterface = 0; iinterface < bnd->n_interfaces; ++iinterface) {
      const int *interface = &mesh->interfaces[bnd->interfaces[iinterface]*4];
      const int iel = interface[0];
      const int icl = interface[1];
      const int *cl = elbnd[icl];
      int nodes[N_LSF];
      double *x[N_LSF];
      const int *el = &mesh->elements[iel*N_N];
      for (int i = 0; i < D; ++i){
        nodes[i] = el[cl[i]];
        x[i] = &mesh->x[nodes[i]*3];
      }
      double dxidx[D][D], dphi[N_N][D];
      mesh_dxidx(mesh,iel,dxidx);
      double n[D],detbnd;
      get_normal_and_det(x,n,&detbnd);
      grad_shape_functions(dxidx, dphi);
Michel Henry's avatar
test    
Michel Henry committed
733

Matthieu Constant's avatar
Matthieu Constant committed
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
      for (int iqp = 0; iqp < N_LQP; ++iqp) {
        double *dataqp = &data[n_value*(N_LQP*iinterface+iqp)];
        double phi[DIMENSION];
        l_shape_functions(LQP[iqp],phi);
        for (int i = 0; i < n_fields; ++i) {
          s[i] = 0;
        }
        double a = 0;
        for (int i = 0; i < DIMENSION; ++i) {
          if (problem->n_fluids == 2) {
            a += problem->concentration[iel*N_N+cl[i]]*phi[i];
          }
          for (int j = 0; j < n_fields; ++j) {
            double dof = solution[nodes[i]*n_fields+j];
            s[j] += phi[i]*dof;
          }
        }
        const double jw = LQW[iqp]*detbnd;
        if (wbnd->type != BND_WALL){
          for (int i = 0; i < D; ++i){
            if (wbnd->vid<0) {
              i_bnd -= a*s[U+i]*n[i]*jw*dt;
            }
            else {
              i_bnd -= a*dataqp[wbnd->vid+i]*n[i]*jw*dt;
            }
          }
        }
      }
    }
  }
  return i_bnd;
}

Jonathan Lambrechts's avatar
usolid    
Jonathan Lambrechts committed
768
static void fluid_problem_volume(FluidProblem *problem, const double *solution_old, const double *u_solid, double dt, double *all_local_vector, double *all_local_matrix)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
769
770
{
  const Mesh *mesh = problem->mesh;
Matthieu Constant's avatar
Matthieu Constant committed
771
  const double *porosity = problem->porosity;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
772
773
774
775

  const double *solution = problem->solution;
  int n_fields = fluid_problem_n_fields(problem);
  size_t local_size = N_SF*n_fields;
776
777
778
  double *s = malloc(sizeof(double)*n_fields);
  double *sold = malloc(sizeof(double)*n_fields);
  double *ds = malloc(sizeof(double)*n_fields*D);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
779
  double *dsold = malloc(sizeof(double)*n_fields*D);
Michel Henry's avatar
ALE    
Michel Henry committed
780
  double *mesh_velocity = malloc(sizeof(double)*D);
781
782
  double *f0 = malloc(sizeof(double)*n_fields);
  double *f1 = malloc(sizeof(double)*n_fields*D);
783
784
785
786
  double *f00 = malloc(sizeof(double)*n_fields*n_fields);
  double *f10 = malloc(sizeof(double)*n_fields*n_fields*D);
  double *f01 = malloc(sizeof(double)*n_fields*n_fields*D);
  double *f11 = malloc(sizeof(double)*n_fields*n_fields*D*D);
Matthieu Constant's avatar
Matthieu Constant committed
787
  problem->kinetic_energy = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
788
  for (int iel=0; iel < mesh->n_elements; ++iel) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
789
    const int *el = &mesh->elements[iel*N_N];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
790
    double dxidx[D][D], dphi[N_N][D];
Jonathan Lambrechts's avatar
usolid    
Jonathan Lambrechts committed
791
    double dus[D][D] = {0};
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
792
    const double detj = mesh_dxidx(mesh,iel,dxidx);
793
    grad_shape_functions(dxidx, dphi);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
794
795
    double *local_vector = &all_local_vector[local_size*iel];
    double *local_matrix = &all_local_matrix[local_size*local_size*iel];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
796
797
798
    for (int i = 0; i < n_fields; ++i) {
      for (int j = 0; j < D; ++j) {
        ds[i*D+j] = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
799
        dsold[i*D+j] = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
800
801
802
803
804
805
      }
    }
    double dc[D] = {0}, da[D]={0};
    for (int i = 0; i < N_SF; ++i) {
      for (int j = 0; j < n_fields; ++j) {
        double dof = solution[el[i]*n_fields+j];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
806
807
        double dofold = solution_old[el[i]*n_fields+j];
        for (int k = 0; k < D; ++k) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
808
          ds[j*D+k] += dphi[i][k]*dof;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
809
810
          dsold[j*D+k] += dphi[i][k]*dofold;
        }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
811
812
813
      }
      for (int k=0; k<D; ++k){
        dc[k] += problem->porosity[el[i]]*dphi[i][k];
Jonathan Lambrechts's avatar
usolid    
Jonathan Lambrechts committed
814
815
816
        for (int j = 0; j < D; ++j) {
          dus[j][k] += u_solid[el[i]*DIMENSION+j]*dphi[i][k];
        }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
817
818
819
820
821
822
823
      }
      if (problem->n_fluids==2) {
        for (int k=0; k<D; ++k){
          da[k] += problem->concentration[iel*N_N+i]*dphi[i][k];
        }
      }
    }
824
825
826
    for (int iqp = 0; iqp< N_QP; ++iqp) {
      double phi[N_SF];
      shape_functions(QP[iqp],phi);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
827
828
829
      double bf[D];
      for (int j = 0; j < D; ++j) {
        bf[j] = 0;
Michel Henry's avatar
ALE    
Michel Henry committed
830
        mesh_velocity[j] = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
831
      }
832
833
834
835
      for (int i = 0; i < n_fields; ++i) {
        s[i] = 0;
        sold[i] = 0;
      }
836
837
838
839
840
841
842
843
844
845
      for (int i = 0; i < n_fields*n_fields; ++i) {
        f00[i] = 0;
      }
      for (int i = 0; i < D*n_fields*n_fields; ++i) {
        f10[i] = 0;
        f01[i] = 0;
      }
      for (int i = 0; i < D*D*n_fields*n_fields; ++i) {
        f11[i] = 0;
      }
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
846
      double c = 0, a = 0;
Nathan Coppin's avatar
Nathan Coppin committed
847
      double cold = 0, aold = 0;
848
      for (int i = 0; i < N_SF; ++i) {
Matthieu Constant's avatar
Matthieu Constant committed
849
        c += problem->porosity[el[i]]*phi[i];
Nathan Coppin's avatar
Nathan Coppin committed
850
        cold += problem->oldporosity[el[i]]*phi[i];
851
852
853
854
855
        for (int j = 0; j < n_fields; ++j) {
          double dof = solution[el[i]*n_fields+j];
          double dofold = solution_old[el[i]*n_fields+j];
          s[j] += phi[i]*dof;
          sold[j] += phi[i]*dofold;
Matthieu Constant's avatar
Matthieu Constant committed
856
        }
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
857
858
859
        if (problem->n_fluids==2) {
          a += problem->concentration[iel*N_N+i]*phi[i];
        }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
860
        for (int j=0; j<D; ++j) {
Michel Henry's avatar
ALE    
Michel Henry committed
861
          mesh_velocity[j] += phi[i]*problem->mesh_velocity[el[i]*D+j];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
862
863
          bf[j] += phi[i]*problem->bulk_force[el[i]*D+j];
        }
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
864
      }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
865
866
867
      double mu;
      double rho;
      fluid_problem_interpolate_rho_and_nu(problem,a, &rho, &mu);
868
      const double jw = QW[iqp]*detj;
Matthieu Constant's avatar
Matthieu Constant committed
869
      problem->kinetic_energy += rho*sqrt(s[U]*s[U]+s[U+1]*s[U+1])*sqrt(s[U]*s[U]+s[U+1]*s[U+1])/2*jw;
870

Jonathan Lambrechts's avatar
usolid    
Jonathan Lambrechts committed
871
      fluid_problem_f(problem,s,ds,sold,dsold,mesh_velocity,c,dc,bf,cold,dus,rho,mu,dt,iel,f0,f1,f00,f10,f01,f11);
872
873
874
875
876
      for (int ifield = 0; ifield < n_fields; ++ifield) {
        for (int iphi = 0; iphi < N_SF; ++iphi){
          local_vector[iphi+N_SF*ifield] += phi[iphi]*f0[ifield]*jw;
          for (int id = 0; id < D; ++id) {
            local_vector[iphi+N_SF*ifield] += dphi[iphi][id]*f1[ifield*D+id]*jw;
Matthieu Constant's avatar
Matthieu Constant committed
877
878
          }
        }
879
880
881
882
883
      }
      for (int jfield = 0; jfield < n_fields; ++jfield) {
        for (int ifield = 0; ifield < n_fields; ++ifield){
          for (int iphi = 0; iphi < N_SF; ++iphi){
            for (int jphi = 0; jphi < N_SF; ++jphi){
884
              double m = jw*phi[iphi]*phi[jphi]*f00[ifield*n_fields+jfield];
885
              for (int id = 0; id < D; ++id) {
886
                m += jw*dphi[iphi][id]*phi[jphi]*f10[(ifield*D+id)*n_fields+jfield];
887
                for (int jd = 0; jd < D; ++jd) {
888
                  m += jw*dphi[iphi][id]*dphi[jphi][jd]*f11[(ifield*D+id)*n_fields*D+jfield*D+jd];
889
890
                }
              }
891
              for (int jd = 0; jd < D; ++jd) {
Jonathan Lambrechts's avatar