def run(plot=False): '''Run test case. Parameters ---------- plot : bool, optional True to plot timeseries results. Default is False. Returns ------- kpi : dict Dictionary of KPI names and values. {kpi_name : value} res : dict Dictionary of trajectories of inputs and outputs. ''' # SETUP TEST CASE # --------------- # Set URL for testcase url = 'http://127.0.0.1:5000' # Set simulation parameters length = 48*3600 step = 300 # --------------- # GET TEST INFORMATION # -------------------- print('\nTEST CASE INFORMATION\n---------------------') # Test case name name = requests.get('{0}/name'.format(url)).json() print('Name:\t\t\t\t{0}'.format(name)) # Inputs available inputs = requests.get('{0}/inputs'.format(url)).json() print('Control Inputs:\t\t\t{0}'.format(inputs)) # Measurements available measurements = requests.get('{0}/measurements'.format(url)).json() print('Measurements:\t\t\t{0}'.format(measurements)) # Default simulation step step_def = requests.get('{0}/step'.format(url)).json() print('Default Simulation Step:\t{0}'.format(step_def)) # -------------------- # RUN TEST CASE # ------------- # Reset test case print('Resetting test case if needed.') res = requests.put('{0}/reset'.format(url)) # Set simulation step print('Setting simulation step to {0}.'.format(step)) res = requests.put('{0}/step'.format(url), data={'step':step}) print('\nRunning test case...') # Initialize u u = pid.initialize() # Simulation Loop for i in range(int(length/step)): # Advance simulation y = requests.post('{0}/advance'.format(url), data=u).json() # Compute next control signal u = pid.compute_control(y) print('\nTest case complete.') # ------------- # VIEW RESULTS # ------------ # Report KPIs kpi = requests.get('{0}/kpi'.format(url)).json() print('\nKPI RESULTS \n-----------') for key in kpi.keys(): if key == 'tdis_tot': unit = 'Kh' elif key == 'ener_tot': unit = 'kWh' elif key == 'cost_tot': unit = 'euro or $' elif key == 'emis_tot': unit = 'kg CO2' elif key == 'time_rat': unit = '' print('{0}: {1} {2}'.format(key, kpi[key], unit)) # ------------ # POST PROCESS RESULTS # -------------------- # Get result data res = requests.get('{0}/results'.format(url)).json() time = [x/3600 for x in res['y']['time']] # convert s --> hr TZone = [x-273.15 for x in res['y']['TRooAir_y']] # convert K --> C PHeat = res['y']['PHea_y'] QHeat = res['u']['oveAct_u'] # Plot results if plot: from matplotlib import pyplot as plt plt.figure(1) plt.title('Zone Temperature') plt.plot(time, TZone) plt.plot(time, 20*np.ones(len(time)), '--') plt.plot(time, 23*np.ones(len(time)), '--') plt.ylabel('Temperature [C]') plt.xlabel('Time [hr]') plt.figure(2) plt.title('Heater Power') plt.plot(time, PHeat) plt.ylabel('Electrical Power [W]') plt.xlabel('Time [hr]') plt.show() # -------------------- return kpi, res
import os thispath = os.path.dirname(os.path.abspath(__file__)) sys.path.append(thispath + '/../') import boptest import time sys.path.insert(0, './controllers') from controllers import pid bop = boptest.Boptest(url='http://localhost') length = 48*3600 step = 300 u = pid.initialize() site = bop.submit(thispath + '/simple_1_zone_heating.fmu') bop.start(site, external_clock = "true") for i in range(int(length/step)): bop.setInputs(site, u) bop.advance([site]) y = bop.outputs(site) print(u) print(y) sys.stdout.flush() u = pid.compute_control(y) bop.stop(site)
def run(plot=False, customized_kpi_config=None): '''Run test case. Parameters ---------- plot : bool, optional True to plot timeseries results. Default is False. customized_kpi_config : string, optional The path of the json file which contains the customized kpi information. Default is None. Returns ------- kpi : dict Dictionary of core KPI names and values. {kpi_name : value} res : dict Dictionary of trajectories of inputs and outputs. customizedkpis_result: dict Dictionary of tracked custom KPI calculations. Empty if no customized KPI calculations defined. ''' # SETUP TEST CASE # --------------- # Set URL for testcase url = 'http://127.0.0.1:5000' # Set simulation parameters length = 48 * 3600 step = 300 # --------------- # GET TEST INFORMATION # -------------------- print('\nTEST CASE INFORMATION\n---------------------') # Test case name name = requests.get('{0}/name'.format(url)).json() print('Name:\t\t\t\t{0}'.format(name)) # Inputs available inputs = requests.get('{0}/inputs'.format(url)).json() print('Control Inputs:\t\t\t{0}'.format(inputs)) # Measurements available measurements = requests.get('{0}/measurements'.format(url)).json() print('Measurements:\t\t\t{0}'.format(measurements)) # Default simulation step step_def = requests.get('{0}/step'.format(url)).json() print('Default Simulation Step:\t{0}'.format(step_def)) # -------------------- # Define customized KPI if any customizedkpis = [] # Initialize customzied kpi calculation list customizedkpis_result = { } # Initialize tracking of customized kpi calculation results if customized_kpi_config is not None: with open(customized_kpi_config) as f: config = json.load(f, object_pairs_hook=collections.OrderedDict) for key in config.keys(): customizedkpis.append(kpicalculation.cutomizedKPI(config[key])) customizedkpis_result[kpicalculation.cutomizedKPI( config[key]).name] = [] customizedkpis_result['time'] = [] # -------------------- # RUN TEST CASE # ------------- # Reset test case print('Resetting test case if needed.') res = requests.put('{0}/reset'.format(url)) # Set simulation step print('Setting simulation step to {0}.'.format(step)) res = requests.put('{0}/step'.format(url), data={'step': step}) print('\nRunning test case...') # Initialize u u = pid.initialize() # Simulation Loop for i in range(int(length / step)): # Advance simulation y = requests.post('{0}/advance'.format(url), data=u).json() # Compute next control signal u = pid.compute_control(y) # Compute customized KPIs if any if customized_kpi_config is not None: for customizedkpi in customizedkpis: customizedkpi.processing_data( y) # Process data as needed for custom KPI customizedkpi_value = customizedkpi.calculation( ) # Calculate custom KPI value customizedkpis_result[customizedkpi.name].append( round(customizedkpi_value, 2)) # Track custom KPI value print('KPI:\t{0}:\t{1}'.format( customizedkpi.name, round(customizedkpi_value, 2))) # Print custom KPI value customizedkpis_result['time'].append( y['time']) # Track custom KPI calculation time print('\nTest case complete.') # ------------- # VIEW RESULTS # ------------ # Report KPIs kpi = requests.get('{0}/kpi'.format(url)).json() print('\nKPI RESULTS \n-----------') for key in kpi.keys(): if key == 'tdis_tot': unit = 'Kh' elif key == 'ener_tot': unit = 'kWh' elif key == 'cost_tot': unit = 'euro or $' elif key == 'emis_tot': unit = 'kg CO2' elif key == 'time_rat': unit = '' print('{0}: {1} {2}'.format(key, kpi[key], unit)) # ------------ # POST PROCESS RESULTS # -------------------- # Get result data res = requests.get('{0}/results'.format(url)).json() time = [x / 3600 for x in res['y']['time']] # convert s --> hr TZone = [x - 273.15 for x in res['y']['TRooAir_y']] # convert K --> C PHeat = res['y']['PHea_y'] QHeat = res['u']['oveAct_u'] # Plot results if plot: from matplotlib import pyplot as plt plt.figure(1) plt.title('Zone Temperature') plt.plot(time, TZone) plt.plot(time, 20 * np.ones(len(time)), '--') plt.plot(time, 23 * np.ones(len(time)), '--') plt.ylabel('Temperature [C]') plt.xlabel('Time [hr]') plt.figure(2) plt.title('Heater Power') plt.plot(time, PHeat) plt.ylabel('Electrical Power [W]') plt.xlabel('Time [hr]') plt.show() # -------------------- return kpi, res, customizedkpis_result
def run(plot=True): '''This is the main script. Parameters ---------- plot : boolean True to plot result at end. Default is True. Returns ------- kpi : dict Dictionary containing KPI values at end of test. ''' # GENERAL PACKAGE IMPORT # ----------------------------------------------------------------------------- import requests import numpy as np # ----------------------------------------------------------------------------- # TEST CONTROLLER IMPORT # ----------------------------------------------------------------------------- from controllers import pid # ----------------------------------------------------------------------------- # SET TEST PARAMETERS # ----------------------------------------------------------------------------- # Set URL for test case url = 'http://127.0.0.1:5000' # Set testing scenario scenario = {'time_period': 'test_day', 'electricity_price': 'dynamic'} # Set control step step = 300 # ----------------------------------------------------------------------------- # GET TEST INFORMATION # ----------------------------------------------------------------------------- # Get test case name name = requests.get('{0}/name'.format(url)).json() # Get inputs available inputs = requests.get('{0}/inputs'.format(url)).json() # Get measurements available measurements = requests.get('{0}/measurements'.format(url)).json() # ----------------------------------------------------------------------------- # RUN TEST CASE # ----------------------------------------------------------------------------- # Set control step requests.put('{0}/step'.format(url), data={'step': step}) # Set test case scenario y = requests.put('{0}/scenario'.format(url), data=scenario).json()['time_period'] # Record test start time start_time = y['time'] # Simulation Loop while y: # Compute control signal u = pid.compute_control(y) # Advance simulation with control signal y = requests.post('{0}/advance'.format(url), data=u).json() # ----------------------------------------------------------------------------- # GET RESULTS # ----------------------------------------------------------------------------- # Get KPIs kpi = requests.get('{0}/kpi'.format(url)).json() # Get zone temperature over test period args = { 'point_name': 'TRooAir_y', 'start_time': start_time, 'final_time': np.inf } res = requests.put('{0}/results'.format(url), data=args).json() # ----------------------------------------------------------------------------- # PRINT AND VIEW RESULTS # ----------------------------------------------------------------------------- print('\nKPI RESULTS \n-----------') for key in kpi.keys(): if key == 'tdis_tot': unit = 'Kh' if key == 'idis_tot': unit = 'ppmh' elif key == 'ener_tot': unit = 'kWh' elif key == 'cost_tot': unit = 'euro or $' elif key == 'emis_tot': unit = 'kg CO2' elif key == 'time_rat': unit = '' print('{0}: {1} {2}'.format(key, kpi[key], unit)) # Plot if plot: from matplotlib import pyplot as plt plt.figure(1) plt.title('Zone Temperature') plt.plot(res['time'], np.array(res['TRooAir_y']) - 273.15) plt.plot(res['time'], 20 * np.ones(len(res['time'])), '--') plt.plot(res['time'], 23 * np.ones(len(res['time'])), '--') plt.ylabel('Temperature [C]') plt.xlabel('Time [hr]') plt.show() return kpi