forked from springfall2008/batpred
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_kernel_parity.py
More file actions
1487 lines (1324 loc) · 72.1 KB
/
Copy pathtest_kernel_parity.py
File metadata and controls
1487 lines (1324 loc) · 72.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -----------------------------------------------------------------------------
# Predbat Home Battery System
# Copyright Trefor Southwell 2026 - All Rights Reserved
# This application maybe used for personal use only and not for commercial use
# -----------------------------------------------------------------------------
# fmt off
# pylint: disable=consider-using-f-string
# pylint: disable=line-too-long
"""Parity tests for the C++ prediction kernel.
Every scenario is run twice on identical inputs - once through the Python
engine (Prediction.run_prediction) and once through the C++ kernel - and the
full result tuples (including the per-step SoC prediction) must match to
within 1e-6. A deterministic edge-case table pins each kernel branch and a
seeded random sweep covers the wider configuration space.
If the kernel shared library is not present the test attempts to build it with
build_kernel.sh; if that fails the test is skipped with a loud notice, unless
PREDBAT_KERNEL_REQUIRED=1 is set (CI) in which case it fails.
"""
import array
import copy
import ctypes
import gc
import os
import random
import signal
import subprocess
import sys
import threading
import time
import prediction_kernel
from const import PV_SCENARIO_NOMINAL, PV_SCENARIO_PV10, PV_SCENARIO_PV90
from prediction import Prediction
from prediction_kernel import create_kernel_context, run_prediction_kernel, load_kernel
from utils import remove_intersecting_windows
from tests.test_infra import reset_inverter, reset_rates
from tests.test_model import run_model_tests
# Tolerance for scalar and SoC comparisons; the kernel targets bit-exact results
PARITY_TOLERANCE = 1e-6
RESULT_NAMES = [
"final_metric",
"import_kwh_battery",
"import_kwh_house",
"export_kwh",
"soc_min",
"final_soc",
"soc_min_minute",
"battery_cycle",
"metric_keep",
"final_iboost",
"final_carbon_g",
]
# Attributes mutated by the parity scenarios that reset_inverter/reset_rates do not restore;
# snapshotted before the tests and restored afterwards so later tests see a clean predbat
SCENARIO_STATE_ATTRS = [
"soc_max",
"soc_kw",
"reserve",
"best_soc_min",
"best_soc_keep",
"best_soc_keep_weight",
"battery_rate_max_charge",
"battery_rate_max_charge_dc",
"battery_rate_max_discharge",
"battery_rate_max_export",
"battery_rate_min",
"charge_rate_now",
"discharge_rate_now",
"battery_rate_max_scaling",
"battery_rate_max_scaling_discharge",
"charge_scaling10",
"battery_loss",
"battery_loss_discharge",
"inverter_hybrid",
"inverter_loss",
"inverter_freeze_export_discharge_rate",
"inverter_limit",
"export_limit",
"pv_ac_limit",
"inverter_can_charge_during_export",
"set_charge_freeze",
"set_reserve_enable",
"set_export_freeze",
"set_export_freeze_only",
"set_charge_window",
"set_export_window",
"set_discharge_during_charge",
"set_export_low_power",
"calculate_export_on_pv",
"battery_charge_power_curve",
"battery_discharge_power_curve",
"battery_temperature",
"battery_temperature_prediction",
"battery_temperature_charge_curve",
"battery_temperature_discharge_curve",
"rate_max",
"rate_import",
"rate_export",
"io_adjusted",
"all_active_keep",
"carbon_enable",
"carbon_intensity",
"carbon_today_sofar",
"num_cars",
"car_charging_loss",
"car_energy_reported_load",
"car_charging_from_battery",
"car_charging_soc",
"car_charging_limit",
"car_charging_slots",
"iboost_enable",
"iboost_solar",
"iboost_solar_excess",
"iboost_charging",
"iboost_gas",
"iboost_gas_export",
"iboost_prevent_discharge",
"iboost_on_export",
"iboost_max_energy",
"iboost_max_power",
"iboost_min_power",
"iboost_min_soc",
"iboost_rate_threshold",
"iboost_rate_threshold_export",
"iboost_gas_scale",
"iboost_today",
"rate_gas",
"iboost_plan",
"end_record",
]
def snapshot_scenario_state(my_predbat):
"""Deep-copy the predbat attributes the parity scenarios mutate"""
return {attr: copy.deepcopy(getattr(my_predbat, attr)) for attr in SCENARIO_STATE_ATTRS if hasattr(my_predbat, attr)}
def restore_scenario_state(my_predbat, state):
"""Restore attributes captured by snapshot_scenario_state"""
for attr, value in state.items():
setattr(my_predbat, attr, value)
def ensure_kernel_built():
"""Ensure a loadable kernel shared library is available, building one locally if necessary.
Existence alone isn't enough to skip building - a candidate binary (e.g. a checked-in
cross-built one) can be stale (parity revision mismatch) or corrupted - so this actually
attempts to load before deciding a build is needed. Returns True once a usable library
has been loaded, whether that was an existing candidate or a freshly built local one.
"""
prediction_kernel.KERNEL_LOAD_TRIED = False
prediction_kernel.KERNEL_LIB = None
if load_kernel(log=print):
return True
build_script = os.path.join(os.path.dirname(os.path.abspath(prediction_kernel.__file__)), "build_kernel.sh")
try:
result = subprocess.run(["bash", build_script], capture_output=True, text=True, timeout=120)
if result.returncode != 0:
print("Kernel build failed: {}".format(result.stderr))
return False
except (OSError, subprocess.SubprocessError) as error:
print("Kernel build failed: {}".format(error))
return False
prediction_kernel.KERNEL_LOAD_TRIED = False
prediction_kernel.KERNEL_LIB = None
return load_kernel(log=print) is not None
def make_windows(rng, minutes_now, forecast_minutes, count, align=5):
"""Generate a list of random windows within the forecast horizon"""
windows = []
for _ in range(count):
start = minutes_now + rng.randrange(0, forecast_minutes - 60, align)
length = rng.randrange(30, 8 * 60, align)
windows.append({"start": start, "end": min(start + length, minutes_now + forecast_minutes), "average": round(rng.uniform(0, 40), 2)})
return windows
def apply_random_scenario(my_predbat, rng):
"""Randomise my_predbat's battery/inverter/rate configuration for one parity scenario"""
soc_max = rng.choice([4.8, 9.5, 10.0, 19.0, 50.0, 100.0])
my_predbat.soc_max = soc_max
my_predbat.soc_kw = round(rng.uniform(0, soc_max), 2)
my_predbat.reserve = rng.choice([0.0, round(soc_max * 0.04, 2), round(rng.uniform(0, soc_max / 4), 2)])
my_predbat.best_soc_min = rng.choice([0.0, my_predbat.reserve])
my_predbat.best_soc_keep = rng.choice([0.0, round(rng.uniform(0, soc_max / 2), 2)])
my_predbat.best_soc_keep_weight = round(rng.uniform(0, 1), 2)
my_predbat.battery_rate_max_charge = rng.uniform(0.5, 5) / 60.0
my_predbat.battery_rate_max_charge_dc = my_predbat.battery_rate_max_charge * rng.choice([1.0, 1.0, 1.5, 2.0])
my_predbat.battery_rate_max_discharge = rng.uniform(0.5, 5) / 60.0
my_predbat.battery_rate_max_export = my_predbat.battery_rate_max_discharge * rng.choice([1.0, 1.0, 0.5])
my_predbat.battery_rate_min = rng.choice([0.0, 0.0, 0.05 / 60.0])
my_predbat.charge_rate_now = my_predbat.battery_rate_max_charge * rng.choice([1.0, 0.5])
my_predbat.discharge_rate_now = my_predbat.battery_rate_max_discharge * rng.choice([1.0, 0.5])
my_predbat.battery_rate_max_scaling = rng.choice([1.0, round(rng.uniform(0.9, 1.0), 3)])
my_predbat.battery_rate_max_scaling_discharge = rng.choice([1.0, round(rng.uniform(0.9, 1.0), 3)])
my_predbat.charge_scaling10 = rng.choice([1.0, round(rng.uniform(0.8, 1.0), 3)])
my_predbat.battery_loss = round(rng.uniform(0.9, 1.0), 3)
my_predbat.battery_loss_discharge = round(rng.uniform(0.9, 1.0), 3)
my_predbat.inverter_hybrid = rng.choice([True, False])
my_predbat.inverter_loss = round(rng.uniform(0.8, 1.0), 3)
my_predbat.inverter_limit = rng.uniform(1, 10) / 60.0
my_predbat.export_limit = rng.uniform(0, 10) / 60.0
my_predbat.pv_ac_limit = rng.choice([0.0, rng.uniform(1, 5) / 60.0])
my_predbat.inverter_can_charge_during_export = rng.choice([True, False])
my_predbat.set_charge_freeze = rng.choice([True, False])
my_predbat.set_reserve_enable = rng.choice([True, False])
my_predbat.set_export_freeze = rng.choice([True, False])
my_predbat.set_export_freeze_only = rng.choice([True, False, False, False])
# Exercise a non-zero value without consuming another RNG draw, preserving seeded scenarios.
my_predbat.inverter_freeze_export_discharge_rate = 240.0 if my_predbat.set_export_freeze else 0.0
my_predbat.set_charge_window = rng.choice([True, True, False])
my_predbat.set_export_window = rng.choice([True, True, False])
my_predbat.set_discharge_during_charge = rng.choice([True, False])
my_predbat.set_export_low_power = rng.choice([True, False])
my_predbat.calculate_export_on_pv = rng.choice([True, False])
# Random battery power curves (percent -> multiplier), sometimes empty
if rng.random() < 0.5:
my_predbat.battery_charge_power_curve = {percent: round(rng.uniform(0.05, 1.0), 2) for percent in range(rng.randint(90, 100), 101)}
else:
my_predbat.battery_charge_power_curve = {}
if rng.random() < 0.5:
my_predbat.battery_discharge_power_curve = {percent: round(rng.uniform(0.05, 1.0), 2) for percent in range(rng.randint(90, 100), 101)}
else:
my_predbat.battery_discharge_power_curve = {}
# Battery temperature model
my_predbat.battery_temperature = rng.choice([20, 15, 8, 3, 0, -5])
my_predbat.battery_temperature_prediction = {minute: max(my_predbat.battery_temperature - minute / (4 * 60.0), -10) for minute in range(0, my_predbat.forecast_minutes, 5)} if rng.random() < 0.5 else {}
# Random rates in 30 minute blocks, occasional negative export/import rates
for minute in range(0, my_predbat.forecast_minutes + my_predbat.minutes_now, 30):
import_rate = round(rng.uniform(-5, 45), 2)
export_rate = round(rng.uniform(0, 30), 2)
for offset in range(30):
my_predbat.rate_import[minute + offset] = import_rate
my_predbat.rate_export[minute + offset] = export_rate
my_predbat.rate_max = max(my_predbat.rate_import.values())
# Octopus intelligent adjusted slots and alerts
my_predbat.io_adjusted = {}
if rng.random() < 0.3:
start = my_predbat.minutes_now + rng.randrange(0, my_predbat.forecast_minutes - 60, 5)
for minute in range(start, start + 60):
my_predbat.io_adjusted[minute] = True
my_predbat.all_active_keep = {}
if rng.random() < 0.3:
start = my_predbat.minutes_now + rng.randrange(0, my_predbat.forecast_minutes - 60, 5)
for minute in range(start, start + 120):
my_predbat.all_active_keep[minute] = rng.choice([20, 50, 100])
# Carbon intensity
my_predbat.carbon_enable = rng.random() < 0.3
my_predbat.carbon_intensity = {minute: round(rng.uniform(0, 400), 1) for minute in range(0, my_predbat.forecast_minutes, 5)} if my_predbat.carbon_enable else {}
my_predbat.carbon_today_sofar = round(rng.uniform(0, 2000), 1) if my_predbat.carbon_enable else 0
# Cars
my_predbat.num_cars = rng.choice([0, 0, 0, 1, 1, 2])
my_predbat.car_charging_loss = round(rng.uniform(0.85, 1.0), 3)
my_predbat.car_energy_reported_load = rng.choice([True, False])
my_predbat.car_charging_from_battery = rng.choice([True, False])
for car_n in range(my_predbat.num_cars):
my_predbat.car_charging_soc[car_n] = round(rng.uniform(0, 30), 2)
my_predbat.car_charging_limit[car_n] = round(rng.uniform(20, 80), 2)
slots = []
for _ in range(rng.randint(0, 3)):
start = my_predbat.minutes_now + rng.randrange(0, my_predbat.forecast_minutes - 60, 30)
length = rng.randrange(30, 4 * 60, 30)
slots.append({"start": start, "end": start + length, "kwh": round(rng.uniform(1, 20), 2), "average": round(rng.uniform(5, 40), 2), "octopus": rng.choice([True, False])})
my_predbat.car_charging_slots[car_n] = slots
# iBoost
my_predbat.iboost_enable = rng.random() < 0.4
if my_predbat.iboost_enable:
my_predbat.iboost_solar = rng.choice([True, False])
my_predbat.iboost_solar_excess = rng.choice([True, False])
my_predbat.iboost_charging = rng.choice([True, False])
my_predbat.iboost_gas = rng.choice([True, False])
my_predbat.iboost_gas_export = rng.choice([True, False])
my_predbat.iboost_prevent_discharge = rng.choice([True, False])
my_predbat.iboost_on_export = rng.choice([True, False])
my_predbat.iboost_max_energy = round(rng.uniform(1, 20), 2)
my_predbat.iboost_max_power = rng.uniform(1, 4) / 60.0
my_predbat.iboost_min_power = rng.choice([0.0, rng.uniform(0, 1) / 60.0])
my_predbat.iboost_min_soc = rng.choice([0, rng.randint(0, 100)])
my_predbat.iboost_rate_threshold = rng.choice([9999, round(rng.uniform(0, 40), 2)])
my_predbat.iboost_rate_threshold_export = rng.choice([9999, round(rng.uniform(0, 30), 2)])
my_predbat.iboost_gas_scale = round(rng.uniform(0.5, 1.5), 2)
my_predbat.iboost_today = round(rng.uniform(0, 5), 2)
my_predbat.rate_gas = {minute: round(rng.uniform(2, 15), 2) for minute in range(0, my_predbat.forecast_minutes + my_predbat.minutes_now)} if rng.random() < 0.7 else {}
if rng.random() < 0.5:
plan = []
for _ in range(rng.randint(1, 3)):
start = my_predbat.minutes_now + rng.randrange(0, my_predbat.forecast_minutes - 60, 30)
length = rng.randrange(30, 3 * 60, 30)
plan.append({"start": start, "end": start + length, "kwh": round(rng.uniform(1, 10), 2)})
my_predbat.iboost_plan = plan
else:
my_predbat.iboost_plan = []
def make_step_data(my_predbat, rng=None, pv_kw=0.0, load_kw=0.0):
"""Build pv/load step dictionaries, random per slot when an rng is given"""
pv_step = {}
load_step = {}
pv10_step = {}
load10_step = {}
for minute in range(0, my_predbat.forecast_minutes, 5):
if rng:
pv = round(rng.uniform(0, 0.4), 3) if rng.random() < 0.7 else 0.0
load = round(rng.uniform(0, 0.5), 3)
pv_step[minute] = pv
load_step[minute] = load
pv10_step[minute] = round(pv * rng.uniform(0.3, 1.0), 3)
load10_step[minute] = round(load * rng.uniform(1.0, 1.2), 3)
else:
pv_step[minute] = pv_kw / 12.0
load_step[minute] = load_kw / 12.0
pv10_step[minute] = pv_kw / 24.0
load10_step[minute] = load_kw / 12.0
return pv_step, pv10_step, load_step, load10_step
def compare_results(name, python_result, kernel_result):
"""Compare the Python engine and kernel result tuples, returns True on failure"""
failed = False
for index, result_name in enumerate(RESULT_NAMES):
python_value = python_result[index]
kernel_value = kernel_result[index]
if abs(python_value - kernel_value) > PARITY_TOLERANCE:
print("ERROR: Scenario {} mismatch on {}: python {} kernel {}".format(name, result_name, python_value, kernel_value))
failed = True
python_soc = python_result[11]
kernel_soc = kernel_result[11]
if sorted(python_soc.keys()) != sorted(kernel_soc.keys()):
print("ERROR: Scenario {} predict_soc keys mismatch: python {} kernel {} entries".format(name, len(python_soc), len(kernel_soc)))
failed = True
else:
for minute in python_soc:
if abs(python_soc[minute] - kernel_soc[minute]) > PARITY_TOLERANCE:
print("ERROR: Scenario {} predict_soc mismatch at minute {}: python {} kernel {}".format(name, minute, python_soc[minute], kernel_soc[minute]))
failed = True
break
for index, item_name in [(12, "car_charging_soc_next"), (13, "iboost_next"), (14, "iboost_running"), (15, "iboost_running_solar"), (16, "iboost_running_full")]:
if python_result[index] != kernel_result[index]:
print("ERROR: Scenario {} mismatch on {}: python {} kernel {}".format(name, item_name, python_result[index], kernel_result[index]))
failed = True
return failed
def dual_run(name, my_predbat, pv_step, pv10_step, load_step, load10_step, charge_limit, charge_window, export_window, export_limits, pv_scenario, end_record, pv90_step=None, load90_step=None):
"""Run one scenario through both engines and compare, returns True on failure"""
# Python engine first (kernel disabled so run_prediction cannot dispatch)
my_predbat.prediction_kernel_enable = False
prediction = Prediction(my_predbat, pv_step, pv10_step, load_step, load10_step, pv90_step, load90_step)
python_result = prediction.run_prediction(charge_limit, charge_window, export_window, export_limits, pv_scenario, end_record, save=None, cache=False)
# Kernel run on the identical Prediction state
prediction.kernel_handle = create_kernel_context(prediction)
if not prediction.kernel_handle:
print("ERROR: Scenario {} kernel context creation failed".format(name))
return True
kernel_result = run_prediction_kernel(prediction, charge_limit, charge_window, export_window, export_limits, pv_scenario, end_record, 5, False)
if kernel_result is None:
print("ERROR: Scenario {} kernel run failed".format(name))
return True
failed = compare_results(name, python_result, kernel_result)
# Also check the run_prediction dispatch glue path picks the kernel and agrees
prediction.prediction_kernel_enable = True
dispatch_result = prediction.run_prediction(charge_limit, charge_window, export_window, export_limits, pv_scenario, end_record, save=None, cache=False)
failed |= compare_results(name + "_dispatch", kernel_result, dispatch_result)
return failed
def run_marshalling_tests():
"""Check the ctypes buffer helpers, returns True on failure.
double_array/int32_array build their buffers with from_buffer, which returns a view over an
array.array rather than a copy. If ctypes did not keep the backing object alive the kernel would
read freed memory - silently, and only sometimes - so that guarantee is asserted here rather than
assumed, along with the values surviving the round trip.
"""
print("**** Running kernel marshalling tests ****")
failed = False
# The typecode chosen for the backing array must match the ctypes element exactly. from_buffer
# only checks the buffer is large enough, so a wider backing type is accepted and then read as
# interleaved garbage - a silent corruption rather than an exception.
for name, typecode, ctype in (("DOUBLE_TYPECODE", prediction_kernel.DOUBLE_TYPECODE, ctypes.c_double), ("INT32_TYPECODE", prediction_kernel.INT32_TYPECODE, ctypes.c_int32)):
if typecode is not None and array.array(typecode).itemsize != ctypes.sizeof(ctype):
print("ERROR: {} is '{}' with itemsize {} but the ctypes element is {} bytes".format(name, typecode, array.array(typecode).itemsize, ctypes.sizeof(ctype)))
failed = True
for name, builder, values, typecode in (("double_array", prediction_kernel.double_array, [0.0, -1.5, 3.25, 1e6], prediction_kernel.DOUBLE_TYPECODE), ("int32_array", prediction_kernel.int32_array, [0, -7, 42, 100000], prediction_kernel.INT32_TYPECODE)):
# Build from a temporary so the source list/array is unreferenced by the time it is read
buffer = builder(list(values))
gc.collect()
got = [buffer[i] for i in range(len(values))]
if got != values:
print("ERROR: {} round trip expected {} but got {}".format(name, values, got))
failed = True
# Only the from_buffer path holds a view that needs its backing kept alive; the fallback
# copies the values, so it has nothing to retain and is safe without _objects. Truthiness
# rather than "is not None": an empty _objects would mean nothing is retained, which is just
# as unsafe as the attribute being absent.
if typecode is not None and not getattr(buffer, "_objects", None):
print("ERROR: {} did not retain its backing buffer - the kernel could read freed memory".format(name))
failed = True
empty = builder([])
if len(empty) != 0:
print("ERROR: {}([]) should be empty, got length {}".format(name, len(empty)))
failed = True
# pk_run_batch's binding used to be missing its trailing n_threads entry in argtypes. That bug is invisible
# to the batch parity test: ctypes silently accepts more call arguments than argtypes declares, and
# a garbage thread count only changes how pk_run_batch splits work across threads - the results are
# thread-count-independent by design. Only asserting the binding itself catches a regression here.
if prediction_kernel.KERNEL_HAS_BATCH:
expected_argtypes = (ctypes.c_int64, ctypes.POINTER(prediction_kernel.PkBatchJob), ctypes.c_int32, ctypes.POINTER(prediction_kernel.PkBatchResult), ctypes.c_int32)
actual_argtypes = tuple(prediction_kernel.KERNEL_LIB.pk_run_batch.argtypes)
if actual_argtypes != expected_argtypes:
print("ERROR: pk_run_batch.argtypes is {} but the C++ signature (handle, jobs, n_jobs, results, n_threads) needs {}".format(actual_argtypes, expected_argtypes))
failed = True
else:
print("SKIP: pk_run_batch argtypes check - kernel does not expose pk_run_batch")
if not failed:
print("PASS")
return failed
def run_edge_case_tests(my_predbat):
"""Deterministic scenarios pinning each kernel branch, returns True on failure"""
failed = False
minutes_now = my_predbat.minutes_now
forecast_minutes = my_predbat.forecast_minutes
full_window = [{"start": minutes_now, "end": minutes_now + forecast_minutes, "average": 10}]
half_window = [{"start": minutes_now, "end": minutes_now + forecast_minutes // 2, "average": 10}]
cases = [
# name, config overrides, charge_limit, charge_window, export_window, export_limits, pv_kw, load_kw, end_record
("idle", {}, [], [], [], [], 0, 0, forecast_minutes),
("load_only", {}, [], [], [], [], 0, 1.0, forecast_minutes),
("pv_only", {}, [], [], [], [], 2.0, 0, forecast_minutes),
("pv_load_battery", {"soc_kw": 50.0}, [], [], [], [], 2.0, 1.0, forecast_minutes),
("charge_full", {}, [100.0], full_window, [], [], 0, 0.5, forecast_minutes),
("charge_freeze", {"soc_kw": 50.0, "reserve": 4.0, "set_charge_freeze": True}, [4.0], full_window, [], [], 1.0, 0.5, forecast_minutes),
("charge_hybrid_pv", {"inverter_hybrid": True, "battery_rate_max_charge_dc": 2 / 60.0}, [100.0], full_window, [], [], 3.0, 0.5, forecast_minutes),
("export_full", {"soc_kw": 100.0}, [], [], half_window, [0.0], 0, 0.5, forecast_minutes),
("export_freeze", {"soc_kw": 100.0, "set_export_freeze": True}, [], [], half_window, [99.0], 1.0, 0.5, forecast_minutes),
("export_low_power", {"soc_kw": 100.0, "set_export_low_power": True}, [], [], half_window, [49.5], 0, 0.5, forecast_minutes),
("export_limited", {"soc_kw": 100.0, "export_limit": 0.5 / 60.0, "battery_rate_max_export": 2 / 60.0}, [], [], half_window, [0.0], 2.0, 0.2, forecast_minutes),
("export_no_charge_during", {"soc_kw": 100.0, "inverter_can_charge_during_export": False, "export_limit": 0.5 / 60.0}, [], [], half_window, [0.0], 3.0, 0.2, forecast_minutes),
("intersecting_windows", {"soc_kw": 50.0}, [100.0], full_window, half_window, [0.0], 1.0, 0.5, forecast_minutes),
("pv_ac_limit_clip", {"pv_ac_limit": 1 / 60.0}, [], [], [], [], 3.0, 0.2, forecast_minutes),
("small_inverter", {"soc_kw": 50.0, "inverter_limit": 0.5 / 60.0}, [], [], [], [], 2.0, 2.0, forecast_minutes),
("keep_metric", {"best_soc_keep": 10.0, "best_soc_keep_weight": 0.5, "soc_kw": 5.0}, [], [], [], [], 0, 1.0, forecast_minutes),
("end_record_zero", {"soc_kw": 20.0}, [], [], [], [], 1.0, 1.0, 0),
("end_record_half", {"soc_kw": 20.0}, [], [], [], [], 1.0, 1.0, forecast_minutes // 2),
("misaligned_window", {"soc_kw": 50.0}, [100.0], [{"start": minutes_now + 3, "end": minutes_now + 63, "average": 10}], [], [], 0, 0.5, forecast_minutes),
("no_discharge_during_charge", {"soc_kw": 50.0, "set_discharge_during_charge": False}, [50.0], half_window, [], [], 0, 1.0, forecast_minutes),
("cold_battery", {"battery_temperature": 2, "soc_kw": 20.0}, [100.0], half_window, [], [], 1.0, 1.0, forecast_minutes),
("carbon", {"carbon_enable": True, "carbon_intensity": {minute: 100 + (minute % 60) for minute in range(0, forecast_minutes, 5)}, "carbon_today_sofar": 500.0, "soc_kw": 20.0}, [], [], [], [], 1.0, 1.0, forecast_minutes),
(
"car_charging",
{
"num_cars": 1,
"car_charging_slots": [[{"start": minutes_now + 60, "end": minutes_now + 240, "kwh": 21.0, "average": 30, "octopus": True}], [], [], []],
"car_charging_soc": [10, 0, 0, 0],
"car_charging_limit": [50, 100, 100, 100],
"car_charging_loss": 0.9,
"soc_kw": 30.0,
},
[],
[],
[],
[],
0,
0.5,
forecast_minutes,
),
(
"car_not_reported",
{"num_cars": 1, "car_energy_reported_load": False, "car_charging_slots": [[{"start": minutes_now, "end": minutes_now + 300, "kwh": 15.0, "average": 0, "octopus": False}], [], [], []], "car_charging_soc": [0, 0, 0, 0], "soc_kw": 50.0},
[],
[],
[],
[],
2.0,
0.5,
forecast_minutes,
),
(
"car_no_charge_from_battery",
{"num_cars": 1, "car_charging_from_battery": False, "car_charging_slots": [[{"start": minutes_now, "end": minutes_now + 300, "kwh": 15.0, "average": 0, "octopus": False}], [], [], []], "car_charging_soc": [0, 0, 0, 0], "soc_kw": 50.0},
[],
[],
[],
[],
0,
0.5,
forecast_minutes,
),
(
"iboost_charging",
{
"iboost_enable": True,
"iboost_charging": True,
"iboost_max_energy": 5.0,
"iboost_max_power": 3.0 / 60.0,
"iboost_min_power": 0.0,
"iboost_min_soc": 0,
"iboost_rate_threshold": 9999,
"iboost_rate_threshold_export": 9999,
"iboost_today": 0.5,
"iboost_plan": [],
},
[100.0],
half_window,
[],
[],
0,
0.5,
forecast_minutes,
),
(
"iboost_solar",
{
"iboost_enable": True,
"iboost_solar": True,
"iboost_solar_excess": False,
"iboost_max_energy": 8.0,
"iboost_max_power": 2.0 / 60.0,
"iboost_min_power": 0.1 / 60.0,
"iboost_min_soc": 10,
"iboost_rate_threshold": 9999,
"iboost_rate_threshold_export": 9999,
"iboost_today": 0.0,
"iboost_plan": [],
"soc_kw": 50.0,
},
[],
[],
[],
[],
3.0,
0.5,
forecast_minutes,
),
(
"iboost_solar_excess",
{
"iboost_enable": True,
"iboost_solar": True,
"iboost_solar_excess": True,
"iboost_max_energy": 8.0,
"iboost_max_power": 2.0 / 60.0,
"iboost_min_power": 0.1 / 60.0,
"iboost_min_soc": 0,
"iboost_rate_threshold": 9999,
"iboost_rate_threshold_export": 9999,
"iboost_today": 0.0,
"iboost_plan": [],
"soc_kw": 100.0,
},
[],
[],
[],
[],
3.0,
0.2,
forecast_minutes,
),
(
"iboost_gas",
{
"iboost_enable": True,
"iboost_charging": True,
"iboost_gas": True,
"iboost_gas_scale": 1.1,
"rate_gas": {minute: 7.0 for minute in range(forecast_minutes + minutes_now)},
"iboost_max_energy": 5.0,
"iboost_max_power": 3.0 / 60.0,
"iboost_min_power": 0.0,
"iboost_min_soc": 0,
"iboost_rate_threshold": 9999,
"iboost_rate_threshold_export": 9999,
"iboost_today": 0.0,
"iboost_plan": [],
},
[100.0],
half_window,
[],
[],
0,
0.5,
forecast_minutes,
),
(
"iboost_plan",
{
"iboost_enable": True,
"iboost_plan": [{"start": minutes_now + 60, "end": minutes_now + 180, "kwh": 4.0}],
"iboost_max_energy": 5.0,
"iboost_max_power": 3.0 / 60.0,
"iboost_min_power": 0.0,
"iboost_min_soc": 0,
"iboost_rate_threshold": 9999,
"iboost_rate_threshold_export": 9999,
"iboost_today": 0.0,
},
[],
[],
[],
[],
0,
0.5,
forecast_minutes,
),
]
for name, overrides, charge_limit, charge_window, export_window, export_limits, pv_kw, load_kw, end_record in cases:
reset_inverter(my_predbat)
reset_rates(my_predbat, 10.0, 5.0)
my_predbat.battery_rate_max_export = my_predbat.battery_rate_max_discharge
for key, value in overrides.items():
setattr(my_predbat, key, value)
pv_step, pv10_step, load_step, load10_step = make_step_data(my_predbat, pv_kw=pv_kw, load_kw=load_kw)
for pv_scenario in (PV_SCENARIO_NOMINAL, PV_SCENARIO_PV10):
failed |= dual_run(
"{}_scenario_{}".format(name, pv_scenario),
my_predbat,
pv_step,
pv10_step,
load_step,
load10_step,
charge_limit[:],
[dict(window) for window in charge_window],
[dict(window) for window in export_window],
export_limits[:],
pv_scenario,
end_record,
)
# pv90: the kernel must select the p90 arrays, skip the pv10 charge de-rate, and skip the
# io_adjusted worst-case import rate. Distinct series per scenario so a wrong selection shows up.
#
# Two profiles are needed because the three pv90-specific behaviours are not all observable in one:
# - "charge" is PV-rich with a charge window, so it pins the array selection (export volume differs
# per scenario) and the pv10 charge de-rate (final_soc differs), but every scenario ends up
# exporting, which makes import_rate - and therefore the io_adjusted substitution - unobservable.
# - "import" is load-dominated with an empty battery, so all three scenarios import and the
# io_adjusted worst-case rate substitution moves the metric. load90 must stay above pv90 here
# (load_kw > 4 * pv_kw, given the *0.5 / *2.0 p90 derivation below) or pv90 would export too.
pv90_cases = [
# name, pv_kw, load_kw, charge_limit, charge_window
("pv90_charge", 2.0, 0.5, [100.0], [{"start": minutes_now, "end": minutes_now + 120, "average": 5.0}]),
("pv90_import", 0.5, 3.0, [], []),
]
for case_name, pv_kw, load_kw, charge_limit, charge_window in pv90_cases:
reset_inverter(my_predbat)
reset_rates(my_predbat, 10.0, 5.0)
my_predbat.battery_rate_max_export = my_predbat.battery_rate_max_discharge
pv_step, pv10_step, load_step, load10_step = make_step_data(my_predbat, pv_kw=pv_kw, load_kw=load_kw)
pv90_step = {minute: value * 2.0 for minute, value in pv_step.items()}
load90_step = {minute: value * 0.5 for minute, value in load_step.items()}
my_predbat.charge_scaling10 = 0.5
my_predbat.io_adjusted = {minute: 1 for minute in range(0, my_predbat.forecast_minutes + my_predbat.minutes_now)}
# reset_rates leaves rate_max equal to the flat import rate, which would make the pv10 worst-case
# substitution (import_rate = rate_max) a no-op and hide a kernel that wrongly applied it to pv90
my_predbat.rate_max = 50.0
for scenario in (PV_SCENARIO_NOMINAL, PV_SCENARIO_PV10, PV_SCENARIO_PV90):
failed |= dual_run(
"{}_scenario_{}".format(case_name, scenario),
my_predbat,
pv_step,
pv10_step,
load_step,
load10_step,
charge_limit[:],
[dict(window) for window in charge_window],
[],
[],
scenario,
my_predbat.forecast_minutes,
pv90_step=pv90_step,
load90_step=load90_step,
)
my_predbat.io_adjusted = {}
return failed
def run_random_sweep_tests(my_predbat, count=150):
"""Seeded random configuration sweep comparing both engines, returns True on failure.
Every seed is run against all three PV scenarios. Drawing one scenario per seed instead would
trade away coverage of nominal and pv10 - the two scenarios every user runs at the default
pv_metric90_weight of 0 - to buy coverage of pv90; running all three is strictly additive and
the sweep is fast enough to absorb the 3x.
"""
failed = False
scenario_counts = {PV_SCENARIO_NOMINAL: 0, PV_SCENARIO_PV10: 0, PV_SCENARIO_PV90: 0}
for seed in range(count):
rng = random.Random(seed)
reset_inverter(my_predbat)
reset_rates(my_predbat, 10.0, 5.0)
my_predbat.battery_rate_max_export = my_predbat.battery_rate_max_discharge
apply_random_scenario(my_predbat, rng)
pv_step, pv10_step, load_step, load10_step = make_step_data(my_predbat, rng=rng)
# p90 series derived from a separate generator so the main rng stream - and therefore every
# pre-existing random scenario - is unchanged by the addition of the pv90 case
rng90 = random.Random(seed + 1000000)
pv90_step = {minute: round(value * rng90.uniform(1.0, 2.0), 3) for minute, value in pv_step.items()}
load90_step = {minute: round(value * rng90.uniform(0.5, 1.0), 3) for minute, value in load_step.items()}
charge_window = make_windows(rng, my_predbat.minutes_now, my_predbat.forecast_minutes, rng.randint(0, 3), align=rng.choice([5, 5, 30, 3]))
charge_limit = [rng.choice([0.0, my_predbat.reserve, my_predbat.soc_max, round(rng.uniform(0, my_predbat.soc_max), 2)]) for _ in charge_window]
export_window = make_windows(rng, my_predbat.minutes_now, my_predbat.forecast_minutes, rng.randint(0, 3), align=rng.choice([5, 5, 30]))
export_limits = [rng.choice([100.0, 99.0, 0.0, round(rng.uniform(0, 100), 1)]) for _ in export_window]
end_record = rng.choice([my_predbat.forecast_minutes, my_predbat.forecast_minutes - 30, rng.randrange(0, my_predbat.forecast_minutes, 5)])
# No scenario is drawn from rng here: the draw that used to sit at this position was the last
# use of rng in the loop body, so looping the scenarios instead leaves every previously
# generated configuration (windows, limits, end_record, step data) bit-for-bit unchanged.
for pv_scenario in (PV_SCENARIO_NOMINAL, PV_SCENARIO_PV10, PV_SCENARIO_PV90):
scenario_counts[pv_scenario] += 1
failed |= dual_run(
"random_{}_s{}".format(seed, pv_scenario), my_predbat, pv_step, pv10_step, load_step, load10_step, charge_limit, charge_window, export_window, export_limits, pv_scenario, end_record, pv90_step=pv90_step, load90_step=load90_step
)
if failed:
print("Random sweep failed at seed {} scenario {}".format(seed, pv_scenario))
break
if failed:
break
print("Random sweep ran {} configurations: nominal {}, pv10 {}, pv90 {}".format(sum(scenario_counts.values()), scenario_counts[PV_SCENARIO_NOMINAL], scenario_counts[PV_SCENARIO_PV10], scenario_counts[PV_SCENARIO_PV90]))
return failed
def make_intersecting_windows(rng, minutes_now, forecast_minutes):
"""Build a charge/export layout that deliberately exercises window clipping.
The generic sweep places a handful of windows at random, so an export window landing strictly
inside a charge window - the split case, and the subtlest part of the clipping - almost never
comes up. Here export windows are positioned relative to the charge windows on purpose: covering
them entirely, overlapping either end, sitting inside them, and touching exactly at a boundary.
Short windows and short remnants are included because the 5 minute minimum only applies to
remnants clipping itself created, never to a window that was left alone.
"""
charge_window = []
minute = minutes_now
for _ in range(rng.randint(1, 5)):
length = rng.choice([5, 10, 30, 60, 120, 240])
end = min(minute + length, minutes_now + forecast_minutes)
if end <= minute:
break
charge_window.append({"start": minute, "end": end, "average": round(rng.uniform(0, 40), 2)})
minute = end + rng.choice([0, 0, 5, 30])
if minute >= minutes_now + forecast_minutes:
break
export_window = []
for window in charge_window:
mode = rng.choice(["inside", "inside", "overlap_start", "overlap_end", "cover", "touch_start", "touch_end", "clear", "tiny_remnant"])
start, end = window["start"], window["end"]
span = end - start
if mode == "inside" and span >= 20:
dstart = start + rng.randrange(5, max(span - 10, 6), 5)
dend = min(dstart + rng.choice([5, 10, 30]), end - 1)
if dend <= dstart:
continue
elif mode == "overlap_start":
dstart, dend = max(start - rng.choice([5, 30]), minutes_now), start + max(span // 3, 5)
elif mode == "overlap_end":
dstart, dend = end - max(span // 3, 5), end + rng.choice([5, 30])
elif mode == "cover":
dstart, dend = start, end
elif mode == "touch_start":
dstart, dend = max(start - 30, minutes_now), start
elif mode == "touch_end":
dstart, dend = end, end + 30
elif mode == "tiny_remnant" and span >= 10:
# Leave only a couple of minutes at the end, below the 5 minute minimum
dstart, dend = start, end - rng.choice([1, 2, 3])
else:
dstart, dend = end + 60, end + 90
dstart = max(min(dstart, minutes_now + forecast_minutes), minutes_now)
dend = max(min(dend, minutes_now + forecast_minutes), dstart)
if dend > dstart:
export_window.append({"start": dstart, "end": dend, "average": round(rng.uniform(0, 40), 2)})
export_window.sort(key=lambda w: w["start"])
return charge_window, export_window
def run_clipping_parity_tests(my_predbat, count=250):
"""Compare both engines on layouts built to exercise window clipping, returns True on failure.
The kernel clips intersecting charge windows itself (clip_intersecting_charge_windows in
prediction_kernel.cpp) rather than having Python do it first, so this is the sweep that pins
those two implementations together.
"""
failed = False
split_layouts = 0
for seed in range(count):
rng = random.Random(500000 + seed)
reset_inverter(my_predbat)
reset_rates(my_predbat, 10.0, 5.0)
my_predbat.battery_rate_max_export = my_predbat.battery_rate_max_discharge
apply_random_scenario(my_predbat, rng)
pv_step, pv10_step, load_step, load10_step = make_step_data(my_predbat, rng=rng)
charge_window, export_window = make_intersecting_windows(rng, my_predbat.minutes_now, my_predbat.forecast_minutes)
charge_limit = [rng.choice([0.0, my_predbat.reserve, my_predbat.soc_max, round(rng.uniform(0, my_predbat.soc_max), 2)]) for _ in charge_window]
export_limits = [rng.choice([100.0, 99.0, 0.0, round(rng.uniform(0, 100), 1)]) for _ in export_window]
end_record = rng.choice([my_predbat.forecast_minutes, my_predbat.forecast_minutes - 30])
# Count the layouts that actually split a charge window, so a generator that stopped
# producing them would show up rather than silently weakening this sweep
clipped_limits, clipped_windows = remove_intersecting_windows(charge_limit, charge_window, export_limits, export_window)
if len(clipped_windows) > len(charge_window):
split_layouts += 1
for pv_scenario in (PV_SCENARIO_NOMINAL, PV_SCENARIO_PV10):
failed |= dual_run(
"clip_{}_s{}".format(seed, pv_scenario),
my_predbat,
pv_step,
pv10_step,
load_step,
load10_step,
charge_limit,
charge_window,
export_window,
export_limits,
pv_scenario,
end_record,
)
if failed:
print("Clipping sweep failed at seed {} scenario {}".format(seed, pv_scenario))
print(" charge {} limits {}".format([(w["start"], w["end"]) for w in charge_window], charge_limit))
print(" export {} limits {}".format([(w["start"], w["end"]) for w in export_window], export_limits))
break
if failed:
break
print("Clipping sweep ran {} layouts, {} of which split a charge window".format(count, split_layouts))
if split_layouts == 0:
print("ERROR: clipping sweep generated no window splits - the sweep is not exercising the split path")
failed = True
return failed
def build_batch_jobs(my_predbat, rng, count, window_variants=1):
"""Build a list of BatchJob scenarios plus the Prediction they run against.
window_variants controls how many distinct charge/export window lists the batch draws from, cycled
by job index (job N uses variant N % window_variants). The default of 1 shares a single list across
every job, as the real min/max fan-out does when it scans one window at a time. A caller wanting
per-job window lists - to exercise run_prediction_kernel_batch's identity-keyed window memo, whose
cache key is id(windows), not its content - passes a larger value.
"""
apply_random_scenario(my_predbat, rng)
pv_step, pv10_step, load_step, load10_step = make_step_data(my_predbat, rng)
my_predbat.prediction_kernel_enable = True
prediction = Prediction(my_predbat, pv_step, pv10_step, load_step, load10_step)
prediction.kernel_handle = create_kernel_context(prediction)
charge_windows = [make_windows(rng, my_predbat.minutes_now, my_predbat.forecast_minutes, rng.randint(2, 5)) for _ in range(window_variants)]
export_windows = [make_windows(rng, my_predbat.minutes_now, my_predbat.forecast_minutes, rng.randint(2, 5)) for _ in range(window_variants)]
end_record = my_predbat.forecast_minutes
jobs = []
for index in range(count):
charge_window = charge_windows[index % window_variants]
export_window = export_windows[index % window_variants]
charge_limit = [round(rng.uniform(0, my_predbat.soc_max), 2) for _ in charge_window]
export_limits = [rng.choice([100.0, 99.0, 0.0, round(rng.uniform(1, 99), 1)]) for _ in export_window]
pv_scenario = rng.choice([PV_SCENARIO_NOMINAL, PV_SCENARIO_PV10, PV_SCENARIO_PV90])
# Every third job asks for the SoC range over a charge window, as the min/max fan-out does
range_window = charge_window[index % len(charge_window)] if (index % 3) == 0 else None
start_step = -1
end_step = -1
if range_window is not None:
start_step = max(int((range_window["start"] - my_predbat.minutes_now) / 5) * 5, 0) // 5
end_step = int((range_window["end"] - my_predbat.minutes_now) / 5) * 5 // 5
jobs.append(
prediction_kernel.BatchJob(
charge_limit,
charge_window,
export_window,
export_limits,
pv_scenario,
end_record,
5,
soc_range_start_step=start_step,
soc_range_end_step=end_step,
want_range=range_window is not None,
range_window=range_window,
)
)
return prediction, jobs
def build_batch_reference(prediction, jobs):
"""Run one pk_run per job to build the reference results run_prediction_kernel_batch is checked against.
The SoC series is materialised (unlike the batch path) so the range can be scanned in Python
exactly as thread_run_prediction_charge_min_max does. Returns None if a reference run itself fails.
"""
reference = []
for job in jobs:
single = run_prediction_kernel(prediction, job.charge_limit, job.charge_window, job.export_window, job.export_limits, job.pv_scenario, job.end_record, 5, False)
if single is None:
print("ERROR: reference pk_run failed")
return None
if job.range_window is None:
soc_range = (prediction.soc_max, 0)
else:
soc_range = prediction.scan_soc_range(single[11], job.range_window)
reference.append((single, soc_range))
return reference
def check_batch_results(jobs, reference, batched, n_threads):
"""Compare one pk_run_batch call's results against the build_batch_reference() reference, returns True on failure"""
if len(batched) != len(jobs):
print("ERROR: pk_run_batch returned {} results for {} jobs".format(len(batched), len(jobs)))
return True
failed = False
for index, (result_tuple, soc_range_min, soc_range_max) in enumerate(batched):
single, (expect_min, expect_max) = reference[index]
if result_tuple is None:
print("ERROR: job {} reported a non-zero status at {} threads".format(index, n_threads))
failed = True
continue
for field, name in enumerate(RESULT_NAMES):
if result_tuple[field] != single[field]:
print("ERROR: job {} at {} threads differs on {}: batch {} single {}".format(index, n_threads, name, result_tuple[field], single[field]))
failed = True
# The batch never materialises the SoC series - that is what makes it affordable. Checked
# against the single path rather than against an empty dict on its own, so this fails both
# if the batch starts filling the series and if the reference stopped filling it (which
# would make the batch's emptiness prove nothing).
if result_tuple[11] or not single[11]:
print("ERROR: job {} SoC series: batch has {} entries, single reference has {}".format(index, len(result_tuple[11]), len(single[11])))
failed = True
# car_charging_soc_next is recorded at minute 0, before any trial limit can move it, so it is
# the same value for every job in the fan-out - comparing it job by job pins that the batch
# assembles it like the single path does, but cannot catch a mis-routed result. That the
# kernel fills it at all is asserted once per run in run_batch_parity_tests.
for field, name in [(12, "car_charging_soc_next"), (13, "iboost_next"), (14, "iboost_running"), (15, "iboost_running_solar"), (16, "iboost_running_full")]:
if result_tuple[field] != single[field]:
print("ERROR: job {} at {} threads differs on {}: batch {} single {}".format(index, n_threads, name, result_tuple[field], single[field]))
failed = True
if soc_range_min != expect_min or soc_range_max != expect_max:
print("ERROR: job {} at {} threads SoC range {} {} expected {} {}".format(index, n_threads, soc_range_min, soc_range_max, expect_min, expect_max))
failed = True
return failed
def run_batch_parity_tests(my_predbat, count=60):
"""Check pk_run_batch matches a loop of pk_run exactly, at every thread count, returns True on failure.
The batch path is the only one Python will use after this refactor, and it is the only one that
can run scenarios concurrently, so it is checked against the single-scenario path it replaces
rather than against the Python engine (which run_random_sweep_tests already pins pk_run against).
Results must be bit-identical, not merely close: the whole point of the fan-out is that plans do
not change.
"""
print("**** Running kernel batch parity tests ****")
if not prediction_kernel.KERNEL_HAS_BATCH:
print("SKIP: kernel does not expose pk_run_batch")
return False
prediction, jobs = build_batch_jobs(my_predbat, random.Random(4321), count)
if not prediction.kernel_handle:
print("ERROR: batch parity kernel context creation failed")
return True
reference = build_batch_reference(prediction, jobs)