Calling Julia from EMTDC/PSCAD or MATLAB code

I would like to use Julia programming as a software in loop for my EMTDC/PSCAD or MATLAB simulation i.e., optimization problem solved in julia and output of this optimization solution is given to the Simulation platform and again output of this simulation is given as input to the julia for optimization.
As I found some julia modules like PowerModels.jl for application of power systems optimization, I want to use same for a software in loop for my research work. If we can use julia as a software in loop, please let me know how to do it.

I would start looking at basic socket communication:



How do I use sockets?

In most environments, you can read & write (commands) much the same way as you would a file (once the socket is open).

Problem with sockets

The problem with socket is that they are relatively low level. You might have to define your own communication protocol (example):

  • Matlab->Julia: “Run operation X in Julia”
  • Julia->Matlab: “Ok, I’m done with operation X” (i.e. some form of ACK)
  • etc.

Alternatives

There are higher level tools out there that can make things simpler:

  • RPC: remote procedure calls
  • (Sorry, I don’t know that many of them)

Simulink seems to support other protocols, listed here:
–> Communication Protocols - MATLAB & Simulink

But I’m not sure which ones would be practical for this application (probably not CAN or RS-232 in your case).

I know many CAD tools support the LXI protocol (typically through ethernet) to talk to instruments that perform measurements. There might be a way to use that somehow. But again, I think socket communication is probably a better starting point.

What if my application doesn’t support sockets?

Linux systems provide the mkfifo command to create “named pipes”. It creates a “file” that pretty much any program can write to in order to communicate with your Julia socket:
–> Using Named Pipes (FIFOs) with Bash | Linux Journal

I use ZMQ.jl and MessagePack.jl to pack up dictionaries of data and send them over a socket to julia and then back. This means you need zeromq and messagepack in your EMTDC/PSCAD or MATLAB code too. It’s actually a pretty nice way to work.

Alternatively you can try to embed julia in MATLAB etc, but this has it’s own problems. I’m not embedding in MATLAB, so maybe you can get it working there, but I’ve essentially given up on this route for my own plugin. I check back on PackageCompiler.jl every few months to see if there is any progress on this front, but it’s kinda gone backwards recently. Some info in the docs too: Embedding Julia · The Julia Language

Hi @Orbots, would you like to share some details how you got the calling-Julia-from-Matlab working?

I don’t use this from Matlab. I do use it from a number of other tools. Basically anywhere I can write C++ plugins.

From Julia:
use the packages

From Matlab:

Run Matlab.
Run you Julia program in a terminal or notebook.

Choose a local port address to use. e.g. “tcp://*:5556”
You need to write a little loop in julia or matlab ( depending on which is the “server” ) using zeromq to listen on that address for the packets, unpack and dispatch on your work function. When the work function is done you return the result via zeromq. From the other end you pack up your data and send via zeromq to the address.

Packing and unpacking is via msgpack, so you can use any data supported by msgpack. I use msgpack ( rather than JSON ) because it has the option of extending to use your own types.

Hope this helps.

Thanks a lot!