+ Show code- Hide code
# This code is Op_en3283/totalCost [[Economic impacts]]
library(OpasnetUtils)
emissionprice <- Ovariable("emissionprice",
dependencies = data.frame(Name = c("co2price")),
formula = function(...) {
temp <- c("CO2direct", "CO2eq", "CO2trade", "PM2.5")
weight <- c(0.33, 0.34, 0.33, 0)
if(openv$N > 0) {
temp <- sample(temp, size = max(1, openv$N), replace = TRUE, prob = weight)
weight <- 1
}
out <- Ovariable(
output = data.frame(
Iter = 1:length(temp),
Pollutant = temp,
Result = weight
),
marginal = c(TRUE, FALSE, FALSE)
)
if(openv$N == 0) {
out <- unkeep(out, cols = "Iter")
}
out <- out * co2price
return(out)
}
)
externalCost <- Ovariable("externalCost",
dependencies = data.frame(
Name = c(
"emissions",
"emissionprice",
"DALYs",
"DALYprice"
)
),
formula = function(...) {
health <- oapply(
unkeep(DALYs, prevresults = TRUE, sources = TRUE),
cols = c(
"Fuel",
"Pollutant",
"Response",
"Population",
"Age",
"Sex",
"Exposure",
"ER_function",
"Exposure_unit"
),
FUN = sum
)
health <- health * DALYprice * 1E-6 * 10 # from € to M€ for 10 a
climate <- oapply(
unkeep(emissions, prevresults = TRUE, sources = TRUE),
cols = c(
"Fuel",
"Emission_site",
"Emission_height"
),
FUN = sum
)
climate <- climate * emissionprice * 1E-6 * 10 # from € to M€ for 10 a
climate <- oapply(climate, cols = "Pollutant", FUN = sum) # Remove joint index Pollutant
out <- combine(health, climate)
# out$Cost <- factor(out$Cost, levels = c(
# "Management cost",
# "Operation cost",
# "Fuel cost",
# "Investment cost",
# "Health",
# "Climate",
# "Profit"
# ))
out$Time <- as.numeric(as.character(out$Time))
out <- oapply(unkeep(out, sources = TRUE), cols = "", FUN = sum)
return(out)
}
)
EnergyNetworkCost <- Ovariable("EnergyNetworkCost",
# Energy network costs using profit (includes excesses and deficits but is not strictly plant or fuel-specific.
dependencies =
data.frame(
Name = c(
"plantParameters",
"EnergyNetworkOptim",
"fuelUse",
"fuelPrice",
"temperdays"
),
Ident = c(
NA,
"Op_en5141/EnergyNetworkOptim", # [[Energy balance]]
NA,
"Op_en4151/fuelPrice", # [[Prices of fuels in heat production]]
NA
)
),
formula = function(...) {
oper <- plantParameters[plantParameters$Parameter == "Max" , colnames(plantParameters@output) != "Parameter"]
result(oper)[result(oper) != 0] <- 1
oper <- plantParameters * oper
# Take the first year when a plant is operated and put all investment cost there.
investment <- oper[oper$Parameter == "Investment cost" , ]
investment <- investment[result(investment) > 0 , ]
investment <- investment[order(investment@output$Time) , ]
investment <- investment[!duplicated(investment@output[investment@marginal & colnames(investment@output) != "Time"]) , ]
investment <- unkeep(investment, sources = TRUE)
proinve <- investment # Profit does not include investment, so must subtract.
proinve$Parameter <- "Profit"
maintenance <- oper[oper$Parameter == "Management cost" , ]
maintenance <- unkeep(maintenance, sources = TRUE)
promain <- maintenance # Profit does not include maintenance, so must subtract.
promain$Parameter <- "Profit"
profit <- EnergyNetworkOptim[EnergyNetworkOptim$Process_variable_name == "Profit" , ]
colnames(profit@output)[colnames(profit@output) == "Process_variable_name"] <- "Parameter"
profit <- profit * temperdays * 10 * 1E-6 # For 10-year periods, € -> M€
profit <- oapply(profit, cols = c("Temperature"), FUN = sum)
profit <- unkeep(profit, cols = c("Process_variable_type"), sources = TRUE, prevresults = TRUE)
profit <- profit * Ovariable(output = data.frame(Plant = "Profit", Result = -1), marginal = c(TRUE, FALSE))
EnergyFlow <- EnergyNetworkOptim[EnergyNetworkOptim$Process_variable_type == "Activity" ,
colnames(EnergyNetworkOptim@output) != "Process_variable_type"
]
colnames(EnergyFlow@output)[colnames(EnergyFlow@output) == "Process_variable_name"] <- "Plant"
EnergyFlow <- unkeep(EnergyFlow, sources = TRUE, prevresults = TRUE)
EnergyFlow <- EnergyFlow * temperdays * 24 * 10 # MW to MWh/10 a
EnergyFlow <- oapply(EnergyFlow, cols = c("Temperature"), FUN = sum)
opercost <- EnergyFlow * oper[oper$Parameter == "Operation cost" , ] * 1E-6 # € to M€
fuelcost <- fuelPrice * fuelUse[!fuelUse$Fuel %in% c("Heat", "Electricity") , ] / 3.6E+8 # From MJ to MWh, € to M€, and a to 10 a
colnames(fuelcost@output)[colnames(fuelcost@output) == "Fuel"] <- "Parameter"
fuelcost$Parameter <- "Fuel cost"
fuelcost <- oapply(unkeep(fuelcost, sources = TRUE), cols = c("Burner"), FUN = sum)
cost <- combine(
investment, # Investment cost
maintenance, # Maintenance cost
opercost, # Operation cost
fuelcost, # Fuel cost
profit, # Profit without maintenance and investment
proinve, # Investment cost that reduces profit
promain # Maintenance cost that reduces profit
)
colnames(cost@output)[colnames(cost@output) == "Parameter"] <- "Cost"
cost <- oapply(unkeep(cost, sources = TRUE), cols = "", FUN = sum) # aggregate rows
cost$Time <- as.numeric(as.character(cost$Time))
marginals <- character()
for(i in colnames(cost@output)[cost@marginal]) {
if(any(is.na(cost@output[[i]]))) marginals <- c(marginals, i)
}
if(length(marginals) > 0) {
cost@output <- fillna(cost@output, marginals)
warning(paste("In combine had to fillna marginals", marginals, "\n"))
}
return(cost)
}
)
plantCost <- Ovariable("plantCost",
# Energy network costs using income from heat and electricity as profit.
# This is strictly plant and fuel-specific but does not account for excesses and deficits.
dependencies =
data.frame(
Name = c(
"plantParameters",
"EnergyNetworkOptim",
"fuelUse",
"fuelPrice",
"temperdays"
),
Ident = c(
NA,
"Op_en5141/EnergyNetworkOptim", # [[Energy balance]]
NA,
"Op_en4151/fuelPrice", # [[Prices of fuels in heat production]]
NA
)
),
formula = function(...) {
oper <- plantParameters[plantParameters$Parameter == "Max" , colnames(plantParameters@output) != "Parameter"]
result(oper)[result(oper) != 0] <- 1
oper <- plantParameters * oper
# Take the first year when a plant is operated and put all investment cost there.
investment <- oper[oper$Parameter == "Investment cost" , ]
investment <- investment[result(investment) > 0 , ]
investment <- investment[order(investment@output$Time) , ]
investment <- investment[!duplicated(investment@output[investment@marginal & colnames(investment@output) != "Time"]) , ]
investment <- unkeep(investment, sources = TRUE)
maintenance <- oper[oper$Parameter == "Management cost" , ]
maintenance <- unkeep(maintenance, sources = TRUE)
EnergyFlow <- EnergyNetworkOptim[EnergyNetworkOptim$Process_variable_type == "Activity" ,
colnames(EnergyNetworkOptim@output) != "Process_variable_type"
]
colnames(EnergyFlow@output)[colnames(EnergyFlow@output) == "Process_variable_name"] <- "Plant"
EnergyFlow <- unkeep(EnergyFlow, sources = TRUE, prevresults = TRUE)
EnergyFlow <- EnergyFlow * temperdays * 24 * 10 # MW to MWh/10 a
EnergyFlow <- oapply(EnergyFlow, cols = c("Temperature"), FUN = sum)
opercost <- EnergyFlow * oper[oper$Parameter == "Operation cost" , ] * 1E-6 # € to M€
# Include all fuels in fuelcost, also Heat and electricity to get the profits.
fuelcost <- fuelPrice * fuelUse[fuelUse$Plant != "Domestic" , ] / 3.6E+8
# From MJ to MWh, € to M€, and a to 10 a
fuelcost$Fuel <- NULL
fuelcost$Parameter <- "Fuel cost"
fuelcost$Parameter[result(fuelcost) < 0] <- "Sales proceeds"
fuelcost <- oapply(unkeep(fuelcost, sources = TRUE), cols = c("Burner"), FUN = sum)
cost <- combine(
investment, # Investment cost
maintenance, # Maintenance cost
opercost, # Operation cost
fuelcost # Fuel cost and profit
)
colnames(cost@output)[colnames(cost@output) == "Parameter"] <- "Cost"
cost <- oapply(unkeep(cost, sources = TRUE), cols = "", FUN = sum) # aggregate rows
cost$Time <- as.numeric(as.character(cost$Time))
return(cost)
}
)
totalCost <- Ovariable("totalCost",
dependencies = data.frame(
Name = c(
"plantCost",
"externalCost"
)
),
formula = function(...) {
out <- combine(plantCost, externalCost)
# out$Cost <- factor(out$Cost, levels = c(
# "Management cost",
# "Operation cost",
# "Fuel cost",
# "Investment cost",
# "Health",
# "Climate",
# "Sales proceeds",
# "Profit"
# ))
out$Time <- as.numeric(as.character(out$Time))
out <- oapply(unkeep(out, sources = TRUE), cols = "", FUN = sum)
return(out)
}
)
objects.store(emissionprice, externalCost, EnergyNetworkCost, plantCost, totalCost)
cat("Ovariables emissionprice, externalCost, EnergyNetworkCost, plantCost, totalCost stored.\n")
| |