-
Notifications
You must be signed in to change notification settings - Fork 1
/
cnmodel_simpel.R
331 lines (248 loc) · 9.01 KB
/
cnmodel_simpel.R
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
## Parameters
lue <<- 1
ppfd <<- 100
kbeer <<- 0.5
sla <<- 1.0
fleaf_fixed <<- 0.5
r_ntoc_plant <<- 1/30
r_ntoc_soil <<- 1/10
eff <<- 0.7
resp <<- 0.1
f_unavoidable <<- 0.0
k_noloss <<- 0.5
tau_plant <<- 10
tau_soil_c <<- 50
tau_soil_n <<- 150
tau_labl <<- 0.5
n_input <<- 1.0
## Functions
prod <- function( cleaf ){
prod <- ppfd * lue * ( 1.0 - exp( - kbeer * sla * cleaf ) )
return(prod)
}
curve( prod, from = 0, to = 10 )
npp <- function( ctot ){
npp <- eff * prod( ctot * fleaf_fixed ) - resp * ctot
return(npp)
}
n_u_demand <- function( ctot ){
n_u <- r_ntoc_plant * npp(ctot)
return(n_u)
}
turnover <- function( ctot, tau ){
turnover <- ctot / tau
return(turnover)
}
f_noloss <- function( croot ){
# out <- (1.0 - f_unavoidable) / (croot + k_noloss )
out <- (1.0 - f_unavoidable) * (1.0 - exp( - k_noloss * croot ))
return(out)
}
curve( f_noloss, from = 0, to = 10 )
n_u_supply <- function( ctot ){
n_u <- f_noloss( ctot ) * (r_ntoc_plant * turnover( ctot ) + n_input)
return(n_u)
}
## C-only model--------------------------------------------
## Simulation settings
ntsteps <- 1000
## starting value
cplant <- 20
csoil <- 20
## output initialisation
out_cplant <- rep(NA, ntsteps)
out_csoil <- rep(NA, ntsteps)
out_nsoil <- rep(NA, ntsteps)
out_netmin <- rep(NA, ntsteps)
for (it in 1:ntsteps){
## Soil turnover and net mineralisation
csoil_turnover <- csoil / tau_soil_c
csoil <- csoil - csoil_turnover
nsoil_turnover <- nsoil / tau_soil_n
nsoil <- nsoil - nsoil_turnover
netmin <- nsoil_turnover + n_input
cplant <- cplant + npp( cplant ) - turnover( cplant, tau_plant )
## plant turnover
cplant_turnover <- cplant / tau_plant
nplant_turnover <- cplant_turnover * r_ntoc_plant
csoil <- csoil + cplant_turnover
nsoil <- nsoil + nplant_turnover
## output collection
out_cplant[it] <- cplant
out_csoil[it] <- csoil
out_nsoil[it] <- nsoil
out_netmin[it] <- netmin
}
plot( 1:ntsteps, out_cplant )
plot( 1:ntsteps, out_csoil )
plot( 1:ntsteps, out_nsoil )
plot( 1:ntsteps, out_netmin )
##---------------------------------------------------------
##---------------------------------------------------------
## CN MODEL BELOW
##---------------------------------------------------------
ntoc_balance <- function( fleaf, ctot ){
## net C gain (c_acq) and N acquisition
c_acq <- prod( fleaf * ctot ) - resp * ctot
n_acq <- f_noloss( (1.0 - fleaf) * ctot ) * (r_ntoc_plant * ctot / tau_plant + n_input)
ntoc_acq <- n_acq / c_acq
balance <- ntoc_acq - r_ntoc_plant * eff
return(balance)
}
## doesn't have a root - always positive
curve( ntoc_balance(x, 10), from = 0, to = 1.0 )
eval_alloc <- function( fleaf, c_alloc, cplant_ag, cplant_bg, netmin ){
# ## VERSION A: fleaf only acts on new growth
# ## allocate C, N follows from r_ntoc_plant
# c_alloc_ag <- fleaf * c_alloc
# cplant_ag <- cplant_ag + c_alloc_ag * eff - cplant_ag / tau_plant
#
# c_alloc_bg <- (1.0 - fleaf) * c_alloc
# cplant_bg <- cplant_bg + c_alloc_bg * eff - cplant_bg / tau_plant
## VERSION B: fleaf redistributes entire plant (to avoid legacy in mal-allocation)
## total c to re-distribute:
ctot <- (cplant_ag + cplant_bg) * (1 - 1/tau_plant) + eff * c_alloc
## create new plant
cplant_ag <- fleaf * ctot
cplant_bg <- (1.0 - fleaf) * ctot
## C and N acquisition after allocation
c_acq <- prod( cplant_ag ) - resp * (cplant_ag + cplant_bg)
n_acq <- f_noloss( cplant_bg ) * netmin
eval <- eff * c_acq / n_acq - (1.0 / r_ntoc_plant)
return(eval)
}
calc_r_cton_acq <- function( fleaf, c_alloc, cplant_ag, cplant_bg, netmin ){
## total c to re-distribute:
ctot <- (cplant_ag + cplant_bg) * (1 - 1/tau_plant) + eff * c_alloc
## create new plant
cplant_ag <- fleaf * ctot
cplant_bg <- (1.0 - fleaf) * ctot
## C and N acquisition after allocation
c_acq <- prod( cplant_ag ) - resp * (cplant_ag + cplant_bg)
n_acq <- f_noloss( cplant_bg ) * netmin
r_cton_acq <- eff * c_acq / n_acq
return(r_cton_acq)
}
cplant_ag <- 175
cplant_bg <- 175
netmin <- 2.165898
c_alloc <- 2
out_root <- uniroot( function(x) eval_alloc( x, c_alloc, cplant_ag, cplant_bg, netmin), interval=c(0.0,1.0) )
curve( calc_r_cton_acq( x, c_alloc, cplant_ag, cplant_bg, netmin), from = 0, to = 1.0 )
abline(h=(1/r_ntoc_plant), lty=3)
abline(v=out_root$root, col="red")
curve( eval_alloc( x, c_alloc, cplant_ag, cplant_bg, netmin), from = 0, to = 1.0 )
abline(h=0, lty=3)
abline(v=out_root$root, col="red")
calc_r_cton_acq( 0.99, 5, 20, 20, 0.5 )
calc_r_cton_acq( 0.01, 5, 20, 20, 0.5 )
print("moin he")
## CN-model--------------------------------------------
## Simulation settings
ntsteps <- 1000
## initialise output variables
out_cplant_ag <- rep(NA, ntsteps)
out_nplant_ag <- rep(NA, ntsteps)
out_cplant_bg <- rep(NA, ntsteps)
out_nplant_bg <- rep(NA, ntsteps)
out_csoil <- rep(NA, ntsteps)
out_nsoil <- rep(NA, ntsteps)
out_clabl <- rep(NA, ntsteps)
out_nlabl <- rep(NA, ntsteps)
## plant, aboveground
cplant_ag <- 10
nplant_ag <- cplant_ag * r_ntoc_plant
## plant, belowground
cplant_bg <- 10
nplant_bg <- cplant_bg * r_ntoc_plant
## soil
csoil <- 100
nsoil <- csoil * r_ntoc_plant * tau_soil_n/tau_soil_c
## plant labile pools
nlabl <- 0
clabl <- 0
for (it in 1:ntsteps){
## Soil turnover and net mineralisation
csoil <- csoil - csoil / tau_soil_c
nsoil_turnover <- nsoil / tau_soil_n
nsoil <- nsoil - nsoil_turnover
netmin <- nsoil_turnover + n_input
## actual C and N acquisition with current plant
clabl <- prod( cplant_ag ) - resp * (cplant_ag + cplant_bg) #+ clabl
nlabl <- f_noloss( cplant_bg ) * netmin #+ nlabl
# ## short-cut: by-passing soil
# nlabl <- f_noloss( cplant_bg ) * r_ntoc_plant * (cplant_bg + cplant_ag) / tau_plant #+ nlabl
## limit allocatable C depending on labile N
c_alloc <- min( nlabl / (eff * r_ntoc_plant), clabl )
# curve( eval_alloc( x, c_alloc, cplant_ag, cplant_bg, netmin ), from = 0, to = 1.0 )
curve( calc_r_cton_acq( x, c_alloc, cplant_ag, cplant_bg, netmin), from = 0, to = 1.0 )
if ( eval_alloc( 0.0, c_alloc, cplant_ag, cplant_bg, netmin ) < 0.0 ){
## all to roots
fleaf <- 0.0
} else if ( eval_alloc( 1.0, c_alloc, cplant_ag, cplant_bg, netmin ) > 0.0 ){
## all to leaves
fleaf <- 1.0
} else {
## find allocation fraction to leaves so that return next year matches (eff * r_cton_plant)
out_root <- uniroot( function(x) eval_alloc( x, c_alloc, cplant_ag, cplant_bg, netmin), interval=c(1e-12,1.0) )
fleaf <- out_root$root
}
print(fleaf)
# fleaf <- 0.5
## update pools
clabl <- clabl - c_alloc
nlabl <- nlabl - eff * c_alloc * r_ntoc_plant
## labile pool decay
clabl <- clabl - tau_labl * clabl
nlabl <- nlabl - tau_labl * nlabl
##---------------------------------------------------------------------
# ## VERSION A: fleaf only acts on new growth
# ## allocate C, N follows from r_ntoc_plant
# cturnover_ag <- cplant_ag / tau_plant
# c_alloc_ag <- fleaf * c_alloc * eff
# cplant_ag <- cplant_ag + c_alloc_ag - cturnover_ag
# nturnover_ag <- nplant_ag / tau_plant
# n_alloc_ag <- c_alloc_ag * r_ntoc_plant
# nplant_ag <- nplant_ag + n_alloc_ag - nturnover_ag
# cturnover_bg <- cplant_bg / tau_plant
# c_alloc_bg <- (1.0 - fleaf) * c_alloc * eff
# cplant_bg <- cplant_bg + c_alloc_bg - cturnover_bg
# nturnover_bg <- nplant_bg / tau_plant
# n_alloc_bg <- c_alloc_bg * r_ntoc_plant
# nplant_bg <- nplant_bg + n_alloc_bg - nturnover_bg
##---------------------------------------------------------------------
##---------------------------------------------------------------------
## VERSION B: fleaf redistributes entire plant (to avoid legacy in mal-allocation)
## allocate C, N follows from r_ntoc_plant
cturnover_ag <- cplant_ag / tau_plant
cplant_ag <- cplant_ag - cturnover_ag
cplant_ag <- fleaf * (c_alloc * eff + cplant_ag + cplant_bg)
nturnover_ag <- nplant_ag / tau_plant
nplant_ag <- nplant_ag - nturnover_ag
nplant_ag <- cplant_ag * r_ntoc_plant
cturnover_bg <- cplant_bg / tau_plant
cplant_bg <- cplant_bg - cturnover_bg
cplant_bg <- (1.0 - fleaf) * (c_alloc * eff + cplant_ag + cplant_bg)
nturnover_ag <- nplant_ag / tau_plant
nplant_ag <- nplant_ag - nturnover_ag
nplant_ag <- cplant_ag * r_ntoc_plant
##---------------------------------------------------------------------
csoil <- csoil + cturnover_ag + cturnover_bg
nsoil <- nsoil + nturnover_ag + nturnover_bg
## gater output variables
out_cplant_ag[it] <- cplant_ag
out_nplant_ag[it] <- nplant_ag
out_cplant_bg[it] <- cplant_bg
out_nplant_bg[it] <- nplant_bg
out_csoil[it] <- csoil
out_nsoil[it] <- nsoil
out_clabl[it] <- clabl
out_nlabl[it] <- nlabl
}
##---------------------------------------------------------
plot( 1:ntsteps, out_cplant_ag )
plot( 1:ntsteps, out_cplant_bg )
plot( 1:ntsteps, out_csoil )
plot( 1:ntsteps, out_nsoil )
plot( 1:ntsteps, out_clabl )
plot( 1:ntsteps, out_nlabl )