Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Here’s a basic example that shows how to start HYPERSIM®, load a network and interact with HYPERSIM® using the API.

C:\OPAL-RT\HYPERSIM\hypersim-version\Windows\HyApi\C\py\testexample_Windows_Api.py

Code Block
languagepy
themeEclipse
titleBasic example
import os
import sys
sys.path.append(r'C:\OPAL-RT\HYPERSIM\hypersim-version\Windows\HyApi\C\pypython')
# Replace hypersim-version by the version you want to test

import HyWorksApiGRPC as HyWorksApi
import time

HyWorksApi.startAndConnectHypersim()
# This script finds the model next to it, when we launch python from the same directory
designPath = r'C:\demo\HVAC_38BUS_735KV os.path.join(os.getcwd(), 'HVAC_735kV_38Bus.ecf')
HyWorksApi.openDesign(designPath)

HyWorksApi.setPreference('simulation.calculationStep', '50e-6')
calcStep = HyWorksApi.getPreference('simulation.calculationStep')

print('calcStep = ' + calcStep)

print('code directory : ' + HyWorksApi.getPreference('simulation.codeDirectory'))

print('mode : ' + HyWorksApi.getPreference('simulation.architecture'))

HyWorksApi.mapTask()
HyWorksApi.genCode()
HyWorksApi.startLoadFlow()
HyWorksApi.startSim()


time.sleep(10)
print('startSim done')

volt = HyWorksApi.getComponentParameter('Ge7', 'baseVolt')
print(('baseVolt = ' + volt[0] + volt[1]))

HyWorksApi.setComponentParameter('Ge7', 'baseVolt', str(float(volt[0]) + 2.75))


volt2 = HyWorksApi.getComponentParameter('Ge7', 'baseVolt')

print(('baseVolt = ' + volt2[0] + volt2[1]))

if( abs( float(volt2[0]) - float(volt[0]) - 2.75) > 0.0001 ):
    	print('SET_COMP ERROR')
else:
	    print('SET_COMP SUCCESS')

time.sleep(105)

HyWorksApi.stopSim()
HyWorksApi.closeDesign(designPath)
HyWorksApi.closeHyperWorks() 


Another example using the API to control ScopeView can be found here:

C:\OPAL-RT\HYPERSIM\hypersim-version\Windows\ScopeView\lib\ScopeViewApi\example.py

This example starts ScopeView, loads a template and data source, starts an acquisition, saves graphs as a pdf file and saves data to a text file. The output from ScopeView can be read from the stdout.txt and stderr.txt files generated in the same directory as the example.

To run this script, you must have an "exampleFiles" folder that contains the files template.xml, datasource.cfg and datasource.dat


Code Block
languagepy
themeEclipse
titleScopeView example
import os
import sys
import time

#Modify this line to set-up your HYPERSIM version folder
#Example : hypersim_dir = "C:\\OPAL-RT\\HYPERSIM\\hypersim_2020.3.0.o97"
hypersim_dir =  " "

python_path = os.path.join(hypersim_dir, 'Windows', 'HyApi', 'Cpython', 'py')
sys.path.insert(1, python_path)
import HyWorksApiGRPC as HyWorksApi
import ScopeViewApiGRPC as ScopeViewApi

currentPath = os.path.abspath(os.path.dirname( file ))
exportPath = os.path.join(currentPath, 'ExportData')

scopeViewProcess = ScopeViewApi.openScopeView()
ScopeViewApi.loadTemplate(os.path.join(currentPath, 'exampleFiles', 'template.xml'))
ScopeViewApi.replaceDataSource("COM1", "{file=" + os.path.abspath(os.path.join(currentPath, 'exampleFiles', 'datasource.cfg')) + "}")
ScopeViewApi.startAcquisition()

ScopeViewApi.exportToPDF(os.path.join(currentPath, 'exportPDF.pdf'))
ScopeViewApi.exportToFile(os.path.join(currentPath, 'exportTXT.txt'))

ScopeViewApi.close()