# Copyright (c) 2019 Kelvin Say # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. library(ggplot2) library(reshape2) library(data.table) library(lubridate) # ------------------------------------------------------------------------------------ # ausgrid_data <- readRDS('_Scenarios/ConsumptionPatterns_Ausgrid/ausgrid_data.rds') # customer_results <- readRDS('_Outputs/P2.Ausgrid_CustomerResults_Each.rds') # all_customer_results <- readRDS('_Outputs/P2.Ausgrid_CustomerResults_All.rds') # # # obs <- data.table('year' = 1:(NROW(all_customer_results$'Annual Energy Summary')), # 'pv' = all_customer_results$'Annual Energy Summary'$'Installed PV Capacity (kWp)', # 'battery' = all_customer_results$'Annual Energy Summary'$'Installed Battery Capacity (kWh)') # mobs <- melt(obs, id.vars = "year") # ggplot(mobs) + aes(x = year, y = value, group = variable, fill = variable) + geom_area(alpha = 0.5, position = "identity") + geom_line() + theme_minimal() # # obs <- data.table('year' = 1:(NROW(all_customer_results$'Annual Energy Summary')), # 'self-consumed' = all_customer_results$'Annual Energy Summary'$'Self-Consumed (kWh)')#, # # 'imported' = all_customer_results$'Annual Energy Summary'$'Imported (kWh)') # mobs <- melt(obs, id.vars = "year") # ggplot(mobs) + aes(x = year, y = value, group = variable, fill = variable) + geom_step() + theme_minimal() # ----------------------------------------------------------------------------------------------------------------- # Combine all analysis into a single object # ----------------------------------------------------------------------------------------------------------------- # customer_result_names <- c('P2.Ausgrid_CustomerResults_001_220', # 'P2.Ausgrid_CustomerResults_221_300') customer_result_names <- c('P2.Ausgrid_ZeroRebate_CustomerResults_001_004', 'P2.Ausgrid_ZeroRebate_CustomerResults_005_200', 'P2.Ausgrid_ZeroRebate_CustomerResults_201_270') combine_analysis <- function() { for (i in 1:length(customer_result_names)) { if (i == 1) { all_customer_results <- eval(parse(text=paste0(customer_result_names[i], '_All'))) customer_imported <- data.table(Year = 1:NROW(eval(parse(text=paste0(customer_result_names[i], '_All')))$'Annual Energy Summary')) customer_exported <- data.table(Year = 1:NROW(eval(parse(text=paste0(customer_result_names[i], '_All')))$'Annual Energy Summary')) } else { all_customer_results$'Annual Energy Summary' <- all_customer_results$'Annual Energy Summary' + eval(parse(text=paste0(customer_result_names[i], '_All')))$'Annual Energy Summary' all_customer_results$'Net Power Profile (W)' <- all_customer_results$'Net Power Profile (W)' + eval(parse(text=paste0(customer_result_names[i], '_All')))$'Net Power Profile (W)' } for (j in 1:(length(eval(parse(text=paste0(customer_result_names[i], '_Each')))))) { customer_id <- eval(parse(text=paste0(customer_result_names[i], '_Each')))[[j]]$'Customer ID' col_name <- paste0("ag", sprintf("%03d", customer_id)) customer_imported$new <- eval(parse(text=paste0(customer_result_names[i], '_Each')))[[j]]$'Annual Energy Summary'$'Imported (kWh)' customer_exported$new <- eval(parse(text=paste0(customer_result_names[i], '_Each')))[[j]]$'Annual Energy Summary'$'Exported (kWh)' setnames(customer_imported, "new", col_name) setnames(customer_exported, "new", col_name) } } intervals_per_year <- 365 * 3600 * 24 / g.scenario_simulation$'Time Step (s)' datetime_start <- ymd_hms("2017-01-01 00:00:00") + seconds(g.scenario_simulation$'Time Step (s)' * (0:(intervals_per_year-1))) datetime_start <- rep(datetime_start, g.scenario_simulation$'Number of Years') for (i in 2:g.scenario_simulation$'Number of Years') { year(datetime_start[(i-1)*intervals_per_year+(1:intervals_per_year)]) <- 2017 + (i-1) } datetime_finish <- datetime_start + seconds(g.scenario_simulation$'Time Step (s)') convert_W_to_Wh <- g.scenario_simulation$'Time Step (s)' / 3600 net_grid_profile <- data.table('Start' = datetime_start, 'Finish' = datetime_finish, 'Net Power (W)' = all_customer_results$'Net Power Profile (W)', 'Net Energy (kWh)' = all_customer_results$'Net Power Profile (W)' * convert_W_to_Wh / 1000) annual_grid_profile <- data.table('Year' = 1:g.scenario_simulation$'Number of Years', 'Load (MWh)' = numeric(g.scenario_simulation$'Number of Years'), 'Imported (MWh)' = numeric(g.scenario_simulation$'Number of Years'), 'Self-Consumed (MWh)' = numeric(g.scenario_simulation$'Number of Years'), 'Exported (MWh)' = numeric(g.scenario_simulation$'Number of Years'), 'Installed PV Capacity (MWp)' = numeric(g.scenario_simulation$'Number of Years'), 'Installed Battery Capacity (MWh)' = numeric(g.scenario_simulation$'Number of Years')) for (t in 1:g.scenario_simulation$'Number of Years') { year_index_start <- (t-1) * intervals_per_year + 1 year_index_end <- t * intervals_per_year annual_netprofile <- net_grid_profile[year_index_start:year_index_end] annual_load <- all_customer_results$'Annual Energy Summary'$'Load (kWh)'[t] / 1000 annual_imported <- sum(annual_netprofile[annual_netprofile$'Net Energy (kWh)' > 0]$'Net Energy (kWh)') / 1000 annual_selfconsumed <- annual_load - annual_imported annual_exported <- -1 * sum(annual_netprofile[annual_netprofile$'Net Energy (kWh)' < 0]$'Net Energy (kWh)') / 1000 annual_grid_profile$'Load (MWh)'[t] <- annual_load annual_grid_profile$'Imported (MWh)'[t] <- annual_imported annual_grid_profile$'Self-Consumed (MWh)'[t] <- annual_selfconsumed annual_grid_profile$'Exported (MWh)'[t] <- annual_exported annual_grid_profile$'Installed PV Capacity (MWp)'[t] <- all_customer_results$'Annual Energy Summary'$'Installed PV Capacity (kWp)'[t] / 1000 annual_grid_profile$'Installed Battery Capacity (MWh)'[t] <- all_customer_results$'Annual Energy Summary'$'Installed Battery Capacity (kWh)'[t] / 1000 } return(list('Grid' = annual_grid_profile, 'Grid NetProfile' = net_grid_profile, 'Combined Customers' = all_customer_results, 'Each Customer Imported (kWh)' = customer_imported, 'Each Customer Exported (kWh)' = customer_exported)) } get_day_netprofile <- function (i_month, i_day, i_netprofile = netprofile) { intervals_per_year <- 365 * 3600 * 24 / g.scenario_simulation$'Time Step (s)' for (t in 1:g.scenario_simulation$'Number of Years') { year_index_start <- (t-1) * intervals_per_year + 1 year_index_end <- t * intervals_per_year annual_netprofile <- i_netprofile[year_index_start:year_index_end] day_netprofile <- annual_netprofile[month(annual_netprofile$'Start') == i_month & day(annual_netprofile$'Start') == i_day] if (t == 1) { collated_daily_netprofile <- data.table('Start' = day_netprofile$'Start', 'Finish' = day_netprofile$'Finish', '0' = day_netprofile$'Net Energy (kWh)') } else { col_name <- sprintf("%d", t-1) collated_daily_netprofile$new <- day_netprofile$'Net Energy (kWh)' setnames(collated_daily_netprofile, "new", col_name) } } return(collated_daily_netprofile) } get_monthly_imported <- function(i_netprofile = netprofile) { collated_monthly_imported <- data.table('Month' = 1:12) intervals_per_year <- 365 * 3600 * 24 / g.scenario_simulation$'Time Step (s)' for (t in 1:g.scenario_simulation$'Number of Years') { year_index_start <- (t-1) * intervals_per_year + 1 year_index_end <- t * intervals_per_year annual_netprofile <- i_netprofile[year_index_start:year_index_end] month_imported <- rep(0, 12) for (m in 1:12) { month_netprofile <- annual_netprofile[month(annual_netprofile$'Start') == m] month_imported[m] <- sum(month_netprofile[month_netprofile$'Net Energy (kWh)' > 0]$'Net Energy (kWh)' / 1000) } col_name <- sprintf("%d", t-1) collated_monthly_imported$new <- month_imported setnames(collated_monthly_imported, "new", col_name) } return(collated_monthly_imported) } get_monthly_exported <- function(i_netprofile = netprofile) { collated_monthly_exported <- data.table('Month' = 1:12) intervals_per_year <- 365 * 3600 * 24 / g.scenario_simulation$'Time Step (s)' for (t in 1:g.scenario_simulation$'Number of Years') { year_index_start <- (t-1) * intervals_per_year + 1 year_index_end <- t * intervals_per_year annual_netprofile <- i_netprofile[year_index_start:year_index_end] month_exported <- rep(0, 12) for (m in 1:12) { month_netprofile <- annual_netprofile[month(annual_netprofile$'Start') == m] month_exported[m] <- -1 * sum(month_netprofile[month_netprofile$'Net Energy (kWh)' < 0]$'Net Energy (kWh)' / 1000) } col_name <- sprintf("%d", t-1) collated_monthly_exported$new <- month_exported setnames(collated_monthly_exported, "new", col_name) } return(collated_monthly_exported) } get_base_grid <- function() { first_valid_index <- which(ausgrid_data$Summary$'Data Complete' == TRUE)[1] datetime_start <- ausgrid_data$Customers[[first_valid_index]]$Profile$'Date Time (Start)' datetime_finish <- ausgrid_data$Customers[[first_valid_index]]$Profile$'Date Time (End)' num_intervals <- length(datetime_start) grid_load <- data.table('Start' = datetime_start, 'Finish' = datetime_finish, 'Load (kWh)' = numeric(num_intervals)) for (i in 1:length(ausgrid_data$Customers)) { if (ausgrid_data$Summary$'Data Complete'[i]) { grid_load$'Load (kWh)' <- grid_load$'Load (kWh)' + ausgrid_data$Customers[[i]]$Profile$'Load (kWh)' } } monthly_load <- data.table('Month' = 1:12, 'Load (MWh)' = numeric(12)) for (m in 1:12) { month_netprofile <- grid_load[month(grid_load$'Start') == m] monthly_load$'Load (MWh)'[m] <- sum(month_netprofile$'Load (kWh)' / 1000) } return(list('Detailed' = grid_load, 'Monthly' = monthly_load)) }