forked from pacificclimate/VIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_dz.c
61 lines (51 loc) · 1.83 KB
/
compute_dz.c
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
#include <stdio.h>
#include <stdlib.h>
#include "vicNl.h"
#include <string.h>
static char vcid[] = "$Id$";
void compute_dz(double *dz, double *thermdepths, int Nnodes, double dp) {
/***********************************************************************
compute_dz Keith Cherkauer May 16, 2000
This routines computes the soil thermal node thicknesses using an
array of node depths.
10-May-04 Changed
if(thermdepths[Nnodes-1] != dp) {
to
if((int)(thermdepths[Nnodes-1]*1000 + 0.5) != (int)(dp*1000 + 0.5)) {
and changed
rint(something)
to
(float)(int)(something + 0.5)
to handle floating-point representation errors in the data
read from the state file without resorting to rint function.
TJB
***********************************************************************/
char ErrStr[MAXSTRING];
int j;
double sum;
if((int)(thermdepths[Nnodes-1]*1000 + 0.5) != (int)(dp*1000 + 0.5)) {
sprintf(ErrStr,"Thermal solution depth %i (Nnodes-1) must equal thermal damping depth %f, but is equal to %f",
Nnodes-1,dp,thermdepths[Nnodes-1]);
nrerror(ErrStr);
}
for(j=Nnodes-1;j>0;j--) {
thermdepths[j] -= thermdepths[j-1];
thermdepths[j] = (float)(int)(thermdepths[j] * 10000. + 0.5) / 10000.;
}
sum = 0;
dz[0] = (float)(int)(thermdepths[1] * 10000. + 0.5) / 10000.;
for(j=1;j<Nnodes;j++) {
dz[j] = 2. * (float)(int)((thermdepths[j] - dz[j-1] / 2.) * 10000. + 0.5) / 10000.;
if(dz[j] < 0) {
sprintf(ErrStr,"Check spacing between thermal layers %i and %i\n",
j-1,j);
nrerror(ErrStr);
}
sum += (dz[j-1] + dz[j]) / 2.;
}
if((int)(sum*1000 + 0.5) != (int)(dp*1000 + 0.5)) {
sprintf(ErrStr,"Thermal solution depth %i (Nnodes-1) must equal thermal damping depth %f, but is equal to %f",
Nnodes-1,dp,sum);
nrerror(ErrStr);
}
}