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.

Saving simulated model as mat file in matlab, livelink

Please login with a confirmed email address before reporting spam

Hi, I'm using matlab 2022 and comsol 6.1 ver.

I'm simulating comsol with matlab via livelink for various parameters with for loops. I import a mph file into matlab as 'model=mphopen("filename.mph"). Then I can access the result of simulation by accessing the model structure in matlab.

However, it is not possible to save the model structure as mat.file. so it's not possible to post processing once I closed them. According to the following link ( https://www.comsol.com/forum/thread/13088/saving-comsol-model-from-matlab-livelink?last=2015-07-01T14:43:11Z ), we can save the simulated model as mph.file and we can access it as post processing at any time later. However, saving each model as mph file requires too much memory space.. If I save it as m.file, I should re-run everytime to obtain the result again. Does anyone know how to save the simulated model in matlab as m.file, so I can access them easily at anytime later?


3 Replies Last Post 24 ott 2023, 02:32 GMT-4
Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 1 year ago 11 ott 2023, 03:00 GMT-4

Hi Jehyeok

If you need to perform postprocessing on the entire solution of a Comsol model the only way to do that is to have access to the solved Comsol model. The only way to save such a model is to save it as an MPH-file.

If you have an idea of what data you need for postprocessing I suggest that you extract that data using mphinterp or one of the many other functions for extracting numerical data. Once such data have been extracted you can save the data in MAT-files.

If you to extract plots as plotdata you can do: pd = mphplot(model, ...) which will return the data in the pd-variable. Thd pd-variable can be saved in a MAT-file. You can inspect the data in pd or you can perform a plot later using this command: mphplot(pd)

-------------------
Lars Gregersen
Comsol Denmark
Hi Jehyeok If you need to perform postprocessing on the entire solution of a Comsol model the only way to do that is to have access to the solved Comsol model. The only way to save such a model is to save it as an MPH-file. If you have an idea of what data you need for postprocessing I suggest that you extract that data using mphinterp or one of the many other functions for extracting numerical data. Once such data have been extracted you can save the data in MAT-files. If you to extract plots as plotdata you can do: pd = mphplot(model, ...) which will return the data in the pd-variable. Thd pd-variable can be saved in a MAT-file. You can inspect the data in pd or you can perform a plot later using this command: mphplot(pd)

Please login with a confirmed email address before reporting spam

Posted: 1 year ago 23 ott 2023, 11:15 GMT-4
Updated: 1 year ago 23 ott 2023, 11:20 GMT-4

Hi Jehyeok

If you need to perform postprocessing on the entire solution of a Comsol model the only way to do that is to have access to the solved Comsol model. The only way to save such a model is to save it as an MPH-file.

If you have an idea of what data you need for postprocessing I suggest that you extract that data using mphinterp or one of the many other functions for extracting numerical data. Once such data have been extracted you can save the data in MAT-files.

If you to extract plots as plotdata you can do: pd = mphplot(model, ...) which will return the data in the pd-variable. Thd pd-variable can be saved in a MAT-file. You can inspect the data in pd or you can perform a plot later using this command: mphplot(pd)

Thank you for your answer, Lars! Do you have any plan to add the function to save the whole simulated model as mat file? If I simulate with for loops, it will save mph files as many as the for loops. Therefore, it would be very helpful if I can save each model as an element of a cell.

For example,

clear
format long
import com.comsol.model.*
import com.comsol.model.util.*
ModelUtil.showProgress(true);

model=mphload('file.mph');
modelArray = cell(N);

for i 1:N
    model.param.set('X parameter',i);
    model.study('std1').run;
    modelArray{i}=model;
end

Then, postprocessing at anytime later with i-th element of modelArray.

>Hi Jehyeok > >If you need to perform postprocessing on the entire solution of a Comsol model the only way to do that is to have access to the solved Comsol model. **The only way to save such a model is to save it as an MPH-file.** > >If you have an idea of what data you need for postprocessing I suggest that you extract that data using mphinterp or one of the many other functions for extracting numerical data. Once such data have been extracted you can save the data in MAT-files. > >If you to extract plots as plotdata you can do: >pd = mphplot(model, ...) >which will return the data in the pd-variable. Thd pd-variable can be saved in a MAT-file. You can inspect the data in pd or you can perform a plot later using this command: >mphplot(pd) Thank you for your answer, Lars! Do you have any plan to add the function to save the whole simulated model as mat file? If I simulate with for loops, it will save mph files as many as the for loops. Therefore, it would be very helpful if I can save each model as an element of a cell. For example, clear format long import com.comsol.model.* import com.comsol.model.util.* ModelUtil.showProgress(true); model=mphload('file.mph'); modelArray = cell(N); for i 1:N model.param.set('X parameter',i); model.study('std1').run; modelArray{i}=model; end Then, postprocessing at anytime later with i-th element of modelArray.

Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 1 year ago 24 ott 2023, 02:32 GMT-4
Updated: 1 year ago 7 nov 2023, 04:17 GMT-5

Hi Jehyeok

Your code will not work. Not today or in the future.

The reason is that the model variable doesn't contain the model at all. It is simply a variable that points to a model which lives in the COMSOL Multiphysics server.

Extracting results from a Comsol model requires that the model settings, its geometry and mesh as well as the physics and shape functions are processed when you ask Comsol to return some data. There is no way that all this information can be saved into a MAT-file and expect that the MAT-file would be smaller than the corresponding MPH-file.

You can perhaps reduce the size of your MPH-files by using some of the suggestions mentioned on one of these pages:

https://www.comsol.com/support/learning-center/article/How-to-Reduce-the-Size-of-Your-Model-File-59711

https://www.comsol.com/support/learning-center/article/Reducing-the-Amount-of-Solution-Data-Stored-in-a-Model-67151.

For mphsave you can use the optimize argument.

The best option remains to extract any numerical results just after having solved the model.

-------------------
Lars Gregersen
Comsol Denmark
Hi Jehyeok Your code will not work. Not today or in the future. The reason is that the model variable doesn't contain the model at all. It is simply a variable that points to a model which lives in the COMSOL Multiphysics server. Extracting results from a Comsol model requires that the model settings, its geometry and mesh as well as the physics and shape functions are processed when you ask Comsol to return some data. There is no way that all this information can be saved into a MAT-file and expect that the MAT-file would be smaller than the corresponding MPH-file. You can perhaps reduce the size of your MPH-files by using some of the suggestions mentioned on one of these pages: https://www.comsol.com/support/learning-center/article/How-to-Reduce-the-Size-of-Your-Model-File-59711 https://www.comsol.com/support/learning-center/article/Reducing-the-Amount-of-Solution-Data-Stored-in-a-Model-67151. For mphsave you can use the optimize argument. The best option remains to extract any numerical results just after having solved the model.

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.