COMSOL-Example of using livelink for matlab

This example demonstrates how you can apply this technique using the LiveLink™ for MATLAB®.
Application Library path: LiveLink_for_MATLAB/Tutorials/domain_activation_llmatlab

Model definition:

Assume that you want to study the heat distribution in a larger copper plate as it is heated
by a steel plate that is moved between various locations. The hot steel plate remains in each
spot on the base plate for two minutes before being moved to the next.

Notes About the COMSOL Implementation

The most efficient approach for this simulation is

  1. setting up the heat transfer problem in the graphical user interface of the COMSOL Desktop®.
  2. You can then save the model *.mph file, which you can easily load into MATLAB®, where you continue to implement the script for solving the model.

Modeling Instructions — MATLAB®

  1. Start COMSOL with MATLAB.
  2. Load the model containing the heat transfer simulation:
    model = mphopen('domain_activation_llmatlab')
  3. Enter the following command to create a list of domain indices that correspond to the
    activation order of the domains:
    domInd = [2,3,5,4]
  4. Create a shortcut to the heat transfer physics interface by typing the following:
    ht = model.physics('ht')
  5. Create a for-loop with eight iterations:
    for i = 1:8
  6. The next command defines the variable k as the modulus of the division of the iteration
    number by 8. Use this variable in step 8 below to define which domain becomes active
    in the current iteration.
    k = mod(i,4)
  7. Type the following commands to prevent k to be null
if k == 0
k = 4;
end
  1. The heat transfer physics interface should be active only on the copper plate, domain 1,
    and the currently active steel plate, domain k. Set the domain selection for the heat
    transfer physics interface, and initial condition feature node 'init2' according to the
    following:
ht.selection.set([1 domInd(k)]); #将domain 1和domain domInd(k)设置transfer physics interface
ht.feature('init2').selection.set(domInd(k)); #将domain domInd(k)设置的初始值按照'init2'设置
  1. Solve the model:
    model.study('std1').run;

运行之后我们得到了加热domain2的一次运算结果,接下来我们要修改一下我们的model设置

  1. First, create a cut point data set to plot the temperature at points (0;0;L/10), (L/2;L/
    2;L/10) and (L;L;L/10), then create the plot group for the plot. L is the sidelength of the base. Enter:
if i==1
cpt1 = model.result.dataset.create('cpt1', 'CutPoint3D'); #在dataset下create一个'CutPoint3D',label为'cpt1'
#定义'cpt1'的xyz坐标,一共给了三个点分别是 (0;0;L/10), (L/2;L/2;L/10) and (L;L;L/10)
cpt1.set('pointx', '0 L/2 L');
cpt1.set('pointy', '0 L/2 L');
cpt1.set('pointz', 'L/10');
#建立一个'PlotGroup1D'
pg1 = model.result.create('pg1', 'PlotGroup1D');
#设定数据来源是'cpt1'
pg1.set('data', 'cpt1');
#create一个pointgraph
ptgr1 = pg1.feature.create('ptgr1', 'PointGraph');
#将legend设置为on
ptgr1.set('legend', 'on');
  1. Set up a 3D surface plot to display the temperature distribution in the model by typing
    the following:
pg2 = model.result.create('pg2', 'PlotGroup3D');
surf1 = pg2.feature.create('surf1', 'Surface');
surf1.set('rangecoloractive', 'on');
surf1.set('rangecolormax', '336');
surf1.set('rangecolormin', '293.15');
  1. After the first iteration the copper plate should use the previous solution as the initial
    condition. Do this by specifying the temperature variable T in the init1 feature:
    ht.feature('init1').set('T', 1, 'T')
  2. To change the solver settings so that the initial value points the solution stored in sol1
    enter:
v1 = model.sol('sol1').feature('v1');
v1.set('initsol', 'sol1');
end

到此为止循环1结束后的model的更改结束。接下来的循环就不需要更改model了。

  1. Display the first plot group in a MATLAB figure:
figure(1)
mphplot(model,'pg1','rangenum',1) #Rangenum代表颜色表色域,可取值为正整数,这里取1
hold on
  1. Add a second figure to display a 3D plot of the temperature distribution for each
    iteration:
figure(2)
subplot(4,2,i)
pg2.setIndex('looplevel','25',0)
mphplot(model,'pg2');
  1. Use the function mphglobal to extract the value of the simulation stop time of the
    current iteration. Use this to update the solver start time for the next iteration:
time = mphglobal(model,'t','solnum','end');
model.param.set('t0',time);
  1. Display the iteration number and close the for-loop:
disp(sprintf('End of iteration No.%d',i));
end

以上所有的内容可以整合成一个脚本文件,如下所示:

# Domain activation and deactivation
#
# This model demonstrates how to activate and deactivate domains during 
# an analysis using the LiveLink for MATLAB. The problem described here 
# consists in heating of an object from alternating regions.

# Use CTRL+SHIFT+ENTER keys to run the script in sequence.
##
# Load the model containing the heat transfer simulation.
model = mphopen('domain_activation_llmatlab');
# Set the list of domain indices to use alternatively.
domInd = [2,3,5,4];
# Create a shortcut to the heat transfer physics interface.
ht = model.physics('ht');
## 
# Start the loop set for 8 iterations.
for i = 1:8
    ## 
    # Define the variable k as the modulus of the division of the 
    # iteration number by 8.
    k = mod(i,4);
    # Prevent k to be null
    if k == 0
        k = 4;
    end
    # Set the domain selection for the heat transfer physics interface, and
    # initial condition feature node.
    ht.selection.set([1 domInd(k)]);
    ht.feature('init2').selection.set(domInd(k));
    ##
    # Solve the model.
    model.study('std1').run;
    ##
    # At the 1st iteration, change model settings.
    if i==1
        # Create a point graph at points (0;0;L/10), (L/2;L/2;L/10) and
        # (L;L;L/10).
        cpt1 = model.result.dataset.create('cpt1', 'CutPoint3D');
        cpt1.set('pointx', '0 L/2 L');
        cpt1.set('pointy', '0 L/2 L');
        cpt1.set('pointz', 'L/10');
        pg1 = model.result.create('pg1', 'PlotGroup1D');
        pg1.set('data', 'cpt1');
        ptgr1 = pg1.feature.create('ptgr1', 'PointGraph');
        ptgr1.set('legend', 'on');
        # Create a 3D surface plot of the temperature.
        pg2 = model.result.create('pg2', 'PlotGroup3D');
        surf1 = pg2.feature.create('surf1', 'Surface');
        surf1.set('rangecoloractive', 'on');
        surf1.set('rangecolormax', '336');
        surf1.set('rangecolormin', '293.15');
        # Set the initial temperature at domain 1 using the temperature
        # expression.
        ht.feature('init1').set('T', 1, 'T');
        # Set the initial condition expression using the solution contains
        # in sol1.
        v1 = model.sol('sol1').feature('v1');
        v1.set('initsol', 'sol1');
    end
    ## 
    # Display the first plot group in a MATLAB figure.
    figure(1)
    mphplot(model,'pg1','rangenum',1)
    hold on
    # Display the 3D surface plot on a multiple figure window.
    figure(2)
    subplot(4,2,i)
    pg2.setIndex('looplevel', '25', 0);
    mphplot(model,'pg2');
    ## 
    # Update initial time for the next iteration.
    time = mphglobal(model,'t','solnum','end');
    model.param.set('t0',time);
    ## 
    # End of the iteration.
    disp(sprintf('End of iteration No.%d',i));
end
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,245评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,749评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,960评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,575评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,668评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,670评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,664评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,422评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,864评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,178评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,340评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,015评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,646评论 3 323
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,265评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,494评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,261评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,206评论 2 352

推荐阅读更多精彩内容

  • 没有谁是一座孤岛,每个人都是一本书!有点赞同又有点不赞同,因为即使是孤岛与孤岛之间也是有联系的,人与人之间的联系,...
    Artha肖肖阅读 121评论 0 0
  • 昨天应老师要求,买了两本书,一本是口算题卡,另一本是期末冲刺100分。看着这些书,感觉马上到期末了。 ...
    小轩仔0808阅读 111评论 0 0
  • 学了几天,尝试做一个简单的todo list 先建list.rb 建立config.ru 建立views文件夹,在...
    长物记阅读 487评论 0 1