Skip to content

Commit

Permalink
settings file
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jul 1, 2024
1 parent fa3ac72 commit d1fc6e3
Show file tree
Hide file tree
Showing 24 changed files with 46 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

- name: Install Project Dependencies
run: |
julia -e 'import Pkg; Pkg.add("Plots"); Pkg.add("ArgParse"); Pkg.add("FilePathsBase"); Pkg.instantiate()'
julia -e 'import Pkg; Pkg.add("Plots"); Pkg.add("FilePathsBase");Pkg.add("JSON"); Pkg.instantiate()'
- name: Run energy_community.jl script
run: julia src/energy_community.jl --num_nodes 6 --pv_nodes "1,2,3,4" --battery_nodes "2,3,5" --cooperative --cooperative_nodes "1,2,3,4"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Thumbs.db
# Ignore logs and databases
*.log
*.sqlite
outputs/images_EC/



9 changes: 9 additions & 0 deletions settings/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"num_nodes": 16,
"pv_nodes": [1, 2, 3, 4, 6, 8, 9, 10],
"battery_nodes": [2, 3, 5, 7, 9, 10, 11, 12],
"cooperative": true,
"cooperative_nodes": [1, 2, 3, 4],
"dt": 0.2,
"simulation_hours": 24
}
64 changes: 18 additions & 46 deletions src/energy_community.jl
Original file line number Diff line number Diff line change
@@ -1,54 +1,24 @@
# energy_community.jl
using ArgParse
using JSON
using FilePathsBase: mkpath # For creating directories if they don't exist
include("simulation.jl")
include("cleanup.jl")
cleanup_images()
function parse_arguments()
s = ArgParseSettings()

@add_arg_table! s begin
"--num_nodes"
help = "Number of nodes in the energy community"
arg_type = Int
default = 5

"--pv_nodes"
help = "Comma-separated list of nodes with PV systems"
arg_type = String
default = "1,3,4"

"--battery_nodes"
help = "Comma-separated list of nodes with batteries"
arg_type = String
default = "2,3,5"

"--cooperative"
help = "Flag to enable cooperative behavior"
action = :store_true

"--cooperative_nodes"
help = "Comma-separated list of nodes that are cooperative"
arg_type = String
default = "1,2"
end

parsed_args = parse_args(s)

num_nodes = parsed_args["num_nodes"]
pv_nodes = [parse(Int, x) for x in split(parsed_args["pv_nodes"], ",")]
battery_nodes = [parse(Int, x) for x in split(parsed_args["battery_nodes"], ",")]
cooperative = parsed_args["cooperative"]
cooperative_nodes = [parse(Int, x) for x in split(parsed_args["cooperative_nodes"], ",")]

return num_nodes, pv_nodes, battery_nodes, cooperative, cooperative_nodes
end
function load_config(file_path::String)
config = JSON.parsefile(file_path)
num_nodes = config["num_nodes"]
pv_nodes = config["pv_nodes"]
battery_nodes = config["battery_nodes"]
cooperative = config["cooperative"]
cooperative_nodes = config["cooperative_nodes"]
dt = config["dt"]
simulation_hours = config["simulation_hours"]

function central_management(num_nodes, pv_nodes, battery_nodes, cooperative, cooperative_nodes)
dt = 1.0
simulation_hours = 24
return num_nodes, pv_nodes, battery_nodes, cooperative, cooperative_nodes, dt, simulation_hours
end

times, solar_generation_data, load_profile_data, battery_soc, grid_interactions, net_profit, transaction_matrix = simulate_energy_community(num_nodes, SIMULATION_HOURS, DT, pv_nodes, battery_nodes, cooperative, cooperative_nodes)
function central_management(num_nodes, pv_nodes, battery_nodes, cooperative, cooperative_nodes, dt, simulation_hours)
times, solar_generation_data, load_profile_data, battery_soc, grid_interactions, net_profit, transaction_matrix = simulate_energy_community(num_nodes, simulation_hours, dt, pv_nodes, battery_nodes, cooperative, cooperative_nodes)

println("Total Net Profit for $num_nodes nodes: $net_profit Euros")

Expand Down Expand Up @@ -98,5 +68,7 @@ function central_management(num_nodes, pv_nodes, battery_nodes, cooperative, coo
end
end

num_nodes, pv_nodes, battery_nodes, cooperative, cooperative_nodes = parse_arguments()
central_management(num_nodes, pv_nodes, battery_nodes, cooperative, cooperative_nodes)
# Load configuration from JSON file
config_path = joinpath(@__DIR__, "..", "settings", "config.json")
num_nodes, pv_nodes, battery_nodes, cooperative, cooperative_nodes, dt, simulation_hours = load_config(config_path)
central_management(num_nodes, pv_nodes, battery_nodes, cooperative, cooperative_nodes, dt, simulation_hours)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
28 changes: 17 additions & 11 deletions src/simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const BATTERY_CAPACITY = 200.0 # kWh
const BATTERY_MAX_CHARGE_RATE = 50.0 # kW
const BATTERY_MAX_DISCHARGE_RATE = 50.0 # kW
const BATTERY_INITIAL_SOC = 0.5 # Initial state of charge (percentage)
const BATTERY_MIN_SOC = 0.2 # Minimum state of charge (percentage)
const BATTERY_MAX_SOC = 0.8 # Maximum state of charge (percentage)

# Functions for solar generation and grid price
function solar_generation(t, node, amplitude_factor)
Expand Down Expand Up @@ -57,6 +59,11 @@ function simulate_energy_community(num_nodes, simulation_hours, dt, pv_nodes, ba
net_profit = 0.0
node_profits = zeros(num_nodes)

# Initialize battery SOC
for n in battery_nodes
battery_soc[n, 1] = BATTERY_CAPACITY * BATTERY_INITIAL_SOC
end

solar_generation_data = [if node in pv_nodes solar_generation(t, node, 10.0 * node) else 0 end for t in times, node in 1:num_nodes]
load_profile_data = [load_profile(t, node) for t in times, node in 1:num_nodes]
transaction_matrix = zeros(length(cooperative_nodes), length(cooperative_nodes), num_steps)
Expand Down Expand Up @@ -89,33 +96,32 @@ function simulate_energy_community(num_nodes, simulation_hours, dt, pv_nodes, ba
grid_interactions[n, i] += -excess_power
node_profits[n] += excess_power * current_grid_price * dt
end
battery_soc[n, i+1] = min(battery_soc[n, i] + charge_power * dt, BATTERY_CAPACITY)
battery_soc[n, i+1] = min(battery_soc[n, i] + charge_power * dt, BATTERY_CAPACITY * BATTERY_MAX_SOC)
else
grid_interactions[n, i] += -net_power_available[n]
node_profits[n] += net_power_available[n] * current_grid_price * dt
end
else
deficit_power = -net_power_available[n]
if n in battery_nodes
if battery_soc[n, i] >= deficit_power * dt
battery_soc[n, i+1] = battery_soc[n, i] - deficit_power * dt
else
needed_power = deficit_power - battery_soc[n, i] / dt
grid_interactions[n, i] += needed_power
node_profits[n] -= needed_power * current_grid_price * dt
battery_soc[n, i+1] = 0
if battery_soc[n, i] > BATTERY_MIN_SOC * BATTERY_CAPACITY
discharge_power = min(deficit_power, BATTERY_MAX_DISCHARGE_RATE, (battery_soc[n, i] - BATTERY_MIN_SOC * BATTERY_CAPACITY) / dt)
battery_soc[n, i+1] = battery_soc[n, i] - discharge_power * dt
deficit_power -= discharge_power
end
if deficit_power > 0
grid_interactions[n, i] += deficit_power
node_profits[n] -= deficit_power * current_grid_price * dt
end
else
grid_interactions[n, i] += deficit_power
node_profits[n] -= deficit_power * current_grid_price * dt
end
end

battery_soc[n, i+1] = clamp(battery_soc[n, i+1], 0, BATTERY_CAPACITY)
battery_soc[n, i+1] = clamp(battery_soc[n, i+1], BATTERY_MIN_SOC * BATTERY_CAPACITY, BATTERY_CAPACITY * BATTERY_MAX_SOC)
end
end

return times, solar_generation_data, load_profile_data, battery_soc, grid_interactions, node_profits, transaction_matrix
end


0 comments on commit d1fc6e3

Please sign in to comment.