This action might not be possible to undo. Are you sure you want to continue?

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

Written by Matt Aasted, Class of 2008 Last edited: March 2, 2007

Introduction

When optimizing the geometry of a shape in Comsol, you will frequently repeat the same steps over and over again with slight variations. The goal of this paper is to give an example of how to make Matlab do the footwork for you, and digest the results. This document explains how to repeat simple operations in Comsol in a way which can be controlled by Matlab. It does not attempt to comprehensively document the possibilities of interaction between the two programs -- for more information on the interface between them, use the help documentation in Comsol.

General Theory

Comsol is actually implemented in Matlab as a toolbox. Therefore, Comsol is able to output exactly what it having Matlab do as a series of commands in an m file (a Matlab script). Each major action (usually involving hitting ok and a statement at the bottom of the main window) performed in Comsol maps to a statement written in the m file, in the order they are performed. For example, if you hit the "Refine Mesh" button, it corresponds to a line in the m file. Be careful to avoid extraneous actions to prevent the m file from becoming too complicated to work with. For this example, I will be making a rectangle out of Iron, applying a temperature to one side and varying its dimensions. Feel free to change the geometry and materials to fit your application, but I would recommend getting a feel for the rectangle first since it is simple.

Instructions

1: Open Comsol (Not with Matlab -- for some reason, setting the subdomain didn't work for me when I tried this with Matlab, though you may have different result) In the Model Navigator window, choose Application Modes -> COMSOL Multiphysics -> Conduction -> Steady State Analysis. Hit Ok. The program will open a new window. 2: Draw your geometry

3: Set subdomain settings. Since we want a boundary condition of convection. 4: Boundary settings. and T_amb is the temperature for radiative cooling. hit the "Load. Now Choose Mesh -> Refine Mesh 6: Solve the problem Choose Solve -> Solve Problem It will take a moment and then present you with a colorful graph of your solution. You can verify that this is the left hand side of the box by noticing that when you select it. Under the Coefficients tab. Const is a constant for radiative cooling. Under boundary selection. Hit Ok. T_inf is the air temperature for convection. we will set the left hand side to being 400 K and all other sides to having convective cooling to the environment. a. and hit Ok again in the Subdomain Setting window. choose 2. Choose Mesh -> Initialize Mesh b.For this example. Click Physics -> Boundary settings. Under boundary selection. select 1. 7: Measure boundary heat flux.4) and going to (. b. This opens up a variety of parameters for setting. and set T_inf to 300 (a little warmer than room temperature) in its box. choose the boundary condition Temperature and in the T_0 parameter box (it's the only other white box) enter 400.. It will be 1 by 0. we leave all parameters except for h and T_inf at 0. Click Physics -> Subdomain Settings. For this example. q_0 is a constant heat flux value independent of temperature.-. Click ok. Coordinates are of the form (x.. the corresponding boundary turns red in the background window. Set h to 10 in the box next to it. and choose "1" in the Subdomain Selection box.." button and in the window which opens choose Library 1 -> Iron.6. .2). 5: Mesh a. We will now integrate the heat flowing across the temperature boundary and across the convection boundaries..y). click 1. Under boundary selection. Under Boundary Condition choose "Heat Flux". h is the convection constant..6 starting at (-. Choose Postprocessing -> Boundary integration a. 3 and 4 by clicking 2 then holding control while clicking 3 and 4. In the Physics tab. I will draw a rectangle using the upper rectangle tool.4.

Some version information and a statement which generates a new world. In the same window.. The order things are written out in is: . 12: In Matlab. and click Ok. . hit F5 to run it. it is Start -> Program Files -> Comsol -> Comsol with Matlab) 11: Close Comsol but not Matlab -. then the convection boundaries). you should now have two variables I1 and I2 which represent the two boundary integrals in the order they were performed (left boundary. 13: In the script editor. 9: Close Comsol 10: Open Comsol with Matlab (in the default setup. choose File -> Open. choose Normal Heat Flux (the heat flux perpendicular to the boundary). The result will appear in the bottom of the main window. b. choose where you want to save the file and what you want to name it. select boundaries 2-4 in the way described in 4b. choose "Change Current Directory" (the default) and hit ok. Also.. . .The drawing we did in step 2. Choose Normal Heat Flux.m) and click Ok.it is necessary to open Matlab this way because it sets path settings to use Comsol libraries. A loading window should appear.Step 5's meshing and mesh refining. In the window that opens. Click Apply. Choose File -> Save. choose Model M-file (*. Comsol comments fairly well.Under Expressions to integrate in Predefined quantities. Under Files of Type. so you can see each step annotated in the file. Scripting Details Now let's look at our script. and navigate to your m-file you saved in Comsol. and then a graph of temperature. When it asks you if you want to change your path. 8: Save the m-file.

0}. Notice that they are assigned to I1 and I2 -. while the second boundary is the second index.these correspond to our two boundary integrations.h = {10.h is the h setting that we applied for convection. appl. so I'll return to it in a minute.1.Data from step 1 (the type of problem to be solved) and step 4 (the boundary settings). and so the assignment makes fem contain the library information on the problem. . This section is a bit complicated.'T'}. .1].you'll want to assign these to arrays later.lib = lib. .Extend mesh: This activates the mesh the rest of the way. bnd. has an h of 10. bnd. while the multiphysics section further initializes the fem object. bnd.. The script creates an object called fem which is sort of a big object containing everything. bnd. bnd. .400}.Plot Solution: This step is part of solving. in this case two. no T0 value (it's not relevant to a heat flow boundary) and a Tinf value of 300 (the air temperature). Corresponds to step 6 above in our directions. above library materials.bnd = bnd.The subdomain settings from step 3. and may be a consequence of the mesh refinement.ind)."fem.1. there are these lines (if you did the rectangle): clear bnd bnd.T0 = {0. Boundaries in scripts: In the "Application Mode 1" section. The first boundary is the first index of each of these variables (except bnd. The second boundary is the . You're going to want to comment this section out later because it's obnoxious to have it plot every time it solves the system. You can see that the first boundary is type 'q'.Solve Problem: This actually solves the system.ind = [2.type = {'q'. For example. What is happening here is that Comsol creates an array of boundary types." and Multiphysics More boilerplate stuff. . .Tinf = {300.Integrate (two sections) -.0}.

ind assigns each boundary of the drawing in order to one of these two boundary types. what can we do with all of this? We can wrap it in a for loop and make it change the geometry.'corner'. Select the entire rest of the file and hit ctrl+] (or select Text -> Increase Indent).'pos'.constant temperature boundary.4]).[-0. for w = [.6.h.4]. inside the integrate sections. if you want to vary h.6 units high and begins at the point [-.'base'. Now. First. I2 = [ ].-.6.4]).1 by increments of . Hit F5 to save and run the new script. This will save each new integral in a new spot in the array.1:1] We will be varying w (the width of the rectangle) from .6.-0. This will take a minute or two to run. Wrap it all in a for loop So. Look at the first line: g2=rect2(1.1 up to 1. comment out the section "Plot Solution" as they will be very irritating later. Select them and hit ctrl+R to do it quickly.[-0. the command we're working with is in the Geometry section.'corner'. change I1 to I1(end+1) and I2 to I2(end+1). at the top of the file (above the first comments). Therefore. .6.'base'.1:.0. With our rectangle. bnd. What this is doing is making a rectangle which is 1 unit long by . enter this: I1 = [ ]. you would vary the first index value of bnd.'pos'.6. Let's vary our width for this example. Change the line to as follows: g2=rect2(w. At the end of the file put this: end Now.0.-0.

for loop included ****** % Remember. Now.2b (COMSOL 3. Congratulations! You've just made Comsol work inside Matlab. I1 and I2 to make them more human readable. boundary temperature and h value that you want to solve for. vrsn.build = 304. this won't work without loading "Comsol with Matlab" from the start menu. I've implemented this as a function which returns the two integral and takes the width.com/matlabcentral/fileexchange/loadFile. Second.304.2.1:. I1 = []. height.0. don't be afraid to change the names of w.do?objectId=1339& objectType=file If you don't want to look at the guts of the file all the time. plot I1 and I2 -.name = 'COMSOL 3. another clever thing to do is to make the whole thing a function.rcs = '$Name: $'. in the matlab main window. I2 = []. I've included this at the end of this document. you can do more clever loops than just a for loop.major = 0. vrsn.Now. or this Matlab script: http://www. First of all. air temperature. vrsn.see Wikipedia's entry on it for more info.they should both be about equal in magnitude (by conservation of energy since the system is at steady state and we're measuring flow in and flow out) and should both increase as the width increases.ext = 'b'. there are lots of more clever things you can do than what I have done here.mathworks. for w = [. $) . $Date: 2006/04/04 14:56:13 flclear fem % COMSOL version clear vrsn vrsn. vrsn.1:1] % COMSOL Multiphysics Model M-file % Generated by COMSOL 3.2'. A simple example to optimize would be to do a hill climbing algorithm -. Run "help function" in Matlab for more information on writing function files ***** The altered version the Comsol m file.

% Geometry g2=rect2(w.assignsuffix = '_ht'.1.ind = [1]. equ. clear bnd bnd.mode. % Refine mesh fem.1].'base'.6.ind = [2.tags={'g2'}. appl.0. bnd. s. clear equ equ. bnd.equ = equ. equ.0}.mesh=meshinit(fem). fem. s.type = {'q'. appl.'pos'.prop = prop.version = vrsn.C = 'mat1_C'.T0 = {0.analysis='static'.[-0. % (Default values are not included) % Application mode 1 clear appl appl.vrsn. clear s s.6.appl{1} = appl.Tinf = {300. . . clear prop prop. 'mcase'.rho = 'mat1_rho'.1. bnd.mesh=meshrefine(fem. 'rmethod'.name={'R1'}.date = '$Date: 2006/04/04 14:56:13 $'. % Initialize mesh fem.'corner'.bnd = bnd.objs={g2}..s).'regular'). equ..'T'}. appl.400}.geom=geomcsg(fem).0}.draw=struct('s'. bnd.k = 'mat1_k'..4]).class = 'HeatTransfer'. appl.h = {10. .-0. fem.. fem. fem.0.

% % Plot solution % postplot(fem.units = 'SI'...53406326034063.variables.variables.[-1.sigma='1. lib.frame = {'ref'}.mat{1}.'cont'.mat{1}..{'T'.2'. fem.mat{1}.border = 1..varname='mat1'.mat{1}. fem. % Solve problem fem.C='440'. lib.-1.2e-6'. % 'tridata'.. % Library materials clear lib lib. lib. % Save current fem structure for restart purposes fem0=fem.-1.variables. lib.xmesh=meshextend(fem).variables.k='76. lib.sol=femnlin(fem.mat{1}. 'outcomp'. 'solcomp'.'jet(1024)'. lib. .{'T'})..variables.E='200e9'.'Surface: Temperature [K]'.1.variables.fem.epsilonr='1'.mat{1}..mat{1}..1.nu='0.'internal'}.mat{1}. .outform = 'general'.lib = lib. fem.mat{1}.1]).. % 'title'.variables. . . lib.mur='4000'. % 'trimap'...variables.name='Iron'..rho='7870'. . % Multiphysics fem=multiphysics(fem). % Extend mesh fem. lib. lib. fem.12e7'.{'T'}. . % 'axis'.53406326034063. % Integrate .29'.mat{1}.alpha='12.mat{1}. lib.variables.

[1]. $Date: 2006/04/04 14:56:13 $) flclear fem % COMSOL version clear vrsn vrsn. . . 'edim'.1).2b (COMSOL 3.1). % Integrate I2(end+1)=postint(fem. 'dl'.name = 'COMSOL 3.height.h) % % width: Width of the rectangle % height: Height of the rectangle % Tinf: The air temperature for convection % T0: The constant temperature for the left boundary % h: The convection coefficient % % I1: The integral of the heat flowing through the constant temperature % boundary % I2: The integral of the heat flowing through the convection boundaries % % Written by Matt Aasted % Last edited 2/18/07 % COMSOL Multiphysics Model M-file % Generated by COMSOL 3.T0. 'edim'..m to use ***** function [I1. Save as rectangleHeatFlow.needs path information % % [I1.. 'dl'... .[2.T_0.'nflux_ht'. end ***** Implemented as a function. I2] = rectangleHeatFlow(width. % Requires running "Comsol with Matlab" to work -..'nflux_ht'..h) % Calculates the heat flow through the boundaries an iron rectangle with specified properties.2'.2..height.3.0.4].Tinf.304.I1(end+1)=postint(fem. ..T_inf. . I2] = rectangleHeatFlow(width.

'pos'.'corner'. 'mcase'.1..date = '$Date: 2006/04/04 14:56:13 $'. . clear s s. vrsn.type = {'q'.s). .mesh=meshinit(fem).1. equ. % Geometry g2=rect2(width.'T'}. vrsn. vrsn.'base'.. % Initialize mesh fem.0.0}.-0.class = 'HeatTransfer'. appl. fem.rcs = '$Name: $'.T0 = {0. bnd.tags={'g2'}.1].mesh=meshrefine(fem.'regular')..major = 0. % Refine mesh fem.bnd = bnd.ind = [2.rho = 'mat1_rho'.version = vrsn. bnd.geom=geomcsg(fem). bnd..k = 'mat1_k'. bnd.mode. clear equ equ.Tinf = {Tinf.objs={g2}.[-0.0}.build = 304. appl. s.vrsn. 'rmethod'. . fem. vrsn. clear prop prop.6.4]).ext = 'b'.draw=struct('s'. appl.prop = prop.assignsuffix = '_ht'.name={'R1'}. s.analysis='static'. % (Default values are not included) % Application mode 1 clear appl appl.T0}.h = {h. fem. clear bnd bnd. height.

equ = equ.. lib.mat{1}. . % Solve problem fem. fem.mur='4000'.variables..rho='7870'.mat{1}. lib.'jet(1024)'. % Library materials clear lib lib. lib..k='76..mat{1}. lib. lib.alpha='12.name='Iron'.C = 'mat1_C'.equ.mat{1}.ind = [1].mat{1}.. % 'tridata'.mat{1}.lib = lib. % 'trimap'. % Save current fem structure for restart purposes fem0=fem.'internal'}. . fem. lib.2'. . . % % Plot solution % postplot(fem.outform = 'general'.sigma='1.variables.variables.mat{1}.epsilonr='1'.frame = {'ref'}.sol=femnlin(fem.{'T'.. . 'solcomp'.29'.variables.'cont'. appl. fem.{'T'}).2e-6'.varname='mat1'. fem.{'T'}. fem. lib. 'outcomp'.variables.. lib.border = 1.units = 'SI'.mat{1}.nu='0.E='200e9'.appl{1} = appl.C='440'..mat{1}.xmesh=meshextend(fem).mat{1}.. fem. . % Extend mesh fem.variables.mat{1}. equ.variables.. lib.variables.12e7'. lib. % Multiphysics fem=multiphysics(fem).variables.

53406326034063.'Surface: Temperature [K]'.1).. 'axis'.'nflux_ht'. 'edim'. 'edim'...1. ...4].1]). 'dl'.[1].[-1.. .-1.1... . 'dl'. end . .% % 'title'. % Integrate I1=postint(fem.'nflux_ht'.53406326034063.. % Integrate I2=postint(fem.[2. .1).3.-1..

- STAAD.foundation User's Manual
- COMSOL Multi Physics Flow Simulations
- ANSYS Mechanical APDL Advanced Tutorials
- COMSOL Multi Physics Instruction Manual
- A Comparative Study of Neural Networks and Fuzzy Systems in Modeling of a Nonlinear Dynamic System, Metin DEMIRTAS, Musa ALCI
- A Parallel Implicit In Compressible Flow
- Specification
- safe
- 68364571-XFlow-TutorialGuide-v1-0-82
- Deform 3d v6.0
- Tutorial 02 Materials and Loading
- pc5-5
- Siemens PLM Software NX Supplement WEB
- STI0701 CMS in Simulation
- Design of Data Association Filter Using Neural Networks for Multi-target Tracking
- liuzhanqiang-11.pdf
- Technical Manual iTNC530 English
- Control System in Struc Engineering
- Results Post Processing,Patran
- M4_S3_Analysis Procedures& Modeling Features of SAP
- A Computational Algorithm for the Greens Function
- Ansys Fluent 14.0
- ITJIM02(First Review)
- On-Bottom Analysis ABAQUS
- Wamit Manual
- Structural Mechanics Solutions 12.1
- DEM Lecture0224
- 10 Things to Consider When Choosing Vision White Paper
- IST-SiggraphGames
- Effective Temperature Reference

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd