﻿{"id":1763,"date":"2021-12-03T04:59:16","date_gmt":"2021-12-03T04:59:16","guid":{"rendered":"http:\/\/81.70.49.155\/?p=1763"},"modified":"2021-12-03T05:00:00","modified_gmt":"2021-12-03T05:00:00","slug":"matlabrayleigh-benard-convection","status":"publish","type":"post","link":"http:\/\/81.70.49.155\/?p=1763","title":{"rendered":"[Matlab]Rayleigh-B\u00e9nard convection"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/81.70.49.155\/wp-content\/uploads\/2021\/12\/untitled.jpg\" alt=\"\" width=\"588\" height=\"424\" class=\"aligncenter size-full wp-image-1764\" srcset=\"http:\/\/81.70.49.155\/wp-content\/uploads\/2021\/12\/untitled.jpg 588w, http:\/\/81.70.49.155\/wp-content\/uploads\/2021\/12\/untitled-300x216.jpg 300w\" sizes=\"(max-width: 588px) 85vw, 588px\" \/> <\/p>\n<p>\n\tFrom:&nbsp;\n<\/p>\n<p>https:\/\/palabos.unige.ch\/get-started\/lattice-boltzmann\/lattice-boltzmann-sample-codes-various-other-programming-languages\/<br \/>\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br \/>\n% thermalLB.m: Rayleigh Benard Convection, using a LB method,<br \/>\n%&nbsp; &nbsp;based on [Z.Guo, e.a., http:\/\/dx.doi.org\/10.1002\/fld.337].<br \/>\n%&nbsp; &nbsp;Boussinesq approximation is used for the buoyancy term:<br \/>\n%&nbsp; &nbsp; &nbsp;- Fluid is approximated with incompressible Navier-Stokes<br \/>\n%&nbsp; &nbsp; &nbsp; &nbsp;equations including a body force term, and simulated<br \/>\n%&nbsp; &nbsp; &nbsp; &nbsp;with a BGK model<br \/>\n%&nbsp; &nbsp; &nbsp;- Temperature is approximated with advection-diffusion<br \/>\n%&nbsp; &nbsp; &nbsp; &nbsp;equation and simulated with a BGK model<br \/>\n%<br \/>\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br \/>\n% Lattice Boltzmann sample, written in matlab<br \/>\n% Copyright (C) 2008 Andrea Parmigiani, Orestis Malaspinas, Jonas Latt<br \/>\n% Address: Rue General Dufour 24,&nbsp; 1211 Geneva 4, Switzerland<br \/>\n% E-mail: andrea.parmigiani@terre.unige.ch<br \/>\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br \/>\n% This program is free software; you can redistribute it and\/or<br \/>\n% modify it under the terms of the GNU General Public License<br \/>\n% as published by the Free Software Foundation; either version 2<br \/>\n% of the License, or (at your option) any later version.<br \/>\n% This program is distributed in the hope that it will be useful,<br \/>\n% but WITHOUT ANY WARRANTY; without even the implied warranty of<br \/>\n% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.&nbsp; See the<br \/>\n% GNU General Public License for more details.<br \/>\n% You should have received a copy of the GNU General Public<br \/>\n% License along with this program; if not, write to the Free<br \/>\n% Software Foundation, Inc., 51 Franklin Street, Fifth Floor,<br \/>\n% Boston, MA&nbsp; 02110-1301, USA.<br \/>\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<\/p>\n<p>clear<\/p>\n<p>% GENERAL FLOW CONSTANTS<\/p>\n<p>ly&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 51;<br \/>\naspect_ratio = 2;<br \/>\nlx&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= aspect_ratio*ly;<br \/>\ndelta_x&nbsp; &nbsp; &nbsp; = 1.\/(ly-2);<br \/>\nPr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 1.;<br \/>\nRa&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 20000.; % Rayleigh number<br \/>\ngr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 0.001;&nbsp; % Gravity<br \/>\nbuoyancy&nbsp; &nbsp; &nbsp;= [0,gr];<\/p>\n<p>Thot&nbsp; = 1; % Heating on bottom wall<br \/>\nTcold = 0; % Cooling on top wall<br \/>\nT0 = (Thot+Tcold)\/2;<\/p>\n<p>delta_t = sqrt(gr*delta_x);<br \/>\n% nu: kinematic viscosity in lattice units<br \/>\nnu&nbsp; &nbsp; &nbsp; = sqrt(Pr\/Ra)*delta_t\/(delta_x*delta_x);<br \/>\n% k: thermal diffusivity<br \/>\nk&nbsp; &nbsp; &nbsp; &nbsp;= sqrt(1.\/(Pr*Ra))*delta_t\/(delta_x*delta_x);<br \/>\nomegaNS = 1.\/(3*nu+0.5); % Relaxation parameter for fluid<br \/>\nomegaT&nbsp; = 1.\/(3.*k+0.5); % Relaxation parameter for temperature<\/p>\n<p>maxT&nbsp; &nbsp;= 80000;&nbsp; &nbsp; % total number of iterations<br \/>\ntPlot&nbsp; = 100;&nbsp; &nbsp; &nbsp; % iterations between successive graphical outputs<br \/>\ntStatistics = 10;&nbsp; % iterations between successive file accesses<\/p>\n<p>% D2Q9 LATTICE CONSTANTS<br \/>\ntNS&nbsp; &nbsp;= [4\/9, 1\/9,1\/9,1\/9,1\/9, 1\/36,1\/36,1\/36,1\/36];<br \/>\ncxNS&nbsp; = [&nbsp; 0,&nbsp; &nbsp;1,&nbsp; 0, -1,&nbsp; 0,&nbsp; &nbsp; 1,&nbsp; -1,&nbsp; -1,&nbsp; &nbsp;1];<br \/>\ncyNS&nbsp; = [&nbsp; 0,&nbsp; &nbsp;0,&nbsp; 1,&nbsp; 0, -1,&nbsp; &nbsp; 1,&nbsp; &nbsp;1,&nbsp; -1,&nbsp; -1];<br \/>\noppNS = [&nbsp; 1,&nbsp; &nbsp;4,&nbsp; 5,&nbsp; 2,&nbsp; 3,&nbsp; &nbsp; 8,&nbsp; &nbsp;9,&nbsp; &nbsp;6,&nbsp; &nbsp;7];<\/p>\n<p>% D2Q5 LATTICE CONSTANTS<br \/>\ntT&nbsp; &nbsp;= [1\/3, 1\/6, 1\/6, 1\/6, 1\/6];<br \/>\ncxT&nbsp; = [&nbsp; 0,&nbsp; &nbsp;1,&nbsp; &nbsp;0,&nbsp; -1,&nbsp; &nbsp;0];<br \/>\ncyT&nbsp; = [&nbsp; 0,&nbsp; &nbsp;0,&nbsp; &nbsp;1,&nbsp; &nbsp;0,&nbsp; -1];<br \/>\noppT = [&nbsp; 1,&nbsp; &nbsp;4,&nbsp; &nbsp;5,&nbsp; &nbsp;2,&nbsp; &nbsp;3];<\/p>\n<p>[y,x] = meshgrid(1:ly,1:lx);<\/p>\n<p>% INITIAL CONDITION FOR FLUID: (rho=1, u=0) ==&gt; fIn(i) = t(i)<br \/>\nfIn = reshape( tNS' * ones(1,lx*ly), 9, lx, ly);<\/p>\n<p>% INITIAL CONDITION FOR TEMPERATURE: (T=0) ==&gt; TIn(i) = t(i)<br \/>\ntIn = reshape( tT' *Tcold *ones(1,lx*ly), 5, lx, ly);<br \/>\n% Except for bottom wall, where T=1<br \/>\ntIn(:,:,ly)=Thot*tT'*ones(1,lx);<br \/>\n% We need a small trigger, to break symmetry<br \/>\ntIn(:,lx\/2,ly-1)= tT*(Thot + (Thot\/10.));<\/p>\n<p>% Open file for statistics<br \/>\nfid = fopen('thermal_statistics.dat','w');<br \/>\nfprintf(fid,'Thermal Statistics: time-step --- uy[nx\/2,ny\/2] --- Nu\\n\\n\\n');<\/p>\n<p>% MAIN LOOP (TIME CYCLES)<br \/>\nfor cycle = 1:maxT<br \/>\n&nbsp; % MACROSCOPIC VARIABLES<br \/>\n&nbsp; &nbsp;rho = sum(fIn);<br \/>\n&nbsp; &nbsp;T = sum(tIn); %temperature<br \/>\n&nbsp; &nbsp;ux&nbsp; = reshape ( (cxNS * reshape(fIn,9,lx*ly)), 1,lx,ly) .\/rho;<br \/>\n&nbsp; &nbsp;uy&nbsp; = reshape ( (cyNS * reshape(fIn,9,lx*ly)), 1,lx,ly) .\/rho;<\/p>\n<p>&nbsp; &nbsp;% MACROSCOPIC BOUNDARY CONDITIONS<br \/>\n&nbsp; &nbsp;% NO-SLIP for fluid and CONSTANT at lower and upper<br \/>\n&nbsp; &nbsp;% boundary...&nbsp; periodicity wrt. left-right<br \/>\n&nbsp; &nbsp;% COLLISION STEP FLUID<br \/>\n&nbsp; &nbsp;for i=1:9<br \/>\n&nbsp; &nbsp; &nbsp; cuNS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 3*(cxNS(i)*ux+cyNS(i)*uy);<br \/>\n&nbsp; &nbsp; &nbsp; fEq(i,:,:)&nbsp; &nbsp;= rho .* tNS(i) .* ...<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;( 1 + cuNS + 1\/2*(cuNS.*cuNS) - 3\/2*(ux.^2+uy.^2) );<br \/>\n&nbsp; &nbsp; &nbsp; force(i,:,:) = 3.*tNS(i) .*rho .* (T-T0) .* ...<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(cxNS(i)*buoyancy(1)+cyNS(i)*buoyancy(2))\/(Thot-Tcold);<br \/>\n&nbsp; &nbsp; &nbsp; fOut(i,:,:)&nbsp; = fIn(i,:,:) - omegaNS .* (fIn(i,:,:)-fEq(i,:,:)) + force(i,:,:);<br \/>\n&nbsp; &nbsp;end<\/p>\n<p>&nbsp; &nbsp; % COLLISION STEP TEMPERATURE<br \/>\n&nbsp; &nbsp;for i=1:5<br \/>\n&nbsp; &nbsp; &nbsp; cu&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 3*(cxT(i)*ux+cyT(i)*uy);<br \/>\n&nbsp; &nbsp; &nbsp; tEq(i,:,:)&nbsp; = T .* tT(i) .* ( 1 + cu );<br \/>\n&nbsp; &nbsp; &nbsp; tOut(i,:,:) = tIn(i,:,:) - omegaT .* (tIn(i,:,:)-tEq(i,:,:));<br \/>\n&nbsp; &nbsp;end<\/p>\n<p>&nbsp; &nbsp;% MICROSCOPIC BOUNDARY CONDITIONS FOR FLUID<br \/>\n&nbsp; &nbsp;for i=1:9<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; fOut(i,:,1)&nbsp; = fIn(oppNS(i),:,1);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; fOut(i,:,ly) = fIn(oppNS(i),:,ly);<br \/>\n&nbsp; &nbsp;end<\/p>\n<p>&nbsp; &nbsp;% STREAMING STEP FLUID<br \/>\n&nbsp; &nbsp;for i=1:9<br \/>\n&nbsp; &nbsp; &nbsp; fIn(i,:,:) = circshift(fOut(i,:,:), [0,cxNS(i),cyNS(i)]);<br \/>\n&nbsp; &nbsp;end<\/p>\n<p>&nbsp; &nbsp;% STREAMING STEP FLUID<br \/>\n&nbsp; &nbsp;for i=1:5<br \/>\n&nbsp; &nbsp; &nbsp; tIn(i,:,:) = circshift(tOut(i,:,:), [0,cxT(i),cyT(i)]);<br \/>\n&nbsp; &nbsp;end<\/p>\n<p>&nbsp; &nbsp;% MICROSCOPIC BOUNDARY CONDITIONS FOR TEMEPERATURE<br \/>\n&nbsp; &nbsp;%<br \/>\n&nbsp; &nbsp;tIn(5,:,ly) = Tcold-tIn(1,:,ly)-tIn(2,:,ly)-tIn(3,:,ly)-tIn(4,:,ly);<br \/>\n&nbsp; &nbsp;tIn(3,:,1)&nbsp; = Thot-tIn(1,:,1)&nbsp; -tIn(2,:,1) -tIn(4,:,1) -tIn(5,:,1);<\/p>\n<p>&nbsp; &nbsp;% VISUALIZATION<br \/>\n&nbsp; &nbsp;if (mod(cycle,tStatistics)==0)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp;u&nbsp; &nbsp; &nbsp;= reshape(sqrt(ux.^2+uy.^2),lx,ly);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp;uy_Nu = reshape(uy,lx,ly); % vertical velocity<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp;T&nbsp; &nbsp; &nbsp;= reshape(T,lx,ly);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp;Nu&nbsp; &nbsp; = 1. + sum(sum(uy_Nu.*T))\/(lx*k*(Thot-Tcold));<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp;fprintf(fid,'%8.0f&nbsp; %12.8f&nbsp; %12.8f\\n',cycle,u(int8(lx\/2),int8(ly\/2))^2, Nu);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp;if(mod(cycle,tPlot)==0)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;subplot(2,1,1);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;imagesc(u(:,ly:-1:1)');<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title('Fluid velocity');<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;axis off; drawnow<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;subplot(2,1,2);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;imagesc(T(:,ly:-1:1)')<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title(['Temperature (Nusselt number is ' num2str(Nu) ')']);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;axis off; drawnow<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp;end<br \/>\n&nbsp; &nbsp;end<br \/>\nend<\/p>\n<p>fclose(fid);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From:&nbsp; https:\/\/palabos.unige.ch\/get-started\/lattic &hellip; <a href=\"http:\/\/81.70.49.155\/?p=1763\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c[Matlab]Rayleigh-B\u00e9nard convection\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"views":767,"_links":{"self":[{"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/posts\/1763"}],"collection":[{"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/81.70.49.155\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1763"}],"version-history":[{"count":2,"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/posts\/1763\/revisions"}],"predecessor-version":[{"id":1766,"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/posts\/1763\/revisions\/1766"}],"wp:attachment":[{"href":"http:\/\/81.70.49.155\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/81.70.49.155\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1763"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/81.70.49.155\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}