Generating PSIM Schematic from Python API

Francisco Paz
Francisco Paz Altair Community Member
edited September 26 in Community Q&A

Hi,

I am trying to use the python API to generate a circuit schematic in PSIM and getting a stack overflow error.

I originally created a simple circuit in PSIM (one DC voltage source connected to a resistor) and used "save as Python code" to generate the following script that is supposed to regenerate the schematic:

## ## Altair PSIM - Python script for  "bare_bones.psimsch"  ##    from psimapipy import* from pathlib import Path import os  # Get the directory of the current script current_script_dir = os.path.dirname(os.path.realpath(__file__))  # Get the script's file name script_file_name = os.path.basename(__file__) script_name, ext = os.path.splitext(script_file_name)  schematic_filePath = current_script_dir + "\\" + script_name + ".psimsch" #print(schematic_filePath) str_save_FolderPath = current_script_dir + "\\"  print("Loading PSIM..."); p1 = PSIM("") if (p1 and p1.IsValid()) : 	ver, subVer, subsubver, subsubsubver = p1.get_psim_version()     	sch = p1.PsimFileNew() 	if (sch) :   		# Simulation Control settings: 		p1.PsimSetElmValue(sch, None, "TIMESTEP", "1E-05")  		p1.PsimSetElmValue(sch, None, "TOTALTIME", "0.01")  		p1.PsimSetElmValue(sch, None, "PRINTTIME", "0")  		p1.PsimSetElmValue(sch, None, "PRINTSTEP", "1")  		p1.PsimSetElmValue(sch, None, "SAVEFLAG", "0")  		p1.PsimSetElmValue(sch, None, "LOADFLAG", "0")  		p1.PsimSetElmValue(sch, None, "RSWITCHON", "1E-05")  		p1.PsimSetElmValue(sch, None, "RSWITCHOFF", "10000000")   		# Spice specific settings: 		p1.PsimSetElmValue(sch, None, "SPICETIMESTEP", "1.0e-006")  		p1.PsimSetElmValue(sch, None, "MAXSTEP", "1.0e-006")  		p1.PsimSetElmValue(sch, None, "STARTTIME", "0")  		p1.PsimSetElmValue(sch, None, "ENDTIME", "1.0e-003")   		# Add a DC voltage source   		nCreatedIndex = p1.PsimCreateNewElement(sch, "VDC", "VDC1" , AREA = [240, 250, 260, 300], DIRECTION = 0, PAGE=0, XFLIP=0, _OPTIONS_=16, PORTS=[250, 250, 250, 300] , Amplitude = "100" , Lseries = "0" , Rseries = "0" )   		# Add a Resistor   		nCreatedIndex = p1.PsimCreateNewElement(sch, "MULTI_RESISTOR", "R1", SubType="Level 1"  , AREA = [290, 250, 310, 300], DIRECTION = 90, PAGE=0, XFLIP=0, _OPTIONS_=16 , PORTS=[300, 250, 300, 300] , Current_Flag = "0" , info_Manufacturer = "" , info_Package = "" , info_Part_No_ = "" , info_Power_Rating = "1/4W" , Resistance = "10" , Voltage_Flag = "0" )  		# Add a wire   		nCreatedIndex = p1.PsimCreateNewElement(sch, "WIRE", "", PAGE=0 , X1="250" , Y1="250" , X2="300" , Y2="250"  )   		# Add a wire   		nCreatedIndex = p1.PsimCreateNewElement(sch, "WIRE", "", PAGE=0 , X1="250" , Y1="300" , X2="300" , Y2="300"  )   		#Save the circuit and close file handle  		p1.PsimFileSave(sch, schematic_filePath) 		sch = None 	p1 = None  

 

When I try to run the code, I get the output in the console:

Loading PSIM... PSIM version 24.0.0.2471 Traceback (most recent call last):   File "C:\Work\public\python\test_psim\bare_bones_generated.py", line 48, in <module>     nCreatedIndex = p1.PsimCreateNewElement(sch, "VDC", "VDC1" , AREA = [240, 250, 260, 300], DIRECTION = 0, PAGE=0, XFLIP=0, _OPTIONS_=16, PORTS=[250, 250, 250, 300] , Amplitude = "100" , Lseries = "0" , Rseries = "0" )                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "C:\work\PSIM\develop_D\Files\Python\Source\psimapipy-sources\Psim_Class.py", line 1518, in PsimCreateNewElement OSError: exception: stack overflow

From this, I understand the script successfully advanced to "# Add a DC Voltage source" and then it fails. Line 1518 in Psim_Class.py is an iteration over the kwargs.items

If I change the order of the components (add the resistor first), the script fails when adding the resistor (it seems to fail on the first "PsimCreateNewElement" call).

Any help figuring out the problem would be appreciated.

I am running the script in a Windows 11 machine.

Tagged:

Answers

  • NikosDimitrakopoulos
    NikosDimitrakopoulos
    Altair Employee
    edited September 26

    Dear Francisco,

    Nice to see that you started using the python API. May I ask what you are trying to automate here with python schematic creation? Maybe there is a way to reach your goal, code-free.

    Nevertheless, please create a support request at

    Contact Support

    So we can take a deeper look into your issue and potentially involve the development team if this stems from a bug.

  • Francisco Paz
    Francisco Paz Altair Community Member
    edited September 26

    Dear Francisco,

    Nice to see that you started using the python API. May I ask what you are trying to automate here with python schematic creation? Maybe there is a way to reach your goal, code-free.

    Nevertheless, please create a support request at

    Contact Support

    So we can take a deeper look into your issue and potentially involve the development team if this stems from a bug.

    Thanks for the response. I will place the support request.

    What I am trying to accomplish is to implement a form of version control in my schematics. In a way, what I want to do is 

    1st) work on my schematic and simulations

    2nd) save my changes and create a netlist/python code or any

    3rd) push the python code or netlist to a repo. Now I can see what changed and between versions in plain text.

    4th) re-generate the schematic from the python code

    5th) go back to the 1st step.

    It is not ideal, but it may work while we don't have other native forms of version control.

    Initially, I wanted to do it through the generated netlist, but the python API was suggested as a better alternative: Netlist Export/Import - PSIM - Altair Products - Altair Community