#1 RothC application with fictional input for a single site
#1.1 Input
data(RothC_Cin_ex, RothC_Nin_ex, RothC_N0_ex, RothC_C0_ex, RothC_xi_ex,
RothC_site_ex, RothC_env_in_ex) #fictional data
#1.2 Simulations
#In the following two methods are presented, one with a RothC as a predefined
#model (1.2.1), one where the RothC rate modifying factors must be calculated
#beforehand (1.2.2). Both methods lead to the same results.
#1.2.1 Simulation with predefined model
out_rothC <- sorcering( model="RothC", site=RothC_site_ex, env_in=RothC_env_in_ex,
Cin=RothC_Cin_ex, Nin=RothC_Nin_ex, N0=RothC_N0_ex, C0=RothC_C0_ex,
calcN=TRUE, tsteps="monthly")
#1.2.2 Simulation with own model definition and rate modifying factor definition
A_RothC <- fget_A_RothC(clay=30) #create transfer matrix for RothC
out_rothC_own <- sorcering(A=A_RothC , xi=RothC_xi_ex, Cin=RothC_Cin_ex,
Nin=RothC_Nin_ex, N0=RothC_N0_ex, C0=RothC_C0_ex, calcN=TRUE, tsteps="monthly")
#Note that RothC_xi_ex contains site and model specific rate modifying factors that
#are only valid in this specific example. Generally, xi must be calculated by the
#user for different environmental conditions and SOC models used.
#1.3 Results
#output structure summary
summary(out_rothC)
#show that results of 1.2.1 and 1.2.2 differ negligibly
all( abs(out_rothC$C-out_rothC_own$C) < 1e-14)
all( abs(out_rothC$N-out_rothC_own$N) < 1e-14)
#example plot
oldpar <- par(no.readonly=TRUE) #save old par
par(mfrow=c(1,1),mar=c(4,4,1,4))
plot(rowSums(out_rothC$N),axes=FALSE, col=1, cex.lab=2,xlab="",ylab="",ylim=c(0,9),
pch=20)
par(new=TRUE)
plot(rowSums(RothC_Cin_ex)/rowSums(RothC_Nin_ex),
axes=FALSE,col=2, cex.lab=2,xlab="",ylab="",ylim=c(0,60),pch=20)
axis(side=2, pos=0,
labels=(0:6)*1.5, at=(0:6)*10, hadj=0.7, padj=0.5, cex.axis=2,las=1,col.axis=1)
axis(side=4, pos=60,
labels=(0:6)*10, at=(0:6)*10, hadj=0, padj=0.5, cex.axis=2, las=1,col.axis=2)
axis(side=1, pos=0,
labels= (0:6)*10 , at=(0:6)*10, hadj=0.5, padj=0, cex.axis=2)
title(ylab=expression("total N [t ha"^-1*"]"), line=2, cex.lab=2)
mtext("C input / N input", side=4, line=2, cex=2,col=2)
title(xlab="time", line=2, cex.lab=2)
par(oldpar) #back to old par
#2 RothC application with fictional input for a multiple site application
#2.1 Input
data(RothC_Cin_ex_sl, RothC_Nin_ex_sl, RothC_N0_ex, RothC_C0_ex, RothC_site_ex,
RothC_env_in_ex) #fictional data
#2.2. Simulation
out_multi_rothC <- sorcering( model="RothC", site_sl=rep(list(RothC_site_ex),3),
env_in_sl=rep(list(RothC_env_in_ex),3), Cin_sl=RothC_Cin_ex_sl,
Nin_sl=RothC_Nin_ex_sl, N0_sl=rep(list(RothC_N0_ex),3),C0_sl=rep(list(RothC_C0_ex),3),
calcN=TRUE, tsteps="monthly", multisite=TRUE,
sitelist=list("normal","half_input","double_Cin"))
#2.3 Results
#output structure summary
summary(out_multi_rothC$normal)
summary(out_multi_rothC$half_input)
summary(out_multi_rothC$double_Cin)
#example plot
oldpar <- par(no.readonly=TRUE) #save old par
par(mfrow=c(1,1),mar=c(4,4,1,4))
for (listelement in c(1:3))
{
lwidth<-1
if (listelement==2)lwidth<-3
plot(rowSums(out_multi_rothC[[listelement]]$N),axes=FALSE, col=1,type="l", lwd=lwidth,
lty=listelement+2,cex.lab=2,xlab="",ylab="",ylim=c(0,18))
par(new=TRUE)
plot(rowSums(RothC_Cin_ex_sl[[listelement]])/rowSums(RothC_Nin_ex_sl[[listelement]]),
type="l", lwd=lwidth, lty=listelement+2,axes=FALSE,col=2, cex.lab=2,xlab="",
ylab="",ylim=c(0,120))
par(new=TRUE)
}
axis(side=2, pos=0,
labels=(0:6)*3, at=(0:6)*20, hadj=0.7, padj=0.5, cex.axis=2,las=1,col.axis=1)
axis(side=4, pos=60,
labels=(0:6)*20, at=(0:6)*20, hadj=0, padj=0.5, cex.axis=2, las=1,col.axis=2)
axis(side=1, pos=0,
labels= (0:6)*10 , at=(0:6)*10, hadj=0.5, padj=0, cex.axis=2)
title(ylab=expression("total N [t ha"^-1*"]"), line=2, cex.lab=2)
mtext("C input / N input", side=4, line=2, cex=2,col=2)
title(xlab="time", line=2, cex.lab=2)
legend(x=40,y=100,legend=c("normal","half_input","double_Cin"),lty=c(3,4,5),
lwd=c(1,3,1))
par(oldpar) #back to old par
#3 RothC application with fictional input
#and fictional measurement data to calculate C0 and N0
#3.1 Input
#fictional data
data(RothC_Cin_ex_sl, RothC_Nin_ex_sl, RothC_site_ex, RothC_env_in_ex, meas_data_ex)
#3.2. Simulation
out_rothC_C0<-sorcering( model="RothC", site=RothC_site_ex, env_in=RothC_env_in_ex,
Cin=RothC_Cin_ex, Nin=RothC_Nin_ex, calcC0=TRUE, calcN=TRUE, calcN0=TRUE,
tsteps="monthly", meas_data=meas_data_ex)
#3.3 Results
#output structure summary
summary(out_rothC_C0)
#example plot
oldpar <- par(no.readonly=TRUE) #save old par
par(mfrow=c(1,1),mar=c(4,4,1,4))
plot(rowSums(out_rothC_C0$N),axes=FALSE, col=1, cex.lab=2,xlab="",ylab="",ylim=c(0,9),
type="l",lwd=1)
par(new=TRUE)
plot(rowSums(out_rothC_C0$C),axes=FALSE, col=2, cex.lab=2,xlab="",ylab="",ylim=c(0,90),
type="l",lwd=1)
par(new=TRUE)
plot(x=meas_data_ex[,1],y=meas_data_ex[,3],axes=FALSE, col=1, cex.lab=2,xlab="",ylab="",
xlim=c(0,length(rowSums(out_rothC_C0$N))),ylim=c(0,9),pch=4,cex=3)
par(new=TRUE)
plot(x=meas_data_ex[,1],y=meas_data_ex[,2],axes=FALSE, col=2, cex.lab=2,xlab="",ylab="",
xlim=c(0,length(rowSums(out_rothC_C0$N))),ylim=c(0,90),pch=4,cex=3)
par(new=TRUE)
axis(side=2, pos=0,
labels=(0:8)*1, at=(0:8)*10, hadj=1, padj=0.5, cex.axis=2,las=1,col.axis=1)
axis(side=4, pos=60,
labels=(0:8)*10, at=(0:8)*10, hadj=0, padj=0.5, cex.axis=2, las=1,col.axis=2)
axis(side=1, pos=0,
labels= (0:8)*10 , at=(0:8)*10, hadj=0.5, padj=0, cex.axis=2)
title(ylab=expression("SON [t ha"^-1*"]"), line=2, cex.lab=2)
mtext(expression("SOC [t ha"^-1*"]"), side=4, line=3, cex=2,col=2)
title(xlab="time", line=2, cex.lab=2)
legend(x=30,y=30,legend=c("model result","measurement"),lwd=c(1,0))
legend(x=31,y=30,legend=c("",""),pch=4,pt.cex=c(0,3),bty="n")
par(oldpar) #back to old par
#4 Yasso15 application using multiple sites and
#input values of different wood diameters which take uncertainties into account
#4.1 Input
data(Yasso_Cin_ex_wood_u_sl, Yasso_Nin_ex_wood_u_sl, Yasso_C0_ex_sl, Yasso_N0_ex_sl,
RothC_env_in_ex) #fictional data
#show last entries of C input for 3rd site, 2nd wood layer, 4th uncertainty layer
tail(Yasso_Cin_ex_wood_u_sl[[3]][[2]][[4]])
#diameter of wood input: 2 classes of 0 cm and 10 cm for each of the 3 sites
wood_diam_ex_sl<-list(c(0,10),c(0,10),c(0,10))
#environmental variables
Yasso_env_in_ex<-RothC_env_in_ex[,1:2]
#4.2 Simulation
out_multi_yasso_wood_unc <- sorcering( model="Yasso15", C0_sl=Yasso_C0_ex_sl,
env_in_sl=rep(list(Yasso_env_in_ex),3), wood_diam_sl=wood_diam_ex_sl,
Cin_wood_sl=Yasso_Cin_ex_wood_u_sl,Nin_wood_sl=Yasso_Nin_ex_wood_u_sl,
N0_sl=Yasso_N0_ex_sl, calcN=TRUE, tsteps="monthly", multisite=TRUE,
sitelist=list("a","b","c"))
#4.3 Results
#show the last C results for 3rd site, 4th uncertainty layer
tail(out_multi_yasso_wood_unc[[3]][[4]]$C)
#5 RothC application using stochastically varying parameters
#and multiple sites
#5.1 fictional data
data(RothC_Cin_ex_sl, RothC_Nin_ex_sl, RothC_C0_ex, RothC_N0_ex,
RothC_site_ex, RothC_env_in_ex)
#standard deviations [%] used for each of the 7 RothC theta parameters
RothC_theta_unc <- c(0,0,1,1,1,1,2)
#5.2 Simulation
out_sl <- sorcering( model="RothC", site_sl=rep(list(RothC_site_ex),3),
env_in_sl=rep(list(RothC_env_in_ex),3), Cin_sl=RothC_Cin_ex_sl,
Nin_sl=RothC_Nin_ex_sl, C0_sl=rep(list(RothC_C0_ex),3),
N0_sl=rep(list(RothC_N0_ex),3),calcN=TRUE,theta_n_unc=10,
theta_unc=RothC_theta_unc, multisite=TRUE,
sitelist=list("normal","half_input","double_Cin"))
#5.3 Means and standard deviation
#60 time steps, 5 pools, 9 output types, 10 theta_n_unc, 3 sites
out_sl_arr <- array(unlist(out_sl),c(60,5,9,10,3))
out_sl_arr_N <- out_sl_arr[,,2,,] #only output type 2: N
#mean over all uncerts
out_sl_arr_N_mean <- apply( out_sl_arr_N , c(1,2,4), na.rm=TRUE, FUN=mean )
#standard deviation
out_sl_arr_N_sd<-
array(0, dim=c(dim(out_sl_arr_N)[1],dim(out_sl_arr_N)[2],dim(out_sl_arr_N)[4]))
for (dim3 in c(1:dim(out_sl_arr_N)[4]))
out_sl_arr_N_sd[,,dim3]<-apply(out_sl_arr_N[,,,dim3],c(1:2),sd)
#5.4 Results
#show the last N means for stand 1
tail(out_sl_arr_N_mean[,,1])
#show the last N standard deviations for stand 1
tail(out_sl_arr_N_sd[,,1])
#6 How to create input lists for a RothC application using stochastically
#varying inputs and input scenarios
#6.1 Input
#fictional data
data(RothC_Cin_ex_sl, RothC_C0_ex, RothC_site_ex, RothC_env_in_ex)
#create input list of 3 scenarios, 100 uncertainties each
set.seed(17) #to make 'random' results reproducible
f1<-1
for (no in c(1:3)) #loop over 3 input scenarios
{
#normal, half and double input
Cin <- switch (no, RothC_Cin_ex, RothC_Cin_ex/2, RothC_Cin_ex*2)
f2 <- 1
#create fictional uncertainties
for (unc in c(1:100)) #loop over 100 uncertainties
{
randnum<-max(0,rnorm(1,1,0.5)) #out of normal dist. with 50% sd.
if (f2==1) Cin_u <- list(Cin*randnum) else
Cin_u[[length(Cin_u)+1]] <- Cin*randnum
f2 <- 0
}
if (f1==1) Cin_u_sl <- list(Cin_u) else
Cin_u_sl[[length(Cin_u_sl)+1]] <- Cin_u
f1 <- 0
}
#show input of scenario 3, uncertainty 51
head(Cin_u_sl[[3]][[51]])
#6.2 Simulation
out_sl <- sorcering( model="RothC", site_sl=rep(list(RothC_site_ex),3),
env_in_sl=rep(list(RothC_env_in_ex),3),Cin_sl=Cin_u_sl,
C0_sl=list(RothC_C0_ex,RothC_C0_ex,RothC_C0_ex), tsteps="monthly",
multisite=TRUE, sitelist=list("normal","half_input","double_Cin"))
#6.3 Means and standard deviation
#60 time steps, 5 pools, 1000 uncertainties, 3 sites
out_sl_arr <- array(unlist(out_sl),c(60,5,100,3))
#means
out_sl_arr_mean <- apply( out_sl_arr , c(1,2,4), na.rm=TRUE, FUN=mean )
#standard deviation
out_sl_arr_sd<-
array(0, dim=c(dim(out_sl_arr)[1],dim(out_sl_arr)[2],dim(out_sl_arr)[4]))
for (dim3 in c(1:dim(out_sl_arr)[4]))
out_sl_arr_sd[,,dim3]<-apply(out_sl_arr[,,,dim3],c(1:2),sd)
#6.4 Results
#C-pool sums of means for the 3 scenarios
totalC_m1<-rowSums(out_sl_arr_mean[,,1])
totalC_m2<-rowSums(out_sl_arr_mean[,,2])
totalC_m3<-rowSums(out_sl_arr_mean[,,3])
#C-pool sums of standard deviations for the 3 scenarios
totalC_s1<-rowSums(out_sl_arr_sd[,,1])
totalC_s2<-rowSums(out_sl_arr_sd[,,2])
totalC_s3<-rowSums(out_sl_arr_sd[,,3])
#example plot
oldpar <- par(no.readonly=TRUE) #save old par
par(mfrow=c(1,1),mar=c(4,4,1,4))
plot(totalC_m1,axes=FALSE, col=2, cex.lab=2,xlab="",ylab="",ylim=c(0,100),
type="l",lwd=1)
par(new=TRUE)
plot(totalC_m2,axes=FALSE, col=3, cex.lab=2,xlab="",ylab="",ylim=c(0,100),
type="l",lwd=1)
par(new=TRUE)
plot(totalC_m3,axes=FALSE, col=4, cex.lab=2,xlab="",ylab="",ylim=c(0,100),
type="l",lwd=1)
par(new=TRUE)
polygon(c(1:60,60:1),c(totalC_m1+totalC_s1, rev(totalC_m1-totalC_s1)),
border=NA,col=rgb(1,0,0,0.27),density=40,angle=180,xlab="",ylab="")
par(new=TRUE)
polygon(c(1:60,60:1),c(totalC_m2+totalC_s2, rev(totalC_m2-totalC_s2)),
border=NA,col=rgb(0,1,0,0.27),density=30,xlab="",ylab="")
par(new=TRUE)
polygon(c(1:60,60:1),c(totalC_m3+totalC_s3, rev(totalC_m3-totalC_s3)),
border=NA,col=rgb(0,0,1,0.27),density=25,angle=90,xlab="",ylab="")
par(new=TRUE)
axis(side=2, pos=0,
labels=(0:10)*1, at=(0:10)*10, hadj=1, padj=0.5, cex.axis=2,las=1,col.axis=1)
axis(side=1, pos=0,
labels= (0:6)*10 , at=(0:6)*10, hadj=0.5, padj=0, cex.axis=2)
title(ylab=expression("SOC [t ha"^-1*"]"), line=2, cex.lab=2)
title(xlab="time", line=2, cex.lab=2)
legend(x=20,y=30,fill=c(0,0,0,4,2,3),density=c(0,0,0,25,40,30),angle=c(0,0,0,90,0,45),
border=c(0,0,0,1,1,1),legend=c("mean double input scenario",
"mean regular input scenario", "mean half input scneario",
"uncertainty range double input scenario", "uncertainty range regular input scenario",
"uncertainty range half input scenario"))
legend(x=20,y=30,lty=c(1,1,1,0,0,0),seg.len=c(1,1,1,0,0,0), col=c(4,2,3,0,0,0),
legend=c("","","","","",""),bty="n")
par(oldpar) #back to old par
#7 RothC application with fictional input for a spin-up application
#7.1 Input
#fictional data
data(RothC_Cin_ex_sl_spin, RothC_Nin_ex_sl_spin, RothC_site_ex, RothC_env_in_ex)
#7.2. Simulation
out_multi_rothC <- sorcering( model="RothC", site_sl=rep(list(RothC_site_ex),3),
env_in_sl=rep(list(RothC_env_in_ex[1:12,]),3), Cin_sl=RothC_Cin_ex_sl_spin,
Nin_sl=RothC_Nin_ex_sl_spin, calcN=TRUE, tsteps="monthly", multisite=TRUE,
sitelist=list("normal","half_input","double_Cin"), spinup=TRUE, t_spin_sl=36000,
C0=c(0,0,0,0,20), N0=c(0,0,0,0,2), CN_spin=c(100,100,50,50,10))
#7.3 Results
#example plot
oldpar <- par(no.readonly=TRUE) #save old par
par(mfrow=c(1,1),mar=c(4,4,1,4))
for (listelement in c(1:3))
{
lwidth<-1
if (listelement==2)lwidth<-3
printN<-rowSums(out_multi_rothC[[listelement]]$N)
printseq<-seq.int(1L,length(printN),100L)
printC<-rowSums(out_multi_rothC[[listelement]]$C)
plot(printN[printseq],axes=FALSE, col=1,type="l", lwd=lwidth,
lty=listelement+2,cex.lab=2,xlab="",ylab="",ylim=c(0,30))
par(new=TRUE)
plot(printC[printseq],axes=FALSE, col=2,type="l", lwd=lwidth,
lty=listelement+2,cex.lab=2,xlab="",ylab="",ylim=c(0,180))
par(new=TRUE)
}
axis(side=2, pos=0,
labels=(0:6)*5, at=(0:6)*30, hadj=0.7, padj=0.5, cex.axis=2,las=1,col.axis=1)
axis(side=4, pos=360,
labels=(0:6)*30, at=(0:6)*30, hadj=0, padj=0.5, cex.axis=2, las=1,col.axis=2)
axis(side=1, pos=0,
labels= (0:6)*6000 , at=(0:6)*60, hadj=0.5, padj=0, cex.axis=2)
title(ylab=expression("total N [t ha"^-1*"]"), line=2, cex.lab=2)
mtext(expression("total C [t ha"^-1*"]"), side=4, line=2, cex=2,col=2)
title(xlab="time", line=2, cex.lab=2)
legend(x=120,y=140,legend=c("normal","half_input","double_Cin"),lty=c(3,4,5),
lwd=c(1,3,1))
par(oldpar) #back to old par
Run the code above in your browser using DataLab