Skip to content
Snippets Groups Projects
Commit b1d730e1 authored by Bichen Li's avatar Bichen Li
Browse files

- Add simulink read-in function and convert function

- Add assert_modelica and validate_modelica function
- Remove some function from Validationtool
parent 6626eb4d
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import os import os
from dataprocessing.readtools import * from dataprocessing.readtools import *
def convert_neplan_to_modelica_timeseries(neplan_timeseries): def convert_neplan_to_modelica_timeseries(neplan_timeseries):
""" """
Mapping the variable names between modelica and neplan Mapping the variable names between modelica and neplan
...@@ -46,61 +47,71 @@ def convert_neplan_to_modelica_timeseries(neplan_timeseries): ...@@ -46,61 +47,71 @@ def convert_neplan_to_modelica_timeseries(neplan_timeseries):
return neplan_timeseries return neplan_timeseries
def convert_simulink_to_modelica_timeseries(simseri):
def compare_modelica_neplan(modelica_res, neplan_res): # compare the result file from NEPLAN and Modelica res = []
for check in range(len(simseri)):
if 'U AB:' in simseri[check].name:
simseri[check].name = simseri[check].name.replace('U AB:', '')
simseri[check].name = simseri[check].name.replace('Vrms', 'Vpp')
simseri[check].name = simseri[check].name.replace('VDegree', 'Vangle')
simseri[check].name = simseri[check].name.replace(' ', '')
simseri[check].name = simseri[check].name.replace('_', '')
if 'Vangle' in simseri[check].name:
simseri[check].values = (simseri[check].values - 30)/180 * cmath.pi
res.append(simseri[check])
return res
def compare_timeseries(ts1, ts2):
""" """
Compare Results from modelic and neplan, the name of the components should be kept consistent. Compare the result from two timeseries.
:param modelica_res: the path of the modelica result file, whose suffix should be .mat :param ts1: timeseries
:param neplan_res: the path of the neplan result file, whose suffix should be .rlf :param ts2: timeseries
:return: :return: an error dic
""" """
# Read in original neplan result file if len(ts1) > len(ts2):
file_Neplan = os.path.abspath(neplan_res) tmp = ts2
# Read in original Modelica result file ts2 = ts1
file_Modelica = os.path.abspath(modelica_res) ts1 = tmp
result_neplan = convert_neplan_to_modelica_timeseries(read_timeseries_NEPLAN_loadflow(file_Neplan)) for i in range(len(ts1)):
result_modelica = read_timeseries_Modelica(file_Modelica) ts1[i].name = ts1[i].name.upper()
for i in range(len(ts2)):
# Transfer the angle unit to degree ts2[i].name = ts2[i].name.upper()
for i in range(len(result_neplan)):
result_neplan[i].name = result_neplan[i].name.upper()
if 'ANGLE' in result_neplan[i].name:
result_neplan[i].values = result_neplan[i].values / cmath.pi * 180
for i in range(len(result_modelica)):
result_modelica[i].name = result_modelica[i].name.upper()
if 'ANGLE' in result_modelica[i].name:
result_modelica[i].values = result_modelica[i].values / cmath.pi * 180
timeseries_names = [] # list for names of components timeseries_names = [] # list for names of components
timeseries_error = [] # list for error timeseries_error = [] # list for error
len_limit = len(result_modelica) len_ts1 = len(ts1)
len_limit = len(ts2)
# Match the components in result files, and compare them # Match the components in result files, and compare them
for i in range(len(result_neplan)): for i in range(len_ts1):
flag_not_found = False flag_not_found = False
for j in range(len_limit): for j in range(len_limit):
if result_neplan[i].name == result_modelica[j].name: # Find the same variable if ts1[i].name == ts2[j].name: # Find the same variable
timeseries_names.append(result_neplan[i].name) timeseries_names.append(ts1[i].name)
timeseries_error.append(TimeSeries.rmse(result_modelica[j], result_neplan[i])) timeseries_error.append(TimeSeries.rmse(ts2[j], ts1[i])/ts1[i].values[1])
print(ts1[i].name)
print(TimeSeries.rmse(ts2[j], ts1[i])/ts1[i].values[1])
flag_not_found = True flag_not_found = True
if flag_not_found is False: if flag_not_found is False:
# No such variable in Modelica model, set the error to -1 # No such variable in Modelica model, set the error to -1
timeseries_names.append(ts1[i].name)
timeseries_error.append(-1) timeseries_error.append(-1)
return dict(zip(timeseries_names, timeseries_error)) return dict(zip(timeseries_names, timeseries_error))
def assert_modelia_neplan_results(net_name, modelica_res, neplan_res): # Assert the model using the function above
def assert_modelia_results(net_name, error):
""" """
Assert the result in Modelica according to the results from neplan assert the result data of a net.
:param net_name: The name of the net should be clarified manually :param net_name: name of the network
:param modelica_res: the path of the modelica result file, whose suffix should be .mat :param modelica_res: timeseries of modelica result
:param neplan_res: the path of the neplan result file, whose suffix should be .rlf :param simulink_res: timeseries of reference result
:return: :return: outputs to command line which are the results of the assert
""" """
fail_list = [] # List for all the failed test fail_list = [] # List for all the failed test
error = compare_modelica_neplan(modelica_res, neplan_res)
# the limitations are set to 0.5 # the limitations are set to 0.5
for name in error.keys(): for name in error.keys():
if abs(error[name]) > 0.5: if abs(error[name]) > 0.01:
fail_list.append(name) fail_list.append(name)
else: else:
print("Test on %s Passed" % name) print("Test on %s Passed" % name)
...@@ -114,20 +125,19 @@ def assert_modelia_neplan_results(net_name, modelica_res, neplan_res): # Assert ...@@ -114,20 +125,19 @@ def assert_modelia_neplan_results(net_name, modelica_res, neplan_res): # Assert
raise ValueError('Test on %s is not passed!' % net_name) raise ValueError('Test on %s is not passed!' % net_name)
def convert_simulink_to_modelica_timeseries(simseri): def validate_modelica_res(net_name, modelica_res_path, reference_res_path):
for check in range(len(simseri)): """
simseri[check].name = simseri[check].name.replace('U CA:', '') Top level function for the validation of modelica, calls all the function needed to execute the validation.
simseri[check].name = simseri[check].name.replace('Vrms', 'Vpp') :param modelica_res_path: the path of the modelica result file, whose suffix should be .mat
simseri[check].name = simseri[check].name.replace('VDegree', 'Vangle') :param reference_res_path: the path of the reference result file, whose suffix should be .rep(simulink)/.rlf(neplan)
simseri[check].name = simseri[check].name.replace(' ', '') :param reference_res_type: a flag to clarify where the reference come from, should be either Simulink or Neplan
:return: outputs to command line which are the results of the validation.
for check in range(len(simseri)): """
if 'Vpp' in simseri[check].name: res_mod = read_timeseries_Modelica (modelica_res_path)
simseri[check].values = simseri[check].values * 0.577350 if os.path.splitext(reference_res_path)[1] == '.rep':
res_ref = convert_simulink_to_modelica_timeseries(read_timeseries_simulink_loadflow(reference_res_path))
if 'Vangle' in simseri[check].name: elif os.path.splitext(reference_res_path)[1] == '.rlf':
simseri[check].values = simseri[check].values - 30 res_ref = convert_neplan_to_modelica_timeseries(read_timeseries_NEPLAN_loadflow(reference_res_path))
return simseri
res_err = compare_timeseries(res_ref, res_mod)
assert_modelia_results(net_name, res_err)
...@@ -31,9 +31,6 @@ def read_timeseries_Modelica(filename, timeseries_names=None, is_regex=False): ...@@ -31,9 +31,6 @@ def read_timeseries_Modelica(filename, timeseries_names=None, is_regex=False):
for name in timeseries_names: for name in timeseries_names:
timeseries.append(TimeSeries(name, sim(name).times(), sim(name).values())) timeseries.append(TimeSeries(name, sim(name).times(), sim(name).values()))
#print('Modelica results column names: ' + str(timeseries_names))
#print('Modelica results number: ' + str(len(timeseries_names)))
return timeseries return timeseries
...@@ -336,3 +333,5 @@ def read_timeseries_simulink_loadflow(file_name, timeseries_names=None, is_regex ...@@ -336,3 +333,5 @@ def read_timeseries_simulink_loadflow(file_name, timeseries_names=None, is_regex
del timeseries[line_del[len(line_del) - num_to_del - 1]] del timeseries[line_del[len(line_del) - num_to_del - 1]]
return timeseries return timeseries
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment