Richards
Solving The Richards equation with temporally periodic surface boundary conditions
main_periodic_batch.cc
Go to the documentation of this file.
1 
3 #include <string>
4 #include <vector>
5 #include <iostream>
6 
7 #include "omp.h"
8 
9 #include "io.h"
10 #include "grid.h"
11 #include "settings.h"
12 #include "richards.h"
13 
15 int main (int argc, char **argv) {
16 
17  //output csv
18  std::string fn;
19  FILE *ofile;
20  //indices and counters
21  long unsigned i, i0, i1, i2, i3, i4, i5, i6, i7, count, nparam;
22  //parameter table
23  double **param;
24  //parameter vectors
25  std::vector<double> poro = linspace(0.1, 0.3, 3),
26  perm = logspace(-13, -11, 3),
27  b = linspace(3, 6, 4),
28  wilt = linspace(0.1, 0.5, 4),
29  tauevap = linspace(360, 36000, 12),
30  Levap = linspace(0.1, 0.1, 1),
31  infper = linspace(86400, 8640000, 9),
32  infdur = linspace(1800, 14400, 8);
33 
34  if ( argc != 4 )
35  print_exit("batch richards must be given two command line arguments\n 1. path to settings file\n 2. domain depth\n 3. path to output directory");
36 
37  printf(" starting periodic richards batch integrations\n");
38 
39  //output directory
40  std::string dirout = argv[3];
41 
42  //read settings
43  Settings stg = parse_settings(read_values(argv[1]));
44  //set the depth
45  stg.depth = std::atof(argv[2]);
46 
47  //create grid
48  Grid grid(stg.depth, stg.delz0, stg.delzfrac, stg.delzmax);
49  long n = grid.get_n();
50  printf(" %g meter grid created with %li cells\n", grid.get_dep(), grid.get_n());
51  printf(" surface cell depth: %g cm\n", grid.get_delze()[n-1]*100);
52  printf(" bottom cell depth: %g cm\n", grid.get_delze()[0]*100);
53  if ( stg.save_grid )
54  grid.save(dirout);
55 
56  //create parameter table
57  nparam = (poro.size()*perm.size()*b.size()
58  *wilt.size()*tauevap.size()*Levap.size()
59  *infper.size()*infdur.size());
60  param = new double*[nparam];
61  for (i=0; i<nparam; i++) param[i] = new double[8];
62  fn = dirout + "/trials.csv";
63  check_file_write(fn.c_str());
64  ofile = fopen(fn.c_str(), "w");
65  fprintf(ofile, "trial,poro,perm,b,wilt,tauevap,Levap,infper,infdur\n");
66  count = 0;
67  for (i0=0; i0<poro.size(); i0++) {
68  for (i1=0; i1<perm.size(); i1++) {
69  for (i2=0; i2<b.size(); i2++) {
70  for (i3=0; i3<wilt.size(); i3++) {
71  for (i4=0; i4<tauevap.size(); i4++) {
72  for (i5=0; i5<Levap.size(); i5++) {
73  for (i6=0; i6<infper.size(); i6++) {
74  for (i7=0; i7<infdur.size(); i7++) {
75  //fill in a row
76  param[count][0] = poro[i0];
77  param[count][1] = perm[i1];
78  param[count][2] = b[i2];
79  param[count][3] = wilt[i3];
80  param[count][4] = tauevap[i4];
81  param[count][5] = Levap[i5];
82  param[count][6] = infper[i6];
83  param[count][7] = infdur[i7];
84  //write to file
85  fprintf(ofile, "%lu,%g,%g,%g,%g,%g,%g,%g,%g\n",
86  count,
87  param[count][0],
88  param[count][1],
89  param[count][2],
90  param[count][3],
91  param[count][4],
92  param[count][5],
93  param[count][6],
94  param[count][7]
95  );
96  //increment
97  count++;
98  }
99  }
100  }
101  }
102  }
103  }
104  }
105  }
106  //close the csv
107  fclose(ofile);
108  printf("parameter table written to: %s\n", fn.c_str());
109 
110  printf("beginning parallel integrations of %lu trials with %d threads\n",
111  nparam, omp_get_max_threads());
112  printf(" trial | nstep\n");
113  printf(" ----------- | -----------\n");
114  #pragma omp parallel for schedule(dynamic)
115  for (long unsigned i=0; i<nparam; i++) {
116  //copy settings
117  Settings s = copy_settings(stg);
118  //edit params
119  s.poro = param[i][0];
120  s.perm = param[i][1];
121  s.b = param[i][2];
122  s.wilt = param[i][3];
123  s.tauevap = param[i][4];
124  s.Levap = param[i][5];
125  s.infper = param[i][6];
126  s.infdur = param[i][7];
127  //create a solver
128  Richards rich(grid, s);
129  rich.set_quiet(true);
130  rich.set_name(int_to_string(i));
131  //integrate
132  //rich.spinup(1e-6, true);
133  //rich.solve_adaptive(2*s.infper, s.infper/1e12, s.nsnap, dirout.c_str());
134  printf(" %11lu | %11lu\n", i, rich.get_nstep());
135  }
136 
137  for (i=0; i<nparam; i++) delete [] param[i];
138  delete [] param;
139 
140  return(0);
141 }
Settings::perm
double perm
permeability (m^2)
Definition: settings.h:49
read_values
std::vector< std::vector< std::string > > read_values(const char *fn)
reads a settings file into a vector of vectors of strings
Definition: io.cc:72
Settings::b
double b
Brooks-Corey parameter.
Definition: settings.h:57
print_exit
void print_exit(const char *msg)
prints a message before quitting the program
Definition: io.cc:5
settings.h
Grid::get_delze
const std::vector< double > get_delze() const
gets arrayvector of cell widths used for stability calculations (m)
Definition: grid.h:40
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
Grid::get_dep
double get_dep()
gets length/depth of the domain (m)
Definition: grid.h:32
Grid
class for constructing and containing grid information
Definition: grid.h:17
Grid::save
void save(std::string dirout)
writes grid arrays into a directory as binary files
Definition: grid.cc:41
grid.h
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
check_file_write
void check_file_write(const char *fn)
checks if a file can be written to
Definition: io.cc:49
Settings::Levap
double Levap
evaporation depth
Definition: settings.h:67
Settings::delz0
double delz0
surface cell width (m)
Definition: settings.h:21
Grid::get_n
long get_n()
gets number of cells
Definition: grid.h:30
Settings::poro
double poro
porosity
Definition: settings.h:47
Settings::infdur
double infdur
infiltration duration (s)
Definition: settings.h:71
copy_settings
Settings copy_settings(Settings &b)
constructs a copy of another Settings object
Definition: settings.cc:87
Settings::infper
double infper
infiltration period (s)
Definition: settings.h:69
Richards
the main model class
Definition: richards.h:54
Settings::delzmax
double delzmax
maximum cell width (m)
Definition: settings.h:25
logspace
std::vector< double > logspace(double a, double b, long n)
create an logarithmically spaced vector of values over a range
Definition: util.cc:57
parse_settings
Settings parse_settings(std::vector< std::vector< std::string > > sv)
parses a settings file and returns it in a Settings structure
Definition: settings.cc:20
io.h
linspace
std::vector< double > linspace(double a, double b, long n)
create an evenly spaced vector of values over a range
Definition: util.cc:36
richards.h
Settings::depth
double depth
domain depth (m)
Definition: settings.h:19
main
int main(int argc, char **argv)
driver function compiled into richards_periodic_batch.exe
Definition: main_periodic_batch.cc:15
Settings::save_grid
bool save_grid
whether to write grid files
Definition: settings.h:27
Settings::delzfrac
double delzfrac
fraction increase for deeper neighbor cell
Definition: settings.h:23