This article talks about how to editing\controlling COMSOL using MATLAB code to generate and save massive data.
Step 1: Set up the simulation in COMSOL
Here, I am setting up COMSOL to simulate particle tracing in fluid flow. There is no need to detail the simulation. You can select your customized simulation sets for this part.
After finishing the simulation set, click Files -> save as -> choose .m file.
Step 2: Modify your code in MATLAB.
If everything goes right, your .m file be compiled as a function
However, you need another m file to call this function. You also need to adjust the structure of this function to parametrize some of the values so that you can change them externally.
Personally, I would like to replace all these tedious things with one line to load your model and all its info into your MATLAB:
model = mphopen('<your file>.mph');
After that, usemodel.param.set
to change the values in your COMSOL model. Note: usenum2str
to transfer your values into string so that they can be recognized by COMSOL.
You will definitely remove the redundant codes that you don’t need (i.e. codes generating geometries since the Geometries is already there and you don’t want to do it again). Also removing many codes relating to model initiation. Once the model is set up in step 1, there is no need to initiate them again.
You need to revaluate the derived parameters and save them for each simulations, do keep those codes.
#Update, it seems that there is some bugs for COMSOL to pass values to files. Here is the code that guarantee that the tables can be saved in the correct way.
model.result.numerical('par1').set('table', 'tbl1');
model.result.numerical('par1').computeResult();
model.result.numerical('par1').setResult;
model.result.numerical('par1').appendResult;
model.result.numerical('par2').set('table', 'tbl2');
model