Mixing Flow inside a T-junction Part1: Mesh Generation 06...

24
Mixing Flow inside a T-junction Part1: Mesh Generation 06 TJ Mesh Generation 1 Introduction The aim of this tutorial is to illustrate some of Code Saturne ’s pre-processing capabil- ities to generate meshes on-the-fly. The test case deals with the mixing of hot and cold flows inside a T-junction configuration (see figure 1 for the outline of the computational domain). T-junctions are very common in engineering applications, as for instance to sim- ulate thermal fatigue in nuclear application and for Heat, Ventilation and Air-conditioning (HVAC) investigations in comfort design. Figure 1: Outline of the T-junction geometry 2 Objectives The first objective of this exercise is to introduce the Code Saturne ’s pre-processing capabilities. Several operations can be performed with the pre-processor. For instance, it 1

Transcript of Mixing Flow inside a T-junction Part1: Mesh Generation 06...

Page 1: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mixing Flow inside a T-junctionPart1: Mesh Generation06 TJ Mesh Generation

1 IntroductionThe aim of this tutorial is to illustrate some of Code Saturne ’s pre-processing capabil-ities to generate meshes on-the-fly. The test case deals with the mixing of hot and coldflows inside a T-junction configuration (see figure 1 for the outline of the computationaldomain). T-junctions are very common in engineering applications, as for instance to sim-ulate thermal fatigue in nuclear application and for Heat, Ventilation and Air-conditioning(HVAC) investigations in comfort design.

Figure 1: Outline of the T-junction geometry

2 ObjectivesThe first objective of this exercise is to introduce the Code Saturne ’s pre-processingcapabilities. Several operations can be performed with the pre-processor. For instance, it

1

Page 2: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 2

might be used to build very large meshes by parametric manipulation, therefore makingfaster and easier the mesh generation stage. Pre-processing meshes using Code Saturne’s kernel (and not its pre-processor) is fully parallel and therefore meshes of the order ofmillions, if not billion of cells, can be generated very quickly. The second aim of thetutorial is to have a more in-depth experience using Code Saturne with user subroutines,particularly using the C part of the code.

3 InvestigationsThe T-junction is made of an horizontal branch (main) and a vertical one (side). The fluiddensity is ρ = 1.1786 kg/m3 and dynamic viscosity µ = 1.7808 × 10−5 Ns/m2. The hor-izontal branch height is a = 0.06 m and its length 23.5 × a. The vertical branch width isb = a/2 = 0.03 m and its length 13 × b. The bulk velocity is the same for both branches,i.e. UB = 2.833 m/s. The Reynolds number for the horizontal channel, based on the bulkviscosity is equal to ReB,H = ρUBa/µ = 11, 250. The Reynolds number for the verticalchannel, based on the bulk viscosity is equal to ReB,V = ReB,H/2 = 5, 625. The twofluid inlets are characterised by different temperatures, which are T = 12o C for the hor-izontal channel and T = 60o C for vertical.

Because of time constraints only the 2D T-junction is simulated. This is representativeof the flow in the mid-plane of the geometry, whereas the 3D effects are mainly confinedclose to the side walls.

This tutorial is divided into three parts:

1. Creation of a 2D channel with a constant node distribution in the stream-wise di-rection;

2. Creation of a 2D channel following a geometrical expansion of the nodes in thestream-wise direction

3. Creation of the 2D T-junction geometry with a non-conformal interface as a junctionbetween the horizontal and vertical branches.

The first two meshes are obtained extruding a 1D grid, which extends from y = 0 to y = aand it is characterised by a hyperbolic tangent distribution of the nodes in the wall normaldirection (see figure 2a). The full T-junction mesh is obtained using the aforementioned1D mesh for the horizontal channel and a second 1D mesh, which spans from x = −b tox = 0 to create the leg of the T, or vertical channel (see figure 2b).

4 Code Saturne

Code Saturne is a multipurpose CFD software designed to solve the Navier-Stokes equa-tions for 2D, 2D axisymmetric or 3D flows. Its main module is designed for the simulationof flows which may be steady or unsteady, laminar or turbulent, incompressible or poten-tially dilatable, isothermal or not. The code employs a finite volume discretisation andallows the use of various mesh types, including hybrid arrangements (where the meshcontains several kinds of elements) and structural non-conformities (hanging nodes).

Page 3: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 3

(a) 1D mesh for the horizontal channel (b) 1D mesh for the vertical channel

Figure 2: Meshes used to build the full T-junction. In both cases the boundary names arenumbered, following the order given in the figures

The code includes specific modules, referred to as ‘specific physics’, for the treatmentof Lagrangian particle tracking, semi-transparent radiative transfer, gas, pulverised coaland heavy fuel oil combustion, electricity effects (Joule effect and electric arcs), com-pressible flows, but these are not required for the present exercise.

Code Saturne is open-source; it can be downloaded, modified and/or redistributedunder the terms of the GNU General Public License as published by the Free SoftwareFoundation.

The following instructions provide a guide through the steps required to prepare andrun the present cases in Code Saturne version 3.0, and subsequently postprocess the re-sults using the open-source postprocessing software ParaView and xmgrace.

All necessary files are stored in a shared folder available for all the participants. A pdfcopy of this tutorial is to be found in (using the web browser):http://cfd.mace.manchester.ac.uk/twiki/bin/view/Saturne/CodeSaturneTutorialCPDunder the box Tutorial 06 - Part 1. It is recommended to open the pdf file, and to copyand paste the necessary commands to set up the case.

The command to open a pdf file is evince and should be used to open the file asfollows: evince 06_TJ_MeshGeneration.pdf

5 Transformation MatrixThe pre-processor within Code Saturne gives the possibility to manipulate meshes using atransformation matrix to every mesh given as input to the code. The actual Code Saturne

Page 4: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 4

matrix is 4 × 3 and has the following structure:a1,1 a1,2 a1,3

a2,1 a2,2 a2,3

a3,1 a3,2 a3,3

∣∣∣∣∣∣∣∣t1

t2

t3

(1)

where ai,j are the elements of a transformation matrix and ti is a translation vector. Figure3 gives three examples of (a) rotation with an angle of α, (b) a translation by 1.0 in thethird direction and (c) a dilatation by 2.0 in the third direction. Combinations of thesethree types of transformations are also possible to obtain more complex manipulations.

cos (α) − sin (α) 0.0

sin (α) cos (α) 0.0

0.0 0.0 1.0

∣∣∣∣∣∣∣0.0

0.0

0.0

(a) Rotation around the third directionaxis with no translation

1.0 0.0 0.0

0.0 1.0 0.0

0.0 0.0 1.0

∣∣∣∣∣∣∣0.0

0.0

1.0

(b) Translation of themesh in the third direc-tion

1.0 0.0 0.0

0.0 1.0 0.0

0.0 0.0 2.0

∣∣∣∣∣∣∣0.0

0.0

0.0

(c) Dilation matrix in thethird direction

Figure 3: Examples of transformation matrices in Code Saturne

6 Guide to run Code Saturne

In order to allow all the scripts/commands of Code Saturne to work, the path to the direc-tory, where the Code Saturne installation is located, has to be sourced. In the computersat the University of Manchester, this has already been done.

• Open a terminal as Applications - System Tools - Terminal

• Type code saturne. 1

If the path has been set up correctly, the following information should be displayedwhen typing code saturne:

[bash:$] code_saturneUsage: /opt/Code_Saturne/code_saturne-3.0.4/bin/code_saturne <topic>

Topics:helpautovnvcompile

1In a command line interpreter, such as bash, autocomplete of command names and file names may beaccomplished by keeping track of all the possible names of things the user may access. Here autocompleteis usually done by pressing the Tab key after typing the first several letters of the word. For example, ifthe only file in the current directory that starts with x is xLongFileName, the user may prefer to type xand autocomplete to the complete name. If there were another file name or command starting with x inthe same scope, usually the user would have to type some more letters or press the Tab key repeatedly todisambiguate what he or she means to the computer.

Page 5: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 5

configcreateguiinforunsalome

Options:-h, --help show this help message and exit

(Note: [bash:$] in this document represents the prompt. It might be different, depend-ing on the GNU-Linux installation and the instructions of the SHELL profile. There is noneed to type this.)

7 Test case setupThe mesh generation is divided into three main steps with the following objectives:

1. Generation of a 2D mesh using a uniform cell distribution in the direction of theextrusion:

• Understand and manipulate the transformation matrix;

• Understand and manipulate the mesh_input file given to Code Saturne ;

• Perform mesh joining.

2. Generation of a 2D mesh using a non-uniform cell distribution in the direction ofthe extrusion:

• Allocate and deallocate Code Saturne arrays within the C part of the code;

• Include new variables and functions in the C part of Code Saturne .

3. Creation of the full T-junction mesh.

The whole tutorial is based on the modification of the subroutine cs user mesh.c andits relative functions.

7.1 Creation of a 2D uniform mesh in the extrusion directionIn the terminal window the tutorial folder is created by typing

[bash:$] code_saturne create -s MeshCreation -c 2DChannel_Uniform

which yields

code_saturne 3.0.4 study/case generationo Creating study ’MeshCreation’...o Creating case ’2DChannel_Uniform’...

The file structure can be seen in figure 4. The details of the structure are:

• 2DChannel Uniform: This is the case file. A study can have several cases, forexample each using different turbulence models, different boundary conditions, etc.

Page 6: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 6

Figure 4: File structure created by Code Saturne

– DATA: This directory contains the script to launch the GUI, SaturneGUI.This is also where the restarting and other input files should be copied.

– SRC: This is where the user subroutines should be. During the compilationstage the code will re-compile all the subroutines present in this directory. Allthe user subroutines are available in the REFERENCE directory. Exampleshow to program them can be found in EXAMPLES. Depending on what isneeded for the simulation only a few of these might be required.

– RESU: This directory contains the files required for a simulation to run as wellas the result files.

– SCRIPTS: This is where the script to launch simulations is available.

• MESH: The program will read the mesh from this directory. The mesh formatsthat can be read are I-DEAS, CGNS, Gambit Neutral files (neu), EnSight, pro-star/STAR4 (ngeom), Gmsh, NUMECA Hex, MED, Simail and Meta-mesh files.

• POST: This is an empty directory designed to contain postprocessing macros thatmay be relevant in some cases.

The mesh ChMain_001x050x001_DyplB1.00_DyplT1.00.unv is to be foundat the following web pagehttp://cfd.mace.manchester.ac.uk/twiki/bin/view/Saturne/CodeSaturneTutorialCPDunder the box Tutorial 06 - Part 1. To download the file, right click onChMain_001x050x001_DyplB1.00_DyplT1.00.unvunder Mesh Files - Coarse Mesh - Horizontal (main) and choose Save link as. The meshfile has to be downloaded under the MESH directory of the study case MeshCreation.The other mesh file can be downloaded following the same procedure, by right click-ing on ChSide_034x001x001_DyplB1.00_DyplT1.00.unv under Mesh Files -

Page 7: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 7

Coarse Mesh - Vertical (side) and choosing Save link as.As already mentioned the whole tutorial is based on the modification of cs user mesh.cthat can be copied in the SRC directory from REFERENCE.

[bash:$] cd ../2DChannel_Uniform/SRC[bash:$] cp REFERENCE/cs_user_mesh.c .

If you open the subroutine you can find several functions, which are:

• cs user mesh input at line 102. This function is used to add and/or modify amesh input file;

• cs user join at line 152. This function is used to glue together several mesh inputfiles;

• cs user periodicity at line 291. This function is used to insert periodicitybetween boundary faces;

• cs user mesh warping at line 476. This function can be used to cut in trian-gles warp faces;

• cs user mesh thinwall at line 496. This function allows to insert a thin wallinto a mesh;

• cs user mesh modify at line 549. This function can be used to modify directlythe mesh;

• cs user mesh smoothe at line 589. This function can be used to smooth themesh in case of poor mesh quality;

• cs user mesh save at line 624. This function enables/disables mesh saving(active by default);

• cs user mesh bad cells tag at line 644. This function tags bad cells withinthe mesh.

Every function contains some examples and they are all deactivated using if(false)statement. In this tutorial only two functions will be used namely cs user mesh inputand cs user join.

7.1.1 cs user mesh input

The strategy, that is going to be adopted to perform the extrusion, consists of reading ntimes the 1D mesh and translating every time the 1D mesh in the stream-wise direction(x in this case). This is done using the following transformation matrix

1.0 0.0 0.0

0.0 1.0 0.0

0.0 0.0 1.0

∣∣∣∣∣∣∣∣ii ∗ ∆x0

0.0

0.0

Page 8: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 8

where ii = 0, ...n − 1 with n the number of copies and ∆x0 the thickness of the singlecell in the x-direction.

The steps below are followed:

• Cleaning the function cs user mesh input to get:

voidcs_user_mesh_input(void){

}

• Declaration and initialisation of the variables and parameters:

– Integer variables:

int ii, n_ren;

– Parameters for the extrusion:

double static ch_main_thickness = 5.e-3;/* Delta x */static int ch_main_direction = 0;/* 0 for x,

1 for y,2 for z*/

static int ch_main_layers = 10; /* Number of cells *//* in the stream-wise direction */

– Declaration of two arrays for the translation and dilatation vectors and theirinitialisation:

/*=========================================*//* Creation of the channel */double trans[3] = {0.0, 0.0, 0.0};double dilat[3] = {1.0, 1.0, 1.0};

• Creation of the loop for the extrusion:

/* loop over the extrusion */for (ii=0; ii < ch_main_layers; ii++) {

• Creation of the transformation matrix:

double ii2 = ii;bft_printf("ii %d\n", ii); /* This command print

directly to the listing*/trans[ch_main_direction]=ii2*ch_main_thickness;bft_printf("Section %d translation vector %f %f %f \n",ii+1,

trans[0], trans[1], trans[2]);const double transf_matrix[3][4] ={{ 1., 0., 0., trans[0]},

{ 0., 1., 0., trans[1]},{ 0., 0., 1., trans[2]}};

Page 9: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 9

• Change of the boundary names to facilitate boundary condition settings and join-ing operations. All the faces to be joined will be gathered under the same namejoin01. Identifying the faces to be joined is performed in 3 steps, as only 1boundary is used for joining the first and second (resp. the last and the one beforethe last) 1D meshes, whereas 2 boundaries are used for the other 1D meshes.The parameters are passed to the function cs preprocessor data add file,which adds mesh files and performs the necessary transformations.

At this stage, Code Saturne preprocessor will have preprocessed the fileChMain_001x050x001_DyplB1.00_DyplT1.00.unv to Code Saturne in-ternal format, assigning it a new name: mesh input, which is used in the fol-lowing.

– Joining the first and second 1D meshes: left side (4) becomes inlet, rightside (2) becomes a join join01 and bottom (1) and top (3) sides becomewall

if (ii == 0){n_ren = 4; /* Number of BC renamed */const char *renames[] = {"2","join01",

"4","inlet","3","wall","1","wall"};

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

}

– Joining the last and the one before last 1D meshes: left side (4) becomes ajoin join01, right side (2) becomes outlet and bottom (1) and top (3)sides become wall

else if (ii == ch_main_layers-1) {n_ren = 4;const char *renames[] = {"4","join01",

"2","outlet","3","wall","1","wall"};

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

}

– Joining the other 1D meshes: left (4) and right side (2) become a join join01and bottom (1) and top (3) sides become wall

else {n_ren = 4;const char *renames[] = {"2","join01",

"4","join01","3","wall","1","wall"};

Page 10: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 10

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

}; /* End of the if statement */

}; /* End of the loop */

The full function should looks:

voidcs_user_mesh_input(void){

int ii, n_ren;/*=========================================*//* Parameter definition for the extrusion */double static ch_main_thickness = 5.e-3;static int ch_main_direction = 0; /* 0 for x, 1 for y, 2 for z*/static int ch_main_layers = 10; /* Number of cells

in the stream-wise direction*//*=========================================*//* Creation of the channel */double trans[3] = {0.0, 0.0, 0.0};double dilat[3] = {1.0, 1.0, 1.0};/* loop over the extrusion */for (ii=0; ii < ch_main_layers; ii++) {

double ii2 = ii;bft_printf("ii %d\n", ii);trans[ch_main_direction]=ii2*ch_main_thickness;bft_printf("Section %d translation vector %f %f %f \n",ii+1,

trans[0], trans[1], trans[2]);const double transf_matrix[3][4] ={{ 1., 0., 0., trans[0]},

{ 0., 1., 0., trans[1]},{ 0., 0., 1., trans[2]}};

if (ii == 0){n_ren = 4; /* Number of BC renamed */const char *renames[] = {"2","join01",

"4","inlet","3","wall","1","wall"};

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

}else if (ii == ch_main_layers-1) {

n_ren = 4;const char *renames[] = {"4","join01",

"2","outlet","3","wall","1","wall"};

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

}else {

n_ren = 4;

Page 11: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 11

const char *renames[] = {"2","join01","4","join01","3","wall","1","wall"};

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

};

};}

7.1.2 cs user join

This function is used to glue together all the n 1D meshes to obtain a single mesh. Thefaces, which are going to be glued, are the ones named join01.

The steps below are followed:

• Cleaning the function cs user join to get:

voidcs_user_join(void){

}

• Declaration of the variables:

int join_num;

/* Add a joining operation *//* ----------------------- */

int verbosity = 1; /* per-task dump if > 1,debug level if >= 3 */

int visualization = 1; /* debug level if >= 3 *//* join of faces at the outlet branch */float fraction = 0.10, plane = 25.;

where fraction is the tolerance, expressed in % of the smallest edge of the face,used to merge together two nodes and plane is the angle, expressed in o, betweentwo normals to the face to be considered coplanar.

• Calling the function for joining:

join_num = cs_join_add("join01",fraction,plane,verbosity,visualization);

The full function should looks:

Page 12: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 12

voidcs_user_join(void){

int join_num;

/* Add a joining operation *//* ----------------------- */

int verbosity = 1; /* per-task dump if > 1,debug level if >= 3 */

int visualization = 1; /* debug level if >= 3 *//* join of faces at the outlet branch */float fraction = 0.10, plane = 25.;join_num = cs_join_add("join01",

fraction,plane,verbosity,visualization);

}

7.1.3 Compilation of the code

Once all subroutines have been modified the code should be compiled as:

[bash:$] code_saturne compile

in order to check that no compilation errors occur.

7.1.4 Test case running

Before running the simulation the mesh file name needs to be passed to the code, by goinginto DATA.

• Copy the file cs user scripts.py from REFERENCE in the current directoryand open the file;

• Open the file and modify line 166 using the mesh file name.

if domain.param == None:domain.meshes = ["ChMain_001x050x001_DyplB1.00_DyplT1.00.unv"]

• At line 197 add the command to use Code Saturne in preprocessor mode only

if domain.param == None:domain.exec_solver = Truedomain.solver_args = "--preprocess"

To run the simulation go up one directory and into SCRIPTS and type in the terminal

[bash:$] cd ../SCRIPTS[bash:$] ./runcase &

Page 13: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 13

7.1.5 Visualisation of the results

Once the calculation of Code Saturne is finished, the results are copied into the RESUdirectory under a subdirectory with the date (YYYYMMDD-HHMM). This directoryreads:

• The file compile.log if any subroutine is present in the SRC directory.

• The preprocessor.log file contains the output of the pre-processor whichreads the mesh and passes the information to the solver

• The listing file has information about all the parameters of the calculation andother useful data to monitor the convergence to the solution

• The performance.log file has more information about the performance of thewhole simulation.

• The setup.log file contains information about how the simulation has been setup

• The monitoring directory contains ASCII files with the history of monitoredvalues for each variable defined by the user

• The postprocessing directory contains the results files in EnSight format.These files can be viewed with ParaView directly

• The mesh_input and/or mesh_output files contain the mesh in the Code Saturneformat

• Additionally, a copy of the src saturne directory, running scripts (for instance,runcase) and potential xml files used for the calculation are saved

• The summary file

Go under the directory postprocessing and start paraview typing[bash:$] cd postprocessing[bash:$] paraview

Go to the open icon and three different sets of outputs are produced by Code Saturne

• BOUNDARY_GROUPS.case: ensight case with all the boundary groups;

• JOINING.case: ensight case with all cells and faces modified by the joiningoperation(s);

• MESH_GROUPS.case: ensight case with all the mesh groups (boundary faces,internal faces and volumes;

• RESULTS.case: ensight case with the eventual results from the simulation.

Open the BOUNDARY_GROUPS.case and click on Apply. Now on the second panelfrom the top change the visualization from Surface to Surface With Edges and you shouldbe able to see the 2D mesh as in Figure 5. If the Extract Block filter is applied all theboundary names can be seen on the Properties Panel and visualised.

Page 14: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 14

Figure 5: Paraview screen-shot with the uniform 2D channel

7.2 Creation of a 2D non-uniform mesh in the extrusion directionThe second part of this tutorial deals with the creation of a 2D mesh, but with a non-uniform (geometrical in this specific case) mesh extrusion. This new Code Saturne casecan be created in the same study MeshCreation and using as a starting point the previ-ous case 2DChannel Uniform. Firstly go back to the study directory MeshCreationusing the cd command

[bash:$] cd ../../../../[bash:$] ls2DChannel_Uniform/ MESH/ POST/

The new case is created by copying the previous case, 2DChannel Uniform, as fol-lows:

[bash:$] code_saturne create -c 2DChannel_NonUni \--copy-from=2DChannel_Uniform

code_saturne 3.0.4 study/case generationo Creating case ’2DChannel_NonUni’...

The updated file structure is presented in figure 6.

Page 15: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 15

Figure 6: File structure created by Code Saturne

Since the study has been created the subroutine cs user mesh.c, previously cre-ated, is already available in the SRC directory.[bash:$] cd 2DChannel_NonUni/SRC[bash:$] lsEXAMPLES REFERENCE cs_user_mesh.c

The geometrical distribution for the mesh stretching reads

xi = ∆x0 · ri with i = 0, ...n− 1

where ∆x0 is the thickness of the first single cell in the x-direction, n the number of pointsand r is an expansion factor. The expansion factor r can be evaluated using a Newton-Raphson method, which receives as input the number of points n and the total extrusionlength l.

The first step is to create a new a user defined function in cs user mesh.c.

• Insert the following declaration at line 91 just before BEGIN_C_DECLS

void geo_expansion(double eps, double l, double dx0, int n,int direction[2],double *delta_cell, double *trans_cell);

BEGIN_C_DECLS

• Insertion of the function to compute the expansion factor r after/*==============================================================

* User function definitions

*==============================================================*/

Page 16: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 16

at line 101.

void geo_expansion(double eps, double l, double dx0, int layers,int direction[2],double *delta_cell, double *trans_cell)

{static int imax=1000;int ii = 0;double r=2.0;double rold;double err = 1.e10;double fval, fpr;double n = layers;/* Loop for Newton-Raphson to find r */while ((err>eps)&&(ii<=imax)){

ii ++;rold = r;fval = dx0*pow(r,n)-l*r+(l-dx0);fpr = n*dx0*pow(r,n-1.0)-l;r = rold-fval/fpr;

}printf ("l %f \n", l);printf ("ratio %f \n", r);printf ("n %f \n", n);printf ("dir %d \n", direction[1]);for (ii=0; ii < layers; ii++){

delta_cell[ii]=dx0*pow(r,ii);printf ("delta %d %f \n", ii+1, delta_cell[ii]);

};/* Calculation of the translation distance

Remember that is different in the case ofpositive or negative direction of the axis */

if (direction[1]==1) {trans_cell[0]=0.0;for (ii=1; ii < n; ii++){

trans_cell[ii]=trans_cell[ii-1]+direction[1]*delta_cell[ii-1];}

} else {trans_cell[0]=-dx0;for (ii=1; ii < layers; ii++){

trans_cell[ii]=trans_cell[ii-1]-delta_cell[ii];}

};/* Print of the translation distance array */for (ii=0; ii < layers; ii++){

printf ("trans %d %f \n", ii+1, trans_cell[ii]);};

}

Page 17: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 17

The function cs user mesh input is updated to be able to generate a non-uniformmesh. The transformation matrix, on which the extrusion is based, reads:

∆xn/∆x0 0.0 0.0

0.0 1.0 0.0

0.0 0.0 1.0

∣∣∣∣∣∣∣∣xn − x0

0.0

0.0

• Declaration and initialisation of the variables and parameters.

– General variables:

int ii, n_ren;static double eps=5.0e-3;/* Stopping criteria for

calculating expansion factor r*/

– Parameters for the extrusion:

/* Parameter definition for the extrusion */double static ch_main_thickness = 5.e-3;int static ch_main_direction[2] = {0,1};double static ch_main_length = 0.1;int static ch_main_layers = 10;double static ch_main_start_pos = 0.0;double static ch_main_first_delta = 5.e-4;double *ch_main_delta_cell;double *ch_main_trans_cell;

– Memory allocation:

/*=========================================*//* Creation of the channel */double trans[3] = {0.0, 0.0, 0.0};double dilat[3] = {1.0, 1.0, 1.0};/* Extrusion main channel-outlet *//* memory allocation */BFT_MALLOC(ch_main_delta_cell, ch_main_layers+1, double);BFT_MALLOC(ch_main_trans_cell, ch_main_layers+1, double);for (ii=0; ii<=ch_main_layers; ii++)

ch_main_delta_cell[ii] = 0.0;for (ii=0; ii<=ch_main_layers; ii++)

ch_main_trans_cell[ii] = 0.0;/* Calling the function to compute the expansion */geo_expansion(eps, ch_main_length, ch_main_first_delta,

ch_main_layers, ch_main_direction,ch_main_delta_cell, ch_main_trans_cell);

• Creation of the transformation matrix:

/* loop over the extrusion */for (ii=0; ii < ch_main_layers; ii++) {

double ii2 = ii;bft_printf("ii %d\n", ii);bft_printf("ch_main_direction[0] %d\n",

ch_main_direction[0]);

Page 18: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 18

dilat[ch_main_direction[0]]=ch_main_delta_cell[ii]/ch_main_thickness;

trans[ch_main_direction[0]]=ch_main_trans_cell[ii]+ch_main_start_pos;

bft_printf("delta %f trans %f\n",dilat[ch_main_direction[0]],trans[ch_main_direction[0]]);

• The boundary face name changes are the same as for the uniform case:

if (ii == 0){n_ren = 4; /* Number of BC renamed */const double transf_matrix[3][4] =

{{dilat[0], 0., 0., trans[0]},{ 0., dilat[1], 0., trans[1]},{ 0., 0.,dilat[2], trans[2]}};

const char *renames[] = {"2","join01","4","inlet","3","wall","1","wall"};

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

}else if (ii == ch_main_layers-1) {

n_ren = 4;const double transf_matrix[3][4] =

{{dilat[0], 0., 0., trans[0]},{ 0., dilat[1], 0., trans[1]},{ 0., 0.,dilat[2], trans[2]}};

const char *renames[] = {"4","join01","2","outlet","3","wall","1","wall"};

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

}else {

n_ren = 4;const double transf_matrix[3][4] =

{{dilat[0], 0., 0., trans[0]},{ 0., dilat[1], 0., trans[1]},{ 0., 0.,dilat[2], trans[2]}};

const char *renames[] = {"2","join01","4","join01","3","wall","1","wall"};

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

};};

• Deallocation of the local arrays:

Page 19: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 19

BFT_FREE(ch_main_delta_cell);BFT_FREE(ch_main_trans_cell);

}

The full function should looks:

voidcs_user_mesh_input(void){

int ii, n_ren;static double eps=5.0e-3; /* Stopping criteria for

calculating expansion factor*//*=========================================*//* Parameter definition for the extrusion */double static ch_main_thickness = 5.e-3;int static ch_main_direction[2] = {0,1};double static ch_main_length = 0.1;int static ch_main_layers = 10;double static ch_main_start_pos = 0.0;double static ch_main_first_delta = 5.e-3;double *ch_main_delta_cell;double *ch_main_trans_cell;/*=========================================*//* Creation of the channel */double trans[3] = {0.0, 0.0, 0.0};double dilat[3] = {1.0, 1.0, 1.0};/* Extrusion main channel-outlet *//* memory allocation */BFT_MALLOC(ch_main_delta_cell, ch_main_layers+1, double);BFT_MALLOC(ch_main_trans_cell, ch_main_layers+1, double);for (ii=0; ii<=ch_main_layers; ii++) ch_main_delta_cell[ii] = 0.0;for (ii=0; ii<=ch_main_layers; ii++) ch_main_trans_cell[ii] = 0.0;/* Call to the function to compute the geometrical distribution */geo_expansion(eps, ch_main_length, ch_main_first_delta,

ch_main_layers, ch_main_direction,ch_main_delta_cell, ch_main_trans_cell);

/* loop over the extrusion */for (ii=0; ii < ch_main_layers; ii++) {

double ii2 = ii;bft_printf("ii %d\n", ii);bft_printf("ch_main_direction[0] %d\n", ch_main_direction[0]);dilat[ch_main_direction[0]]=ch_main_delta_cell[ii]/ch_main_thickness;

trans[ch_main_direction[0]]=ch_main_trans_cell[ii]+ch_main_start_pos;

bft_printf("delta %f trans %f\n",dilat[ch_main_direction[0]],trans[ch_main_direction[0]]);

if (ii == 0){const char *renames[] = {"2","join01",

"4","inlet","3","wall","1","wall"};

n_ren = 4;

Page 20: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 20

const double transf_matrix[3][4] ={{dilat[0], 0., 0., trans[0]},{ 0., dilat[1], 0., trans[1]},{ 0., 0.,dilat[2], trans[2]}};

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

}else if (ii == ch_main_layers-1) {

const char *renames[] = {"4","join01","2","outlet","3","wall","1","wall"};

n_ren = 4;const double transf_matrix[3][4] =

{{dilat[0], 0., 0., trans[0]},{ 0., dilat[1], 0., trans[1]},{ 0., 0.,dilat[2], trans[2]}};

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

}else {

const char *renames[] = {"2","join01","4","join01","3","wall","1","wall"};

n_ren = 4;const double transf_matrix[3][4] =

{{dilat[0], 0., 0., trans[0]},{ 0., dilat[1], 0., trans[1]},{ 0., 0.,dilat[2], trans[2]}};

cs_preprocessor_data_add_file("mesh_input",n_ren, renames,transf_matrix);

};

};BFT_FREE(ch_main_delta_cell);BFT_FREE(ch_main_trans_cell);

}

7.2.1 cs user join

The function cs user join does not need any modification with respect to the previouscase.

Page 21: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 21

7.2.2 Compilation of the code

Once all subroutines have been modified the code should be compiled as:

[bash:$] code_saturne compile

in order to check that no compilation errors occur.

7.2.3 Test case running

The python scrip cs user scripts.py is already available in DATA and adapted forthe case. Therefore the simulation can be run directly going into SCRIPTS and typing inthe terminal

[bash:$] cd ../SCRIPTS[bash:$] ./runcase &

7.2.4 Visualisation of the results

Once the calculation of Code Saturne is finished, the procedure to see the mesh createdis the same as the one in Section 7.1.5 and the mesh should looks like the one in Figure 7.

Figure 7: Paraview screen-shot with the non-uniform 2D channel

7.3 Creation of the full 2D T-junctionThe third part of this tutorial focuses on creating the whole the T-junction mesh. The T-junction can be divided into 4 blocks as presented in figure 8. Every block might be madeof a uniform mesh, as in Section 7.1, or an non-uniform mesh as in Section 7.2. Because

Page 22: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 22

no new element, with respect to the previous two cases, is introduced the subroutinecs user mesh.c to generate the full T-junction will be downloaded from the web. For

Figure 8: Block division of the T-junction

the full T-junction, the following parameters are separately set for each block as follows:

• Main Inlet: geometrical distribution

double static main_inlet_thickness = 5.e-3;int static main_inlet_direction[2] = {0,-1};double static main_inlet_length = 0.6;int static main_inlet_layers = 96;double static main_inlet_start_pos = -0.03;double static main_inlet_first_delta = 4.7e-4;

• Main Junction: uniform distribution

double static main_junction_thickness = 5.e-3;int static main_junction_direction[2] = {0,-1};double static main_junction_length = 0.03;double static main_junction_start_pos = 0.0;int static main_junction_layers = 64;

• Main Outlet: geometrical distribution

double static main_outlet_thickness = 5.e-3;int static main_outlet_direction[2] = {0,1};double static main_outlet_length = 0.8;

Page 23: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 23

int static main_outlet_layers = 128;double static main_outlet_start_pos = 0.0;double static main_outlet_first_delta = 4.7e-4;

• Side Inlet: geometrical distributiondouble static side_inlet_thickness = 5.e-3;int static side_inlet_direction[2] = {1,-1};double static side_inlet_length = 0.4;int static side_inlet_layers = 64;double static side_inlet_start_pos = 0.0;double static side_inlet_first_delta = 4.7e-4;

Now the CASE can be created in the study directory. Locate yourself in the proper direc-tory using:[bash:$] cd ../../../../[bash:$] ls2DChannel_Uniform/ MESH/ 2DChannel_NonUni/ POST/

Once in the appropriate directory, create the case typing[bash:$] code_saturne create -c 2D_Tjunctioncode_saturne 3.0.4 study/case generation

o Creating case ’2D_Tjunction’...

The full version of cs user mesh.c can be downloaded from the web pagehttp://cfd.mace.manchester.ac.uk/twiki/bin/view/Saturne/CodeSaturneTutorialCPDunder the box Tutorial 06 - Part 1 - Full 2D T-Junction and familiarize yourself withthe subroutine. Essentially the code used in the previous two parts has been adaptedfor every block. Two remarks are necessary at this point: 1) Code Saturne is going toreceive two different meshes as input: one for the vertical channel and another for thehorizontal. The preprocessor of Code Saturne creates always an output for the meshcalled mesh input, but being not a file but a directory. Inside the directory there are asmany files as the number of inputs with the following name convention: mesh nn wherenn is the mesh number. Indeed, the numbering will follow the order with which the meshfiles are given to Code Saturne . 2) More than one join operation is necessary to obtainthe full mesh.

Once cs user mesh.c has been downloaded and you have familiarised with allthe differences with respect to the two previous cases, it is possible to run the case. Gounder the folder DATA and copy the python script cs user scripts.py from theREFERENCE directory. Open the file and introduce the mesh names at line 166

if domain.param == None:domain.meshes = ["ChMain_001x050x001_DyplB1.00_DyplT1.00.unv",

"ChSide_034x001x001_DyplB1.00_DyplT1.00.unv"]

Remember that the mesh for the side channel should have been already downloaded. Ifnot, the mesh can be found in the web page for the tutorial. At line 198 add command touse Code Saturne in preprocessor mode only.

if domain.param == None:domain.exec_solver = Truedomain.solver_args = "--preprocess"

Page 24: Mixing Flow inside a T-junction Part1: Mesh Generation 06 ...cfd.mace.manchester.ac.uk/twiki/pub/Saturne/CodeSaturneTutorialCP… · Mixing Flow inside a T-junction Part1: Mesh Generation

Mesh Generation 24

Go now into SCRIPTS and type in the terminal

[bash:$] cd ../SCRIPTS[bash:$] ./runcase &

for running the case

7.3.1 Visualisation of the results

Once the calculation of Code Saturne is finished, the procedure to see the mesh created isthe same as the one in Section 7.1.5. The non conformal mesh around the junction shouldlooks like the one in figure 9.

Figure 9: Paraview screen-shot of the T-junction mesh around the non-conformal mesh