# A five-node food-web model is reconstructed: decomposers, large deomcposers, herbivores, omnivores, 
# predators. Decomposers receive energy exclusively from detritus, herbivores receive energy 
# exclusively from living plants, predators receive energy from other nodes except 
# for large decomposers and omnivores receive 75% of energy from other nodes except for 
# large decomposers, 12.5% of energy from detritus and 12.5% of energy from living plats. 
# All decomposers that were larger than the largest predator within a community, were assigned 
# to the node of large decomposers (comprising predominantly earthworms) that do not provide 
# energy to the higher trophic levels within the food-web framework. Note, that large herbivores were
# not included here as they were absent.

# Data frame is on community base and contains (1) categorical 'guild' column and (2) total metabolism
# of each guild ('metabolism'). For 5 guilds it's 5 rows.

# metabolism is given in J h^-1 per sample

# Define variables for metabolism of each guild to easy address them

Data.metabolism
##                metabolism       guild
## Decomposers.4 0.158921460 Decomposers
## Herbivores.4  0.002851135  Herbivores
## Large.4       0.338035948       Large
## Omnivores.4   0.900313929   Omnivores
## Predators.4   0.178207107   Predators
d <- Data.metabolism$metabolism[Data.metabolism$guild=="Decomposers"]
h <- Data.metabolism$metabolism[Data.metabolism$guild=="Herbivores"]
o <- Data.metabolism$metabolism[Data.metabolism$guild=="Omnivores"]
p <- Data.metabolism$metabolism[Data.metabolism$guild=="Predators"]
ld <- Data.metabolism$metabolism[Data.metabolism$guild=="Large"]

# Introduce corrected 'o' and 'p' to define the degree of intraguild predation. 
# Coefficient vary from 0 to 1 (or higher); 0 = no IG predation; 1 = equal preferences for predators
# and primary connsumers

ocorr <- o*0.5
pcorr <- p*0.5

# Calculate proportion of each guild in the total community metabolism. Calculations will be used to 
# assign feeding preferences, if coefficient for intraguild predation = 0, proportions for predators 
# and omnivores are also = 0

total <- d+h+ocorr+pcorr
dp <- d/total # proportion of decomposers
hp <- h/total # proportion of herbivores
op <- ocorr/total # proportion of omnivores
pp <- pcorr/total # proportion of predators

# Assign assimilation efficiencies (according to Jochum et al. 2017; Barnes et al. 2014) 

aed <- 0.25 # for decomposers
aeh <- 0.45 # for herbivores
aep <- 0.85 # for predators
aeo <- 0.125*aed + 0.125*aeh + 0.75*aep # for omnivores (proportion of resources is constant)

# Calculate 'extended predation'. Here we account for the fact that if predators feeding on themselves 
# and omnivores, their energy demand is growing, i.e. losses due to predation increase. We loop many times
# to account for extra losses for omnivores and predators

p_ext <- p/aep # basic flux to predators in account for assimilation efficiency (no losses)
o_ext <- o/aeo # basic flux to omnivores in account for assimilation efficiency (no losses)
for(j in 2:99){ # here we loop to account for additional losses due to intraguild predation
  p_ext[j] <- (o_ext[j-1]*0.75*pp + p_ext[j-1]*pp)/aep # feeding preferences according to the proportion in community metabolism; 
  o_ext[j] <- (o_ext[j-1]*0.75*op + p_ext[j-1]*op)/aeo
}
p_ext <- sum(p_ext) # sum up all add-ons will give the final flux
o_ext <- sum(o_ext) 

# Now we know final fluxes to predators and omnivores considering their losses. 
# We can calculate energy fluxes from the top of the food web
# For predators:

fdp <- p_ext*dp # flux decomposers-predators
fhp <- p_ext*hp # flux herbivores-predators
fop <- p_ext*op # flux omnivores-predators
fpp <- p_ext*pp # flux predators-predators
fsump <- fdp+fhp+fop+fpp # sum
fsump
## [1] 1.25499
# For omnivores (they receive 75% of energy from other animals and 25% from basal resources):

fdo <- o_ext*0.75*dp # flux decomposers-omnivores
fho <- o_ext*0.75*hp # flux herbivores-omnivores
foo <- o_ext*0.75*op # flux omnivores-omnivores
fpo <- o_ext*0.75*pp # flux predators-omnivores
fplanto <- 0.125*o_ext # flux plants-omnivore
fdetrito <- 0.125*o_ext # flux detritus-omnivore
fsumo <- fdo+fho+foo+fpo+fplanto+fdetrito
fsumo
## [1] 6.52291
# For decomposers (we account for losses due to predation and assimilation efficiency):

fdetritd <- (d + fdp + fdo)/aed # flux detritus-decomposers
fdetritd
## [1] 6.209846
# For herbivores (we account for losses due to predation and assimilation efficiency):

fplanth <- (h + fhp + fho)/aeh  # flux plants-herbivores
fplanth
## [1] 0.06189328
# For large decomposers (no losses due to predation):

fdetritld <- ld/aed
fdetritld
## [1] 1.352144
# the total flux will be

ftotal <- fsump + fsumo + fdetritd + fplanth + fdetritld

ftotal
## [1] 15.40178
# transform flux in kg fresh mass ha^-1 year^-1
# 39.06 - internal coefficient to recalculate data from sample to square meter
# 8760 - recalculate hour to year
# 10000 - recalculate square meter to hectar
NA

multiplier = 10000*((39.06*8760)/7000000) 
ftotal*multiplier
## [1] 7528.515