Documentation Home Page HYPERSIM Home Page
Pour la documentation en FRANÇAIS, utilisez l'outil de traduction de votre navigateur Chrome, Edge ou Safari. Voir un exemple.

Python API | Examples

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\example_Windows_Api.py

Basic example
import os
import sys
sys.path.append(r'C:\OPAL-RT\HYPERSIM\hypersim-version\Windows\HyApi\python')
# 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 = 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()
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(5)

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


ScopeView 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', 'python')
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()



OPAL-RT TECHNOLOGIES, Inc. | 1751, rue Richardson, bureau 1060 | Montréal, Québec Canada H3K 1G6 | opal-rt.com | +1 514-935-2323
Follow OPAL-RT: LinkedIn | Facebook | YouTube | X/Twitter