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.