Project 4
\documentclass[12pt]{report}
\usepackage{parskip}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{amsmath,amssymb,amsthm}
%\usepackage{physics}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, total={6.5in, 9.8in}]{geometry}
\title{Running OpenFoam cases to compare the discretisation Schemes}
\author{Kawal Thakur}
\usepackage{sectsty}
\sectionfont{\fontsize{15}{15}\selectfont}
\subsectionfont{\fontsize{14}{14}\selectfont}
\begin{document}
\maketitle
\section*{Aim}
Running a simple OpenFoam case of a shockTube with scalarTransportFoam solver to compare
the results we get from different discretisation schemes.
\section*{Objective}
\begin{itemize}
\item{Analysing the results by running a case for pure diffusion by setting velocity Field to 0 \verb!U(0 0 0)!. Comparing the Temperature contour of the results obtained from varying the diffusion coefficient \verb!DT! from \\\\ \verb!DT = 0! \\ \verb!DT = 0.0001! \\ \verb!DT = 0.01! \\ \verb!DT = 1!}
\item{Analysing the results by running a case for pure advection by setting velocity Field to \verb!U(1 0 0)! and diffusion coefficient \verb!DT = 0!}
\item{Comparing the results from different \verb!divSchemes! on a pure advection case, the discretisation schemes includes \\\\ \verb!upwind! \\ \verb!linearUpwind! \\ \verb!quadratic! \\ \verb!cubic! \\ \verb!vanLeer! \\ \verb!QUICK!}
\end{itemize}
\section*{Procedure(Pure Diffusion)}
\subsection*{Copying the pitzDaily case from scalarTransportFoam tutorial to the user run directory.}
Since we intend to run a simple-case we select a \verb!pitzDaily! case from the \verb!scalarTransportFoam! tutorial, here we change the mesh and the initial conditions.\\
\\
\verb!>> tut!
\\\\
\verb!>> cd basic/scalarTransportFoam/!
\\\\
\verb!>> cp pitzDaily $FOAM_RUN/openfoam-cases/pitzDaily-scalarFoam!
\\\\
We now make changes in the copied case, here we replace the blockMeshDict file with a file conatining a simple-tube like mesh, further we replace the 0 directory.
\pagebreak
\subsection*{Copying the blockMeshDict file and 0 folder from the shockTube case to the previously copied pitzDaily case.}
\verb!>> tut!
\\\\
\verb!>> cd compressible/sonicFoam/laminar/shockTube!
\\\\
\verb!>> cp -r 0.orig $FOAM_RUN/openfoam-cases/pitzDaily-scalarFoam/0!
\\\\
\verb!>> cp system/blockMeshDict $FOAM_RUN/openfoam-cases/pitzDaily-scalarFoam!
\\
\verb! /system/blockMeshDict!
\\\\
By doing this we have copied the necessary files into our \verb!pitzDaily-scalarFoam! case
\subsection*{Initial Field for temperature T}
Here we want to initialise the center of the tube with a block of temperature equal to a certain value and then see how the temperature convects when different convection schemes are used.\\ To set the initial temperature field T of certain value in between the tube, we make use of the \verb!setFields! utility in the OpenFoam.\\ With this we can specify how do we want our internal field to be like, the specifications can be provided in the \verb!setFieldsDict! file and to initialise the field we can execute \verb!setFields! command in the case directory.
\\The code for \verb!setFieldsDict! is given as
\begin{verbatim}
FoamFile
{
version 2.0;
format ascii;
class dictionary;
location "system";
object setFieldsDict;
}
defaultFieldValues
(
//volVectorFieldValue U (1 0 0);
volScalarFieldValue T 0.0
);
regions
(
boxToCell
{
box (-0.5 -1 -1)(0.5 1 1);
fieldValues
(
volScalarFieldValue T 1.0
);
}
);
\end{verbatim}
After this our case is ready to run.The previous code sets the value of Temperature in the given box to be 1. Now we can proceed with running the case with different objectives.
\subsection*{Analysing the case for pure diffusion}
For a pure diffusion we need to set our velocity field as \verb!U(0 0 0)!. We can update the internal Field of the velocity by changing it in the 0 folder present in the case directory\\\\
\verb!>> cd $FOAM_RUN/openfoam-cases/pitzDaily-scalarFoam/0!
\\\\
\verb!>> code U!
\\\\
Here we specify the internal field as (0 0 0)\\\\
\verb!internalField uniform (0 0 0);!\\\\
We also need to set the diffusion coefficient in the \verb!transportProperties! file present in the case directory's constant folder.
\\\\
\verb!>> cd $FOAM_RUN/openfoam-cases/pitzDaily-scalarFoam/constant!
\\\\
\verb!>> code transportProperties!
\\\\
Here we can specify the value of diffusion coefficient \verb!DT!\\\\
\verb!DT 0;!
\\\\
Further we set the end time of simulation to be 5 sec and the deltaT i.e the time step to be 0.1 sec.The write interval is set as 10, i.e after every 0 steps. These changes are done in the \verb!controlDict! file.
\begin{verbatim}
endTime 5;
deltaT 0.1;
writeControl timeStep;
writeInterval 10;
\end{verbatim}
We intend to analyse the case for different value of diffusion coefficients
For each of the below case we first need to load the Mesh and set the internal Field for the temperature. This is done by executing
\\\\
\verb!>> blockMesh!
\\\\
\verb!>> setFields!
\\\\
Now our Mesh is ready, the initial internal field for temperature is set to 1.
\\
We set the value of the diffusion coefficient to be 0 in the \verb!transportProperties! file. Then we run the case\\\\
\verb!>> scalarTransportFoam!
\\\\
Further we can visualise our results in the para-view\\\\
\verb!>>paraFoam!
\begin{figure}[!htb]
\centering
\includegraphics[scale=0.35]{dt_0.png}
\caption{Temperature Contour at time = 1 sec and 5 sec respectively.}
\end{figure}
Since our diffusion coefficient is equal to 0, and the velocity field is 0, there is no change in the temperature field over the period of 5 sec. The same can be visualised by ploting the variation of temperature (T) along the length of the tube as shown below
\begin{figure}[!htb]
\centering
\includegraphics[scale=0.55]{temp_at1and5sec.png}
\caption{Temperature variation along the length at time = 1 sec and 5 sec respectively.}
\end{figure}
As it can be seen that for \verb!DT = 0! there is no variation of temperature and hence the two plots coincide.
\\The same plot can be used to analyse the variation of temperature in rest other values of \verb!DT!. We can further automate this to generate the data for other values of \verb!DT!.
\\For this we can use the \verb!postProcess! functions in openFoam, one such \verb!postProcess! function is \verb!singleGraph!, it logs the data of a particular Field along specified line in a configuration file. We can use this functionality to log the values of temperature along the length.
\subsection*{Automating the analysis using singleGraph functionality}
First we need to copy the \verb!singleGraph! file present into the openfoam-etc directory to our system folder of opur case directory.\\\\
\verb!cd $FOAM_ETC/caseDicts/postProcessing/graphs!\\\\
\verb!cp singleGraph $FOAM_RUN/openfoam-cases/pitzDaily-scalarFoam/system/singleGraph!\\\\
After copying the file we need to specify the two points which defines the line over which we want to log the data and the field as temperature in the \verb!singleGraph! file.
\begin{verbatim}
start (-5 0 1);
end (5 0 1);
fields (T);
\end{verbatim}
Further we also need to include the \verb!singleGraph! function in the \verb!controlDict! file.We do this by adding the entry of \verb!functions! in our \verb!controlDict! file.
\begin{verbatim}
functions
{
#includeFunc singleGraph
}
\end{verbatim}
We further need to create a shell script so that we can run our case for each values of \verb!DT!.\\
Thus we create a shell-script that will automate our analysis.
\\\\
\verb!>> touch Allrun-pdiff !
\\\\
\verb!>> chmod 755 Allrun-pdiff!
\\\\
\verb!>> code Allrun-pdiff!
\\
\pagebreak
\begin{verbatim}
#----------------------------------------------------------------------------#
#!/usr/bin/bash
# Cleaning the case before running
rm -r {1..5}
rm -r postProcessing/
#Adding the settings for pure advection
sed -i "s/DT 0.01;/DT 0;/g" ./constant/transportProperties
# Creating a directory to store all the data files
mkdir data-pdiff
# Making the mesh and setting the fields
blockMesh
setFields
# Creating a for loop to run solver scalarTransportFoam on each case
COUNTER=0
DT=(0.01 0 0.0001 1 0.01)
for I in 1
do
sed -i "/DT ${DT[${COUNTER}]};/c\DT ${DT[$((COUNTER+1))]};"\
./constant/transportProperties
scalarTransportFoam
for J in {1..5}
do
# copying the data file from each timestep folder to data folder
NAME="time-${J}-dt-${DT[$((COUNTER+1))]}.xy"
cp ./postProcessing/singleGraph/${J}/line_T.xy $FOAM_RUN/openfoam-cases\
/pitzDaily-scalarFoam/data-pdiff/${NAME}
done
rm -r {1..5}
rm -r postProcessing
COUNTER=$((COUNTER+1))
done
#Reverting the settings
sed -i "s/DT 0;/DT 0.01;/g" ./constant/transportProperties
#-----------------------------------------------------------------------------#
\end{verbatim}
We can now execute the \verb!Allrun-pdiff! script by \\\\
\verb!>> ./Allrun-pdiff!
\\\\
\pagebreak
\subsection*{Plotting the results using gnuplot}
For this we create a \verb!plot! file in the data-pdiff folder we previously stored all the case data in. In it we specify the plots we want.
\\\\
\verb!>> code plot!
\begin{verbatim}
#!/usr/bin/gnuplot
set terminal png size 1280, 800 font "Bahnschrift,19"
set title "Variation of Temperature along the length for DT = 0"
set xlabel "Length (m)"
set ylabel "T (K)"
set yrange [-0.1:1.1]
set xrange [2:8]
set grid xtics ytics
set title "Variation of Temperature along the length for DT = 0"
set output "0.png"
plot "time-2-dt-0.xy" lw 2 with lines title "t = 2 sec",\
"time-5-dt-0.xy" lw 2 with lines title "t = 5 sec",\
"time-0-dt-0.xy" lw 2 with lines title "t = 0 sec"
set title "Variation of Temperature along the length for DT = 0.0001"
set output "0.0001.png"
plot "time-2-dt-0.0001.xy" lw 2 with lines title "t = 2 sec",\
"time-5-dt-0.0001.xy" lw 2 with lines title "t = 5 sec",\
"time-0-dt-0.xy" lw 2 with lines title "t = 0 sec"
set title "Variation of Temperature along the length for DT = 0.01"
set output "0.01.png"
plot "time-2-dt-0.01.xy" lw 2 with lines title "t = 2 sec",\
"time-5-dt-0.01.xy" lw 2 with lines title "t = 5 sec",\
"time-0-dt-0.xy" lw 2 with lines title "t = 0 sec"
set title "Variation of Temperature along the length for DT = 1"
set xrange [0:10]
set output "1.png"
plot "time-2-dt-1.xy" lw 2 with lines title "t = 2 sec",\
"time-5-dt-1.xy" lw 2 with lines title "t = 5 sec",\
"time-0-dt-0.xy" lw 2 with lines title "t = 0 sec"
system("mv 0.png ~/Documents/latex-docs/project-4/0.png")
system("mv 0.0001.png ~/Documents/latex-docs/project-4/0.png")
system("mv 0.01.png ~/Documents/latex-docs/project-4/0.png")
system("mv 1.png ~/Documents/latex-docs/project-4/0.png")
\end{verbatim}
Note: Here the file \verb!time-0-dt-0.xy! needs to be manually exported from the paraview,since its nothing but the initial conditions or initial temperature field, it is common for all the cases.
We then execute the \verb!plot! file \\\\
\verb!>> ./plot!\\\\
The results are stored in the form of .png file.
\section*{Results-(Pure Diffusion)}
\begin{figure}[!htb]
\centering
\includegraphics[scale=0.45]{0.png}
\caption{diffusion coefficient(DT) = 0}
\end{figure}
\begin{figure}[!htb]
\centering
\includegraphics[scale=0.45]{0.0001.png}
\caption{diffusion coefficient(DT) = 0.0001}
\end{figure}
\pagebreak
\begin{figure}[!htb]
\centering
\includegraphics[scale=0.45]{0.01.png}
\caption{diffusion coefficient(DT) = 0.01}
\end{figure}
\begin{figure}[!htb]
\centering
\includegraphics[scale=0.45]{1.png}
\caption{diffusion coefficient(DT) = 1}
\end{figure}
\pagebreak
\section*{Conclusion(Pure Diffusion)}
From the above results it can be concluded that diffusion increases if the diffusion coefficient is increased. The temperature is most diffused in case of \verb!DT = 1!. However there is no convection since we have set our velocity field to be zero.
\section*{Procedure(Pure advection)}
\subsection*{Preparing the case for Pure advection}
For pure advection we need to set our velocity field to \verb!U(1 0 0)! and the diffusion coefficient \verb!DT! to be 0.
Further we also need to analyse the results for different discretisation schemes.
This can be automated with a shell script.
\subsection*{Automating the case to run for different discretisation schemes}
Here we intend to automate the following steps
\begin{itemize}
\item{Running the case for different divSchemes}
\item{Copying the extracted data in the data-padv folder}
\item{Plotting the variation of temperature along the line for different cases using gnuplot}
\end{itemize}
For this we create a \verb!Allrun-padv! shell script and to plot the extracted data we create a plot file in the data-padv folder.\\\\
We create a shell script by \\\\
\verb!>> touch Allrun-padv!\\\\
\verb!>> chmod 755 Allrun-padv!\\\\
\verb!>> code Allrun-padv!\\\\
We create the plot file in the data-padv after extracting the data by running \verb!Allrun-padv! script.
\pagebreak
\begin{verbatim}
#---------------------------------------------------------------------------------#
#!/usr/bin/bash
# Cleaning the case before running
rm -r {1..5}
rm -r postProcessing/
#Adding the settings for pure advection
sed -i "s/DT 0.01;/DT 0;/g" ./constant/transportProperties
sed -i "s/uniform (0 0 0)/uniform (1 0 0)/g" ./0/U
# Creating a directory to store all the data files
mkdir data-padv
# Making the mesh and setting the fields
blockMesh
setFields
COUNTER=0
SCHEMES=("QUICK" "upwind" "linearUpwind" "quadratic" "cubic" "vanLeer" "QUICK")
#echo ${SCHEMES[$((4))]}
for I in {0..5}
do
sed -i "s/${SCHEMES[${COUNTER}]} grad(T)/${SCHEMES[$((COUNTER+1))]} grad(T)/g"\
./system/fvSchemes
scalarTransportFoam
for J in {1..5}
do
# copying the data file from each timestep folder to data folder
NAME="time-${J}-${SCHEMES[$((COUNTER+1))]}.xy"
cp ./postProcessing/singleGraph/${J}/line_T.xy \
$FOAM_RUN/openfoam-cases/pitzDaily-scalarFoam/data-padv/${NAME}
done
rm -r {1..5}
rm -r postProcessing
COUNTER=$((COUNTER+1))
done
# Reverting the settings
sed -i "s/DT 0;/DT 0.01;/g" ./constant/transportProperties
sed -i "s/uniform (1 0 0)/uniform (0 0 0)/g" ./0/U
#---------------------------------------------------------------------------------#
\end{verbatim}
We can further execute the \verb!Allrun-padv! script by \\\\
\verb!>> ./Allrun-padv!\\\\
\pagebreak
This will run all the cases and extract the data in \verb!data-adv! folder \\
\verb!>> cd data-padv!\\\\
\verb!>> ls!
\begin{verbatim}
time-1-cubic.xy time-2-vanLeer.xy time-4-upwind.xy
time-1-linearUpwind.xy time-3-cubic.xy time-4-vanLeer.xy
time-1-QUICK.xy time-3-linearUpwind.xy time-5-cubic.xy
time-1-upwind.xy time-3-QUICK.xy time-5-linearUpwind.xy
time-1-vanLeer.xy time-3-upwind.xy time-5-QUICK.xy
time-2-cubic.xy time-3-vanLeer.xy time-5-upwind.xy
time-2-linearUpwind.xy time-4-cubic.xy time-5-vanLeer.xy
time-2-QUICK.xy time-4-linearUpwind.xy
time-2-upwind.xy time-4-QUICK.xy
\end{verbatim}
This is the extracted data which is reference as time and the discretisation scheme use, e.g. \verb!time-3-cubic.xy! is data from time = 3 sec from the discretisation scheme \verb!cubic!.
\\We can now create the \verb!plot! script to automate the graphing of all the data-files.
\\\\
\verb!>> cd data-padv!\\\\
\verb!>> touch plot && chmod 755 plot!\\\\
\verb!>> code plot!
\begin{verbatim}
#--------------------------------------------------------------#
#!/usr/bin/gnuplot
set terminal png size 1280, 800 font "Bahnschrift,22"
set title "Variation of Temperature along the length for DT = 0"
set xlabel "Length (m)"
set ylabel "T (K)"
set yrange [-0.3:1.3]
set xrange [4:8]
set grid xtics ytics
set title "Variation of Temperature along the length for vanLeer scheme"
set output "vanLeer.png"
plot "time-5-vanLeer.xy" lw 3 with lines title "t = 5 sec",\
"time-1-vanLeer.xy" lw 3 with lines title "t = 1 sec",\
"time-0-any.xy" lw 3 with lines title "t = 0 sec"
set title "Variation with all the linearUpwind and Upwind schemes at time = 5 sec"
set output "linearUpwind-upwind.png"
plot "time-5-upwind.xy" lw 3 with lines title "upwind",\
"time-5-linearUpwind.xy" lw 3 with lines title "linearUpwind",\
set title "Variation with all the cubic and VanLeer scheme at time = 5 sec"
set output "cubic-vanLeer.png"
plot "time-5-cubic.xy" lw 3 with lines title "cubic",\
"time-5-vanLeer.xy" lw 3 with lines title "vanLeer",\
set title "Variation with all the vanLeer and QUICK scheme at time = 5 sec"
set output "vanLeer-QUICK.png"
plot "time-5-vanLeer.xy" lw 3 with lines title "vanLeer",\
"time-5-QUICK.xy" lw 3 with lines title "QUICK",\
system("mv linearUpwind-upwind.png ~/Documents/latex-docs/project-4
/linearUpwind-upwind.png")
system("mv cubic-vanLeer.png ~/Documents/latex-docs/project-4
/cubic-vanLeer.png")
system("mv vanLeer-QUICK.png ~/Documents/latex-docs/project-4
/vanLeer-QUICK.png")
system("mv vanLeer.png ~/Documents/latex-docs/project-4
/vanLeer.png")
#--------------------------------------------------------------#
\end{verbatim}
Running the above script will plot the comparison for all the schemes
\begin{figure}[!htb]
\centering
\includegraphics[scale=0.5]{linearUpwind-upwind.png}
\caption{Comparison of linearUpwind and upwind schemes}
\end{figure}
\begin{figure}[!htb]
\centering
\includegraphics[scale=0.52]{cubic-vanLeer.png}
\caption{Comparison between cubic and vanLeer schemes}
\end{figure}
\begin{figure}[!htb]
\centering
\includegraphics[scale=0.52]{vanLeer-QUICK.png}
\caption{Comparison between vanLeer and QUICK schemes}
\end{figure}
\begin{figure}[!htb]
\centering
\includegraphics[scale=0.52]{vanLeer.png}
\caption{VanLeer Scheme}
\end{figure}
\pagebreak
\section*{Conclusion}
From the above comparisons it is clear that the vanLeer scheme has the least amount of numerical diffusion unlike the rest other schemes, in a case of pure advection.
\section*{References}
github: \href{https://github.com/kawalthakur/openfoam-cases/tree/main/pitzDaily-scalarFoam}{click-here}
\end{document}
The google drive link is as follows :
https://drive.google.com/drive/folders/1iEQ5QkqhYuPnatodum2c-TYWLc91rqn0?usp=share_link