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.

Time dependant coefficients in Livelink for Matlab

Please login with a confirmed email address before reporting spam

Dear all,

I am a new user of COMSOL and currently trying to use COMSOL Livelink for Matlab in order to run simulations.
I am planning on using the coefficient form PDE functions in my simulations. As a curiosity, my question is quite simple, but I haven't been able to find any answers in this discussion forum.

I would like Comsol to solve a 2D-coefficient form PDE, whose coefficients might depend on time t.

I'm just wondering if COMSOL is able to handle such a feature and how it could be done... For this reason, I could really use some documentation on this, or inspiring models where such a feature was implemented.

Thank you for your time,
Best regards,

Matt

5 Replies Last Post 29.06.2012, 09:15 GMT-4

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 05.06.2012, 10:12 GMT-4
Hi,


Yes it is possible.

All models generated in COMSOL can be transferred to MATLAB, be it coefficient form or others. Of course you go to matlab provided you want to do things that cannot be easily or efficiently implemented within COMSOL GUI.

I think (but I cannot guarantee as I don't know your work) it is not difficult for you to solve equations with time dependent coefficients. This can be done explicitly in MATLAB by storing previous time step values in an array and calling them in subsequent time steps. This is one approach.

It can also be done in COMSOL GUI itself if you choose to work with "time discrete solver" wherein you have prev(u,i) operator from which you can use previous time step values in the current time step (u is any expression that is a function of time). This is available in the manual. There is also '"at" and "with" operators, which you can read yourself from the manual. There are also many threads that tell you how to do this exactly.

May be there are other ways too.

Hope this helps.


Suresh
Hi, Yes it is possible. All models generated in COMSOL can be transferred to MATLAB, be it coefficient form or others. Of course you go to matlab provided you want to do things that cannot be easily or efficiently implemented within COMSOL GUI. I think (but I cannot guarantee as I don't know your work) it is not difficult for you to solve equations with time dependent coefficients. This can be done explicitly in MATLAB by storing previous time step values in an array and calling them in subsequent time steps. This is one approach. It can also be done in COMSOL GUI itself if you choose to work with "time discrete solver" wherein you have prev(u,i) operator from which you can use previous time step values in the current time step (u is any expression that is a function of time). This is available in the manual. There is also '"at" and "with" operators, which you can read yourself from the manual. There are also many threads that tell you how to do this exactly. May be there are other ways too. Hope this helps. Suresh

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 06.06.2012, 04:11 GMT-4
Hi,

Thank you for your answer !


Hi,


Yes it is possible.

All models generated in COMSOL can be transferred to MATLAB, be it coefficient form or others. Of course you go to matlab provided you want to do things that cannot be easily or efficiently implemented within COMSOL GUI.

I think (but I cannot guarantee as I don't know your work) it is not difficult for you to solve equations with time dependent coefficients. This can be done explicitly in MATLAB by storing previous time step values in an array and calling them in subsequent time steps. This is one approach.


It sound quite messy. I'm still wondering if instead of setting constant values to the coefficient, we could instead set a time-dependant function. Something similar to pdepe, somehow.
www.mathworks.fr/help/techdoc/ref/pdepe.html


It can also be done in COMSOL GUI itself if you choose to work with "time discrete solver" wherein you have prev(u,i) operator from which you can use previous time step values in the current time step (u is any expression that is a function of time). This is available in the manual. There is also '"at" and "with" operators, which you can read yourself from the manual. There are also many threads that tell you how to do this exactly.


Sounds great. Which manual are you talking about ? Been searching for it yesterday and haven't found precisely, which one, you were talking about... The API Guide ?


May be there are other ways too.

Hope this helps.

Suresh


Matt
Hi, Thank you for your answer ! [QUOTE] Hi, Yes it is possible. All models generated in COMSOL can be transferred to MATLAB, be it coefficient form or others. Of course you go to matlab provided you want to do things that cannot be easily or efficiently implemented within COMSOL GUI. I think (but I cannot guarantee as I don't know your work) it is not difficult for you to solve equations with time dependent coefficients. This can be done explicitly in MATLAB by storing previous time step values in an array and calling them in subsequent time steps. This is one approach. [/QUOTE] It sound quite messy. I'm still wondering if instead of setting constant values to the coefficient, we could instead set a time-dependant function. Something similar to pdepe, somehow. http://www.mathworks.fr/help/techdoc/ref/pdepe.html [QUOTE] It can also be done in COMSOL GUI itself if you choose to work with "time discrete solver" wherein you have prev(u,i) operator from which you can use previous time step values in the current time step (u is any expression that is a function of time). This is available in the manual. There is also '"at" and "with" operators, which you can read yourself from the manual. There are also many threads that tell you how to do this exactly. [/QUOTE] Sounds great. Which manual are you talking about ? Been searching for it yesterday and haven't found precisely, which one, you were talking about... The API Guide ? [QUOTE] May be there are other ways too. Hope this helps. Suresh [/QUOTE] Matt

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 06.06.2012, 05:38 GMT-4
Hi,


In COMSOL GUI, under help, just type prev() and you will get to know. Because everything in the manual can also be accessed through the GUI help, at least that is what I realise.

Also please download a file that Niklas Rom (from COMSOL) had uploaded into this discussion forum in response to a query concerning time discrete solver.


Suresh
Hi, In COMSOL GUI, under help, just type prev() and you will get to know. Because everything in the manual can also be accessed through the GUI help, at least that is what I realise. Also please download a file that Niklas Rom (from COMSOL) had uploaded into this discussion forum in response to a query concerning time discrete solver. Suresh

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 06.06.2012, 05:41 GMT-4
Thanks a lot for your help, sir.
Have a nice day,

Matt
Thanks a lot for your help, sir. Have a nice day, Matt

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 29.06.2012, 09:15 GMT-4
Hello again,

I am stuck again, when it comes down to defining time/space-dependant coefficients in livelink...
Basically, I would like to define a coefficient which will depend on time and space variables in the coefficient form PDE case.

Space and time have been "meshed" into a grid like this. I haven't encountered any issue with the grid.

t_set = [0:0.02:1];
x_set = [0:0.02:1];
y_set = [0:0.02:1];


At this point I need to define a function fun1, which basically returns a coefficient in a 3D-Matrix m_(x,y,t) whose size is length(t_set) * length(x_set) * length(y_set).
This m_ matrix does not depend on the differential equation to be solved. We assume, it is known at the beginning.


out = fun1(t,x,y)

t_num = find(t_set>t,1);
if isempty(t_num)
t_num = length(t_set);
end

x_num = find(x_set>x,1);
if isempty(t_num)
x_num = length(x_set);
end

y_num = find(y_set>y,1);
if isempty(y_num)
y_num = length(y_set);
end

out = m_(x_num, y_num, t_num)

end


Basically, I would like to do this kind of thing.


model.physics('c').feature('cfeq1').set('a', 1, 'fun1(t,x,y)');


But it does not work... I've been reading the manual about the at(), with() and prev() operators as advised by Suresh, but I don't see how it could help me on this. I simply don't get it.

Any simple illustrative example ? Any lead that could help me on this ?
Thank you for your time.
Hello again, I am stuck again, when it comes down to defining time/space-dependant coefficients in livelink... Basically, I would like to define a coefficient which will depend on time and space variables in the coefficient form PDE case. Space and time have been "meshed" into a grid like this. I haven't encountered any issue with the grid. [QUOTE] t_set = [0:0.02:1]; x_set = [0:0.02:1]; y_set = [0:0.02:1]; [/QUOTE] At this point I need to define a function fun1, which basically returns a coefficient in a 3D-Matrix m_(x,y,t) whose size is length(t_set) * length(x_set) * length(y_set). This m_ matrix does not depend on the differential equation to be solved. We assume, it is known at the beginning. [QUOTE] out = fun1(t,x,y) t_num = find(t_set>t,1); if isempty(t_num) t_num = length(t_set); end x_num = find(x_set>x,1); if isempty(t_num) x_num = length(x_set); end y_num = find(y_set>y,1); if isempty(y_num) y_num = length(y_set); end out = m_(x_num, y_num, t_num) end [/QUOTE] Basically, I would like to do this kind of thing. [QUOTE] model.physics('c').feature('cfeq1').set('a', 1, 'fun1(t,x,y)'); [/QUOTE] But it does not work... I've been reading the manual about the at(), with() and prev() operators as advised by Suresh, but I don't see how it could help me on this. I simply don't get it. Any simple illustrative example ? Any lead that could help me on this ? Thank you for your time.

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.