Skip to main content

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 simulation
  • PsimSimulate_HyperSpice → HyperSpice simulation
  • PsimSimulate_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: str or PSIM_schematic
  • Description:
    • Path to a .sch file, or
    • An already loaded schematic object

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:

ValueBehavior
-1No graph, no viewer
0Generate graph only
1Generate 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

FieldDescription
ResultSimulation status (1 = success)
IsErrorError flag
ErrorMessageError details
GraphSimulation 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 → missing
    • 1 → valid
    • 2 → 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 IsError before using results
  • Use simview=0 for 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 PsimSimulate for standard simulations
  • Switch to HyperSpice or LTSpice via dedicated methods
  • Pass parameters via kwargs
  • Results are structured and easy to iterate