Source

PetIGA / demo / TwoPhaseTwoComponent.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
  This code solves multiphase, multicomponent flow in porous media
  (two phase, two component). Thanks to Bilal Saad for his
  contribution.

  keywords: transient, vector, implicit, nonlinear, dimension
  independent, boundary integrals
 */

#include "petiga.h"

typedef struct {
  PetscScalar rholw,porosity,Pr,n,Slr,Sgr,H,Mh,T,mul,mug,Mw,Dlh,k;
} AppCtx;

typedef struct {
  PetscScalar Pl,rholh;
} Field;

PetscReal SEC_PER_YEAR = 365.0*24.0*3600.0;

#undef __FUNCT__
#define __FUNCT__ "EquationOfState"
void EquationOfState(PetscInt dim,PetscScalar Pl,PetscScalar Pl_t,PetscScalar rholh,PetscScalar rholh_t,PetscScalar *rholh_x,
		     PetscScalar *Sl,PetscScalar *Sl_t,PetscScalar *krl,PetscScalar *krg,
		     PetscScalar *rhogh,PetscScalar *rhogh_t,PetscScalar *Pg_x,AppCtx *user)
{
  // VanGenuchten, Henry
  PetscScalar R     = 8.314e-5; // [J/(K-mol)]
  PetscScalar Pr    = user->Pr;
  PetscScalar n     = user->n, m = 1.-1./n;
  PetscScalar Slr   = user->Slr;
  PetscScalar Sgr   = user->Sgr;
  PetscScalar H     = user->H;
  PetscScalar Mh    = user->Mh;
  PetscScalar T     = user->T;
  PetscScalar Pc    = rholh/(H*Mh)-Pl;
  PetscScalar Pc_t  = rholh_t/(H*Mh)-Pl_t;
  PetscScalar Sle   = 1;
  PetscScalar Sle_t = 0;
  if(rholh/H/Mh-Pl > 0.0) {
    Sle   = pow(pow((Pc/Pr),n)+1.,-m);
    Sle_t = -m*n/Pr*pow(pow((Pc/Pr),n)+1.,-m-1.)*pow(Pc/Pr,n-1.)*Pc_t;
  }
  *Sl      = (1.-Slr-Sgr)*Sle+Slr;
  *Sl_t    = (1.-Slr-Sgr)*Sle_t;
  *krl     = sqrt(Sle)*pow(1.-pow(1.-pow(Sle,1./m),m),2);
  *krg     = sqrt(1.-Sle)*pow(1.-pow(Sle,1./m),2*m);
  *rhogh_t = rholh_t/(R*T*H);
  *rhogh   = rholh  /(R*T*H); 
  PetscInt i;
  for(i=0;i<dim;i++) Pg_x[i] = rholh_x[i]/Mh*H; 
  return;
}

#undef  __FUNCT__
#define __FUNCT__ "Residual"
PetscErrorCode Residual(IGAPoint p,PetscReal dt,
			PetscReal shift,const PetscScalar *V,
			PetscReal t,const PetscScalar *U,
			PetscScalar *R,void *ctx)
{
  AppCtx *user = (AppCtx *)ctx;
  PetscScalar rholw = user->rholw; 
  PetscScalar porosity = user->porosity; 
  PetscScalar mul = user->mul;
  PetscScalar mug = user->mug;
  PetscScalar Mh  = user->Mh;
  PetscScalar Mw  = user->Mw;
  PetscScalar Dlh = user->Dlh;
  PetscScalar k   = user->k;

  PetscInt a,i,nen,dim;
  IGAPointGetSizes(p,&nen,0,0);
  IGAPointGetDims(p,&dim,NULL,NULL);

  PetscScalar sol_t[2],sol[2],sol_x[2][dim];
  IGAPointFormValue(p,V,&sol_t[0]);
  IGAPointFormValue(p,U,&sol[0]);
  IGAPointFormGrad (p,U,&sol_x[0][0]);

  const PetscReal *N0,(*N1)[dim];
  IGAPointGetShapeFuns(p,0,(const PetscReal**)&N0);
  IGAPointGetShapeFuns(p,1,(const PetscReal**)&N1);

  PetscScalar  Pl      = sol[0];    // liquid pressure
  PetscScalar  Pl_t    = sol_t[0];    
  PetscScalar *Pl_x    = sol_x[0];    
  PetscScalar  rholh   = sol[1];    // dissolved hydrogen
  PetscScalar  rholh_t = sol_t[1]; 
  PetscScalar *rholh_x = sol_x[1];

  PetscScalar Sl,Sl_t,krl,krg,rhogh,rhogh_t,Pg_x[dim];
  EquationOfState(dim,Pl,Pl_t,rholh,rholh_t,rholh_x,&Sl,&Sl_t,&krl,&krg,&rhogh,&rhogh_t,Pg_x,user);
  PetscScalar Sg = 1.-Sl, Sg_t = -Sl_t;
  PetscScalar cl = Sl*(rholh/Mh+rholw/Mw);
  
  PetscScalar den = rholh/Mh+rholw/Mw;
  PetscScalar jlw[dim],ql[dim],qg[dim],g=0;
  for(i=0;i<dim;i++) {
    if(i==dim-1) g = 0.;
    PetscScalar X_x = (rholh_x[i]/Mh)/den - (rholh/Mh)/(den*den)*(rholh_x[i]/Mh);
    jlw[i] = porosity*Mh*cl*Dlh*X_x;
    ql[i]  = -k*krl/mul*(Pl_x[i]-rholw*g);
    qg[i]  = -k*krg/mug*(Pg_x[i]);
  }

  PetscScalar (*Re)[2] = (PetscScalar (*)[2])R;
  for (a=0; a<nen; a++) {
    PetscScalar gN_dot_ql = 0,gN_dot_qg = 0,gN_dot_jlw = 0;
    for(i=0;i<dim;i++){
      gN_dot_ql += N1[a][i]*ql[i];
      gN_dot_qg += N1[a][i]*qg[i];
      gN_dot_jlw += N1[a][i]*jlw[i];
    }
    Re[a][0]  = porosity*N0[a]*Sl_t*rholw-(rholw*gN_dot_ql + gN_dot_jlw);
    Re[a][1]  = porosity*N0[a]*(Sl_t*rholh+Sl*rholh_t+Sg_t*rhogh+Sg*rhogh_t);
    Re[a][1] -= (rholh*gN_dot_ql + rhogh*gN_dot_qg - gN_dot_jlw);
  }
  return 0;
}

#undef  __FUNCT__
#define __FUNCT__ "Jacobian"
PetscErrorCode Jacobian(IGAPoint p,PetscReal dt,
			PetscReal shift,const PetscScalar *V,
			PetscReal t,const PetscScalar *U,
			PetscScalar *J,void *ctx)
{
  // for now use the option -snes_fd_color for the Jacobian
  return 0;
}

#undef  __FUNCT__
#define __FUNCT__ "LeftInjectionJacobian"
PetscErrorCode LeftInjectionJacobian(IGAPoint p,PetscReal dt,
				     PetscReal shift,const PetscScalar *V,
				     PetscReal t,const PetscScalar *U,
				     PetscScalar *J,void *ctx)
{
  // for now use the option -snes_fd_color for the Jacobian
  return 0;
}

#undef  __FUNCT__
#define __FUNCT__ "LeftInjectionResidual"
PetscErrorCode LeftInjectionResidual(IGAPoint p,PetscReal dt,
				     PetscReal shift,const PetscScalar *V,
				     PetscReal t,const PetscScalar *U,
				     PetscScalar *R,void *ctx)
{
  PetscInt a,nen;
  IGAPointGetSizes(p,&nen,0,0);

  const PetscReal *N0;
  IGAPointGetShapeFuns(p,0,(const PetscReal**)&N0);

  PetscScalar Qh = 0;
  if(t <= 5e5) Qh = -5.57e-6; // inflow

  PetscScalar (*Re)[2] = (PetscScalar (*)[2])R;
  for (a=0; a<nen; a++) {
    Re[a][0] = 0.0;
    Re[a][1] = N0[a]*Qh;
  }
  return 0;
}

#undef __FUNCT__
#define __FUNCT__ "GeometricAdaptivity"
PetscErrorCode GeometricAdaptivity(TS ts,PetscReal t,Vec X,Vec Xdot, PetscReal *nextdt,PetscBool *ok,void *ctx)
{
  PetscErrorCode ierr;
  PetscReal      dt;
  ierr = TSGetTimeStep(ts,&dt);CHKERRQ(ierr);
  *ok = PETSC_TRUE;
  *nextdt = PetscMin(1.2*dt,833.0);
  return 0;
}

#undef __FUNCT__
#define __FUNCT__ "GeometricAdaptivity3"
PetscErrorCode GeometricAdaptivity3(TS ts,PetscReal t,Vec X,Vec Xdot, PetscReal *nextdt,PetscBool *ok,void *ctx)
{
  PetscErrorCode       ierr;
  PetscReal            dt;
  SNES                 snes;
  SNESConvergedReason  snesreason;
  ierr = TSGetTimeStep(ts,&dt);CHKERRQ(ierr);
  ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
  ierr = SNESGetConvergedReason(snes,&snesreason);CHKERRQ(ierr);
  if (snesreason < 0) {
    *ok = PETSC_FALSE;
    *nextdt = 0.9*dt;
    PetscFunctionReturn(0);
  }
  *ok = PETSC_TRUE;
  *nextdt = PetscMin(1.01*dt,10000);
  return 0;
}


#undef __FUNCT__
#define __FUNCT__ "ICTest1"
PetscErrorCode ICTest1(IGA iga,Vec U,AppCtx *user)
{
  PetscErrorCode ierr;
  PetscFunctionBegin;

  PetscScalar Mh = user->Mh;
  PetscScalar H  = user->H;

  DM da;
  ierr = IGACreateNodeDM(iga,2,&da);CHKERRQ(ierr);
  Field **u;
  ierr = DMDAVecGetArray(da,U,&u);CHKERRQ(ierr);
  DMDALocalInfo info;
  ierr = DMDAGetLocalInfo(da,&info);CHKERRQ(ierr);

  PetscInt i,j;
  for(i=info.xs;i<info.xs+info.xm;i++){
    PetscReal x = (PetscReal)i / ( (PetscReal)(info.mx-1) );
    for(j=info.ys;j<info.ys+info.ym;j++){
      u[j][i].Pl = 10.;
      if(x <= 0.5){
	u[j][i].rholh = 15*Mh*H; // Pg = rholh/Mh
      }else{
	u[j][i].rholh = 25*Mh*H;
      }
    }
  }
  ierr = DMDAVecRestoreArray(da,U,&u);CHKERRQ(ierr); 
  ierr = DMDestroy(&da);;CHKERRQ(ierr); 
  PetscFunctionReturn(0); 
}

#undef __FUNCT__
#define __FUNCT__ "Test1"
PetscErrorCode Test1()
{

  PetscErrorCode  ierr;
  AppCtx user;
  user.rholw    = 1000;
  user.porosity = 0.3;
  user.Pr       = 20;
  user.n        = 1.54;
  user.Slr      = 0.01;
  user.Sgr      = 0.;
  user.T        = 303;
  user.H        = 0.765; 
  user.Mw       = 1e-2;
  user.Mh       = 2e-3;
  user.mul      = 1e-8;
  user.mug      = 9e-11;
  user.Dlh      = 3e-9;
  user.k        = 1e-16; 
  PetscInt dim = 2, p = 1, N = 500,L = 1;
  ierr = PetscOptionsBegin(PETSC_COMM_WORLD,"r_","2c2p Options","2c2p");CHKERRQ(ierr);
  ierr = PetscOptionsEnd();CHKERRQ(ierr);

  IGA         iga;
  IGAAxis     axis;
  ierr = IGACreate(PETSC_COMM_WORLD,&iga);CHKERRQ(ierr);
  ierr = IGASetDim(iga,dim);CHKERRQ(ierr);
  ierr = IGASetDof(iga,2);CHKERRQ(ierr);

  ierr = IGAGetAxis(iga,0,&axis);CHKERRQ(ierr);
  ierr = IGAAxisSetDegree(axis,p);CHKERRQ(ierr);
  ierr = IGAAxisInitUniform(axis,N,0.0,L,0);CHKERRQ(ierr);
  ierr = IGAGetAxis(iga,1,&axis);CHKERRQ(ierr);
  ierr = IGAAxisSetDegree(axis,p);CHKERRQ(ierr);
  ierr = IGAAxisInitUniform(axis,N/10,0.0,0.1*L,0);CHKERRQ(ierr);
  
  ierr = IGASetFromOptions(iga);CHKERRQ(ierr);
  ierr = IGASetUp(iga);CHKERRQ(ierr);
  
  ierr = IGASetUserIFunction(iga,Residual,&user);CHKERRQ(ierr);
  ierr = IGASetUserIJacobian(iga,Jacobian,&user);CHKERRQ(ierr);

  TS     ts;
  ierr = IGACreateTS(iga,&ts);CHKERRQ(ierr);
  ierr = TSSetDuration(ts,1000000,1.0e6);CHKERRQ(ierr);
  ierr = TSSetTimeStep(ts,0.17);CHKERRQ(ierr);
  ierr = TSSetType(ts,TSALPHA);CHKERRQ(ierr);
  ierr = TSAlphaSetRadius(ts,0.75);CHKERRQ(ierr);
  ierr = TSAlphaSetAdapt(ts,GeometricAdaptivity,NULL);CHKERRQ(ierr); 
  ierr = TSSetFromOptions(ts);CHKERRQ(ierr);

  PetscReal t; 
  Vec       U;
  ierr = IGACreateVec(iga,&U);CHKERRQ(ierr);
  ierr = ICTest1(iga,U,&user);CHKERRQ(ierr);
  ierr = TSSolve(ts,U,&t);CHKERRQ(ierr);

  ierr = IGAWrite(iga,"iga.dat");CHKERRQ(ierr);
  ierr = IGAWriteVec(iga,U,"sol.dat");CHKERRQ(ierr);

  ierr = VecDestroy(&U);CHKERRQ(ierr);
  ierr = TSDestroy(&ts);CHKERRQ(ierr);
  ierr = IGADestroy(&iga);CHKERRQ(ierr);

  return 0;
}

#undef __FUNCT__
#define __FUNCT__ "ICTest3"
PetscErrorCode ICTest3(IGA iga,Vec U,AppCtx *user)
{
  PetscErrorCode ierr;
  PetscFunctionBegin;

  DM da;
  ierr = IGACreateNodeDM(iga,2,&da);CHKERRQ(ierr);
  Field **u;
  ierr = DMDAVecGetArray(da,U,&u);CHKERRQ(ierr);
  DMDALocalInfo info;
  ierr = DMDAGetLocalInfo(da,&info);CHKERRQ(ierr);

  PetscInt i,j;
  for(i=info.xs;i<info.xs+info.xm;i++){
    for(j=info.ys;j<info.ys+info.ym;j++){
      u[j][i].Pl    = 10.;
      u[j][i].rholh = 0; 
    }
  }
  ierr = DMDAVecRestoreArray(da,U,&u);CHKERRQ(ierr); 
  ierr = DMDestroy(&da);;CHKERRQ(ierr); 
  PetscFunctionReturn(0); 
}

#undef __FUNCT__
#define __FUNCT__ "Test3"
PetscErrorCode Test3()
{
  PetscErrorCode  ierr;

  AppCtx user;
  user.rholw    = 1000;
  user.porosity = 0.15;
  user.Pr       = 20;
  user.n        = 1.49;
  user.Slr      = 0.4;
  user.Sgr      = 0.;
  user.T        = 303;
  user.H        = 0.765; 
  user.Mw       = 1e-2;
  user.Mh       = 2e-3;
  user.mul      = 1e-8/SEC_PER_YEAR;
  user.mug      = 9e-11/SEC_PER_YEAR;
  user.Dlh      = 3e-9*SEC_PER_YEAR;
  user.k        = 5e-20; 
  PetscInt dim = 2, p = 1, N = 200, L = 200;
  ierr = PetscOptionsBegin(PETSC_COMM_WORLD,"r_","2c2p Options","2c2p");CHKERRQ(ierr);
  ierr = PetscOptionsEnd();CHKERRQ(ierr);

  IGA         iga;
  IGAAxis     axis;
  ierr = IGACreate(PETSC_COMM_WORLD,&iga);CHKERRQ(ierr);
  ierr = IGASetDim(iga,dim);CHKERRQ(ierr);
  ierr = IGASetDof(iga,2);CHKERRQ(ierr);

  ierr = IGAGetAxis(iga,0,&axis);CHKERRQ(ierr);
  ierr = IGAAxisSetDegree(axis,p);CHKERRQ(ierr);
  ierr = IGAAxisInitUniform(axis,N,0.0,L,0);CHKERRQ(ierr);
  ierr = IGAGetAxis(iga,1,&axis);CHKERRQ(ierr);
  ierr = IGAAxisSetDegree(axis,p);CHKERRQ(ierr);
  ierr = IGAAxisInitUniform(axis,N/10,0.0,0.1*L,0);CHKERRQ(ierr);
  
  ierr = IGASetFromOptions(iga);CHKERRQ(ierr);
  ierr = IGASetUp(iga);CHKERRQ(ierr);
  
  ierr = IGASetUserIFunction(iga,Residual,&user);CHKERRQ(ierr);
  ierr = IGASetUserIJacobian(iga,Jacobian,&user);CHKERRQ(ierr);

  IGABoundary bnd;
  ierr = IGAGetBoundary(iga,0,0,&bnd);CHKERRQ(ierr);
  ierr = IGABoundarySetUserIFunction(bnd,LeftInjectionResidual,&user);CHKERRQ(ierr);
  ierr = IGABoundarySetUserIJacobian(bnd,LeftInjectionJacobian,&user);CHKERRQ(ierr);
  ierr = IGAGetBoundary(iga,0,1,&bnd);CHKERRQ(ierr);
  ierr = IGABoundarySetValue(bnd,0,10.0);CHKERRQ(ierr);
  ierr = IGABoundarySetValue(bnd,1, 0.0);CHKERRQ(ierr);

  TS     ts;
  ierr = IGACreateTS(iga,&ts);CHKERRQ(ierr);
  ierr = TSSetDuration(ts,1000000,1.0e6);CHKERRQ(ierr);
  ierr = TSSetTimeStep(ts,10.0);CHKERRQ(ierr);
  ierr = TSSetType(ts,TSALPHA);CHKERRQ(ierr);
  ierr = TSAlphaSetRadius(ts,0.75);CHKERRQ(ierr);
  ierr = TSAlphaSetAdapt(ts,GeometricAdaptivity3,NULL);CHKERRQ(ierr); 
  ierr = TSSetFromOptions(ts);CHKERRQ(ierr);

  PetscReal t; 
  Vec       U;
  ierr = IGACreateVec(iga,&U);CHKERRQ(ierr);
  ierr = ICTest3(iga,U,&user);CHKERRQ(ierr);
  ierr = TSSolve(ts,U,&t);CHKERRQ(ierr);

  ierr = IGAWrite(iga,"iga_test3.dat");CHKERRQ(ierr);
  ierr = IGAWriteVec(iga,U,"ss_test3.dat");CHKERRQ(ierr);

  ierr = VecDestroy(&U);CHKERRQ(ierr);
  ierr = TSDestroy(&ts);CHKERRQ(ierr);
  ierr = IGADestroy(&iga);CHKERRQ(ierr);

  return ierr;
}

#undef __FUNCT__
#define __FUNCT__ "main"
int main(int argc, char *argv[]) {

  PetscErrorCode  ierr;
  ierr = PetscInitialize(&argc,&argv,0,0);CHKERRQ(ierr);

  //Test1();
  //Test2();
  ierr = Test3();CHKERRQ(ierr);

  ierr = PetscFinalize();CHKERRQ(ierr);
  return 0;
}