FVTool (Posts about [FVM, heat conduction, diffusion, Robin])http://fvt.simulkade.com/enThu, 26 Aug 2021 13:24:15 GMTNikola (getnikola.com)http://blogs.law.harvard.edu/tech/rssConduction and diffusion: a brief tutorialhttp://fvt.simulkade.com/posts/2014-06-25-conduction-diffusion-explained/Ali A. Eftekhari<div><p><strong>Important Update</strong>: the codes in this post will not work with the new version of FVTool. Download the old version of FVTool <a href="https://github.com/simulkade/FVTool/archive/v0.11.zip">here</a>.</p>
<h3>Warning</h3>
<p>This post is not edited. You may find horrible English mistakes.</p>
<h3>Diffusion/conduction equation; mass and heat transfer</h3>
<p>As a chemical engineer mass transfer is my favorite topic, which is sort of strange given the fact that heat transfer is way easier to feel and understand. It has to be due to the very busy schedule of my heat transfer Professor, who was involved in a project at the time and did not spend enough time on getting ready for his teaching duties, unlike my mass transfer Professor. Back to business... </p>
<p>Temperature gradient is the driving force behind the conductive heat transfer, and the gradient of chemical potential is the driving force behind the diffusive mass transfer. At low concentrations, the gradient of the chemical potential can be replaced by the concentration gradient. Here I'm going to show you how to solve a conservation equation when the only flux term is the diffusive heat or conductive mass (doesn't sound as funny as I expected). The equation reads</p>
<p>$$ \nabla . (-\lambda \nabla \phi)=q, $$</p>
<p>where for mass transfer, $\lambda$ denotes the diffusivity (m^2/s) and $\phi$ denotes the concentration (mol/m^3), and <em>q</em> is a mass source term (mol/(m^3.s)) and for heat transfer, $\lambda$ denotes the conductivity (J/(m.K.s)) and $\phi$ denotes the temperature (K), and <em>q</em> is a heat source term (J/(m^3.s)). There are other physical phenomena that can be described by the above relation, e.g., <a href="http://en.wikipedia.org/wiki/Poisson%27s_equation">Poisson equation</a> or <a href="http://en.wikipedia.org/wiki/Darcy%27s_law">flow in porous media</a>, in which $\lambda$ denotes the total mobility (permeability divided by the viscosity for single phase flow), and $\phi$ denotes pressure.</p>
<h3>Boundary conditions</h3>
<p>I think Dirichlet (constant value) and Neumann (constant flux) boundary conditions are quite clear in terms of their physical meaning. However, there are two important situations that lead to a Robin boundary condition. In heat transfer, when a boundary is gaining/losing heat to a medium with a constant temperature of $$ T_{\infty} $$ by convection mechanism with a heat transfer coefficient <em>h</em> (J/(m^2.K.s)), the energy balance equation at the boundary reads</p>
<p>$$ -\lambda (\mathbf{n}.\nabla T) = h(T-T_{\infty}), $$</p>
<p>which can be rearranged to the following form that can be formulated in the FVMtool:</p>
<p>$$\frac{\lambda}{h} (\mathbf{n}.\nabla T) + T = T_{\infty}$$</p>
<p>For the mass transfer, if mass is produced or consumed at a boundary by a first order reaction rate, the equation reads</p>
<p>$$ -\lambda (\mathbf{n}.\nabla c) = k_0 c, $$</p>
<p>where $k_0$ is the rate constant of the reaction that happens on the boundary.
For a chapter of my thesis, I encountered this sort of boundaries and it was the main reason that I rewrote the implementation of boundary condition from scratch. I will talk about the implementation of the boundary conditions later in a separate post.</p>
<h3>A (sort of) real problem</h3>
<p><a href="http://en.wikipedia.org/wiki/Fin_%28extended_surface%29">Fins</a> are used to increase the heat transfer area and thus the rate of heat transfer. Here we are going to model a fin in 2D. The base of the rectangular fin is attached to a surface with a constant temperature of 100 degree Celsius. The fin is made of <a href="http://en.wikipedia.org/wiki/Aluminium">Aluminum</a>, with a <a href="http://en.wikipedia.org/wiki/List_of_thermal_conductivities">thermal conductivity</a> of 237 W/(m.K), with a thickness of 0.1 cm and a length of 10 cm. The fin is exposed to a air at a constant temperature of 25 degree Celsius, and the heat transfer coefficient is 10 W/(m^2.K). This number may be off so you can estimate it using one of <a href="http://en.wikipedia.org/wiki/Heat_transfer_coefficient">these correlations</a>.<br>
Let us jump into our Matlab <a href="https://github.com/simulkade/FVTool">FVTool</a>, and solve this problem numerically using the finite volume method.</p>
<h3>Solution procedure</h3>
<p>First, we start by defining the domain and creating the mesh structure:</p>
<pre class="code literal-block"><span></span><code><span class="nb">clc</span><span class="p">;</span><span class="w"> </span><span class="nb">clear</span><span class="p">;</span><span class="w"> </span><span class="c">% clean the command prompt, clean the memory</span><span class="w"></span>
<span class="n">L</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mf">0.1</span><span class="p">;</span><span class="w"> </span><span class="c">% 10 cm length</span><span class="w"></span>
<span class="n">W</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mf">0.01</span><span class="p">;</span><span class="w"> </span><span class="c">% 1 cm thickness</span><span class="w"></span>
<span class="n">Nx</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">50</span><span class="p">;</span><span class="w"> </span><span class="c">% number of cell in x direction</span><span class="w"></span>
<span class="n">Ny</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">20</span><span class="p">;</span><span class="w"> </span><span class="c">% number of cells in the y direction</span><span class="w"></span>
<span class="n">m</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">createMesh2D</span><span class="p">(</span><span class="n">Nx</span><span class="p">,</span><span class="w"> </span><span class="n">Ny</span><span class="p">,</span><span class="w"> </span><span class="n">L</span><span class="p">,</span><span class="w"> </span><span class="n">W</span><span class="p">);</span><span class="w"> </span><span class="c">% creates a 2D Cartesian grid</span><span class="w"></span>
</code></pre>
<p>Now, we define the the transfer coefficients. We assume that the thermal conductivity is constant on the whole domain. However, we need to assign this constant value to each individual cell. This is done by the function <code>createCellVariable</code>. After creating this thermal conductivity field, we have to calculate the average values on the interface between cells or the <em>faces</em>. Again, in this special case, the average values are equal to a constant. But in other cases when the conductivity varies with space, the averaging becomes more important. Three averaging techniques are available in the <code>FVTool</code>, viz. <em>arithmetic</em>, <em>geometric</em>, and <em>harmonic</em>. The output of these averaging functions is always a face variable. Let's see that in action: </p>
<pre class="code literal-block"><span></span><code><span class="n">T_inf</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">25</span><span class="o">+</span><span class="mf">273.15</span><span class="p">;</span><span class="w"> </span><span class="c">% [K] ambient temperature</span><span class="w"></span>
<span class="n">T_base</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">100</span><span class="o">+</span><span class="mf">273.15</span><span class="p">;</span><span class="w"> </span><span class="c">% [K] temperature at the base of the fin</span><span class="w"></span>
<span class="n">k_val</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">237</span><span class="p">;</span><span class="w"> </span><span class="c">% W/(m.K) thermal conductivity</span><span class="w"></span>
<span class="n">h_val</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"> </span><span class="c">% W/(m^2.K) heat transfer coefficient</span><span class="w"></span>
<span class="n">k</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">createCellVariable</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="w"> </span><span class="n">k_val</span><span class="p">);</span><span class="w"> </span><span class="c">% assign thermal cond. value to all cells</span><span class="w"></span>
<span class="n">k_face</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">geometricMean</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="w"> </span><span class="n">k</span><span class="p">);</span><span class="w"> </span><span class="c">% geometric average of the thermal conductivity values on the cell faces</span><span class="w"></span>
</code></pre>
<p>Now, we can define the boundary conditions. Here, we have one Dirichlet (constant temperature) and three Robin boundaries. The general boundary condition is defined as </p>
<p>$$ a (\mathbf{n}.\nabla \phi)+b\phi = c. $$</p>
<p><em>a</em>, <em>b</em>, and <em>c</em> must be defined in the program. Here, we have $a=\lambda/h$, $b=1$, and $c=T_{\infty}$. Don't forget to include the sign of the normal vector for the bottom boundary. The normal vector is in the opposite direction of the y axis and therefore its sign must be included in the temperature gradient term. I will talk about it in more details later.</p>
<pre class="code literal-block"><span></span><code><span class="n">BC</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">createBC</span><span class="p">(</span><span class="n">m</span><span class="p">);</span><span class="w"> </span><span class="c">% creates a BC structure for the domain m; all Neumann boundaries</span><span class="w"></span>
<span class="n">BC</span><span class="p">.</span><span class="n">left</span><span class="p">.</span><span class="n">a</span><span class="p">(:)=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">BC</span><span class="p">.</span><span class="n">left</span><span class="p">.</span><span class="n">b</span><span class="p">(:)=</span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">BC</span><span class="p">.</span><span class="n">left</span><span class="p">.</span><span class="n">c</span><span class="p">(:)=</span><span class="n">T_base</span><span class="p">;</span><span class="w"> </span><span class="c">% convert the left boundary to constant temperature</span><span class="w"></span>
<span class="n">BC</span><span class="p">.</span><span class="n">right</span><span class="p">.</span><span class="n">a</span><span class="p">(:)=</span><span class="n">k_val</span><span class="o">/</span><span class="n">h_val</span><span class="p">;</span><span class="w"> </span><span class="n">BC</span><span class="p">.</span><span class="n">right</span><span class="p">.</span><span class="n">b</span><span class="p">(:)=</span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">BC</span><span class="p">.</span><span class="n">right</span><span class="p">.</span><span class="n">c</span><span class="p">(:)=</span><span class="n">T_inf</span><span class="p">;</span><span class="w"> </span><span class="c">% right boundary to Robin</span><span class="w"></span>
<span class="n">BC</span><span class="p">.</span><span class="n">top</span><span class="p">.</span><span class="n">a</span><span class="p">(:)=</span><span class="n">k_val</span><span class="o">/</span><span class="n">h_val</span><span class="p">;</span><span class="w"> </span><span class="n">BC</span><span class="p">.</span><span class="n">top</span><span class="p">.</span><span class="n">b</span><span class="p">(:)=</span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">BC</span><span class="p">.</span><span class="n">top</span><span class="p">.</span><span class="n">c</span><span class="p">(:)=</span><span class="n">T_inf</span><span class="p">;</span><span class="w"> </span><span class="c">% top boundary to Robin</span><span class="w"></span>
<span class="n">BC</span><span class="p">.</span><span class="n">bottom</span><span class="p">.</span><span class="n">a</span><span class="p">(:)=</span><span class="o">-</span><span class="n">k_val</span><span class="o">/</span><span class="n">h_val</span><span class="p">;</span><span class="w"> </span><span class="n">BC</span><span class="p">.</span><span class="n">bottom</span><span class="p">.</span><span class="n">b</span><span class="p">(:)=</span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">BC</span><span class="p">.</span><span class="n">bottom</span><span class="p">.</span><span class="n">c</span><span class="p">(:)=</span><span class="n">T_inf</span><span class="p">;</span><span class="w"> </span><span class="c">% bottom boundary to Robin</span><span class="w"></span>
</code></pre>
<p>Now, we have a domain with fully specified transfer coefficients and defined boundary. Next and final step is to find the matrix of coefficients for the conduction term and the boundary conditions, and solve the linear system of discretized linear equations:</p>
<pre class="code literal-block"><span></span><code><span class="n">M_cond</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">diffusionTerm</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="w"> </span><span class="n">k_face</span><span class="p">);</span><span class="w"> </span><span class="c">% matrix of coefficients for the heat diffusion</span><span class="w"></span>
<span class="p">[</span><span class="n">M_bc</span><span class="p">,</span><span class="w"> </span><span class="n">RHS_bc</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">boundaryCondition</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="w"> </span><span class="n">BC</span><span class="p">);</span><span class="w"> </span><span class="c">% matrix of coefficients and RHS vector for the boundary conditions</span><span class="w"></span>
<span class="n">T</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">solvePDE</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="w"> </span><span class="n">M_cond</span><span class="o">+</span><span class="n">M_bc</span><span class="p">,</span><span class="w"> </span><span class="n">RHS_bc</span><span class="p">);</span><span class="w"> </span><span class="c">% solve the linear system of discretized PDE</span><span class="w"></span>
<span class="n">visualizeCells</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="p">);</span><span class="w"> </span><span class="c">% visualize the results</span><span class="w"></span>
</code></pre>
<h3>Little fun</h3>
<p>Please find the source code for this tutorial <a href="https://github.com/simulkade/FVTool/blob/master/Examples/Tutorial/heatconductionfin.m">here</a>. To have some numerical fun, try to change the problem from 2D to 3D by activating the appropriate line in the Matlab source code. You can also increase the heat transfer coefficient and see its effect on the temperature profile. In the next post, I will explain how to convert this example to a unsteady-state simulation.<br>
This is a snapshot of the 3D result:</p>
<p><img alt="3d conduction in a fin" src="http://fvt.simulkade.com/heattransfer3dfin.png"></p></div>
</body></html>
diffusionheat conductionhttp://fvt.simulkade.com/posts/2014-06-25-conduction-diffusion-explained/Wed, 25 Jun 2014 21:35:13 GMT