mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-12-02 18:36:28 +01:00
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)
724 lines
30 KiB
Python
724 lines
30 KiB
Python
"""Tests for the load-dependent efficiency curves of the GENETIC inverter.
|
|
|
|
``dc_to_ac_efficiency_curve`` replaces the constant ``dc_to_ac_efficiency`` by a
|
|
curve over the load fraction (AC power / rated power). Real inverters are much
|
|
less efficient at low load, which matters for night-time discharge into a small
|
|
base load. ``ac_to_dc_efficiency_curve`` does the same for charging the battery
|
|
from the grid. Without a curve the behaviour must stay exactly the legacy one.
|
|
"""
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock, patch
|
|
|
|
import numpy as np
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from akkudoktoreos.devices.devicesabc import (
|
|
interpolate_efficiency_curve,
|
|
solve_ac_for_charge_dc_energy,
|
|
solve_ac_for_dc_energy,
|
|
validate_efficiency_curve,
|
|
)
|
|
from akkudoktoreos.devices.settings.invertersettings import (
|
|
InverterCommonSettings,
|
|
InverterProfileAdjustment,
|
|
)
|
|
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,
|
|
GeneticEnergyManagementRequest,
|
|
GeneticInverterRequest,
|
|
)
|
|
from akkudoktoreos.optimization.genetic.geneticsimulation import (
|
|
GeneticSimulation,
|
|
)
|
|
from akkudoktoreos.optimization.genetic.genetictailvalue import build_tail_value_curve
|
|
from akkudoktoreos.optimization.genetic.geneticterminalvalue import TerminalValueCurve
|
|
|
|
CURVE = [(0.05, 0.80), (0.20, 0.90), (0.50, 0.95), (1.0, 0.93)]
|
|
|
|
|
|
def _make_inverter(
|
|
curve=None,
|
|
max_power_wh: float = 1000.0,
|
|
battery=None,
|
|
dc_to_ac_efficiency: float = 1.0,
|
|
**kwargs,
|
|
) -> GeneticInverter:
|
|
predictor = Mock()
|
|
predictor.calculate_expected_direct_consumption.side_effect = min
|
|
with patch(
|
|
"akkudoktoreos.optimization.genetic.devices.geneticinverter.get_genetic_load_interpolator",
|
|
return_value=predictor,
|
|
):
|
|
return GeneticInverter(
|
|
GeneticInverterRequest(
|
|
device_id="inverter1",
|
|
max_power_wh=max_power_wh,
|
|
battery_id=battery.parameters.device_id if battery else None,
|
|
dc_to_ac_efficiency=dc_to_ac_efficiency,
|
|
dc_to_ac_efficiency_curve=curve,
|
|
**kwargs,
|
|
).to_parameters(),
|
|
battery=battery,
|
|
)
|
|
|
|
|
|
def _passthrough_battery() -> Mock:
|
|
"""Battery mock that delivers exactly the requested DC energy."""
|
|
battery = Mock()
|
|
battery.parameters.device_id = "battery1"
|
|
battery.charge_energy = Mock(return_value=(0.0, 0.0))
|
|
battery.discharge_energy = Mock(side_effect=lambda dc, hour, **kw: (dc, 0.0))
|
|
return battery
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Validation
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestCurveValidation:
|
|
def test_none_is_allowed(self):
|
|
assert validate_efficiency_curve(None) is None
|
|
params = GeneticInverterRequest(device_id="inverter1", max_power_wh=1000)
|
|
assert params.dc_to_ac_efficiency_curve is None
|
|
assert params.dc_to_ac_efficiency_reference_load_fraction == pytest.approx(0.06)
|
|
|
|
def test_valid_curve_is_accepted(self):
|
|
params = GeneticInverterRequest(
|
|
device_id="inverter1", max_power_wh=1000, dc_to_ac_efficiency_curve=CURVE
|
|
)
|
|
assert params.dc_to_ac_efficiency_curve == CURVE
|
|
|
|
def test_json_lists_are_accepted(self):
|
|
params = GeneticInverterRequest.model_validate(
|
|
{
|
|
"device_id": "inverter1",
|
|
"max_power_wh": 1000,
|
|
"dc_to_ac_efficiency_curve": [[0.0, 0.8], [1.0, 0.95]],
|
|
}
|
|
)
|
|
assert params.dc_to_ac_efficiency_curve == [(0.0, 0.8), (1.0, 0.95)]
|
|
|
|
@pytest.mark.parametrize(
|
|
"curve",
|
|
[
|
|
[],
|
|
[(0.5, 0.9)], # fewer than two points
|
|
[(0.5, 0.9), (0.2, 0.8)], # not sorted
|
|
[(0.2, 0.9), (0.2, 0.8)], # duplicate load fraction
|
|
[(-0.1, 0.9), (1.0, 0.9)], # load fraction below 0
|
|
[(0.0, 0.9), (1.1, 0.9)], # load fraction above 1
|
|
[(0.0, 0.0), (1.0, 0.9)], # efficiency 0
|
|
[(0.0, 0.9), (1.0, 1.2)], # efficiency above 1
|
|
[(0.0, float("nan")), (1.0, 0.9)], # not finite
|
|
],
|
|
)
|
|
def test_invalid_curve_is_rejected(self, curve):
|
|
with pytest.raises(ValidationError):
|
|
GeneticInverterRequest(
|
|
device_id="inverter1", max_power_wh=1000, dc_to_ac_efficiency_curve=curve
|
|
)
|
|
|
|
@pytest.mark.parametrize("fraction", [-0.1, 1.1])
|
|
def test_reference_load_fraction_range(self, fraction):
|
|
with pytest.raises(ValidationError):
|
|
GeneticInverterRequest(
|
|
device_id="inverter1",
|
|
max_power_wh=1000,
|
|
dc_to_ac_efficiency_reference_load_fraction=fraction,
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Interpolation
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestInterpolation:
|
|
def test_points_midpoints_and_clamping(self):
|
|
assert interpolate_efficiency_curve(CURVE, 0.05) == pytest.approx(0.80)
|
|
assert interpolate_efficiency_curve(CURVE, 0.20) == pytest.approx(0.90)
|
|
assert interpolate_efficiency_curve(CURVE, 1.0) == pytest.approx(0.93)
|
|
assert interpolate_efficiency_curve(CURVE, 0.125) == pytest.approx(0.85)
|
|
assert interpolate_efficiency_curve(CURVE, 0.75) == pytest.approx(0.94)
|
|
# Clamped to the first and the last point.
|
|
assert interpolate_efficiency_curve(CURVE, 0.0) == pytest.approx(0.80)
|
|
assert interpolate_efficiency_curve(CURVE, 5.0) == pytest.approx(0.93)
|
|
|
|
def test_inverter_evaluates_slot_load_fraction(self):
|
|
inverter = _make_inverter(CURVE, max_power_wh=1000.0)
|
|
assert inverter.dc_to_ac_efficiency_at(50.0) == pytest.approx(0.80)
|
|
assert inverter.dc_to_ac_efficiency_at(125.0) == pytest.approx(0.85)
|
|
assert inverter.dc_to_ac_efficiency_at(10.0) == pytest.approx(0.80)
|
|
assert inverter.dc_to_ac_efficiency_at(5000.0) == pytest.approx(0.93)
|
|
|
|
def test_curve_scales_with_device_size(self):
|
|
"""The same relative load gives the same efficiency on any device size."""
|
|
small = _make_inverter(CURVE, max_power_wh=5000.0)
|
|
large = _make_inverter(CURVE, max_power_wh=10000.0)
|
|
assert small.dc_to_ac_efficiency_at(1000.0) == pytest.approx(
|
|
large.dc_to_ac_efficiency_at(2000.0)
|
|
)
|
|
assert small.dc_to_ac_efficiency_at(1000.0) == pytest.approx(0.90)
|
|
|
|
def test_load_fraction_uses_slot_duration(self):
|
|
"""max_power_wh is scaled to the slot, so 250 Wh in 15 min is 1 kW."""
|
|
predictor = Mock()
|
|
with patch(
|
|
"akkudoktoreos.optimization.genetic.devices.geneticinverter.get_genetic_load_interpolator",
|
|
return_value=predictor,
|
|
):
|
|
inverter = GeneticInverter(
|
|
GeneticInverterRequest(
|
|
device_id="inverter1",
|
|
max_power_wh=5000.0,
|
|
dc_to_ac_efficiency_curve=CURVE,
|
|
).to_parameters(),
|
|
slot_duration_h=0.25,
|
|
)
|
|
# 1 kW of 5 kW -> load fraction 0.2
|
|
assert inverter.dc_to_ac_efficiency_at(250.0) == pytest.approx(0.90)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Without curve: unchanged behaviour
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestWithoutCurve:
|
|
@pytest.mark.parametrize("efficiency", [1.0, 0.95, 0.8])
|
|
def test_efficiency_is_the_constant(self, efficiency):
|
|
inverter = _make_inverter(None, dc_to_ac_efficiency=efficiency)
|
|
for ac_wh in (0.0, 10.0, 500.0, 1000.0, 5000.0):
|
|
assert inverter.dc_to_ac_efficiency_at(ac_wh) == efficiency
|
|
assert inverter.reference_dc_to_ac_efficiency == efficiency
|
|
|
|
def test_reference_load_fraction_is_ignored(self):
|
|
inverter = _make_inverter(
|
|
None, dc_to_ac_efficiency=0.9, dc_to_ac_efficiency_reference_load_fraction=0.5
|
|
)
|
|
assert inverter.reference_dc_to_ac_efficiency == 0.9
|
|
|
|
def test_discharge_matches_legacy_formula(self):
|
|
battery = _passthrough_battery()
|
|
inverter = _make_inverter(None, battery=battery, dc_to_ac_efficiency=0.93)
|
|
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
|
|
0.0, 100.0, 0
|
|
)
|
|
requested_dc = battery.discharge_energy.call_args[0][0]
|
|
assert requested_dc == 100.0 / 0.93
|
|
assert self_consumption == requested_dc * 0.93
|
|
assert losses == requested_dc - requested_dc * 0.93
|
|
assert grid_import == pytest.approx(0.0, abs=1e-9)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# With curve: process_energy
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestProcessEnergyWithCurve:
|
|
def test_partial_load_draws_more_dc(self):
|
|
"""100 Wh at 10 % load (efficiency 0.8333) needs 120 Wh DC."""
|
|
battery = _passthrough_battery()
|
|
inverter = _make_inverter(CURVE, max_power_wh=1000.0, battery=battery)
|
|
|
|
grid_export, grid_import, losses, self_consumption = inverter.process_energy(
|
|
0.0, 100.0, 0
|
|
)
|
|
|
|
efficiency = 0.80 + (0.05 / 0.15) * 0.10
|
|
requested_dc = battery.discharge_energy.call_args[0][0]
|
|
assert requested_dc == pytest.approx(100.0 / efficiency)
|
|
assert grid_import == pytest.approx(0.0, abs=1e-9)
|
|
assert self_consumption == pytest.approx(100.0)
|
|
assert losses == pytest.approx(requested_dc - 100.0)
|
|
|
|
def test_grid_export_uses_efficiency_at_export_level(self, config_eos):
|
|
"""A full-power export runs at the efficiency of the rated load."""
|
|
battery = GeneticBattery(
|
|
GeneticBatteryRequest(
|
|
device_id="battery1",
|
|
capacity_wh=10000,
|
|
initial_soc_percentage=100,
|
|
min_soc_percentage=0,
|
|
charging_efficiency=1.0,
|
|
discharging_efficiency=1.0,
|
|
max_charge_power_w=1000,
|
|
).to_parameters(),
|
|
prediction_hours=1,
|
|
)
|
|
battery.reset()
|
|
battery.discharge_array = np.ones(1)
|
|
inverter = _make_inverter(CURVE, max_power_wh=1000.0, battery=battery)
|
|
|
|
grid_export, _, losses, _ = inverter.process_energy(
|
|
0.0, 0.0, 0, allow_battery_grid_export=True, battery_grid_export_factor=1.0
|
|
)
|
|
|
|
# The export bound is estimated at the rated DC energy (load fraction
|
|
# 1.0, efficiency 0.93). The conversion itself uses the efficiency at
|
|
# the exported AC energy and never draws more than the rated DC energy.
|
|
assert grid_export == pytest.approx(930.0)
|
|
efficiency = inverter.dc_to_ac_efficiency_at(930.0)
|
|
assert 0.93 < efficiency < 0.95
|
|
assert losses == pytest.approx(930.0 / efficiency - 930.0)
|
|
assert grid_export + losses <= 1000.0 + 1e-9
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Reference efficiency for valuing stored energy
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestReferenceEfficiency:
|
|
def test_default_reference_load(self):
|
|
inverter = _make_inverter(CURVE, max_power_wh=1000.0)
|
|
# 6 % load -> between 0.05:0.80 and 0.20:0.90
|
|
expected = 0.80 + (0.06 - 0.05) / (0.20 - 0.05) * (0.90 - 0.80)
|
|
assert inverter.reference_dc_to_ac_efficiency == pytest.approx(expected)
|
|
|
|
def test_configured_reference_load(self):
|
|
inverter = _make_inverter(
|
|
CURVE, max_power_wh=1000.0, dc_to_ac_efficiency_reference_load_fraction=0.5
|
|
)
|
|
assert inverter.reference_dc_to_ac_efficiency == pytest.approx(0.95)
|
|
|
|
def test_tail_value_uses_reference_efficiency(self):
|
|
"""Stored energy is valued with the efficiency at the reference load."""
|
|
|
|
def tail_curve(efficiency_curve):
|
|
battery = GeneticBattery(
|
|
GeneticBatteryRequest(
|
|
device_id="battery1",
|
|
capacity_wh=1000,
|
|
max_charge_power_w=1000,
|
|
charging_efficiency=1.0,
|
|
discharging_efficiency=1.0,
|
|
initial_soc_percentage=50,
|
|
charge_rates=[0, 0.5, 1],
|
|
).to_parameters(),
|
|
prediction_hours=1,
|
|
)
|
|
inverter = GeneticInverter(
|
|
GeneticInverterRequest(
|
|
device_id="inverter1",
|
|
battery_id="battery1",
|
|
max_power_wh=5000,
|
|
dc_to_ac_efficiency_curve=efficiency_curve,
|
|
).to_parameters(),
|
|
battery=battery,
|
|
)
|
|
return build_tail_value_curve(
|
|
battery=battery,
|
|
inverter=inverter,
|
|
# Grid charging is never worth it at this price.
|
|
prices_euro_per_wh=np.ones(1),
|
|
feed_in_euro_per_wh=np.zeros(1),
|
|
load_wh=np.zeros(1),
|
|
pv_wh=np.zeros(1),
|
|
continuation=TerminalValueCurve(energy_wh=[0.0, 2000.0], value_euro=[0.0, 2.0]),
|
|
charge_rates=[0.5, 1],
|
|
export_rates=[1],
|
|
direct_marketing=False,
|
|
)
|
|
|
|
flat = tail_curve([(0.0, 1.0), (1.0, 1.0)])
|
|
lossy = tail_curve([(0.0, 0.8), (1.0, 0.8)])
|
|
# The same AC energy needs more stored DC energy with lossy conversion.
|
|
assert lossy.value(400.0) == pytest.approx(flat.value(400.0))
|
|
assert lossy.energy_wh[-1] == pytest.approx(0.8 * flat.energy_wh[-1])
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Whole-simulation equivalence
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _simulate(config_eos, efficiency_curve, dc_to_ac_efficiency=0.95):
|
|
hours = 24
|
|
config_eos.merge_settings_from_dict(
|
|
{
|
|
"prediction": {"hours": hours},
|
|
"optimization": {"genetic": {"tail_horizon_hours": 0, "horizon_hours": hours}},
|
|
}
|
|
)
|
|
battery = GeneticBattery(
|
|
GeneticBatteryRequest(
|
|
device_id="battery1",
|
|
capacity_wh=10000,
|
|
initial_soc_percentage=60,
|
|
min_soc_percentage=5,
|
|
charging_efficiency=0.95,
|
|
discharging_efficiency=0.95,
|
|
max_charge_power_w=4000,
|
|
).to_parameters(),
|
|
prediction_hours=hours,
|
|
)
|
|
battery.reset()
|
|
inverter = GeneticInverter(
|
|
GeneticInverterRequest(
|
|
device_id="inverter1",
|
|
max_power_wh=5000,
|
|
battery_id="battery1",
|
|
dc_to_ac_efficiency=dc_to_ac_efficiency,
|
|
ac_to_dc_efficiency=0.95,
|
|
dc_to_ac_efficiency_curve=efficiency_curve,
|
|
).to_parameters(),
|
|
battery=battery,
|
|
)
|
|
rng = np.random.default_rng(42)
|
|
simulation = GeneticSimulation()
|
|
simulation.prepare(
|
|
GeneticEnergyManagementRequest.model_validate(
|
|
dict(
|
|
pv_prognose_wh=list(np.maximum(rng.normal(1500, 1500, hours), 0.0)),
|
|
strompreis_euro_pro_wh=list(rng.uniform(0.0001, 0.0004, hours)),
|
|
einspeiseverguetung_euro_pro_wh=list(rng.uniform(0.00005, 0.0002, hours)),
|
|
preis_euro_pro_wh_akku=0.0001,
|
|
gesamtlast=list(rng.uniform(100, 3000, hours)),
|
|
)
|
|
).to_parameters(),
|
|
optimization_hours=hours,
|
|
prediction_hours=hours,
|
|
inverter=inverter,
|
|
direct_marketing_enabled=True,
|
|
)
|
|
assert simulation.bat_discharge_hours is not None
|
|
assert simulation.bat_grid_export_hours is not None
|
|
assert simulation.ac_charge_hours is not None
|
|
simulation.bat_discharge_hours[:] = 1
|
|
simulation.bat_grid_export_hours[18:22] = [1.0, 0.5, 0.25, 1.0]
|
|
simulation.ac_charge_hours[2:4] = 0.5
|
|
return simulation.simulate(start_hour=0)
|
|
|
|
|
|
def test_flat_curve_equals_constant_efficiency(config_eos):
|
|
"""A flat curve at the constant efficiency reproduces the result exactly."""
|
|
constant = _simulate(config_eos, None)
|
|
flat = _simulate(config_eos, [(0.0, 0.95), (1.0, 0.95)])
|
|
assert constant.keys() == flat.keys()
|
|
for key, value in constant.items():
|
|
if isinstance(value, np.ndarray):
|
|
np.testing.assert_array_equal(value, flat[key], err_msg=key)
|
|
else:
|
|
assert value == flat[key], key
|
|
|
|
|
|
def test_low_load_curve_increases_losses(config_eos):
|
|
constant = _simulate(config_eos, None, dc_to_ac_efficiency=0.95)
|
|
curve = _simulate(config_eos, [(0.02, 0.80), (0.07, 0.93), (0.3, 0.96), (1.0, 0.95)])
|
|
assert curve["Gesamt_Verluste"] > constant["Gesamt_Verluste"]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Configuration
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestSettings:
|
|
"""The DC→AC curve of the GENETIC parameters comes from the inverter profile."""
|
|
|
|
def test_settings_pass_profile_curve_to_genetic_parameters(self, config_eos):
|
|
settings = InverterCommonSettings(
|
|
device_id="inverter1",
|
|
profile_adjustment=InverterProfileAdjustment(nominal_power_w=5000),
|
|
dc_to_ac_efficiency_reference_load_fraction=0.1,
|
|
)
|
|
profile = settings.get_inverter_profile()
|
|
params = geneticprepare.inverter_request(settings)
|
|
assert params.dc_to_ac_efficiency_curve is not None
|
|
# Not derated at 45 degC: the curve is the profile curve over [0, 1].
|
|
expected = list(zip(profile.dc_to_ac_efficiency.x, profile.dc_to_ac_efficiency.y))
|
|
assert [x for x, _ in params.dc_to_ac_efficiency_curve] == pytest.approx(
|
|
[x for x, _ in expected]
|
|
)
|
|
assert [y for _, y in params.dc_to_ac_efficiency_curve] == pytest.approx(
|
|
[y for _, y in expected]
|
|
)
|
|
assert params.dc_to_ac_efficiency_reference_load_fraction == 0.1
|
|
assert params.dc_to_ac_efficiency == pytest.approx(profile.dc_to_ac_efficiency_at(0.1))
|
|
|
|
def test_genetic0_has_no_curve(self, config_eos):
|
|
settings = InverterCommonSettings(
|
|
device_id="inverter1",
|
|
profile_adjustment=InverterProfileAdjustment(nominal_power_w=5000),
|
|
)
|
|
params = genetic0prepare.inverter_request(settings)
|
|
assert not hasattr(params, "dc_to_ac_efficiency_curve")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Discharge limited by the battery (low state of charge, power limit)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# Rising up to half load, flat above: efficiency(x) = 0.8 + 0.3 * x for x <= 0.5.
|
|
RISING_CURVE = [(0.0, 0.80), (0.5, 0.95), (1.0, 0.95)]
|
|
|
|
|
|
def _real_battery(
|
|
*,
|
|
capacity_wh: int = 1000,
|
|
soc_percentage: int = 100,
|
|
max_power_w: float = 1000.0,
|
|
discharging_efficiency: float = 1.0,
|
|
slot_duration_h: float = 1.0,
|
|
) -> GeneticBattery:
|
|
battery = GeneticBattery(
|
|
GeneticBatteryRequest(
|
|
device_id="battery1",
|
|
capacity_wh=capacity_wh,
|
|
initial_soc_percentage=soc_percentage,
|
|
min_soc_percentage=0,
|
|
charging_efficiency=1.0,
|
|
discharging_efficiency=discharging_efficiency,
|
|
max_charge_power_w=max_power_w,
|
|
).to_parameters(),
|
|
prediction_hours=1,
|
|
slot_duration_h=slot_duration_h,
|
|
)
|
|
battery.reset()
|
|
battery.discharge_array = np.ones(1)
|
|
return battery
|
|
|
|
|
|
def _real_inverter(battery: GeneticBattery, curve=None, slot_duration_h: float = 1.0, **kwargs) -> GeneticInverter:
|
|
predictor = Mock()
|
|
predictor.calculate_expected_direct_consumption.side_effect = min
|
|
with patch(
|
|
"akkudoktoreos.optimization.genetic.devices.geneticinverter.get_genetic_load_interpolator",
|
|
return_value=predictor,
|
|
):
|
|
return GeneticInverter(
|
|
GeneticInverterRequest(
|
|
device_id="inverter1",
|
|
max_power_wh=1000.0,
|
|
battery_id="battery1",
|
|
dc_to_ac_efficiency_curve=curve,
|
|
**kwargs,
|
|
).to_parameters(),
|
|
battery=battery,
|
|
slot_duration_h=slot_duration_h,
|
|
)
|
|
|
|
|
|
class TestSolveAcForDcEnergy:
|
|
def test_rising_piece(self):
|
|
# a = 100 * (0.8 + 0.3 * a / 1000) => a = 80 / 0.97
|
|
assert solve_ac_for_dc_energy(RISING_CURVE, 100.0, 1000.0, 500.0) == pytest.approx(
|
|
80.0 / 0.97
|
|
)
|
|
|
|
def test_clamped_ends(self):
|
|
curve = [(0.2, 0.90), (0.6, 0.96)]
|
|
# Below the first point the efficiency is constant.
|
|
assert solve_ac_for_dc_energy(curve, 100.0, 1000.0, 1000.0) == pytest.approx(90.0)
|
|
# Above the last point as well.
|
|
assert solve_ac_for_dc_energy(curve, 800.0, 1000.0, 1000.0) == pytest.approx(768.0)
|
|
|
|
def test_falling_piece(self):
|
|
curve = [(0.0, 0.90), (0.5, 0.96), (1.0, 0.90)]
|
|
ac = solve_ac_for_dc_energy(curve, 900.0, 1000.0, 1000.0)
|
|
assert ac == pytest.approx(900.0 * interpolate_efficiency_curve(curve, ac / 1000.0))
|
|
assert 0.5 <= ac / 1000.0 <= 1.0
|
|
|
|
def test_never_exceeds_the_bound(self):
|
|
assert solve_ac_for_dc_energy(RISING_CURVE, 900.0, 1000.0, 300.0) <= 300.0 + 1e-6
|
|
|
|
@pytest.mark.parametrize("dc_wh", [0.0, -5.0])
|
|
def test_nothing_from_nothing(self, dc_wh):
|
|
assert solve_ac_for_dc_energy(RISING_CURVE, dc_wh, 1000.0, 500.0) == 0.0
|
|
|
|
@pytest.mark.parametrize("curve", [CURVE, RISING_CURVE, [(0.1, 0.7), (0.3, 0.97), (1.0, 0.9)]])
|
|
@pytest.mark.parametrize("dc_wh", [1.0, 37.0, 180.0, 420.0, 760.0, 1050.0])
|
|
def test_result_is_consistent_with_the_curve(self, curve, dc_wh):
|
|
ac = solve_ac_for_dc_energy(curve, dc_wh, 1000.0, 1000.0)
|
|
if ac < 1000.0 - 1e-6:
|
|
assert ac == pytest.approx(dc_wh * interpolate_efficiency_curve(curve, ac / 1000.0))
|
|
|
|
|
|
class TestBatteryLimitedDischarge:
|
|
"""The efficiency follows the AC load that is delivered, not the request."""
|
|
|
|
def test_low_soc_hourly_slot(self):
|
|
# 100 Wh DC left, 500 Wh AC requested from a 1000 W inverter.
|
|
battery = _real_battery(soc_percentage=10)
|
|
inverter = _real_inverter(battery, RISING_CURVE)
|
|
|
|
ac, losses = inverter._discharge_battery_to_ac(500.0, 0)
|
|
|
|
assert ac == pytest.approx(80.0 / 0.97) # 82.474 Wh
|
|
assert ac / 100.0 == pytest.approx(inverter.dc_to_ac_efficiency_at(ac))
|
|
assert ac + losses == pytest.approx(100.0)
|
|
assert battery.soc_wh == pytest.approx(0.0)
|
|
|
|
def test_low_soc_quarter_hour_slot(self):
|
|
# Same 100 Wh DC, but a 15-minute slot: rated AC energy is 250 Wh, so the
|
|
# same energy is a four times higher load.
|
|
battery = _real_battery(soc_percentage=10, slot_duration_h=0.25)
|
|
inverter = _real_inverter(battery, RISING_CURVE, slot_duration_h=0.25)
|
|
|
|
ac, losses = inverter._discharge_battery_to_ac(125.0, 0)
|
|
|
|
# a = 100 * (0.8 + 0.3 * a / 250) => a = 80 / 0.88
|
|
assert ac == pytest.approx(80.0 / 0.88)
|
|
assert ac / 100.0 == pytest.approx(inverter.dc_to_ac_efficiency_at(ac))
|
|
assert ac + losses == pytest.approx(100.0)
|
|
|
|
def test_power_limited_hourly_slot(self):
|
|
# Full battery, but it can only discharge with 200 W.
|
|
battery = _real_battery(capacity_wh=10000, max_power_w=200.0)
|
|
inverter = _real_inverter(battery, RISING_CURVE)
|
|
|
|
ac, losses = inverter._discharge_battery_to_ac(500.0, 0)
|
|
|
|
# a = 200 * (0.8 + 0.3 * a / 1000) => a = 160 / 0.94
|
|
assert ac == pytest.approx(160.0 / 0.94)
|
|
assert ac / 200.0 == pytest.approx(inverter.dc_to_ac_efficiency_at(ac))
|
|
assert ac + losses == pytest.approx(200.0)
|
|
|
|
def test_power_limited_quarter_hour_slot(self):
|
|
battery = _real_battery(capacity_wh=10000, max_power_w=200.0, slot_duration_h=0.25)
|
|
inverter = _real_inverter(battery, RISING_CURVE, slot_duration_h=0.25)
|
|
|
|
ac, losses = inverter._discharge_battery_to_ac(125.0, 0)
|
|
|
|
# 50 Wh DC per slot: a = 50 * (0.8 + 0.3 * a / 250) => a = 40 / 0.94
|
|
assert ac == pytest.approx(40.0 / 0.94)
|
|
assert ac / 50.0 == pytest.approx(inverter.dc_to_ac_efficiency_at(ac))
|
|
assert ac + losses == pytest.approx(50.0)
|
|
|
|
def test_battery_discharge_losses_stay_separate(self):
|
|
# Battery delivers 90 Wh DC of its 100 Wh; the curve applies to those 90 Wh.
|
|
battery = _real_battery(soc_percentage=10, discharging_efficiency=0.9)
|
|
inverter = _real_inverter(battery, RISING_CURVE)
|
|
|
|
ac, losses = inverter._discharge_battery_to_ac(500.0, 0)
|
|
|
|
assert ac == pytest.approx(90.0 * 0.8 / (1.0 - 90.0 * 0.3 / 1000.0))
|
|
assert ac / 90.0 == pytest.approx(inverter.dc_to_ac_efficiency_at(ac))
|
|
assert ac + losses == pytest.approx(100.0)
|
|
|
|
def test_load_is_covered_with_the_delivered_energy(self):
|
|
battery = _real_battery(soc_percentage=10)
|
|
inverter = _real_inverter(battery, RISING_CURVE)
|
|
|
|
grid_export, grid_import, losses, self_consumption = inverter.process_energy(0.0, 500.0, 0)
|
|
|
|
assert self_consumption == pytest.approx(80.0 / 0.97)
|
|
assert grid_import == pytest.approx(500.0 - 80.0 / 0.97)
|
|
assert grid_export == 0.0
|
|
assert self_consumption + losses == pytest.approx(100.0)
|
|
|
|
def test_unlimited_discharge_is_unchanged(self):
|
|
battery = _real_battery(capacity_wh=10000)
|
|
inverter = _real_inverter(battery, RISING_CURVE)
|
|
|
|
ac, losses = inverter._discharge_battery_to_ac(500.0, 0)
|
|
|
|
assert ac == pytest.approx(500.0)
|
|
assert losses == pytest.approx(500.0 / 0.95 - 500.0)
|
|
|
|
@pytest.mark.parametrize("slot_duration_h", [1.0, 0.25])
|
|
@pytest.mark.parametrize("efficiency", [1.0, 0.95, 0.8])
|
|
def test_constant_efficiency_is_unchanged(self, slot_duration_h, efficiency):
|
|
# Without a curve a limited discharge converts with the constant, as before.
|
|
low = _real_battery(soc_percentage=10, slot_duration_h=slot_duration_h)
|
|
inverter = _real_inverter(
|
|
low, None, slot_duration_h=slot_duration_h, dc_to_ac_efficiency=efficiency
|
|
)
|
|
ac, losses = inverter._discharge_battery_to_ac(500.0 * slot_duration_h, 0)
|
|
assert ac == pytest.approx(100.0 * efficiency)
|
|
assert losses == pytest.approx(100.0 * (1.0 - efficiency))
|
|
|
|
limited = _real_battery(
|
|
capacity_wh=10000, max_power_w=200.0, slot_duration_h=slot_duration_h
|
|
)
|
|
inverter = _real_inverter(
|
|
limited, None, slot_duration_h=slot_duration_h, dc_to_ac_efficiency=efficiency
|
|
)
|
|
ac, losses = inverter._discharge_battery_to_ac(500.0 * slot_duration_h, 0)
|
|
assert ac == pytest.approx(200.0 * slot_duration_h * efficiency)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# AC→DC curve for charging the battery from the grid
|
|
# ---------------------------------------------------------------------------
|
|
|
|
CHARGE_CURVE = [(0.05, 0.86), (0.3, 0.96), (1.0, 0.95)]
|
|
|
|
|
|
class TestSolveAcForChargeDcEnergy:
|
|
@pytest.mark.parametrize("dc_wh", [1.0, 30.0, 100.0, 250.0, 500.0, 900.0, 2000.0])
|
|
def test_solution_is_consistent(self, dc_wh):
|
|
ac_wh = solve_ac_for_charge_dc_energy(CHARGE_CURVE, dc_wh, 1000.0)
|
|
efficiency = interpolate_efficiency_curve(CHARGE_CURVE, ac_wh / 1000.0)
|
|
assert ac_wh * efficiency == pytest.approx(dc_wh, rel=1e-9)
|
|
|
|
def test_clamped_ends(self):
|
|
# Below the first point the first efficiency applies, above the last the last.
|
|
assert solve_ac_for_charge_dc_energy(CHARGE_CURVE, 8.6, 1000.0) == pytest.approx(10.0)
|
|
assert solve_ac_for_charge_dc_energy(CHARGE_CURVE, 1900.0, 1000.0) == pytest.approx(2000.0)
|
|
|
|
def test_flat_curve_equals_constant_efficiency(self):
|
|
flat = [(0.0, 0.9), (1.0, 0.9)]
|
|
assert solve_ac_for_charge_dc_energy(flat, 450.0, 1000.0) == pytest.approx(500.0)
|
|
|
|
def test_no_energy(self):
|
|
assert solve_ac_for_charge_dc_energy(CHARGE_CURVE, 0.0, 1000.0) == 0.0
|
|
|
|
|
|
class TestChargeFromGridWithCurve:
|
|
def test_curve_is_validated(self):
|
|
with pytest.raises(ValidationError):
|
|
GeneticInverterRequest(
|
|
device_id="inverter1", max_power_wh=1000, ac_to_dc_efficiency_curve=[(0.5, 0.9)]
|
|
)
|
|
|
|
@staticmethod
|
|
def _empty_battery() -> GeneticBattery:
|
|
battery = _real_battery(soc_percentage=0)
|
|
battery.charge_array = np.ones(1)
|
|
return battery
|
|
|
|
def test_low_load_charge_draws_more_ac(self):
|
|
flat = _real_inverter(self._empty_battery(), ac_to_dc_efficiency=0.96)
|
|
curved = _real_inverter(
|
|
self._empty_battery(),
|
|
ac_to_dc_efficiency=0.96,
|
|
ac_to_dc_efficiency_curve=CHARGE_CURVE,
|
|
)
|
|
# 5 % of the 1 kW battery charge power: a low AC load, efficiency ~0.87.
|
|
flat_ac, flat_losses = flat.charge_battery_from_grid(0, 0.05)
|
|
curved_ac, curved_losses = curved.charge_battery_from_grid(0, 0.05)
|
|
assert flat_ac == pytest.approx(50.0 / 0.96)
|
|
assert curved_ac > flat_ac
|
|
assert curved_ac * interpolate_efficiency_curve(
|
|
CHARGE_CURVE, curved_ac / 1000.0
|
|
) == pytest.approx(50.0)
|
|
assert curved_losses == pytest.approx(curved_ac - 50.0)
|
|
|
|
def test_settings_pass_the_profile_curve(self, config_eos):
|
|
settings = InverterCommonSettings(
|
|
device_id="inverter1",
|
|
profile_adjustment=InverterProfileAdjustment(nominal_power_w=5000),
|
|
)
|
|
profile = settings.get_inverter_profile()
|
|
params = geneticprepare.inverter_request(settings)
|
|
assert params.ac_to_dc_efficiency_curve is not None
|
|
assert [y for _, y in params.ac_to_dc_efficiency_curve] == pytest.approx(
|
|
list(profile.ac_to_dc_efficiency.y)
|
|
)
|
|
# The constant is the efficiency at the AC charge limit, here the nominal power.
|
|
assert params.ac_to_dc_efficiency == pytest.approx(profile.ac_to_dc_efficiency_at(1.0))
|