mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-10-09 16:06:40 +00:00
* feat: adapt configuration for multi optimization algorithms
Decouple configuration from optimization algorithm parameters. Add to_[algorithm]_param() methods
to the configuration that derive optimization algorithm specific parameters from the configuration.
Add x-scope tags to the configuration options that describe for which specific algorithms the
configuration option is for.
The whole device settings are restructured. There are now general settings for the device classes
with the afore mentioned to_[algorithm]_param() methods. The general device settings got their own
directory `devices/settings`. By this the parameter class also does not have to be a pydantic model
which can be used for future optimization/ simulations speed up.
Also the parameter class for a device is now part of the device module. This better decouples and
also is the natural place for parameters of a device.
Besides this feature there are also fixes and improvements:
* feat: extend home appliance time window settings and simulation
Home appliance can now be configured for multiple runs with per-cycle allowed time windows. The
number of remaining cycles to plan is determined at runtime by reading the
``cycles_completed_measurement_key`` from the measurement store.
* feat: specialiced CycleTimeWindowSequence for time window sequences
Sequence of time windows associated to cycles.
This model specializes ``ValueTimeWindowSequence`` so that the ``value``
field of each ``ValueTimeWindow`` encodes the **cycle index** (0-based
integer) the window belongs to.
Typical use: an appliance that must run ``n`` times per day, each run
constrained to a distinct time window. Assign ``value=0`` to windows
for the first cycle, ``value=1`` for the second, and so on. Multiple
windows may share the same cycle index (their allowed regions are unioned).
Windows with ``value=None`` are silently ignored by all cycle-aware methods.
* fix: Make test_configmigrate also regard the _ANY_SENTENIEL in key values
* chore: Make devices configurations a map instead of a list
This makes config paths stable regardless of declaration order and lets each device settings
class build its own config path from ``self.device_id`` without needing an external index.
Tests are adapted likewise.
Devices configurations are automatically migrated from lists to maps.
* chore: rename levelized_cost_of_storage_kwh to levelized_cost_of_storage_amt kwh
This better fits in the naming scheme and also makes clear the costs are money.
Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
* fix: runtime config update ignored by config file
Runtime settings were handed back to pydantic-settings as init settings,
which rank below the config file and the environment. Any key already
present in EOS.config.json or in the environment silently discarded the
update, so a bulk PUT /v1/config returned 200 without applying anything,
while the granular PUT /v1/config/{path} endpoint kept working.
Add a dedicated runtime settings source ranked directly below the command
line arguments and record granular updates there as well, so both
endpoints share one store that survives re-evaluation of the settings
sources. Environment variables keep precedence over the config file for
all keys that were not set at runtime.
Also repairs revert_settings() and update(), which passed their data
through the same init settings.
Closes #1303
* fix: env vars ignored on first config build
ConfigEOS.__init__ passed self as first positional argument to _setup,
which forwards it to pydantic_settings.BaseSettings.__init__. Its first
positional parameter is _case_sensitive, so the environment source
matched the upper case variable names against the lower case field names
and returned nothing. Environment settings only took effect after the
next configuration setup.
* docs: changelog for config priority fixes
* fix(config): preserve device identities and storage costs during migration
* fix(devices): preserve charge-rate typing and public import compatibility
* ruff format fix
* feat(devices): port slot-aware battery export and direct-use physics
Port scoped device changes from d2e2d58237. Keep PR #1256 parameter conversion structure and separate GENETIC0 devices. Validate physical flows and reprice changed simulation results.
Co-authored-by: Andreas <drbacke@gmx.de>
Co-authored-by: Christin <info@bikinibottom.capital>
* fix(cache): distinguish callables in the shared EMS cache
Include the function object in cache keys so methods of one interpolator cannot reuse a probability as a power value. Cover both call orders, keyword arguments, cache hits and separate closures with identical qualified names.
* fix(devices): constrain the physics port and validate export levels
Defer inactive EV deadline fields to the optimizer port, reject nonfinite export rates, and document the hourly Optimize boundary. Verify converter IDs, rates and LCOS, separate GENETIC0 interpolation, physical boundary flows and independent GENETIC repricing.
* docs(devices): regenerate slot-physics configuration and OpenAPI schemas
* fix(config): satisfy typed device conversion and migration contracts
* docs(config): refresh validated configuration prerequisite schemas
* test(devices): align physics regressions with strict type checking
* docs(devices): refresh API version after prerequisite merge
* docs(interpolator): use portable reStructuredText markup
* docs(devices): refresh API version after docstring compatibility fix
---------
Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
Co-authored-by: Bobby Noelte <b0661n0e17e@gmail.com>
Co-authored-by: r0b2g1t <r0b2g1t@users.noreply.github.com>
Co-authored-by: Christin <info@bikinibottom.capital>
401 lines
14 KiB
Python
401 lines
14 KiB
Python
import numpy as np
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from akkudoktoreos.devices.devices import BatteriesCommonSettings
|
|
from akkudoktoreos.devices.genetic.battery import Battery, SolarPanelBatteryParameters
|
|
|
|
|
|
@pytest.fixture
|
|
def setup_pv_battery():
|
|
device_id="battery1"
|
|
capacity_wh=10000
|
|
initial_soc_percentage=50
|
|
charging_efficiency=0.88
|
|
discharging_efficiency=0.88
|
|
min_soc_percentage=20
|
|
max_soc_percentage=80
|
|
max_charge_power_w=8000
|
|
hours=24
|
|
|
|
params = SolarPanelBatteryParameters(
|
|
device_id=device_id,
|
|
capacity_wh=capacity_wh,
|
|
initial_soc_percentage=initial_soc_percentage,
|
|
charging_efficiency=charging_efficiency,
|
|
discharging_efficiency=discharging_efficiency,
|
|
min_soc_percentage=min_soc_percentage,
|
|
max_soc_percentage=max_soc_percentage,
|
|
max_charge_power_w=max_charge_power_w,
|
|
hours=hours,
|
|
)
|
|
battery = Battery(
|
|
params,
|
|
prediction_hours=48,
|
|
)
|
|
battery.reset()
|
|
|
|
assert battery.parameters.device_id==device_id
|
|
assert battery.capacity_wh==capacity_wh
|
|
assert battery.initial_soc_percentage==initial_soc_percentage
|
|
assert battery.charging_efficiency==charging_efficiency
|
|
assert battery.initial_soc_percentage==initial_soc_percentage
|
|
assert battery.discharging_efficiency==discharging_efficiency
|
|
assert battery.max_soc_percentage==max_soc_percentage
|
|
assert battery.max_charge_power_w==max_charge_power_w
|
|
assert battery.soc_wh==float((initial_soc_percentage / 100) * capacity_wh)
|
|
assert battery.min_soc_wh==float((min_soc_percentage / 100) * capacity_wh)
|
|
assert battery.max_soc_wh==float((max_soc_percentage / 100) * capacity_wh)
|
|
assert np.all(battery.charge_array == 0)
|
|
assert np.all(battery.discharge_array == 0)
|
|
|
|
# Init for test
|
|
battery.charge_array = np.full(battery.prediction_hours, 1)
|
|
battery.discharge_array = np.full(battery.prediction_hours, 1)
|
|
assert np.all(battery.charge_array == 1)
|
|
assert np.all(battery.discharge_array == 1)
|
|
|
|
return battery
|
|
|
|
|
|
def test_initial_state_of_charge(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
assert battery.current_soc_percentage() == 50.0, "Initial SoC should be 50%"
|
|
|
|
|
|
def test_battery_discharge_below_min_soc(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
discharged_wh, loss_wh = battery.discharge_energy(5000, 0)
|
|
|
|
# Ensure it discharges energy and stops at the min SOC
|
|
assert discharged_wh > 0
|
|
print(discharged_wh, loss_wh, battery.current_soc_percentage(), battery.min_soc_percentage)
|
|
assert battery.current_soc_percentage() >= 20 # Ensure it's above min_soc_percentage
|
|
assert loss_wh >= 0 # Losses should not be negative
|
|
assert discharged_wh == 2640.0, "The energy discharged should be limited by min_soc"
|
|
|
|
|
|
def test_battery_charge_above_max_soc(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
charged_wh, loss_wh = battery.charge_energy(5000, 0)
|
|
|
|
# Ensure it charges energy and stops at the max SOC
|
|
assert charged_wh > 0
|
|
assert battery.current_soc_percentage() <= 80 # Ensure it's below max_soc_percentage
|
|
assert loss_wh >= 0 # Losses should not be negative
|
|
assert charged_wh == 3000.0, "The energy charged should be limited by max_soc"
|
|
|
|
|
|
def test_battery_charge_when_full(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
battery.soc_wh = battery.max_soc_wh # Set battery to full
|
|
charged_wh, loss_wh = battery.charge_energy(5000, 0)
|
|
|
|
# No charging should happen if battery is full
|
|
assert charged_wh == 0
|
|
assert loss_wh == 0
|
|
assert battery.current_soc_percentage() == 80, "SoC should remain at max_soc"
|
|
|
|
|
|
def test_battery_discharge_when_empty(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
battery.soc_wh = battery.min_soc_wh # Set battery to minimum SOC
|
|
discharged_wh, loss_wh = battery.discharge_energy(5000, 0)
|
|
|
|
# No discharge should happen if battery is at min SOC
|
|
assert discharged_wh == 0
|
|
assert loss_wh == 0
|
|
assert battery.current_soc_percentage() == 20, "SoC should remain at min_soc"
|
|
|
|
|
|
def test_battery_discharge_exactly_min_soc(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
battery.soc_wh = battery.min_soc_wh # Set battery to exactly min SOC
|
|
discharged_wh, loss_wh = battery.discharge_energy(1000, 0)
|
|
|
|
# Battery should not go below the min SOC
|
|
assert discharged_wh == 0
|
|
assert battery.current_soc_percentage() == 20 # SOC should remain at min_SOC
|
|
|
|
|
|
def test_battery_charge_exactly_max_soc(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
battery.soc_wh = battery.max_soc_wh # Set battery to exactly max SOC
|
|
charged_wh, loss_wh = battery.charge_energy(1000, 0)
|
|
|
|
# Battery should not exceed the max SOC
|
|
assert charged_wh == 0
|
|
assert battery.current_soc_percentage() == 80 # SOC should remain at max_SOC
|
|
|
|
|
|
def test_battery_reset_function(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
battery.soc_wh = 8000 # Change the SOC to some value
|
|
battery.reset()
|
|
|
|
# After reset, SOC should be equal to the initial value
|
|
assert battery.current_soc_percentage() == battery.initial_soc_percentage
|
|
|
|
|
|
def test_soc_limits(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
|
|
# Manually set SoC above max limit
|
|
battery.soc_wh = battery.max_soc_wh + 1000
|
|
battery.soc_wh = min(battery.soc_wh, battery.max_soc_wh)
|
|
assert battery.current_soc_percentage() <= 80, "SoC should not exceed max_soc"
|
|
|
|
# Manually set SoC below min limit
|
|
battery.soc_wh = battery.min_soc_wh - 1000
|
|
battery.soc_wh = max(battery.soc_wh, battery.min_soc_wh)
|
|
assert battery.current_soc_percentage() >= 20, "SoC should not drop below min_soc"
|
|
|
|
|
|
def test_max_charge_power_w(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
assert battery.parameters.max_charge_power_w == 8000, (
|
|
"Default max charge power should be 5000W, We ask for 8000W here"
|
|
)
|
|
|
|
|
|
def test_charge_energy_within_limits(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
initial_soc_wh = battery.soc_wh
|
|
|
|
charged_wh, losses_wh = battery.charge_energy(wh=4000, hour=1)
|
|
|
|
assert charged_wh > 0, "Charging should add energy"
|
|
assert losses_wh >= 0, "Losses should not be negative"
|
|
assert battery.soc_wh > initial_soc_wh, "State of charge should increase after charging"
|
|
assert battery.soc_wh <= battery.max_soc_wh, "SOC should not exceed max SOC"
|
|
|
|
|
|
def test_charge_energy_exceeds_capacity(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
initial_soc_wh = battery.soc_wh
|
|
|
|
# Try to overcharge beyond max capacity
|
|
charged_wh, losses_wh = battery.charge_energy(wh=20000, hour=2)
|
|
|
|
assert charged_wh + initial_soc_wh <= battery.max_soc_wh, (
|
|
"Charging should not exceed max capacity"
|
|
)
|
|
assert losses_wh >= 0, "Losses should not be negative"
|
|
assert battery.soc_wh == battery.max_soc_wh, "SOC should be at max after overcharge attempt"
|
|
|
|
|
|
def test_charge_energy_not_allowed_hour(setup_pv_battery):
|
|
battery = setup_pv_battery
|
|
|
|
# Disable charging for all hours
|
|
battery.set_charge_per_hour(np.zeros(battery.prediction_hours))
|
|
|
|
charged_wh, losses_wh = battery.charge_energy(wh=4000, hour=3)
|
|
|
|
assert charged_wh == 0, "No energy should be charged in disallowed hours"
|
|
assert losses_wh == 0, "No losses should occur if charging is not allowed"
|
|
assert (
|
|
battery.soc_wh == (battery.parameters.initial_soc_percentage / 100) * battery.capacity_wh
|
|
), "SOC should remain unchanged"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"wh, charge_factor, expected_raises",
|
|
[
|
|
(None, 0.5, False), # Expected to work normally (if capacity allows)
|
|
(None, 1.0, False), # Often still OK, depending on fixture capacity
|
|
(None, 2.0, False), # Exceeds max charge → always ValueError
|
|
(1000, 0, False),
|
|
(1000, 1.0, True),
|
|
],
|
|
)
|
|
def test_charge_energy_with_charge_factor(setup_pv_battery, wh, charge_factor, expected_raises):
|
|
battery = setup_pv_battery
|
|
hour = 4
|
|
|
|
if wh is not None and charge_factor == 0.0: # mode 1
|
|
raw_request_wh = wh
|
|
else:
|
|
raw_request_wh = battery.max_charge_power_w * charge_factor
|
|
raw_capacity_wh = max(battery.max_soc_wh - battery.soc_wh, 0.0)
|
|
|
|
if expected_raises:
|
|
# Should raise
|
|
with pytest.raises(ValueError):
|
|
battery.charge_energy(
|
|
wh=wh,
|
|
hour=hour,
|
|
charge_factor=charge_factor,
|
|
)
|
|
return
|
|
|
|
# Should NOT raise
|
|
charged_wh, losses_wh = battery.charge_energy(
|
|
wh=wh,
|
|
hour=hour,
|
|
charge_factor=charge_factor,
|
|
)
|
|
|
|
# Expectations
|
|
assert charged_wh > 0, "Charging should occur with charge factor"
|
|
assert losses_wh >= 0, "Losses must not be negative"
|
|
assert charged_wh <= raw_request_wh, "Charging must not exceed request"
|
|
assert battery.soc_wh > 0, "SOC should increase after charging"
|
|
|
|
|
|
@pytest.fixture
|
|
def setup_car_battery():
|
|
from akkudoktoreos.optimization.genetic.geneticparams import (
|
|
ElectricVehicleParameters,
|
|
)
|
|
|
|
params = ElectricVehicleParameters(
|
|
device_id="ev1",
|
|
capacity_wh=40000,
|
|
initial_soc_percentage=60,
|
|
min_soc_percentage=10,
|
|
max_soc_percentage=90,
|
|
max_charge_power_w=7000,
|
|
hours=24,
|
|
)
|
|
battery = Battery(
|
|
params,
|
|
prediction_hours=48,
|
|
)
|
|
battery.reset()
|
|
|
|
# Init for test
|
|
battery.charge_array = np.full(battery.prediction_hours, 1)
|
|
battery.discharge_array = np.full(battery.prediction_hours, 1)
|
|
assert np.all(battery.charge_array == 1)
|
|
assert np.all(battery.discharge_array == 1)
|
|
|
|
return battery
|
|
|
|
|
|
def test_car_and_pv_battery_discharge_and_max_charge_power(setup_pv_battery, setup_car_battery):
|
|
pv_battery = setup_pv_battery
|
|
car_battery = setup_car_battery
|
|
|
|
# Test discharge for PV battery
|
|
pv_discharged_wh, pv_loss_wh = pv_battery.discharge_energy(3000, 5)
|
|
assert pv_discharged_wh > 0, "PV battery should discharge energy"
|
|
assert pv_battery.current_soc_percentage() >= pv_battery.parameters.min_soc_percentage, (
|
|
"PV battery SOC should stay above min SOC"
|
|
)
|
|
assert pv_battery.parameters.max_charge_power_w == 8000, (
|
|
"PV battery max charge power should remain as defined"
|
|
)
|
|
|
|
# Test discharge for car battery
|
|
car_discharged_wh, car_loss_wh = car_battery.discharge_energy(5000, 10)
|
|
assert car_discharged_wh > 0, "Car battery should discharge energy"
|
|
assert car_battery.current_soc_percentage() >= car_battery.parameters.min_soc_percentage, (
|
|
"Car battery SOC should stay above min SOC"
|
|
)
|
|
assert car_battery.parameters.max_charge_power_w == 7000, (
|
|
"Car battery max charge power should remain as defined"
|
|
)
|
|
|
|
|
|
def test_quarter_hour_charge_calls_share_one_power_budget():
|
|
params = SolarPanelBatteryParameters(
|
|
device_id="battery1",
|
|
capacity_wh=10_000,
|
|
initial_soc_percentage=0,
|
|
min_soc_percentage=0,
|
|
max_soc_percentage=100,
|
|
max_charge_power_w=1_000,
|
|
charging_efficiency=1.0,
|
|
discharging_efficiency=1.0,
|
|
)
|
|
battery = Battery(params, prediction_hours=4, slot_duration_h=0.25)
|
|
battery.set_charge_per_hour(np.ones(4))
|
|
|
|
first_stored, _ = battery.charge_energy(200.0, 0)
|
|
second_stored, _ = battery.charge_energy(200.0, 0)
|
|
|
|
assert first_stored == pytest.approx(200.0)
|
|
assert second_stored == pytest.approx(50.0)
|
|
assert battery.soc_wh == pytest.approx(250.0)
|
|
|
|
|
|
def test_quarter_hour_discharge_calls_share_one_power_budget():
|
|
params = SolarPanelBatteryParameters(
|
|
device_id="battery1",
|
|
capacity_wh=10_000,
|
|
initial_soc_percentage=100,
|
|
min_soc_percentage=0,
|
|
max_soc_percentage=100,
|
|
max_charge_power_w=1_000,
|
|
charging_efficiency=1.0,
|
|
discharging_efficiency=1.0,
|
|
)
|
|
battery = Battery(params, prediction_hours=4, slot_duration_h=0.25)
|
|
battery.set_discharge_per_hour(np.ones(4))
|
|
|
|
first_delivered, _ = battery.discharge_energy(200.0, 0)
|
|
second_delivered, _ = battery.discharge_energy(200.0, 0)
|
|
|
|
assert first_delivered == pytest.approx(200.0)
|
|
assert second_delivered == pytest.approx(50.0)
|
|
assert battery.discharged_energy_wh(0) == pytest.approx(250.0)
|
|
assert battery.soc_wh == pytest.approx(9_750.0)
|
|
|
|
battery.reset()
|
|
|
|
assert battery.discharged_energy_wh(0) == 0.0
|
|
|
|
|
|
def test_grid_export_rates_are_sorted_and_deduplicated():
|
|
"""Export rates are normalized like the charge rates."""
|
|
settings = BatteriesCommonSettings(
|
|
device_id="battery1", grid_export_rates=[1.0, 0.5, 0.5, 0.25]
|
|
)
|
|
assert settings.grid_export_rates == [0.25, 0.5, 1.0]
|
|
|
|
|
|
def test_grid_export_rates_default_and_override():
|
|
"""None falls back to the defaults; [1.0] restores all-or-nothing export."""
|
|
assert BatteriesCommonSettings(device_id="battery1").grid_export_rates == [
|
|
0.25,
|
|
0.5,
|
|
0.75,
|
|
1.0,
|
|
]
|
|
fallback = BatteriesCommonSettings(device_id="battery1", grid_export_rates=None)
|
|
assert fallback.grid_export_rates == [0.25, 0.5, 0.75, 1.0]
|
|
full_export = BatteriesCommonSettings(device_id="battery1", grid_export_rates=[1.0])
|
|
assert full_export.grid_export_rates == [1.0]
|
|
|
|
|
|
@pytest.mark.parametrize("rates", [[0.0, 0.5], [1.5], [-0.25], [], [np.nan], [np.inf], [[0.5]], 0.5])
|
|
def test_grid_export_rates_reject_invalid_values(rates):
|
|
"""0.0 is not an export level, and rates above the rated power are rejected."""
|
|
with pytest.raises(ValidationError):
|
|
BatteriesCommonSettings(device_id="battery1", grid_export_rates=rates)
|
|
|
|
|
|
def test_rated_discharge_energy_scales_with_slot_duration(setup_pv_battery):
|
|
"""The rate reference is the rated discharge energy of one slot."""
|
|
battery = setup_pv_battery
|
|
expected = battery.max_charge_power_w * battery.slot_duration_h * battery.discharging_efficiency
|
|
assert battery.rated_discharge_energy_wh() == pytest.approx(expected)
|
|
|
|
|
|
def test_pv_converter_preserves_id_lcos_charge_and_export_rates():
|
|
settings = BatteriesCommonSettings(
|
|
device_id="house",
|
|
charge_rates=[0.0, 0.5, 1.0],
|
|
grid_export_rates=[1.0, 0.25],
|
|
levelized_cost_of_storage_amt_kwh=0.123,
|
|
)
|
|
assert isinstance(BatteriesCommonSettings.validate_and_sort_charge_rates(None), np.ndarray)
|
|
parameters = settings.to_genetic_pv_bat_param()
|
|
assert parameters.device_id == "house"
|
|
assert parameters.charge_rates == [0.0, 0.5, 1.0]
|
|
assert parameters.grid_export_rates == [0.25, 1.0]
|
|
assert parameters.levelized_cost_of_storage_kwh == pytest.approx(0.123)
|
|
battery = Battery(parameters, prediction_hours=4, slot_duration_h=0.25)
|
|
assert battery.levelized_cost_of_storage_kwh == pytest.approx(0.123)
|