fluid_problem.c 82.8 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
/*
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
2
 * MigFlow - Copyright (C) <2010-2018>
3
4
 * <Universite catholique de Louvain (UCL), Belgium
 *  Universite de Montpellier, France>
Jonathan Lambrechts's avatar
Jonathan Lambrechts 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
8
 * Description and complete License: see LICENSE file.
 * 	
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
9
 * This program (MigFlow) is free software: 
10
 * 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
13
14
15
16
 * Public License as published by the Free Software Foundation, either version
 * 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Lesser General Public License for more details.
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
18
 * 
19
20
 * You should have received a copy of the GNU Lesser General Public License
 * 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
#define P  D
#define U 0
Margaux Boxho's avatar
wip    
Margaux Boxho committed
35
int global_count = 0;
36
int global_ind = 0;
Margaux Boxho's avatar
wip    
Margaux Boxho committed
37
38
#define D DIMENSION

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
39

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

42
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
43
44
45
46
47
48
49
50
51
52
53
54
55
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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;
#else        
    return detj/6;
#endif    
}

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
81
int fluid_problem_n_fields(const FluidProblem *p) {return D+1;}
82

83

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

Jonathan Lambrechts's avatar
tmp    
Jonathan Lambrechts committed
181
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 c, const double *dc, const double rho, const double mu, const double dt, int eid, const double *data, double *f00, double *f01)
182
183
184
185
186
{
  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
187
  double p = s[P];
188
  const int n_fields = fluid_problem_n_fields(problem);
Jonathan Lambrechts's avatar
tmp    
Jonathan Lambrechts committed
189
  double u[D], c_du_o_c[D][D], dp[D];
190
  double s_c = 0;
191
  double unold = 0;
192
  double unext = 0;
193
194
195
196
  double dcn = 0;
  for (int iD = 0; iD < D; ++iD) {
    u[iD] = s[U+iD];
    dp[iD] = ds[P*D+iD];
197
    unold += sold[U+iD]*n[iD];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
198
    if(vid>=0)
199
    unext += data[vid+iD]*n[iD];
200
201
    dcn += dc[iD]*n[iD];
    s_c += vid<0?0:(u[iD]-data[vid+iD])*n[iD];
Jonathan Lambrechts's avatar
tmp    
Jonathan Lambrechts committed
202
203
    for (int jD = 0; jD < D; ++jD)
      c_du_o_c[iD][jD] = ds[(U+iD)*D+jD] -u[iD]/c*dc[jD];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
204
  }
205
206
  double h = problem->element_size[eid];
  double sigma = (1+D)/(D*h)*mu*N_N;
207
208
209
  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];
210
  }
211
  if(wbnd->type == BND_OPEN) {
212
213
    double unext = 0;
    for (int i = 0; i < D; ++i) {
214
      unext += (vid<0?s[U+i]:data[vid+i])*n[i];
215
      f00[P*n_fields+U+i] += vid<0?n[i]:0;
216
    }
217
218
219
    f0[P] = unext;
  }
  for (int id = 0; id < D; ++id) {
Jonathan Lambrechts's avatar
tmp    
Jonathan Lambrechts committed
220
    f0[U+id] = (pid<0?0:data[pid]-p)*n[id] + (vid<0?0:sigma*(u[id]-data[vid+id]+s_c*n[id]));
221
222
223
    f00[(U+id)*n_fields+P] += (pid<0?0:-n[id]);
    f00[(U+id)*n_fields+U+id] += (vid<0?0:sigma);
    for (int jd = 0; jd <D; ++jd) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
224
225
226
227
228
      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];
229
    }
Matthieu Constant's avatar
Matthieu Constant committed
230
  }
231
232
233
234
235
236
237
238
239
240
  double unbnd = unold;
   if (wbnd->type == BND_WALL) unbnd = unold/2;
   else if(vid>0) unbnd = (unold+unext)/2;

   if (unbnd<0) {
     for (int id = 0; id < D; ++id) {
       f0[U+id] += unbnd*((vid<0 ? 0 : data[vid+id])-u[id])*rho/c;
       f00[(U+id)*n_fields+U+id] -= unbnd*rho/c;
     }
   }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
241
242
243
244
245
}

static double pow2(double a) {return a*a;}
static void compute_stab_parameters(FluidProblem *problem, double dt) {
  const Mesh *mesh = problem->mesh;
246

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
247
  problem->element_size = realloc(problem->element_size,sizeof(double)*mesh->n_elements);
248
249
250
  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
251
252
253
  double maxtaup = 0;
  double mintaup = DBL_MAX;
  const int n_fields = fluid_problem_n_fields(problem);
254
255
  double maxtaa = 0;
  double mintaa = DBL_MAX;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
256
257
  for (int iel = 0; iel < mesh->n_elements; ++iel) {
    double normu = {0.};
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
258
    const int *el = &mesh->elements[iel*N_N];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
259
    double a = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
260
261
    double amax = 0.5;
    double amin = 0.5;
Matthieu Constant's avatar
Matthieu Constant committed
262
    double c = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
263
264
    for (int i=0; i< N_N; ++i) {
      double normup = 0;
Matthieu Constant's avatar
Matthieu Constant committed
265
      c += problem->porosity[el[i]]/N_N;
266
      if(problem->n_fluids == 2){
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
267
268
269
        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
270
      }
271
      for(int j = 0; j < DIMENSION; ++j) normup += pow2(problem->solution[el[i]*n_fields+j]);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
272
273
      normu = fmax(normu,sqrt(normup));
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
274
275
    double dxidx[DIMENSION][DIMENSION];
    double detj = mesh_dxidx(mesh,iel,dxidx);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
276
#if DIMENSION == 2
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
277
    const double h = 2*sqrt(detj/(2*M_PI));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
278
#else
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
279
    const double h = 2*cbrt(detj/(8*M_PI));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
280
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
281
    problem->element_size[iel] = h;
Matthieu Constant's avatar
Matthieu Constant committed
282
    double rho, mu;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
283
284
285
    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
286
    double nu = mu/rho;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
287
    problem->taup[iel] = 1/sqrt(pow2(2/dt)+pow2(2*normu/h)+pow2(4*nu/pow2(h)));
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
288
    problem->tauc[iel] = h*normu*fmin(h*normu/(6*nu),0.5);
Jonathan Lambrechts's avatar
weak bc    
Jonathan Lambrechts committed
289
290
  }
}
291

292
293
294
295
296
297
298
299
300
301
302
303
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#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];
  double u[D], uold[D], dp[D],G[D]={0};
  for (int iD = 0; iD < D; ++iD) {
    u[iD] = sol[U+iD];
    uold[iD] = solold[U+iD];
    dp[iD] = dsol[P*D+iD];
  }
  G[1] = problem->g;
  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];
  double g = problem->g;
  if (problem->reduced_gravity){
    double rhop = mass/vol;
    g = g*(rhop-problem->rho[0])/rhop;
  }
  double gamma;
  
  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{
356
    gamma = particle_drag_coeff(Due,mu,rho,vol,c);
357
358
359
360
361
362
  }
  double gammastar = gamma/(1+dt/mass*gamma);
  for (int i = 0; i < D; ++i) {
    double upstar = up[i]+dt/mass*(contact[i]+mass*G[i]-vol*dp[i]);
    double drag = gammastar*(upstar-u[i]/c);
    problem->particle_force[ip*D+i] = -drag+G[i]*mass-vol*dp[i];
363
    f[U+i] = -drag-vol*dp[i];
364
365
  }
  *dfdu = gammastar/c;
366
  *dfddp = gammastar*dt/mass*vol-vol;
367
368
}

369
static void fluid_problem_f(const FluidProblem *problem, const double *sol, double *dsol, const double *solold, const double *dsolold, const double c, const double *dc, const double cold, const double rho, const double mu, double dt, int iel, double *f0, double *f1, double *f00, double *f10, double *f01, double *f11) {
370
  double p = sol[P];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
371
372
  double taup = problem->taup[iel];
  double tauc = problem->tauc[iel];
373
  const int n_fields = fluid_problem_n_fields(problem);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
374
375
376
  double u[D], uold[D], du[D][D], duold[D][D],dp[D];
  if(problem->stab_param != 0)
    taup = tauc = 0;
377
  double divu = 0;
378
379
380
  for (int iD = 0; iD < D; ++iD) {
    u[iD] = sol[U+iD];
    uold[iD] = solold[U+iD];
381
    dp[iD] = dsol[P*D+iD];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
382
383
384
385
    for (int jD = 0; jD < D; ++jD) {
      du[iD][jD] = dsol[(U+iD)*D+jD];
      duold[iD][jD] = dsolold[(U+iD)*D+jD];
    }
386
    divu += du[iD][iD];
387
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
388
389
  f0[P] = (c-cold)/dt;// + (sol[P]-solold[P])/dt*0.1;
  //f00[P*n_fields+P] = 1/dt*0.1;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
390
391
392
  double G[D] = {0};
  G[1] = -problem->g;
  double rhoreduced = (problem->reduced_gravity ? (rho-problem->rho[0]) : rho);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
393
  for (int i = 0; i < D; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
394
395
396
397
398
    f0[U+i] = 
      rho*(u[i]-uold[i])/dt 
      + dp[i] 
      + G[i]*rhoreduced*c
      + problem->volume_drag*u[i]*mu;
399
400
401
402
403
404
    f00[(U+i)*n_fields+U+i] = rho/dt + problem->volume_drag*mu;
    f01[(U+i)*n_fields*D+P*D+i] = 1;
    // 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) {
405
      f0[U+i] += rho/c*(uold[j]*du[i][j] + u[i]*(duold[j][j]-uold[j]/c*dc[j]));
406
      f00[(U+i)*n_fields+U+i] += rho/c*(duold[j][j]-uold[j]/c*dc[j]);
407
      f01[(U+i)*n_fields*D+(U+i)*D+j] += rho/c*uold[j];
408
409
    }
    for (int j = 0; j < D; ++j) {
410
      f1[(U+i)*D+j] = mu*(du[i][j]-u[i]*dc[j]/c+ du[j][i]-u[j]*dc[i]/c);
411
412
413
414
415
416
      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;
      f11[((U+i)*D+j)*n_fields*D+(U+j)*D+i] += mu; 
      f11[((U+i)*D+j)*n_fields*D+(U+i)*D+j] += mu;
      // SUPG
      double supg = uold[j]*taup;
417
      f1[(U+i)*D+j] += supg*f0[U+i];
418
419
420
421
422
      f10[((U+i)*D+j)*n_fields+U+i] += supg*f00[(U+i)*n_fields+U+i];
      f11[((U+i)*D+j)*(n_fields*D)+P*D+i] += supg*f01[(U+i)*n_fields*D+P*D+i]; 
      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];
    }
423
424
    double lsic = tauc*rho;
    f1[(U+i)*D+i] += ((c-cold)/dt+divu)*lsic;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
425
    for (int j = 0; j < D; ++j) {
426
      f11[((U+i)*D+i)*(n_fields*D)+(U+j)*D+j] += lsic;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
427
    }
428
429
430
    f1[P*D+i] = -u[i];
    f10[(P*D+i)*n_fields+U+i] += -1;
    // PSPG
Jonathan Lambrechts's avatar
pp    
Jonathan Lambrechts committed
431
432
433
434
    double pspg = taup/rho;
    f1[P*D+i] += pspg*f0[U+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->stab_param;
    f10[(P*D+i)*n_fields+U+i] += pspg*f00[(U+i)*n_fields+(U+i)];
435
    for (int j = 0; j < D; ++j) {
Jonathan Lambrechts's avatar
pp    
Jonathan Lambrechts committed
436
      f11[(P*D+i)*n_fields*D+(U+i)*D+j] = pspg*f01[(U+i)*n_fields*D+(U+i)*D+j];
437
    }
Matthieu Constant's avatar
Matthieu Constant committed
438
  }
439
440
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
441
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
442
443
444
{
  const Mesh *mesh = problem->mesh;
  const double *solution = problem->solution;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
445
446
447
448
449
  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
450
  double *dsold = malloc(sizeof(double)*n_fields*D);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
451
  double *f0 = malloc(sizeof(double)*n_fields);
452
  double *f00 = malloc(sizeof(double)*n_fields*n_fields);
Jonathan Lambrechts's avatar
tmp    
Jonathan Lambrechts committed
453
  double *f01 = malloc(sizeof(double)*n_fields*n_fields*D);
Matthieu Constant's avatar
Matthieu Constant committed
454
455
456
  double i_bnd = 0;
  double iv_bnd_up = 0;
  double iv_bnd_down = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
457
458
459
460
461
462
463
464
465
466
467
468
  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];
469
470
471
472
    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
473
474
475
476
    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
477
478
479
      const int *cl = elbnd[icl];
      int nodes[N_LSF];
      double *x[N_LSF];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
480
      const int *el = &mesh->elements[iel*N_N];
481
      for (int i = 0; i < D; ++i){
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
482
        nodes[i] = el[cl[i]];
483
484
        x[i] = &mesh->x[nodes[i]*3];
      }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
485
486
      double dxidx[D][D], dphi[N_N][D];
      mesh_dxidx(mesh,iel,dxidx);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
487
488
      double n[D],detbnd;
      get_normal_and_det(x,n,&detbnd);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
489
      grad_shape_functions(dxidx, dphi);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
490
491
492
493
494
495
496
      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
497
498
          double dofold = solution_old[el[i]*n_fields+j];
          for (int k = 0; k < D; ++k){
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
499
            ds[j*D+k] += dphi[i][k]*dof;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
500
501
            dsold[j*D+k] += dphi[i][k]*dofold;
          }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
502
503
504
505
506
        }
        for (int k=0; k<D; ++k){
          dc[k] += problem->porosity[el[i]]*dphi[i][k];
        }
      }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
507
      for (int iqp = 0; iqp < N_LQP; ++iqp) {
508
        double *dataqp = &data[n_value*(N_LQP*iinterface+iqp)];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
509
510
511
        double dp[D]={0};
        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
512
513
        double phi[DIMENSION];
        l_shape_functions(LQP[iqp],phi);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
514
515
516
        for (int i = 0; i < n_fields; ++i) {
          s[i] = 0;
          sold[i] = 0;
517
          f0[i] = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
518
        }
519
520
521
        for (int i = 0; i < n_fields*n_fields; ++i) {
          f00[i] = 0;
        }
Jonathan Lambrechts's avatar
tmp    
Jonathan Lambrechts committed
522
523
524
        for (int i = 0; i < n_fields*n_fields*D; ++i) {
          f01[i] = 0;
        }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
525
        double c = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
526
        double a = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
527
528
        for (int i = 0; i < DIMENSION; ++i) {
          c += problem->porosity[nodes[i]]*phi[i];
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
529
          if (problem->n_fluids == 2) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
530
            a += problem->concentration[iel*N_N+cl[i]]*phi[i];
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
531
          }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
532
          for (int j = 0; j < n_fields; ++j) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
533
            double dof = solution[nodes[i]*n_fields+j];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
534
            double dofold = solution_old[nodes[i]*n_fields+j];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
535
            s[j] += phi[i]*dof;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
536
            sold[j] += phi[i]*dofold;
Matthieu Constant's avatar
Matthieu Constant committed
537
          }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
538
        }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
539
540
        double rho, mu;
        fluid_problem_interpolate_rho_and_nu(problem,a, &rho, &mu);
541
        const double jw = LQW[iqp]*detbnd;
Matthieu Constant's avatar
Matthieu Constant committed
542
543
544
545
546
547
548
549
550
551
552
553
        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;
            }
          }
        }
Jonathan Lambrechts's avatar
tmp    
Jonathan Lambrechts committed
554
        f_boundary(wbnd,problem,n,f0,s,ds,sold,dsold,c,dc,rho,mu,dt,iel,dataqp,f00,f01);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
555
        for (int ifield = 0; ifield < n_fields; ++ifield) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
556
557
          for (int iphi = 0; iphi < D; ++iphi){
            local_vector[cl[iphi]+N_SF*ifield] += phi[iphi]*f0[ifield]*jw;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
558
559
560
561
          }
        }
        for (int jfield = 0; jfield < n_fields; ++jfield) {
          for (int ifield = 0; ifield < n_fields; ++ifield){
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
562
563
            for (int iphi = 0; iphi < N_LSF; ++iphi){
              for (int jphi = 0; jphi < N_LSF; ++jphi){
Jonathan Lambrechts's avatar
tmp    
Jonathan Lambrechts committed
564
565
566
567
568
569
570
571
572
                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
573
574
575
576
577
              }
            }
          }
        }
      }
Jonathan Lambrechts's avatar
weak bc    
Jonathan Lambrechts committed
578
    }
579
    free(data);
Jonathan Lambrechts's avatar
weak bc    
Jonathan Lambrechts committed
580
  }
581
582
583
  free(s);
  free(ds);
  free(sold);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
584
  free(dsold);
585
  free(f0);
586
  free(f00);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
587
}
Jonathan Lambrechts's avatar
weak bc    
Jonathan Lambrechts committed
588

Matthieu Constant's avatar
Matthieu Constant committed
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
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)
      for (int j = 0; j< N_N; ++j) 
        I_a += detj * problem->porosity[el[i]]*problem->concentration[iel*N_N+j]*mass_matrix[i][j];
  }
  return I_a;
}

604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
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
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
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
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);
      
      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
Jonathan Lambrechts committed
727
static void fluid_problem_volume(FluidProblem *problem, const double *solution_old, double dt, double *all_local_vector, double *all_local_matrix)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
728
729
{
  const Mesh *mesh = problem->mesh;
Matthieu Constant's avatar
Matthieu Constant committed
730
  const double *porosity = problem->porosity;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
731
732
733
734

  const double *solution = problem->solution;
  int n_fields = fluid_problem_n_fields(problem);
  size_t local_size = N_SF*n_fields;
735
736
737
  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
738
  double *dsold = malloc(sizeof(double)*n_fields*D);
739
740
  double *f0 = malloc(sizeof(double)*n_fields);
  double *f1 = malloc(sizeof(double)*n_fields*D);
741
742
743
744
  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
745
  problem->kinetic_energy = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
746
  for (int iel=0; iel < mesh->n_elements; ++iel) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
747
    const int *el = &mesh->elements[iel*N_N];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
748
749
    double dxidx[D][D], dphi[N_N][D];
    const double detj = mesh_dxidx(mesh,iel,dxidx);
750
    grad_shape_functions(dxidx, dphi);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
751
752
    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
753
754
755
    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
756
        dsold[i*D+j] = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
757
758
759
760
761
762
      }
    }
    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
763
764
        double dofold = solution_old[el[i]*n_fields+j];
        for (int k = 0; k < D; ++k) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
765
          ds[j*D+k] += dphi[i][k]*dof;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
766
767
          dsold[j*D+k] += dphi[i][k]*dofold;
        }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
768
769
770
771
772
773
774
775
776
777
      }
      for (int k=0; k<D; ++k){
        dc[k] += problem->porosity[el[i]]*dphi[i][k];
      }
      if (problem->n_fluids==2) {
        for (int k=0; k<D; ++k){
          da[k] += problem->concentration[iel*N_N+i]*dphi[i][k];
        }
      }
    }
778
779
780
    for (int iqp = 0; iqp< N_QP; ++iqp) {
      double phi[N_SF];
      shape_functions(QP[iqp],phi);
781
782
783
784
      for (int i = 0; i < n_fields; ++i) {
        s[i] = 0;
        sold[i] = 0;
      }
785
786
787
788
789
790
791
792
793
794
      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
795
796
      double c = 0, a = 0;
      double cold = 0, aold = 0;
797
      for (int i = 0; i < N_SF; ++i) {
Matthieu Constant's avatar
Matthieu Constant committed
798
799
        c += problem->porosity[el[i]]*phi[i];
        cold += problem->oldporosity[el[i]]*phi[i];
800
801
802
803
804
        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
805
        }
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
806
807
808
809
        if (problem->n_fluids==2) {
          a += problem->concentration[iel*N_N+i]*phi[i];
        }
      }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
810
811
812
      double mu;
      double rho;
      fluid_problem_interpolate_rho_and_nu(problem,a, &rho, &mu);
813
      const double jw = QW[iqp]*detj;
Matthieu Constant's avatar
Matthieu Constant committed
814
      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;
815

816
      fluid_problem_f(problem,s,ds,sold,dsold,c,dc,cold,rho,mu,dt,iel,f0,f1,f00,f10,f01,f11);
817
818
819
820
821
      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
822
823
          }
        }
824
825
826
827
828
      }
      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){
829
              double m = jw*phi[iphi]*phi[jphi]*f00[ifield*n_fields+jfield];
830
              for (int id = 0; id < D; ++id) {
831
                m += jw*dphi[iphi][id]*phi[jphi]*f10[(ifield*D+id)*n_fields+jfield];
832
                for (int jd = 0; jd < D; ++jd) {
833
                  m += jw*dphi[iphi][id]*dphi[jphi][jd]*f11[(ifield*D+id)*n_fields*D+jfield*D+jd];
834
835
                }
              }
836
              for (int jd = 0; jd < D; ++jd) {
837
                m += jw*phi[iphi]*dphi[jphi][jd]*f01[ifield*(n_fields*D)+jfield*D+jd];
Matthieu Constant's avatar
Matthieu Constant committed
838
              }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
839
              LOCAL_MATRIX(iphi,jphi,ifield,jfield) += m;
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
840
            }
Matthieu Constant's avatar
Matthieu Constant committed
841
          }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
842
843
844
        }
      }
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
845
846
847
  }
  free(s);
  free(ds);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
848
  free(dsold);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
849
850
851
  free(sold);
  free(f0);
  free(f1);
852
853
854
855
  free(f00);
  free(f01);
  free(f10);
  free(f11);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
856
857
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
858
static void fluid_problem_surface_tension_bnd(FluidProblem *problem, double *a_cg, double *all_local_vector){
859
  const Mesh *mesh = problem->mesh;
860
  double sigma = problem->sigma;
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
  const int n_fields = fluid_problem_n_fields(problem);
  const size_t local_size = N_SF*n_fields;
  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);
    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[D];
      double *x[D];
      const int *el = &mesh->elements[iel*N_N];
      for (int i = 0; i < D; ++i){
        nodes[i] = el[elbnd[icl][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);
      double da[D] = {0};
      double nda = 0;
      for (int k=0; k<D; ++k){
        for (int i = 0; i < N_SF; ++i) {
          da[k] += a_cg[el[i]]*dphi[i][k];
        }
        nda += da[k]*da[k];
      }
      nda = fmax(sqrt(nda),1e-8);
      for (int iqp = 0; iqp < N_LQP; ++iqp) {
        double *local_vector = &all_local_vector[local_size*iel];
        double phi[DIMENSION];
        l_shape_functions(LQP[iqp],phi);
        const double jw = LQW[iqp]*detbnd;
        double a = 0;
        double c = 0;
        for (int i = 0; i < D; ++i){
          a += a_cg[nodes[i]]*phi[i];
          c += problem->porosity[nodes[i]]*phi[i];
        }
        for (int iphi = 0; iphi < D; ++iphi) {
          for (int iD = 0; iD < D; ++iD) {
            for (int jD = 0; jD < D; ++jD){
              local_vector[cl[iphi]+N_SF*iD] += phi[iphi]*jw*c*da[iD]*da[jD]*n[jD]/nda*sigma;
            }
            local_vector[cl[iphi]+N_SF*iD] -= phi[iphi]*jw*c*nda*n[iD]*sigma;
          }
        }
      }
    }
  }
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
926
static void fluid_problem_surface_tension(FluidProblem *problem, double *a_cg, double *all_local_vector){
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
927
928
  if (problem->n_fluids == 1) return;
  const Mesh *mesh = problem->mesh;
929
  double sigma = problem->sigma;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
930
931
932
  int n_fields = fluid_problem_n_fields(problem);
  for (int iel = 0; iel < mesh->n_elements; ++iel) {
    const int *el = &mesh->elements[iel*N_N];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
933
934
935
    double dxidx[D][D], dphi[N_N][D];
    const double det = mesh_dxidx(mesh,iel,dxidx);
    const double vol = element_volume_from_detj(det);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
936
937
938
    grad_shape_functions(dxidx, dphi);
    double da[D] = {0};
    double nda = 0;
Matthieu Constant's avatar
Matthieu Constant committed
939
940
941
942
    double c = 0;
    for (int i = 0; i < N_N; ++i){
      c += problem->porosity[el[i]]/N_N;
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
943
944
    for (int k=0; k<D; ++k){
      for (int i = 0; i < N_SF; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
945
946
        //da[k] += a_cg[el[i]]*dphi[i][k];
        da[k] += problem->concentration[iel*N_N+i]*dphi[i][k];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
947
948
949
950
      }
      nda += da[k]*da[k];
    }
    nda = fmax(sqrt(nda),1e-8);
951
    double *local_vector = &all_local_vector[iel*n_fields*N_N];
952
953
954
    for (int iphi = 0; iphi < N_N; ++iphi) {
      for (int id = 0; id < D; ++id) {
        for (int jd = 0; jd < D; ++jd) {
Matthieu Constant's avatar
Matthieu Constant committed
955
          local_vector[(U+id)*N_N+iphi] -= c*vol*da[id]*da[jd]*dphi[iphi][jd]/nda*sigma;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
956
        }
Matthieu Constant's avatar
Matthieu Constant committed
957
        local_vector[(U+id)*N_N+iphi] += c*vol*nda*dphi[iphi][id]*sigma;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
958
959
960
      }
    }
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
}


static void fluid_problem_dg_to_cg(FluidProblem *problem, const double *dg, double *cg) {
  const Mesh *mesh = problem->mesh;
  for (int i = 0; i< mesh->n_nodes; ++i) {
    cg[i] = 0;
  }
  for (int iel = 0; iel < mesh->n_elements; ++iel) {
    const int *el = &mesh->elements[iel*N_N];
    double dxidx[D][D];
    const double det = mesh_dxidx(mesh,iel,dxidx);
    const double vol = node_volume_from_detj(det);
    for (int i = 0; i< N_N; ++i) {
      cg[el[i]] += vol * dg[iel*N_N+i];
    }
  }
  for (int i = 0; i< mesh->n_nodes; ++i) {
    cg[i] /= problem->node_volume[i];
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
981
982
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
983
static void fluid_problem_assemble_system(FluidProblem *problem, double *rhs, const double *solution_old, double dt)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
{
  HXTLinearSystem *lsys = problem->linear_system;
  const Mesh *mesh = problem->mesh;

  int n_fields = fluid_problem_n_fields(problem);
  
  size_t local_size = N_SF*n_fields;
  size_t N_E = mesh->n_elements;
  double *all_local_vector = (double*) malloc(N_E*local_size*sizeof(double));
  double *all_local_matrix = (double*) malloc(N_E*local_size*local_size*sizeof(double));
  for (size_t i = 0; i < N_E*local_size; ++i)
    all_local_vector[i] = 0;
  for (size_t i = 0; i < N_E*local_size*local_size; ++i)
    all_local_matrix[i] = 0;

  char *forced_row = malloc(sizeof(char)*n_fields*mesh->n_nodes);
  for (int i = 0; i < n_fields*mesh->n_nodes; ++i)
    forced_row[i] = -1;
  for (int ibnd = 0; ibnd < problem->n_strong_boundaries; ++ibnd) {
    const StrongBoundary *bnd = problem->strong_boundaries + ibnd;
    int iphys;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1005
1006
    for (iphys = 0; iphys < mesh->n_boundaries; ++iphys) {
      if (strcmp(bnd->tag, mesh->boundary_names[iphys]) == 0)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1007
1008
        break;
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1009
    if (iphys == mesh->n_boundaries){
1010
<