Skip to content

Commit

Permalink
Merge pull request #1 from psrenergy/rs/tests
Browse files Browse the repository at this point in the history
Tests improvements
  • Loading branch information
raphasampaio authored Aug 19, 2024
2 parents fe16edc + f76d77d commit ae43398
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ docs/site/

format/Manifest.toml
revise/Manifest.toml

*.sqlite
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
julia = "1.9"

[extras]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"

[targets]
test = ["Test", "TimerOutputs"]
test = ["DataFrames", "Test", "TimerOutputs"]
28 changes: 6 additions & 22 deletions src/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ struct Cache
end
end

function file_size(cache::Cache)
return Base.summarysize(cache.path)
end

function files(cache::Cache)
return readdir(cache.path)
end

function finalize!(cache::Cache)
if isdir(cache.path)
if cache.verbose
Expand Down Expand Up @@ -59,30 +51,22 @@ function build_filename(kwargs...)
return join(vector, "-") * ".bin"
end

function update!(collections::AbstractCollections, db::DatabaseSQLite, cache::Cache; kwargs...)
function update!(inputs::AbstractInputs, cache::Cache; kwargs...)
filename = build_filename(kwargs...)
path = joinpath(cache.path, filename)

if isfile(path)
if cache.verbose
println("Loading cache: $path")
println("Loading cache ($(Base.summarysize(path)) kb): $path")
end

collections = Serialization.deserialize(path)
inputs.collections = Serialization.deserialize(path)
else
update!(collections, db; kwargs...)
update!(inputs.collections, inputs.db; kwargs...)
Serialization.serialize(path, inputs.collections)

if cache.verbose
println("Saving cache: $path")
println("Saving cache ($(Base.summarysize(path)) kb): $path")
end

Serialization.serialize(path, collections)
end

return nothing
end

function update!(inputs::AbstractInputs, cache::Cache; kwargs...)
update!(inputs.collections, inputs.db, cache; kwargs...)
return nothing
end
77 changes: 77 additions & 0 deletions test/build.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function build_thermal_plant_label(i::Integer)
return "Thermal Plant $i"
end

function build_thermal_plant_shutdown_cost(i::Integer)
return Float64(i)
end

function build_thermal_plant_max_startups(i::Integer)
return i
end

function build_hydro_plant_label(i::Integer)
return "Hydro Plant $i"
end

function build_hydro_plant_initial_volume(i::Integer)
return Float64(i)
end

function build_hydro_plant_has_commitment(i::Integer)
return i % 2 == 0
end

function build_hydro_plant_existing(date_time::DateTime)
return Dates.month(date_time) % 2 == 0
end

function build_hydro_plant_max_generation(date_time::DateTime)
return Float64(Dates.month(date_time))
end

function build_database(path::AbstractString)
if isfile(path)
rm(path; force = true)
end

path_schema = joinpath(@__DIR__, "schema.sql")

db = PSRDatabaseSQLite.create_empty_db_from_schema(
path,
path_schema;
force = true,
)

PSRDatabaseSQLite.create_element!(
db,
"Configuration";
label = "Bridge",
)

for i in 1:THERMAL_PLANT_SIZE
add_thermal_plant!(
db;
label = build_thermal_plant_label(i),
shutdown_cost = build_thermal_plant_shutdown_cost(i),
max_startups = build_thermal_plant_max_startups(i),
)
end

for i in 1:HYDRO_PLANT_SIZE
add_hydro_plant!(db;
label = build_hydro_plant_label(i),
initial_volume = build_hydro_plant_initial_volume(i),
has_commitment = build_hydro_plant_has_commitment(i) ? 1 : 0,
parameters = DataFrame(;
date_time = DATE_TIMES,
existing = [build_hydro_plant_existing(date_time) ? 1 : 0 for date_time in DATE_TIMES],
max_generation = [build_hydro_plant_max_generation(date_time) for date_time in DATE_TIMES],
),
)
end

PSRDatabaseSQLite.close!(db)

return nothing
end
160 changes: 113 additions & 47 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using PSRBridge

using DataFrames
using Dates
import PSRClassesInterface as PSRI
using Random
Expand All @@ -10,47 +11,64 @@ using TimerOutputs
const PSRDatabaseSQLite = PSRI.PSRDatabaseSQLite
const DatabaseSQLite = PSRI.PSRDatabaseSQLite.DatabaseSQLite

@collection @kwdef mutable struct ThermalPlant <: AbstractCollection
id::String = "ThermalPlant"
const ITERATIONS = 10
const THERMAL_PLANT_SIZE = 20
const HYDRO_PLANT_SIZE = 20
const DATE_TIMES = [DateTime(2025, month, 1) for month in 1:12]

include("build.jl")

@collection @kwdef mutable struct HydroPlant <: AbstractCollection
id::String = "HydroPlant"

label::StaticVectorData{String} = "label"
initial_volume::StaticVectorData{Float64} = "initial_volume"
has_commitment::StaticVectorData{Bool} = "has_commitment"
max_startups::StaticVectorData{Int} = "max_startups"
shutdown_cost::StaticVectorData{Float64} = "shutdown_cost"

existing::TimeSeriesData{Bool} = "existing"
min_generation::TimeSeriesData{Float64} = "min_generation"
max_generation::TimeSeriesData{Float64} = "max_generation"
om_cost::TimeSeriesData{Float64} = "om_cost"
startup_cost::TimeSeriesData{Float64} = "startup_cost"

# non_controllable_spillage::StaticVectorData{Bool} = "non_controllable_spillage"

# bus_index::MapData = ("Bus", "id")
# agent_index::MapData = ("Agent", "id")
# gauging_station_index::MapData = ("GaugingStation", "id")
# turbine_to_index::MapData = ("HydroPlant", "turbine_to")
# spill_to_index::MapData = ("HydroPlant", "spill_to")
end

function adjust!(collection::ThermalPlant, collections::AbstractCollections, db::DatabaseSQLite; kwargs...)
function add_hydro_plant!(db::DatabaseSQLite; kwargs...)
PSRI.create_element!(db, "HydroPlant"; kwargs...)
return nothing
end

@collection @kwdef mutable struct HydroPlant <: AbstractCollection
id::String = "HydroPlant"
function adjust!(collection::HydroPlant, collections::AbstractCollections, db::DatabaseSQLite; kwargs...)
return nothing
end

@collection @kwdef mutable struct ThermalPlant <: AbstractCollection
id::String = "ThermalPlant"

label::StaticVectorData{String} = "label"
initial_volume::StaticVectorData{Float64} = "initial_volume"
has_commitment::StaticVectorData{Bool} = "has_commitment"
non_controllable_spillage::StaticVectorData{Bool} = "non_controllable_spillage"
shutdown_cost::StaticVectorData{Float64} = "shutdown_cost"
max_startups::StaticVectorData{Int} = "max_startups"

existing::TimeSeriesData{Float64} = "existing"
production_factor::TimeSeriesData{Float64} = "production_factor"
min_generation::TimeSeriesData{Float64} = "min_generation"
max_generation::TimeSeriesData{Float64} = "max_generation"
min_turbining::TimeSeriesData{Float64} = "min_turbining"
max_turbining::TimeSeriesData{Float64} = "max_turbining"
min_volume::TimeSeriesData{Float64} = "min_volume"
max_volume::TimeSeriesData{Float64} = "max_volume"
min_outflow::TimeSeriesData{Float64} = "min_outflow"
om_cost::TimeSeriesData{Float64} = "om_cost"

bus_index::MapData = ("Bus", "id")
agent_index::MapData = ("Agent", "id")
gauging_station_index::MapData = ("GaugingStation", "id")
turbine_to_index::MapData = ("HydroPlant", "turbine_to")
spill_to_index::MapData = ("HydroPlant", "spill_to")
# has_commitment::StaticVectorData{Bool} = "has_commitment"

# existing::TimeSeriesData{Bool} = "existing"
# min_generation::TimeSeriesData{Float64} = "min_generation"
# max_generation::TimeSeriesData{Float64} = "max_generation"
# om_cost::TimeSeriesData{Float64} = "om_cost"
# startup_cost::TimeSeriesData{Float64} = "startup_cost"
end

function add_thermal_plant!(db::DatabaseSQLite; kwargs...)
PSRI.create_element!(db, "ThermalPlant"; kwargs...)
return nothing
end

function adjust!(collection::ThermalPlant, collections::AbstractCollections, db::DatabaseSQLite; kwargs...)
return nothing
end

@kwdef mutable struct Collections <: AbstractCollections
Expand All @@ -64,49 +82,97 @@ end
end

function test_all()
iterations = 2
path = raw"C:\Development\BidBasedDispatch\slow\study.bid_based_dispatch"
path = joinpath(@__DIR__, "db.sqlite")
build_database(path)

db = PSRI.load_study(PSRI.PSRDatabaseSQLiteInterface(), path, read_only = true)

inputs = Inputs(; db = db)
cache = Cache(verbose = true)
cache = Cache(verbose = false)

@timeit "initialize!" initialize!(inputs)

@show thermal_plant_label(inputs.collections.thermal_plant, 1)
@show thermal_plant_label(inputs.collections, 1)
@show thermal_plant_label(inputs, 1)
for i in 1:HYDRO_PLANT_SIZE
label = build_hydro_plant_label(i)
@test hydro_plant_label(inputs.collections.hydro_plant, i) == label
@test hydro_plant_label(inputs.collections, i) == label
@test hydro_plant_label(inputs, i) == label

initial_volume = Float64(i)
@test hydro_plant_initial_volume(inputs.collections.hydro_plant, i) == initial_volume
@test hydro_plant_initial_volume(inputs.collections, i) == initial_volume
@test hydro_plant_initial_volume(inputs, i) == initial_volume

has_commitment = build_hydro_plant_has_commitment(i)
@test hydro_plant_has_commitment(inputs.collections.hydro_plant, i) == has_commitment
@test hydro_plant_has_commitment(inputs.collections, i) == has_commitment
@test hydro_plant_has_commitment(inputs, i) == has_commitment
end

for i in 1:THERMAL_PLANT_SIZE
label = build_thermal_plant_label(i)
@test thermal_plant_label(inputs.collections.thermal_plant, i) == label
@test thermal_plant_label(inputs.collections, i) == label
@test thermal_plant_label(inputs, i) == label

shutdown_cost = build_thermal_plant_shutdown_cost(i)
@test thermal_plant_shutdown_cost(inputs.collections.thermal_plant, i) == shutdown_cost
@test thermal_plant_shutdown_cost(inputs.collections, i) == shutdown_cost
@test thermal_plant_shutdown_cost(inputs, i) == shutdown_cost

max_startups = build_thermal_plant_max_startups(i)
@test thermal_plant_max_startups(inputs.collections.thermal_plant, i) == max_startups
@test thermal_plant_max_startups(inputs.collections, i) == max_startups
@test thermal_plant_max_startups(inputs, i) == max_startups
end

@show Base.doc(thermal_plant_label)

@timeit "not cached" begin
for _ in 1:iterations
for month in 1:12
@timeit "update!" update!(inputs, date_time = DateTime(2025, month, 1))
for _ in 1:ITERATIONS
for date_time in DATE_TIMES
@timeit "not cached - update!" update!(inputs, date_time = date_time)

for i in 1:HYDRO_PLANT_SIZE
existing = build_hydro_plant_existing(date_time)
@test hydro_plant_existing(inputs.collections.hydro_plant, i) == existing
@test hydro_plant_existing(inputs.collections, i) == existing
@test hydro_plant_existing(inputs, i) == existing

max_generation = build_hydro_plant_max_generation(date_time)
@test hydro_plant_max_generation(inputs.collections.hydro_plant, i) == max_generation
@test hydro_plant_max_generation(inputs.collections, i) == max_generation
@test hydro_plant_max_generation(inputs, i) == max_generation
end
end
end

@timeit "cached" begin
for _ in 1:iterations
for month in 1:12
@timeit "update!" update!(inputs, cache, date_time = DateTime(2025, month, 1))
for _ in 1:ITERATIONS
for date_time in DATE_TIMES
@timeit "cached - update!" update!(inputs, cache, date_time = date_time)

for i in 1:HYDRO_PLANT_SIZE
existing = build_hydro_plant_existing(date_time)
@test hydro_plant_existing(inputs.collections.hydro_plant, i) == existing
@test hydro_plant_existing(inputs.collections, i) == existing
@test hydro_plant_existing(inputs, i) == existing

max_generation = build_hydro_plant_max_generation(date_time)
@test hydro_plant_max_generation(inputs.collections.hydro_plant, i) == max_generation
@test hydro_plant_max_generation(inputs.collections, i) == max_generation
@test hydro_plant_max_generation(inputs, i) == max_generation
end
end
end

file_size = PSRBridge.file_size(cache)
files = length(PSRBridge.files(cache))
println("Cache size: $files files with $file_size bytes")

@timeit "finalize!" begin
finalize!(inputs)
finalize!(cache)
end

PSRDatabaseSQLite.close!(db)

rm(path)

return nothing
end

Expand Down
30 changes: 30 additions & 0 deletions test/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
PRAGMA user_version = 1;
PRAGMA foreign_keys = ON;

CREATE TABLE Configuration (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE NOT NULL
) STRICT;

CREATE TABLE HydroPlant (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE NOT NULL,
initial_volume REAL,
has_commitment INTEGER DEFAULT 0
) STRICT;

CREATE TABLE HydroPlant_time_series_parameters (
id INTEGER,
date_time TEXT NOT NULL,
existing INTEGER,
max_generation REAL,
FOREIGN KEY(id) REFERENCES HydroPlant(id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (id, date_time)
) STRICT;

CREATE TABLE ThermalPlant (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE NOT NULL,
shutdown_cost REAL,
max_startups INTEGER
) STRICT;

0 comments on commit ae43398

Please sign in to comment.