metu mechanical engineering department me 582 finite...

22
ME 582 – Handout 6 –COMSOL Tutorial 1 6-1 METU Mechanical Engineering Department ME 582 Finite Element Analysis in Thermofluids Spring 2018 (Dr. C. Sert) Handout 6 – COMSOL Tutorial 1 1 COMSOL is a finite element based simulation software. It comes with several modules under the titles structural mechanics, acoustics, fluid flow, heat transfer, chemical engineering, electromagnetics, etc. METU has a limited Classkit license of COMSOL, which has only a few of the basic modules. In these tutorials we will learn how to use them. I am not an expert on COMSOL. I use it in my classes as a teaching aid, not in my research. In this tutorial we’ll solve the following 1D problem, which was Q2a of Study Set 2. In Study Set 3 we also solved it with our steady1D.m MATLAB code. 2 2 + + = 1 + + cos() , 0 < < 8 (0) = 0 , (8) = 8 + sin(8) One of the important capabilities of COMSOL, which you’ll not be able to find in most other similar software, is that it lets you define your own differential equations. If you want to solve for example an incompressible flow problem, you can directly use the COMSOL module specifically designed for that. But if the problem you want to study cannot be classified as such, and instead it is a purely mathematical test problem such as the one we want to solve here, you can still use COMSOL, as will be demonstrated in this tutorial. COMSOL’s user interface changed a bit from version 4 to version 5. Here we use version 5.3a. Its user interface is a bit crowded and my screenshots in these tutorials are a bit small to fit into the pages. Step 1. Start COMSOL. Click on Model Wizard. Select 1D. Under Mathematics PDE Interfaces select Coefficient Form PDE (c). This will let us to work with a generic PDE, which can be made to fit our DE. Press the Add button. Press the Study button. See the screenshot in the next page. Note that by default the unknown is called and its unit is 1. Correspondingly the unit of the right hand side force function of the PDE is 1/m 2 . Let’s not change them. 1 COMSOL 5.3a is used to prepare this tutorial

Upload: others

Post on 17-Feb-2020

76 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-1

METU Mechanical Engineering Department ME 582 Finite Element Analysis in Thermofluids

Spring 2018 (Dr. C. Sert) Handout 6 – COMSOL Tutorial 11

COMSOL is a finite element based simulation software. It comes with several modules under the titles structural mechanics, acoustics, fluid flow, heat transfer, chemical engineering, electromagnetics, etc. METU has a limited Classkit license of COMSOL, which has only a few of the basic modules. In these tutorials we will learn how to use them. I am not an expert on COMSOL. I use it in my classes as a teaching aid, not in my research. In this tutorial we’ll solve the following 1D problem, which was Q2a of Study Set 2. In Study Set 3 we also solved it with our steady1D.m MATLAB code.

𝑑2𝜙

𝑑𝑥2+

𝑑𝜙

𝑑𝑥+ 𝜙 = 1 + 𝑥 + cos(𝑥) , 0 < 𝑥 < 8

𝜙(0) = 0 , 𝜙(8) = 8 + sin(8)

One of the important capabilities of COMSOL, which you’ll not be able to find in most other similar software, is that it lets you define your own differential equations. If you want to solve for example an incompressible flow problem, you can directly use the COMSOL module specifically designed for that. But if the problem you want to study cannot be classified as such, and instead it is a purely mathematical test problem such as the one we want to solve here, you can still use COMSOL, as will be demonstrated in this tutorial.

COMSOL’s user interface changed a bit from version 4 to version 5. Here we use version 5.3a. Its user interface is a bit crowded and my screenshots in these tutorials are a bit small to fit into the pages.

Step 1. Start COMSOL.

Click on Model Wizard.

Select 1D.

Under Mathematics – PDE Interfaces select Coefficient Form PDE (c). This will let us to work with a generic PDE, which can be made to fit our DE.

Press the Add button.

Press the Study button. See the screenshot in the next page.

Note that by default the unknown is called 𝑢 and its unit is 1. Correspondingly the unit of the right hand side force function of the PDE is 1/m2. Let’s not change them.

1 COMSOL 5.3a is used to prepare this tutorial

Page 2: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-2

Select Stationary to solve a time independent problem.

Press the Done button down the page.

Page 3: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-3

COMSOL window has the following parts.

Main ribbon style menu

Model Builder (MB)

Settings

Graphics

Messages

We’ll mostly be entering information into the Model Builder and Settings tabs.

Page 4: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-4

Step 2. Let’s create a 1D domain of length 8. In the Model Builder (MB) tab right click Geometry 1 and select Interval. An item called “Interval 1” will be created.

In the Setting tab enter the left and right end points of the domain as 0 and 8.

Click Build Selected.

You’ll see the created 1D domain as a line in the Graphics tab.

You can pan around and zoom in/out using the left and middle mouse buttons. Also there is the useful Zoom Extents button to see everything.

Page 5: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-5

Step 3. In the MB tab select Coefficient Form PDE 1 under Coefficient Form PDE (c).

Under Equation you’ll see the generic PDE that COMSOL uses. It has the following terms (from left to right)

second order time derivative term 𝑒𝑎𝜕2𝑢

𝜕𝑡2, which is typically zero for thermofluidic transport problems

first order time derivative term 𝑑𝑎𝜕𝑢

𝜕𝑡, which is zero for this steady problem

diffusion term ∇ ⋅ (−𝑐∇𝑢), with diffusion coefficient c. In 1D with constant c this becomes −𝑐𝑑2𝑢

𝑑𝑥2.

convection term ∇ ⋅ (−α𝑢), where α is called the conservative flux convection coefficient. This is NOT the convection term that we are using in our lectures

conservative flux source term ∇ ⋅ 𝛾, which is a special source term

convection term 𝛽 ⋅ ∇𝑢, with the convection coefficient 𝛽. In 1D it becomes 𝛽𝑑𝑢

𝑑𝑥. This is the convection

term that we are discussing in our lectures.

zero order unknown term a𝑢, where a is called the absorption coefficient

and finally the right hand side source term 𝑓

Note that u is the unknown. In our case it is a scalar, but in general it can be a vector quantity too. As you noticed COMSOL has its own vocabulary and symbol list for all these terms. Some of them go parallel with what we use in class and some do not.

You can read more details in the following part of the COMSOL documentation

COMSOL Multiphysics > COMSOL Multiphysics Reference Manual > Equation-Based Modeling > Modeling with PDEs > The Coefficient Form PDE

Page 6: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-6

To make this generic PDE equal to the DE that we want to solve, we need to set

𝑐 = −1 ,

a = 1 ,

𝛽 = 1 ,

𝑓 = 1 + 𝑥 [1/m^3] + cos(𝑥 [1/m]) [1/m^2]

and set all other coefficients to zero. Be careful about the ones that are not zero by default. Set them to zero as shown below.

In COMSOL equations are solved in dimensional form. In the “Coefficient Form PDE” solution that we are trying to do, the unknown 𝑢 is dimensionless by default. We could select a dimension for it in Step 1, but we did not. Accordingly, all the coefficients that define the PDE need to be in proper dimensions. Their required dimensions are shown next to the textboxes where you enter them. For example 𝑐 is dimensionless, a is in 1/m2 and so on. Default dimension of 𝑓 is 1/m2. Again we could change it in Step 1, but we did not. When you say 𝑓 = 1, COMSOL automatically understands its dimension as 1/m2. But when you say 𝑓 = 1 + 𝑥 it gets confused because the dimension of 1 and the dimension of 𝑥 do not match. To set the dimension of the 𝑥 term to 1/m2 we use [1/m^3] next to it, i.e. we divide it by m3. In the cos(𝑥) term first we multiply 𝑥 by [1/m] because cosine function needs a dimensionless argument (radian is dimensionless). And then we multiply the last term by [1/m^2] to set its dimension to the desired 1/m2.

Note that here we only entered f as a function of 𝑥. Other coefficients could be functions of 𝑥 too. It is possible to make these coefficients function of the unknown 𝑢 and its derivative making the PDE nonlinear.Also note that COMSOL is aware of standard math functions such as cosine.

Page 7: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-7

Step 4. By default both boundary conditions are set to zero flux (NBC type with SV = 0), which is a “do nothing” BC. To change the BCs, right click Coefficient Form PDE (c) and select Dirichlet Boundary Condition (Dirichlet BC is the same as EBC). A new item named “Dirichlet Boundary Condition 1” will appear in the MB tab.

On the Graphics tab use the mouse to select the left boundary point.

Do not change the default value of 𝑟 = 0, which is the value specified at the left boundary. For EBCs, COMSOL uses the equation 𝑢 = 𝑟.

Note that Dirichlet BC is the same as Essential BC.

Page 8: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-8

To change the second BC, again right click Coefficient Form PDE (c) in the MB tab and select Dirichlet Boundary Condition. A new item named “Dirichlet Boundary Condition 2” will appear in the MB tab.

On the Graphics tab use the mouse to select the right boundary point.

Set 𝑟 = 8 + sin (8), which is the specified EBC.

Important: We did not delete the default generated Zero Flux boundary condition. You can see it above under Coefficient Form PDE (c). But when you click on it you’ll see that the conditions defined there are labeled as overridden. This happened when we defined Dirichlet BCs.

Page 9: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-9

Step 5. On the MB tab select Mesh 1.

In the Settings tab, do not change the default settings.

Click Build All.

As will be seen in the Messages tab, a mesh of 15 elements will be generated with the default settings. By changing the mesh parameters we can control the number and distribution of the elements. We’ll not do that here.

The generated uniform mesh of 15 elements is visible in the Graphics tab (Sometimes the mesh is not shown, I don’t know why).

Page 10: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-10

Step 6. Select Study 1 in the MB tab and press the Compute button under Settings.

Solution will finish in 1 second and the following result will be plotted.

Use the Image Snapshot button to copy the plot to the clipboard to paste it into a document or save it as an image file.

When the solution is done 1D Plot Group 1 item will appear in the MB tab under Results. Many properties of the above plot, such as its title, axes names, line color, etc. can be changed by selecting it.

Note that by default COMSOL uses quadratic elements and that’s why the solution looks quite smooth.

Page 11: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-11

Step 7. It is possible to post-process the solution data in many different ways. For example, one common thing we do is to evaluate the secondary variable (or the derivative that appears in the secondary variable) at the boundary points where EBC is specified.

In this problem EBC is specified at the left boundary. To calculate the slope of the unknown there, in the MB tab right click Derived Values under Results and select Point Evaluation.

In the Graphics tab select both the left and right boundary points using the mouse and the Ctrl key.

Change Expression to ux, which means 𝑑𝑢/𝑑𝑥 (or as COMSOL says, “Gradient of u, x component”).

Note: To see other available derived values, click the Replace Expression button.

Click the Evaluate button at the top of the “Point Evaluation” tab.

The calculated gradient of 𝑢 at the boundaries will appear in the Table 1 tab next to the Messages tab. These are NOT the secondary variables, but the slopes of the unknown at the boundaries.

Page 12: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-12

Step 8. To generate a new plot right click the 1D Plot Group 1 under Results. A new item called Line Graph 2 will appear. There was already Line Graph 1, which is the plot of the solution itself.

In the Graphics tab select the 1D domain.

Change Expression to ux to plot 𝑑𝑢/𝑑𝑥.

Click the Plot button.

Both 𝑢 and 𝑑𝑢/𝑑𝑥 will be seen together as shown in the next page.

Page 13: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-13

v

To see the plot of only 𝑑𝑢/𝑑𝑥, in the MB tab right click Line Graph 1 and select Disable. Plot of u will disappear. You can enable it again any time you want.

Or for 𝑑𝑢/𝑑𝑥 you could have created a new 1D Plot Group by right clicking Results in the MB tab and only show 𝑑𝑢/𝑑𝑥 in it.

Page 14: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-14

Step 9. To export the solution to a text file right click Study 1/Solution 1 under Results -> Data Sets and select Add Data to Export.

A new Data 1 item will appear under Export. Select it.

Add u and ux to the Expression list.

Select a file name and press the Export button.

A file with the data shown in the next page will be created. It has the coordinates, 𝑢 values and 𝑑𝑢/𝑑𝑥 values at 16 mesh points.

Page 15: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-15

Save your work. COMSOL files are saved with MPH extension.

Page 16: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-16

Step 10. Let’s change the left boundary condition to solve Q2b of Study Set 2. New BCs are

𝑑𝜙

𝑑𝑥|𝑥=0

= 2 , 𝜙(8) = 8 + sin(8)

Now the left BC is of NBC type. In COMSOL both NBC and MBC type BCs are called Flux/Source.

Right click Coefficient Form PDE (c) and select Flux/Source. A new item named “Flux/Source 1” will appear in the MB tab.

On the Graphics tab use the mouse to select the left boundary point.

As seen in the screenshot on the next page Flux/Source type BC is defined as follows

−�⃗� ⋅ (−𝑐∇𝑢 − 𝛼𝑢 + 𝛾) = 𝑔 − 𝑞𝑢

In our problem 𝛼 = 0 and 𝛾 = 0, therefore the equation reduces to

�⃗� ⋅ (𝑐∇𝑢) = 𝑔 − 𝑞𝑢

In 1D the dot product �⃗� ⋅ ∇𝑢 simply gives 𝑛𝑥𝑑𝑢

𝑑𝑥. Therefore the equation is

𝑐𝑑𝑢

𝑑𝑥𝑛𝑥 = 𝑔 − 𝑞𝑢

This is very similar to how we handle MBCs in our lectures. Left hand side is the SV and u on the right hand side is the PV. If we set 𝑞 = 0 it becomes a NBC, if not it is used to provide a MBC.

In our problem we have 𝑑𝑢

𝑑𝑥= 2 at the left boundary. 𝑐 is previously defined as -1. 𝑛𝑥 at the left boundary is -1.

Therefore we need to specify 𝑔 = 2. Also 𝑞 = 0 because this is a NBC.

See the next page for a screenshot of this step.

Important: The previously defined Dirichlet BC at the left node still seems to be active. So there seems to be 2 BCs defined at the left node. To make sure that the NBC that we just defined will be used, right click on the first Dirichlet BC and Disable it.

Page 17: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-17

Disable this EBC

Page 18: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-18

Step 11. To perform a new run with the new BC, select Study 1 and click Compute. A new will be performed, but you’ll not any changes in the plots. Actually the solution changes but very slightly. If you look at the solution plot you’ll see that 𝑢(0) is zero. Actually it is not. If you zoom in enough you’ll find out that it is very slightly below zero, around −0.0052. With the same 15 quadratic element mesh, our steady1D.m code calculates 𝑢(0) = 0.00517882, the same as COMSOL.

History & Motivation: For this simple 1D problem, steady1D.m and COMSOL are doing exactly the same thing. At this point it might be encouraging for young researchers like you to note that COMSOL started its journey as a simple MATLAB Toolbox called PDE Toolbox. Today this toolbox is still available in MATLAB as a simple tool to solve model DEs using FEM, with some mesh generation and post-processing capabilities. Try it if it is available in your MATLAB installation. The developers of the toolbox were visionary researchers and coders, and in 1986 they founded a company called FEMLAB. First version of their software, called FEMLAB, was a much improved version of the PDE Toolbox requiring MATLAB to work. Later they made it a standalone software that can work without MATLAB. People liked this idea of “multiphysics software” and FEMLAB became very successful. 12 years later, in 1998, the company changed its name to COMSOL and today they develop and market this very popular simulation software for engineers and scientists.

Today COMSOL still carries this historical link with MATLAB. For example, when you install it you get a menu item called “COMSOL with MATLAB”. These two software get along very well. You can call MATLAB within COMSOL and vice versa.

So, a handful of Swedish coders playing with MATLAB and having fun are now known as the creators of a very beautiful and very successful software.

Be visionary. Be creative. Love coding. Work hard. Have fun.

http://homepage.usask.ca/~ijm451/finite/fe_resources/node417.html

Exercise: Try to solve Q2c of Study Set 3 which uses MBC at the left boundary.

Page 19: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-19

Step 12. With the default mesh generation options we had a mesh of 15 elements. By default, in 1D COMSOL uses quadratic elements. Let’s see how we can control mesh generation a bit.

In MB, Press the Show button and enable Discretization. With this we’ll be able to see more discretization related details, which are hidden by default. As you see there are hidden features too.

Go to Coefficient Form PDE (c) and you’ll see a Discretization part, which was hidden before.

Page 20: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-20

As you see MATLAB supports a number of different shape function types. The default selection in 1D is Lagrange. The ones we used in our lectures are also Lagrange type shape functions. High order Lagrange type shape functions have equally spaced inner nodes.

The default element order in 1D is quadratic. In 1D COMSOL supports all the way to septic, which is 7th order.

Let’s change the element order to Linear, delete the old mesh (right click Mesh 1 and select Clear Mesh), create a new mesh (select Mesh 1 and select Build All) and solve the problem again (select Study 1 and press Compute).

This mesh has now 15 linear (1st order) elements. The new result is as follows (Note that left BC is NBC).

Still the solution (blue curve) looks smooth. If you zoom in, you’ll see that it is composed of many straight lines. First derivative of the solution (green curve) is constant over each line and jumps from one element to the other. 𝑢(0) value at the left boundary is now not that close to zero. It is below -1. Our steady1D. m code with 15 linear elements finds that value as -1.186.

Page 21: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-21

Step 13. Let’s use the highest available elements order (7th order) and try to obtain a solution with only a single element.

Under Coefficient Form PDE (c) change the element order to Septic (7th order).

Right click Mesh 1 and select Clear Mesh.

To create a single element mesh, select Mesh 1 and change “Sequence Type” to User Controlled Mesh.

This will create two new entries under Mesh 1, as Size and Edge 1.

Select the newly created Size under Mesh 1.

Change Predefined to Custom.

Set Maximum element size to 8 (Our domain length is 8).

Click Build All. In the messages tab you’ll see that a mesh of 1 element is generated.

Under Solve 1 click Compute with this mesh of only a single 7th order element. The result is the following.

Page 22: METU Mechanical Engineering Department ME 582 Finite ...courses.me.metu.edu.tr/courses/me582/files/Handouts/Handout 6 - COMSOL... · Handout 6 – COMSOL Tutorial 11 COMSOL is a finite

ME 582 – Handout 6 –COMSOL Tutorial 1

6-22

Not bad at all.

Exercise: One by one select lower orders and perform new runs with this single element mesh. See how the solutions changes. You’ll notice that the derivative of the solution gets worse more rapidly than the solution itself.

Of course COMSOL has many other mesh creating and handling capabilities. It can also perform Adaptive Mesh Refinement (AMR).

This is the end of the first COMSOL tutorial. If you noticed any mistakes/typos or unclear parts, please send me an email.