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 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

你可能感兴趣的:(COMSOL-Example of using livelink for matlab)