-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModel1.R
268 lines (207 loc) · 8.74 KB
/
Model1.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
#input parameters
carbmax = function(dfe) {
enddf = length(dfe[[1]])
GW = list()
Evaporation = list()
Assimilation = list()
Canoppypresure = list()
GC = list()
VPDLEAF = list()
for (i in 1:enddf) {
airtemp = dfe[[4]][i] #airtemperature in degree celcius
RH = dfe[[6]][i] #humidity in %
patm = dfe[[8]][i] #atmospheric presure in kpa
wind = dfe[[5]][i] #wind speed in m s-2
solar = dfe[[3]][i] #total incoming solar radiation in W m-2
date = dfe[[1]][i] #read date from csv or excel as number and not date time
time = dfe[[2]][i] #read time as hours (12h)
P = dfe[[7]][i] #soil presure
Tleaf = dfe[[9]][i] #leaf temperature in degree celcius
kmax = dfe[[10]][i] #kmax
b = dfe[[11]][i] #vulnerability curve parameter
c = dfe[[12]][i] #vulnerability curve parameter
vmax25 = dfe[[13]][i]
jmax25 = dfe[[14]][i]
r = dfe[[15]][i] #CO2 compensation point rate at 25 degree
recovery = dfe[[16]][i] #xylem recovery index
LABA = dfe[[17]][i] #leaf area: basel area ratio
##Hydralic cost model
#weibull curve to get p-crit
Pint = 0 #soil water presure
wbr = kmax*(exp(-1*(Pint/b)^c)) # weibull function
PLC = 1 - wbr/kmax #PLC function
##get p critical
while (PLC <= 0.99) {
wbr = kmax*(exp(-1*(Pint/b)^c))
PLC = 1 - wbr/kmax
Pint = Pint + 0.1
}
#get critical P value
Pcrit = Pint
#get new PLC based on soil P value
#weibull curve from P to Pcrit
if (i<2){
#get new PLC based on soil P value
#weibull curve from P to Pcrit
P = dfe[[7]][i]
wbr = kmax*(exp(-1*(P/b)^c)) # weibull function
kcrit = kmax*(exp(-1*(Pcrit/b)^c)) #kcrit at P99
kc = max(wbr) #instantanous kmax
PLC = (kc - wbr)/(kc - kcrit)
l = list() #initialize empty list for PLC
pl = list() #initialize empty list for soil presures
wbrl = list() #initialize empty list for k
inc = 0.1
while (P <=Pcrit){
wbr = kmax*(exp(-1*(P/b)^c))
PLC = 1 - wbr/kmax
l = c(l,PLC)
wbrl = c(wbrl, wbr)
pl = c(pl,P)
P = P + inc
}
df = do.call(rbind, Map(data.frame, A=pl, B=l,C=wbrl))
colnames(df)[1] = 'canopy_presure'
colnames(df)[2] = 'PLC'
colnames(df)[3] = 'K'
plcprev = df$PLC[1]
} else if (i > 1 && (dfe[[7]][i] >= dfe[[7]][i-1])) {
#get new PLC based on soil P value
#weibull curve from P to Pcrit
P = dfe[[7]][i]
wbr = kmax*(exp(-1*(P/b)^c)) # weibull function
kcrit = kmax*(exp(-1*(Pcrit/b)^c)) #kcrit at P99
kc = max(wbr) #instantanous kmax
PLC = (kc - wbr)/(kc - kcrit)
l = list() #initialize empty list for PLC
pl = list() #initialize empty list for soil presures
wbrl = list() #initialize empty list for k
inc = 0.1
while (P <=Pcrit){
wbr = kmax*(exp(-1*(P/b)^c))
PLC = 1 - wbr/kmax
l = c(l,PLC)
wbrl = c(wbrl, wbr)
pl = c(pl,P)
P = P + inc
}
df = do.call(rbind, Map(data.frame, A=pl, B=l, C=wbrl))
colnames(df)[1] = 'canopy_presure'
colnames(df)[2] = 'PLC'
colnames(df)[3] = 'K'
plcnew = plcprev-(plcprev-df$PLC[1])*recovery
df$PLCnew = ((1-plcnew)/(1-df$PLC[1]))*(df$PLC-1)+1
df$knew = (-1*(df$PLCnew-1))*kmax
#counter loop to keep original PLC if recoverd PLC is smaller than PLC
if (df$PLCnew[1] > df$PLC[1]){
df = subset(df, select = -c(PLC,K))
df$PLC = df$PLCnew
df$K =df$knew
}
plcprev = df$PLC[1]
} else if (i > 1 && (dfe[[7]][i] <= dfe[[7]][i-1])){
#get new PLC based on soil P value
#weibull curve from P to Pcrit
P = dfe[[7]][i]
wbr = kmax*(exp(-1*(P/b)^c)) # weibull function
kcrit = kmax*(exp(-1*(Pcrit/b)^c)) #kcrit at P99
kc = max(wbr) #instantanous kmax
PLC = (kc - wbr)/(kc - kcrit)
l = list() #initialize empty list for PLC
pl = list() #initialize empty list for soil presures
wbrl = list() #initialize empty list for k
inc = 0.1
while (P <=Pcrit){
wbr = kmax*(exp(-1*(P/b)^c))
PLC = (1 - wbr/kmax)
l = c(l,PLC)
wbrl = c(wbrl, wbr)
pl = c(pl,P)
P = P + inc
}
df = do.call(rbind, Map(data.frame, A=pl, B=l, C=wbrl))
colnames(df)[1] = 'canopy_presure'
colnames(df)[2] = 'PLC'
colnames(df)[3] = 'K'
plcprev = df$PLC[1]
}
df$integral_area = df$K/inc
df$integral_sum = cumsum(df$integral_area)
#E needs to be converted from kg h-1 m-2 Mpa -1 per basel area to mmol s-1 m-2 p leaf area
df$E = (df$integral_sum - df$integral_sum[1])*55.4*(1/3600)*(1/LABA)*(1000)*(1/100)
#Assimilation gain model
ca = 40 #atmospheric CO2 partial presure in kpa
oa = 21000 #atmospheric O2 partial presure in kpa
satvp = 0.61365*exp((17.502*Tleaf)/(240.97+Tleaf)) # Buck 1981
VPchm = ((RH/100))*satvp
VPleaf = (100/100)*satvp
df$VPDleaf = VPleaf - VPchm
df$gw = (df$E/df$VPDleaf)*100 #stomatal conductance in mmol s-1 m-2
df$gc = df$gw/1.6 #CO2 stomatal conductance in mmol s-1 m-2
ko25 = 28202 #michales-menton constant for oxylation (pa) at 25 degrees celcious
kc25 = 41 #michales-menton constant for carboxylation (pa) at 25 degrees celcious
qe = 0.3 #quatum yield of electron transport rate (mol photon mol-1 e)
cc = 0.9 #curvature of light response curve
cj = 0.98 #curvature of factor je v. jc limited photosynthesis (collatz et al 1991 and in Campbell and Norman)
df$vnumerator = vmax25*(1+exp((-4424)/(298*8.314)))*exp((73637/(8.314*298))*(1-(298/(273+Tleaf))))
df$vdenominator = 1+exp((486*(Tleaf+273)-149252)/(8.314*(273+Tleaf)))
df$vmax = df$vnumerator/df$vdenominator #Vmax adjusted according to temperature (Leuning 2002 temperature adjusted)
df$kc = kc25*exp((79430*((Tleaf+273)-298))/(298*8.314*(Tleaf+273))) #Kc temperature corrected (Bernacchi et al. 2001 temperature adjusted)
df$ko = ko25*exp((36380*((Tleaf+273)-298))/(298*8.314*(Tleaf+273))) #Ko temperature corrected (Bernacchi et al. 2001 temperature adjusted)
df$r = r*exp((37830*((Tleaf+273)-298))/(298*8.314*(Tleaf+273))) #temperature adjusted (Bernacchi et al. 2001)
par = solar*0.45*4.57 #PAR (calculated from Wm-2) (umols-1m-2) Plant Growth Chamber Handbook (chapter 1, radiation; https://www.controlledenvironments.org/wp-content/uploads/sites/6/2017/06/Ch01.pdf)
df$jnumerator = jmax25*(1+exp(-4534/(298*8.314)))*exp((50300/(8.314*298))*(1-(298/(Tleaf+273))))
df$jdenominator = 1+exp((495*(Tleaf+273)-152044)/(8.314*(273+Tleaf)))
df$jmax = df$jnumerator/df$jdenominator #Jmax adjusted according to temperature (Leuning 2002 temperature adjusted)
df$resp = (0.01*vmax25*2^(((Tleaf+273)-298)/10))* (1 + exp(1.3 * (Tleaf - 55))) #respiration rate (umol s-1m-2 (collatz et a; 1991/ also in Campbell and Norman) and medlyn 2002
#get assimilation rate and ci
ci = 0
A = list(0)
Ag = 0.1
An = 0
end = length((df$gc))
for (i in 2:end){
An = 0
Ag = 0.1
while (Ag > An) {
jc =(df$vmax[i]*(ci-r))/(ci+df$kc[i]*(oa/df$ko[i]))
j =(qe*par+df$jmax[i]-((((qe*par+df$jmax[i])^2)-
(par*df$jmax[i]*cc*qe*4))^0.5))/(2*cc)
je = (j/4)*((ci-df$r[i])/(ci+2*df$r[i]))
An = (je +jc-((((je+jc)^2)-4*cj*je*jc)^0.5))/(2*cj)
Ag = (df$gc[i]*(ca-ci))/101.3
ci = ci + inc
}
A = c(A, An)
i = i + 1
}
df[["An"]] = as.numeric(as.character(unlist(A))) #add assimilation to dataframe (umol s-1 m-2)
Amax = df$An[[end]] #get maximum An
df$gain = df$An/Amax #normalize An
df$profit = df$gain - df$PLC #get profit
index = which.max(df$profit) #get index of maximum profit
g = df$gw[index]
E = df$E[index]
As = df$An[index]
Tleaf = df$Tleaf[index]
Cpresure = df$canopy_presure[index]
gc = df$gc[index]
VPDL = df$VPDleaf[index]
#create list of all the results
GW = c(GW,g)
Evaporation = c(Evaporation,E)
Assimilation = c(Assimilation,As)
Canoppypresure = c(Canoppypresure,Cpresure)
GC = c(GC,gc)
VPDLEAF = c(VPDLEAF, VPDL)
}
#create dataframe of results
#create dataframe of results
results = do.call(rbind, Map(data.frame, A=GW, B=Evaporation, C=Assimilation,
E=Canoppypresure))
colnames(results)[1] = 'Gw'
colnames(results)[2] = 'E'
colnames(results)[3] = 'An'
colnames(results)[4] = 'Cpresure'
results <<- results #move results df to global variable
}