COMSOL with matlab---Quick Start

COMSOL 是一个多物理场仿真软件,它的一个最大的问题就是各个版本之间的不兼容问题。解决这个问题的方法就是把COMSOL保存中m文件。m文件实际上是一种录制宏,它保存了你的几乎所有操作,可以用matlab来打开。COMSOL与matlab的连接需要 LiveLink™ for MATLAB®,需要在安装COMSOL时选择安装。具体的安装步骤这里不再赘述。关于COMSOL与matlab的连接可以参考官方的user‘s guide:
https://www.researchgate.net/profile/Mohan_Pradhan/post/Connecting_matlab_with_COMSOL_Livelink/attachment/59d626c079197b807798506e/AS%3A323111042387968%401454047083245/download/LiveLinkForMATLABUsersGuide.pdf
另外官方提供了一个例子教学视频https://cn.comsol.com/blogs/using-matlab-functions-comsol-multiphysics-models/可以供参考。

Quick Start

1. Start COMSOL with matlab

• On Windows® use the COMSOL with MATLAB shortcut icon that is created on the
desktop after the automatic installation. A link is also available in the Windows start
menu under All Programs>COMSOL 43b>COMSOL 4.3b with MATLAB.
• On Mac OS X, use the COMSOL with MATLAB application available in the Application
folder.
• On Linux®, enter the command comsol server matlab at a terminal window

首次启动 会提示输入 username 和 password,这个可以根据个人需要自己填写,之后的启动就不需要了。

启动之后会打开一个matlab的界面以及一个COMSOL with matlab的promt,在matlab中建立合适的工作路径就可以开始工作了。

2. 使用mphload函数导入模型

首先将matlab的工作路径设置到mph文件所在的路径位置。然后使用mphload命令进行导入
model = mphload(‘PATH\Mymodel.mph’)
例如:

  % Load the file model_tutorial_llmatlab.mph:
  model = mphload('model_tutorial_llmatlab')

  % Load the file model_tutorial_llmatlab.mph and set the model name in the COMSOL server to Model2:
  model = mphload('model_tutorial_llmatlab','Model2')

  % Load MyModel.mph with the path specified:
  model = mphload('PATH\MyModel.mph')

  % Load model_tutorial_llmatlab.mph and return the filename:
  [model, filename] = mphload('model_tutorial_llmatlab')

关于mphload函数的更多用法可以用help mphload查看。

模型导入成功后会显示如下内容:

model =

COMSOL Model Object
Name: ML_squareloop.mph
Tag: Model
Identifier: root

3. 使用mphsave函数保存或导出模型

语句如下:
mphsave(model,'/ML_squareloop.m')
导出成功会发现在工作路径下多了一个m文件。

Model Object

While working with the LiveLink™ interface in MATLAB® you work with models
through the model object. Use methods to create, modify, and access models.

Important Notes About the Model Object

  1. All algorithms and data structures for the model are integrated in the model object
  2. The model object and the COMSOL Desktop behavior are virtually identical
  3. The model object includes methods to set up and run sequences of operations to
    create geometry, meshes, and to solve your model.

The Model Object Methods

The model Object 下有大量的methods,他们排列成树型,有点像是COMSOL desktop中Model builder下的构型。

The General Utility Functionality ModelUtil

这个函数可以用来 create or remove a new model object, but also to enable the progress bar or list the model object available in the COMSOL server.

  1. MANAGING THE COMSOL MODEL OBJECT

model = ModelUtil.create('Model')
This command creates a model object Model on the COMSOL server and a MATLAB
object model that is linked to the model object.这个命令在COMSOL server上创建了一个object,同时也创建了一个关联的matlab object。

一个comsol server上可以存在不同的model objects,当想要用matlab来操纵时,需要赋予它不同的matlab variables。对于已经存在的model object,可以创建一个matlab variable来对它连接,例如:
model = ModelUtil.model('Model')
comsol sever上存在一个'Model',用上述命令,我们创建了一个matlab variable model将两者连接起来。

ModelUtil.remove('Model') 从COMSOL server上移除 model object Model

ModelUtil.clear清除COMSOL sever上所有的model objects。

list = ModelUtil.tags列出COMSOL sever上所有的model objects。

  1. ACTIVATING THE PROGRESS BAR
    在默认状态下, matlab不会展示进程信息,通过运行命令:
    ModelUtil.showProgress(true);
    可以activate the progress bar。
    To deactivate the progress bar enter:
    ModelUtil.showProgress(false);

你可能感兴趣的:(COMSOL with matlab---Quick Start)