Control COMSOL simulation with MATLAB code using LiveLink

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.setto 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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值