Commit 196ad411 authored by Artur Löwen | Gridhound's avatar Artur Löwen | Gridhound
Browse files

clean up + write pf output

parent a4991e78
......@@ -5,7 +5,6 @@ import csv
import logging
import time
from collections import defaultdict
from copy import copy
from pathlib import Path
from typing import List, Any, Dict, Tuple
......@@ -45,7 +44,7 @@ def get_res(all_results: List[Results], terminals_by_cond_equip_id: Dict[str, Li
return results_dict
def write_rows(filename: Path, rows: List[Any]) -> None:
def write_rows(filename: Path, data: Dict[str, List[float]]) -> None:
format_mapping = {
"VM1": lambda v: f"{v*1e3:>+13.1f}",
......@@ -56,9 +55,8 @@ def write_rows(filename: Path, rows: List[Any]) -> None:
with open(filename, "w+", newline="") as csvfile:
file_writer = csv.writer(csvfile, delimiter=',')
for row in rows:
out_row = copy(row)
out_row[1] = format_mapping[out_row[0][:3]](out_row[1])
for meas_id, values in data.items():
out_row = [meas_id, *[format_mapping[meas_id[:3]](val) for val in values]]
file_writer.writerow(out_row)
def get_diff(true_output: Dict[str, List[float]], se_output: Dict[str, List[float]]) -> Dict[str, List[float]]:
......@@ -158,17 +156,6 @@ def get_meas_input() -> Dict[str, List[float]]:
csv_to_dict(xml_path / "test_input.csv", meas_input)
# values taken from GridLab-D scenario load-flow simulation
# meas_input = {"SI1__82d69c70-dae2-11e9-9b03-8c1645e1ae40": [-998807.632616],
# "SI1__82d85100-dae2-11e9-9b03-8c1645e1ae40": [-739305.435083],
# "SI1__82d8f4d4-dae2-11e9-9b03-8c1645e1ae40": [ 5507.937033],
# "SR1__82d69c70-dae2-11e9-9b03-8c1645e1ae40": [2803175.225748],
# "SR1__82d85100-dae2-11e9-9b03-8c1645e1ae40": [3160922.936208],
# "SR1__82d8f4d4-dae2-11e9-9b03-8c1645e1ae40": [ -99249.192861],
# "VM1__829b5ec6-dae2-11e9-9b03-8c1645e1ae40": [ 395.454225],
# "VM1__829cb5a0-dae2-11e9-9b03-8c1645e1ae40": [ 19899.422574],
# }
return meas_input
def get_meas_output() -> Dict[str, List[float]]:
......@@ -184,9 +171,6 @@ def get_meas_output() -> Dict[str, List[float]]:
def create_pseudo_measurements(measurements_set: measurement.MeasurementSet, results_pf: Results, test_cast: int) -> None:
""" Write here the percent uncertainties of the measurements"""
# V_unc = 0
# I_unc = 0
# Sinj_unc = 0
S_unc = 0
Pmu_mag_unc = 0
Pmu_phase_unc = 0
......@@ -251,10 +235,7 @@ def create_actual_measurements(measurements_set: measurement.MeasurementSet, top
pass
def update_actual_measurements(measurements_set: measurement.MeasurementSet, topo: Dict[str, Any],
meas_input: Dict[str, List[float]], idx: int,
sys_branches: Dict[str, network.Branch], sys_nodes: Dict[str, network.Node],
pf_branches: Dict[str, ResultsBranch], pf_nodes: Dict[str, ResultsNode], test_case: int
) -> None:
meas_input: Dict[str, List[float]], idx: int, test_case: int) -> None:
# update given measurements with actual values
for meas_id, values in meas_input.items():
......@@ -263,8 +244,6 @@ def update_actual_measurements(measurements_set: measurement.MeasurementSet, top
if meas_id[0] == "S":
terminal = topo[meas_id[4:]]
# branch = sys_branches[terminal.ConductingEquipment.mRID]
# pf_branch = pf_branches[branch.uuid]
real = meas_id[1] == "R"
if terminal.sequenceNumber == 1:
......@@ -276,7 +255,6 @@ def update_actual_measurements(measurements_set: measurement.MeasurementSet, top
elif meas_id[0] == "V":
# pf_node = pf_nodes[meas_id[4:]]
meas_type = measurement.MeasType.V_mag if test_case in [1,2,3] else measurement.MeasType.Vpmu_mag
measurements_set.update_measurement(meas_id[4:], meas_type, val, False)
......@@ -336,7 +314,7 @@ def run_state_estimation(system: network.System, topo: Dict[str, Any], results_p
all_results = []
samples = len(next(iter(meas_input.values())))
for idx in range(samples):
update_actual_measurements(measurements_set, topo, meas_input, idx, sys_branches, sys_nodes, pf_branches, pf_nodes, test_case)
update_actual_measurements(measurements_set, topo, meas_input, idx, test_case)
# Perform state estimation
start = time.time()
......@@ -367,14 +345,14 @@ for terminal in topo.values():
# write powerflow results to file (per element)
pf_res = get_res([results_pf], terminals_by_cond_equip_id)
# write_rows(xml_path / "pf_out.csv", pf_res_rows)
write_rows(xml_path / "pf_out.csv", pf_res)
# --- State Estimation ---
test_cases = [
1, # pseudo: V_mag + given (if not yet added), update: given
2, # pseudo: given, update: given
3, # pseudo: V_mag + S_real + S_imag + given (if not yet added), update: given
#4, # pseudo: V_mag + V_ang + S_real + S_imag + given (if not yet added), update: given
#4, # pseudo: V_mag + V_ang + S_real + S_imag + given (if not yet added), update: given - doesn't converge
5, # pseudo: V_mag + V_ang + given (if not yet added), update: given
]
......@@ -386,11 +364,6 @@ for test_case in test_cases:
print(f"Running SE for test case {test_case}")
all_state_estimation_results = run_state_estimation(system, topo, results_pf, test_case)
# print node voltages
# print("state_estimation_results.voltages: ")
# for node in state_estimation_results.nodes:
# print('{}={}'.format(node.topology_node.uuid, node.voltage))
# write state estimation results to file (per element)
se_res = get_res(all_state_estimation_results, terminals_by_cond_equip_id)
# write_rows(xml_path / "se_out.csv", se_res)
......@@ -398,9 +371,6 @@ for test_case in test_cases:
diff = get_diff(true_output, se_res)
diff_by_test_case[test_case] = diff
# write diff between pf and se to file (per element)
# write_rows(xml_path / "pf_se_diff.csv", diff)
for test_case, diff in diff_by_test_case.items():
print(f"Test case {test_case}")
print_diff(diff)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment