Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.

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.

Matlab functions in Comsol - limited compared to 3.5?

Please login with a confirmed email address before reporting spam

Dear All,

I'm running Comsol 4.1 via the LifeLink from Matlab. Basically it works fine but I encountered a limitation, which was not present in the version 3.5.
I have a Matlab function which sets up the Comsol model object based on an input image. This is realized with the following way:
- Matlab reads in image information from an image data file
- Matlab sets up the Comsol model geometry (via Lifelink) based on this image
- mesh, physics, solvers, etc are also set by Matlab
- a Matlab function is defined, which is giving back the material property at a location (x,y,z) based on the image information. This function should be called from Comsol during runtime for each element, but this function should still be connected to Matlab (from which the Lifelink was started) as the image information is read and stored there and this function should access this image-based information during execution of the Comsol solution.

In the old (3.5) version it was possible to have a such a Matlab function in the material definition fields of Comsol.
In the new (4.1) version I cannot make it work.

What I could do is to define a Matlab function within the Comsol model object, like this:

% MATLAB CODE
extm1=model.func.create('extm1', 'MATLAB');
extm1.setIndex('funcs', 'simple_function', 0, 0);
extm1.setIndex('funcs', 'x', 0, 1);
% END MATLAB CODE

This way Comsol will be able to recognize the function 'simple_function(x)' (if it's in the Matlab path) and I can then use it to define material properties as a function of 'x', e.g. like this (just a dummy example):

% MATLAB CODE
mName='MYmat';
model.material.create(mName);
model.material(mName).name(mName);
model.material(mName).materialModel.create('Orthotropic', 'Orthotropic');
Ex = '100001';
Ey = '100002';
Ez = 'simple_function(x)';
...
... definitions of nu_ii and G_ii are done here
...
model.material(mName).materialModel('Orthotropic').set('Evector', {Ex, Ey, Ez});
model.material(mName).materialModel('Orthotropic').set('nuvector', {nuxy, nuyz, nuxz});
model.material(mName).materialModel('Orthotropic').set('Gvector', {Gxy, Gyz, Gxz});
model.material(mName).selection.set('1');
% END MATLAB CODE

This way 'Ez' will be defined by 'simple_function(x)'.

BUT. The problem is that - as far as I have understood - Comsol calls a 'new' Matlab to evaluate the function 'simple_function(x)'. This 'new' Matlab cannot communicate with the 'original' Matlab (I mean from which Comsol Lifelink was started and where the image data is stored a global variable).
Therefore 'simple_function(x)' will not have access to the image data from which it should read the material property information

Could you please help me with this issue?

Thanks a lot in advance,
Peter

5 Replies Last Post 2013年3月4日 GMT-5 14:48
Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2011年5月9日 GMT-4 03:49
Hi Peter

You are right. A new Matlab will be opened by the Comsol solver.

Solutions to your problems can be

1)
Skip calling an external function for your material data. If your material data only depends on x,y, and other independent variables (not part of the solution) you could simply use Comsol's interpolation table functionality. Simply export your data in a format that is readable by Comsol and you should get the maximum performance (since Matlab doesn't have to be called by the solver)

2)
Load the image data repeatedly in your user m-file. On a modern computer it shouldn't take (very) long.

3)
Use a persistent variable to store the image data after having loaded it once.


Lars Gregersen
Comsol Denmark
Hi Peter You are right. A new Matlab will be opened by the Comsol solver. Solutions to your problems can be 1) Skip calling an external function for your material data. If your material data only depends on x,y, and other independent variables (not part of the solution) you could simply use Comsol's interpolation table functionality. Simply export your data in a format that is readable by Comsol and you should get the maximum performance (since Matlab doesn't have to be called by the solver) 2) Load the image data repeatedly in your user m-file. On a modern computer it shouldn't take (very) long. 3) Use a persistent variable to store the image data after having loaded it once. Lars Gregersen Comsol Denmark

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2011年5月10日 GMT-4 02:50
Dear Lars,

Thanks for the answer!
Following a discussion with my colleagues we found a workaround which is in line with your suggestion no. 3: we use a global variable in the Matlab function called by Comsol to store the image data. This way the image has to be loaded only once, remains there in the 'new' Matlab and can be accessed later on if the function (or even a different function) is called again from Comsol.

Thank you again, best regards,
Peter
Dear Lars, Thanks for the answer! Following a discussion with my colleagues we found a workaround which is in line with your suggestion no. 3: we use a global variable in the Matlab function called by Comsol to store the image data. This way the image has to be loaded only once, remains there in the 'new' Matlab and can be accessed later on if the function (or even a different function) is called again from Comsol. Thank you again, best regards, Peter

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2011年6月8日 GMT-4 09:22
Peter and Lars,

Would you mind explaining how you were able to call in an image and work with the image data? I am working with a 2D image and would like to assign different material properties to parts of the image, but I am not sure how to call in and use image data.

I would really appreciate any help you can give.

Thank you!

Kelly
Peter and Lars, Would you mind explaining how you were able to call in an image and work with the image data? I am working with a 2D image and would like to assign different material properties to parts of the image, but I am not sure how to call in and use image data. I would really appreciate any help you can give. Thank you! Kelly

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2011年6月10日 GMT-4 09:01

Hi Peter

You are right. A new Matlab will be opened by the Comsol solver.

Solutions to your problems can be

1)
Skip calling an external function for your material data. If your material data only depends on x,y, and other independent variables (not part of the solution) you could simply use Comsol's interpolation table functionality. Simply export your data in a format that is readable by Comsol and you should get the maximum performance (since Matlab doesn't have to be called by the solver)

2)
Load the image data repeatedly in your user m-file. On a modern computer it shouldn't take (very) long.

3)
Use a persistent variable to store the image data after having loaded it once.


Lars Gregersen
Comsol Denmark


These solutions are good "workaround", but they do not solve a more important issue in 4.1.

When doing a standard topological optimization, the material is often defined by a complicated function "myfunction.m" that depends on [T,x,y, and other independant variables]. For each design solved during the optimization, the "main Matlab script" changes the value of the "independant variables", and then it calls Comsol in which the material is "myfunction.m". See below the 2 matlab functions.

====================================================

%% main Matlab script .m
Global (independant variables here)

while (optimum design is not reached)
change "independant variables"
define comsol model, where the material is "myfunction.m"
solve the colsol model
check if the solution correspond to an optimal design
end

====================================================

%% material script
function output= myfunction(T,x,y).m

Global (independant variables here)
calculate output using (T,x,y, and independant variables)
return output
====================================================

In 3.5, "myfunction" could receive the value of those changing "independant variable" by defining them as Global variables in the "main Matlab script" and in "myfunction". (see above)

Now in 4.1, the script shown above doesnt work. The material "myfunction" cannot receive the values of those changing "independant variables", because "main Matlab script" and "myfunction" run in completely different Matlab session.

The only solutions I see is to repeat the sequence of (i) changing the "independant variables", (ii) writing the "independant variables" in a file "myfile.mat", (iii) loading "myfile.mat" in "myfunction". This sequence of Loading and Closing a ".mat" file for every element in an iterative solution in COMSOL solver seems too much time consuming.

I'm concerned that the developpers consider this as a feature of comsol, rather than an issue to be corrected.

Please post your solutions if you find another workaround!.

----------------
Frank
[QUOTE] Hi Peter You are right. A new Matlab will be opened by the Comsol solver. Solutions to your problems can be 1) Skip calling an external function for your material data. If your material data only depends on x,y, and other independent variables (not part of the solution) you could simply use Comsol's interpolation table functionality. Simply export your data in a format that is readable by Comsol and you should get the maximum performance (since Matlab doesn't have to be called by the solver) 2) Load the image data repeatedly in your user m-file. On a modern computer it shouldn't take (very) long. 3) Use a persistent variable to store the image data after having loaded it once. Lars Gregersen Comsol Denmark [/QUOTE] These solutions are good "workaround", but they do not solve a more important issue in 4.1. When doing a standard topological optimization, the material is often defined by a complicated function "myfunction.m" that depends on [T,x,y, and other independant variables]. For each design solved during the optimization, the "main Matlab script" changes the value of the "independant variables", and then it calls Comsol in which the material is "myfunction.m". See below the 2 matlab functions. ==================================================== %% main Matlab script .m Global (independant variables here) while (optimum design is not reached) change "independant variables" define comsol model, where the material is "myfunction.m" solve the colsol model check if the solution correspond to an optimal design end ==================================================== %% material script function output= myfunction(T,x,y).m Global (independant variables here) calculate output using (T,x,y, and independant variables) return output ==================================================== In 3.5, "myfunction" could receive the value of those changing "independant variable" by defining them as Global variables in the "main Matlab script" and in "myfunction". (see above) Now in 4.1, the script shown above doesnt work. The material "myfunction" cannot receive the values of those changing "independant variables", because "main Matlab script" and "myfunction" run in completely different Matlab session. The only solutions I see is to repeat the sequence of (i) changing the "independant variables", (ii) writing the "independant variables" in a file "myfile.mat", (iii) loading "myfile.mat" in "myfunction". This sequence of Loading and Closing a ".mat" file for every element in an iterative solution in COMSOL solver seems too much time consuming. I'm concerned that the developpers consider this as a feature of comsol, rather than an issue to be corrected. Please post your solutions if you find another workaround!. ---------------- Frank

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2013年3月4日 GMT-5 14:48
I am currently dealing with the same problem listed by Frank, any suggestions?
I am currently dealing with the same problem listed by Frank, any suggestions?

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.