Richards
Solving The Richards equation with temporally periodic surface boundary conditions
util.cc
Go to the documentation of this file.
1 
3 #include "util.h"
4 
6 #define YRSEC 31557600.0
7 
8 double max (double *a, long n) {
9  double r = 0.0;
10  long i;
11  //first find a minimum or let m stay at zero
12  for (i=0; i<n; i++) if (a[i] < r) r = a[i];
13  //now that m is less than or equal to the lowest element of a, find the max
14  for (i=0; i<n; i++) if (a[i] > r) r = a[i];
15  return(r);
16 }
17 
18 double absmax (double *a, long n) {
19  double r = 0.0;
20  long i;
21  //now that m is less than or equal to the lowest element of a, find the max
22  for (i=0; i<n; i++) if (fabs(a[i]) > r) r = fabs(a[i]);
23  return(r);
24 }
25 
26 double min (double *a, long n) {
27  double r = 0.0;
28  long i;
29  //first find a maximum or let m stay at zero
30  for (i=0; i<n; i++) if (a[i] > r) r = a[i];
31  //now that m is greater than or equal to the largest element of a, find the min
32  for (i=0; i<n; i++) if (a[i] < r) r = a[i];
33  return(r);
34 }
35 
36 std::vector<double> linspace (double a, double b, long n) {
37 
38  //avoid division by zero
39  if ( n == 1 ) {
40  if ( a != b ) {
41  printf("FAILURE: cannot linspace with a single value if the range limits are not identical");
42  exit(EXIT_FAILURE);
43  }
44  std::vector<double> v(1, a);
45  return(v);
46  }
47  //spacing
48  double h = (b - a)/(n - 1);
49  //values
50  std::vector<double> v;
51  for (long i=0; i<n; i++)
52  v.push_back( a + i*h );
53 
54  return(v);
55 }
56 
57 std::vector<double> logspace (double a, double b, long n) {
58 
59  //avoid division by zero
60  if ( n == 1 ) {
61  if ( a != b ) {
62  printf("FAILURE: cannot logspace with a single value if the range limits are not identical");
63  exit(EXIT_FAILURE);
64  }
65  std::vector<double> v(1, pow(10, a));
66  return(v);
67  }
68  //spacing
69  double h = (b - a)/(n - 1);
70  //values
71  std::vector<double> v;
72  for (long i=0; i<n; i++)
73  v.push_back( pow(10, a + i*h) );
74 
75  return(v);
76 }
min
double min(double *a, long n)
find min of array
Definition: util.cc:26
max
double max(double *a, long n)
find max of array
Definition: util.cc:8
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
absmax
double absmax(double *a, long n)
find max magnitude of array
Definition: util.cc:18
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
util.h