使用Python实现PSCAD自动仿真

PSCAD自动运行

为什么使用Python?

目的是为了实现在参数不断调整下的仿真自动化,在需要少量数据时可以使用手动操作,但是当数据过多时,手动操作过于费时费力,很容易想到使用脚本语言进行编程,先前尝试过使用MATLAB,但是MATLAB并不支持GFortran,需要更改底层编译器,另外官方对MATLAB的支持也不多,后来看到官方在PSCAD 4.6.1后支持PSCAD Automation with Python Scripting,因此选择使用Python。

我仅仅只是修改几个参数,然后重复运行得到一系列数据,如何做?

1、安装PSCAD Automation Library

从官网下载Automation Library,解压缩,然后点击“Installer.exe”安装。

注意:安装中可以安装AL自带的Python包,若之前安装过Python可以选择不安装,有个.whl文件可能安装较慢,请耐性等待,此外,查看“C:\Users\Public\Documents\Manitoba HVDC Research Centre\ATS\ProductList.xml”文件是否存在,若不存在,请打开“\AutomationLibrary\Medic\FortranMedic.exe”,执行完后会存在ProductList.xml。

2、例程代码

import sys, os
import mhrc.automation

# Import other utilities to perform cool stuff
from mhrc.automation.utilities.file import File

import win32com.client
import shutil

#---------------------------------------------------------------------
# Configuration
#---------------------------------------------------------------------
print("Automation Library:", mhrc.automation.VERSION)

pscad_version = 'PSCAD 4.6.2 (x64)'
fortran_version = 'GFortran 4.6.2'
fortran_ext = '.gf46'
project_name = 'DC_microgrid_mini'

# Working directory
working_dir = os.getcwd() + "\\"

#---------------------------------------------------------------------
# Main script 
#---------------------------------------------------------------------

# Source and destination folders for output data
src_folder = working_dir + project_name + fortran_ext
dst_folder = working_dir + "DC_mini_output"   

# Clear the ouput folder
try:
    shutil.rmtree(dst_folder)
except Exception as ignored:
    pass

os.mkdir(dst_folder)

# Launch specific PSCAD and Fortran version
pscad = mhrc.automation.launch_pscad(pscad_version=pscad_version, fortran_version=fortran_version,certificate=False)

if pscad:
    try:
        # Load the project
        pscad.load([working_dir + project_name + ".pscx"])
        project = pscad.project(project_name) 
        project.focus()

        # Get the "Main" canvas
        main = project.user_canvas('Main')

        # Get all breaker components
        b1 = main.user_cmp(1687879632)
        b2 = main.user_cmp(1492289460)
        b3 = main.user_cmp(523751948)

        # Get the Harmonic Impedance component
        #harm = main.user_cmp(229910773)

        #-----------------------------------------------------
        # Test 1 Energize T1 with T2 and T3 out of service
        #-----------------------------------------------------
        for j in range(1,10):
            for i in range(1,51):
            # Close Breaker 1 at 0.5
                res=0.1*i
                dis=j
                dsname="dis"+str(j)+"res"+str(i)
                b1.set_parameters(ROn=res)
                # Open Breaker 2
                b2.set_parameters(Length=dis)
                # Open Breaker 3
                b3.set_parameters(Length=10-dis)

                project.run()

                # Save data to output folder
                folder = os.path.join(dst_folder, dsname)
                File.move_files(src_folder, folder, ".out", ".inf")
                shutil.copy(folder+"\\dis_res_01.out",dst_folder+"\\"+dsname+".out")

        #-----------------------------------------------------


        #project.run()


        print("Script is Done!")

    finally:
        #pscad.quit()
        pass
else:
    print("Failed to launch PSCAD")
    

# ------------------------------------------------------------------------------
#  End of script
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

当然,也可以参考官网的例子或者API文档,然后编写py代码。

默认图片
admin
文章: 6

留下评论