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.

Is the Comsol Model Class a Handle Class in Matlab? Can/should I pass it as "call by reference"?

Please login with a confirmed email address before reporting spam

Hey there,

I'm new to Comsol and I'm developing my first Matlab application using LiveLink. As I understand the object oriented concept in Matlab, there are two types of classes. Value Classes and Handle Classes. Instances of the latter can be used as function argument in the sense of "call by reference".

Which of those two types of Matlab classes belongs the Comsol Model Object to?
If I load a Comsol model in Matlab using the Matlab code

model = mphload(mymodel.mph, 'My Model');

can I then pass the above variable to some Matlab function "myFun()" as call by reference, modify in that function the Model Object and see the modifications in the main Matlab script when returning from "myFun()"?

Is that all save to do or what is the best practice to structure and modularize Matlab code when using the Comsol Model Object?

Thanks a lot in advance,
Joerg

3 Replies Last Post 2013年3月17日 GMT-4 09:01

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2013年3月16日 GMT-4 08:14
Hello Joerg,

I'm not quite sure I understood your question... perhaps it would be better if you could explain exactly what you wanna do.

Mind that the model object is just a sequence of instructions for Comsol. Basically what you do with LiveLink is giving instructions to Comsol via Matlab instead of the classic Comsol's GUI. So, ether if you have you model object in one, two, or hundred .m-file does not matter.

When you import a model with mphload, you store in Matlab's workspace a (more ore less) structure which contains all the information of that particular model. Extracting data from this structure you can: play with built-in functions of matlab but also enrich the the model itself following the syntax of the model object (API user manual would be your reference).

hope it helps
Mattia
Hello Joerg, I'm not quite sure I understood your question... perhaps it would be better if you could explain exactly what you wanna do. Mind that the model object is just a sequence of instructions for Comsol. Basically what you do with LiveLink is giving instructions to Comsol via Matlab instead of the classic Comsol's GUI. So, ether if you have you model object in one, two, or hundred .m-file does not matter. When you import a model with mphload, you store in Matlab's workspace a (more ore less) structure which contains all the information of that particular model. Extracting data from this structure you can: play with built-in functions of matlab but also enrich the the model itself following the syntax of the model object (API user manual would be your reference). hope it helps Mattia

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2013年3月16日 GMT-4 15:19
Hey Mattia,

first of all thank you very much for your answer. The things you've explained are all clear. Today I've managed to create and/or loaded mph-models in Matlab using LiveLink and by now I know how to use the COMSOL-API through Matlab. My question was a different one. Sorry for not being precise enough. I will try to clarify my question.

Let's say I write the following code in two different Matlab files.

"main.m":
=================
model = mphload("mymodel.mph");
model.geom('geom1').feature.create('pt1', 'Point');
model.geom('geom1').feature('pt1').set('p', {'10'; '10'; '10'});
myFun(model);
disp('done');
=================

"myFun.m":
=================
function myFun(model)
model.geom('geom1').feature.create('pt2', 'Point');
model.geom('geom1').feature('pt2').set('p', {'20'; '20'; '20'});
end
=================

Then, what happens? The script "main.m" and the function "myFun" use different workspaces and for primitive build-in data types Matlab would only pass a copy of "model" as an argument to the function "myFun". For classes it is a bit different.

More precisely I have the following questions.

1.) Does the variable "model" in both files/workspaces point to the same object / data structure in memory? Or is the variable only copied?

2.) Where is the data for the model located anyway? In the Matlab workspace or in COMSOL? In other words, does LiveLink only provide an interface for invoking remote procedures in COMSOL or is there actually data being mirrored in the Matlab workspace?

3.) When the function "myFun" returns, does the model object in "main.m" also include the point "pt2"?

4.) Is it save to use code like the above or should I do it differently? How about performance?

TIA,
Joerg
Hey Mattia, first of all thank you very much for your answer. The things you've explained are all clear. Today I've managed to create and/or loaded mph-models in Matlab using LiveLink and by now I know how to use the COMSOL-API through Matlab. My question was a different one. Sorry for not being precise enough. I will try to clarify my question. Let's say I write the following code in two different Matlab files. "main.m": ================= model = mphload("mymodel.mph"); model.geom('geom1').feature.create('pt1', 'Point'); model.geom('geom1').feature('pt1').set('p', {'10'; '10'; '10'}); myFun(model); disp('done'); ================= "myFun.m": ================= function myFun(model) model.geom('geom1').feature.create('pt2', 'Point'); model.geom('geom1').feature('pt2').set('p', {'20'; '20'; '20'}); end ================= Then, what happens? The script "main.m" and the function "myFun" use different workspaces and for primitive build-in data types Matlab would only pass a copy of "model" as an argument to the function "myFun". For classes it is a bit different. More precisely I have the following questions. 1.) Does the variable "model" in both files/workspaces point to the same object / data structure in memory? Or is the variable only copied? 2.) Where is the data for the model located anyway? In the Matlab workspace or in COMSOL? In other words, does LiveLink only provide an interface for invoking remote procedures in COMSOL or is there actually data being mirrored in the Matlab workspace? 3.) When the function "myFun" returns, does the model object in "main.m" also include the point "pt2"? 4.) Is it save to use code like the above or should I do it differently? How about performance? TIA, Joerg

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2013年3月17日 GMT-4 09:01
Hej Joerg,

now I do understand what you mean and perhaps I can answer you:

1.) Yes, your variable 'model' would point to the same Comsol object.

2.) My understanding is that when you run LiveLink for Matlab you still run Comsol, but instead of use your mouse (like in Comsol GUI) to instruct Comsol you use another syntax described in API's guide (as you know). The memory used by LiveLink is simply RAM memory....

3) Yes it does include 'pt2'. You can very verify it very easily: run only main.m, open Comsol GUI and import the model from the server (if you don't know how to do that look in LiveLink user guide), where you'll see only 'pt1'. Go back to Matlab, run 'myFun' and re-import the model in Comsol. Voilà you've got to points :-)

4.) I'd say yes. it is safe and it is the advantage of defining you model in Matlab!

Moreover, whenever you code something about your variable 'model', this command is sent directly to Comsol's server. So, you don't have to have the variable 'model' as output of your 'myFun'. In fact Matlab doesn't work direcly on the variable 'model', whenever you import data from Comsol server ( e.g. when you write model = mphload(..etc..), or mphmatrix..) you should still use comsol's syntax: all the mphfunctions are nothing but matlab functions within a sequence of commands that you find in API user guide. For instance, to get any vector from Comsol server you'd like to write somethink like that:
vect = model.sol('solver').feature('assemble').getVector
now you've importeded an array 'vect' that Matlab can handle.

Hope this helps,
Mattia
Hej Joerg, now I do understand what you mean and perhaps I can answer you: 1.) Yes, your variable 'model' would point to the same Comsol object. 2.) My understanding is that when you run LiveLink for Matlab you still run Comsol, but instead of use your mouse (like in Comsol GUI) to instruct Comsol you use another syntax described in API's guide (as you know). The memory used by LiveLink is simply RAM memory.... 3) Yes it does include 'pt2'. You can very verify it very easily: run only main.m, open Comsol GUI and import the model from the server (if you don't know how to do that look in LiveLink user guide), where you'll see only 'pt1'. Go back to Matlab, run 'myFun' and re-import the model in Comsol. Voilà you've got to points :-) 4.) I'd say yes. it is safe and it is the advantage of defining you model in Matlab! Moreover, whenever you code something about your variable 'model', this command is sent directly to Comsol's server. So, you don't have to have the variable 'model' as output of your 'myFun'. In fact Matlab doesn't work direcly on the variable 'model', whenever you import data from Comsol server ( e.g. when you write model = mphload(..etc..), or mphmatrix..) you should still use comsol's syntax: all the mphfunctions are nothing but matlab functions within a sequence of commands that you find in API user guide. For instance, to get any vector from Comsol server you'd like to write somethink like that: vect = model.sol('solver').feature('assemble').getVector now you've importeded an array 'vect' that Matlab can handle. Hope this helps, Mattia

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.