mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-10-09 07:56:40 +00:00
feat(devices): add bounded slot physics for GENETIC (#1327)
* 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>
This commit is contained in:
co-authored by
Andreas
Christin
Bobby Noelte
r0b2g1t
parent
1a18935667
commit
3c862543a1
+177
-28
@@ -1,7 +1,12 @@
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import Mock, call, patch
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from akkudoktoreos.devices.genetic.battery import (
|
||||
Battery,
|
||||
SolarPanelBatteryParameters,
|
||||
)
|
||||
from akkudoktoreos.devices.genetic.inverter import Inverter, InverterParameters
|
||||
|
||||
|
||||
@@ -10,6 +15,9 @@ def mock_battery() -> Mock:
|
||||
mock_battery = Mock()
|
||||
mock_battery.charge_energy = Mock(return_value=(0.0, 0.0))
|
||||
mock_battery.discharge_energy = Mock(return_value=(0.0, 0.0))
|
||||
# Rated discharge energy of one slot - the reference a grid-export rate is
|
||||
# applied to. Large enough to never bind at the default factor of 1.0.
|
||||
mock_battery.rated_discharge_energy_wh = Mock(return_value=1e9)
|
||||
mock_battery.parameters.device_id = "battery1"
|
||||
return mock_battery
|
||||
|
||||
@@ -17,7 +25,7 @@ def mock_battery() -> Mock:
|
||||
@pytest.fixture
|
||||
def inverter(mock_battery) -> Inverter:
|
||||
mock_self_consumption_predictor = Mock()
|
||||
mock_self_consumption_predictor.calculate_self_consumption.return_value = 1.0
|
||||
mock_self_consumption_predictor.calculate_expected_direct_consumption.side_effect = min
|
||||
with patch(
|
||||
"akkudoktoreos.devices.genetic.inverter.get_eos_load_interpolator",
|
||||
return_value=mock_self_consumption_predictor,
|
||||
@@ -26,11 +34,51 @@ def inverter(mock_battery) -> Inverter:
|
||||
InverterParameters(
|
||||
device_id="iv1", max_power_wh=500.0, battery_id=mock_battery.parameters.device_id
|
||||
),
|
||||
battery = mock_battery
|
||||
battery=mock_battery,
|
||||
)
|
||||
return iv
|
||||
|
||||
|
||||
def test_quarter_hour_load_and_grid_export_share_discharge_power_limit():
|
||||
"""Local supply plus direct export may not exceed one slot's battery budget."""
|
||||
battery = Battery(
|
||||
SolarPanelBatteryParameters(
|
||||
device_id="battery",
|
||||
capacity_wh=10000,
|
||||
charging_efficiency=1.0,
|
||||
discharging_efficiency=1.0,
|
||||
max_charge_power_w=7000,
|
||||
initial_soc_percentage=100,
|
||||
),
|
||||
prediction_hours=1,
|
||||
slot_duration_h=0.25,
|
||||
)
|
||||
battery.set_discharge_per_hour(np.array([1]))
|
||||
quarter_hour_inverter = Inverter(
|
||||
InverterParameters(
|
||||
device_id="inverter",
|
||||
max_power_wh=10000,
|
||||
battery_id="battery",
|
||||
dc_to_ac_efficiency=1.0,
|
||||
ac_to_dc_efficiency=1.0,
|
||||
),
|
||||
battery=battery,
|
||||
slot_duration_h=0.25,
|
||||
)
|
||||
initial_soc_wh = battery.soc_wh
|
||||
|
||||
grid_export, grid_import, _, _ = quarter_hour_inverter.process_energy(
|
||||
generation=0.0,
|
||||
consumption=1000.0,
|
||||
hour=0,
|
||||
allow_battery_grid_export=True,
|
||||
)
|
||||
|
||||
assert grid_import == 0.0
|
||||
assert grid_export == pytest.approx(750.0)
|
||||
assert initial_soc_wh - battery.soc_wh == pytest.approx(1750.0)
|
||||
|
||||
|
||||
def test_process_energy_excess_generation(inverter, mock_battery):
|
||||
# Battery charges 100 Wh with 10 Wh loss
|
||||
mock_battery.charge_energy.return_value = (100.0, 10.0)
|
||||
@@ -48,7 +96,7 @@ def test_process_energy_excess_generation(inverter, mock_battery):
|
||||
assert self_consumption == 200.0 # All consumption is met
|
||||
mock_battery.charge_energy.assert_called_once_with(400.0, hour)
|
||||
mock_battery.discharge_energy.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_called_once_with(
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
|
||||
@@ -57,7 +105,8 @@ def test_process_energy_excess_generation_interpolator(inverter, mock_battery):
|
||||
# Battery charges 100 Wh with 10 Wh loss
|
||||
mock_battery.charge_energy.return_value = (100.0, 10.0)
|
||||
mock_battery.discharge_energy.return_value = (20.0, 2.0)
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.return_value = 0.95
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.side_effect = None
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.return_value = 180.0
|
||||
|
||||
generation = 600.0
|
||||
consumption = 200.0
|
||||
@@ -67,19 +116,71 @@ def test_process_energy_excess_generation_interpolator(inverter, mock_battery):
|
||||
generation, consumption, hour
|
||||
)
|
||||
|
||||
assert grid_export == pytest.approx(
|
||||
270.0, rel=1e-2
|
||||
) # 290 Wh feed-in - 5% of generation-consumption self consumption after battery charges
|
||||
assert grid_export == pytest.approx(300.0, rel=1e-2)
|
||||
assert grid_import == pytest.approx(0.0, rel=1e-2) # No grid draw
|
||||
assert losses == 12.0 # Battery charging losses
|
||||
assert self_consumption == 220.0 # All consumption is met
|
||||
mock_battery.charge_energy.assert_called_once_with(pytest.approx(380.0, rel=1e-2), hour)
|
||||
assert losses == 22.0 # Battery/inverter losses plus curtailed PV
|
||||
assert self_consumption == 200.0 # 180 Wh direct PV + 20 Wh battery
|
||||
mock_battery.charge_energy.assert_called_once_with(pytest.approx(420.0, rel=1e-2), hour)
|
||||
mock_battery.discharge_energy.assert_called_once_with(pytest.approx(20.0, rel=1e-2), hour)
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_called_once_with(
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
|
||||
|
||||
def test_probabilistic_bypass_conserves_energy_without_battery():
|
||||
predictor = Mock()
|
||||
predictor.calculate_expected_direct_consumption.return_value = 150.0
|
||||
with patch(
|
||||
"akkudoktoreos.devices.genetic.inverter.get_eos_load_interpolator",
|
||||
return_value=predictor,
|
||||
):
|
||||
inverter_without_battery = Inverter(
|
||||
InverterParameters(device_id="inverter", max_power_wh=1000.0)
|
||||
)
|
||||
|
||||
generation = 600.0
|
||||
consumption = 200.0
|
||||
grid_export, grid_import, losses, self_consumption = (
|
||||
inverter_without_battery.process_energy(generation, consumption, hour=0)
|
||||
)
|
||||
|
||||
assert self_consumption == pytest.approx(150.0)
|
||||
assert grid_import == pytest.approx(50.0)
|
||||
assert grid_export == pytest.approx(450.0)
|
||||
assert losses == 0.0
|
||||
assert generation + grid_import == pytest.approx(
|
||||
consumption + grid_export + losses
|
||||
)
|
||||
|
||||
|
||||
def test_probabilistic_bypass_conserves_energy_on_quarter_hour_grid():
|
||||
predictor = Mock()
|
||||
predictor.calculate_expected_direct_consumption.return_value = 600.0
|
||||
with patch(
|
||||
"akkudoktoreos.devices.genetic.inverter.get_eos_load_interpolator",
|
||||
return_value=predictor,
|
||||
):
|
||||
inverter_without_battery = Inverter(
|
||||
InverterParameters(device_id="inverter", max_power_wh=2000.0),
|
||||
slot_duration_h=0.25,
|
||||
)
|
||||
|
||||
generation = 300.0 # 1200 W over 15 minutes
|
||||
consumption = 200.0 # 800 W over 15 minutes
|
||||
grid_export, grid_import, losses, self_consumption = (
|
||||
inverter_without_battery.process_energy(generation, consumption, hour=0)
|
||||
)
|
||||
|
||||
predictor.calculate_expected_direct_consumption.assert_called_once_with(800.0, 1200.0)
|
||||
assert self_consumption == pytest.approx(150.0)
|
||||
assert grid_import == pytest.approx(50.0)
|
||||
assert grid_export == pytest.approx(150.0)
|
||||
assert losses == 0.0
|
||||
assert generation + grid_import == pytest.approx(
|
||||
consumption + grid_export + losses
|
||||
)
|
||||
|
||||
|
||||
def test_process_energy_generation_equals_consumption(inverter, mock_battery):
|
||||
generation = 300.0
|
||||
consumption = 300.0
|
||||
@@ -96,7 +197,7 @@ def test_process_energy_generation_equals_consumption(inverter, mock_battery):
|
||||
|
||||
mock_battery.charge_energy.assert_not_called()
|
||||
mock_battery.discharge_energy.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_called_once_with(
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
|
||||
@@ -120,7 +221,49 @@ def test_process_energy_battery_discharges(inverter, mock_battery):
|
||||
assert self_consumption == 200.0 # Generation + battery discharge
|
||||
mock_battery.charge_energy.assert_not_called()
|
||||
mock_battery.discharge_energy.assert_called_once_with(150.0, hour)
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
|
||||
|
||||
def test_process_energy_allows_battery_grid_export(inverter, mock_battery):
|
||||
mock_battery.max_charge_power_w = 300.0
|
||||
mock_battery.remaining_discharge_energy_wh.return_value = 200.0
|
||||
mock_battery.discharge_energy.side_effect = [(100.0, 0.0), (200.0, 0.0)]
|
||||
|
||||
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
|
||||
generation=0.0,
|
||||
consumption=100.0,
|
||||
hour=12,
|
||||
allow_battery_grid_export=True,
|
||||
)
|
||||
|
||||
assert grid_export == pytest.approx(200.0, rel=1e-2)
|
||||
assert grid_import == 0.0
|
||||
assert losses == 0.0
|
||||
assert self_consumption == 100.0
|
||||
mock_battery.discharge_energy.assert_has_calls([call(100.0, 12), call(200.0, 12)])
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_not_called()
|
||||
|
||||
|
||||
def test_process_energy_grid_export_rate_limits_export(inverter, mock_battery):
|
||||
"""An export rate caps the export at that share of the rated discharge power."""
|
||||
mock_battery.max_charge_power_w = 300.0
|
||||
mock_battery.remaining_discharge_energy_wh.return_value = 200.0
|
||||
mock_battery.rated_discharge_energy_wh.return_value = 300.0
|
||||
mock_battery.discharge_energy.side_effect = [(100.0, 0.0), (150.0, 0.0)]
|
||||
|
||||
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
|
||||
generation=0.0,
|
||||
consumption=100.0,
|
||||
hour=12,
|
||||
allow_battery_grid_export=True,
|
||||
battery_grid_export_factor=0.5,
|
||||
)
|
||||
|
||||
# 0.5 * 300 Wh rated = 150 Wh, below the 200 Wh the battery could still give.
|
||||
assert grid_export == pytest.approx(150.0)
|
||||
mock_battery.discharge_energy.assert_has_calls([call(100.0, 12), call(150.0, 12)])
|
||||
|
||||
|
||||
def test_process_energy_battery_empty(inverter, mock_battery):
|
||||
@@ -140,7 +283,9 @@ def test_process_energy_battery_empty(inverter, mock_battery):
|
||||
assert self_consumption == 100.0 # Only generation is consumed
|
||||
mock_battery.charge_energy.assert_not_called()
|
||||
mock_battery.discharge_energy.assert_called_once_with(200.0, hour)
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
|
||||
|
||||
def test_process_energy_battery_full_at_start(inverter, mock_battery):
|
||||
@@ -162,7 +307,7 @@ def test_process_energy_battery_full_at_start(inverter, mock_battery):
|
||||
assert self_consumption == 200.0 # Only consumption is met
|
||||
mock_battery.charge_energy.assert_called_once_with(300.0, hour)
|
||||
mock_battery.discharge_energy.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_called_once_with(
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
|
||||
@@ -184,7 +329,9 @@ def test_process_energy_insufficient_generation_no_battery(inverter, mock_batter
|
||||
assert self_consumption == 100.0 # Only generation is consumed
|
||||
mock_battery.charge_energy.assert_not_called()
|
||||
mock_battery.discharge_energy.assert_called_once_with(400.0, hour)
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
|
||||
|
||||
def test_process_energy_insufficient_generation_battery_assists(inverter, mock_battery):
|
||||
@@ -209,7 +356,9 @@ def test_process_energy_insufficient_generation_battery_assists(inverter, mock_b
|
||||
assert self_consumption == 250.0 # Generation + battery discharge
|
||||
mock_battery.charge_energy.assert_not_called()
|
||||
mock_battery.discharge_energy.assert_called_once_with(200.0, hour)
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
|
||||
|
||||
def test_process_energy_zero_generation(inverter, mock_battery):
|
||||
@@ -232,7 +381,7 @@ def test_process_energy_zero_generation(inverter, mock_battery):
|
||||
assert self_consumption == 100.0 # Only battery discharge is consumed
|
||||
mock_battery.charge_energy.assert_not_called()
|
||||
mock_battery.discharge_energy.assert_called_once_with(300.0, hour)
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_not_called()
|
||||
|
||||
|
||||
def test_process_energy_zero_consumption(inverter, mock_battery):
|
||||
@@ -252,9 +401,7 @@ def test_process_energy_zero_consumption(inverter, mock_battery):
|
||||
assert self_consumption == 0.0 # Zero consumption
|
||||
mock_battery.charge_energy.assert_called_once_with(500.0, hour)
|
||||
mock_battery.discharge_energy.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_not_called()
|
||||
|
||||
|
||||
def test_process_energy_zero_generation_zero_consumption(inverter, mock_battery):
|
||||
@@ -272,9 +419,7 @@ def test_process_energy_zero_generation_zero_consumption(inverter, mock_battery)
|
||||
assert self_consumption == 0.0 # No consumption
|
||||
mock_battery.charge_energy.assert_not_called()
|
||||
mock_battery.discharge_energy.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_not_called()
|
||||
|
||||
|
||||
def test_process_energy_partial_battery_discharge(inverter, mock_battery):
|
||||
@@ -295,7 +440,9 @@ def test_process_energy_partial_battery_discharge(inverter, mock_battery):
|
||||
assert self_consumption == 250.0 # Generation + battery discharge
|
||||
mock_battery.charge_energy.assert_not_called()
|
||||
mock_battery.discharge_energy.assert_called_once_with(200.0, 12)
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
|
||||
|
||||
def test_process_energy_consumption_exceeds_max_no_battery(inverter, mock_battery):
|
||||
@@ -315,7 +462,9 @@ def test_process_energy_consumption_exceeds_max_no_battery(inverter, mock_batter
|
||||
assert self_consumption == 100.0 # Only the generation is consumed, maxing out the inverter
|
||||
mock_battery.charge_energy.assert_not_called()
|
||||
mock_battery.discharge_energy.assert_called_once_with(400.0, hour)
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_called_once_with(
|
||||
consumption, generation
|
||||
)
|
||||
|
||||
|
||||
def test_process_energy_zero_generation_full_battery_high_consumption(inverter, mock_battery):
|
||||
@@ -337,4 +486,4 @@ def test_process_energy_zero_generation_full_battery_high_consumption(inverter,
|
||||
assert self_consumption == 500.0 # Battery fully discharges to meet consumption
|
||||
mock_battery.charge_energy.assert_not_called()
|
||||
mock_battery.discharge_energy.assert_called_once_with(500.0, hour)
|
||||
inverter.self_consumption_predictor.calculate_self_consumption.assert_not_called()
|
||||
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_not_called()
|
||||
|
||||
Reference in New Issue
Block a user