Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

Exporting data in Simulink

Please login with a confirmed email address before reporting spam

Hi everyone, I am new to Comsol so I apologize for any obvious questions. I am trying to connect a time dependent Comsol study with a matlab subroutine to feed to a multiphysics solve at every timestep. It seems that Simulink is the preferred method to do this. However, in all the examples I’ve seen, the controlling output has been a scalar value (temp at a specific location, etc). Is it possible to export a vector from Comsol to Simulink and feed it to a matlab function? I am trying to output the temperatures and densities in every cell so the matlab function can calculate temp dependencies everywhere.

John


3 Replies Last Post Oct 25, 2022, 7:14 a.m. EDT
Remi Magnard COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 2 years ago Oct 20, 2022, 9:49 a.m. EDT
Updated: 2 years ago Oct 20, 2022, 10:35 a.m. EDT

Dear John,

You are right, Livelink for Simulink only supports scalar as input/output to run cosimulation. If you want to implement a controller on a field you need to do it in COMSOL, either implement the controller with a COMSOL expression or using an extra equation. If you have the controller implemented in MATLAB you can call an M-function directly inside the COMSOL model (see in the Home tab > Functions > MATLAB). The function argument and output are assumed to be field variable. Note that the size for the input and output variables has to be the same and thus pointwise operation may be necessary.

Best regards,

Rémi Magnard

Dear John, You are right, Livelink for Simulink only supports scalar as input/output to run cosimulation. If you want to implement a controller on a field you need to do it in COMSOL, either implement the controller with a COMSOL expression or using an extra equation. If you have the controller implemented in MATLAB you can call an M-function directly inside the COMSOL model (see in the Home tab > Functions > MATLAB). The function argument and output are assumed to be field variable. Note that the size for the input and output variables has to be the same and thus pointwise operation may be necessary. Best regards, Rémi Magnard

Please login with a confirmed email address before reporting spam

Posted: 2 years ago Oct 21, 2022, 12:36 p.m. EDT

Hi Remi, That is good to know about Simulink’s capabilities. For implementing a field controller with a Matlab function inside Comsol, would you be able to guarantee that Comsol calls it at every timestep for a transient study? I was under the impression that the two main ways to get a “real time” controller would be to either use Simulink concurrently, or to run a case at every timestep and update the Comsol solve via a Matlab function before the next run. Thank you for your help.

John

Hi Remi, That is good to know about Simulink’s capabilities. For implementing a field controller with a Matlab function inside Comsol, would you be able to guarantee that Comsol calls it at every timestep for a transient study? I was under the impression that the two main ways to get a “real time” controller would be to either use Simulink concurrently, or to run a case at every timestep and update the Comsol solve via a Matlab function before the next run. Thank you for your help. John

Remi Magnard COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 2 years ago Oct 25, 2022, 7:14 a.m. EDT
Updated: 2 years ago Oct 25, 2022, 8:01 a.m. EDT

Yes the MATLAB function callback is performed at every time step as any functions defined in COMSOL.

Note that COMSOL may assemble the system in different blocks; this means that the same function can be called several times for the same time step. If you need to keep track of the solution in a previous time step, then you may need to define a persistent variable in your MATLAB function.

If you have further questions, I invite you to send them via the COMSOL support (you can mention me in the subject); this will ensure you a proper follow up.

Yes the MATLAB function callback is performed at every time step as any functions defined in COMSOL. Note that COMSOL may assemble the system in different blocks; this means that the same function can be called several times for the same time step. If you need to keep track of the solution in a previous time step, then you may need to define a persistent variable in your MATLAB function. If you have further questions, I invite you to send them via the COMSOL support (you can mention me in the subject); this will ensure you a proper follow up.

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.