1
votes

I have to realise a coupled simulation with Simulink and an external application (LS-DYNA). The leading simulation is done in Simulink, where I want to implement a function block as following: enter image description here

The interaction is done using the cmd of windows, so the Simulink block should do the following:

function [ x,y ] = ExternalSimlation( u,v )

% open the windows cmd and run the external simulation by passing u and v
[status,cmdout] = system( 'command for executing the external simulation -u -v');

    function [ x,y ] = readcmd( cmdout )
        %algorithm to evaluate the cmd output
    end

    [x,y] = readcmd(cmdout);

end

The exact code should NOT be relevant here. My question is, how can I implement the interface into the simulink model? Can I just use one of the custom function blocks, use my code above, and it will work? But which one, I don't really see the difference.

OR, my other idea was to build something like the following: enter image description here

And then use a while-loop like this:

while ... do
[u,v] = sim('model', 'x',x,'y', y, 'some option just to run a single step');
[x,y] = ExternalSimlation( u,v )
[u,v] = sim('model' .... next step ...)

to execute the simulink simulations step-by-step. How can I realise that? (The rest of the simulation contains complex control algorithms, derivations and integrations)

I don't have experiences in writing batch files, but that seems possible for me as well.

If you wonder why I'm not just testing things out, it's because I don't have the external application available (I just know how the in- and output works) and don't want to waste time in coding a substitutional application just for testing, if it's not possible at all.

Any hints and experiences in coupled simulations with simulink are highly appreciated! Thank you!

2
Your first approach should work. The system command blocks execution by default so the simulation should wait until it has completed before allowing Simulink to proceed with further processing. If using a MATLAB Function block then you'll need to define system as being a coder.extrinsic function to use it, and may have trouble depending on the types of data returned by the system call. But if so then use an m-code S-function. The second approach, although technically feasible, would be prohibitively slow. - Phil Goddard
So you suggest a S-function to be the best solution? I don't understand your statement about the Matlab function block. Can you specify it? Which system do I need to re-define? - Robert Seifert
The MATLAB Function block works best when you have data that doesn't change size/dimensions or datatype, and any functions not in the embedded MATLAB subset must be defined as being coder.extrinsic. Search the doc for that term (i.e. coder.extrinsic) for more info. There a brief example here that uses the older syntax eml.extrinsic (which has the same effect as the newer coder.extrinsic). Should you have trouble with that approach then it will be very easy to do in an m-code S-function. - Phil Goddard

2 Answers

0
votes

As far as I know the "proper" way to couple simulink to other applications would be to write S-Functions. If the external application has a C/C++ interface this should also by far be the best solution performance-wise: http://www.mathworks.de/de/help/simulink/create-cc-s-functions.html

0
votes

The question pertains to the co-simulation mechanisms of Simulink. i suggest http://www.mathworks.in/help/matlab/calling-external-functions.html as a first step. And to compare and understand the implementations of existing software's solution this manual should help. http://vector.com/portal/medien/cmc/application_notes/AN-IND-1-007_Using_MATLAB_with_CANoe.pdf

To start with , basically all the co-simulation softwares i have worked with uses COM interfaces from windows.(i have not worked matlab in any other OS) They create objects and uses shared memory for data transmissions.And basically it is through S-functions they achieve them. Also you can consider code generation from simulink or stateflow and writing a wrapper for external applications.