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.

help with matlab comsol - how to work with model physics

Please login with a confirmed email address before reporting spam

Hi, i have been working on COMSOL for several months and only recently started trying to script my work.
I am trying to run multiple cases of electrical current studies, which differ by different settings in model physics. Specifically, setting different surface components to be at certain electrical potential.
I looked through the help api I have on hand, but found very limited commands working with model physics. I wonder if anyone can help me out?
Thanks a lot.

For example, refer to the attached image.
I want to run 3 cases of study. First, setting top small square to be at 10V. Run the study, and gather data at certain locations; then setting two other small square to be at 10V respectively.
This is merely an example. The actual model that I am working with involves many more cases and proved impractical running by hand.


4 Replies Last Post 2 avr. 2012, 06:13 UTC−4

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 27 mars 2012, 02:41 UTC−4
What I usually do in a case such as yours, is:
* Start with the full model or the simpler example model.
* Load it in the Comsol GUI.
* Reset history (from the File menu).
* Make the modification of interest, such as reassigning the boundary conditions in your case.
* Save the model as Model M-File (from the File menu).
The API commands pertaining to the modifcation of interest are then the last one(s) in the resulting .m file. It's usually rather straightforward to generalize those commands to apply to all other cases as well. It may help, though, to define the appropriate selections in the Comsol model first.

Eventually, the Matlab loop will be quite short:
* Load the original Comsol model using model = mphload('filename.mph').
* Apply the modifications to the (Matlab) model object as examplified by the .m file.
* If the geometry was changed (not the case in your example), re-analyze geometry (model.geom('geom1').run) and re-mesh (model.mesh('mesh1').run).
* Run the simulation (model.sol('sol1').runAll).
* Postprocess (mpheval, mphint, etc.) or save the model to disk (mphsave).
* Continue to loop through the other cases.
Tags such as "sol1" for the study or "mesh1" may be different in any given model.

Note that in your case it may be sufficient to run a "port scan" (see the AC/DC module's documentation), completely within the Comsol GUI without the need for Matlab scripting.

What I usually do in a case such as yours, is: * Start with the full model or the simpler example model. * Load it in the Comsol GUI. * Reset history (from the File menu). * Make the modification of interest, such as reassigning the boundary conditions in your case. * Save the model as Model M-File (from the File menu). The API commands pertaining to the modifcation of interest are then the last one(s) in the resulting .m file. It's usually rather straightforward to generalize those commands to apply to all other cases as well. It may help, though, to define the appropriate selections in the Comsol model first. Eventually, the Matlab loop will be quite short: * Load the original Comsol model using model = mphload('filename.mph'). * Apply the modifications to the (Matlab) model object as examplified by the .m file. * If the geometry was changed (not the case in your example), re-analyze geometry (model.geom('geom1').run) and re-mesh (model.mesh('mesh1').run). * Run the simulation (model.sol('sol1').runAll). * Postprocess (mpheval, mphint, etc.) or save the model to disk (mphsave). * Continue to loop through the other cases. Tags such as "sol1" for the study or "mesh1" may be different in any given model. Note that in your case it may be sufficient to run a "port scan" (see the AC/DC module's documentation), completely within the Comsol GUI without the need for Matlab scripting.

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 28 mars 2012, 03:11 UTC−4
Thank you very much for the detailed response!
Could you elaborate a little more on the "port scan?" I tried to search in my COMSOL documentation and online, but the closest function I found was "port sweep" in the EM Waves Interface, about which I am not sure how to perform the tasks I desire...
Thank you very much for the detailed response! Could you elaborate a little more on the "port scan?" I tried to search in my COMSOL documentation and online, but the closest function I found was "port sweep" in the EM Waves Interface, about which I am not sure how to perform the tasks I desire...

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 28 mars 2012, 03:24 UTC−4
You're right, "port sweep" is the key word, not port scan. I remembered it wrong. It's described in the AC/DC module's documentation. The purpose of a port sweep is to determine "lumped parameters" such as the capacitance matrix. Basically, how it works is: you define several electrodes (ports/terminals) and then run a parametric sweep where in each run only one port is at a given potential (say 10 V), while all the other ones are on ground. This is similar to what you described above, which is why I suggested you look into it.
You're right, "port sweep" is the key word, not port scan. I remembered it wrong. It's described in the AC/DC module's documentation. The purpose of a port sweep is to determine "lumped parameters" such as the capacitance matrix. Basically, how it works is: you define several electrodes (ports/terminals) and then run a parametric sweep where in each run only one port is at a given potential (say 10 V), while all the other ones are on ground. This is similar to what you described above, which is why I suggested you look into it.

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2 avr. 2012, 06:13 UTC−4
hi I could not get the parametric sweep to do what I wanted, but the save as M-model really simplifies the process, thanks a lot.
however, does it ever happen to you that you get different solution result running in matlab than running in comsol?
i first ran one case in comsol, export the data. then saved the exact same file to a .m, and in matlab console ran it again. i literarily got somewhat similar but different data set...
if it were minor differences i would not even bother, but than it occurs when in comsol it gave me the solution very quickly, in matlab it complains about "divergence of linear iterations."
hi I could not get the parametric sweep to do what I wanted, but the save as M-model really simplifies the process, thanks a lot. however, does it ever happen to you that you get different solution result running in matlab than running in comsol? i first ran one case in comsol, export the data. then saved the exact same file to a .m, and in matlab console ran it again. i literarily got somewhat similar but different data set... if it were minor differences i would not even bother, but than it occurs when in comsol it gave me the solution very quickly, in matlab it complains about "divergence of linear iterations."

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.