Skip to main content

⚡ PSIM Fast MEX Interface (MATLAB) — Usage Guide

This document explains how to use the run_psim_mex MEX function for high-performance PSIM simulations directly inside MATLAB, with in-memory data extraction and timetable output.


🚀 Overview

run_psim_mex allows you to:

  • Run multiple PSIM simulations in parallel
  • Avoid (or minimize) disk I/O
  • Extract simulation results directly into MATLAB
  • Return results as native timetable objects
  • Automatically stop execution on first failure (fail-fast)

📦 Function Signature

res = run_psim_mex(schList, maxThreads)

📥 Inputs

schList

  • Type: cell array of char/string
  • Description: List of .sch schematic file paths
{"buck.sch", "boost.sch"}

maxThreads

  • Type: scalar (int)
  • Description: Maximum number of parallel simulations
4

📤 Output

res → struct array

Each simulation returns:

res(i).tt % timetable
res(i).status % int32
res(i).error % char

📊 res(i).tt (timetable)

  • Row times: duration (seconds)
  • Variables: simulation signals

Example:

res(1).tt

ans =

timetable

Time I_L1 I_R1 Vout
_____ ______ ______ ______
0 sec ...
1e-6 sec ...

📌 status values

ValueMeaning
1Success
0Failed
-1Initialization error
-99Aborted due to another failure

⚠️ error

  • Contains PSIM error messages (if available)
  • Empty string if simulation succeeded

🧪 Basic Example

sch = {
"buck.sch"
"boost.sch"
};

res = run_psim_mex(sch, 2);

tt = res(1).tt;

plot(tt.Time, tt.Vout)
res = psim_parameters_mex(singlesch, {params1, params2}, maxThreads)

⚙️ Execution Modes (Important)

The MEX internally controls PSIM using dwOptions.

Modes:

ModeValueBehavior
Full GUI0Writes graph + opens Simview
Headless1Writes graph only
🚀 In-memory5No graph file, no Simview

The MEX uses:

dwOptions = 5;

✔ No disk writes ✔ Maximum performance


⚠️ Compatibility Note

Some PSIM versions require graph generation.

If you observe:

res(i).tt = []

It means:

PSIM did not expose in-memory graph data.

Fix:

Recompile MEX with:

dwOptions = 1;

⚡ Performance Tips

1. Use multiple threads

res = run_psim_mex(schList, 8);

2. Avoid large file outputs

Use in-memory mode (dwOptions = 5)


3. Plot directly from timetable

plot(res(1).tt.Time, res(1).tt.Vout)

4. Access signals easily

tt = res(1).tt;

tt.I_L1
tt.Vout

🧠 Data Model

Each simulation:

PSIM → memory (graph object) → MEX → MATLAB arrays → timetable

✔ Only one memory copy ✔ No intermediate buffers


🛑 Error Handling

Fail-fast behavior

  • If one simulation fails:
    • Remaining simulations are aborted
    • Status becomes -99

Example:

for i = 1:length(res)
if res(i).status ~= 1
disp(res(i).error)
end
end

🔧 Troubleshooting


❌ Empty timetable

Cause:

  • PSIM did not generate in-memory graph

Fix:

  • Use dwOptions = 1

❌ MATLAB crash

Cause:

  • Invalid pointer usage
  • Wrong memory ownership

Fix:

  • Ensure no direct pointer sharing (already handled in MEX)

❌ Invalid variable names

PSIM names like:

I(L1)
V(out)

Are sanitized to:

I_L1_
V_out_

❌ Timetable error

If you see:

Provide a datetime or duration...

✔ Already fixed internally via:

seconds(time)

🚀 Advanced Usage


Loop over simulations

for i = 1:length(res)
if res(i).status == 1
plot(res(i).tt.Time, res(i).tt.Vout)
hold on
end
end

Extract matrix

tt = res(1).tt;

data = [tt.I_L1, tt.Vout];

Convert to array

A = table2array(timetable2table(res(1).tt));

🏁 Summary

✔ High-performance PSIM execution ✔ No graph files required (if supported) ✔ Native MATLAB timetable output ✔ Parallel execution ✔ Fail-fast reliability


🔥 Next Steps

Possible upgrades:

  • Multi-run merged timetables
  • Automatic time scaling (ns / µs / ms)
  • GPU processing pipeline
  • Real-time streaming

You now have a near-native PSIM → MATLAB pipeline. Happy simulating!