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.

Using data from Matlab

Please login with a confirmed email address before reporting spam

Dear all Comsol users,

To make the question simple, a heat transfer model is set up in 2-D, and every step, I want to calculate the heat source term in Matlab (which will be a 2-D matrix), then send this heat source term back to Comsol.

Currently, from what I found online, the most viable way seems to be setting up a interpolation function. But my concern is that, for a 2-D problem, the interpolation function in Comsol seems to require the data source to be from 'file' instead of table (for 'table' I can set it in Matlab code). This means that I need to save this heat source term matrix from Matlab to hard drive, then read it again in Comsol every step. Will this be very slow? Or is there a better way to do this?

Thanks!

6 Replies Last Post 2016年5月16日 GMT-4 08:14

Please login with a confirmed email address before reporting spam

Posted: 9 years ago 2016年3月21日 GMT-4 14:45
You can set 'table' in comsol so that you don't need to read from files. Here is the script that you may change in the *.m file:

% set interpolation function

model.func('int1').set('table', cellfun( @(x) num2str(x, '%0.10g'), num2cell( [y0, fy] ), 'UniformOutput', false));

where [y0, fy] is the two-column data that you stored in matlab. cellfun works as converting the data (mostly in double type ) into cells of string, so that comsol can read. In this way, you do not need to save data into the hard disk, but import directly from matlab to comsol.

By the way, unless you really have very large size of data, say ~GB, I don't think using the "files" option will be slow, since the FEM simulation usually consumes more time than data exchange between your hard drive and matlab.
You can set 'table' in comsol so that you don't need to read from files. Here is the script that you may change in the *.m file: % set interpolation function model.func('int1').set('table', cellfun( @(x) num2str(x, '%0.10g'), num2cell( [y0, fy] ), 'UniformOutput', false)); where [y0, fy] is the two-column data that you stored in matlab. cellfun works as converting the data (mostly in double type ) into cells of string, so that comsol can read. In this way, you do not need to save data into the hard disk, but import directly from matlab to comsol. By the way, unless you really have very large size of data, say ~GB, I don't think using the "files" option will be slow, since the FEM simulation usually consumes more time than data exchange between your hard drive and matlab.

Please login with a confirmed email address before reporting spam

Posted: 9 years ago 2016年3月21日 GMT-4 15:00
Thanks for the information.

I have been occupied with some other projects recently, and I will try it out as soon as I got some time.

Once again, thank you so much.
Thanks for the information. I have been occupied with some other projects recently, and I will try it out as soon as I got some time. Once again, thank you so much.

Please login with a confirmed email address before reporting spam

Posted: 9 years ago 2016年3月22日 GMT-4 21:48
Dear Peng,

I think what you showed is how to pass 1-dimensional data back to Comsol? That is, fy = f(y0)?

What I want to know, is actually how to pass 2-dimensional data to Comsol, that is f(x,y) = f(x0,y0).

Do you know how to do this?

Thanks again,

Yadong
Dear Peng, I think what you showed is how to pass 1-dimensional data back to Comsol? That is, fy = f(y0)? What I want to know, is actually how to pass 2-dimensional data to Comsol, that is f(x,y) = f(x0,y0). Do you know how to do this? Thanks again, Yadong

Please login with a confirmed email address before reporting spam

Posted: 9 years ago 2016年3月25日 GMT-4 16:24
Dear Yadong,

Yes, for the multi-variable interpolation, the only way that I know is to use "import from file".

Peng
Dear Yadong, Yes, for the multi-variable interpolation, the only way that I know is to use "import from file". Peng

Please login with a confirmed email address before reporting spam

Posted: 9 years ago 2016年3月25日 GMT-4 16:27
Ok, I see.

Thanks anyway!

Yadong
Ok, I see. Thanks anyway! Yadong

Please login with a confirmed email address before reporting spam

Posted: 9 years ago 2016年5月16日 GMT-4 08:14
Hi all!

I found the solution very interesting as it is what I would need. Unfortunately, my MATLAB script does not replace the orders in the .m functions exported from COMSOL...

I need to replace the initial interpolation file established as a global definition. However, when I replace the corresponding line in the script, to add/import a new table, when running, it does not account for it and it runs according to the previous file. It seems like I am not able to overcome the first instructions from COMSOL and the default orders prevail.

I am trying to replace this line (a certain injection rate "q" over a time series, q(t):
model.func('int2').set('table', {'0' '0'; '600' '40'; '1200' '40'; '1800' '40'; '2400' '40'});

with this one:
model.func('int2').set('table', cellfun( @(x) num2str(x, '%0.10g'), num2cell([t, q_t]), 'UniformOutput', false));

I am sure there is something I am missing... I just cannot figure it out where... Maybe some of you could propose a solution! :)

Thanks!

Maria
Hi all! I found the solution very interesting as it is what I would need. Unfortunately, my MATLAB script does not replace the orders in the .m functions exported from COMSOL... I need to replace the initial interpolation file established as a global definition. However, when I replace the corresponding line in the script, to add/import a new table, when running, it does not account for it and it runs according to the previous file. It seems like I am not able to overcome the first instructions from COMSOL and the default orders prevail. I am trying to replace this line (a certain injection rate "q" over a time series, q(t): model.func('int2').set('table', {'0' '0'; '600' '40'; '1200' '40'; '1800' '40'; '2400' '40'}); with this one: model.func('int2').set('table', cellfun( @(x) num2str(x, '%0.10g'), num2cell([t, q_t]), 'UniformOutput', false)); I am sure there is something I am missing... I just cannot figure it out where... Maybe some of you could propose a solution! :) Thanks! Maria

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.