Files
EOS/tests/test_geneticinverter.py
T
Bobby NoelteandClaude Opus 5.5 b3d5334d3e refactor(genetic): refactor GENETIC to the consolidated optimizer architecture
Pin result, conversion, plan and visualization of golden runs.
Drive GENETIC through the optimizer interface and registry.
Move GENETIC into one directory with role-named modules.
Prepare every run input once, run from the parameters.
Fall back instead of cancelling the optimization run.
Simulate battery, EV and inverter from frozen dataclasses.
Simulate the home appliance from frozen dataclasses.
Convert and visualize from the parameters and result only.
Serve GENETIC from its own router.
Return a frozen result, build responses in the API.
Echo the request as sent, not with an invented time window.

Additional changes
- Pin that the result does not depend on the number of evaluation workers.
- Run the golden module in UTC: until the request date-times are held in UTC they render in the host time zone.
- Add Optimizer.run_clock(), so the EMS gets slot length and time zone without GENETIC settings.
- Rename ConfigOptimizationRequest, RuntimeForecasts and GeneticCommonSettings as ADR 0010 asks.
- Correct the request example in ADR 0010.
- Record the renamed OpenAPI schemas in CHANGELOG.md and the upgrade guide.
- Add replay files that reproduce each golden result from its parameters.
- Drop OptimizationBase from the algorithm.
- Give the replay and worker tests own review files against xdist races.
- Handle a failing preparation in the EMS like a failing run, no separate cancel path.
- Record the fallbacks in CHANGELOG.md and the upgrade guide.
- Without an inverter, buy the whole load from the grid instead of planning it at no cost.
- Do not charge from the grid without any known price.
- Treat an infinite value after the control horizon as a gap.
- Correct the documentation that said missing forecasts cancel the run.
- Copy the efficiency helpers and the self-consumption table evaluation into GENETIC.
- Add CycleTimeWindowSequence, ConsumerScheduleMode and ConsumerDeadlinePolicy as contract names.
- Hold the date-times of battery, EV and home appliance requests in UTC, so they render the same on every host.
- Drop the UTC pin of the GENETIC golden module; the request date-times no longer depend on the host.
- Copy the time window logic GENETIC needs into genetictimewindow.py.
- Add ValueTimeWindow as a contract name.
- Move the settings' to_genetic_*() converters into the preparation.
- Drop the conversion's prediction reads and configuration fallbacks, which runs never reached.
- Make the moved docstrings valid reST.
- Remove the generic optimization solution route; each optimizer serves its own path.
- Import the GENETIC settings from the package.
- Record the removed route in CHANGELOG.md and the upgrade guide.
- Make the terminal and tail value models frozen dataclasses.
- Move the response models into geneticapi; remove geneticsolution and geneticabc.
- Remove the legacy appliance start arrays and API, which read the wall clock.
- Remove the time window helpers only they used.
- Echo a start_solution without start_solution_datetime as sent.
- Carry the previous solution's start in the run parameters.
- Plan the home appliance cycles that fit, or leave the appliance out, instead of failing.
- Move the appliance layout rules into geneticappliancelayout for the preparation.
- Leave out a DAILY home appliance whose open windows leave no room for a run, with an error log, instead of silently.
- Name the reason of the SoC fallback instead of Python's exception text.
- Answer a solution request for an unknown algorithm with a 404 problem response.
- Record the fixes in CHANGELOG.md and the upgrade guide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: Claude Code (claude-opus-5-5)
2026-11-27 13:55:41 +01:00

630 lines
25 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from unittest.mock import Mock, call, patch
import numpy as np
import pytest
from akkudoktoreos.devices.settings.invertersettings import InverterCommonSettings
from akkudoktoreos.optimization.genetic import geneticprepare
from akkudoktoreos.optimization.genetic0 import genetic0prepare
from akkudoktoreos.optimization.genetic.devices.geneticbattery import (
GeneticBattery,
)
from akkudoktoreos.optimization.genetic.devices.geneticinverter import (
GeneticInverter,
)
from akkudoktoreos.optimization.genetic.geneticrequest import (
GeneticBatteryRequest,
GeneticInverterRequest,
)
@pytest.fixture
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
@pytest.fixture
def inverter(mock_battery) -> GeneticInverter:
mock_self_consumption_predictor = Mock()
mock_self_consumption_predictor.calculate_expected_direct_consumption.side_effect = min
with patch(
"akkudoktoreos.optimization.genetic.devices.geneticinverter.get_genetic_load_interpolator",
return_value=mock_self_consumption_predictor,
):
iv = GeneticInverter(
GeneticInverterRequest(
device_id="iv1", max_power_wh=500.0, battery_id=mock_battery.parameters.device_id
).to_parameters(),
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 = GeneticBattery(
GeneticBatteryRequest(
device_id="battery",
capacity_wh=10000,
charging_efficiency=1.0,
discharging_efficiency=1.0,
max_charge_power_w=7000,
initial_soc_percentage=100,
).to_parameters(),
prediction_hours=1,
slot_duration_h=0.25,
)
battery.set_discharge_per_hour(np.array([1]))
quarter_hour_inverter = GeneticInverter(
GeneticInverterRequest(
device_id="inverter",
max_power_wh=10000,
battery_id="battery",
dc_to_ac_efficiency=1.0,
ac_to_dc_efficiency=1.0,
).to_parameters(),
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)
generation = 600.0
consumption = 200.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == pytest.approx(290.0, rel=1e-2) # 290 Wh feed-in after battery charges
assert grid_import == 0.0 # No grid draw
assert losses == 10.0 # Battery charging losses
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_expected_direct_consumption.assert_called_once_with(
consumption, generation
)
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_expected_direct_consumption.side_effect = None
inverter.self_consumption_predictor.calculate_expected_direct_consumption.return_value = 180.0
generation = 600.0
consumption = 200.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
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 == 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_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.optimization.genetic.devices.geneticinverter.get_genetic_load_interpolator",
return_value=predictor,
):
inverter_without_battery = GeneticInverter(
GeneticInverterRequest(device_id="inverter", max_power_wh=1000.0).to_parameters()
)
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.optimization.genetic.devices.geneticinverter.get_genetic_load_interpolator",
return_value=predictor,
):
inverter_without_battery = GeneticInverter(
GeneticInverterRequest(device_id="inverter", max_power_wh=2000.0).to_parameters(),
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
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == 0.0 # No feed-in as generation equals consumption
assert grid_import == 0.0 # No grid draw
assert losses == 0.0 # No losses
assert self_consumption == 300.0 # All consumption is met with generation
mock_battery.charge_energy.assert_not_called()
mock_battery.discharge_energy.assert_not_called()
inverter.self_consumption_predictor.calculate_expected_direct_consumption.assert_called_once_with(
consumption, generation
)
def test_process_energy_battery_discharges(inverter, mock_battery):
# Battery discharges 100 Wh with 10 Wh loss already accounted for in the discharge
mock_battery.discharge_energy.return_value = (100.0, 10.0)
generation = 100.0
consumption = 250.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == 0.0 # No feed-in as generation is insufficient
assert grid_import == pytest.approx(
50.0, rel=1e-2
) # Grid supplies remaining shortfall after battery discharge
assert losses == 10.0 # Discharge losses
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_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):
# Battery is empty, so no energy can be discharged
mock_battery.discharge_energy.return_value = (0.0, 0.0)
generation = 100.0
consumption = 300.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == 0.0 # No feed-in as generation is insufficient
assert grid_import == pytest.approx(200.0, rel=1e-2) # Grid has to cover the full shortfall
assert losses == 0.0 # No losses as the battery didn't discharge
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_expected_direct_consumption.assert_called_once_with(
consumption, generation
)
def test_process_energy_battery_full_at_start(inverter, mock_battery):
# Battery is full, so no charging happens
mock_battery.charge_energy.return_value = (0.0, 0.0)
generation = 500.0
consumption = 200.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == pytest.approx(
300.0, rel=1e-2
) # All excess energy should be fed into the grid
assert grid_import == 0.0 # No grid draw
assert losses == 0.0 # No losses
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_expected_direct_consumption.assert_called_once_with(
consumption, generation
)
def test_process_energy_insufficient_generation_no_battery(inverter, mock_battery):
# Insufficient generation and no battery discharge
mock_battery.discharge_energy.return_value = (0.0, 0.0)
generation = 100.0
consumption = 500.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == 0.0 # No feed-in as generation is insufficient
assert grid_import == pytest.approx(400.0, rel=1e-2) # Grid supplies the shortfall
assert losses == 0.0 # No losses
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_expected_direct_consumption.assert_called_once_with(
consumption, generation
)
def test_process_energy_insufficient_generation_battery_assists(inverter, mock_battery):
# Battery assists with some discharge to cover the shortfall
mock_battery.discharge_energy.return_value = (
50.0,
5.0,
) # Battery discharges 50 Wh with 5 Wh loss
generation = 200.0
consumption = 400.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == 0.0 # No feed-in as generation is insufficient
assert grid_import == pytest.approx(
150.0, rel=1e-2
) # Grid supplies the remaining shortfall after battery discharge
assert losses == 5.0 # Discharge losses
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_expected_direct_consumption.assert_called_once_with(
consumption, generation
)
def test_process_energy_zero_generation(inverter, mock_battery):
# Zero generation, full reliance on battery and grid
mock_battery.discharge_energy.return_value = (
100.0,
5.0,
) # Battery discharges 100 Wh with 5 Wh loss
generation = 0.0
consumption = 300.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == 0.0 # No feed-in as there is zero generation
assert grid_import == pytest.approx(200.0, rel=1e-2) # Grid supplies the remaining shortfall
assert losses == 5.0 # Discharge losses
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_expected_direct_consumption.assert_not_called()
def test_process_energy_zero_consumption(inverter, mock_battery):
# Generation exceeds consumption, but consumption is zero
mock_battery.charge_energy.return_value = (100.0, 10.0)
generation = 500.0
consumption = 0.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == pytest.approx(390.0, rel=1e-2) # Excess energy after battery charges
assert grid_import == 0.0 # No grid draw as no consumption
assert losses == 10.0 # Charging losses
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_expected_direct_consumption.assert_not_called()
def test_process_energy_zero_generation_zero_consumption(inverter, mock_battery):
generation = 0.0
consumption = 0.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == 0.0 # No feed-in
assert grid_import == 0.0 # No grid draw
assert losses == 0.0 # No losses
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_expected_direct_consumption.assert_not_called()
def test_process_energy_partial_battery_discharge(inverter, mock_battery):
mock_battery.discharge_energy.return_value = (50.0, 5.0)
generation = 200.0
consumption = 400.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == 0.0 # No feed-in due to insufficient generation
assert grid_import == pytest.approx(
150.0, rel=1e-2
) # Grid supplies the shortfall after battery assist
assert losses == 5.0 # Discharge losses
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_expected_direct_consumption.assert_called_once_with(
consumption, generation
)
def test_process_energy_consumption_exceeds_max_no_battery(inverter, mock_battery):
# Battery is empty, and consumption is much higher than the inverter's max power
mock_battery.discharge_energy.return_value = (0.0, 0.0)
generation = 100.0
consumption = 1000.0 # Exceeds the inverter's max power
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == 0.0 # No feed-in
assert grid_import == pytest.approx(900.0, rel=1e-2) # Grid covers the remaining shortfall
assert losses == 0.0 # No losses as the battery didn’t assist
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_expected_direct_consumption.assert_called_once_with(
consumption, generation
)
def test_process_energy_zero_generation_full_battery_high_consumption(inverter, mock_battery):
# Full battery, no generation, and high consumption
mock_battery.discharge_energy.return_value = (500.0, 10.0)
generation = 0.0
consumption = 600.0
hour = 12
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
generation, consumption, hour
)
assert grid_export == 0.0 # No feed-in due to zero generation
assert grid_import == pytest.approx(
100.0, rel=1e-2
) # Grid covers remaining shortfall after battery discharge
assert losses == 10.0 # Battery discharge losses
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_expected_direct_consumption.assert_not_called()
class TestInverterSettings:
@pytest.mark.parametrize("converter", ["to_genetic_param", "to_genetic0_param"])
def test_inverter_conversion_requires_nominal_power(self, config_eos, converter):
# The generic inverter profile provides no nominal power.
settings = InverterCommonSettings(device_id="inverter")
convert = (
(lambda: genetic0prepare.inverter_request(settings))
if converter == "to_genetic0_param"
else (lambda: geneticprepare.inverter_request(settings))
)
with pytest.raises(ValueError, match="nominal_power_w"):
convert()
settings.profile_adjustment.nominal_power_w = 4200
assert convert().max_power_wh == 4200
def test_battery_discharge_limit_caps_load_and_grid_export():
"""The inverter's inlined discharge uses the battery's discharge power limit."""
battery = GeneticBattery(
GeneticBatteryRequest(
device_id="battery",
capacity_wh=10000,
charging_efficiency=1.0,
discharging_efficiency=1.0,
max_charge_power_w=7000,
max_discharge_power_w=4000,
initial_soc_percentage=100,
).to_parameters(),
prediction_hours=1,
slot_duration_h=0.25,
)
battery.set_discharge_per_hour(np.array([1]))
quarter_hour_inverter = GeneticInverter(
GeneticInverterRequest(
device_id="inverter",
max_power_wh=10000,
battery_id="battery",
dc_to_ac_efficiency=1.0,
ac_to_dc_efficiency=1.0,
).to_parameters(),
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=1500.0,
hour=0,
allow_battery_grid_export=True,
)
# 4000 W for a quarter hour: 1000 Wh, all of it for the load.
assert initial_soc_wh - battery.soc_wh == pytest.approx(1000.0)
assert grid_import == pytest.approx(500.0)
assert grid_export == 0.0
class TestFirstSlotFraction:
"""A run within slot 0 simulates only the remaining part of that slot."""
@staticmethod
def _inverter(first_slot_fraction=0.25, battery=None, **parameters):
predictor = Mock()
predictor.calculate_expected_direct_consumption.side_effect = min
with patch(
"akkudoktoreos.optimization.genetic.devices.geneticinverter.get_genetic_load_interpolator",
return_value=predictor,
):
inverter = GeneticInverter(
GeneticInverterRequest(
device_id="inverter1",
max_power_wh=4000.0,
battery_id=battery.parameters.device_id if battery else None,
**parameters,
).to_parameters(),
battery=battery,
first_slot_fraction=first_slot_fraction,
)
return inverter, predictor
def test_slot_0_moves_only_the_remaining_part(self):
inverter, _ = self._inverter()
grid_export, _, losses, _ = inverter.process_energy(5000.0, 0.0, hour=0)
assert grid_export == pytest.approx(1000.0)
assert losses == pytest.approx(4000.0)
grid_export, _, losses, _ = inverter.process_energy(5000.0, 0.0, hour=1)
assert grid_export == pytest.approx(4000.0)
assert losses == pytest.approx(1000.0)
def test_self_consumption_table_gets_the_power_of_the_remaining_part(self):
"""250 Wh in 15 min is 1000 W, not 250 W."""
inverter, predictor = self._inverter()
inverter.process_energy(500.0, 250.0, hour=0)
predictor.calculate_expected_direct_consumption.assert_called_with(1000.0, 2000.0)
def test_efficiency_curve_is_read_at_the_power_of_the_remaining_part(self):
inverter, _ = self._inverter(
dc_to_ac_efficiency_curve=[(0.0, 0.97), (0.25, 0.97), (1.0, 0.90)]
)
# 1000 Wh in 15 min is the full 4000 W.
assert inverter.dc_to_ac_efficiency_at(1000.0, 0.25) == pytest.approx(0.90)
assert inverter.dc_to_ac_efficiency_at(1000.0) == pytest.approx(0.97)
def test_ac_charge_in_slot_0_uses_the_remaining_part(self):
battery = GeneticBattery(
GeneticBatteryRequest(
device_id="battery1",
capacity_wh=40_000,
initial_soc_percentage=0,
min_soc_percentage=0,
max_charge_power_w=8_000,
charging_efficiency=1.0,
).to_parameters(),
prediction_hours=2,
first_slot_fraction=0.25,
)
battery.set_charge_per_hour(np.ones(2))
inverter, _ = self._inverter(
battery=battery, ac_to_dc_efficiency=1.0, ac_charge_limits_total_charge=True
)
inverter.begin_ac_charge_slot(0, 0.5)
ac_wh, _ = inverter.charge_battery_from_grid(0, 0.5)
# Half the charge power for 15 min.
assert ac_wh == pytest.approx(1000.0)
@pytest.mark.parametrize("fraction", [0.0, 1.01])
def test_invalid_fraction_is_rejected(self, fraction):
with pytest.raises(ValueError, match="first_slot_fraction"):
self._inverter(first_slot_fraction=fraction)