Here’s a clean MDX documentation file explaining how to use the simulation API based on your provided code.
PSIM Simulation Guide
This guide explains how to run simulations using the PSIM Python interface, including standard PSIM, HyperSpice, and LTSpice simulation modes.
Overview
The API provides three main simulation methods:
PsimSimulate→ Standard PSIM simulationPsimSimulate_HyperSpice→ HyperSpice simulationPsimSimulate_LTSpice→ LTSpice simulation
All methods share a similar structure and accept:
simulate(schematic, graphFilePath, **kwargs)
Basic Usage
Example: Running a Standard Simulation
result = psim.PsimSimulate(
"circuit.sch",
"output.txt",
simview=1,
Tstop=0.01
)
Parameters
1. schFilePath_or_Obj
- Type:
strorPSIM_schematic - Description:
- Path to a
.schfile, or - An already loaded schematic object
- Path to a
2. graphFilePath
- Type:
str - Description:
- Output file path where simulation results will be stored
3. **kwargs
Optional simulation parameters passed as key-value pairs.
Special Parameter: simview
Controls graph generation and viewer execution:
| Value | Behavior |
|---|---|
-1 | No graph, no viewer |
0 | Generate graph only |
1 | Generate graph and open viewer |
Example:
simview=0
Simulation Modes
1. Standard PSIM
result = psim.PsimSimulate("file.sch", "out.txt")
- Uses internal PSIM solver
- Supports full parameter customization
2. HyperSpice
result = psim.PsimSimulate_HyperSpice("file.sch", "out.txt")
- Uses HyperSpice engine
- Same interface as standard simulation
3. LTSpice
result = psim.PsimSimulate_LTSpice("file.sch", "out.txt")
- Uses LTSpice backend
- May override output file path internally
How It Works (Internally)
Step 1: Open Schematic
If a file path is provided:
SimOpenSchematicFileW(...)
Step 2: Convert Parameters
Keyword arguments are converted into a flat list:
["Tstop", "0.01", "Vdc", "100"]
Step 3: Initialize Simulation
Depending on mode:
- PSIM →
SimulationInit2 - HyperSpice →
Simulation2_HyperSpice - LTSpice →
Simulation2_LTSpice
Step 4: Run Simulation
-
PSIM uses:
Thread_Simulation_Do(threadIndex) -
HyperSpice / LTSpice run directly
Step 5: Wait for Completion
GetSimulationResult(threadIndex)
Loop continues while simulation is running.
Step 6: Retrieve Results
If successful:
- Graph handle is retrieved
- Data is read column by column
Each column represents a waveform:
curve = PSIM_curve(rows, name, values, valid_flags)
Result Object
Returned object: PSIM_result
Key Fields
| Field | Description |
|---|---|
Result | Simulation status (1 = success) |
IsError | Error flag |
ErrorMessage | Error details |
Graph | Simulation data |
Accessing Simulation Data
Each simulation result contains curves:
for curve in result.Graph.curves:
print(curve.name)
Each curve includes:
- Values (
double[]) - Validity flags:
0→ missing1→ valid2→ invalid
Error Handling
If simulation fails:
if result.IsError:
print(result.ErrorMessage)
Detailed error messages are retrieved from:
Get_SimErrorMessages(...)
Notes & Best Practices
- Always check
IsErrorbefore using results - Use
simview=0for faster batch simulations - Reuse schematic objects for performance
- Avoid unnecessary file open/close cycles
Full Example
psim = PSIM()
result = psim.PsimSimulate(
"buck_converter.sch",
"buck_output.txt",
simview=0,
Tstop=0.02,
Vin=48
)
if result.IsError:
print("Simulation failed:", result.ErrorMessage)
else:
print("Simulation successful!")
for curve in result.Graph.curves:
print("Curve:", curve.name)
Summary
- Use
PsimSimulatefor standard simulations - Switch to HyperSpice or LTSpice via dedicated methods
- Pass parameters via
kwargs - Results are structured and easy to iterate