Calling solvetime from Simulink

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Dan
Reply | Threaded
Open this post in threaded view
|

Calling solvetime from Simulink

Dan
I want to integrate a FEATool Probem into Simulink. Essentially, I want to setup a FEATool Problem struct from a normal Matlab Script like

clear fea
fea.sdim = { 'x', 'y' };
fea = addphys( fea, @heattransfer, { 'T' } );

and so on. Then, I am calling solvetime to solve the first Second from this Matlab Script. From here onwards I want to call solvetime from Simulink to advance in time (always using the last result as an inital solution) but I am struggling to do so. Currently, I am using a Matlab Function Block in Simulink. As an easy example, imagine a Matlab Function Block in Simulink that contains

function T = fcn(fea)

    T = evalexpr('T', [0; 0], fea);

where fea is defined as parameter data (as the problem struct is available in the base workspace). I get an error saying

Parameters that have cell arrays are not supported. Cell array variable found in fea.

Is there any way to use Simulink to call solvetime? I want to use Simulink to advance in solution time as I would like to incorporate transfer functions and a PID controller into the fea problem.
Reply | Threaded
Open this post in threaded view
|

Re: Calling solvetime from Simulink

Precise Simulation
Administrator
Dan wrote
I want to integrate a FEATool Probem into Simulink. Essentially, I want to setup a FEATool Problem struct from a normal Matlab Script like

clear fea
fea.sdim = { 'x', 'y' };
fea = addphys( fea, @heattransfer, { 'T' } );

and so on. Then, I am calling solvetime to solve the first Second from this Matlab Script. From here onwards I want to call solvetime from Simulink to advance in time (always using the last result as an inital solution) but I am struggling to do so. Currently, I am using a Matlab Function Block in Simulink. As an easy example, imagine a Matlab Function Block in Simulink that contains

function T = fcn(fea)

    T = evalexpr('T', [0; 0], fea);

where fea is defined as parameter data (as the problem struct is available in the base workspace). I get an error saying

Parameters that have cell arrays are not supported. Cell array variable found in fea.

Is there any way to use Simulink to call solvetime? I want to use Simulink to advance in solution time as I would like to incorporate transfer functions and a PID controller into the fea problem.
Technically your approach should work. From the error message it seems like that the simulink block function may not contain data with cell arrays, that is passing the "fea" struct

function T = fcn(fea)

    T = evalexpr('T', [0; 0], fea);

doesn't seem to work. Workarounds is to use a "persistent" variable to "store" the fea struct

function T = fcn()
    persistent fea
    if isempty(fea)
      % first call to define/set up fea
    end
    T = evalexpr('T', [0; 0], fea);

alternatively use a global variable and have "fea" defined in the main/global workspace

function T = fcn()
    global fea     % get fea from main workspace
    T = evalexpr('T', [0; 0], fea);
Dan
Reply | Threaded
Open this post in threaded view
|

Re: Calling solvetime from Simulink

Dan
Thank you for your support.

I tried both methods, but I still couldn't make it work. The persistant variable seems to be ok with the struct array of fea containing cells an all. But I get an error when setting up the fea problem inside the Matlab Function (see screenshot).

Error

Any ideas on how to fix it?

When using a global variable I seem to still be having problems with the data type.
Reply | Threaded
Open this post in threaded view
|

Re: Calling solvetime from Simulink

Precise Simulation
Administrator
Hmm, will have to try and reproduce the issue. If you could attach the model and/or script file that would be very helpful.
Dan
Reply | Threaded
Open this post in threaded view
|

Re: Calling solvetime from Simulink

Dan
Sure, I attached the Simulink Model File.
test.slx

Here is the basic idea again:
function T = fcn(on_off_signal)
    persistent fea

    if isempty(fea)
        % Set up/ initialize fea problem struct
        fea.sdim = { 'x', 'y' };
        fea = addphys( fea, @heattransfer, { 'T' } );
        % an so on...

        % Set boundary conditions depending on on_off_signal

        % Call solvetime until t = 1 Seconds
    else
        % Advance in time by 1 Second through calling solvetime using previous results as initial values
   
    end
    % Temperatur measurement
    T = evalexpr('T', [0; 0], fea);


The output of the Matlab function ist the tempeartur T at a specific point. This will be the input to a Simulink temperature controller, who will in return provide the on_off_singnal (as a boundary condition) to the next call of solvetime.

Thanks for your help!
Reply | Threaded
Open this post in threaded view
|

Re: Calling solvetime from Simulink

Precise Simulation
Administrator
Thank you for sharing your model. This seems like a very Simulink specific issue but I will have a look and try to get back to you on this by next week sometime.
Dan
Reply | Threaded
Open this post in threaded view
|

Re: Calling solvetime from Simulink

Dan
Thanks, that would be great - I'm still struggling to make it work.
Reply | Threaded
Open this post in threaded view
|

Re: Calling solvetime from Simulink

Precise Simulation
Administrator
After looking looking more closely, it should work if you change from a "MATLAB function" block to a "Interpreted MATLAB function" (and allow for input arguments even if not used, ex "T = fcn(varargin)").

The difference is that the "MATLAB function" block will try to compile down the whole FEATool toolbox to C/C++ code (with MATLAB coder) which comes with significant restrictions, and difficult to get to work. The "Interpreted" function block will run the function with standard Matlab. Although compiling can technically be faster, as FEA models are already so heavy, the most critical parts are for the most part already compiled Fortran/C++ mex functions, so further compilation is doubtful to show significant performance improvements.

Please do follow up if it still doesn't work.