proteus 1.9.1
C/C++/Fortran libraries
Loading...
Searching...
No Matches
pskRelations.h
Go to the documentation of this file.
1#ifndef PSKRELATIONS_H
2#define PSKRELATIONS_H
3#include <algorithm>
4#include <cmath>
5#include <iostream>
6#include <cassert>
7#include "densityRelations.h"
14
16
17/* jcc two phase flow, modified by cek and mwf*/
18using namespace std;
19
21{
22public:
23 double Se,
33 //in case need psic--> Se form
35 PskRelation(const double* rwork, const int* iwork = 0):
36 Se(1.0),dSe_dSw(1.0),
37 Sw_min(rwork[0]),
38 Sw_max(rwork[1]),
39 krw(1.0),dkrw(0.0),
40 krn(0.0),dkrn(0.0),psic(0.0),
41 dpsic(0.0),dSe_dpsic(0.0)
42 {}
44 Se(1.0),dSe_dSw(1.0),
45 Sw_min(0.0),
46 Sw_max(1),
47 krw(1.0),dkrw(0.0),
48 krn(0.0),dkrn(0.0),psic(0.0),
49 dpsic(0.0),dSe_dpsic(0.0)
50 {}
51 virtual inline void setParams(const double* rwork, const int* iwork = 0)
52 {
53 Sw_min = rwork[0];
54 Sw_max = rwork[1];
55 }
56 /*for fudge factors aka tolerances in various models*/
57 virtual inline void setTolerances(const double* rwork_tol)
58 {
59 }
60 virtual ~PskRelation(){}
61
62 /*linear coefficients*/
63 inline void calc(const double& Sw)
64 {
65 calc_Se(Sw);
66
67 krw = Se;
68 dkrw = dSe_dSw;
69
70 krn = (1.0-Se);
71 dkrn = -dSe_dSw;
72
73 psic = Se;
74 dpsic = dSe_dSw;
75 }
76
77 inline void calc_Se(const double& Sw)
78 {
79 if (Sw < Sw_min)
80 {
81 Se = 0.0;
82 dSe_dSw = 0.0;
83 }
84 else if (Sw > Sw_max)
85 {
86 Se = 1.0;
87 dSe_dSw =0.0;
88 }
89 else
90 {
91 Se = (Sw - Sw_min)/(Sw_max-Sw_min);
92 dSe_dSw = 1.0/(Sw_max - Sw_min);
93 }
94 }
95 //for Sw as a function of capillary pressure head
96 //note dkrw,dkrn are set to be wrt psic
97 virtual inline void calc_from_psic(const double& psicIn)
98 {
99 bool implemented = false;
100 assert(implemented);
101 }
102
103};
104
105/* quadratic kr */
106class SimplePSK : public PskRelation
107{
108public:
109 SimplePSK(const double* rwork, const int* iwork = 0):
110 PskRelation(rwork)
111 {}
112
113 inline void calc(const double& Sw)
114 {
115 calc_Se(Sw);
116
117 krw = Se*Se;
118 dkrw = 2.0*Se*dSe_dSw;
119
120 krn = (1.0-Se)*(1.0-Se);
121 dkrn = 2.0*(Se-1.0)*dSe_dSw;
122
123 psic = (1.0-Se)/Sw_min; /*mwf change to test het -Se;*/
124 dpsic = -dSe_dSw;
125 }
126 //
127 virtual inline void calc_from_psic(const double& psicIn)
128 {
129 psic = max(0.0,psicIn);
130 dpsic= 1.0;
131 Se = 1.0-psic*Sw_min;
132 Se = max(0.0,min(1.0,Se));
133
134 krw = Se*Se;
135 dkrw = 2.0*Se*dSe_dSw;
136
137 krn = (1.0-Se)*(1.0-Se);
138 dkrn = 2.0*(Se-1.0)*dSe_dSw;
139
140 }
141};
142
143/* Van Genuchten-Mualem */
144class VGMorig: public PskRelation
145{
146public:
147 double alpha,
153 {}
154 VGMorig(const double* rwork, const int* iwork = 0):
155 PskRelation(rwork),
156 alpha(rwork[2]),
157 m(rwork[3])
158 {
159 n = (1.0)/(1.0-m);
160 Se_eps_const=1.0e-4;
161 //mwf debug
162 //std::cout<<"VGMorig ctor rwork[2]= "<<rwork[2]<<" alpha= "<<alpha<<" rwork[3]= "<<rwork[3]<<" m= "<<m<<" n= "<<n<<std::endl;
163 }
164 /*for fudge factors aka tolerances in various models*/
165 virtual inline void setTolerances(const double* rwork_tol)
166 {
167 Se_eps_const = rwork_tol[0];
168 }
169
170 inline void setParams(const double* rwork, const int* iwork = 0)
171 {
172 Sw_min = rwork[0];
173 Sw_max = rwork[1];
174 alpha = rwork[2];
175 m = rwork[3];
176 n = (1.0)/(1.0-m);
177 //mwf debug
178 //std::cout<<"VGMorig setParams rwork[2]= "<<rwork[2]<<" alpha= "<<alpha<<" rwork[3]= "<<rwork[3]<<" m= "<<m<<" n= "<<n<<std::endl;
179 }
180 inline void calc_Se_eps(const double& Se)
181 {
182 /* cek todo, work out when you need to stay away from Se=0 and 1 and what to assign in these cases */
183 if(Se <= Se_eps_const)
185 else if(Se >= (1.0-Se_eps_const))
187 else
188 Se_eps=Se;
189 }
190
191 inline void calc(const double& Sw)
192 {
193 double Seovmmo,Seovm,Seovmmh,S,Smmo,S2mmo,Sm,S2m,pcesub1,pcesub2;
194 calc_Se(Sw);
196
197 Seovmmo = pow(Se_eps,((1.0/m)-1.0));
198 Seovm = Seovmmo*Se_eps;
199 Seovmmh = pow(Se_eps,((1.0/m)-0.5));
200 S = 1.0 - Seovm;
201 Smmo = pow(S,m-1.0);
202 S2mmo = pow(S,2.0*m-1.0);
203 Sm = Smmo*S;
204 S2m = S2mmo*S;
205
206 pcesub1 = pow(((1.0/Seovm)-1.0),((1.0/n)-1.0));
207 pcesub2 = pcesub1*((1.0/Seovm)-1.0);
208
209 krw = sqrt(Se)*(1.0-Sm)*(1.0-Sm);
210 dkrw = (0.5*(1.0/sqrt(Se_eps))*(1.0-Sm)*(1.0-Sm) + 2.0*(1-Sm)*Smmo*Seovmmh)*dSe_dSw;
211
212 krn = sqrt(1.0-Se)*S2m;
213 dkrn = (-0.5*(1.0/sqrt(1.0-Se_eps))*S2m - 2.0*sqrt(1.0-Se)*(S2mmo)*Seovmmo)*dSe_dSw;
214
215 psic = pow((pow(Se_eps,(-1.0/m)) - 1.0),(1.0/n))/alpha;
216 dpsic = ((-1.0/(alpha*n*m))*(pow((pow(Se_eps,-1.0/m)-1.0),-m))*(pow(Se_eps,((-1.0/m)-1.0))))*dSe_dSw;
217 }
218};
219
220class VGM : public VGMorig
221{
222 public:
225 {}
226 VGM(const double* rwork, const int* iwork = 0):
227 VGMorig(rwork,iwork),
228 ns_del(1.0e-8),
229 eps_small(1.0e-16),
230 sqrt_eps_small(1.0e-8)
231 {}
232 VGM(const VGM& r)
233 {
234 Se=r.Se;
235 dSe_dSw=r.dSe_dSw;
236 Sw_min=r.Sw_min;
237 Sw_max=r.Sw_max;
238 krw=r.krw;
239 dkrw=r.dkrw;
240 krn=r.krn;
241 dkrn=r.dkrn;
242 psic=r.psic;
243 dpsic=r.dpsic;
244 dSe_dpsic=r.dSe_dpsic;
245 alpha=r.alpha;
246 m=r.m;
247 n=r.n;
248 Se_eps_const=r.Se_eps_const;
249 ns_del=r.ns_del;
250 eps_small=r.eps_small;
251 sqrt_eps_small=r.sqrt_eps_small;
252 }
253 /*for fudge factors aka tolerances in various models*/
254 virtual inline void setTolerances(const double* rwork_tol)
255 {
256 eps_small = rwork_tol[0]; //mwf 072110 don't tie to eps_small? sqrt_eps_small = sqrt(eps_small);
257 ns_del = rwork_tol[1];
258 }
259
260 inline void calc_Se(const double& Sw)
261 {
262 Se = (Sw - Sw_min)/(Sw_max-Sw_min);
263 dSe_dSw = 1.0/(Sw_max - Sw_min);
264 Se = max(eps_small,min(Se,1.0-eps_small));
265 }
266
267 inline void calc(const double& Sw)
268 {
269 calc_Se(Sw);
270 //taken from MualemVanGenuchten2p in pdetk
271 double sBar,psiC,DsBar_DpC,DDsBar_DDpC,DkrW_DpC,DkrN_DpC;
272 double vBar,uBar,
273 alphaPsiC, alphaPsiC_n, alphaPsiC_nM1, alphaPsiC_nM2,
274 onePlus_alphaPsiC_n,
275 sqrt_sBar, sqrt_1minusSbar,
276 sBarByOnePlus_alphaPsiC_n, sBarBy_onePlus_alphaPsiC_n_2;
277
278 sBar = Se;
279 //begin MualemVanGenuchten2p setVFraction
280 onePlus_alphaPsiC_n = pow(sBar,1.0/-m);
281 alphaPsiC_n = onePlus_alphaPsiC_n - 1.0;
282 alphaPsiC = pow(alphaPsiC_n,1.0/n);
283 psiC = alphaPsiC/alpha;
284
285 alphaPsiC_nM1 = alphaPsiC_n/alphaPsiC;
286 sBarByOnePlus_alphaPsiC_n = sBar/onePlus_alphaPsiC_n;
287 sqrt_sBar = sqrt(sBar);
288 sqrt_1minusSbar = sqrt(1.0 - sBar);
289
290 DsBar_DpC = -alpha*(n-1.0)*alphaPsiC_nM1
291 *sBarByOnePlus_alphaPsiC_n;
292 //DthetaW_DpC = thetaSR[i] * DsBar_DpC;
293
294 vBar = 1.0-alphaPsiC_nM1*sBar;
295 uBar = alphaPsiC_nM1*sBar;
296
297 //change names krW--> krw, krN--> krn
298 krw = sqrt_sBar*vBar*vBar;
299 krn = sqrt_1minusSbar*uBar*uBar;
300 psic= psiC;
301 if(psiC<=0.0)
302 {
303 DsBar_DpC = 0.0;
304 //DthetaW_DpC = 0.0;
305 krw = 1.0;
306 krn = 0.0;
307 }
308
309 //begin MualemVanGenuchten2p calculateDerivatives
310 alphaPsiC_nM2 = alphaPsiC_nM1/alphaPsiC;
311
312 sBarBy_onePlus_alphaPsiC_n_2 = sBarByOnePlus_alphaPsiC_n
313 /onePlus_alphaPsiC_n;
314 DDsBar_DDpC = alpha*alpha*(n-1.)
315 *((2*n-1.)*alphaPsiC_nM1*alphaPsiC_nM1
316 *sBarBy_onePlus_alphaPsiC_n_2
317 -
318 (n-1.)*alphaPsiC_nM2
319 *sBarByOnePlus_alphaPsiC_n);
320
321 //DDthetaW_DDpC = thetaSR[i]*DDsBar_DDpC;
322
323 DkrW_DpC = (0.5/sqrt_sBar)*DsBar_DpC*vBar*vBar
324 -
325 2.0*sqrt_sBar*vBar*
326 (alpha*(n-1.0)*alphaPsiC_nM2*sBar
327 + alphaPsiC_nM1 * DsBar_DpC);
328
329 //DKW_DpC = KWs[i]*DkrW_DpC;
330
331
332 //recalculate if necessary
333 if (sqrt_1minusSbar >= sqrt_eps_small)//SQRT_MACHINE_EPSILON)
334 {
335 DkrN_DpC = -(0.5/sqrt_1minusSbar)*DsBar_DpC*uBar*uBar
336 +
337 2.0*sqrt_1minusSbar*uBar*
338 (alpha*(n-1.0)*alphaPsiC_nM2*sBar
339 + alphaPsiC_nM1 * DsBar_DpC);
340 }
341 else
342 {
343 DkrN_DpC =((1.0 - sBar)/eps_small)*2.0*sqrt_eps_small*uBar*
344 (alpha*(n-1.0)*alphaPsiC_nM2*sBar
345 + alphaPsiC_nM1 * DsBar_DpC)
346 - (DsBar_DpC/eps_small)*sqrt_eps_small*uBar*uBar;
347 }
348
349 //if we're in the nonsmooth regime
350 if (psiC < ns_del && psiC > 0.0 )
351 {
352 DkrW_DpC = 0.0;
353 }
354
355 if (psiC <= 0.0)
356 {
357 DDsBar_DDpC = 0.0;
358 //DDthetaW_DDpC = 0.0;
359 DkrW_DpC = 0.0;
360 DkrN_DpC = 0.0;
361 }
362 //end calculateDerivatives
363 double DpC_Dse = 0.0;
364 if (fabs(DsBar_DpC) > 0.0)
365 DpC_Dse = 1.0/DsBar_DpC;
366 double DpC_Dsw = DpC_Dse*dSe_dSw;
367 dkrw = DkrW_DpC*DpC_Dsw;
368 dkrn = DkrN_DpC*DpC_Dsw;
369 dpsic= DpC_Dsw;
370
371 }
372 //TODO add this to other classes
373 virtual inline void calc_from_psic(const double& psicIn)
374 {
375 //taken from MualemVanGenuchten2p in pdetk
376 double sBar,psiC,DsBar_DpC,DDsBar_DDpC,DkrW_DpC,DkrN_DpC;
377 double vBar,uBar,
378 alphaPsiC, alphaPsiC_n, alphaPsiC_nM1, alphaPsiC_nM2,
379 onePlus_alphaPsiC_n,
380 sqrt_sBar, sqrt_1minusSbar,
381 sBarByOnePlus_alphaPsiC_n, sBarBy_onePlus_alphaPsiC_n_2;
382
383
384 psiC = max(0.0,psicIn);
385 alphaPsiC = alpha*psiC;
386 alphaPsiC_n = pow(alphaPsiC,n);
387 alphaPsiC_nM1 = alphaPsiC_n/alphaPsiC;
388 onePlus_alphaPsiC_n = 1.0 + alphaPsiC_n;
389 sBar = pow(onePlus_alphaPsiC_n,-m);
390 sBarByOnePlus_alphaPsiC_n = sBar/onePlus_alphaPsiC_n;
391 sqrt_sBar = sqrt(sBar);
392 sqrt_1minusSbar = sqrt(1.0 - sBar);
393 //thetaW = thetaSR[i]*sBar + thetaR[i];
394 DsBar_DpC = -alpha*(n-1.0)*alphaPsiC_nM1
395 *sBarByOnePlus_alphaPsiC_n;
396 //DthetaW_DpC = thetaSR[i] * DsBar_DpC;
397 vBar = 1.0-alphaPsiC_nM1*sBar;
398 uBar = alphaPsiC_nM1*sBar;
399
400 //change names krW--> krw, krN--> krn
401 krw = sqrt_sBar*vBar*vBar;
402 krn = sqrt_1minusSbar*uBar*uBar;
403 Se = sBar;
404 psic=psiC;
405 if(psiC<=0.0)
406 {
407 sBar = 1.0;
408 Se = sBar;
409 //thetaW = thetaS[i];
410 DsBar_DpC = 0.0;
411 //DthetaW_DpC = 0.0;
412 krw = 1.0;
413 krn = 0.0;
414 }
415 //mwf debug
416 //std::cout<<"vgm_calc_from_psic alpha= "<<alpha<<" m= "<<m<<" psic= "<<psic<<" sBar= "<<sBar<<" krw= "<<krw<<" krn= "<<krn
417 // <<std::endl;
418 //begin MualemVanGenuchten2p calculateDerivatives
419 alphaPsiC_nM2 = alphaPsiC_nM1/alphaPsiC;
420
421 sBarBy_onePlus_alphaPsiC_n_2 = sBarByOnePlus_alphaPsiC_n
422 /onePlus_alphaPsiC_n;
423 DDsBar_DDpC = alpha*alpha*(n-1.)
424 *((2*n-1.)*alphaPsiC_nM1*alphaPsiC_nM1
425 *sBarBy_onePlus_alphaPsiC_n_2
426 -
427 (n-1.)*alphaPsiC_nM2
428 *sBarByOnePlus_alphaPsiC_n);
429
430 //DDthetaW_DDpC = thetaSR[i]*DDsBar_DDpC;
431
432 DkrW_DpC = (0.5/sqrt_sBar)*DsBar_DpC*vBar*vBar
433 -
434 2.0*sqrt_sBar*vBar*
435 (alpha*(n-1.0)*alphaPsiC_nM2*sBar
436 + alphaPsiC_nM1 * DsBar_DpC);
437
438 //DKW_DpC = KWs[i]*DkrW_DpC;
439
440
441 //recalculate if necessary
442 if (sqrt_1minusSbar >= sqrt_eps_small)//SQRT_MACHINE_EPSILON)
443 {
444 DkrN_DpC = -(0.5/sqrt_1minusSbar)*DsBar_DpC*uBar*uBar
445 +
446 2.0*sqrt_1minusSbar*uBar*
447 (alpha*(n-1.0)*alphaPsiC_nM2*sBar
448 + alphaPsiC_nM1 * DsBar_DpC);
449 }
450 else
451 {
452 DkrN_DpC =((1.0 - sBar)/eps_small)*2.0*sqrt_eps_small*uBar*
453 (alpha*(n-1.0)*alphaPsiC_nM2*sBar
454 + alphaPsiC_nM1 * DsBar_DpC)
455 - (DsBar_DpC/eps_small)*sqrt_eps_small*uBar*uBar;
456 }
457
458 //if we're in the nonsmooth regime
459 if (psiC < ns_del && psiC > 0.0 )
460 {
461 DkrW_DpC = 0.0;
462 }
463
464 if (psiC <= 0.0)
465 {
466 DDsBar_DDpC = 0.0;
467 //DDthetaW_DDpC = 0.0;
468 DkrW_DpC = 0.0;
469 DkrN_DpC = 0.0;
470 }
471 //end calculateDerivatives
472 dkrw = DkrW_DpC; //note: \od{k_{rw}}{\psi_c} not \od{k_{rw}}{S_w}
473 dkrn = DkrN_DpC;
474 dpsic= 1.0;
475 dSe_dpsic=DsBar_DpC;
476 }
477};
478/* Van Genuchten-Burdine */
479class VGB : public VGM
480{
481public:
482 VGB(const double* rwork, const int* iwork = 0):
483 VGM(rwork,iwork)
484 {}
485
486 inline void calc(const double& Sw)
487 {
488 double S,Smmo,Sm,alpha,Se1ovMmo;
489 calc_Se(Sw);
491
492 Se1ovMmo = pow(Se_eps,((1.0/m)-1.0));
493 S = 1.0-Se1ovMmo*Se_eps;
494 Smmo = pow(S,(m-1.0));
495 Sm = Smmo*S;
496
497 //cek from matlab, needs optimizing
498
499 krw = Se_eps*Se_eps*(1.0-pow(1.0-pow(Se_eps,1.0/m),1.0/m));
500 dkrw = (2.0*Se_eps*(1.0-pow(1.0-pow(Se_eps,1.0/m),1.0/m))+Se_eps*pow(1.0-pow(Se_eps,1.0/m),1.0/m)/(m*m)*pow(Se_eps,1.0/m)/(1.0-pow(Se_eps,1.0/m)))*dSe_dSw;
501
502 krn = pow(1.0-Se_eps,2.0)*pow(1.0-pow(Se_eps,1.0/m),1.0*m);
503 dkrn = (-2.0*(1.0-Se_eps)*pow(1.0-pow(Se_eps,1.0/m),1.0*m)-pow(1.0-Se_eps,2.0)*pow(1.0-pow(Se_eps,1.0/m),1.0*m)*pow(Se_eps,1.0/m)/Se_eps/(1.0-pow(Se_eps,1.0/m)))*dSe_dSw;
504
505// krw = (Se*Se)*(1.0-Sm);
506// dkrw = ((2.0*Se)*(1.0-Sm)+(Se*Se)*(Se1ovMmo)*Smmo)*dSe_dSw;
507
508// krn = (1.0-Se)*(1.0-Se)*(Sm);
509// dkrn = (2.0*(Se-1.0)*Sm-((1.0-Se)*(1.0-Se))*Smmo*Se1ovMmo)*dSe_dSw;
510 psic = pow((pow(Se_eps,(-1.0/m)) - 1.0),(1.0/n))/alpha;
511 dpsic = ((-1.0/(alpha*n*m))*(pow((pow(Se_eps,-1.0/m)-1.0),-m))*(pow(Se_eps,((-1.0/m)-1.0))))*dSe_dSw;
512 }
513};
514
515/* Brooks-Corey-Mualem */
516class BCM : public PskRelation
517{
518 public:
519 double pd,lambda;
520
521 BCM(const double* rwork, const int* iwork = 0):
522 PskRelation(rwork,iwork),
523 pd(rwork[2]),
524 lambda(rwork[3])
525 {}
526
527 inline void setParams(const double* rwork, const int* iwork = 0)
528 {
529 Sw_min = rwork[0];
530 Sw_max = rwork[1];
531 pd = rwork[2];
532 lambda = rwork[3];
533 }
534 inline void calc(const double& Sw)
535 {
536 double Value,Expon,krwovSe,Oovbclpo,Oovbcl,X,sqrt1mu;
537 calc_Se(Sw);
538
539 Oovbcl = 1.0/lambda;
540 Oovbclpo = Oovbcl+1.0;
541 X = pow(Se,Oovbcl);
542 Value = 1.0-X*Se;
543 Expon = ((4.0+5.0*lambda)/(2.0*lambda));
544 krwovSe = pow(Se,(Expon-1.0));
545 sqrt1mu = sqrt(1.0-Se);
546
547 krw = krwovSe*Se;
548 dkrw = (Expon*krwovSe)*dSe_dSw;
549
550 krn = sqrt1mu*Value*Value;
551 dkrn = (-0.5*(1.0/sqrt1mu)*Value*Value - 2.0*sqrt1mu*Value*Oovbclpo*X )*dSe_dSw;
552
553 psic = 1.0/X;
554 dpsic = (-Oovbcl/(X*Se))*dSe_dSw;
555 }
556};
557
558/* Brooks-Corey-Burdine */
559class BCB : public BCM
560{
561public:
562 BCB(const double* rwork, const int* iwork = 0):BCM(rwork,iwork)
563 {}
564
565 inline void calc(const double& Sw)
566 {
567 double Se2ovL,Se2,Se3,omSe,Expon,Semoovlmo,Se_cutOff;
568 calc_Se(Sw);
569
570 Se2ovL = pow(Se,(2.0/lambda));
571 Se2 = Se*Se;
572 Se3 = Se*Se2;
573 omSe = 1.0-Se;
574 Expon = (2.0+3.0*lambda)/lambda;
575 Se_cutOff = max(1.0e-4,Se);
576 Semoovlmo = pow(Se_cutOff,((-1.0/lambda)-1.0));
577
578 krw = Se2ovL*Se3;
579 dkrw = Expon*Se2ovL*Se2*dSe_dSw;
580
581 krn = (omSe*omSe)*(1.0-Se2ovL*Se);
582 dkrn = (2.*omSe*(1.0-Se2ovL*Se) - (omSe*omSe)*(Expon-2.0)*Se2ovL*dSe_dSw)*dSe_dSw;
583
584 /* cek debug */
585 krw = pow(Se,(2.0+3.0*lambda)/lambda);
586 dkrw = (((2.0+3.0*lambda)/lambda)*pow(Se,(2.0+3.0*lambda)/lambda - 1.0))*dSe_dSw;
587
588 krn = (1.0-Se)*(1.0-Se)*(1.0-pow(Se,(2.0+lambda)/lambda));
589 dkrn = (-2.0*(1.0-Se)*(1.0-pow(Se,(2.0+lambda)/lambda))
590 -((2.0+lambda)/lambda)*(1.0-Se)*(1.0-Se)*pow(Se,(2.0+lambda)/lambda-1.0))*dSe_dSw;
591
592 psic = pd*Semoovlmo*Se;
593 dpsic = pd*(-1.0/lambda)*Semoovlmo*dSe_dSw;
594 }
595};
596
598{
599public:
600 double muw,
612
613 FractionalFlowVariables(double muwIn,double munIn):
614 muw(muwIn),
615 mun(munIn)
616 {}
617
618 inline void calc(const PskRelation& psk,
619 const DensityRelation& density_w,
620 const DensityRelation& density_n)
621 {
622 lambdaw = density_w.rho*psk.krw/muw;
623 dlambdaw =(density_w.rho/muw)*psk.dkrw;
624
625 lambdan =(density_n.rho*psk.krn)/mun;
626 dlambdan =(density_n.rho/mun)*psk.dkrn;
627
630
633
636 }
637};
638
640{
641public:
642 CompressibleN_FractionalFlowVariables(double muwIn,double munIn):
643 FractionalFlowVariables(muwIn,munIn)
644 {}
645
646 double drhon,
653
654 inline void calc(const PskRelation& psk,
655 const DensityRelation& density_w,
656 const DensityRelation& density_n)
657 {
658 lambdaw = density_w.rho*psk.krw/muw;
659 dlambdaw = (density_w.rho/muw)*psk.dkrw;
660 dlambdaw_psiw = 0.0;
661
662 drhon = density_n.drho*psk.dpsic;
663 drhon_psiw = density_n.drho;
664
665 lambdan = (density_n.rho*psk.krn)/mun;
666 dlambdan = (1.0/mun)*(psk.dkrn*density_n.rho + drhon*psk.krn);
668
672
676
680 }
681};
682
683
685{
686 /*************************************************************
687 spline psk relations. tables of values are held (externally)
688 in splineArray in the order
689
690 u (sw, or psic), u^-1 (either sw or psic), krw,krn
691
692 force dSe_dSw = 1.0, so that will be consistent with analytical evaluations
693 where dkrw, etc are wrt to se
694
695 ************************************************************/
696public:
697 PskSpline(const double* rworkIn, const int* iworkIn=0):
698 PskRelation(),
699 nknots(2),
700 lastIndex(0),
701 uinvOffset(1),
702 krwOffset(2),
703 krnOffset(3),
704 splineArray(rworkIn)
705 {
706 assert(iworkIn);
707 nknots = iworkIn[0];
708 }
709 virtual ~PskSpline() {}
710 virtual inline void calc(const double& Sw)
711 {
712 assert(splineArray);
713 //
714 Se = Sw; //note force Se = Sw since splines are evaluated directly
715 dSe_dSw = 1.0;
717 nknots,
718 &lastIndex,
719 &psic,
720 &dpsic,
723
724 //
726 nknots,
727 &lastIndex,
728 &krw,
729 &dkrw,
732 //
734 nknots,
735 &lastIndex,
736 &krn,
737 &dkrn,
740
741 }
742 virtual inline void calc_from_psic(const double& psicIn)
743 {
744 assert(splineArray);
745 //
746 psic = psicIn;
748 nknots,
749 &lastIndex,
750 &Se, //same as Sw
751 &dSe_dpsic, //same as dSw_dpsic
754
755 //
757 nknots,
758 &lastIndex,
759 &krw,
760 &dkrw,
763 //
765 nknots,
766 &lastIndex,
767 &krn,
768 &dkrn,
771
772 }
773 virtual inline void setParams(const double* rwork, const int* iwork = 0)
774 {
775 splineArray = rwork;
776 if (iwork)
777 nknots = iwork[0];
778 }
779public:
781 const double* splineArray;
782};
783
796
797
798// =============================================================================
799// Pore size distribution / relative permeability (PSK) closures for Richards.
800//
801// Richards.h owns the PDE coefficient assembly (mass, diffusion tensor,
802// buoyancy flux, their Jacobians); this header owns *only* the constitutive
803// relations theta_w(psiC) and k_rw(psiC) plus their inverses. Selecting a
804// model is a single branch on PSK_TYPE_member at each call site:
805//
806// PSK_TYPE 0 -> vgm_* van Genuchten retention + Mualem k_rw (default)
807// PSK_TYPE 1 -> bc_* Brooks-Corey retention + Burdine k_rw
808// PSK_TYPE 2 -> bc_* Brooks-Corey retention + Mualem k_rw
809// PSK_TYPE 3 -> gardner_* Gardner exponential retention + k_rw = S_e
810//
811// Codes 1 and 2 share one retention curve and differ only in the exponent of
812// k_rw = S_e^eta, so only the forward closure branches on them; the inverses
813// (which see theta_w alone) treat them alike. Code 3 is a model of its own in
814// both halves; it is the closure Tracy's analytical solutions assume.
815//
816// Conventions shared by every routine here:
817// psiC is the suction (-u). psiC > 0 is unsaturated, psiC <= 0 saturated.
818// vgm_ and bc_ clamp that saturated side to (thetaS, 1) with zero slope;
819// gardner_ deliberately does NOT, and the block below says why -- a zero
820// dtheta/dpsiC is a lost Jacobian diagonal wherever beta = 0.
821// Derivatives are with respect to psiC, NOT with respect to u; the caller in
822// Richards.h owns the sign flip d(psiC)/du = -1.
823// The vgm_, bc_ and gardner_ routines take the same parameter slots in the
824// same order, so the branch is a one-line swap. In the BC parameterisation
825// the second numeric parameter is the pore-size index lambda (taking n_vg's
826// slot) and alpha = 1/p_d is the inverse entry-pressure head; under Gardner
827// alpha is the exponential decay rate [1/m] and the n_vg slot is unused.
828//
829// The Brooks-Corey conductivity exponent eta is NOT fixed by lambda. Two
830// closures are in common use and they do not agree: Burdine gives
831// eta = (2+3*lambda)/lambda, Mualem gives eta = 2.5 + 2/lambda. At the
832// lambda = 0.592 sand of Szymkiewicz [2009], WRR 45, W10403 (Table 1, soil 5)
833// that is 6.378 against 5.878 -- not a rounding difference: Burdine is a factor
834// ~4 drier in k_rw by psiC = 7.5 m and ~7 by 50 m, which moves a gravity-driven
835// wetting front. Which one applies is a property of the parameter set being
836// reproduced, so it is selected by PSK_TYPE and the exponent is derived from
837// lambda here, in bc_eta, rather than computed by the caller.
838// =============================================================================
839
840namespace proteus
841{
842namespace richards
843{
844namespace psk
845{
846
847// -----------------------------------------------------------------------------
848// van Genuchten - Mualem (pore-connectivity exponent l = 1/2)
849//
850// S_e = (1 + (alpha*psiC)^n)^(-m), m = 1 - 1/n
851// theta_w = thetaR + thetaSR * S_e
852// k_rw = sqrt(S_e) * (1 - (alpha*psiC)^(n-1) * S_e)^2
853//
854// pcBarStar floors alpha*psiC at 1e-8 so pow(pcBar, n-2) stays finite as
855// psiC -> 0 for n < 2; sqrt_sBarStar floors the same way inside the k_rw
856// derivative only.
857// -----------------------------------------------------------------------------
858inline void vgm_wetting(const double psiC,
859 const double alpha,
860 const double n_vg,
861 const double thetaR,
862 const double thetaSR,
863 double &thetaW,
864 double &DthetaW_DpsiC,
865 double &KWr,
866 double &DKWr_DpsiC)
867{
868 const double m_vg = 1.0 - 1.0 / n_vg;
869 const double thetaS = thetaR + thetaSR;
870 if (psiC > 0.0) {
871 const double pcBar = alpha * psiC;
872 double pcBarStar = pcBar;
873 if (pcBar < 1.0e-8) pcBarStar = 1.0e-8;
874 const double pcBar_nM2 = pow(pcBarStar, n_vg - 2);
875 const double pcBar_nM1 = pcBar_nM2 * pcBar;
876 const double pcBar_n = pcBar_nM1 * pcBar;
877 const double onePlus_pcBar_n = 1.0 + pcBar_n;
878
879 const double sBar = pow(onePlus_pcBar_n, -m_vg);
880 /* using -mn = 1-n */
881 const double DsBar_DpsiC =
882 alpha * (1.0 - n_vg) * (sBar / onePlus_pcBar_n) * pcBar_nM1;
883
884 const double vBar = 1.0 - pcBar_nM1 * sBar;
885 const double vBar2 = vBar * vBar;
886 const double DvBar_DpsiC =
887 -alpha * (n_vg - 1.0) * pcBar_nM2 * sBar - pcBar_nM1 * DsBar_DpsiC;
888
889 thetaW = thetaSR * sBar + thetaR; //thetaS;//
890 DthetaW_DpsiC = thetaSR * DsBar_DpsiC; //0.0;//
891
892 const double sqrt_sBar = sqrt(sBar);
893 double sqrt_sBarStar = sqrt_sBar;
894 if (sqrt_sBar < 1.0e-8) sqrt_sBarStar = 1.0e-8;
895 KWr = sqrt_sBar * vBar2;
896 DKWr_DpsiC = ((0.5 / sqrt_sBarStar) * DsBar_DpsiC * vBar2
897 + 2.0 * sqrt_sBar * vBar * DvBar_DpsiC);
898 } else {
899 thetaW = thetaS;
900 DthetaW_DpsiC = 0.0;
901 KWr = 1.0;
902 DKWr_DpsiC = 0.0;
903 }
904}
905
906// Analytic van Genuchten inverse: theta_w -> psiC. Leaves u untouched (so the
907// caller keeps its previous iterate) outside the strictly unsaturated range.
908//
909// The dry limit is a cap on psiC, not a fraction of thetaR -- same convention as
910// bc_invert_analytic below. 1.01*thetaR looks harmless but is a band in theta,
911// and theta -> psiC is exponentially steep in the tail, so it silently swallows
912// a huge band in head: for a sand with alpha=14.5 1/m and n=2.68 it refuses to
913// invert anything drier than psiC = 3.8 m, which for a 20 m column is the whole
914// unwetted region. Every FCT-limited mass landing there was discarded (u kept
915// its pre-limiter value), which breaks the conservation chain theta_limited ->
916// psi and shows up as spurious infiltration. pcBar <= 1e4 puts the cut at
917// psiC = 1e4/alpha instead, i.e. far outside any physical range.
918inline void vgm_invert_analytic(const double m,
919 const double rho,
920 const double alpha,
921 const double n_vg,
922 const double thetaR,
923 const double thetaSR,
924 double &u)
925{
926 double psiC, pcBar, pcBar_n, sBar, thetaW, thetaS, m_vg;
927 m_vg = 1.0 - 1.0 / n_vg;
928 thetaS = thetaR + thetaSR;
929 thetaW = m / rho;
930 const double pcBarMax = 1.0e4;
931 const double SeMin = pow(1.0 + pow(pcBarMax, n_vg), -m_vg);
932 if (thetaW > thetaR + SeMin * thetaSR && thetaW < thetaS) {
933 sBar = (thetaW - thetaR) / thetaSR;
934 pcBar_n = pow(sBar, -1.0 / m_vg) - 1.0;
935 pcBar = pow(pcBar_n, 1.0 / n_vg);
936 psiC = pcBar / alpha;
937 u = -psiC;
938 } else if (thetaW >= thetaS) {
939 if (u < 0.0) u = 0.0;
940 } else {
941 u = -pcBarMax / alpha;
942 }
943}
944
945// Newton inverse of the FULL forward mass m = rho(u) * theta_w(u), so the
946// exp(beta*u) factor the analytic inverse ignores is included. The analytic
947// inverse seeds the iteration.
948//
949// The step cap is one capillary length, not an absolute 5 cm. dtheta/du spans
950// ~4 decades over the retention curve, so a fixed step cap cannot reach the
951// root at either end: with duMax = 5e-2 the 50 iterations here covered only
952// 2.5 m of head, and any correction needing more than that fell through to the
953// u = u_prev revert at the bottom of the loop.
954inline void vgm_invert_newton(const double m,
955 const double rho,
956 const double beta,
957 const double alpha,
958 const double n_vg,
959 const double thetaR,
960 const double thetaSR,
961 double &u)
962{
963 const double u_prev = u;
964
965 const double thetaS = thetaR + thetaSR;
966 const double m_vg = 1.0 - 1.0 / n_vg;
967
968 const double psiC0 = -u;
969 //saturated, no inversion -- unless the target mass is BELOW saturation: then a
970 //saturated start psi is simply the wrong branch, and keeping it discards the
971 //limited decrease (the node keeps rho*theta_s and water appears)
972 if (psiC0 <= 0.0 && m >= rho * thetaS * (1.0 - 1.0e-12)) { return; }
973 const double rhom0 = rho * std::exp(beta * u);//first guess
974 const double thetaW_imp = m / rhom0;
975 // Dry limit stated the same way as in vgm_invert_analytic: a cap on psiC, not
976 // a fraction of thetaR. Same reason -- theta -> psiC is exponentially steep
977 // in the tail, so 1.01*thetaR is a narrow band in theta but a huge one in
978 // head (psiC = 3.8 m for alpha = 14.5 1/m, n = 2.68), and every FCT-limited
979 // mass landing in it was silently discarded, breaking theta_limited -> psi.
980 // The analytic seed below is exact at beta = 0 and lands within a couple of
981 // Newton steps otherwise, so widening the range does not lengthen the solve.
982 const double pcBarMax = 1.0e4;
983 const double SeMin = std::pow(1.0 + std::pow(pcBarMax, n_vg), -m_vg);
984 if (thetaW_imp < thetaR + SeMin * thetaSR) { return; } //below the dry cut
985 const double thetaEps = 1e-12;
986 double m_target = m;
987 if (thetaW_imp > 0.99 * thetaS) {
988 const double thetaWc = std::min(thetaW_imp, thetaS - thetaEps);
989 m_target = rhom0 * thetaWc;
990 }
991
992 // Use the analytic van Genuchten inverse as the initial guess for Newton.
993 {
994 const double thetaW_guess = std::min(m_target / rhom0, thetaS - thetaEps);
995 if (thetaW_guess > thetaR + thetaEps && thetaW_guess < thetaS - thetaEps) {
996 const double sBar = (thetaW_guess - thetaR) / thetaSR;
997 if (sBar > 0.0 && sBar < 1.0) {
998 const double pcBar_n = std::pow(sBar, -1.0 / m_vg) - 1.0;
999 if (pcBar_n > 0.0) {
1000 const double pcBar = std::pow(pcBar_n, 1.0 / n_vg);
1001 const double u_guess = -pcBar / alpha;
1002 if (std::isfinite(u_guess) && u_guess < 0.0) {
1003 u = u_guess;
1004 }
1005 }
1006 }
1007 }
1008 }
1009
1010 /*----------------------------------------------------
1011 Newton solve (UNSATURATED ONLY)
1012 ----------------------------------------------------*/
1013 const int maxIts = 50;
1014 const double tol = 1e-12 * std::max(1.0, std::fabs(m));
1015 const double duMax = 1.0 / alpha;
1016
1017 auto theta_and_dtheta_du = [&](double u,
1018 double &thetaW,
1019 double &dtheta_du) -> bool
1020 {
1021 const double psiC = -u;
1022
1023 if (psiC <= 0.0) { return false; } //no inversion in saturation
1024 //van Genuchten relations
1025 const double pcBar = alpha * psiC;
1026 const double pcBarStar = (pcBar < 1e-12) ? 1e-12 : pcBar;
1027 const double pcBar_nM2 = std::pow(pcBarStar, n_vg - 2.0);
1028 const double pcBar_nM1 = pcBar_nM2 * pcBar;
1029 const double pcBar_n = pcBar_nM1 * pcBar;
1030 const double onePlus_pcBar_n = 1.0 + pcBar_n;
1031 const double sBar = std::pow(onePlus_pcBar_n, -m_vg);
1032
1033 const double DsBar_DpsiC =
1034 alpha * (1.0 - n_vg) * (sBar / onePlus_pcBar_n) * pcBar_nM1;
1035
1036 thetaW = thetaR + thetaSR * sBar;
1037 dtheta_du = -thetaSR * DsBar_DpsiC;
1038 if (thetaW <= thetaR + thetaEps) return false;
1039 if (thetaW >= thetaS - thetaEps) return false;
1040 return true;
1041 };
1042 for (int it = 0; it < maxIts; ++it)
1043 {
1044 if (-u <= 0.0) { u = u_prev; return; }
1045
1046 double thetaW, dtheta_du;
1047 if (!theta_and_dtheta_du(u, thetaW, dtheta_du)) {
1048 u = u_prev; return;
1049 }
1050
1051 const double rhom = rho * std::exp(beta * u);
1052 const double g = rhom * thetaW - m_target;
1053 if (std::fabs(g) < tol) return;
1054
1055 const double gp = rhom * (beta * thetaW + dtheta_du);
1056 // guard against near-zero derivative
1057 const double gpTol = 1e-14 * std::max(1.0, std::fabs(rhom * thetaW));
1058 if (std::fabs(gp) < gpTol) { u = u_prev; return; }
1059
1060 double du = -g / gp;
1061 if (du > duMax) du = duMax;
1062 if (du < -duMax) du = -duMax;
1063
1064 u += du;
1065 if (-u <= 0.0) { u = u_prev; return; }
1066 }
1067 u = u_prev;
1068}
1069
1070// -----------------------------------------------------------------------------
1071// Brooks-Corey
1072//
1073// S_e = (alpha*psiC)^(-lambda) for alpha*psiC >= 1
1074// S_e = 1 for alpha*psiC < 1 (saturated)
1075// theta_w = thetaR + thetaSR * S_e
1076// k_rw = S_e^eta, eta = bc_eta(lambda, kr_model)
1077// psiC(S_e) = S_e^(-1/lambda) / alpha (analytic inverse)
1078//
1079// BC has a derivative discontinuity in (theta_w, k_rw) at the entry pressure
1080// alpha*psiC = 1. Newton typically needs a smoothed regularisation there in
1081// production; that is deliberately not added at this layer - callers can wrap
1082// these closures with their own smoothing if needed.
1083// -----------------------------------------------------------------------------
1084
1085// Which k_rw closure supplies the Brooks-Corey exponent. Maps onto PSK_TYPE:
1086// 1 -> burdine, 2 -> mualem. burdine is first so it is the zero value and
1087// stays the default, matching what this header did before the choice existed.
1088enum class bc_kr { burdine = 0, mualem = 1 };
1089
1090// eta(lambda) for the two closures. Kept as a named function rather than
1091// inlined into bc_wetting so the two formulas sit side by side and the caller
1092// never has to restate either one.
1093inline double bc_eta(const double lam, const bc_kr kr_model)
1094{
1095 return (kr_model == bc_kr::mualem) ? (2.5 + 2.0 / lam)
1096 : ((2.0 + 3.0 * lam) / lam);
1097}
1098
1099inline void bc_wetting(const double psiC,
1100 const double alpha,
1101 const double lam,
1102 const double thetaR,
1103 const double thetaSR,
1104 double &thetaW,
1105 double &DthetaW_DpsiC,
1106 double &KWr,
1107 double &DKWr_DpsiC,
1108 const bc_kr kr_model = bc_kr::burdine)
1109{
1110 const double thetaS = thetaR + thetaSR;
1111 if (psiC <= 0.0) {
1112 // saturated (no suction)
1113 thetaW = thetaS;
1114 DthetaW_DpsiC = 0.0;
1115 KWr = 1.0;
1116 DKWr_DpsiC = 0.0;
1117 return;
1118 }
1119 const double pcBar = alpha * psiC;
1120 if (pcBar <= 1.0) {
1121 // suction below entry pressure: still fully wetting-saturated
1122 thetaW = thetaS;
1123 DthetaW_DpsiC = 0.0;
1124 KWr = 1.0;
1125 DKWr_DpsiC = 0.0;
1126 return;
1127 }
1128 // unsaturated branch (pcBar > 1)
1129 const double Se = pow(pcBar, -lam);
1130 const double dSe_DpsiC = -lam * alpha * pow(pcBar, -lam - 1.0);
1131 thetaW = thetaR + thetaSR * Se;
1132 DthetaW_DpsiC = thetaSR * dSe_DpsiC;
1133 // k_rw = Se^eta, exponent set by the selected closure
1134 const double exp_w = bc_eta(lam, kr_model);
1135 KWr = pow(Se, exp_w);
1136 DKWr_DpsiC = exp_w * pow(Se, exp_w - 1.0) * dSe_DpsiC;
1137}
1138
1139inline void bc_invert_analytic(const double m,
1140 const double rho,
1141 const double alpha,
1142 const double lam,
1143 const double thetaR,
1144 const double thetaSR,
1145 double &u)
1146{
1147 const double thetaS = thetaR + thetaSR;
1148 const double thetaW = m / rho;
1149 // Dry limit of the inversion, expressed the same way as in bc_invert_newton:
1150 // a multiple of the entry pressure, not a fraction of thetaR. See the note
1151 // there for why 1.01*thetaR cannot be used with the BC tail.
1152 const double psiCMax_over_pd = 1.0e4;
1153 const double SeMin = pow(psiCMax_over_pd, -lam);
1154 if (thetaW > thetaR + SeMin * thetaSR && thetaW < thetaS) {
1155 const double Se = (thetaW - thetaR) / thetaSR;
1156 if (Se > 0.0 && Se < 1.0) {
1157 const double pcBar = pow(Se, -1.0 / lam);
1158 const double psiC = pcBar / alpha;
1159 u = -psiC;
1160 }
1161 } else if (thetaW >= thetaS) {
1162 if (u < -1.0 / alpha) u = -1.0 / alpha;
1163 } else {
1164 u = -psiCMax_over_pd / alpha;
1165 }
1166}
1167
1168// Inverse of the FULL forward mass m = rho(u)*theta_w(u) against the BC
1169// retention curve. Solved as a bracketed (safeguarded) Newton rather than the
1170// plain Newton vgm_invert_newton uses, for three reasons specific to BC:
1171//
1172// * Dead band. Both routines cut the dry tail at a multiple of the entry
1173// pressure rather than at a fraction of thetaR (see vgm_invert_newton for
1174// why a band in theta is the wrong variable to state it in); BC needs that
1175// the more, since its Se ~ psiC^-lambda tail is far fatter than van
1176// Genuchten's. With thetaR = 0.05, thetaSR = 0.35, p_d = 0.5 m, lambda = 2
1177// a 1.01*thetaR band would fire for every node drier than psiC ~ 13 m, where
1178// under VG (n = 1.8) it does not fire until psiC ~ 1800 m. Here the limit is
1179// enforced exactly rather than through a proxy in theta: if g at that head
1180// has not yet changed sign the root is drier than the band and u is left
1181// alone.
1182//
1183// * Entry-pressure wall. theta is flat (== thetaS) for psiC <= p_d, so the
1184// root can lie inside a region the retention curve cannot resolve. That is
1185// detected up front (g(u_wall) <= 0) and answered with the entry pressure
1186// itself -- the driest head consistent with full saturation, and the
1187// correct inverse of the mass handed in -- rather than by discarding the
1188// solve and reverting to a stale iterate carrying an unrelated theta.
1189//
1190// * Conditioning. In the dry tail the compressibility term beta*theta
1191// dominates thetaSR*dSe/du (at Se = 1e-6, beta = 1e-5 it is ~350x larger),
1192// so the root is set almost entirely by rho(u). Any seed that evaluates
1193// exp(beta*u) at a stale iterate is then wrong by orders of magnitude in
1194// Se, and unguarded Newton walks off. Bracketing removes the dependence on
1195// the seed: g is monotone increasing in u, so [u_floor, u_wall] brackets the
1196// root by construction and every iterate stays inside it.
1197inline void bc_invert_newton(const double m,
1198 const double rho,
1199 const double beta,
1200 const double alpha,
1201 const double lam,
1202 const double thetaR,
1203 const double thetaSR,
1204 double &u)
1205{
1206 if (-u <= 0.0) { return; } //ponded / saturated head: no inversion
1207
1208 // g(u) = rho(u)*theta_w(u) - m, evaluated with the BC closure (theta_w
1209 // clamped to thetaS inside the entry pressure). Monotone increasing in u.
1210 auto g_of = [&](const double uu) -> double {
1211 const double pcBar = alpha * (-uu);
1212 const double Se = (pcBar <= 1.0) ? 1.0 : pow(pcBar, -lam);
1213 return rho * std::exp(beta * uu) * (thetaR + thetaSR * Se) - m;
1214 };
1215
1216 // Upper end of the bracket: one part in 1e9 outside the entry pressure, so
1217 // the BC derivative (which drops to zero across it) stays defined.
1218 const double u_entry = -1.0 / alpha;
1219 const double u_wall = -(1.0 + 1.0e-9) / alpha;
1220 if (g_of(u_wall) <= 0.0) { u = u_entry; return; } //root at or inside the wall
1221
1222 // Lower end: the driest head the inversion is defined down to, stated as a
1223 // multiple of the entry pressure. A floor on Se instead would sit at
1224 // psiC = p_d*Se^(-1/lambda) and so swing with lambda -- Se = 1e-6 is
1225 // psiC = 1e3*p_d at lambda = 2 but 1e12*p_d at lambda = 0.5, far outside any
1226 // head the solver will ever see and wide enough to make the bracket useless.
1227 const double psiCMax_over_pd = 1.0e4;
1228 const double u_floor = -psiCMax_over_pd / alpha;
1229 if (g_of(u_floor) > 0.0) { return; } //root drier than the band: leave u alone
1230
1231 double lo = u_floor, hi = u_wall; // g(lo) <= 0 < g(hi)
1232
1233 // Analytic BC inverse (density-free) as the opening guess; the bracket
1234 // catches it if the neglected exp(beta*u) puts it in the wrong place.
1235 {
1236 const double Se = (m / rho - thetaR) / thetaSR;
1237 if (Se > 0.0 && Se < 1.0) {
1238 const double u_guess = -pow(Se, -1.0 / lam) / alpha;
1239 if (std::isfinite(u_guess) && u_guess > lo && u_guess < hi) u = u_guess;
1240 else u = 0.5 * (lo + hi);
1241 } else u = 0.5 * (lo + hi);
1242 }
1243 if (!(u > lo && u < hi)) u = 0.5 * (lo + hi);
1244
1245 // maxIts covers the bisection worst case over the widest bracket; Newton
1246 // reaches the root in a handful of steps whenever the guess is sane.
1247 const int maxIts = 100;
1248 const double tol = 1e-12 * std::max(1.0, std::fabs(m));
1249
1250 for (int it = 0; it < maxIts; ++it) {
1251 // u is strictly inside (u_floor, u_wall), so pcBar > 1 and the BC
1252 // derivative below is always the unsaturated-branch one.
1253 const double pcBar = alpha * (-u);
1254 const double Se = pow(pcBar, -lam);
1255 const double dSe_DpsiC = -lam * alpha * pow(pcBar, -lam - 1.0);
1256 const double thetaW = thetaR + thetaSR * Se;
1257 const double dtheta_du = -thetaSR * dSe_DpsiC; // d(psiC)/d(u) = -1
1258
1259 const double rhom = rho * std::exp(beta * u);
1260 const double g = rhom * thetaW - m;
1261 if (std::fabs(g) < tol) return;
1262 if (g > 0.0) hi = u; else lo = u;
1263
1264 const double gp = rhom * (beta * thetaW + dtheta_du);
1265 const double gpTol = 1e-14 * std::max(1.0, std::fabs(rhom * thetaW));
1266 double u_next = (std::fabs(gp) > gpTol) ? (u - g / gp) : 0.5 * (lo + hi);
1267 // Bisect whenever Newton leaves the bracket.
1268 if (!(u_next > lo && u_next < hi)) u_next = 0.5 * (lo + hi);
1269
1270 if (std::fabs(u_next - u) <= 1e-15 * std::fabs(u)) return;
1271 u = u_next;
1272 }
1273 // Bracketed throughout, so the last iterate is the best available answer.
1274}
1275
1276// -----------------------------------------------------------------------------
1277// Gardner (quasi-linear) retention + conductivity, with the Irmay-style linear
1278// theta_w(k_rw) pairing:
1279//
1280// S_e = exp(-alpha*psiC) = exp(alpha*u)
1281// theta_w = thetaR + thetaSR * S_e
1282// k_rw = S_e i.e. K(psi) = Ks*exp(alpha*psi)
1283// psiC(S_e) = -ln(S_e)/alpha (analytic inverse, exact)
1284//
1285// alpha here is Gardner's exponent [1/m]; it takes the same parameter slot as
1286// van Genuchten's alpha and the Brooks-Corey inverse entry pressure, and the
1287// second numeric parameter (n_vg / lambda) is unused.
1288//
1289// This is the pairing Tracy's analytical solutions are built on, and both
1290// halves of it matter: with k_rw = S_e and theta_w affine in S_e, the Kirchhoff
1291// transform hbar = exp(alpha*psi) turns Richards into a linear advection-
1292// diffusion equation for hbar, which is what makes the closed-form steady and
1293// transient solutions exist. Substituting Mualem k_rw here would keep the
1294// retention curve but destroy the linearisation, so the two are one model, not
1295// two independent choices.
1296//
1297// There is no entry pressure and no dry-end kink: theta_w and k_rw are C-inf
1298// for psiC > 0 and both approach saturation smoothly as psiC -> 0. What it
1299// does have is an unbounded dry tail -- S_e -> 0 only as psiC -> inf -- so the
1300// inversions cut at alpha*psiC = 7e2, stated as a cap on head like the vgm_/bc_
1301// routines rather than as a band in theta. That is where exp() underflows,
1302// i.e. as far out as double precision can represent the curve at all, and
1303// orders of magnitude beyond any head a solve will see.
1304//
1305// UNLIKE vgm_ and bc_, there is NO saturated branch: the exponential is
1306// continued through psiC = 0 into psiC < 0 rather than clamped to
1307// (theta_w, k_rw) = (thetaS, 1). That is deliberate and it is not cosmetic.
1308// Clamping sets DthetaW_DpsiC = 0, hence dm = drhom*thetaW, which at beta = 0
1309// is exactly zero -- and the low-order diagonal in Richards.h is
1310//
1311// globalJacobian[ii] += bc_mask[i]*(MLi*dm/dt + J_ii) + (1-bc_mask[i])
1312//
1313// where MLi*dm/dt outweighs the graph term J_ii by ~3 orders at a small dt. A
1314// free DOF that reaches psi >= 0 therefore keeps its residual row but loses its
1315// diagonal, takes a correction ~3 orders too large, falls back below zero where
1316// dm > 0 again, and overshoots once more: Newton locks into an exact period-2
1317// orbit that no tolerance or step cap will break. vgm_/bc_ carry the same
1318// clamp harmlessly because the only node pinned at psi = 0 is normally a
1319// Dirichlet node, which bc_mask = 0 replaces with an identity row; Gardner
1320// reaches psi = 0 at *free* nodes because its diffusivity is
1321// Ks/(alpha*thetaSR), constant over the whole curve and typically orders above
1322// van Genuchten's in the unsaturated range, so a sharp boundary layer drives
1323// interior nodes into saturation within one step.
1324//
1325// The continuation is the same exponential, so the closure stays C-inf and
1326// every derivative used to build the diagonal stays strictly positive. It does
1327// mean k_rw > 1 and theta_w > thetaS for psi > 0, which is unphysical -- but
1328// Gardner is a psi <= 0 model with no saturated branch to speak of, and these
1329// values are only ever visited by transient iterates on the way back down. Any
1330// case that genuinely ponds wants vgm_ or bc_, which cap properly.
1331// -----------------------------------------------------------------------------
1332
1333// Dry-end cut for the Gardner inversions, in units of alpha*psiC. exp(-7e2) is
1334// ~1e-304, one decade off denormal, so this is the widest cut that keeps S_e a
1335// normal double.
1336constexpr double gardner_alphaPsiCMax = 7.0e2;
1337
1338// Wet-end rail on the SAME exponent, so exp(alpha*u) cannot overflow if an
1339// iterate runs away. This is an arithmetic guard, not a saturation limit: at a
1340// typical alpha it sits hundreds of metres above ground, far outside any head a
1341// solve can reach without having already failed, and it is placed on the
1342// exponent (not on theta) so it can never fire near psi = 0 where the clamp
1343// above would cost the diagonal.
1344constexpr double gardner_alphaPsiMax = 5.0e1;
1345
1346inline void gardner_wetting(const double psiC,
1347 const double alpha,
1348 const double n_vg,
1349 const double thetaR,
1350 const double thetaSR,
1351 double &thetaW,
1352 double &DthetaW_DpsiC,
1353 double &KWr,
1354 double &DKWr_DpsiC)
1355{
1356 (void)n_vg; // Gardner is a one-parameter curve
1357 // One branch for every psiC. exp(-alpha*psiC) underflows to 0 past the dry
1358 // cut (theta_w -> thetaR, k_rw -> 0 with zero slope, all correct); the wet
1359 // rail below only bounds a runaway iterate.
1360 const double x = -alpha * psiC;
1361 const double Se = std::exp(x < gardner_alphaPsiMax ? x : gardner_alphaPsiMax);
1362 const double dSe_DpsiC = (x < gardner_alphaPsiMax) ? (-alpha * Se) : 0.0;
1363 thetaW = thetaR + thetaSR * Se;
1364 DthetaW_DpsiC = thetaSR * dSe_DpsiC;
1365 KWr = Se;
1366 DKWr_DpsiC = dSe_DpsiC;
1367}
1368
1369// Analytic Gardner inverse: theta_w -> psiC. Exact (the retention curve is a
1370// plain exponential), so this is the whole inversion whenever beta = 0.
1371//
1372// The admissible band is the forward curve's own range, dry cut to wet rail --
1373// NOT thetaW < thetaS. Since gardner_wetting continues the exponential above
1374// psi = 0, theta_w > thetaS is a value the forward model genuinely produces and
1375// the inverse has to be able to return the u > 0 that generated it; cutting at
1376// thetaS would leave those DOFs holding a stale iterate whose theta is
1377// unrelated to the mass handed in, which is the conservation break the vgm_/bc_
1378// dry-cut comments describe, at the other end of the curve.
1379inline void gardner_invert_analytic(const double m,
1380 const double rho,
1381 const double alpha,
1382 const double n_vg,
1383 const double thetaR,
1384 const double thetaSR,
1385 double &u)
1386{
1387 (void)n_vg;
1388 const double thetaW = m / rho;
1389 const double SeMin = std::exp(-gardner_alphaPsiCMax);
1390 const double SeMax = std::exp(gardner_alphaPsiMax);
1391 if (thetaW > thetaR + SeMin * thetaSR && thetaW < thetaR + SeMax * thetaSR) {
1392 const double Se = (thetaW - thetaR) / thetaSR;
1393 const double u_new = std::log(Se) / alpha; // = -psiC; sign follows Se vs 1
1394 if (std::isfinite(u_new)) u = u_new;
1395 }
1396}
1397
1398// Inverse of the FULL forward mass m = rho(u)*theta_w(u) against the Gardner
1399// curve, i.e. including the exp(beta*u) the analytic inverse drops.
1400//
1401// Bracketed Newton, like bc_invert_newton and for the same conditioning
1402// reason: g(u) = rho*exp(beta*u)*(thetaR + thetaSR*exp(alpha*u)) - m is a sum
1403// of increasing exponentials, so it is monotone in u and [u_floor, u_rail]
1404// brackets the root by construction. In the dry tail the beta*theta term
1405// dominates thetaSR*alpha*Se, so a plain Newton seeded from a stale iterate can
1406// walk off exactly as it does under BC; bracketing removes the dependence on
1407// the seed.
1408//
1409// Both ends of the bracket are the forward curve's own limits. There is no
1410// wall at u = 0: gardner_wetting has no saturated branch, so psi > 0 is inside
1411// the model here and a mass above rho*thetaS inverts to the positive head that
1412// produced it rather than being flattened onto zero. Nothing is refused for
1413// being "ponded" either -- an iterate that has overshot into psi > 0 is exactly
1414// the state the inversion has to be able to walk back down.
1415inline void gardner_invert_newton(const double m,
1416 const double rho,
1417 const double beta,
1418 const double alpha,
1419 const double n_vg,
1420 const double thetaR,
1421 const double thetaSR,
1422 double &u)
1423{
1424 (void)n_vg;
1425
1426 auto g_of = [&](const double uu) -> double {
1427 const double x = alpha * uu;
1428 const double Se = std::exp(x < gardner_alphaPsiMax ? x : gardner_alphaPsiMax);
1429 return rho * std::exp(beta * uu) * (thetaR + thetaSR * Se) - m;
1430 };
1431
1432 const double u_rail = gardner_alphaPsiMax / alpha;
1433 if (g_of(u_rail) <= 0.0) { u = u_rail; return; } //root at or above the rail
1434
1435 const double u_floor = -gardner_alphaPsiCMax / alpha;
1436 if (g_of(u_floor) > 0.0) { return; } //root drier than the band: leave u alone
1437
1438 double lo = u_floor, hi = u_rail; // g(lo) <= 0 < g(hi)
1439
1440 // Density-free analytic inverse as the opening guess; the bracket catches it
1441 // if the neglected exp(beta*u) puts it in the wrong place.
1442 {
1443 const double Se = (m / rho - thetaR) / thetaSR;
1444 double u_guess = 0.5 * (lo + hi);
1445 if (Se > 0.0) { // Se >= 1 is admissible now: it is the psi > 0 continuation
1446 const double u_analytic = std::log(Se) / alpha;
1447 if (std::isfinite(u_analytic) && u_analytic > lo && u_analytic < hi)
1448 u_guess = u_analytic;
1449 }
1450 u = u_guess;
1451 }
1452 if (!(u > lo && u < hi)) u = 0.5 * (lo + hi);
1453
1454 // maxIts covers the bisection worst case over the widest bracket; from the
1455 // analytic seed Newton lands in a couple of steps for any sane beta.
1456 const int maxIts = 100;
1457 const double tol = 1e-12 * std::max(1.0, std::fabs(m));
1458
1459 for (int it = 0; it < maxIts; ++it) {
1460 const double Se = std::exp(alpha * u);
1461 const double thetaW = thetaR + thetaSR * Se;
1462 const double dtheta_du = thetaSR * alpha * Se;
1463
1464 const double rhom = rho * std::exp(beta * u);
1465 const double g = rhom * thetaW - m;
1466 if (std::fabs(g) < tol) return;
1467 if (g > 0.0) hi = u; else lo = u;
1468
1469 const double gp = rhom * (beta * thetaW + dtheta_du);
1470 const double gpTol = 1e-14 * std::max(1.0, std::fabs(rhom * thetaW));
1471 double u_next = (std::fabs(gp) > gpTol) ? (u - g / gp) : 0.5 * (lo + hi);
1472 // Bisect whenever Newton leaves the bracket.
1473 if (!(u_next > lo && u_next < hi)) u_next = 0.5 * (lo + hi);
1474
1475 if (std::fabs(u_next - u) <= 1e-15 * std::fabs(u)) return;
1476 u = u_next;
1477 }
1478 // Bracketed throughout, so the last iterate is the best available answer.
1479}
1480
1481// -----------------------------------------------------------------------------
1482// Hornung-Messing manufactured law (Ait Hammou Oulhaj, Cances & Chainais-
1483// Hillairet 2018; Dielmann, Kuzmin & Kees, JNMA), written with u = -psiC:
1484//
1485// S_e = 1 - (4/pi^2) atan(u)^2, k_rw = 1/(1+u^2) for u < 0
1486// S_e = 1, k_rw = 1 for u >= 0
1487// theta_w = thetaR + thetaSR * S_e
1488//
1489// With thetaR = 0, thetaSR = pi^2/2 and K_s = 2 this is s(p) = pi^2/2 -
1490// 2 atan(p)^2 and eta(p) = 2/(1+p^2). S_e and k_rw are C^1 across u = 0 and
1491// S_e -> 0 only as u -> -infinity. alpha and n_vg are unused.
1492// -----------------------------------------------------------------------------
1493inline void hm_wetting(const double psiC,
1494 const double alpha,
1495 const double n_vg,
1496 const double thetaR,
1497 const double thetaSR,
1498 double &thetaW,
1499 double &DthetaW_DpsiC,
1500 double &KWr,
1501 double &DKWr_DpsiC)
1502{
1503 (void)alpha; (void)n_vg;
1504 if (psiC > 0.0) {
1505 const double u = -psiC;
1506 const double at = std::atan(u);
1507 const double opu2 = 1.0 + u * u;
1508 const double c = 4.0 / (M_PI * M_PI);
1509 const double Se = 1.0 - c * at * at;
1510 // d/dpsiC = -d/du
1511 thetaW = thetaR + thetaSR * Se;
1512 DthetaW_DpsiC = thetaSR * (2.0 * c * at / opu2);
1513 KWr = 1.0 / opu2;
1514 DKWr_DpsiC = 2.0 * u / (opu2 * opu2);
1515 } else {
1516 thetaW = thetaR + thetaSR;
1517 DthetaW_DpsiC = 0.0;
1518 KWr = 1.0;
1519 DKWr_DpsiC = 0.0;
1520 }
1521}
1522
1523// Analytic inverse theta_w -> u on the unsaturated branch,
1524// u = -tan((pi/2) sqrt(1 - S_e)).
1525// Same conventions as vgm_invert_analytic: at or above thetaS a negative u is
1526// moved to 0 (any u >= 0 has the same theta) and a nonnegative u is kept; below
1527// the dry cut u is set to the cut.
1528constexpr double hm_uMin = -1.0e8;
1529inline void hm_invert_analytic(const double m,
1530 const double rho,
1531 const double alpha,
1532 const double n_vg,
1533 const double thetaR,
1534 const double thetaSR,
1535 double &u)
1536{
1537 (void)alpha; (void)n_vg;
1538 const double thetaW = m / rho;
1539 const double thetaS = thetaR + thetaSR;
1540 const double c = 4.0 / (M_PI * M_PI);
1541 const double SeMin = 1.0 - c * std::atan(hm_uMin) * std::atan(hm_uMin);
1542 if (thetaW > thetaR + SeMin * thetaSR && thetaW < thetaS) {
1543 const double Se = (thetaW - thetaR) / thetaSR;
1544 const double u_new = -std::tan(0.5 * M_PI * std::sqrt(1.0 - Se));
1545 if (std::isfinite(u_new)) u = u_new;
1546 } else if (thetaW >= thetaS) {
1547 if (u < 0.0) u = 0.0;
1548 } else {
1549 u = hm_uMin;
1550 }
1551}
1552
1553// Inverse of the full mass m = rho exp(beta u) theta_w(u). g(u) is increasing
1554// on the unsaturated branch, so a bracketed Newton/bisection on
1555// [hm_uMin, 0] is safe. If m is at or above the saturated mass the head is
1556// not determined by m; then u is left alone when u >= 0 and moved to 0 otherwise.
1557inline void hm_invert_newton(const double m,
1558 const double rho,
1559 const double beta,
1560 const double alpha,
1561 const double n_vg,
1562 const double thetaR,
1563 const double thetaSR,
1564 double &u)
1565{
1566 auto g_of = [&](const double uu, double &gp) -> double {
1567 double thetaW, dth_dpsiC, kr, dkr;
1568 hm_wetting(-uu, alpha, n_vg, thetaR, thetaSR, thetaW, dth_dpsiC, kr, dkr);
1569 const double rhom = rho * std::exp(beta * uu);
1570 gp = rhom * (beta * thetaW - dth_dpsiC);
1571 return rhom * thetaW - m;
1572 };
1573 double gp;
1574 if (g_of(0.0, gp) <= 0.0) { if (u < 0.0) u = 0.0; return; } // saturated
1575 double lo = hm_uMin, hi = 0.0;
1576 if (g_of(lo, gp) > 0.0) { u = lo; return; } // drier than the cut
1577 {
1578 double ua = u;
1579 hm_invert_analytic(m * std::exp(-beta * u), rho, alpha, n_vg, thetaR, thetaSR, ua);
1580 u = (ua > lo && ua < hi) ? ua : 0.5 * (lo + hi);
1581 }
1582 const double tol = 1e-12 * std::max(1.0, std::fabs(m));
1583 for (int it = 0; it < 200; ++it) {
1584 const double g = g_of(u, gp);
1585 if (std::fabs(g) < tol) return;
1586 if (g > 0.0) hi = u; else lo = u;
1587 double u_next = (gp > 0.0) ? u - g / gp : 0.5 * (lo + hi);
1588 if (!(u_next > lo && u_next < hi)) u_next = 0.5 * (lo + hi);
1589 if (std::fabs(u_next - u) <= 1e-15 * std::max(1.0, std::fabs(u))) return;
1590 u = u_next;
1591 }
1592}
1593
1594} // namespace psk
1595} // namespace richards
1596} // namespace proteus
1597
1598namespace proteus
1599{
1600namespace m_comp_co2
1601{
1602namespace psk
1603{
1604
1605// =============================================================================
1606// Two-phase PSK closures for m_comp_co2, parameterised by effective saturation
1607// S_e rather than by suction psiC. These six routines are the only ones
1608// m_comp_co2.h calls.
1609//
1610// vgm_pc_from_Se / bc_pc_from_Se capillary curve p_c(S_e)
1611// vgm_wetting_from_Se / bc_wetting_from_Se theta_w(S_e), k_rw(S_e)
1612// vgm_kr_nonwetting_from_Se / bc_..._from_Se k_rn(S_e)
1613//
1614// The underlying constitutive models are NOT new here: the VGM branch is the
1615// same Mualem-van Genuchten curve as VGM::calc in proteus/pskRelations.h (the
1616// two forms are related by (1 - S_e^(1/m))^m = (alpha*psiC)^(n-1) * S_e), and
1617// the BC branch is Brooks-Corey-Burdine, the same as BCB::calc there. What
1618// these routines add, and pskRelations.h does not have, is the regularisation
1619// the compositional Newton solve needs: second derivatives d2p_c/dS_e2, the C1
1620// cubic-Hermite ramp across gas appearance at S_e = 1, the linear cap on the
1621// diverging p_c tail below Se_min_pc, the residual-gas remap Se_trap, and the
1622// Hermite bridge over k_rn's triple zero at S_e = 1. Unifying the two layers
1623// is post-1.9.0 work.
1624//
1625// The psiC-based closures (vgm_wetting, bc_wetting, vgm_kr_nonwetting,
1626// bc_kr_nonwetting and the four invert routines) used to be duplicated here as
1627// well. They had no call site in m_comp_co2.h and were a stale fork of the
1628// Richards versions, so they have been removed; the maintained copies live in
1629// proteus/pskRelations.h under namespace proteus::richards::psk.
1630//
1631// vgm_pc_from_Se : Inverse capillary curve, given effective saturation
1632// S_e = (theta_w - theta_R) / theta_SR (= sBar).
1633// pcBar = (S_e^(-1/m) - 1)^(1/n); pc = pcBar / alpha.
1634// Returns (pc, dpc/dSe) with dpc/dSe < 0. Both clamped to
1635// 0 at S_e >= 1 (no capillary suction in the fully-wet
1636// branch); near S_e = 0 the value diverges - clamped via
1637// a small floor so derivatives remain finite.
1638// =============================================================================
1639
1640inline void vgm_pc_from_Se(const double Se,
1641 const double alpha,
1642 const double n_vg,
1643 double &pc,
1644 double &Dpc_DSe,
1645 double &D2pc_DSe2)
1646{
1647 const double m_vg = 1.0 - 1.0 / n_vg;
1648 // Floor Se from below to keep pcBar_n bounded for Se near 0 (gas-saturated).
1649 // Raised from 1e-12 to 1e-3 to bound pc at the saturation cap; otherwise
1650 // pc reaches ~1e12/alpha at the floor and floating-point cancellation in
1651 // the antisymmetric edge flux F_ij + F_ji = 0 leaks mass on the order of
1652 // |pc|*eps_machine per edge. With the 1e-3 floor, |pc_max| ~ 1e3/alpha and
1653 // the antisymmetric roundoff drops by ~9 orders of magnitude. bc_pc_from_Se
1654 // bounds the same tail with its own cap (Se_min_pc), but the two are not
1655 // equivalent: below the floor VGM holds pc flat (Dpc_DSe = 0 via
1656 // dSeStar_dSe), whereas BC extrapolates linearly with a finite slope.
1657 const double SeStar = (Se < 1.0e-3) ? 1.0e-3 : Se;
1658 const double SemInvM = pow(SeStar, -1.0 / m_vg);
1659 const double pcBar_n_raw = SemInvM - 1.0;
1660 // ------------------------------------------------------------------------
1661 // VGM regularization: SOFT FLOOR on pcBar_n.
1662 //
1663 // pcBar_n = pcBar_n_raw + eps_pcBar
1664 //
1665 // This is C-infinity smooth across Se -> 1 (no kinks), bounds Dpc_DSe by
1666 // pcBar_n_soft^(1/n - 1) * (constant), and recovers the physical
1667 // pcBar_n_raw whenever raw >> eps_pcBar (i.e., away from full saturation).
1668 //
1669 // The earlier hard-cutoff regularization (pcBar_n = max(raw, eps)) made
1670 // Dpc_DSe C0-discontinuous at the threshold; Newton would stall when its
1671 // iterate crossed the threshold mid-step. The soft floor eliminates that.
1672 //
1673 // Cost: pc(Se = 1) = (eps_pcBar)^(1/n)/alpha ~ 0.001 instead of exactly 0.
1674 // Physically negligible (any DOF actually AT Se = 1 has zero gas flux
1675 // already because k_rn = 0 there).
1676 //
1677 // The legacy `if (Se >= 1.0)` and `pcBar_n_raw <= 0.0` early-returns are
1678 // dropped: with the soft floor, the formula is well-defined everywhere
1679 // including Se = 1 (pcBar_n_raw can dip slightly negative due to roundoff
1680 // when Se >= 1, but pcBar_n_soft remains >= eps_pcBar/2 > 0 as long as
1681 // |pcBar_n_raw| < eps_pcBar/2 which holds for Se in [1 - small, 1 + small]).
1682 // ------------------------------------------------------------------------
1683 const double eps_pcBar = 1.0e-3;
1684 // For Se >= 1 the raw value can be slightly negative due to roundoff;
1685 // clamp from below to keep the soft-floored pcBar_n strictly positive.
1686 const double pcBar_n_raw_clamped = (pcBar_n_raw > -0.5 * eps_pcBar)
1687 ? pcBar_n_raw
1688 : -0.5 * eps_pcBar;
1689 const double pcBar_n = pcBar_n_raw_clamped + eps_pcBar;
1690 const double pcBar = pow(pcBar_n, 1.0 / n_vg);
1691 pc = pcBar / alpha;
1692
1693 // First derivative -- C-infinity smooth in Se now.
1694 // d(pcBar_n)/d(Se) = d(pcBar_n_raw)/d(Se) (since eps_pcBar is constant)
1695 // = -(1/m) * Se^(-1/m - 1)
1696 // = -(1/m) * SemInvM / SeStar
1697 // d(pcBar)/d(Se) = (1/n) * pcBar_n^(1/n - 1) * d(pcBar_n)/d(Se)
1698 // For Se > 1 (numerical excursion) we hold dpcBar_n/dSe at its Se = 1 value
1699 // (which is small) -- no discontinuity introduced because pcBar_n_raw was
1700 // clamped above.
1701 const double dpcBar_n_dSe = -(1.0 / m_vg) * (SemInvM / SeStar);
1702 const double dpcBar_dSe = (1.0 / n_vg) * (pcBar / pcBar_n) * dpcBar_n_dSe;
1703 // pc is built from SeStar = max(Se, 1e-3); below the floor pc is FLAT, so the
1704 // chain rule gives d(pc)/d(Se) = d(pc)/d(SeStar) * d(SeStar)/d(Se) with
1705 // d(SeStar)/d(Se) = 0 for Se < 1e-3. Without this factor pc(Se) is flat but
1706 // Dpc_DSe is nonzero -> residual/Jacobian inconsistency at gas-saturated nodes
1707 // (Se_a -> 0), which stalls Newton near the saturation ceiling.
1708 const double dSeStar_dSe = (Se < 1.0e-3) ? 0.0 : 1.0;
1709 Dpc_DSe = (dpcBar_dSe / alpha) * dSeStar_dSe;
1710
1711 // Second derivative (gas-eq (1,1) capillary sensitivity), also C-infinity.
1712 // d2(pcBar_n)/d(Se)2 = (1/m)*(1/m + 1) * Se^(-1/m - 2)
1713 // = (1/m)*(1/m + 1) * SemInvM / Se^2
1714 // d2(pcBar)/d(Se)2 = (1/n) * [(1/n - 1) * pcBar_n^(1/n - 2) * (d pcBar_n / dSe)^2
1715 // + pcBar_n^(1/n - 1) * d2(pcBar_n)/d(Se)2]
1716 const double inv_m = 1.0 / m_vg;
1717 const double inv_n = 1.0 / n_vg;
1718 const double d2pcBar_n_dSe2 = inv_m * (inv_m + 1.0) * (SemInvM / (SeStar * SeStar));
1719 const double pcBar_n_pow_a = pow(pcBar_n, inv_n - 2.0); // pcBar_n^(1/n - 2)
1720 const double pcBar_n_pow_b = pow(pcBar_n, inv_n - 1.0); // pcBar_n^(1/n - 1)
1721 const double d2pcBar_dSe2 = inv_n * ((inv_n - 1.0) * pcBar_n_pow_a * dpcBar_n_dSe * dpcBar_n_dSe
1722 + pcBar_n_pow_b * d2pcBar_n_dSe2);
1723 D2pc_DSe2 = (d2pcBar_dSe2 / alpha) * dSeStar_dSe; // 0 below the Se floor (flat pc)
1724}
1725
1726// =============================================================================
1727// Brooks-Corey (Burdine relative-permeability) closures.
1728// Parallel API to the vgm_* functions: same parameter slots, same output args.
1729// In the BC parameterisation the second numeric parameter is the pore-size
1730// index lambda (taking the slot of vgm's n_vg). alpha = 1/p_d is the inverse
1731// entry-pressure head.
1732//
1733// S_e = (alpha * p_c)^(-lambda) for alpha*p_c >= 1
1734// S_e = 1 for alpha*p_c < 1 (saturated)
1735// theta_w = theta_R + theta_SR * S_e
1736// k_rw = S_e^((2+3*lambda)/lambda) (Burdine)
1737// k_rn = (1-S_e)^2 * (1 - S_e^((2+lambda)/lambda))
1738// p_c(S_e) = S_e^(-1/lambda) / alpha (analytic inverse)
1739//
1740// BC has a discontinuity in (k_rw, k_rn, theta_w) derivatives at p_c = p_d
1741// (alpha*p_c = 1), i.e. at S_e = 1. Unlike the raw model, the routines below
1742// regularise it: bc_pc_from_Se carries the C1 Hermite ramp across S_e = 1 and
1743// the linear cap below Se_min_pc, and bc_kr_nonwetting_from_Se bridges k_rn's
1744// triple zero at S_e = 1. Each is documented at its own definition.
1745// =============================================================================
1746
1747// Below Se_min_pc bc_pc_from_Se continues the BC tangent linearly (gas-saturated
1748// cap); bc_Se_from_pc inverts the same curve, so both take the same per-rock
1749// Se_min_pc (m_comp_co2.py Coefficients.bc_Se_min_types sets it).
1750
1751inline void bc_pc_from_Se(const double Se,
1752 const double alpha,
1753 const double lam,
1754 const double Se_min_pc,
1755 double &pc,
1756 double &Dpc_DSe,
1757 double &D2pc_DSe2)
1758{
1759 // ----------------------------------------------------------------------
1760 // Brooks-Corey p_c(S_e) with smoothed transition out of the physical range
1761 // (so Newton can move through Se = 1 without seeing a value-jump).
1762 //
1763 // Se in [0, 1): pc = (1/alpha) * Se^(-1/lambda) (standard BC)
1764 // At Se -> 1: pc -> 1/alpha (the "entry pressure"
1765 // p_d). At Se -> 0: pc -> +infinity (asymptotic).
1766 //
1767 // Se in [1, 1+delta]: pc smoothly ramps from 1/alpha down to 0 using a
1768 // cubic Hermite smoothstep phi(t) = 1 - 3t^2 + 2t^3
1769 // in t = (Se - 1)/delta. phi(0) = 1, phi(1) = 0,
1770 // phi'(0) = phi'(1) = 0.
1771 //
1772 // Se > 1 + delta: pc = 0.
1773 //
1774 // Value continuity at Se = 1: pc(1-) = pc(1+) = 1/alpha. ENTRY PRESSURE
1775 // PRESERVED. The previous "if (Se >= 1.0) { pc = 0 }" cutoff produced a
1776 // finite jump of size 1/alpha at Se = 1 and stalled Newton whenever an
1777 // iterate crossed this threshold.
1778 //
1779 // C1 at Se = 1: the Se >= 1 ramp's left slope is matched to the BC branch
1780 // slope -(1/lambda)/alpha (see that branch), so dpc/dSe is now continuous
1781 // across gas appearance. (Previously the ramp left with slope 0, leaving a
1782 // slope kink that mispredicted Newton steps overshooting across Se = 1.)
1783 // ----------------------------------------------------------------------
1784 const double delta_smooth = 5.0e-2;
1785
1786 if (Se >= 1.0 + delta_smooth) {
1787 pc = 0.0;
1788 Dpc_DSe = 0.0;
1789 D2pc_DSe2 = 0.0;
1790 return;
1791 }
1792
1793 if (Se >= 1.0) {
1794 // Non-physical overshoot region (S_g < 0, only reached when a Newton iterate
1795 // crosses gas appearance from above). Cubic-Hermite ramp from pc(1) = 1/alpha
1796 // (the Brooks-Corey entry pressure p_d) down to pc(1+delta) = 0.
1797 //
1798 // C1 ACROSS GAS APPEARANCE: the LEFT-end slope is matched to the BC branch
1799 // slope at Se = 1, dpc/dSe = -1/(alpha*lam) (see the Se in [Se_min_pc,1)
1800 // branch below). The earlier ramp used phi'(0) = 0, leaving a slope kink at
1801 // Se = 1 (BC side -p_d/lam vs ramp side 0); a Newton iterate overshooting
1802 // across Se = 1 then saw an inconsistent linearization and mispredicted the
1803 // step. Right end keeps value 0 and slope 0 so pc joins the pc = 0 region C1.
1804 //
1805 // Hermite basis on t = (Se - 1)/delta in [0,1] with endpoints
1806 // left (t=0): value p_d = 1/alpha, slope_t m0_t = (-1/(alpha*lam))*delta
1807 // right (t=1): value 0, slope_t 0
1808 // so only H00 (value) and H10 (left slope) contribute.
1809 const double inv_alpha = 1.0 / alpha;
1810 const double m0_t = (-inv_alpha / lam) * delta_smooth; // BC slope*delta at Se=1
1811 const double t = (Se - 1.0) / delta_smooth;
1812 const double t2 = t * t, t3 = t2 * t;
1813 const double H00 = 2.0 * t3 - 3.0 * t2 + 1.0;
1814 const double H10 = t3 - 2.0 * t2 + t;
1815 const double dH00 = 6.0 * t2 - 6.0 * t;
1816 const double dH10 = 3.0 * t2 - 4.0 * t + 1.0;
1817 const double d2H00 = 12.0 * t - 6.0;
1818 const double d2H10 = 6.0 * t - 4.0;
1819 pc = H00 * inv_alpha + H10 * m0_t;
1820 Dpc_DSe = (dH00 * inv_alpha + dH10 * m0_t) / delta_smooth;
1821 D2pc_DSe2 = (d2H00 * inv_alpha + d2H10 * m0_t) / (delta_smooth * delta_smooth);
1822 return;
1823 }
1824
1825 // ----------------------------------------------------------------------
1826 // Gas-saturated cap. BC's pc and dpc/dSe diverge as Se -> 0 (i.e.
1827 // S_n -> 1 - S_wr). In the FluidFlower disk-injection case this kills
1828 // Newton once the disk fills toward the saturation ceiling -- the
1829 // Jacobian sees |dpc/dSe| ~ 1e2 .. 1e8 m/unit and the S_n step blows up.
1830 //
1831 // Fix: below Se_min_pc, freeze the BC tangent so pc and dpc/dSe stay
1832 // finite and the Jacobian stays bounded. C^1 continuous at Se_min_pc
1833 // (value + slope match by construction). d2pc/dSe2 has a finite jump
1834 // at the seam, which Newton tolerates (only pc and dpc/dSe enter the
1835 // residual and Jacobian). Standard "Pc cap" trick from reservoir
1836 // simulation; doesn't change pc above Se_min_pc, so undisturbed in
1837 // normal operation.
1838 //
1839 // Tuning Se_min_pc: smaller = sharper cap (closer to true BC), larger
1840 // = more robust Newton but more physics distortion near the gas-sat
1841 // endpoint. The cap value is pc(0) = pc(Se_min_pc) * (1 + 1/lambda).
1842 // Across rock interfaces every rock's pc(0) must exceed the entry
1843 // pressures of its neighbours, otherwise a coarse rock saturates at
1844 // S_e = 0 before the fine rock's entry pressure is reached and the
1845 // per-rock closure can only store further gas in the fine rock; the
1846 // caller therefore passes a per-rock Se_min_pc.
1847 // ----------------------------------------------------------------------
1848 if (Se < Se_min_pc) {
1849 const double SemInvLam_min = pow(Se_min_pc, -1.0 / lam);
1850 const double pc_min = SemInvLam_min / alpha;
1851 const double slope_min = -(SemInvLam_min / Se_min_pc) / (alpha * lam);
1852 pc = pc_min + slope_min * (Se - Se_min_pc);
1853 Dpc_DSe = slope_min;
1854 D2pc_DSe2 = 0.0;
1855 return;
1856 }
1857
1858 // Standard BC in the physical range Se in [Se_min_pc, 1).
1859 const double SemInvLam = pow(Se, -1.0 / lam);
1860 pc = SemInvLam / alpha;
1861 // dpc/dS_e = -(1/(alpha*lambda)) * S_e^(-1/lambda - 1)
1862 // = -(1/(alpha*lambda)) * SemInvLam / S_e
1863 Dpc_DSe = -(SemInvLam / Se) / (alpha * lam);
1864 // d2pc/dS_e2 = (1/(alpha*lambda)) * (1/lambda + 1) * S_e^(-1/lambda - 2)
1865 // = (1/(alpha*lambda)) * (1/lambda + 1) * SemInvLam / S_e^2
1866 const double inv_lam = 1.0 / lam;
1867 D2pc_DSe2 = inv_lam * (inv_lam + 1.0) * (SemInvLam / (Se * Se)) / alpha;
1868}
1869
1870// Exact inverse of bc_pc_from_Se on S_e in [0, 1], the range the m_comp_co2
1871// kernel calls it on (it clamps S_e first): S_e(pc) and dS_e/dpc.
1872// pc <= p_d S_e = 1 (no gas)
1873// p_d < pc <= pc(Se_min_pc) S_e = (alpha pc)^(-lam) (Brooks-Corey)
1874// up to pc(0) linear cap of bc_pc_from_Se
1875// beyond S_e = 0
1876// Used by the per-rock nodal closure (Z_ACROSS_ROCK_INTERFACES.md Sec. 3.2).
1877inline void bc_Se_from_pc(const double pc,
1878 const double alpha,
1879 const double lam,
1880 const double Se_min_pc,
1881 double &Se,
1882 double &DSe_Dpc)
1883{
1884 const double p_d = 1.0 / alpha;
1885 const double s_min = pow(Se_min_pc, -1.0 / lam);
1886 const double pc_min = s_min / alpha; // pc(Se_min_pc)
1887 const double slope_min = -(s_min / Se_min_pc) / (alpha * lam); // dpc/dSe there
1888 const double pc_0 = pc_min - slope_min * Se_min_pc; // pc(0)
1889 if (pc <= p_d) {
1890 Se = 1.0;
1891 DSe_Dpc = 0.0;
1892 } else if (pc <= pc_min) {
1893 Se = pow(alpha * pc, -lam);
1894 DSe_Dpc = -lam * Se / pc;
1895 } else if (pc < pc_0) {
1896 Se = Se_min_pc + (pc - pc_min) / slope_min;
1897 DSe_Dpc = 1.0 / slope_min;
1898 } else {
1899 Se = 0.0;
1900 DSe_Dpc = 0.0;
1901 }
1902}
1903
1904// =============================================================================
1905// theta_w(Se) and k_r(Se) (Phase B Step 3a). Same physics as the psiC-based
1906// closures in proteus/pskRelations.h, but parameterised directly by effective
1907// saturation Se. Used by the formulation-(psi_w, S_w) wetting equation, where
1908// saturation is the primary variable rather than something inverted from psiC.
1909//
1910// Conventions:
1911// Se in [0, 1] is the effective saturation = (theta_w - theta_R)/theta_SR.
1912// In formulation B, u_v IS S_w; if a residual saturation theta_R/theta_S is
1913// in play the caller must convert before calling these.
1914// Outputs are derivatives with respect to Se (NOT psiC).
1915// =============================================================================
1916
1917inline void vgm_wetting_from_Se(const double Se,
1918 const double /*alpha*/, // unused, kept for API parity
1919 const double n_vg,
1920 const double thetaR,
1921 const double thetaSR,
1922 double &thetaW,
1923 double &DthetaW_DSe,
1924 double &KWr,
1925 double &DKWr_DSe)
1926{
1927 const double m_vg = 1.0 - 1.0 / n_vg;
1928 thetaW = thetaR + thetaSR * Se;
1929 DthetaW_DSe = thetaSR;
1930 if (Se >= 1.0) {
1931 KWr = 1.0;
1932 DKWr_DSe = 0.0;
1933 return;
1934 }
1935 if (Se <= 0.0) {
1936 KWr = 0.0;
1937 DKWr_DSe = 0.0;
1938 return;
1939 }
1940 // Floor for the powers near the endpoints.
1941 const double SeStar = (Se < 1.0e-12) ? 1.0e-12 : Se;
1942 const double SeInvM = pow(SeStar, 1.0 / m_vg); // Se^(1/m)
1943 const double x_ = 1.0 - SeInvM; // 1 - Se^(1/m)
1944 const double xStar = (x_ < 1.0e-12) ? 1.0e-12 : x_;
1945 const double y_ = pow(xStar, m_vg); // (1 - Se^(1/m))^m
1946 const double term = 1.0 - y_; // 1 - (1 - Se^(1/m))^m
1947 const double sqrtSe = sqrt(SeStar);
1948 const double sqrtSe_safe = (sqrtSe < 1.0e-12) ? 1.0e-12 : sqrtSe;
1949 // Mualem k_rw = sqrt(Se) * [1 - (1 - Se^(1/m))^m]^2
1950 KWr = sqrtSe * term * term;
1951 // dterm/dSe = x^(m-1) * Se^(1/m - 1) (chain rule through y = x^m, x = 1 - Se^(1/m))
1952 const double Dterm_DSe = pow(xStar, m_vg - 1.0) * pow(SeStar, 1.0 / m_vg - 1.0);
1953 DKWr_DSe = (0.5 / sqrtSe_safe) * term * term + 2.0 * sqrtSe * term * Dterm_DSe;
1954}
1955
1956inline void vgm_kr_nonwetting_from_Se(const double Se_w,
1957 const double /*alpha*/,
1958 const double n_vg,
1959 double &KNr,
1960 double &DKNr_DSe,
1961 const double Se_trap = 1.0)
1962{
1963 // Gas-only residual trapping: see bc_kr_nonwetting_from_Se. Remap the
1964 // wetting Se_w over the mobile range [0,Se_trap]; k_rn=0 for S_g<=S_gr.
1965 // Se_trap=1 -> original. DKNr_DSe is d k_rn/d Se_w (1/Se_trap folded in).
1966 const double inv_trap = 1.0 / Se_trap;
1967 const double Se = Se_w * inv_trap;
1968 const double m_vg = 1.0 - 1.0 / n_vg;
1969 if (Se >= 1.0) {
1970 KNr = 0.0;
1971 DKNr_DSe = 0.0;
1972 return;
1973 }
1974 if (Se <= 0.0) {
1975 // S_n = 1 -> k_rn = 1 in the limit; derivative degenerate.
1976 KNr = 1.0;
1977 DKNr_DSe = 0.0;
1978 return;
1979 }
1980 const double SeStar = (Se < 1.0e-12) ? 1.0e-12 : Se;
1981 const double SeInvM = pow(SeStar, 1.0 / m_vg);
1982 const double x_ = 1.0 - SeInvM;
1983 const double xStar = (x_ < 1.0e-12) ? 1.0e-12 : x_;
1984 // Mualem k_rn = sqrt(1 - Se) * (1 - Se^(1/m))^(2m)
1985 const double y2m = pow(xStar, 2.0 * m_vg);
1986 const double oneMinusSe = 1.0 - Se;
1987 const double oneMinusSeStar = (oneMinusSe < 1.0e-12) ? 1.0e-12 : oneMinusSe;
1988 const double sqrt_oms = sqrt(oneMinusSeStar);
1989 KNr = sqrt_oms * y2m;
1990 // d(y^(2m))/dSe = 2m * x^(2m - 1) * dx/dSe = -2 * x^(2m - 1) * Se^(1/m - 1)
1991 const double Dy2m_DSe = -2.0 * pow(xStar, 2.0 * m_vg - 1.0) * pow(SeStar, 1.0 / m_vg - 1.0);
1992 DKNr_DSe = ((-0.5 / sqrt_oms) * y2m + sqrt_oms * Dy2m_DSe) * inv_trap;
1993}
1994
1995inline void bc_wetting_from_Se(const double Se,
1996 const double /*alpha*/,
1997 const double lam,
1998 const double thetaR,
1999 const double thetaSR,
2000 double &thetaW,
2001 double &DthetaW_DSe,
2002 double &KWr,
2003 double &DKWr_DSe)
2004{
2005 thetaW = thetaR + thetaSR * Se;
2006 DthetaW_DSe = thetaSR;
2007 if (Se >= 1.0) {
2008 // Saturated branch; BC has a one-sided derivative jump here.
2009 KWr = 1.0;
2010 DKWr_DSe = 0.0;
2011 return;
2012 }
2013 if (Se <= 0.0) {
2014 KWr = 0.0;
2015 DKWr_DSe = 0.0;
2016 return;
2017 }
2018 // Burdine k_rw = Se^((2 + 3*lambda)/lambda)
2019 const double exp_w = (2.0 + 3.0 * lam) / lam;
2020 KWr = pow(Se, exp_w);
2021 DKWr_DSe = exp_w * pow(Se, exp_w - 1.0);
2022}
2023
2024inline void bc_kr_nonwetting_from_Se(const double Se_w,
2025 const double /*alpha*/,
2026 const double lam,
2027 double &KNr,
2028 double &DKNr_DSe,
2029 const double Se_trap = 1.0)
2030{
2031 // ---------------------------------------------------------------------
2032 // Gas-only residual trapping (Jun 2026): remap the wetting effective
2033 // saturation Se_w onto [0,1] over the MOBILE gas range Se_w in [0,Se_trap],
2034 // Se_trap = (1 - S_wr - S_gr)/(1 - S_wr) = 1 - S_gr/(1 - S_wr).
2035 // Gas is immobile (k_rn = 0) for Se_w >= Se_trap, i.e. S_g <= S_gr.
2036 // Se_trap = 1.0 recovers the original no-trapping closure. Only k_rn is
2037 // remapped here (gas-only) -- k_rw and p_c keep the drainage Se_w. The
2038 // returned DKNr_DSe is d k_rn / d Se_w (the 1/Se_trap chain factor is folded
2039 // in), so every caller's existing dkrn*dSe_dp chain stays correct unchanged.
2040 const double inv_trap = 1.0 / Se_trap;
2041 const double Se = Se_w * inv_trap;
2042 // ---------------------------------------------------------------------
2043 // Option A regularisation (May 2026): cubic-Hermite endpoint bridge for
2044 // k_rn near the wet endpoint Se = 1.
2045 //
2046 // Burdine k_rn = (1 - Se)^2 * (1 - Se^((2+lambda)/lambda)) has a TRIPLE
2047 // zero at Se = 1 (value, 1st, and 2nd derivative all vanish). Newton
2048 // cannot predict gas-mobile breakthrough from local linearisation: when
2049 // its trial step lands at Se = 1 - epsilon, the residual jumps and the
2050 // step direction was wrong. This is the FluidFlower disk-injection
2051 // breakthrough stall (k_rn turning on at t ~ 47 s under benchmark
2052 // conditions).
2053 //
2054 // Fix: on a narrow window Se in [Se_a, 1] with Se_a = 1 - delta_brn,
2055 // replace BC by a cubic Hermite that matches BC value+slope at Se = Se_a
2056 // and ends at (k_rn = 0, dk_rn/dSe = -s_min_brn) at Se = 1. Bulk physics
2057 // (Se in [0, Se_a]) is pure Brooks-Corey.
2058 //
2059 // Tuning:
2060 // delta_brn = 5e-2: bridge spans 5% of the S_e range
2061 // s_min_brn = 1e-3: |slope| at Se = 1; controls how much "advance
2062 // warning" Newton gets about k_rn turning on. Larger ->
2063 // earlier breakthrough prediction, more distortion in the
2064 // bridge. At Se = 1-delta the bridge value is ~21% above
2065 // pure BC k_rn (negligible compared to absolute magnitudes
2066 // here ~1e-4).
2067 //
2068 // For Se >= 1 the value is identically 0, so the derivative is 0. With
2069 // residual trapping (Se_trap < 1) this branch is the physical trapped range
2070 // 0 < S_g <= S_gr, not only Newton overshoot; a nonzero slope here would put
2071 // a gas-mobility derivative in the Jacobian that the residual does not have.
2072 // ---------------------------------------------------------------------
2073 const double delta_brn = 5.0e-2;
2074 const double s_min_brn = 1.0e-3;
2075 const double Se_a = 1.0 - delta_brn;
2076
2077 if (Se <= 0.0) {
2078 // S_n = 1 - S_wr (gas-saturated): non-wetting endpoint.
2079 KNr = 1.0;
2080 DKNr_DSe = 0.0;
2081 return;
2082 }
2083 if (Se >= 1.0) {
2084 // Wet endpoint and trapped range: k_rn = 0 and its derivative is 0.
2085 KNr = 0.0;
2086 DKNr_DSe = 0.0;
2087 return;
2088 }
2089
2090 const double exp_n = (2.0 + lam) / lam;
2091
2092 if (Se < Se_a) {
2093 // Bulk Brooks-Corey (Burdine) k_rn = (1 - Se)^2 * (1 - Se^((2+lambda)/lambda))
2094 const double SeExp = pow(Se, exp_n);
2095 const double oneMinusSe = 1.0 - Se;
2096 const double Y = 1.0 - SeExp;
2097 KNr = oneMinusSe * oneMinusSe * Y;
2098 DKNr_DSe = (-2.0 * oneMinusSe * Y
2099 - oneMinusSe * oneMinusSe * exp_n * pow(Se, exp_n - 1.0)) * inv_trap;
2100 return;
2101 }
2102
2103 // Cubic-Hermite bridge on [Se_a, 1].
2104 // Anchors:
2105 // left (t=0): k_rn = fa = k_rn_BC(Se_a), dk/dSe = da = dk_rn_BC/dSe(Se_a)
2106 // right (t=1): k_rn = 0, dk/dSe = -s_min_brn
2107 // Parameterised by t = (Se - Se_a)/delta_brn in [0, 1].
2108 const double SeExp_a = pow(Se_a, exp_n);
2109 const double oneMinusSe_a = 1.0 - Se_a;
2110 const double Y_a = 1.0 - SeExp_a;
2111 const double fa = oneMinusSe_a * oneMinusSe_a * Y_a;
2112 const double da = -2.0 * oneMinusSe_a * Y_a
2113 - oneMinusSe_a * oneMinusSe_a * exp_n * pow(Se_a, exp_n - 1.0);
2114
2115 const double t = (Se - Se_a) / delta_brn;
2116 const double t2 = t * t;
2117 const double t3 = t2 * t;
2118
2119 // Hermite basis (only H00, H10, H11 contribute since fb = 0).
2120 const double H00 = 2.0 * t3 - 3.0 * t2 + 1.0;
2121 const double H10 = t3 - 2.0 * t2 + t;
2122 const double H11 = t3 - t2;
2123 const double dH00 = 6.0 * t2 - 6.0 * t;
2124 const double dH10 = 3.0 * t2 - 4.0 * t + 1.0;
2125 const double dH11 = 3.0 * t2 - 2.0 * t;
2126
2127 KNr = H00 * fa + H10 * delta_brn * da + H11 * delta_brn * (-s_min_brn);
2128 DKNr_DSe = (dH00 * fa + dH10 * delta_brn * da
2129 + dH11 * delta_brn * (-s_min_brn)) / delta_brn * inv_trap;
2130}
2131
2132} // namespace psk
2133} // namespace m_comp_co2
2134} // namespace proteus
2135
2137#endif
Double r
Definition Headers.h:83
Double u
Definition Headers.h:89
Double * Y
Definition Headers.h:48
void piecewiseLinearTableLookup(double x, int nv, int *start, double *y, double *dy, const double *xv, const double *yv)
BCB(const double *rwork, const int *iwork=0)
void calc(const double &Sw)
double lambda
void calc(const double &Sw)
double pd
void setParams(const double *rwork, const int *iwork=0)
BCM(const double *rwork, const int *iwork=0)
void calc(const PskRelation &psk, const DensityRelation &density_w, const DensityRelation &density_n)
FractionalFlowVariables(double muwIn, double munIn)
void calc(const double &Sw)
virtual void setParams(const double *rwork, const int *iwork=0)
PskRelation(const double *rwork, const int *iwork=0)
double dSe_dpsic
double dSe_dSw
double Sw_min
virtual ~PskRelation()
double Sw_max
virtual void setTolerances(const double *rwork_tol)
virtual void calc_from_psic(const double &psicIn)
void calc_Se(const double &Sw)
virtual ~PskSpline()
virtual void calc_from_psic(const double &psicIn)
PskSpline(const double *rworkIn, const int *iworkIn=0)
virtual void calc(const double &Sw)
const double * splineArray
virtual void setParams(const double *rwork, const int *iwork=0)
void calc(const double &Sw)
virtual void calc_from_psic(const double &psicIn)
SimplePSK(const double *rwork, const int *iwork=0)
void calc(const double &Sw)
VGB(const double *rwork, const int *iwork=0)
virtual void setTolerances(const double *rwork_tol)
void calc(const double &Sw)
virtual void calc_from_psic(const double &psicIn)
double eps_small
double sqrt_eps_small
VGM(const VGM &r)
VGM(const double *rwork, const int *iwork=0)
double ns_del
void calc_Se(const double &Sw)
double n
void calc_Se_eps(const double &Se)
double Se_eps_const
double Se_eps
void setParams(const double *rwork, const int *iwork=0)
double alpha
void calc(const double &Sw)
virtual void setTolerances(const double *rwork_tol)
VGMorig(const double *rwork, const int *iwork=0)
double m
#define max(a, b)
#define c(i)
Definition jf.h:21
#define min(a, b)
Definition jf.h:71
void vgm_kr_nonwetting_from_Se(const double Se_w, const double, const double n_vg, double &KNr, double &DKNr_DSe, const double Se_trap=1.0)
void bc_pc_from_Se(const double Se, const double alpha, const double lam, const double Se_min_pc, double &pc, double &Dpc_DSe, double &D2pc_DSe2)
void bc_wetting_from_Se(const double Se, const double, const double lam, const double thetaR, const double thetaSR, double &thetaW, double &DthetaW_DSe, double &KWr, double &DKWr_DSe)
void bc_kr_nonwetting_from_Se(const double Se_w, const double, const double lam, double &KNr, double &DKNr_DSe, const double Se_trap=1.0)
void bc_Se_from_pc(const double pc, const double alpha, const double lam, const double Se_min_pc, double &Se, double &DSe_Dpc)
void vgm_pc_from_Se(const double Se, const double alpha, const double n_vg, double &pc, double &Dpc_DSe, double &D2pc_DSe2)
void vgm_wetting_from_Se(const double Se, const double, const double n_vg, const double thetaR, const double thetaSR, double &thetaW, double &DthetaW_DSe, double &KWr, double &DKWr_DSe)
void vgm_invert_analytic(const double m, const double rho, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
void gardner_invert_analytic(const double m, const double rho, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
constexpr double gardner_alphaPsiCMax
void bc_wetting(const double psiC, const double alpha, const double lam, const double thetaR, const double thetaSR, double &thetaW, double &DthetaW_DpsiC, double &KWr, double &DKWr_DpsiC, const bc_kr kr_model=bc_kr::burdine)
double bc_eta(const double lam, const bc_kr kr_model)
void vgm_wetting(const double psiC, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &thetaW, double &DthetaW_DpsiC, double &KWr, double &DKWr_DpsiC)
constexpr double gardner_alphaPsiMax
void bc_invert_newton(const double m, const double rho, const double beta, const double alpha, const double lam, const double thetaR, const double thetaSR, double &u)
constexpr double hm_uMin
void gardner_invert_newton(const double m, const double rho, const double beta, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
void vgm_invert_newton(const double m, const double rho, const double beta, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
void hm_wetting(const double psiC, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &thetaW, double &DthetaW_DpsiC, double &KWr, double &DKWr_DpsiC)
void hm_invert_newton(const double m, const double rho, const double beta, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
void gardner_wetting(const double psiC, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &thetaW, double &DthetaW_DpsiC, double &KWr, double &DKWr_DpsiC)
void bc_invert_analytic(const double m, const double rho, const double alpha, const double lam, const double thetaR, const double thetaSR, double &u)
void hm_invert_analytic(const double m, const double rho, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
Definition ADR.h:21
CompressibleN_FractionalFlowVariables(double muwIn, double munIn)
void calc(const PskRelation &psk, const DensityRelation &density_w, const DensityRelation &density_n)