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:
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:
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!
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
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.