-
Notifications
You must be signed in to change notification settings - Fork 5
/
ST_environs.c
305 lines (258 loc) · 9.55 KB
/
ST_environs.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* \file ST_environs.c
* \brief Controls all environmental phenomenon from creating ppt and temp to
* creating the disturbances.
*
* \author CWB (initial coding)
* \date 15 June 2000
* \ingroup ENVIRONMENT
*/
/* =================================================== */
/* INCLUDES / DEFINES */
/* --------------------------------------------------- */
#include <string.h>
#include "ST_steppe.h"
#include "ST_globals.h"
#include "sw_src/include/SW_Defines.h"
#include "sw_src/include/rands.h"
#include "sxw.h" // externs `*SXW`
#include "sxw_funcs.h"
#include "sw_src/include/filefuncs.h"
/*********** Locally Used Function Declarations ************/
/***********************************************************/
static void _make_ppt( void);
static void _set_ppt_reduction( void);
static void _set_temp_reduction( void);
static void _make_disturbance( void);
/**************************************************************/
/**************************************************************/
sw_random_t environs_rng;
void Env_Generate( void) {
/*======================================================*/
/* PURPOSE */
/* Wrapper to generate a new set of environmental factors,
usually for the current year. Any new environmental
generators should be called from this subroutine.
*/
/* HISTORY */
/* Chris Bennett @ LTER-CSU 6/15/2000 */
/*------------------------------------------------------*/
SXW_Run_SOILWAT();
_make_ppt();
_set_ppt_reduction();
_set_temp_reduction();
_make_disturbance();
}
/* Deep copy one EnvType's information to another. Note that both must be initialized
BEFORE calling this function. */
void copy_environment(const EnvType* src, EnvType* dest){
if(!src){
return;
}
dest->lyppt = src->lyppt;
dest->ppt = src->ppt;
dest->temp = src->temp;
dest->gsppt = src->gsppt;
dest->temp_reduction[0] = src->temp_reduction[0];
dest->temp_reduction[1] = src->temp_reduction[1];
dest->wet_dry = src->wet_dry;
}
/* Deep copy one PlotType's information to another. Note that both must be initialized
BEFORE calling this function. */
void copy_plot(const PlotType* src, PlotType* dest){
if(!src){
return;
}
dest->disturbance = src->disturbance;
dest->disturbed = src->disturbed;
dest->pat_removed = src->pat_removed;
}
/* Deep copy one SucculentType's information to another. Note that both must be initialized
BEFORE calling this function. */
void copy_succulent(const SucculentType* src, SucculentType* dest){
if(!src){
return;
}
dest->growth[0] = src->growth[0];
dest->growth[1] = src->growth[1];
dest->mort[0] = src->mort[0];
dest->mort[1] = src->mort[1];
dest->prob_death = src->prob_death;
dest->reduction = src->reduction;
}
/**************************************************************/
static void _make_ppt( void) {
/*======================================================*/
/* If not running SOILWAT,take a random number from normal distribution with*/
/* mean, stddev that is between min & max from */
/* the Globals->ppt structure.*/
/* Also determine growing season precipitation. */
/* HISTORY */
/* Chris Bennett @ LTER-CSU 6/15/2000 */
/* cwb - 6-Dec-02 -- added code to interface with STEPWAT.
* The ppt and gsppt are set in _sxw_set_environs()
* but we still pass through this code to set the
* Dry/Wet/Normal state. */
/*------------------------------------------------------*/
IntS i;
#ifdef DEBUG_ENVCONST
IntS r=320;
#endif
// Run with SOILWAT2: we have monthly PPT and temperature to calculate
// growing season precipitation as sum of monthly precipitation of those
// months when mean air temperature exceeds a threshold that allows for plant growth 'Globals.temp.gstemp'
Env->gsppt = 0; // gsppt is defined as IntS and units are millimeters
for (i = 0; i < MAX_MONTHS; i++)
{
Env->gsppt += GE(SXW->temp_monthly[i], Globals->temp.gstemp) ?
(IntS) (SXW->ppt_monthly[i] * 10. + 0.5) : 0;
}
if (Env->gsppt <= 0)
{
LogError(&LogInfo, LOGWARN, "Zero growing season precipitation in "\
"year = %d of iteration = %d", Globals->currYear, Globals->currIter);
Env->gsppt = 0;
}
if ( Env->ppt <= Globals->ppt.dry )
Env->wet_dry = Ppt_Dry;
else if (Env->ppt >= Globals->ppt.wet)
Env->wet_dry = Ppt_Wet;
else
Env->wet_dry = Ppt_Norm;
}
/**************************************************************/
static void _set_ppt_reduction( void) {
/*======================================================*/
/* HISTORY */
/* Chris Bennett @ LTER-CSU 6/15/2000 */
/*------------------------------------------------------*/
/* EQN 10*/
Succulent->reduction = fabs(Succulent->growth[Slope]
* Env->gsppt
+ Succulent->growth[Intcpt]);
/* EQN 16*/
Succulent->prob_death = (Succulent->mort[Slope] * Env->gsppt
+ Succulent->mort[Intcpt]) / 100.0;
}
/**************************************************************/
static void _set_temp_reduction( void) {
/*======================================================*/
/* This routine implements EQNs 12 and 13*/
/* HISTORY */
/* Chris Bennett @ LTER-CSU 6/15/2000 */
/*
* 4-Mar-03 - ran into a bug where temperature extremes cause
* negative temp_reductions, so I truncate to 0.
* 7-Mar-03 - found a logic bomb in C&L90!. Eqns 12 & 13
* are reported to be based on daily temps but
* the model necessarily uses MAT. I'm adding
* a hack to compensate somewhat until a better
* method comes along. The current fix is to
* account for the fact that the MAT is actually
* about 1/3 of optimal max yearly temp. */
/*------------------------------------------------------*/
int i;
RealF tp[4]; /* parms for temp growth modifier eqn.*/
for ( i=CoolSeason; i <= WarmSeason; i ++ ){
tp[1] = Globals->tempparm[i][0];
tp[2] = Globals->tempparm[i][1];
tp[3] = Globals->tempparm[i][2];
tp[0] = Env->temp + tp[1];
Env->temp_reduction[i] = tp[2]*tp[0] + tp[3] * (tp[0]*tp[0]);
Env->temp_reduction[i] = MAX(0., Env->temp_reduction[i]);
}
if (Env->temp < 9.5 ) {
Env->temp_reduction[CoolSeason] = .9;
Env->temp_reduction[WarmSeason] = .6;
} else {
Env->temp_reduction[CoolSeason] = .6;
Env->temp_reduction[WarmSeason] = .9;
}
}
/**************************************************************/
static void _make_disturbance( void) {
/*======================================================*/
/* PURPOSE */
/* Generate disturbances, if any, for this year. */
/* HISTORY */
/* Chris Bennett @ LTER-CSU 6/15/2000 */
/*------------------------------------------------------*/
RealF pc; /* probability of colonization if current */
/* disturbance is fecalpat*/
/*new disturbance event generated, if any*/
DisturbEvent event;
/*------------------------------------------------------*/
/* Can't have simultaneous disturbances*/
if (Plot->disturbance != NoDisturb) {
switch( Plot->disturbance) {
case FecalPat:
if (Plot->pat_removed) {
Plot->disturbed = 0;
Plot->pat_removed = FALSE;
Plot->disturbance = NoDisturb;
} else {
pc = Globals->pat.recol[Slope] * Plot->disturbed
+ Globals->pat.recol[Intcpt];
if (RandUni(&environs_rng) <= pc) {
Plot->pat_removed = TRUE;
/* slight effects for one year*/
Plot->disturbed = 1;
} else {
Plot->pat_removed = FALSE;
Plot->disturbed++;
}
}
break;
case NoDisturb: // Does nothing but prevent a compiler warning
case LastDisturb:
default:
Plot->disturbed = (Plot->disturbed) ? (Plot->disturbed - 1) : 0;
break;
}
if (Plot->disturbed == 0)
Plot->disturbance = NoDisturb;
}
/* if the disturbance was expired above, */
/* we can generate a new one immediately */
if (Plot->disturbance == NoDisturb) {
/* pick some type of disturbance (other than none)*/
event = (DisturbEvent) RandUniIntRange(1, LastDisturb -1, &environs_rng);
/* make sure this is off unless needed */
Plot->pat_removed = FALSE;
switch( event) {
case FecalPat:
if (!Globals->pat.use) {event=NoDisturb; break;}
event = (RandUni(&environs_rng) <= Globals->pat.occur)
? event : NoDisturb;
if (event == NoDisturb) break;
Plot->pat_removed = (RandUni(&environs_rng) <= Globals->pat.removal);
Plot->disturbed = 0;
break;
case AntMound:
if (!Globals->mound.use) {event=NoDisturb; break;}
event = (RandUni(&environs_rng) <= Globals->mound.occur)
? event :NoDisturb;
if (event == NoDisturb) break;
Plot->disturbed = RandUniIntRange(Globals->mound.minyr,
Globals->mound.maxyr,
&environs_rng);
break;
case Burrow:
if (!Globals->burrow.use) {event=NoDisturb; break;}
event = (RandUni(&environs_rng) <= Globals->burrow.occur)
? event :NoDisturb;
if (event == NoDisturb) break;
Plot->disturbed = (Globals->burrow.minyr > 0)
? RandUniIntRange(1, Globals->burrow.minyr, &environs_rng)
: 0;
break;
case NoDisturb:
break;
case LastDisturb:
break;
default:
break;
}
Plot->disturbance = event;
}
}