hello everyone and welcome back to my channel i know i'm a little bit late in this lecture but i hope the weight would be worth it so previously we have been talking about the two dimensional unsteady diffusion equation and we saw how we can incorporate the unsteady time derivative term so today in the first part of this lecture we are going to build up on that we are going to take the matlab code and we are going to see how we can see the solution that is evolving with time we know that it's a transient phenomenon so it means that the flow field or in this case the temperature field would be evolving with time so we want to see how this temperature field is evolving and secondly if we want to evaluate the temperature at any particular time or time step how we can do that so those are the two things that would be done in the first part and in the second part of this lecture we will be diving a little bit deeper into this equation uh altogether so we would be talking about what is called as the convection diffusion equation so so far we have been talking only about the diffusion equation then we introduced this unsteady diffusion and now we are coming to the core of the subject which is only possible if we involve that convection term so let's first look at the first part where we would start with the unsteady diffusion equation and i'll give you a quick recap of what we have been doing so far and then we'll see how we can uh see the time evolution of this particular solution using matlab so let's jump right in so on the screen we have the unsteady diffusion equation here in the box and we saw that if we use a forward differencing in time and a central differencing in space we can arrive at this particular discretized version where it says that that the new value of t at any i comma j it's equal to t i j at old which is n and at the time step plus delta t over h square where h is the grid spacing times a where a is nothing but this particular value that is the sum of the neighbors minus four times the value at i comma j so for this we had already written a code so this is my matlab command window which is clean my workspace again clean so it just means that i'm starting from a zero solution here so on the left here what you see is the code that we wrote last time so this is exactly the same code and on the right is just just a code with some minor modifications that i would be walking you through so one one thing that at this point you might ask is how i can get an environment like this where you want to compare two different codes or you want to see what are the differences in two set of codes so you can go here in view and rather than single so if i have single i have all the codes lined up here for all the open scripts lined up here and if i change it to left and right i can get something like this that would allow me to compare these two codes one-on-one so the first thing is we clear all the variable we close all the figures and we clean the command window that is exactly the same the way in which we have defined the mesh is also the same except that for if you remember we started with the a 51 by 51 grid and then we moved on to 101 by 101 so that is why this difference in the number here but that is not something that would influence the code as such if i go further down so this is where i initialize the problem in these two parts so you can see that i have added one line of code here which says that y transient is equal to zero so let me explain how we can store the information that is being collected at multiple time steps so so far what we had been doing was we get the new value and we replace the old value with the new value we used to say that y equals to y nu so every time the information was being overwritten by the new value and now if you want to see the time evolution we want to have all that information at every time step so what we are doing for that is we are creating a new array that contains the information of y at every time step so previously why let us say we are using a grid size of 51 by 51 so every time the y or the y knew it was 51 by 51. now if we want to incorporate this time series into that we need a array or in this case a 3d matrix the the first component of that 3d matrix would be to handle this time index and the second or the third and the second and the third would be the same as the y or while you so you can think of this as a space time kind of matrix where i am using one of these index to store the time information while the second and the third would it in store the that two dimensional information i'll walk you through the code and then it would be more clear as to what i'm trying to say so after i define that the y transient is zero it's just a way of initializing a particular variable so that's the only difference in these two code then i go to the calculation part in the calculation part you will see that this until this particular line where we obtain the new value we are having exactly the same thing but after i calculate this y mu what i'm saying is that y transient iterations plus 1 so this iterations plus 1 is to store the time step into that so if you remember we start with iterations being zero and because matlab doesn't like the zero index it starts with the first index so that is why iterations plus one because i want to store the first iteration into the first time step so the first index of this 3d matrix goes as y transient mean iterations plus 1. and then the second and the third index which is from 1 to n point so this says from 1 to 51 for 101 and again the same from 1 to n points and says that assign that to be y nu so now you can see that iterations plus one is used to handle the time indexing while one two end points and one to n points this gives you a 51 by 51 indexing which is exactly used to store this y nu so the first index is constant remember the first index at this point is one the second index is from 1 to 51 the third index is 1 251 so these are varying so you can store multiple information into that and because this y or y nu it's a 51 by 51 matrix so we use this third second and the third index to store that information so in this way for every time step we store the information the second and the third index would remain exactly the same because they are 51 by 51 but the first index that corresponds to time it would change with every iteration so let's say after the first iteration the variable iterations would become 1 and when the code would come to this y transient it would say that iterations plus 1 would become 2 and this says that 2 51 1 251 and 1 251 so again this the second time step solution the second y new it would be stored in the second time index of y transient so this way you can so what i'll do is because this takes a lot of time so i'll run this particular code with let's say a slightly smaller grid so i'll go back and see that the things have been initialized here and you can see that the y transient is absolutely zero while the y and y are 31 by 31 and when i initialize or when i run this calculation loop so i'll i want to show you what happens or how the y transient actually looks like so everything else here after this particular line of code after this white transient line everything else is exactly the same as the previous code the error magnitude and everything is exactly the same the slight modification i did was i changed this remainder line because i wanted to see the solution after every 100 time steps because it usually takes a long time because now we are saving the values of every time step into a variable so the memory buffer that you would need for these calculations would be much larger that is why i am reducing that printing command so that i can see the evolution and i can judge how faster the computations are going so if i come here there are there were two plots that we did last time we did the contour plot and we saw how we can plot the error with time and i also show you how to do the subplots so these are exactly the same here these two parts and first thing that i want to talk about today is how can you plot a particular time step so now because of y transient we have the information of this variable y or the temperature field at any particular time step so we can select any time step and then we can see how the uh temperature contour or the temperature profiles are looking like at that particular time so i would go back to this single uh view so that you can see it clearly as to what exactly is happening so let me go back here so the solutions are now converge and one thing to note here is that y and y knew they are both 31 by 31 i had changed the grid size to 31.
and we saw that the solution is converged in about 6374 iterations and that gives me a 3d matrix y transient which is 600 374 as the first index while the second and the third index is 31 by 31 so for every index or for every first index in y transient we have this 2d matrix you can think of it as a 3d cuboid where the cross section area of that represents the y or the temperature field at any particular time step and along the length or along the length of the skew cuboid sorry you have the time index so i hope it would be clear as to what we are actually trying to do so to plot any particular time step let us say i select a time step of 100 and these two lines the x domain and y domain are used to generate a grid so i'll first run these four line of code and then what i'm doing is i'm extracting that particular information from y transient because i only want the information at time step 100 so i'm saying that y at that particular time step is to be fetched from y transient and we only need the first index here because as soon as we get the first index we locate that time location we want the entire 2d data out of it so we write the first index which is time step selected and these columns indicate that i want the entire information so as soon as i hit this particular line one thing one very important thing to note here is that that the y time step is actually again a 3d matrix because that first information is stored as one variable so you can think of it as a 2d matrix with one 3d element so we still are not getting a 31 by 31 whether we have one by 31 by 31 and if you try to use the contour f command on it you'll be failed because the contour it only works on the 2d 2d data sets so the urgency that we do is we need to convert or we need to reshape this 3d matrix which is not really a 3d matrix 2d to a 2d matrix so the command for that is using reshape so what reshape does is it create or it reshapes the variable into of another variable of different size the only requirement is that the number of elements should remain the same so let us say if you have a three by two matrix and if you want to convert it into a two by three matrix that is possible but you can't convert it to let's let us say four by one because the number of variables would be different so what we do is we use the command reshape we say that we want to reshape the y time step and this particular line of code it says that i want to reshape it into a 51 or 31 in this case into a endpoints by endpoints so one advice here is never try to hard code anything in this code for instance if i had hard-coded 31 or 51 here and if i say change the grid size at other let's say when i'm running the code later on this code might fail because because of that hard-coded 31 or 51 so always try to make the code more general so this is where we are using n points rather than hard coding any particular number so we are saying that reshape the y time step which is initially 1 by 31 by 31 to 31 by 31 and as soon as i do that so i'm here i'm overwriting this y time step so when i evaluate this you can see that the y time step is now 31 by 31 so we have removed that one from here so we have converted this from a 3d system to a 2d system which is now suitable for plotting so once we do that we can use the contour f command and then the color bar and i'm also using this title command here which i'll show you what it would be doing so as soon as i give this command i'll wait i'll put the plot on the other window so i can explain a little better so what the control f did was to plot the contour at that particular time instant the color bar gave you the color bar corresponding to these colors and this title command it put the title of this time at the top so the way i have written this is i say that the time equals to and because corresponding to the time step selected this might change so i say that this command is called as number two string because in the title you can only have the strings so again remember previously we were talking about how the labels were assigned to be string you can't use anything else for the labeling and similarly for the title you only need the strings so if you have some variable that in this case we had these numbers one second two second we had to convert this into string so the command is called as num to which converts a number to the string so we say that convert that number to string and the number is time step selected multiply by dt don't forget this dt because if you don't use it you will simply get 100 here which is not true because we are having these as time steps but not as physical time so this is how you can plot a particular time instant now i'll give you an overview how you can see the time evolution with time so the idea is very simple so in this case we had picked up one particular time so for the time evolution what we would do is we would define an array of this time so our time step selected would vary from let us say 1 then 100 200 300 and for every of those time step we would plot the contour but rather than plotting very quickly we would wait for a hiccup and that would allow us to see that transient growth again this is not a very official way but this is good enough to show for instance in a presentation i'll later show you how you can save this as for instance a movie or something but for now if you just want to quickly like demonstrate to someone as to how the time evolution of this problem was you can use this particular command so the way in which we do is we say that let us say we want to animate it after every 100 time steps again see that i'm not generalized i'm not hard coding anything here i say that i can change this value of n so i say that if i want to animate it after every 100 time i can create this time step array which starts from 1 and i know that the maximum value of iterations is about 6000 so 100 is a reasonable value there so this time step array so if i run these two lines of code so this time step array is nothing but 1 then 101 201 so what i want to say is i want to see the solution being evolved all of these time so then i open a new figure so what i'll do here is i'll put the figure in this side and now to plot this evolution i want to put together these figures on top of each other so what i'm doing is i'm using a for loop for plotting so what i need is i have this time indexing i have this array of time step that i want to plot the solution on so i use this every element using a for loop and i plot these solutions on top of each other one at a time so i start with the time step one plot it look at it for a little bit and then plot 101 on top of top of it and that keeps on going until the very end of this array this time step array so i say that for i being 1 to the length of this time step array so length of the time step array was 64 if you remember so the length of the timeless time step area was 64 and we say that the selected time step is the time step array the the index of that time step area so you remember that this i would vary from 1 to 64 but the corresponding time step selected so let us say i equals to i'll say i equals to 64 that should correspond to this particular value right so time step selected would correspondingly be 6300 and 1 and then i'm using these again the same line of code here that the y time step is fetched from the y transient and then we reshape it and again we brought the contour we brought the color bar we put a title so that we can see what time instance it is and finally i gave it a pause of 0. 25 seconds so i gave this pause so that we can see the time evolution you can give a larger pause if you want to explore more features or you can just put it there until you say press any command or any button so that the figure would continue plotting so once i do that if i perform this particular action so you can see here on the right that all of these figures are being plotted on top of each other with a weight of a quarter second which is not very easily detectable from a naked eye but you can see that this is good enough for you to demonstrate how the temperature field is changing with time finally let us say you don't want a 2d contour you just want to plot a temperature at let us say the center line which is one of the very good ways to validate your results so remember you have this let us say you only want the final temperature profile at the center line so to do that we have this 31 by 31 so remember these are the rows and these are the columns so and if you want to let us say get the middle column or somebody had asked you to get the temperature as a function of y along the center line so what you need to do there is you need to pick the correct column and have the value for all the rows i'll quickly demonstrate you what i'm trying to say here so we have this small y which is 31 by 31 and let us say that we want the temperature value at the center line across this y direction so we want we know that if we want to get along the y we need to include all the rows but only a fixed column to do that we say that my center equals to y then i put a column to include that i want to incorporate all the rows and the center index here would be 16 so you can always generalize it i'm hard coding it here because i'm in the command window but if i were in the editor i would use 31 which is n points plus 1 divided by 2. so if i go here i get the y center as a 31 by 1 vector and then you can plot i'll use a new figure otherwise my figure would be overwritten so once i do that you can plot any of these profiles any of these 1d profiles all you have to do you have to extract that information just like we were extracting these 2d matrices matrix from these 3d y transient matrix in a similar way you can extract a 1d vector or a 1d profile from these 3d matrixes now having done this now we'll move to the second part of this lecture so so far we have been looking at how to discretize your equation using finite differencing so i hope that this would be clear now so we had gone through diffusion then we jumped on to unsteady diffusion we have been looking at both the 1d and 2d phenomena using finite differencing and now we'll introduce the convection part into our equations so in this part i'll i'll not walk you through the matlab part so first we'll look at the theoretical aspects and then once we are perfectly aligned with the theoretical part then we'll move on to matlab but another thing that i want to mention here is now we want to introduce the concept of finite volume method these are again a subset of computational methods that can be used to calculate your solutions on these kind of grid system so once we start that then you will be more comfortable with what i'm trying to say so i'll go back to the notebook here so we have been looking at the ones one or two d unsteady diffusion so now we want to introduce what is called as the convection diffusion equation so we need to understand what these terms really mean so diffusion implies that if you remember very basic sciences that diffusion happens because of the difference in concentration for instance so if you have a very high temperature here in a material and if you have a low temperature here and you have something that connects the two let us say you have the steel rod or whatever then because of uh the material properties in this case we used to have diffusion coefficient the temperature would be fused from a higher value to a lower value so this doesn't really involve any kind of flow happening there so to incorporate that flow we have this convection term which takes into account a flow that might be happening at the same time so you can think of convection for here because the convection could be due to both due to let us say that when we say natural convection then we are not taking into account any kind of flow but when it happens with the flow then we call it as a free convection and or a force convection so in that case we consider that flow so in this case we are going to look at those kind of problems where we have a convective part that happens due to flow and we also consider the diffusive part that happens because of these gradients so i'll first start with writing a general value or a general idea of what a convection diffusion equation looks like and then i'll explain all these parts so in the general form a convection diffusion equation has an unsteady term which is represented as del over del t rho phi so in here i am using these partial derivatives because i have x y and z also playing their variations the rho is the density while phi is any general variable it could be temperature it could be your velocity field the convection term is written as del x rho u5 and the diffusion term can be written as so i'm writing is a general way plus s so let me explain the convection term and then the diffusion and the source term s so the convection term it contains a spatial derivative which is it could be delta x or delta y but then the corresponding velocity so u here is the x velocity so we have x y and z and correspondingly we have u v and w so we have this as a space variable and we have this as a sorry so this is the velocity because velocity is a 3d field so we have uv and w as the three components rho is again the density and phi here could be another variable as the same as the unsteady part so if the phi is equal to u then this term would become rho u square if the phi is equal to temperature then it would become rho u t as i was saying that the diffusion part is happening because of the temperature or the gradients in the flow that is why you have this d phi dx term and this tau is called as the diffusion coefficient and this s is called as the source term so let us say if you have a heater placed inside a domain then that heater would act like as a source term it could also be a sink which means that if something is coming out of it then we have to take care regarding that so this is a general form of convection diffusion equation so this could be a 3d version but we would for now we would only be looking at a very special case which is steady and one dimensional so when i say steady it means that there are no time derivatives and when i say one dimensional it means that it only varies in one dimension and that implies if there are no time derivatives then we can change the partial derivative to total derivative so if i write the special case of a steady 1d and not write the entire thing so i'll say it as the convection diffusion equation this could be written as d over dx so this particular unsteady term would vanish so i have d over dx rho u phi equals to d over dx tau t tau d phi over dx let us set this as the equation so coming to the finite volume method so in finite element method what we did sorry in finite differencing what we did was we had a domain and we divided that domain into multiple segments using what is called as the grid so the idea here remains exactly the same we have an over underlying grid and a grid grid points but rather than using the taylor series to get those derivatives what we do is we consider a control volume or a finite volume around any particular grid point and then we exploit the basic fundamentals of that control volume to discretize this equation so let me explain you through a picture here so let us say we have a 1d system we have this i minus 1 we have this i and we have this i plus 1 so this must be familiar to me right so let us call this i as the point as p which is the focus of our attention and then i plus 1 because it's lying in the eastern side to i we call this as e and similarly i minus 1 because it lies in the western side it would become w so in finite volume method we consider a finite volume around this point p so one way to consider this particular finite volume is to take half of this line segment and create this finite volume here so i'll just shade it for your convenience so now we have this finite volume that's sitting around p but not going all the way towards the capital e n capital w and i'll call these edges here or the points here as small e and small w so the way in which we move towards the discretized version of this equation is we take the governing equation and we integrate it over the control volume so previously we were using taylor series expansion to substitute for these derivatives but in finite volume methods we take the governing equation as it is with no approximation so this is important so with taylor series we were truncating our series but in finite volume we take the entire equation as it is and integrate it over the control volume so if i do this for now so we have d over dx rho u5 equals to d over dx tau d phi over dx so if i want to integrate it over the entire control volume it means that i am integrating it from the point small w to point small e and similarly in the other direction and we can write a dx here because it's being integrated in x so because there is a spatial derivative here sitting already which means i can simply integrate the row u phi as it is and when i take the limits it means that it becomes rho u phi at e minus rho u pi at w and that would simply be equal to similarly we can for this part we can cancel out the time der spatial derivative with the integrand here and what we have is tau d phi over dx at point e minus tau d phi over dx at point small w now the fun part happens as to how we can evaluate these small e and small w properties so that is where we can use different kind of schemes so one of the very simpler scheme is to assume that the function phi it weighs linearly between all these points so what i mean to say is we are saying that the value of phi from let us say this is phi w this is phi p and let us say this is 5 e so let us say if the value is or if the function phi is varying linearly between these points or we are trying to say that the function 5 is piecewise linear we can very simply write what we can write because the phi w it's lying in the exact center so we have defined these control volumes or the finite volumes such that phi w or the small w it's exactly at the center so we can write that phi small w is half of pi capital p plus phi capital w and similarly phi x small e is equals to pi 1 by 2 phi capital p plus pi capital e right so these things can be substituted here and the way in which we do is we keep the rho and u as it is so once we do that we get 1 by 2 rho u e and then for phi small e we get phi p plus 5 e capital e and similarly we get here 1 by 2 rho u w small w phi p plus pi capital w and now since we assume that the function phi it was varying linearly between let us say this is point p and this is point e capital e then the derivative of this function at small e it would simply be phi e minus y p divided by delta x because for a line we know that the slope of this line is y 2 minus y 1 divided by the delta x so we can write so i'll write it over here so we can write the d5 over dx at cap small e so it means that the derivative on this particular point it would simply be phi capital e minus phi capital p divided by delta x and i am writing delta x e in case we are considering a non-uniform mesh but the idea here is the same that we have this intersection points exactly halfway there so that is why we can write 1 by 2.