import sys
import time
from power_grid_model import PowerGridModel, CalculationMethod
from power_grid_model.utils import json_deserialize
path = sys.argv[1] if len(sys.argv) > 1 else "grid_bench_input.json"
with open(path) as f:
raw = f.read()
dataset = json_deserialize(raw)
t0 = time.perf_counter()
model = PowerGridModel(dataset)
t1 = time.perf_counter()
print(f"model construction: {(t1 - t0) * 1e3:.3f} ms", file=sys.stderr)
model._calculate_power_flow( calculation_method=CalculationMethod.newton_raphson, symmetric=True, experimental_features="enabled"
)
n_node = len(dataset["node"])
times = []
for _ in range(5):
t0 = time.perf_counter()
result = model._calculate_power_flow( calculation_method=CalculationMethod.newton_raphson, symmetric=True, experimental_features="enabled"
)
t1 = time.perf_counter()
times.append(t1 - t0)
print(f"nodes={n_node}")
print(f"calculate_power_flow (warm, 5 runs, ms): {[f'{t * 1e3:.3f}' for t in times]}")
print(f"min={min(times) * 1e3:.3f}ms mean={sum(times) / len(times) * 1e3:.3f}ms")
u_pu = result["node"]["u_pu"]
print(f"sample u_pu[0:5] = {u_pu[:5]}")
print(f"u_pu min/max = {u_pu.min():.6f} / {u_pu.max():.6f}")
t0 = time.perf_counter()
model2 = PowerGridModel(dataset)
model2._calculate_power_flow( calculation_method=CalculationMethod.newton_raphson, symmetric=True, experimental_features="enabled"
)
t1 = time.perf_counter()
print(f"cold (construct+calc): {(t1 - t0) * 1e3:.3f} ms")