Skip to content

Commit

Permalink
Put back skew against charging from solar at end of period (#1271)
Browse files Browse the repository at this point in the history
* Put back skew against charging from solar at end of period

* [pre-commit.ci lite] apply automatic fixes

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
springfall2008 and pre-commit-ci-lite[bot] authored Jun 29, 2024
1 parent 7af331b commit 74277f6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
27 changes: 18 additions & 9 deletions apps/predbat/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,19 +600,28 @@ def run_prediction(self, charge_limit, charge_window, discharge_window, discharg
battery_state = "f+"
first_charge = min(first_charge, minute)

if self.inverter_hybrid:
if inverter_hybrid:
pv_dc = min(abs(battery_draw), pv_now)
else:
pv_dc = 0
pv_ac = (pv_now - pv_dc) * inverter_loss_ac

if (charge_limit_n - soc) < (charge_rate_now_curve * step):
# The battery will hit the charge limit in this period, so if the charge was spread over the period
# it could be done from solar, but in reality it will be full rate and then stop meaning the solar
# won't cover it and it will likely create an import.
pv_compare = pv_dc + pv_ac
if pv_dc >= (charge_limit_n - soc) and (pv_compare < (charge_rate_now_curve * step)):
potential_import = (charge_rate_now_curve * step) - pv_compare
metric_keep += potential_import * rate_import.get(minute_absolute, 0)
else:
# ECO Mode
pv_ac = pv_now * inverter_loss_ac
pv_dc = 0
diff = get_diff(0, pv_dc, pv_ac, load_yesterday, inverter_loss)

required_for_load = load_yesterday / inverter_loss_ac
if self.inverter_hybrid:
if inverter_hybrid:
potential_to_charge = pv_now
else:
potential_to_charge = pv_ac
Expand All @@ -629,15 +638,15 @@ def run_prediction(self, charge_limit, charge_window, discharge_window, discharg
else:
battery_state = "e~"

if self.inverter_hybrid:
if inverter_hybrid:
pv_dc = min(abs(battery_draw), pv_now)
else:
pv_dc = 0
pv_ac = (pv_now - pv_dc) * inverter_loss_ac

# Clamp at inverter limit
if self.inverter_hybrid:
battery_inverted = get_total_inverted(battery_draw, pv_dc, 0, inverter_loss, self.inverter_hybrid)
if inverter_hybrid:
battery_inverted = get_total_inverted(battery_draw, pv_dc, 0, inverter_loss, inverter_hybrid)
if battery_inverted > inverter_limit:
over_limit = battery_inverted - inverter_limit

Expand All @@ -652,14 +661,14 @@ def run_prediction(self, charge_limit, charge_window, discharge_window, discharg
pv_ac = (pv_now - pv_dc) * inverter_loss_ac

# Clip battery discharge back
total_inverted = get_total_inverted(battery_draw, pv_dc, pv_ac, inverter_loss, self.inverter_hybrid)
total_inverted = get_total_inverted(battery_draw, pv_dc, pv_ac, inverter_loss, inverter_hybrid)
if total_inverted > inverter_limit and (battery_draw + pv_dc) > 0:
over_limit = total_inverted - inverter_limit
if battery_draw + pv_dc > 0:
battery_draw = max(battery_draw - over_limit, 0)

if battery_draw == 0:
total_inverted = get_total_inverted(battery_draw, pv_dc, pv_ac, inverter_loss, self.inverter_hybrid)
total_inverted = get_total_inverted(battery_draw, pv_dc, pv_ac, inverter_loss, inverter_hybrid)
if total_inverted > inverter_limit:
over_limit = total_inverted - inverter_limit
battery_draw = max(-over_limit * inverter_loss, -charge_rate_now_curve * step, -battery_to_max, -pv_ac)
Expand All @@ -669,12 +678,12 @@ def run_prediction(self, charge_limit, charge_window, discharge_window, discharg
pv_ac = (pv_now - pv_dc) * inverter_loss_ac

# Clip solar
total_inverted = get_total_inverted(battery_draw, pv_dc, pv_ac, inverter_loss, self.inverter_hybrid)
total_inverted = get_total_inverted(battery_draw, pv_dc, pv_ac, inverter_loss, inverter_hybrid)
if total_inverted > inverter_limit:
over_limit = total_inverted - inverter_limit
pv_ac = max(pv_ac - over_limit * inverter_loss, 0)
else:
total_inverted = get_total_inverted(battery_draw, pv_dc, pv_ac, inverter_loss, self.inverter_hybrid)
total_inverted = get_total_inverted(battery_draw, pv_dc, pv_ac, inverter_loss, inverter_hybrid)
if total_inverted > inverter_limit:
over_limit = total_inverted - inverter_limit
if battery_draw > 0:
Expand Down
17 changes: 15 additions & 2 deletions apps/predbat/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ def run_perf_test(my_predbat):
failed = False

start_time = time.time()
for count in range(0, 200):
for count in range(0, 50):
failed |= simple_scenario(
"load_bat_dc_pv2",
my_predbat,
Expand All @@ -636,7 +636,7 @@ def run_perf_test(my_predbat):
print("Performance test failed")

run_time = end_time - start_time
print("Performance test took {} seconds for 200 iterations = {} iterations per second".format(run_time, round(1 / (run_time / 200.0), 2)))
print("Performance test took {} seconds for 200 iterations = {} iterations per second".format(run_time, round(1 / (run_time / 50.0), 2)))
return failed


Expand Down Expand Up @@ -961,6 +961,19 @@ def run_model_tests(my_predbat):
inverter_limit=2,
hybrid=True,
)
failed |= simple_scenario(
"battery_charge_pv_term_dc",
my_predbat,
0,
0.5,
assert_final_metric=import_rate * 10 * 0.5,
assert_final_soc=10 + 14 * 0.5,
with_battery=True,
charge=10,
battery_size=100,
hybrid=True,
assert_keep=import_rate / 60 * 5 * 0.5,
)
failed |= simple_scenario(
"battery_charge_pv_load1",
my_predbat,
Expand Down

0 comments on commit 74277f6

Please sign in to comment.