-
Notifications
You must be signed in to change notification settings - Fork 13
tests: add logic scenario helpers #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+119
−63
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| """Shared fixtures for logic tests.""" | ||
| import pytest | ||
|
|
||
| from batcontrol.logic.common import CommonLogic | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reset_common_logic(): | ||
| """Keep the CommonLogic singleton from leaking settings between tests.""" | ||
| CommonLogic._instance = None | ||
| yield | ||
| CommonLogic._instance = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """Helpers for logic scenario tests.""" | ||
| import datetime | ||
|
|
||
| import numpy as np | ||
|
|
||
| from batcontrol.logic.common import CommonLogic | ||
| from batcontrol.logic.logic_interface import ( | ||
| CalculationInput, | ||
| CalculationParameters, | ||
| PeakShavingConfig, | ||
| ) | ||
|
|
||
|
|
||
| CAPACITY_WH = 10240 | ||
| MIN_SOC = 0.10 | ||
| MAX_CHARGING_FROM_GRID_LIMIT = 0.89 | ||
| MIN_GRID_CHARGE_SOC = 0.55 | ||
| CHEAP_PRICE = 0.4635 | ||
| EXPENSIVE_PRICE = 0.7018 | ||
|
|
||
|
|
||
| def make_logic(logic_cls, *, | ||
| timezone=datetime.timezone.utc, | ||
| capacity_wh=CAPACITY_WH, | ||
| max_charging_from_grid_limit=MAX_CHARGING_FROM_GRID_LIMIT, | ||
| min_grid_charge_soc=MIN_GRID_CHARGE_SOC, | ||
| preserve_min_grid_charge_soc=True, | ||
| min_price_difference=0.05, | ||
| min_price_difference_rel=0.10, | ||
| charge_rate_multiplier=1.1, | ||
| always_allow_discharge_limit=0.90, | ||
| min_charge_energy=100, | ||
| peak_shaving_enabled=False): | ||
| """Create a logic instance with common scenario defaults. | ||
|
|
||
| The CommonLogic singleton is reset so each helper call applies the | ||
| requested singleton-backed tuning values independently. | ||
| """ | ||
| CommonLogic._instance = None | ||
| CommonLogic.get_instance( | ||
| charge_rate_multiplier=charge_rate_multiplier, | ||
| always_allow_discharge_limit=always_allow_discharge_limit, | ||
| max_capacity=capacity_wh, | ||
| min_charge_energy=min_charge_energy, | ||
| ) | ||
| logic = logic_cls(timezone=timezone, interval_minutes=60) | ||
| logic.set_calculation_parameters(CalculationParameters( | ||
| max_charging_from_grid_limit=max_charging_from_grid_limit, | ||
| min_price_difference=min_price_difference, | ||
| min_price_difference_rel=min_price_difference_rel, | ||
| max_capacity=capacity_wh, | ||
| min_grid_charge_soc=min_grid_charge_soc, | ||
| preserve_min_grid_charge_soc=preserve_min_grid_charge_soc, | ||
| peak_shaving=PeakShavingConfig(enabled=peak_shaving_enabled), | ||
| )) | ||
| return logic | ||
|
|
||
|
|
||
| def make_calc_input(production, consumption, prices, soc, *, | ||
| capacity_wh=CAPACITY_WH, | ||
| min_soc=MIN_SOC): | ||
| """Build CalculationInput from forecast arrays and state of charge. | ||
|
|
||
| Args: | ||
| production: Forecast production values in Wh for each time slot. | ||
| consumption: Forecast consumption values in Wh for each time slot. | ||
| prices: Energy prices for each time slot. | ||
| soc: Current battery state of charge as a percentage, 0-100. | ||
| capacity_wh: Battery capacity in Wh. | ||
| min_soc: Minimum battery state of charge as a ratio, 0-1. | ||
| """ | ||
| stored_energy = capacity_wh * soc / 100 | ||
| min_soc_energy = capacity_wh * min_soc | ||
| return CalculationInput( | ||
| production=np.array(production, dtype=float), | ||
| consumption=np.array(consumption, dtype=float), | ||
| prices={slot: price for slot, price in enumerate(prices)}, | ||
| stored_energy=stored_energy, | ||
| stored_usable_energy=max(0.0, stored_energy - min_soc_energy), | ||
| free_capacity=capacity_wh - stored_energy, | ||
| ) | ||
|
|
||
|
|
||
| def target_usable_energy(*, | ||
| capacity_wh=CAPACITY_WH, | ||
| min_soc=MIN_SOC, | ||
| min_grid_charge_soc=MIN_GRID_CHARGE_SOC): | ||
| """Return usable energy reserved by the minimum grid-charge target.""" | ||
| return capacity_wh * (min_grid_charge_soc - min_soc) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.