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.
Work on COMSOL through MATLAB?
Posted 03.05.2011, 08:36 GMT-4 Interfacing, Studies & Solvers Version 3.5a, Version 4.2 27 Replies
Please login with a confirmed email address before reporting spam
I would like to run COMSOL through MATLAB but don't understand how? For example when I have saved a COMSOL simulation as M-file and tries to open it in MATLAB it won't open and I am not sure how to open the program in COMSOL in MATLAB. I tried to read the information in help but I don't get it. I didn't install the programs on this computer so I am not sure how it was installed.
Basically my end goal is that I want to save the solutions from COMSOL in MATLAB to be able to use them for further calculations in MATLAB. Is it possible and how can I do this?
For example, I have a certain parameter (a refractive index as input) that I sweep and get different effective refractive indexes. I would like to store these in MATLAB and then use it for calculations in MATLAB. How can I do this?
Edit:
I have MATLAB R2010B 64BIT, COMSOL4.1, MATLAB was installed first. I uses a Mac computer.
Many thanks for any help!
Please login with a confirmed email address before reporting spam
The part you are missing is that you need to run some code to connect Comsol to Matlab. There are two ways to do this:
1. Use the "Comsol with Matlab" application that came with Comsol when you installed it. If you're using Windows you can find this in the same Start menu group that the main Comsol application is in.
2. You can start a Comsol server manually (from the Comsol Start menu group or installation folder) and then, in Matlab, run the command "mphstart;" to connect to Comsol.
You may find that the first time you start the server, it asks for a username and password. I don't think it matters what you use here, just use your normal username and password.
Once you have connected to Comsol, you should be able to run the m-file you saved from Comsol. Then you want to get some results out of it. At this point I need to refer you to the Comsol API Guide, which you find from Help > Documentation within Comsol. This guide lists all the commands you can use in Matlab, and you should be able to find the results you want to access listed there.
See how you get on, and if any of this doesn't work, let me know some more detail and I'll try and help!
Cheers,
matt
Edit: Oops, I didn't see your edit!
On a Mac, you should see the "Comsol with Matlab" application in the /Applications/Comsol folder, and if you need to run the server manually, I think it's in /Applications/Comsol/Comsol.app/Contents/bin from memory... I don't have a Mac with me.
Please login with a confirmed email address before reporting spam
However I still can't run the m-file. It says,
"??? Undefined function or method.
Simulation_Waveguide_2D_SV2_Sweep' for input arguments of type
'char'."
What does this mean?
Please login with a confirmed email address before reporting spam
1. What is the name of the m-file?
2. How are you running/calling the m-file?
3. Can you attach the m-file, or type in the first five lines or so?
Please login with a confirmed email address before reporting spam
It sounds like you're trying to run the m-file as a function instead of a script. Three questions:
1. What is the name of the m-file?
2. How are you running/calling the m-file?
3. Can you attach the m-file, or type in the first five lines or so?
1. Simulation_Waveguide_Crytophane_2D_SV2_Sweep HR_ET1e-7.m
2. Opening the m-file and then push the run button.
3.
function out = model
%
% Simulation_Waveguide_Crytophane_2D_SV2_Sweep HR_ET1e-7.m
%
% Model exported on May 3 2011, 16:05 by COMSOL 4.1.0.88.
import com.comsol.model.*
import com.comsol.model.util.*
model = ModelUtil.create('Model');
model.modelPath('/Users/sli027/Desktop/The PhD Project/PHD - SIMULATIONS/PhD Simulation 1 - Waveguide Cryptophane/04.28.2011');
model.modelNode.create('mod1');
model.geom.create('geom1', 2);
model.mesh.create('mesh1', 'geom1');
model.physics.create('emw', 'ElectromagneticWaves', 'geom1');
model.study.create('std1');
model.study('std1').feature.create('mode', 'ModeAnalysis');
model.geom('geom1').feature.create('r1', 'Rectangle');
model.geom('geom1').run('r1');
model.geom('geom1').run('r1');
model.geom('geom1').feature.create('r2', 'Rectangle');
model.geom('geom1').run('r2');
model.geom('geom1').run('r2');
etc
Please login with a confirmed email address before reporting spam
1. Simulation_Waveguide_Crytophane_2D_SV2_Sweep HR_ET1e-7.m
The problem seems to be the space in the file name. Matlab is trying to run the command "Simulation_Waveguide_Crytophane_2D_SV2_Sweep" with parameter "HR_ET1e-7.m"
Try renaming the file to Simulation_Waveguide_Crytophane_2D_SV2_Sweep_HR_ET1e-7.m
Please login with a confirmed email address before reporting spam
I am still trying to open a m-file saved in COMSOL in MATLAB. When I later run this m-file in MATLAB I get the following error message (after a while thinking);
?? Java exception occurred:
Exception:
com.comsol.nativeutil.properties.InvalidPropertyValueException: Invalid
property value
Messages:
Invalid property value
- Property: windowtitle
X is Y#Window title#a string
Stack trace:
at com.comsol.nativeutil.properties.Property.c(Unknown Source)
at com.comsol.nativeutil.properties.ac.set(Unknown Source)
at com.comsol.model.impl.PropValueImpl.set(Unknown Source)
at com.comsol.model.impl.PropFeatureImpl.a(Unknown Source)
at com.comsol.model.impl.PropFeatureImpl$q.a(Unknown Source)
at com.comsol.model.impl.PropFeatureImpl$q.execute(Unknown Source)
at com.comsol.model.clientserver.ClientManagerImpl$d.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Error in ==> WGsimul_slab_noH20_withoutPML at 460
model.result('pg2').set('windowtitle', '');
What is the problem here?
I have version 4.1 COMSOL and MATLAB R2010B.
Please login with a confirmed email address before reporting spam
Error in ==> WGsimul_slab_noH20_withoutPML at 460
model.result('pg2').set('windowtitle', '');
What is the problem here?
I don't really know what's going wrong - those Java errors are never particularly informative. However, it has pointed out the line of code that is causing the problem, and as this code only sets a blank window title and this isn't working, I would advise you to comment out or delete that line 460 and try running it again.
Please login with a confirmed email address before reporting spam
Error in ==> WGsimul_slab_noH20_withoutPML at 460
model.result('pg2').set('windowtitle', '');
What is the problem here?
I don't really know what's going wrong - those Java errors are never particularly informative. However, it has pointed out the line of code that is causing the problem, and as this code only sets a blank window title and this isn't working, I would advise you to comment out or delete that line 460 and try running it again.
Thank you for answering, my problem now with MATLAB is that it does not stop. When I clicked run it still calculating and its been almost over an hour. Is that really normal?
Please login with a confirmed email address before reporting spam
Thank you for answering, my problem now with MATLAB is that it does not stop. When I clicked run it still calculating and its been almost over an hour. Is that really normal?
Also, I can't access the help documentation earlier, is the site/server down?
Hi Susan,
If the model doesn't take long to run in Comsol but takes ages to run in Matlab, something is going wrong. The best way to troubleshoot the problem is to manually run each command from the m-file in the immediate window in Matlab, and see when the problem occurs. Alternatively, you can edit the m-file and put some disp() commands in so you know how far the code has got.
The Comsol documentation is hosted on your local computer, but Comsol needs to be open for it to work. Alternatively, you can find the PDF versions in the installation directory. The Matlab documentation is built into Matlab itself, I think, just hit F1.
Please login with a confirmed email address before reporting spam
You have suggested two ways to connect COMSOL with MATLAB. The first one worked for me easily. However, my task requires that COMSOL can be opened from MATLAB through a command (second way). I am not able to get this working. Please elaborate and help me. I am a COMSOL 3.5a user.
Please login with a confirmed email address before reporting spam
The natural ways are two, as far as I know:
Open "COMSOL 3.5a with MATLAB". Once in MATLAB, you can always open COMSOL simply with the command "comsol".
Or open "COMSOL Multiphysics 3.5a". Once in COMSOL you could connect to MATLAB by clicking in "File" / "Client/Server/MATLAB" / "Connect to MATLAB".
But, previously, in both cases you have to tell COMSOL where MATLAB is: or at installation time, or later in the file "comsol.opts", which is in "bin" directory inside your COMSOL 3.5a installation. There you should find a line such as:
mlroot=C:\Program Files\MATLAB\R2007b
Of course, that is the directory where COMSOL can find MATLAB.
Bye.
Please login with a confirmed email address before reporting spam
Thanks for clarifying all the information. As I have already quoted that my task needs that COMSOL can be opened from MATLAB through some commands. Here I elaborate my task:
My basis workspace is in MS Excel where I write different codes (macros). In one of my codes, I need to solve some transport phenomena for which I need help of COMSOL. MS Excel can be linked with MATLAB in the similar way as COMSOL can. Now the problem arises that both -COMSOL and Excel- do not have a common interface of MATLAB, rather they have different MATLAB desktops. The solution which I could think of that COMSOL can be called through some command in the MATLAB desktop which is already linked with Excel.
Is it possible?
Please login with a confirmed email address before reporting spam
Hi Jesus,
Thanks for clarifying all the information. As I have already quoted that my task needs that COMSOL can be opened from MATLAB through some commands. Here I elaborate my task:
My basis workspace is in MS Excel where I write different codes (macros). In one of my codes, I need to solve some transport phenomena for which I need help of COMSOL. MS Excel can be linked with MATLAB in the similar way as COMSOL can. Now the problem arises that both -COMSOL and Excel- do not have a common interface of MATLAB, rather they have different MATLAB desktops. The solution which I could think of that COMSOL can be called through some command in the MATLAB desktop which is already linked with Excel.
Is it possible?
Hi Ashwini,
In my code I use a "system" command in Matlab, which basically runs a command as if it were running in the command prompt. Here's an example:
[status,result] = system('C:\COMSOL41\bin\win64\comsolserver.exe -port 2036 &']);
You'll have to change the path for your own version of comsolserver.exe. The "&" at the end is important as it tells Matlab to continue processing, and not wait for the Comsol server to quit.
Then I call "mphstart" and continue working with the server as normal, which you say works.
Hope this helps,
matt
Please login with a confirmed email address before reporting spam
Thanks. The quoted command can open the server which reflects in the form of black colored command screen (cmd.exe) saying that "Server successfully started using port 2036". However, "mphstart" didn't work. I am calling it in the MATLAB command prompt. Am I doing it right?
Thanks,
Ashwini
Please login with a confirmed email address before reporting spam
Hi Matt,
Thanks. The quoted command can open the server which reflects in the form of black colored command screen (cmd.exe) saying that "Server successfully started using port 2036". However, "mphstart" didn't work. I am calling it in the MATLAB command prompt. Am I doing it right?
Thanks,
Ashwini
Hi Ashwini,
Can you give a bit more information about the problem than just "didn't work"? What happens when you run mphstart? Does it give an error message? Does the program hang and stop responding? Does the Comsol server window report any activity?
Have you tried running "mphstart(2036);" to explicitly define the port?
Have you included the Comsol libraries in your Matlab code by the commands:
import com.comsol.model.*
import com.comsol.model.util.*
Please login with a confirmed email address before reporting spam
Thanks. After starting the server when I type "mphstart" in MATLAB command prompt, it says,
??? Undefined function or variable 'mphstart'.
Program do not hang and Comsol server do not report any activity.
Running "mphstart(2036) also gives an error message as follows:
??? Undefined function or method 'mphstart' for input arguments of type 'double'.
Regards,
Ashwini
Please login with a confirmed email address before reporting spam
Hi Matt,
Thanks. After starting the server when I type "mphstart" in MATLAB command prompt, it says,
??? Undefined function or variable 'mphstart'.
Program do not hang and Comsol server do not report any activity.
Running "mphstart(2036) also gives an error message as follows:
??? Undefined function or method 'mphstart' for input arguments of type 'double'.
Regards,
Ashwini
Those messages mean that Matlab doesn't know where to find the Comsol library of commands. Have you used the "import" commands from above? These need to be included in every script file that interacts with Comsol.
Please login with a confirmed email address before reporting spam
Previously, I was not using this "import" commands. Now, I included the two you have mentioned. However the same error message appears as I run the command "mphstart". TO remind you, I am working with COMSOL 3.5a.
Regards,
Ashwini
Please login with a confirmed email address before reporting spam
Hi Matt,
Previously, I was not using this "import" commands. Now, I included the two you have mentioned. However the same error message appears as I run the command "mphstart". TO remind you, I am working with COMSOL 3.5a.
Regards,
Ashwini
Good thought about the version - I only have experience with connecting Matlab to version 4.0 onwards. I think I remember that the mechanism changed and the "mphstart" was only for version 4.0, but I could be wrong.
When you run Comsol with Matlab, do you need to run any commands in Matlab first to connect to Comsol, or does it work right away?
It may be that someone with experience using Matlab with 3.5a can help - how do you connect an existing Matlab session to an existing Comsol server session manually?
Please login with a confirmed email address before reporting spam
When I run Comsol with Matlab, I didn't need to enter any other command. I can run the scripts right away.
Ashwini
Please login with a confirmed email address before reporting spam
"using mphstart"
As Matt said rightly," Matlab doesn't know where to find the Comsol library of commands".
Firstly set your current folder to C:\COMSOL42\mli
In fact, mphstart is in "mli folder". Once you set and then type mphstart in command prompt, things will work..(make sure comsol server is ON before using mphstart)..
Attachments:
Please login with a confirmed email address before reporting spam
www.rz.uni-karlsruhe.de/~hf118/tutorial_comsol_matlab_livelink.pdf
It is an example of looping over a parameter of a COMSOL model via MATLAB
Please login with a confirmed email address before reporting spam
I solved set of ODEs using matlab. I know we can solve the .m files in comsol. but,I want to solve a solid conduction problem (PDE) coupled with the above ODE set . I want to solve both combinely. IS it possible . how to couple them in the COMSOL. please suggest
Please login with a confirmed email address before reporting spam
Hai,
I solved set of ODEs using matlab. I know we can solve the .m files in comsol. but,I want to solve a solid conduction problem (PDE) coupled with the above ODE set . I want to solve both combinely. IS it possible . how to couple them in the COMSOL. please suggest
Hello,
Comsol has the functionality to solve the ODEs; I don't know whether this fits in your problem but you can put ODEs in drop down menu 'physics' and then'global equations' (Comsol 3.5a). Alternately, you can solve the equations iteratively where you solve ODEs in matlab and PDEs in Comsol and communicate the solutions. You have to do a little bit of coding in matlab to define the tolerance and looping for the iteration. Hope this helps.
All the best,
Ashwini
Please login with a confirmed email address before reporting spam
thanks for the information.
Please login with a confirmed email address before reporting spam
I have comsol 4.3b.
Please help me out.
Please login with a confirmed email address before reporting spam
Warning: The encoding 'windows-1255' is not supported.
See the documentation for FOPEN.
> In C:\Program Files\COMSOL\COMSOL43b\mli\mphstart.p>mphstart at 107
please help me out..
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.