Richards
Solving The Richards equation with temporally periodic surface boundary conditions
richards.cc
Go to the documentation of this file.
1 
3 #include "richards.h"
4 
6  OdeSsp3 (grid.get_n()+1),
7  stg (copy_settings(stgin)),
8  n (grid.get_n()),
9  dep (grid.get_dep()),
10  ze (grid.get_ze()),
11  zc (grid.get_zc()),
12  delz (grid.get_delz()),
13  delze (grid.get_delze()),
14  vefac (grid.get_vefac()),
15  gefac (grid.get_gefac()) {
16 
17  long i;
18 
19  //set the name of the object
20  set_name("richards");
21  //turn on silent snapping
22  set_silent_snap(true);
23 
24  //------------------
25  //physical variables
26 
27  //porosity at cell centers
28  for (i=0; i<n; i++) poroc.push_back( f_poro(-zc[i], stg.poro) );
29  //porosity at cell edges
30  for (i=0; i<n+1; i++) poroe.push_back( f_poro(-ze[i], stg.poro) );
31  //saturated hydraulic conductivity at cell edges
32  for (i=0; i<n+1; i++) Ksat.push_back( f_Ksat(-ze[i], stg.g, stg.mu, stg.rho) );
33  //saturated matric head
34  for (i=0; i<n+1; i++) psisat.push_back( f_psisat(-ze[i]) );
35 
36  //water fractions at cell edges
37  we.resize(n+1);
38  //derivative of psi w/r/t moisture
39  dpsidw.resize(n+1);
40  //derivative of moisture w/r/t z
41  dwdz.resize(n+1);
42  //hydraulic conductivity (unsaturated)
43  K.resize(n+1);
44  //diffusivity (K*d psi / d t)
45  D.resize(n+1, INFINITY);
46  //fluxes
47  q.resize(n+1);
48 
49  //storage vectors, if needed
50  if ( stg.qall ) qall.resize(n+1);
51  if ( stg.wall ) wall.resize(n);
52 
53  //find the approximate middle cell edge
54  mididx = argclose(ze, -dep/2.0, n+1);
55 
56  //-------------------------------------
57  //discretization constants for stable dt
58 
59  for (i=0; i<n+1; i++) dtcons.push_back( delze[i]*delze[i]/2.0 );
60 
61  //------------------
62  //initial condition
63 
64  //water fractions
65  for (i=0; i<n; i++) set_sol(i, 0.999*poroc[i]);
66  //time
67  set_sol(n, 0.0);
68 
69 }
70 
71 //------------------------------------------------------------------------------
72 //physical functions
73 
74 double Richards::f_poro (double depth, double poro) {
75  (void)depth;
76  return(
77  poro
78  );
79 }
80 
81 double Richards::f_ksat (double depth, double perm) {
82  (void)depth;
83  return(
84  perm
85  );
86 }
87 
88 double Richards::f_Ksat (double depth, double g, double mu, double rho) {
89  return ( rho*g*f_ksat(depth, stg.perm)/mu );
90 }
91 
92 double Richards::f_K (double w, double Ksat, double wsat, double b) {
93  return(
94  Ksat*pow(w/wsat, 2.0*b + 3.0)
95  );
96 }
97 
98 double Richards::f_psisat (double depth) {
99  (void)depth;
100  return(
101  -0.2
102  );
103 }
104 
105 double Richards::f_dpsidw (double w, double psisat, double wsat, double b) {
106  return(
107  -(b/wsat)*psisat*pow(w/wsat, -(b + 1))
108  );
109 }
110 
111 bool Richards::f_infil (double t) {
112 
113  //bounds of next/current infiltration event
114  double ta, tb;
115  infil_times(t, &ta, &tb);
116  //check if current time is in the event window
117  if ( (t >= ta) && (t <= tb) )
118  //make it rain
119  return(true);
120  //let it dry
121  return(false);
122 }
123 
124 double Richards::f_w_bot (double poro) {
125  return(
126  poro
127  );
128 }
129 
130 //------------------------------------------------------------------------------
131 //ODE solver functions
132 
133 void Richards::infil_times (double t, double *ta, double *tb) {
134  //end of next infiltration event
135  (*tb) = t - fmod(t, stg.infper) + stg.infper;
136  //beginning of next infiltration event
137  (*ta) = (*tb) - stg.infdur;
138 }
139 
140 double Richards::f_q (double K, double dpsidw, double dwdz, double satl, double satr) {
141 
142  double q = -K*dpsidw*dwdz - K;
143 
144  if ( q < 0 ) {
145  if ( (satl > 1) || (satr < stg.wilt) )
146  q = 0.0;
147  } else if ( q > 0 ) {
148  if ( (satr > 1) || (satl < stg.wilt) )
149  q = 0.0;
150  }
151 
152  return(q);
153 }
154 
155 double Richards::f_dwdt (double qt, double qb, double delz) {
156  return(
157  (qt - qb)/delz
158  );
159 }
160 
161 void Richards::update_q (double *w, double t) {
162 
163  //index
164  long i;
165  //infiltration flag
166  bool infil = f_infil(t);
167 
168  //----------------------------------------------
169  //cell edge saturations and saturation gradients
170 
171  //bottom
172  we[0] = f_w_bot(poroe[0]);
173  dwdz[0] = (w[0] - we[0])/(delz[0]/2);
174  //interior
175  for (i=1; i<n; i++) {
176  we[i] = vefac[i]*w[i] + (1.0 - vefac[i])*w[i-1];
177  dwdz[i] = gefac[i]*(w[i] - w[i-1]);
178  }
179  //top value depends on infiltration flag
180  infil ? we[n] = poroe[n] : NAN;
181  dwdz[n] = (we[n] - w[n-1])/(delz[n-1]/2);
182 
183  //------------------------------
184  //cell edge hydraulic properties
185 
186  for (i=0; i<n+1; i++) {
187  K[i] = f_K(we[i], Ksat[i], poroe[i], stg.b);
188  dpsidw[i] = f_dpsidw(we[i], psisat[i], poroe[i], stg.b);
189  D[i] = K[i]*dpsidw[i];
190  }
191 
192  //------
193  //fluxes
194 
195  //water table
196  q[0] = f_q(K[0], dpsidw[0], dwdz[0], stg.wilt, w[0]/poroc[0]);
197  //cell-to-cell exchange
198  for (i=1; i<n; i++)
199  q[i] = f_q(K[i], dpsidw[i], dwdz[i], w[i-1]/poroc[i-1], w[i]/poroc[i-1]);
200  //surface infiltration/evaporation
201  if ( infil ) {
202  q[n] = f_q(K[n], dpsidw[n], dwdz[n], w[n-1]/poroc[n-1], stg.wilt);
203  } else {
204  q[n] = stg.Levap*(w[n-1] - stg.wilt*poroe[n])/stg.tauevap;
205  }
206 }
207 
208 void Richards::ode_fun (double *solin, double *fout) {
209 
210  //autonomous form for time
211  fout[n] = 1.0;
212  //compute fluxes
213  update_q(solin, solin[n]);
214  //time derivatives of water fractions
215  for (long i=0; i<n; i++)
216  fout[i] = f_dwdt(q[i], q[i+1], delz[i]);
217 }
218 
220 
221  //compute fraction of maximum stable time step
222  double dt = INFINITY;
223  double dtmax;
224  for (long i=0; i<n+1; i++) {
225  dtmax = dtcons[i]/D[i];
226  if ( (dtmax < dt) && !std::isnan(dtmax) )
227  dt = dtmax;
228  }
229  dt *= stg.dtfac;
230  //infiltration management
231  double t, ta, tb;
232  t = get_sol(n);
233  infil_times(get_sol(n), &ta, &tb);
234  if ( (t >= ta) && (t <= tb) ) {
235  if ( dt > stg.infdur/1000 )
236  dt = stg.infdur/1000;
237  if ( t + dt > tb )
238  dt = tb - t;
239  } else {
240  if ( t + dt > ta )
241  dt = ta - t;
242  }
243  //evaporation management
244  if ( dt > stg.tauevap*delz[0]/stg.Levap )
245  dt = 0.99*stg.tauevap*delz[0]/stg.Levap;
246 
247  return( dt );
248 }
249 
250 void Richards::steady (double atol, unsigned long ntol) {
251 
252  //index
253  long i;
254  //step counter
255  unsigned long count = 0;
256  //euler time step
257  double dt = INFINITY;
258  //alias the solution array
259  double *w = get_sol();
260  //storage for time derivatives, initially at inf
261  double *dwdt = new double[n+1];
262  for (i=0; i<n; i++) dwdt[i] = INFINITY;
263 
264  //integrate until dwdt is small enough or too many steps are taken
265  while ( (absmax(dwdt, n)*dt > atol) && (count < ntol) ) {
266  ode_fun(w, dwdt);
267  dt = 0.75*dt_adapt();
268  for (i=0; i<n; i++)
269  w[i] += dt*dwdt[i];
270  count++;
271  }
272 
273  delete [] dwdt;
274 }
275 
276 void Richards::spinup (double rtol, bool quiet) {
277 
278  long i;
279  if ( !quiet ) {
280  printf(" max rel dif requirement is %g\n", rtol);
281  printf(" PERIODS | MAX REL DIF \n");
282  printf(" ------- | -------------\n");
283  }
284  //integrate over a single infiltration period to get started
285  solve_adaptive(stg.infper, stg.infper/1e12, false);
286  //store the bottom boundary flux
287  std::vector<double> q_a(n, INFINITY);
288  std::vector<double> q_b;
289  for (i=0; i<n; i++) q_b.push_back(q[i]);
290  //maximum relative difference
291  double mrd = maxreldif(q_a, q_b, n);
292  //continue integrating over infiltration periods until the qb is stable
293  long count = 0;
294  long ord = floor(log10(mrd));
295  while ( (mrd > rtol) || (count <= 5) ) {
296  solve_adaptive(stg.infper, stg.infper/1e12, false);
297  count++;
298  for (i=0; i<n; i++) {
299  q_a[i] = q_b[i];
300  q_b[i] = q[i];
301  }
302  mrd = maxreldif(q_a, q_b, n);
303  if ( (!quiet) && (floor(log10(mrd)) != ord ) ) {
304  printf(" %-7li | %-11g\n", count, mrd);
305  ord = floor(log10(mrd));
306  }
307  }
308 }
309 
310 //------------------------------------------------------------------------------
311 //extras
312 
314  std::string name = get_name();
315  std::string dirout = get_dirout();
316  if ( stg.poroc )
317  write_array(dirout + "/" + name + "_poroc", poroc);
318  if ( stg.poroe )
319  write_array(dirout + "/" + name + "_poroe", poroe);
320  if ( stg.Ksat )
321  write_array(dirout + "/" + name + "_Ksat", Ksat);
322  if ( stg.psisat )
323  write_array(dirout + "/" + name + "_psisat", psisat);
324 }
325 
326 void Richards::after_snap (std::string dirout, long isnap, double tin) {
327  std::string name = get_name();
328  std::string i = int_to_string(isnap);
329  if ( stg.we )
330  write_array(dirout + "/" + name + "_we_" + i, we);
331  if ( stg.dpsidw )
332  write_array(dirout + "/" + name + "_dpsidw_" + i, dpsidw);
333  if ( stg.dwdz )
334  write_array(dirout + "/" + name + "_dwdz_" + i, dwdz);
335  if ( stg.K )
336  write_array(dirout + "/" + name + "_K_" + i, K);
337  if ( stg.D )
338  write_array(dirout + "/" + name + "_D_" + i, D);
339  if ( stg.w )
340  write_array(dirout + "/" + name + "_w_" + i, get_sol(), n);
341  if ( stg.q )
342  write_array(dirout + "/" + name + "_q_" + i, q);
343  if ( stg.tsnap )
344  tsnap.push_back( tin );
345 }
346 
347 void Richards::q_updated (double t, bool *qup) {
348 
349  if ( !(*qup) ) {
350  update_q(get_sol(), t);
351  *qup = true;
352  }
353 }
354 
355 void Richards::after_step (double tin) {
356 
357  //q updated flag
358  bool qup = false;
359 
360  if ( stg.t )
361  t.push_back( tin );
362  if ( stg.qtop ) {
363  q_updated(tin, &qup);
364  qtop.push_back( q.back() );
365  }
366  if ( stg.qmid ) {
367  q_updated(tin, &qup);
368  qmid.push_back( q[mididx] );
369  }
370  if ( stg.qbot ) {
371  q_updated(tin, &qup);
372  qbot.push_back( q[0] );
373  }
374  if ( stg.qall ) {
375  q_updated(tin, &qup);
376  for (long i=0; i<n+1; i++) qall[i].push_back( q[i] );
377  }
378  if ( stg.wall )
379  for (long i=0; i<n; i++) wall[i].push_back( get_sol(i) );
380  if ( stg.infil )
381  infil.push_back( f_infil(tin) );
382 }
383 
385 
386  std::string name = get_name();
387  std::string dirout = get_dirout();
388  std::vector<float> all, temp;
389  if ( stg.tsnap )
390  write_array(dirout + "/" + name + "_tsnap", tsnap);
391  if ( stg.t )
392  write_array(dirout + "/" + name + "_t", subsample(t, stg.nmaxout));
393  if ( stg.qtop )
394  write_array(dirout + "/" + name + "_qtop", subsample(qtop, stg.nmaxout));
395  if ( stg.qmid )
396  write_array(dirout + "/" + name + "_qmid", subsample(qmid, stg.nmaxout));
397  if ( stg.qbot )
398  write_array(dirout + "/" + name + "_qbot", subsample(qbot, stg.nmaxout));
399  if ( stg.qall ) {
400  all.clear();
401  for (long i=0; i<n+1; i++) {
402  temp = subsample(qall[i], stg.nmaxout);
403  for (long unsigned j=0; j<temp.size(); j++)
404  all.push_back(temp[j]);
405  }
406  write_array(dirout + "/" + name + "_qall", all);
407  }
408  if ( stg.wall ) {
409  all.clear();
410  for (long i=0; i<n; i++) {
411  temp = subsample(wall[i], stg.nmaxout);
412  for (long unsigned j=0; j<temp.size(); j++)
413  all.push_back(temp[j]);
414  }
415  write_array(dirout + "/" + name + "_wall", all);
416  }
417  if ( stg.infil )
418  write_array(dirout + "/" + name + "_infil", subsample(infil, stg.nmaxout));
419 }
Richards::f_w_bot
double f_w_bot(double poro)
bottom boundary condition
Definition: richards.cc:124
Richards::qall
std::vector< std::vector< float > > qall
whether to track ALL fluxes
Definition: richards.h:135
Settings::qall
bool qall
whether to track all fluxes
Definition: settings.h:107
Richards::f_dwdt
double f_dwdt(double qt, double qb, double delz)
computes the time derivative of a cell, given fluxes on its sides
Definition: richards.cc:155
Settings::perm
double perm
permeability (m^2)
Definition: settings.h:49
Settings::qbot
bool qbot
whether to track bottom flux
Definition: settings.h:105
Settings::K
bool K
whether to write hydraulic conductivity when snapping
Definition: settings.h:89
Richards::dwdz
std::vector< double > dwdz
derivative of moisture w/r/t z
Definition: richards.h:111
Settings::b
double b
Brooks-Corey parameter.
Definition: settings.h:57
Richards::qtop
std::vector< float > qtop
top boundary flux
Definition: richards.h:127
Richards::t
std::vector< float > t
time tracker
Definition: richards.h:123
Settings::infil
bool infil
whether to track infiltration flag
Definition: settings.h:109
Settings::dtfac
double dtfac
safety factor for stable time step
Definition: settings.h:41
Richards::f_Ksat
double f_Ksat(double depth, double g, double mu, double rho)
computes saturated hydraulic conductivity (m/s)
Definition: richards.cc:88
Settings
container struct for all the settings variables needed for a BousThermModel run
Definition: settings.h:13
Settings::tauevap
double tauevap
evaporation timescale
Definition: settings.h:65
Settings::w
bool w
whether to water fraction profile when snapping
Definition: settings.h:93
Richards::spinup
void spinup(double rtol=1e-12, bool quiet=true)
integrates over infiltration periods until nearly periodic behavior is established
Definition: richards.cc:276
Richards::f_ksat
double f_ksat(double depth, double perm)
computes saturated permeability (m^2)
Definition: richards.cc:81
Richards::f_poro
double f_poro(double depth, double poro)
computes porosity as a function of depth (-)
Definition: richards.cc:74
Grid
class for constructing and containing grid information
Definition: grid.h:17
Richards::qbot
std::vector< float > qbot
bottom boundary flux
Definition: richards.h:133
Richards::f_q
double f_q(double K, double dpsidw, double dwdz, double satl, double satr)
computes the flux between two cells
Definition: richards.cc:140
Richards::before_solve
void before_solve()
does extra stuff before starting a solve
Definition: richards.cc:313
Richards::K
std::vector< double > K
hydraulic conductivity (unsaturated)
Definition: richards.h:113
Settings::tsnap
bool tsnap
whether to track snap times
Definition: settings.h:113
Richards::dt_adapt
double dt_adapt()
computes the next time step, based on the maximum diffusivity
Definition: richards.cc:219
Settings::t
bool t
whether to track step times
Definition: settings.h:111
int_to_string
std::string int_to_string(long i)
converts an integer into a string
Definition: io.cc:37
Settings::wilt
double wilt
wilting saturation as fraction of porosity
Definition: settings.h:59
Richards::q_updated
void q_updated(double t, bool *qup)
updates fluxes and sets a flag, if needed
Definition: richards.cc:347
Richards::after_step
void after_step(double tin)
does extra stuff after every step
Definition: richards.cc:355
Richards::Richards
Richards(Grid grid, Settings stgin)
constructs
Definition: richards.cc:5
Richards::n
const long n
number of cells
Definition: richards.h:72
Richards::infil
std::vector< float > infil
infiltration flag
Definition: richards.h:139
Settings::Levap
double Levap
evaporation depth
Definition: settings.h:67
Richards::infil_times
void infil_times(double t, double *ta, double *tb)
computes beginning and end of next or current infiltration event
Definition: richards.cc:133
Richards::zc
const std::vector< double > zc
cell center coordinates (m)
Definition: richards.h:78
write_array
void write_array(const char *fn, T *a, long size)
writes an array to a binary file
Definition: io.h:53
Settings::mu
double mu
water viscosity (Pa*s)
Definition: settings.h:53
Settings::nmaxout
long nmaxout
maximum length of output vectors (subsampled to accomodate)
Definition: settings.h:39
Settings::poroc
bool poroc
whether to write porosity at cell centers
Definition: settings.h:77
Richards::ze
const std::vector< double > ze
cell edge coordinates (m)
Definition: richards.h:76
subsample
std::vector< T > subsample(std::vector< T > v, unsigned long n)
subsample a vector, keeping first and last elements
Definition: util.h:71
Richards::psisat
std::vector< double > psisat
saturated matric head (m)
Definition: richards.h:102
Richards::dtcons
std::vector< double > dtcons
constants for finding maximum stable time step
Definition: richards.h:88
Settings::poro
double poro
porosity
Definition: settings.h:47
Richards::delz
const std::vector< double > delz
cell width (m)
Definition: richards.h:80
Settings::infdur
double infdur
infiltration duration (s)
Definition: settings.h:71
Richards::f_dpsidw
double f_dpsidw(double w, double psisat, double wsat, double b)
computes derivative of Brooks-Corey matric head w/r/t water fraction (-)
Definition: richards.cc:105
Settings::qtop
bool qtop
whether to track top flux
Definition: settings.h:101
Richards::mididx
unsigned long mididx
index of approximate middle edge
Definition: richards.h:131
maxreldif
double maxreldif(T a, T b, long n)
finds the maximum relative difference between two arrays/vectors
Definition: util.h:31
copy_settings
Settings copy_settings(Settings &b)
constructs a copy of another Settings object
Definition: settings.cc:87
Richards::delze
const std::vector< double > delze
cell widths used for stability calculations (m)
Definition: richards.h:82
Richards::Ksat
std::vector< double > Ksat
saturated hydraulic conductivity over depth (m/s)
Definition: richards.h:100
Richards::f_K
double f_K(double w, double Ksat, double wsat, double b)
computes Brooks-Corey hydraulic conductivity (m/s)
Definition: richards.cc:92
Richards::tsnap
std::vector< float > tsnap
snapshot times
Definition: richards.h:125
Settings::Ksat
bool Ksat
whether to write saturated hydraulic conductivity
Definition: settings.h:81
Richards::wall
std::vector< std::vector< float > > wall
whether to track ALL water fractions
Definition: richards.h:137
Settings::poroe
bool poroe
whether to write porosity at cell edges
Definition: settings.h:79
Richards::f_infil
bool f_infil(double t)
infiltration flag
Definition: richards.cc:111
Richards::q
std::vector< double > q
fluxes
Definition: richards.h:117
Settings::we
bool we
whether to write water fraction at cell edges when snapping
Definition: settings.h:95
argclose
long argclose(T a, double b, long n)
find index of value in array closest to a number
Definition: util.h:43
Settings::psisat
bool psisat
whether to write saturated matric head
Definition: settings.h:83
Settings::infper
double infper
infiltration period (s)
Definition: settings.h:69
Richards::ode_fun
void ode_fun(double *solin, double *fout)
ode function for the integrator
Definition: richards.cc:208
Settings::g
double g
gravity (m/s^2)
Definition: settings.h:51
Richards::dep
const double dep
length/depth of domain (m)
Definition: richards.h:74
Settings::rho
double rho
water density (kg/m^3)
Definition: settings.h:55
Richards::D
std::vector< double > D
diffusivity (K*d psi /dt)
Definition: richards.h:115
Settings::dwdz
bool dwdz
whether to write moisture fraction derivative when snapping
Definition: settings.h:87
Richards::poroe
std::vector< double > poroe
porosity at cell edges
Definition: richards.h:98
Settings::dpsidw
bool dpsidw
whether to write dpsidw when snapping
Definition: settings.h:85
Richards::steady
void steady(double atol=1e-9, unsigned long ntol=1000000)
integrates to steady state using current state boundary conditions
Definition: richards.cc:250
Richards::we
std::vector< double > we
water fractions at cell edges
Definition: richards.h:107
absmax
double absmax(double *a, long n)
find max magnitude of array
Definition: util.cc:18
Richards::update_q
void update_q(double *w, double t)
updates fluxes
Definition: richards.cc:161
Settings::q
bool q
whether to write water flux when snapping
Definition: settings.h:99
Settings::D
bool D
whether to write diffusivity when snapping
Definition: settings.h:91
Settings::wall
bool wall
whether to track all water fractions
Definition: settings.h:97
Richards::f_psisat
double f_psisat(double depth)
computes the saturation matric head (m)
Definition: richards.cc:98
richards.h
Richards::poroc
std::vector< double > poroc
porosity at cell centers (maximum saturation fraction)
Definition: richards.h:96
Richards::vefac
const std::vector< double > vefac
factors for cell edge values
Definition: richards.h:84
Settings::qmid
bool qmid
whether to track middle flux
Definition: settings.h:103
Richards::qmid
std::vector< float > qmid
middle boundary flux
Definition: richards.h:129
Richards::gefac
const std::vector< double > gefac
factors for cell edge gradients
Definition: richards.h:86
Richards::after_snap
void after_snap(std::string dirout, long isnap, double t)
does extra stuff after every snap
Definition: richards.cc:326
Richards::after_solve
void after_solve()
does extra stuff after integrating
Definition: richards.cc:384
Richards::stg
Settings stg
settings container
Definition: richards.h:66
Richards::dpsidw
std::vector< double > dpsidw
derivative of psi w/r/t moisture
Definition: richards.h:109