Richards
Solving The Richards equation with temporally periodic surface boundary conditions
grid.cc
Go to the documentation of this file.
1 
3 #include "grid.h"
4 
5 Grid::Grid (double depth, double delz0, double delzfrac, double delzmax) {
6 
7  long i;
8 
9  //compute grid edges
10  grid_edges(depth, delz0, delzfrac, delzmax, ze);
11 
12  //number of cells
13  n = ze.size() - 1;
14 
15  //assign the domain length/depth
16  dep = -ze[0];
17 
18  //compute cell center coordinates
19  for (i=0; i<n; i++) zc.push_back( ze[i+1]/2.0 + ze[i]/2.0 );
20 
21  //compute cell widths
22  for (i=0; i<n; i++) delz.push_back( ze[i+1] - ze[i] );
23 
24  //cell widths used for stability calculations
25  delze.push_back( delz[0] ); //bottom edge looks at bottom cell
26  for (i=1; i<n; i++) delze.push_back( delz[i] );
27  delze.push_back( delz[n-1] );
28 
29  //factors for cell edge interpolation
30  vefac.push_back( NAN );
31  for (i=1; i<n; i++) vefac.push_back( (ze[i] - zc[i-1])/(zc[i] - zc[i-1]) );
32  vefac.push_back( NAN );
33 
34  //factors for cell edge gradients
35  gefac.push_back( NAN );
36  for (i=1; i<n; i++) gefac.push_back( 1.0/(zc[i] - zc[i-1]) );
37  gefac.push_back( NAN );
38 
39 }
40 
41 void Grid::save (std::string dirout) {
42  write_array(dirout + "/zc", zc);
43  write_array(dirout + "/ze", ze);
44  write_array(dirout + "/delz", delz);
45  write_array(dirout + "/delze", delze);
46  write_array(dirout + "/vefac", vefac);
47  write_array(dirout + "/gefac", gefac);
48 }
49 
50 void Grid::grid_edges(double depth, double delz0, double delzfrac,
51  double delzmax, std::vector<double> &ze) {
52 
53  double delz, f, tem;
54  long i,n;
55 
56  //start with the top cell edge
57  ze.clear();
58  ze.push_back( 0.0 );
59  ze.push_back( delz0 );
60  //fill the vector with increasingly large cells
61  while ( ze.back() < depth ) {
62  delz = (ze.back() - ze.end()[-2])*delzfrac;
63  if ( delz < delzmax ) {
64  ze.push_back( ze.back() + delz );
65  } else {
66  ze.push_back( ze.back() + delzmax );
67  }
68  }
69  n = long(ze.size());
70  //squeeze cells down to get the depth correct
71  f = depth/ze.back();
72  for (i=0; i<n; i++)
73  ze[i] *= -f;
74  //swap the order
75  for (i=0; i<n/2; i++) {
76  tem = ze[i];
77  ze[i] = ze[n-i-1];
78  ze[n-i-1] = tem;
79  }
80 }
Grid::save
void save(std::string dirout)
writes grid arrays into a directory as binary files
Definition: grid.cc:41
grid.h
Grid::Grid
Grid(double depth, double delz0, double delzfrac, double delzmax)
constructor
Definition: grid.cc:5
write_array
void write_array(const char *fn, T *a, long size)
writes an array to a binary file
Definition: io.h:53