-
Notifications
You must be signed in to change notification settings - Fork 1
/
van_genuchten.h
663 lines (480 loc) · 14.9 KB
/
van_genuchten.h
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
* richards.h
*
* Created on: 21.12.2019
* Author: anaegel
*/
#ifndef RICHARDS__VAN_GENUCHTEN_H_
#define RICHARDS__VAN_GENUCHTEN_H_
// Standard lib.
#include <cmath>
// Use ADOL-C for automatic differentiation.
// #include <adolc/adtl.h>
// Use autodiff for automatic differentiation.
#include <autodiff/forward/dual.hpp>
// UG4 lib.
#include "common/util/smart_pointer.h"
#include "common/assert.h"
#include "registry/class.h"
// My libs.
#include <nlohmann/json.hpp>
#include "json_basics.hh"
namespace ug{
namespace Richards{
typedef autodiff::dual dual;
// const double one = 1.0;
inline void UGCheckValues(const dual &number)
{
if (!std::isfinite(number.val) || std::isnan(number.val))
{
std::cerr << "WARNING: Invalid value:" << number.val << std::endl;
UG_ASSERT(std::isfinite(number.val), "Function is not bounded");
}
if (!std::isfinite(number.grad) || std::isnan(number.grad))
{
std::cerr << "WARNING: Invalid value:" << number.grad << std::endl;
UG_ASSERT(std::isfinite(number.grad), "Derivative is not bounded");
}
}
// Parameters for a van Genuchten model.
struct VanGenuchtenParameters
{
double alpha=1.0;
double n=2.0;
double m=0.5; // default: 1.0 - (1.0/n);}
double thetaR=0.0; // default: 0.0
double thetaS=1.0; // default: 1.0
double Ksat=1.0; // saturated conductivity K_s (optional)
};
#ifdef UG_JSON
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(VanGenuchtenParameters, alpha, n, m, thetaS, thetaR, Ksat);
#endif
#ifdef UG_JSON
/// Some JSON
void CreateJSONMap(const JSONType &array, std::map<std::string, JSONType> &map);
#endif
/*
class BrooksCorey
{
public:
BrooksCorrey()
: m_pd(1.0), m_lambda(1.0)
{}
double capillary_pressure(double Sw_eff)
{
return pow(m_pd*Sw_eff, 1.0/m_lambda);
}
protected:
double m_pd; // entry pressure
double m_lambda; // ~pore size distribution
};
*/
//! This is the interface for a Richards-type model. All derived classes use CRTP for evaluation.
/*! Two non-dimensional quantities are returned
* - Saturation $ 0 \le S(\psi) \le 1$
* - Relative permeability 0 \le k_r(\psi) \le 1
*
* For convenience, we also return
* - Conductitivity K = K_s *k_r(\psi)
*/
template <typename TDerived>
class IRichardsModel
{
protected:
// CRTP functions.
const TDerived* me() const
{ return static_cast<const TDerived*>(this); }
TDerived* me()
{ return static_cast<TDerived*>(this); }
template <typename TFunc>
void get_value_and_deriv(TFunc F, double H, double &f, double &df) const
{
dual h= H;
/*f = F(h).val; df = derivative(F, wrt(h), at(h));*/
auto [F0, Df0] = derivatives(F, wrt(h), at(h));
f = F0; df = Df0;
};
public:
//! Calls Saturation_
void get_saturation(double H, double &S, double &dSdH) const
{
auto sat = [this](dual h){ return me()->Saturation_(h);};
get_value_and_deriv(sat, H, S, dSdH);
}
void get_saturations(const double *H, double *S, double *dSdH, size_t n) const
{
for (size_t i=0; i<n; ++i)
{ this->get_saturation(H[i], S[i], dSdH[i]); }
}
//! Calls RelativePermeability_
void get_relative_permeability(double H, double &Kr, double &dKrdH) const
{
auto perm = [this](dual h){ return me()->RelativePermeability_(h);};
get_value_and_deriv(perm, H, Kr, dKrdH);
}
void get_relative_permeabilities(const double *H, double *K, double *dKdH, size_t n) const
{
for (size_t i=0; i<n; ++i)
{ this->get_relative_permeability(H[i], K[i], dKdH[i]); }
}
//! Calls Conductivity_
void get_conductivity(double H, double &K, double &dKdH) const
{
auto cond = [this](dual h){ return me()->Conductivity_(h);};
get_value_and_deriv(cond, H, K, dKdH);
}
void get_conductivities(const double *H, double *K, double *dKdH, size_t n) const
{
for (size_t i=0; i<n; ++i)
{ this->get_conductivity(H[i], K[i], dKdH[i]); }
}
public: // The following functions can be used from LUA
// Saturation
double Saturation(double H) const
{
dual h=H;
auto func = [this](dual h){ return me()->Saturation_(h);};
return func(h).val;
}
double dSaturation_dH(double H) const
{
dual h=H;
auto func = [this](dual h){ return me()->Saturation_(h);};
return derivative(func, wrt(h), at(h));
}
/// Conductivity K = Ks*kr
double Conductivity(double H)
{
dual h=H;
auto func = [this](dual h){ return me()->Conductivity_(h);};
return func(h).val;
}
double dConductivity_dH(double H)
{
dual h=H;
auto func = [this](dual h){ return me()->Conductivity_(h);};
return derivative(func, wrt(h), at(h));
}
};
struct BrooksCoreyFunctions
{
static dual ComputeEffectiveSaturation(dual pc, double lambda, double pb)
{
dual val = pow(pb/pc, lambda);
UGCheckValues(val);
return val;
};
};
//! Base class for a parameterized model. Provides serialization.
template <typename TParameter>
struct IParameterizedModel
{
IParameterizedModel() {}
IParameterizedModel(const TParameter &p) : m_param(p) {}
void set_parameters(const TParameter &p)
{ m_param = p; }
const TParameter& get_parameters() const
{ return m_param; }
std::string config_string() const
{
nlohmann::json jaux = m_param;
return jaux.dump();
}
TParameter m_param;
};
/*********************************************
* Exponential model.
*********************************************/
struct ExponentialModelParameters
{
double pentry = 1.0;
double alpha = 1.0;
double beta = 1.0;
double thetaR = 0.0; // default: 0.0
double thetaS = 1.0; // default: 1.0
double Ksat = 1.0; // saturated conductivity K_s (optional)
};
#ifdef UG_JSON
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ExponentialModelParameters, pentry, alpha, beta, thetaR, thetaS, Ksat)
#endif
/// Implement a simple exponential model.
class ExponentialModel :
public IRichardsModel<ExponentialModel>,
public IParameterizedModel<ExponentialModelParameters>
{
public:
typedef IRichardsModel<ExponentialModel> base_type;
typedef ExponentialModelParameters parameter_type;
typedef IParameterizedModel<ExponentialModelParameters> parameterized_model_type;
friend base_type;
friend parameterized_model_type;
ExponentialModel(const parameter_type &p) : parameterized_model_type(p) {}
protected:
// p_c = p_a - p_w : Kapillardruck >=0
// psi = p_c / (rho_w * g) : Kapillardruckhoehe >=0
/// Effective saturation: $0 \le \hat S \le 1$.
inline dual EffSaturation_(dual pc) const
{
const parameter_type &p = this->get_parameters();
return exp(-p.alpha * (pc/p.pentry));
}
/// Rescaled Saturation: $$ S:=\theta_r+ (\theta_s - \theta_r) * \hat S$$.
inline dual Saturation_(dual pc) const
{
const parameter_type &p = this->get_parameters();
UGCheckValues(pc);
if (pc.val <= 0) return p.thetaS; // Medium is saturated
const dual Seff = EffSaturation_(pc); UGCheckValues(Seff);
//UG_LOG("seff:" << (pc.val/p.pentry) << " -> " << Seff.val << std::endl);
return p.thetaR + (p.thetaS-p.thetaR)*Seff;
}
/// Relative permeability: $0 \le k_r \le 1$
inline dual RelativePermeability_(dual pc) const
{
const parameter_type &p = this->get_parameters();
UGCheckValues(pc);
if (pc.val<= 0) return 1.0;
const dual kval = exp(-p.beta * (pc/p.pentry));
//UG_LOG("k:" << (pc.val/p.pentry) << " -> " << kval.val << std::endl);
return kval;
}
// Conductivity C:=K_{sat}*k_r
inline dual Conductivity_(dual pc) const
{
return get_parameters().Ksat*RelativePermeability_(pc);
}
};
/*********************************************
* Van Genuchten-Mualem
*********************************************/
struct VanGenuchtenFunctions
{
/// Effective (reduced) saturation $0 \le \hat S \le 1$
/** \hat S:= (1/1+(alpha * psi )^n)^m, */
static dual ComputeEffectiveSaturation(dual psi_, double alpha, double n, double m)
{
UGCheckValues(psi_);
dual apn = alpha*psi_;
UGCheckValues(apn);
apn = pow(apn, n);
UGCheckValues(apn);
dual val = pow(1.0/(1.0+apn), m);
UGCheckValues(val);
return val;
}
//! Two argument version.
static dual ComputeEffectiveSaturation(dual psi_, double alpha, double n)
{ return ComputeEffectiveSaturation(psi_, alpha, n, 1.0-(1.0/n)); }
};
/// Implements a van Genuchten-Mualem model.
class VanGenuchtenModel
: public IRichardsModel<VanGenuchtenModel>, public IParameterizedModel<VanGenuchtenParameters>
{
public:
typedef IRichardsModel<VanGenuchtenModel> base_type;
typedef IParameterizedModel<VanGenuchtenParameters> parameterized_model_type;
typedef VanGenuchtenParameters parameter_type;
friend base_type;
friend parameterized_model_type;
VanGenuchtenModel(const parameter_type &p) : parameterized_model_type(p) {}
protected:
// p_c = p_a - p_w : Kapillardruck
// psi = p_c / (rho_w * g) : Kapillardruckhoehe
/// Effective saturation: $0 \le \hat S \le 1$.
inline dual EffSaturation_(dual psi_) const
{
const VanGenuchtenParameters &p = m_param;
return VanGenuchtenFunctions::ComputeEffectiveSaturation(psi_, p.alpha, p.n, p.m);
}
/// Rescaled Saturation: $$ S:=\theta_r+ (\theta_s - \theta_r) * \hat S$$.
inline dual Saturation_(dual psi_) const
{
const VanGenuchtenParameters &p = m_param;
UGCheckValues(psi_);
if (psi_ <= 0) return p.thetaS;
else
{
dual Seff = EffSaturation_(psi_);
UGCheckValues(Seff);
return p.thetaR + (p.thetaS-p.thetaR)*Seff;
}
}
/// Relative permeability: $0 \le k_r \le 1$
inline dual RelativePermeability_(dual psi_) const
{
const VanGenuchtenParameters &p = m_param;
UGCheckValues(psi_);
if (psi_<= 0) return 1.0;
dual Seff = EffSaturation_(psi_); // Seff -> 1 is dangerous!
if (fabs(Seff.val-1.0)<1e-15) return 1.0;
UGCheckValues(Seff);
double m = p.m;
dual brackS = 1.0-pow(Seff,1.0/m); // => brackS -> 0.0
UGCheckValues(brackS);
dual auxS = pow(brackS,m); // => this derivative may explode!
UGCheckValues(auxS);
return sqrt(Seff)*(1.0-auxS)*(1.0-auxS); // check!
}
// Conductivity C:=K_{sat}*k_r
inline dual Conductivity_(dual psi_) const
{
const VanGenuchtenParameters &p = m_param;
return p.Ksat*RelativePermeability_(psi_);
}
};
/*********************************************
* Haverkamp model.
*********************************************/
// Parameters for Haverkamp model.
struct HaverkampParameters
{
double alpha;
double n;
double beta;
double m;
double thetaR=0.0; // default: 0.0
double thetaS=1.0; // default: 1.0
double Ksat=1.0; // saturated conductivity K_s (optional)
};
#ifdef UG_JSON
/// JSON serialize.
void to_json(JSONType& j, const HaverkampParameters& p);
/// JSON de-serialize.
void from_json(const JSONType& j, HaverkampParameters& p);
#endif
/// Implements a Haverkamp model.
class HaverkampModel : public IRichardsModel<HaverkampModel>, public IParameterizedModel<HaverkampParameters>
{
public:
typedef IRichardsModel<HaverkampModel> base_type;
typedef IParameterizedModel<HaverkampParameters> parameterized_model_type;
typedef HaverkampParameters parameter_type;
friend base_type;
friend parameterized_model_type;
HaverkampModel(const parameter_type &p) : parameterized_model_type(p)
{}
#ifdef UG_JSON
// TODO: Move to Factory
HaverkampModel(const char* json)
{
nlohmann::json j = nlohmann::json::parse(json);
parameter_type p = j.get<parameter_type>();
set_parameters(p);
}
#endif
protected:
// p_c = p_a - p_w : Kapillardruck
// psi = p_c / (rho_w * g) : Kapillardruckhoehe
/// Rescaled Saturation $$ S:=\theta_r+ (\theta_s - \theta_r) * \hat S $$
dual Saturation_(dual psi_) const
{
const parameter_type &p = get_parameters();
if (psi_ <= 0.0) return p.thetaS;
dual Seff = VanGenuchtenFunctions::ComputeEffectiveSaturation(psi_, p.alpha, p.n, 1.0);
return p.thetaR + (p.thetaS-p.thetaR)*Seff;
}
/// Relative permeability $0 \le k_r \le 1$
dual RelativePermeability_(dual psi_) const
{
const parameter_type &p = get_parameters();
if (psi_<= 0.0) return 1.0;
return VanGenuchtenFunctions::ComputeEffectiveSaturation(psi_, p.beta, p.m, 1.0);
}
// Conductivity K:=K_{sat}*k_r
dual Conductivity_(dual psi_) const
{
const parameter_type &p = get_parameters();
return p.Ksat*RelativePermeability_(psi_);
}
};
/// Implements a van Genuchten model.
class GardnerModel :
public IParameterizedModel<HaverkampParameters>,
public IRichardsModel<GardnerModel>
{
public:
typedef IRichardsModel<GardnerModel> base_type;
typedef IParameterizedModel<HaverkampParameters> parameterized_model_type;
typedef VanGenuchtenParameters parameter_type;
GardnerModel(const VanGenuchtenParameters &p) : m_param(p) {}
/// Saturation
void get_saturation(double H, double &S, double &dSdH) const
{
auto sat = [this](dual h){ return me()->Saturation_(h, m_param);};
base_type::get_value_and_deriv(sat, H, S, dSdH);
}
/// Conductivity K
void get_conductivity(double H, double &K, double &dKdH) const
{
auto cond = [this](dual h){ return Conductivity_(h, m_param);};
base_type::get_value_and_deriv(cond, H, K, dKdH);
}
std::string config_string() const;
protected:
static double one;
VanGenuchtenParameters m_param;
// p_c = p_a - p_w : Kapillardruck
// hpc = p_c / (rho_w * g) : Kapillardruckhoehe
/// Normalisierter Wassergehalt (reduzierte Sättigung):
/** 0 \le (1/1+(alpha * psi )^n)^m \le 1*/
static dual EffSaturation_(dual psi_, const VanGenuchtenParameters &p)
{ return exp(-p.alpha*psi_); }
/// Rescaled Saturation
static dual Saturation_(dual psi_, const VanGenuchtenParameters &p)
{
if (psi_ <= 0) return p.thetaS;
else
{
dual Seff = EffSaturation_(psi_, p);
return p.thetaR + (p.thetaS-p.thetaR)*Seff;
}
}
// Conductivity K_s*k_r
static dual Conductivity_(dual psi_ , const VanGenuchtenParameters &p)
{ return p.Ksat* EffSaturation_(psi_, p); }
};
#ifdef UG_JSON
//! Generic model creation.
template <typename TModel, typename TParameter = typename TModel::parameter_type>
SmartPtr<TModel> CreateModel(const char *jstring) {
SmartPtr<TModel> inst = SPNULL;
try
{
nlohmann::json j = nlohmann::json::parse(jstring);
TParameter p = j.get<TParameter>();
inst = make_sp(new TModel(p));
}
catch (...)
{
UG_THROW ("Construction failed: " << jstring);
}
return inst;
};
//! Factory function.
SmartPtr<VanGenuchtenModel> CreateVanGenuchtenModel(const char *json);
//! Factory class.
struct RichardsModelFactory {
SmartPtr<ExponentialModel> create_exponential(const char *json);
SmartPtr<VanGenuchtenModel> create_van_genuchten(const char *json);
SmartPtr<HaverkampModel> create_haverkamp(const char *json);
};
#endif
}
#ifdef UG_JSON
template <> struct is_json_constructible<typename Richards::ExponentialModelParameters> { const static bool value = true;};
#endif
}
#ifdef UG_JSON
// Finally, we need to define how to construct from JSON.
namespace nlohmann {
template <>
struct adl_serializer<ug::Richards::VanGenuchtenModel>
{
static ug::Richards::VanGenuchtenModel from_json(const json& j)
{ return j.get<typename ug::Richards::VanGenuchtenParameters>(); }; // initialize from model }
static void to_json(json& j, typename ug::Richards::VanGenuchtenModel m)
{ j = m.get_parameters();}
};
};
#endif
#endif /* RICHARDS__VAN_GENUCHTEN_H_ */