You are on page 1of 136

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/327594760

OpenFOAM course for beginners: Hands-on training

Presentation · September 2018


DOI: 10.5281/zenodo.1435601

CITATIONS READS

0 3,683

1 author:

Jibran Haider
Swansea University
9 PUBLICATIONS   18 CITATIONS   

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Explicit solid dynamics in OpenFOAM View project

All content following this page was uploaded by Jibran Haider on 12 September 2018.

The user has requested enhancement of the downloaded file.


Introduction Code Simulations Programming Tutorials Conclusions Appendix

OpenFOAM course for beginners:


Hands-on training

Jibran Haider

Zienkiewicz Centre for Computational Engineering (ZCCE),


College of Engineering, Swansea University, UK

https://www.jibranhaider.weebly.com

12 th September 2018

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 1


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Disclaimer

“ This offering is not approved or endorsed by


OpenCFD Limited, producer and distributor of the
OpenFOAM software and owner of the
OPENFOAM R
and OpenCFD R
trade marks.”

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 2


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 3


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction
My background
Course details
OpenFOAM preliminaries

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 4


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction
My background
Course details
OpenFOAM preliminaries

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 5


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Experience in computational mechanics

Year Topic Software Institution

[2009] CFD and experimental study of flow over FLUENT NUST


cylindrical fins.
[2012] Numerical simulation of sloshing FLOW-3D Universität Stuttgart & DLR
phenomena in complex tanks.
Modeling of phase change phenomena
[2013] OpenFOAM Universität Stuttgart & DLR
through a liquid-vapor interface in
microgravity.
[2014] A first order hyperbolic framework for large OpenFOAM Swansea University & UPC
strain fast solid dynamics.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 6


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction
My background
Course details
OpenFOAM preliminaries

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 7


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Course details

Objectives
• Introduce the basics of OpenFOAM.
• Signify the advantages of using OpenFOAM.
• Utilise the power of Linux by extensive use of shell commands.
• By the end of the course, candidates should be able to run OpenFOAM simulations.

Desirable traits
• Basic level of programming in C++.
• Familiarization with Linux interface.
• Fundamentals of Computational Fluid Dynamics (CFD).

During this course we will be using

• OpenFOAM Foundation release (version 6).


• ParaView (version 5.4.0).

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 8


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction
My background
Course details
OpenFOAM preliminaries

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 9


Introduction Code Simulations Programming Tutorials Conclusions Appendix

What is OpenFOAM?

Summary
• An open source software package.
• Released under the GNU General Public License.
• Abbreviation of Open Field Operation And Manipulation.
• An efficient library of C++ modules.
• Based on the cell centered Finite Volume Method.
• Extensive CFD and Multi-physics capabilities.
kC e f k Ω0e
• Cross-platform installation.

e F CN e f
OpenFOAM forks
• OpenFOAM Foundation release (official standard release).
• OpenFOAM + (official development release).
• FOAM-extend (community driven development branch).

 This blog post [link] provides plenty of free references available on the web which might be a good
starting point for learning OpenFOAM.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 10


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Key features of OpenFOAM

OpenFOAM is a C++ library of highest quality of programming for solving


computational continuum mechanics problems utilising all the features of
Objected Oriented Programming (OOP).

Key features of OOP


• Abstraction
• Inheritance
• Polymorphism
Objects of different classes respond differently to functions of the same name.
Also includes operator overloading.

Template metaprogramming
• C++ provides template classes.
• General features of the template class are passed on to any other class created from it.
• This reduces code duplication.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 11


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Why OpenFOAM?

Commercial Software
OpenFOAM
(ANSYS, ABAQUS etc.)

1. Cost effectiveness X ×
2. Parallel computing X X
3. Source code X ×
4. Redistribution of code X ×
5. Collaborative development X ×
6. Documentation × X
7. GUI / user friendliness × X

X Motivates collaborative and project-driven development.

X According to the GNU GPL v3, OpenFOAM is free to download, install, use, modify and distribute.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 12


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Development of OpenFOAM

1989 Project was initially started under the title of FOAM at Imperial college, London.
The idea was to create a simulation platform more powerful than FORTRAN. Due
to its object oriented features C++ was chosen as the programming language.

10th Dec 2004 OpenCFD Ltd was founded and first version (v1.0) of OpenFOAM was released
under the GNU GPL license.

8th Aug 2011 OpenCFD was acquired by Silicon Graphics International (SGI).

15th Aug 2012 OpenCFD became a wholly owned subsidiary of ESI group.
th
6 Aug 2018 OpenFOAM v6 was released.

 Henry Weller and Hrvoje Jasak are the main initial contributors to its development.
 Several developers from all over the globe have contributed to its development.
 OPENFOAM
R
is a registered trade mark of OpenCFD Limited, producer and distributor of the
OpenFOAM software.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 13


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Simulation workflow

Preprocessing Solver Postprocessing

- blockMesh - Incompressible - OpenFOAM utilities


- snappyHexMesh - Compressible
- OpenFOAM utilities - Multiphase
- Combustion - ParaView - VISIT
- Gmsh - Netgen - Heat transfer - OpenDX - Netfabb
- Engrid - Tetgen - SALOME - Gnuplot
- SALOME - Blender
- Molecular dynamics
- Electromagnetics - ANSYS - EnSight
- ANSYS
- Solid mechanics - FieldView - Tecplot
- Star-CCM+
- SOLIDWORKS

OpenFOAM Other open source Commercial

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 14


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Conventions used in this presentation

OpenFOAM terminologies
OpenFOAM_terminology

OpenFOAM command for applications


OpenFOAM_application

Commands on terminal
<< command_name

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 15


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Environment variables & aliases

Environment variables are preconfigured to important OpenFOAM directories.

<< env | grep ˆFOAM_

FOAM_SOLVERS= / o p t / openfoam6 / a p p l i c a t i o n s / s o l v e r s
FOAM_EXT_LIBBIN = / o p t / T h i r d P a r t y −6/p l a t f o r m s / linux64GccDPInt32 / l i b
FOAM_APPBIN= / o p t / openfoam6 / p l a t f o r m s / linux64GccDPInt32Opt / b i n
FOAM_TUTORIALS= / o p t / openfoam6 / t u t o r i a l s
.....

Aliases make use of environment variables to navigate inside OpenFOAM sources.

<< cat $WM_PROJECT_DIR/etc/config.sh/aliases

.....
62 a l i a s s r c = ’ cd $FOAM_SRC ’
63 a l i a s l i b = ’ cd $FOAM_LIBBIN ’
64 a l i a s app= ’ cd $FOAM_APP ’
.....

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 16


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code


Code structure
Applications
Solvers
Utilities

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 17


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code


Code structure
Applications
Solvers
Utilities

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 18


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Installation directory I

$WM_PROJECT_DIR
1. applications:
applications alias: app = ’cd $FOAM_APP’.
This directory contains the source files of all the
solvers executables created using the C++ libraries. It contains
the following directories:
test
1.1 solvers:
utilities alias: sol = ’cd $FOAM_SOLVERS’.
tutorials Source code to solve a particular continuum
mechanics problem.
src
1.2 test:
bin Sample codes to help understand the usage of
OpenFOAM libraries.
doc
1.3 Utilities:
etc alias: util = ’cd $FOAM_UTILITIES’.
Source code to perform pre- and post-processing
platforms tasks involving data manipulation and algebraic
manipulations.
wmake

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 19


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Installation directory II

$WM_PROJECT_DIR

applications 2. tutorials:
alias: tut = ’cd $FOAM_TUTORIALS’.
tutorials Contains tutorials that demonstrate the usage of all
solvers and most of the utilities.
src

3. src:
finiteVolume alias: src = ’cd $FOAM_SRC’.
OpenFOAM It contains several subdirectories which include the
source code for all libraries. The important folders are:
turbulenceModels
3.1 finiteVolume:
... alias: foamfv = ’cd $FOAM_SRC/finiteVolume’.
bin Includes classes for finite volume space/time
discretisation, boundary conditions etc.
doc
3.2 OpenFOAM:
etc This core library includes important definitions.

platforms 3.3 turbulenceModels:


Contains libraries for turbulence models.
wmake

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 20


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Installation directory III

4. bin:
This directory contains shell scripts such as paraFoam,
foamLog etc.
$WM_PROJECT_DIR
5. doc:
applications It contains all the documentation relevant to the version of
OpenFOAM including:
tutorials 5.1 User and Programmer’s guides
5.2 Doxygen generated documentation
src 5.3 OpenFOAM coding style guide
bin 6. etc:
It contains global OpenFOAM dictionaries and setup files.
doc
7. platforms:
etc
The binaries generated during the compilation of the
platforms applications and the dynamic libraries are stored here.

8. wmake:
wmake
Compiler settings are included in this directory including
optimisation flags. It also contains wmake, a special make
command which understands the OpenFOAM file structure.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 21


Introduction Code Simulations Programming Tutorials Conclusions Appendix

User’s working directory

The path to user’s working directory is stored in $WM_PROJECT_USER_DIR.

Check the path of working directory


<< echo $WM_PROJECT_USER_DIR

/ home / j i b r a n / OpenFOAM / j i b r a n −6

Create the run directory


<< mkdir -p $FOAM_RUN

Check the path of ’run’ directory


<< echo $FOAM_RUN

/ home / j i b r a n / OpenFOAM / j i b r a n −6/run

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 22


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code


Code structure
Applications
Solvers
Utilities

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 23


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Directory structure

1. appName.C:
Contains the main source code of the application.

2. *.H:
<application>
The necessary header files needed to compile the application.
The solvers include createFields.H file which declares and
appName.C initialises the field variables.

*.H
3. Make:
Make This directory contains the compilation instructions and
includes the following files:
files 3.1 files:
List of source files (*.C) needed to compile the
options
application and the executable name.
3.2 options:
Specifies the directories to search for the included
header files (*.H) and libraries needed to link the
application against.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 24


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code


Code structure
Applications
Solvers
Utilities

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 25


Introduction Code Simulations Programming Tutorials Conclusions Appendix

OpenFOAM solvers I

X Over 70+ OpenFOAM solvers.

Basic CFD solvers

Solver Description

1. laplacianFoam Solves a Laplace equation.


2. potentialFoam Solves for potential flow.

Incompressible flow

Solver Description

1. icoFoam Transient solver for incompressible, laminar flow of Newtonian fluids.


2. simpleFoam Steady state solver for incompressible, turbulent flow.
3. pisoFoam Transient solver for incompressible flow.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 26


Introduction Code Simulations Programming Tutorials Conclusions Appendix

OpenFOAM solvers II

Compressible flow

Solver Description

Density based compressible flow solver based on cen-


1. rhoCentralFoam
tral upwind scheme.
Transient solver for trans-sonic/supersonic, laminar/tur-
2. sonicFoam
bulent flow of a compressible gas.

Multiphase flow

Solver Description

Solver for 2 incompressible, isothermal and immis-


1. interFoam
cible fluids based on the VOF method.
Solver for 2 incompressible, isothermal and immis-
2. interPhaseChangeFoam
cible fluids with a phase change model.
Solver for multiple compressible fluid phases with
3. multiPhaseEulerFoam
heat transfer.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 27


Introduction Code Simulations Programming Tutorials Conclusions Appendix

OpenFOAM solvers III

Other solvers

Solver Description

Transient solver for linear-elastic, small-strain solver de-


1. solidDisplacementFoam
formation of a solid body.
2. mdFoam Molecular dynamics solver for fluids.
Steady state solver for buoyant, turbulent flow of com-
3. buoyantSimpleFoam
pressible fluids.

Solver names describe their functionality

simple: SIMPLE algorithm used in steady-state solvers.


piso: PISO algorithm used in transient solvers αCFL < 1.
pimple: PIMPLE algorithm used in transient solvers.
DyM: Supports dynamic mesh (eg. mesh refinement, moving meshes).

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 28


Introduction Code Simulations Programming Tutorials Conclusions Appendix

icoFoam solver

Main solver file


<< find $FOAM_SOLVERS -name icoFoam.C | xargs cat -n

.....
27 D e s c r i p t i o n
28 T r a n s i e n t s o l v e r f o r i n c o m p r e s s i b l e , l a m i n a r f l o w o f Newtonian f l u i d s .
.....
32 # i n c l u d e " fvCFD . H" / / Header f i l e w i t h a l l t h e FVM machinery
33 # i n c l u d e " p i s o C o n t r o l . H" / / F i l e s d e f i n i n g PISO c o n t r o l s
.....
37 i n t main ( i n t argc , char ∗argv [ ] ) / / Beginning o f t h e main program
38 {
39 # i n c l u d e " setRootCase . H" / / Set path and case d i r e c t o r i e s
40 # i n c l u d e " createTime . H" / / I n i t i a l i s e time v a r i a b l e
41 # i n c l u d e " createMesh . H" / / I n i t i a l i s e t h e mesh t o work w i t h
.....
52 while ( runTime . l o o p ( ) ) / / Time l o o p begins
53 {
.....
115 runTime . w r i t e ( ) ; / / Write r e s u l t s to time d i r e c t o r i e s
.....
120 } / / End t i m e l o o p
121
122 I n f o << " End \ n " << e n d l ;
123
124 return 0;
125 }
.....

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 29


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code


Code structure
Applications
Solvers
Utilities

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 30


Introduction Code Simulations Programming Tutorials Conclusions Appendix

OpenFOAM utilities I

X Over 80+ OpenFOAM utilities.

Mesh generation tools

Utility Description

1. blockMesh Multiblock mesh generator.


Automatic split hexahedral mesher which refines and snaps to
2. snappyHexMesh
surface producing hexahedral dominant cells.

Mesh conversion to OpenFOAM format

Utility Description

1. ansysToFoam ANSYS input mesh file exported from I-DEAS.


2. gambitToFoam GAMBIT mesh file.
3. gmshToFoam Reads .msh GMSH file.
4. ideasUnvToFoam I-DEAS unv format (eg. SALOME mesh).

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 31


Introduction Code Simulations Programming Tutorials Conclusions Appendix

OpenFOAM utilities II

Mesh manipulation tools

Utility Description

1. checkMesh Checks the quality of the mesh by providing a detailed output.


2. renumberMesh Renumbers the cell list to reduce the bandwidth.
3. refineMesh Refines mesh in multiple directions.
4. autoRefineMesh Refines cells near to a surface.
5. refineHexMesh Refines a hexahederal mesh by 2x2x2 cell splitting.

Post-processing tools

Utility Description

1. patchAverage Calculates average of a field over a patch.


2. patchIntegrate Calculates integral of a field over a patch.
3. probeLocations Outputs the field value at a particular location.
4. foamCalc Utility for simple field calculations at specified times.
5. writeCellCentres Output cell centres as a volScalarField.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 32


Introduction Code Simulations Programming Tutorials Conclusions Appendix

OpenFOAM utilities III

Parallel processing tools

Utility Description

Decomposes a mesh and fields of a case for parallel


1. decomposePar
execution.
Reconstructs the decomposed mesh after parallel ex-
2. reconstructParMesh
ecution using only the geometric information.

Hands-on training:
Some useful OpenFOAM utilities (See page 91)

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 33


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations
Tutorial directory structure
Meshing
Problem setup
Solving
Post-processing

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 34


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations
Tutorial directory structure
Meshing
Problem setup
Solving
Post-processing

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 35


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Tutorial directory structure I

<tutorial>

constant
1. constant:
This directory contains the information which remains constant
polymesh
throughout the simulation. It contains the following:

points 1.1 polymesh:


faces Contains all the mesh information including:
(a) points → nodal positions
owner
(b) faces → face connectivities
neighbour
(c) owner → owner cell labels
boundary (d) neighbour → neighbour cell labels
(e) boundary → boundary information

... properties
1.2 ... properties:
Files which specify physical properties for a particular
system application eg. gravity, viscosity, thermal properties etc.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 36


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Tutorial directory structure II

2. system:
This directory contains all the parameters associated with the
<tutorial> solution procedure. It contains at least the following files:

constant 2.1 controlDict:


Specifies the run control parameters such as start/end
system time, time step, write interval etc.

controlDict 2.2 fvSchemes:


Contains the finite volume discretisation schemes used
fvSchemes for the solution procedure such as spatial and temporal
discretisations.
fvSolution
2.3 fvSolution:
0
Contains equation solvers, algorithm controls and
p tolerances for the implicit solvers.

U 3. 0:
The ’0’ directory corresponds to zero time. It contains the
initial and boundary conditions for variables (ie. pressure p,
velocity U) in individual files.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 37


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations
Tutorial directory structure
Meshing
Problem setup
Solving
Post-processing

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 38


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Meshing in OpenFOAM

• OpenFOAM only supports three dimensional meshes !!!


• 1D & 2D simulations are carried out by using appropriate ’empty ’ boundary conditions.
• Supports arbitrary polyhedral cells bounded by arbitrary polygonal faces.

Using blockMesh
• Simple mesh generator using blocks.
• Allows multiple blocks and curved edges.
• Not suitable to use for complex geometries.

Good practices
• Run ’checkMesh’ utility to monitor mesh quality.
• Check mesh in ParaView before starting the simulation using paraFoam command .

Hands-on training:
Meshing with blockMesh (See page 71)
Meshing with snappyHexMesh (See page 118)

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 39


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations
Tutorial directory structure
Meshing
Problem setup
Solving
Post-processing

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 40


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Selection of an appropriate OpenFOAM solver

Procedure

• Select an appropriate OpenFOAM solver (alias: sol) for the problem:


<< ls $FOAM_SOLVERS

• Copy a suitable tutorial (alias: tut) to your run directory (alias: run).
<< cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity/cavity $FOAM_RUN

• Understand the inputs of the solver by reading the dictionaries of the tutorial.

• Modify the tutorial accordingly including geometry, meshing and problem setup.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 41


Introduction Code Simulations Programming Tutorials Conclusions Appendix

OpenFOAM tutorial file syntax

A sub-dictionary is characterised by curly brackets {...}

{
..... / / Keyword e n t r i e s
}

A list is characterised by round brackets (...);

L i s t <Type> / / Type o f l i s t
<n> / / L i s t size
(
..... / / List entries
);

A dimensionSet is characterised by square brackets [...]

[ 0 1 −1 0 0 0 0 ] / / [ Mass Length Time Temp . Q u a n t i t y C u r r e n t L u m i n o s i t y ]

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 42


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Initial conditions

Initial conditions must be specified for all variables inside ’0’ directory.

Standard initial conditions


<< cat $FOAM_RUN/cavity/0/U

.....
17 dimensions [ 0 1 −1 0 0 0 0 ] ; / / Dimensions i n SI base u n i t s [ kg m s K mol A cd ]
18
19 i n t e r n a l F i e l d uniform (0 0 0) ; / / C e l l values
.....

Non standard initial conditions

• setFields utility could also be utilised to initialise fields in OpenFOAM.


• The dictionary setFieldsDict provides the necessary inputs.
• funkySetFields utility of the swak4Foam library by B. Gschaider could also be utilised.

<< find $FOAM_TUTORIALS -name setFieldsDict

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 43


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Boundary conditions I

• Boundary conditions must also be specified for all variables inside ’0’ directory.
• Geometry is broken down into patches where boundary conditions are applied.

Initial and boundary conditions


<< cat $FOAM_RUN/cavity/0/U

.....
21 b o u n d a r y F i e l d / / D i c t i o n a r y t o s p e c i f y boundary c o n d i t i o n s
22 {
23 movingWall / / Boundary patch name
24 {
25 type fixedValue ; / / Boundary c o n d i t i o n t y p e
26 value uniform (1 0 0) ; / / Boundary v a l u e s
27 }
28
29 fixedWalls
30 {
31 type noSlip ;
32 }
33
34 frontAndBack
35 {
36 type empty ; / / Boundary c o n d i t i o n t y p e f o r 2D s i m u l a t i o n s
37 }
38 }
.....

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 44


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Boundary conditions II

Basic boundary conditions

BC type Data Example

1. fixedValue value U = (5, 10, 0)


∂T
2. fixedGradient gradient ∂n = 3.5
∂p
3. zeroGradient _ ∂n =0

Non standard boundary conditions

• groovyBC library developed by B. Gschaider could be utilised without the need for programming.
• Custom boundary conditions can be programmed.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 45


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Time controls
controlDict dictionary
<< cat $FOAM_RUN/cavity/system/controlDict

18 application icoFoam ; / / Name o f s o l v e r


19
20 startFrom startTime ; / / Useful f o r r e s t a r t simulations
21
22 startTime 0; / / S t a r t i n g time
23
24 stopAt endTime ; / / Stopping c r i t e r i a
25
26 endTime 0.5; / / End t i m e
27
28 deltaT 0.005; / / Time s t e p p i n g ( D e f a u l t v a l u e o f 1 f o r steady s t a t e s o l v e r s )
29
30 writeControl timeStep ; / / Criteria for writing results
31
32 writeInterval 20; / / Interval for writing results
33
34 purgeWrite 0; / / D i s a b l e r e w r i t i n g over t i m e d i r e c t o r i e s
35
36 writeFormat ascii ; / / Format f o r w r i t i n g r e s u l t s
37
38 writePrecision 6; / / Precision for writing results
39
40 writeCompression o f f ; / / D i s a b l e compression f o r w r t i n g r e s u l t s
41
42 timeFormat general ; / / Format f o r w r i t i n g t i m e d i r e c t o r i e s
43
44 timePrecision 6; / / Precision f o r w r i t i n g time d i r e c t o r i e s
45
46 runTimeModifiable true ; / / A l l o w s m o d i f i c a t i o n o f s e t t i n g s d u r i n g t h e run

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 46


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Solver controls I

fvSchemes dictionary
<< cat $FOAM_RUN/cavity/system/fvSchemes

18 ddtSchemes
19 {
20 default Euler ; / / Time d i s c r e t i s a t i o n schemes
21 }
23 gradSchemes / / G r a d i e n t e v a l u a t i o n schemes
24 {
25 default Gauss l i n e a r ;
26 grad ( p ) Gauss l i n e a r ;
27 }
29 divSchemes / / D i s c r e t i s a t i o n o f t h e c o n v e c t i v e terms
30 {
31 default none ;
32 d i v ( phi , U) Gauss l i n e a r ;
33 }
35 laplacianSchemes / / D i s c r e t i s a t i o n o f t h e L a p l a c i a n term
36 {
37 default Gauss l i n e a r orthogonal ;
38 }
40 interpolationSchemes / / Method o f i n t e r p o l a t i o n
41 {
42 default linear ;
43 }
45 snGradSchemes / / Surface normal g r a d i e n t scheme
46 {
47 default orthogonal ;
48 }

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 47


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Solver controls II
fvSolution dictionary
<< cat $FOAM_RUN/cavity/system/fvSolution

18 s o l v e r s
19 {
20 p
21 {
22 solver PCG; / / S o l v e r t o s o l v e t h e system
23 p r e c o n d i t i o n e r DIC ; / / Preconditioner type
24 tolerance 1e−06;
25 relTol 0.05;
26 }
28 pFinal
29 {
30 $p ;
31 relTol 0;
32 }
34 U
35 {
36 solver smoothSolver ;
37 smoother symGaussSeidel ;
38 tolerance 1e−05;
39 relTol 0;
40 }
41 }
42
43 PISO / / PISO c o n t r o l s
44 {
45 nCorrectors 2;
46 nNonOrthogonalCorrectors 0 ;
....
49 }

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 48


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations
Tutorial directory structure
Meshing
Problem setup
Solving
Post-processing

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 49


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Running an OpenFOAM tutorial

Default procedure
• Copy OpenFOAM tutorial to you run directory.
<< cp -r <tutorial_directory> $FOAM_RUN
<< cd <tutorial_name>

• Create mesh by using the blockMesh command.


<< blockMesh

• Run simulation by using name of an OpenFOAM solver (eg. pisoFoam).


<< pisoFoam

Procedure for complex tutorials


Some tutorials require an Allrun script to run and Allclean script to clear the tutorial directory.
<< find $FOAM_TUTORIALS -name Allrun

Hands-on training:
Two phase flow problem (See page 82)

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 50


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations
Tutorial directory structure
Meshing
Problem setup
Solving
Post-processing

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 51


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Post-processing OpenFOAM results

• OpenFOAM comes with builtin support for ParaView.

• ParaView is an open-source, multi-platform data analysis and visualisation application.

• Also possible to visualise OpenFOAM results with third-party applications.

• OpenFOAM results can be visualised using the paraFoam utility.


<< paraFoam

ParaView toolbars
Main controls VCR controls Time controls

Camera controls Axes control


Common filters
Active variable controls

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 52


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM
OpenFOAM classes
Dimensional compatibility
Mathematical operators
Mesh information
Code compilation

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 53


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM
OpenFOAM classes
Dimensional compatibility
Mathematical operators
Mesh information
Code compilation

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 54


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Basic classes

X Basic classes in OpenFOAM have been derived from more fundamental C++ classes.

X OpenFOAM classes are more advanced and have more functionality.

Some OpenFOAM classes

C++ class OpenFOAM class

1. int/long label

2. bool switch - Accepts true/false, on/off and yes/no.

3. string word

4. float/double scalar

5. — vector

6. — tensor - 3 × 3 tensor with algebra

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 55


Introduction Code Simulations Programming Tutorials Conclusions Appendix

GeometricField class I

GeometricField class is templated around 3 arguments:


GeometricField < Type, PatchField, GeoMesh >

A ’Field’ refers to a list of tensors along with a mesh.

• typedef is an alias for a class to improve readability.


• Used extensively in OpenFOAM particularly in relation to template classes.

The geometricField< Type > is renamed used typedef declarations to indicate where field variables are
stored:
1. volField<Type> −→ Field defined at cell centres.
2. surfaceField<Type> −→ Field defined at cell faces.
3. pointField<Type> −→ Field defined at cell vertices.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 56


Introduction Code Simulations Programming Tutorials Conclusions Appendix

GeometricField class II

Internal field: Boundary field:


Defined at cell centers
Defined at boundary face centers
Defined at internal faces centers
Defined at nodes

(a) volField (b) surfaceField (c) pointField

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 57


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Equation mimicking

Classical incompressible Navier-Stokes equation

∂ρU
+ ∇ · (ρ UU) − µ∇2 U = −∇p + b
∂t

1. typedef - fvm:
Navier-Stokes equation in OpenFOAM
- Belongs the finiteVolumeMethod class.
solve
( - Performs an implicit evaluation and returns an
fvm : : d d t ( rho ,U) fvMatrix.
+ fvm : : d i v (U,U)
− fvm : : l a p l a c i a n (mu,U)
== 2. typedef - fvc:
− f v c : : grad ( p )
+ b - Belongs to the finiteVolumeCalculus class.
); - Performs an explicit calculation and returns a
geometricField.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 58


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM
OpenFOAM classes
Dimensional compatibility
Mathematical operators
Mesh information
Code compilation

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 59


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Dimensional compatibility

X OpenFOAM is fully dimensional.

X Fields and properties have dimensions associated with them.

X Dimensional checking is performed to avoid meaningless operations.

X Units are defined using the dimensionSet class.

Dictionary in OpenFOAM

FoamFile Sample OpenFOAM code


{
version 2.0; dimensionedTensor sigma
format ascii ; (
class dictionary ; " sigma " ,
object transportProperties ; dimensionSet (1 , −1 , −2 ,0 ,0 ,0 ,0) ,
} t e n s o r ( 1 e6 , 0 , 0 , 0 , 1 0 e6 , 0 , 0 , 0 , 5 e6 )
);
/ / Thermal d i f f u s i v i t y
DT DT [ 0 2 −1 0 0 0 0 ] 0.01;

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 60


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Standard dimensional units

SI Base Units
Quantity
kg m s K mol A cd

1. Density 1 -3 0 0 0 0 0

2. Linear Momentum 1 -2 -1 0 0 0 0

3. Pressure 1 -1 -2 0 0 0 0

4. Force 1 1 -2 0 0 0 0

5. Lame’s Coefficients 1 -1 -2 0 0 0 0

6. Bulk Modulus 1 -1 -2 0 0 0 0

7. Young’s Modulus of Elasticity 1 -1 -2 0 0 0 0

8. Specific Heat Capacity 0 2 -2 -1 0 0 0

9. Thermal Conductivity 1 1 -3 -1 0 0 0

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 61


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM
OpenFOAM classes
Dimensional compatibility
Mathematical operators
Mesh information
Code compilation

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 62


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Tensor operations

• Tensor operations available to all OpenFOAM tensor classes

Product operations

Product operator OpenFOAM notation Rank of a & b

1. Outer a∗b ≥ 1
2. Inner/dot a&b ≥ 1
3. Double inner/dot a && b ≥ 2
4. Cross aˆb = 1

Other operations

Product operator OpenFOAM notation

1. Magnitude mag (a)


2. Square of magnitude magSqr (a)
3. Square sqr (a)
4. Power pow (a, n)

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 63


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Tensor operations

Operations exclusive to tensors of rank 2 Operations exclusive to tensors of rank 0

Operator OpenFOAM notation


Operator OpenFOAM notation

1. Transpose A · T()
1. Square root sqrt (s)
2. Diagonal diag (A)
2. Exponential exp (s)
3. Trace tr (A)
3. Natural log log (s)
4. Symmetric part symm (A)
4. Log base 10 log10 (s)
5. Skew-symmetric part skew (A)
5. Sine sin (s)
6. Determinant det (A)
6. Cosine cos (s)
7. Cofactor cof (A)
7. Tangent tan (s)
8. Inverse inv (A)

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 64


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM
OpenFOAM classes
Dimensional compatibility
Mathematical operators
Mesh information
Code compilation

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 65


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Mesh parameters

Important mesh parameters

Description Access function Data type

1. Total number of cells mesh.nCells() scalar


2. Total number of internal faces mesh.nInternalFaces() scalar
3. Cell volumes mesh.V() volScalarField
4. Face area normal vectors mesh.Sf() surfaceVectorField
5. Face area magnitudes mesh.magSf() surfaceVectorField

Coordinates

Coordinates Access function Data type

1. Cell center mesh.C() volVectorField


2. Face center mesh.Cf() surfaceVectorField
3. Nodal mesh.points() pointField

 This blog post [link] contains useful mesh related information particularly mesh connectivities.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 66


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM
OpenFOAM classes
Dimensional compatibility
Mathematical operators
Mesh information
Code compilation

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 67


Introduction Code Simulations Programming Tutorials Conclusions Appendix

wmake compilation tool

X OpenFOAM uses its own compilation tool wmake.

X OpenFOAM applications and libraries require a Make directory.

X The Make directory contains the files and options files.

Make directories
<< find $FOAM_SOLVERS -name Make | sort
<< cat -n
$FOAM_TUTORIALS/stressAnalysis/solidEquilibriumDisplacementFoam/Make/files
<< cat -n
$FOAM_TUTORIALS/stressAnalysis/solidEquilibriumDisplacementFoam/Make/options

Compilation
This is performed by executing the wmake command from the directory containing the Make folder.
<< wmake

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 68


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session


Meshing with blockMesh
Two-phase flow problem
OpenFOAM utilities
Parallel computing
Steady state flow problem
Meshing with snappyHexMesh

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 69


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session


Meshing with blockMesh
Two-phase flow problem
OpenFOAM utilities
Parallel computing
Steady state flow problem
Meshing with snappyHexMesh

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 70


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Mesh creation using blockMesh utility

Find blockMesh files


<< find $FOAM_TUTORIALS -name *blockMesh*

Arrange files according to size


<< find $FOAM_TUTORIALS -name *blockMesh* | sort

Copy tutorial
<< cp -r
$FOAM_TUTORIALS/stressAnalysis/solidEquilibriumDisplacementFoam/beamEndLoad/
$FOAM_RUN
<< cd $FOAM_RUN/beamEndLoad
<< tree

Generate mesh
<< blockMesh | tee log.blockMesh

63 boundingBox : ( 0 0 0 ) (30 1 1 )
64 n P o i n t s : 42
65 n C e l l s : 12
66 nFaces : 56
67 n I n t e r n a l F a c e s : 16

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 71


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Mesh quality using checkMesh utility

Check the mesh quality


<< checkMesh > log.checkMesh
<< cat -n log.checkMesh

65 Checking geometry . . .
66 O v e r a l l domain bounding box ( 0 0 0 ) (30 1 1 )
67 Mesh has 2 g e o m e t r i c ( non−empty / wedge ) d i r e c t i o n s ( 1 1 0 )
68 Mesh has 2 s o l u t i o n ( non−empty ) d i r e c t i o n s ( 1 1 0 )
69 A l l edges a l i g n e d w i t h or p e r p e n d i c u l a r t o non−empty d i r e c t i o n s .
70 Boundary openness ( 0 0 0 ) OK.
71 Max c e l l openness = 0 OK.
72 Max aspect r a t i o = 10 OK.
73 Minimum f a c e area = 0 . 5 . Maximum f a c e area = 5 . Face area magnitudes OK.
74 Min volume = 2 . 5 . Max volume = 2 . 5 . T o t a l volume = 3 0 . C e l l volumes OK.
75 Mesh non−o r t h o g o n a l i t y Max : 0 average : 0
76 Non−o r t h o g o n a l i t y check OK.
77 Face pyramids OK.
78 Max skewness = 0 OK.
79 Coupled p o i n t l o c a t i o n match ( average 0 ) OK.
80
81 Mesh OK.

Visualise the mesh


<< paraFoam
Display the mesh using surface with edges option.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 72


Introduction Code Simulations Programming Tutorials Conclusions Appendix

BlockMesh inputs I

Inputs for creating a mesh using blockMesh requires a blockMeshDict file


located in the system directory.

Study blockMeshDict file


<< cat -n system/blockMeshDict

Modify blockMeshDict file to create a cube


<< gedit system/blockMeshDict

17 convertToMeters 0 . 0 0 1 ; / / Scaling f o r conversion to m i l l i m e t e r s

19 v e r t i c e s 2 3
20 (
21 (0 0 0) // Vertex 0
22 (1 0 0) // Vertex 1 7 6
23 (0 1 0) // Vertex 2
24 (1 1 0) // Vertex 3
25 (0 0 1) // Vertex 4 y
26 (1 0 1) // Vertex 5 0
27 (1 1 1) // Vertex 6 x 1
z
28 (0 1 1) // Vertex 7
29 ) ;
4 5

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 73


Introduction Code Simulations Programming Tutorials Conclusions Appendix

BlockMesh inputs II
Defining cells

31 b l o c k s
32 (
33 hex ( 0 1 3 2 4 5 6 7 ) / / V e r t e x l i s t f o r c r e a t i o n o f a hexahedral c e l l
34 (5 5 5) / / No . o f c e l l s i n each d i r e c t i o n
35 simpleGrading ( 1 . 0 1 . 0 1 . 0 ) / / C e l l expansion r a t i o s
36 ) ;

Ordering of vertices is critical when defining blocks!


2 3
Plane 1 Plane 2
7 6
33 hex ( 0 1 3 2 4 5 6 7)
y
x
0
y x 1
z
z
4 5

Delete unused entries

38 edges
39 (
40 ) ;

81 mergePatchPairs
82 (
83 ) ;

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 74


Introduction Code Simulations Programming Tutorials Conclusions Appendix

BlockMesh inputs III


Modify boundary definition

38 boundary / / Contains d e f i n i t i o n o f boundary patches


39 (
40 s i d e s / / Boundary patch name
41 {
42 t y p e patch ;
43 faces
44 (
45 (4 0 2 7) / / Boundary f a c e d e f i n i t i o n s
46 (1 5 6 3)
47 (0 1 3 2)
48 (4 5 6 7)
49 );
50 }
51 bottom
52 {
53 t y p e patch ;
54 faces
55 (
56 (0 1 5 4)
57 );
58 }
59 t o p
60 {
61 t y p e patch ;
62 faces
63 (
64 (2 3 6 7)
65 );
66 }
67 ) ;

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 75


Introduction Code Simulations Programming Tutorials Conclusions Appendix

blockMesh and checkMesh utilities


Generate mesh
<< blockMesh > log.blockMesh
<< cat -n log.blockMesh

61 boundingBox : ( 0 0 0 ) ( 0 . 0 0 1 0.001 0 . 0 0 1 )
62 n P o i n t s : 216
63 n C e l l s : 125
64 nFaces : 450
65 n I n t e r n a l F a c e s : 300

Check the mesh quality


<< checkMesh > log.checkMesh
<< cat -n log.checkMesh

62 Checking geometry . . .
66 Boundary openness ( 9 . 4 8 5 e−17 9.485e−17 5.9557e−17) OK.
67 Max c e l l openness = 8.27181e−17 OK.
68 Max aspect r a t i o = 1 OK.
69 Minimum f a c e area = 4e−08. Maximum f a c e area = 4e−08. Face area magnitudes OK.
70 Min volume = 8e−12. Max volume = 8e−12. T o t a l volume = 1e−09. C e l l volumes OK.
71 Mesh non−o r t h o g o n a l i t y Max : 0 average : 0
72 Non−o r t h o g o n a l i t y check OK.
73 Face pyramids OK.
74 Max skewness = 3.68629e−15 OK.
75 Coupled p o i n t l o c a t i o n match ( average 0 ) OK.
76
77 Mesh OK.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 76


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Post-processing with paraFoam utility

Visualise (or try to visualise) the mesh


<< paraFoam

−−> FOAM FATAL IO ERROR:


Cannot f i n d p a t c h F i e l d e n t r y f o r s i d e s

f i l e : / home / j i b r a n / OpenFOAM / j i b r a n −6/run / beamEndLoad / 0 / p . b o u n d a r y F i e l d from l i n e 25 t o l i n e 4 9 .

From f u n c t i o n void Foam : : G e o m e t r i c F i e l d <Type , P a t c h F i e l d , GeoMesh > : : Boundary : : r e a d F i e l d ( const


Foam : : DimensionedField <TypeR , GeoMesh>& , const Foam : : d i c t i o n a r y &) [ w i t h Type = double ; P a t c h F i e l d
= Foam : : f v P a t c h F i e l d ; GeoMesh = Foam : : volMesh ]
i n f i l e / home / ubuntu / OpenFOAM / OpenFOAM−6/s r c / OpenFOAM / l n I n c l u d e / GeometricBoundaryField . C a t l i n e 191.

FOAM e x i t i n g

Delete 0 directory
<< rm -r 0

Visualise the mesh


<< paraFoam
Display the mesh using surface with edges option.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 77


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Exercise I: Meshing with blockMesh I

Increase the mesh density to 10×10×10 cells.

Hint
• You can edit the blockMeshDict file using your favourite text editor (eg. vi, gedit, nano).
<< vi system/blockMeshDict

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 78


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Exercise I: Meshing with blockMesh II

Connect points 6 and 3 with an arc instead of a line.

2 3
Add the following after the blocks definition
7 6
38 edges
39 (
40 arc 6 3 ( 1 . 3 1.3 0 . 5 )
y
41 ) ; 0
x 1
z

4 5

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 79


Introduction Code Simulations Programming Tutorials Conclusions Appendix

What have we learnt in this tutorial?

X Mesh generation using blockMesh utility.


X Understand the structure of blockMeshDict file.
X Modify blockMeshDict file to our requirements.
X Determine the quality of mesh with checkMesh utility.
X Visualise the mesh with paraFoam utility.
X Understand OpenFOAM error messages displayed on the terminal.
X Use of tee command to simultaneously display and write output to a file.
X Use of the standard output stream < to redirect output to a file.
X Use of cat command to print and vi to modify contents of a file.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 80


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session


Meshing with blockMesh
Two-phase flow problem
OpenFOAM utilities
Parallel computing
Steady state flow problem
Meshing with snappyHexMesh

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 81


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Breaking of a dam

Simulate breaking of a dam using the two-phase flow solver in OpenFOAM.


The interFoam solver is based on the Volume Of Fluid interface capturing approach.

0.584 m

Problem description
Air
• Transient two phase flow
0.584 m
• Incompressible, isothermal flow
Water
• Immiscible fluids 0.292 m Column
0.024 m

0.048 m

0.1461 m 0.1459 m

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 82


Introduction Code Simulations Programming Tutorials Conclusions Appendix

interFoam solver

Description of solver
<< find $FOAM_SOLVERS -name interFoam.C | xargs cat -n | sed -n
"/Description/,/*/p"

27 Description
28 S o l v e r f o r 2 i n c o m p r e s s i b l e , i s o t h e r m a l i m m i s c i b l e f l u i d s using a VOF
29 ( volume o f f l u i d ) phase−f r a c t i o n based i n t e r f a c e c a p t u r i n g approach ,
30 w i t h o p t i o n a l mesh motion and mesh t o p o l o g y changes i n c l u d i n g a d a p t i v e
31 re−meshing .

Find solver location


<< locate interFoam.C

/ o p t / openfoam6 / a p p l i c a t i o n s / s o l v e r s / m u l t i p h a s e / interFoam / interFoam . C

Copy damBreak tutorial


<< cp -r $FOAM_TUTORIALS/multiphase/interFoam/laminar/damBreak/damBreak/
$FOAM_RUN

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 83


Introduction Code Simulations Programming Tutorials Conclusions Appendix

interFoam solver

Change to damBreak directory


<< cd $FOAM_RUN/damBreak

Study contents of tutorial


<< tree

Generate mesh and view in ParaView


<< blockMesh
<< checkMesh
<< paraFoam

Solve and view results


<< interFoam
<< paraFoam
Display the mesh using surface with edges option.
Select phase fraction (alpha.water) field to display.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 84


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Check problem setup

Initial phase fraction field


<< cat 0/alpha.water.orig

19 internalField uniform 0;

Study contents of tutorial


<< tree system

system
|− blockMeshDict
|− c o n t r o l D i c t
|− decomposeParDict
|− fvSchemes
|− f v S o l u t i o n
|− s e t F i e l d s D i c t

Description of setFields utility


<< find $FOAM_UTILITIES -name setFields.C | xargs cat -n | sed -n
"/Description/,/*/p"

24 Description
25 Set v a l u e s on a s e l e c t e d s e t o f c e l l s / p a t c h f a c e s t h r o u g h a d i c t i o n a r y .

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 85


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Initialise fields

Display contents of setFieldsDict


<< cat -n system/setFieldsDict

18 defaultFieldValues
19 (
20 v o l S c a l a r F i e l d V a l u e alpha . water 0
21 );
22
23 regions
24 (
25 boxToCell
26 {
27 box ( 0 0 −1) ( 0 . 1 4 6 1 0.292 1 ) ;
28 fieldValues
29 (
30 v o l S c a l a r F i e l d V a l u e alpha . water 1
31 );
32 }
33 );

Run setFieldsDict utility


<< setFields
<< tree 0
<< head -50 0/alpha.water

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 86


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Solution
t = 0.0 s

Run the solver again


<< interFoam
<< paraFoam

t = 0.15 s t = 0.30 s t = 1.0 s

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 87


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Exercise II: Breaking of a dam

Simulate the same scenario but with a refined mesh and save an animation.

Hint
• A utility "refineMesh" could be utilised.
• Time directories can be removed with << foamListTimes -rm.
• Please pay attention to the error messages displayed on your terminal.
The solution is always there!!!

t = 0.15 s t = 0.30 s t = 1.0 s

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 88


Introduction Code Simulations Programming Tutorials Conclusions Appendix

What have we learnt in this tutorial?

X Running an OpenFOAM solver.


X Use find command to search description of OpenFOAM solvers and utilities.
X Use setFields utility to initialise fields.
X Output OpenFOAM solver data to a log file.
X Use refineMesh utility to increase mesh density.
X Understand OpenFOAM errors displayed on the terminal.
X Clear tutorial time directories with foamListTimes -rm.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 89


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session


Meshing with blockMesh
Two-phase flow problem
OpenFOAM utilities
Parallel computing
Steady state flow problem
Meshing with snappyHexMesh

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 90


Introduction Code Simulations Programming Tutorials Conclusions Appendix

In this tutorial we will learn about the usage of some useful


OpenFOAM utilities.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 91


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Alternative mesh formats I

How to use a mesh generated from ANSYS FLUENT for OpenFOAM simulations?

Search for a fluent mesh tutorial


<< grep -r fluent $FOAM_TUTORIALS

/ o p t / openfoam6 / t u t o r i a l s / i n c o m p r e s s i b l e / icoFoam / elbow / A l l r u n : r u n A p p l i c a t i o n fluentMeshToFoam elbow . msh


/ o p t / openfoam6 / t u t o r i a l s / i n c o m p r e s s i b l e / icoFoam / elbow / A l l c l e a n : rm −r f f l u e n t I n t e r f a c e

Copy ’elbow’ tutorial


<< cp -r $FOAM_TUTORIALS/incompressible/icoFoam/elbow/ $FOAM_RUN
<< cd $FOAM_RUN/elbow
<< tree

Convert FLUENT mesh to OpenFOAM format


<< fluentMeshToFoam elbow.msh
<< checkMesh
<< paraFoam

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 92


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Alternative mesh formats II

Other supported mesh formats


<< ls $FOAM_UTILITIES/mesh/conversion

ansysToFoam foamMeshToFluent ideasUnvToFoam plot3dToFoam vtkUnstructuredToFoam


cfx4ToFoam foamToStarMesh kivaToFoam sammToFoam writeMeshObj
datToFoam foamToSurface mshToFoam star3ToFoam
fluent3DMeshToFoam gambitToFoam netgenNeutralToFoam star4ToFoam
fluentMeshToFoam gmshToFoam Optional tetgenToFoam

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 93


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Cell labels

Convert to ParaView’s VTK format


<< foamToVTK
<< tree

Read mesh in paraview


<< paraview
File −→ Open −→ ’VTK/elbow_0.vtk’.
Display the mesh using surface with edges.
Color by cell ID.

X High bandwidth slows the performance of matrix solvers.


X Renumbering of cell labels can reduce the bandwidth in a solution matrix.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 94


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Renumber cell labels

Use ’renumberMesh’ utility


<< renumberMesh

Mesh s i z e : 918
Before renumbering :
band : 735
profile : 103613

A f t e r renumbering :
band : 29
profile : 11753

W r i t i n g mesh t o " 0.05 "

<< tree

VTK
|− elbow_0 . v t k
|− elbow_1 . v t k

Renumber cells
<< renumberMesh -help
−o v e r w r i t e o v e r w r i t e e x i s t i n g mesh / r e s u l t s f i l e s

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 95


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Visualise results

Display new cell labels


<< foamToVTK
<< paraview&
File −→ Open −→ "VTK/elbow_1.vtk"
Display the mesh using surface with edges and color by cell ID.

(a) Before (b) After

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 96


Introduction Code Simulations Programming Tutorials Conclusions Appendix

What have we learnt in this tutorial?

X Convert mesh from FLUENT to OpenFOAM compatible format using the


fluentMeshToFoam utility.

X Convert mesh from OpenFOAM to VTK format using the foamToVTK utility.
X Use renumberMesh utility to decrease bandwidth.
X Search for a particular text using ‘grep’ command.
X See usage of an OpenFOAM command using ‘-help’ option.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 97


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session


Meshing with blockMesh
Two-phase flow problem
OpenFOAM utilities
Parallel computing
Steady state flow problem
Meshing with snappyHexMesh

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 98


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Compare the performance of a parallel run against a serial run


in OpenFOAM.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 99


Introduction Code Simulations Programming Tutorials Conclusions Appendix

Problem setup

Copy damBreak tutorial


<< run
<< cp -r $FOAM_TUTORIALS/multiphase/interFoam/laminar/damBreak/damBreak/ .

Refine the mesh for better comparison of run times for serial and parallel simulations

Mesh refinement
<< mv damBreak damBreakFine_serial
<< cd damBreakFine_serial
<< gedit system/blockMeshDict

47 b l o c k s
48 (
49 hex (0 1 5 4 12 13 17 16) ( 70 15 1 ) simpleGrading ( 1 1 1 )
50 hex (2 3 7 6 14 15 19 18) ( 60 15 1 ) simpleGrading ( 1 1 1 )
51 hex (4 5 9 8 16 17 21 20) ( 70 120 1 ) simpleGrading ( 1 2 1 )
52 hex (5 6 10 9 17 18 22 21) ( 8 120 1 ) simpleGrading ( 1 2 1 )
53 hex (6 7 11 10 18 19 23 22) ( 60 120 1 ) simpleGrading ( 1 2 1 )
54 ) ;

<< blockMesh

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 100
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Serial run

Check mesh
<< checkMesh

O v e r a l l number o f c e l l s o f each t y p e :
hexahedra : 18510

<< paraFoam

Duplicate case setup for parallel run


<< cp -r $FOAM_RUN/damBreakFine_serial $FOAM_RUN/damBreakFine_parallel

Run the problem


<< setFields
<< interFoam | tee log.serial

ExecutionTime = 330.7 s ClockTime = 331 s

Visualise results
<< paraFoam

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 101
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Domain decomposition I

• OpenFOAM uses domain decomposition for parallel runs.

• Geometry is broken down and allocated to separate processors.

• Decomposition requires a system/decomposeParDict file.

• Decomposition process is initiated by using the decomposePar utility.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 102
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Domain decomposition II

Check contents of decomposeParDict


<< cd $FOAM_RUN/damBreakFine_parallel
<< cat -n system/decomposeParDict

18 numberOfSubdomains 4 ; / / No . o f subdomains f o r decomposition


19
20 method simple ; / / Method o f decomposition
21
22 simpleCoeffs / / C o e f f i c i e n t s f o r s i m p l e method
23 {
24 n (2 2 1) ; / / Domain s p l i t i n x , y , z d i r e c t i o n s
25 delta 0.001; / / C e l l skew f a c t o r ( D e f a u l t v a l u e = 0 . 0 0 1 )
26 }
27
28 hierarchicalCoeffs / / C o e f f i c i e n t s f o r h i e r a r c h i c a l method
29 {
30 n (1 1 1) ;
31 delta 0.001;
32 order xyz ; / / Order i n which t h e decomposition i s done
33 }
34
35 manualCoeffs
36 {
37 dataFile "";
38 }
39
40 distributed no ; // I f data i s d i s t r i b u t e d over s e v e r a l d i s k s
41
42 roots ( );

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 103
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Domain decomposition III


Find available processors
<< nproc -all
16

Modify decomposeParDict if necessary


<< gedit system/decomposeParDict
Modify the ’numberOfSubdomains’ and the domain split ’n’ accordingly:

18 numberOfSubdomains 9; / / No . o f subdomains f o r decomposition


19
20 method simple ; / / Method o f decomposition
21
22 simpleCoeffs / / C o e f f i c i e n t s f o r s i m p l e method
23 {
24 n (3 3 1) ; / / Domain s p l i t i n x , y , z d i r e c t i o n s
25 delta 0.001; / / C e l l skew f a c t o r ( D e f a u l t v a l u e = 0 . 0 0 1 )
26 }

Some decomposition methodologies


1. simple −→ Used for simple geometries by using geometric decomposition.
2. hierarchical −→ Similar to simple but with order of direction split.
3. scotch −→ Used for complex geometries by minimising the number of processor boundaries.
4. manual −→ User specified decomposition for each cell.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 104
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Domain decomposition IV
Initialise the problem
<< blockMesh
<< setFields
<< checkMesh

Decompose domain for parallel execution


<< decomposePar
<< tree
<< paraFoam -case processor0

(a) Processor 0 (b) Processor 1

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 105
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Parallel run

• OpenFOAM uses the public domain openMPI implementation of the standard


Message Passing Interface (MPI).

• Syntax for parallel execution on a single node is as follows:


<< mpirun -np <no_of_processors> <solver_name> -parallel

Parallel run
<< foamJob -parallel -screen interFoam
<< gnome-system-monitor

Visualise results
<< ls
<< tree processor0
<< paraFoam -case processor0

Simulation time for parallel run


<< tail log

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 106
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Reconstruct solution

reconstructPar utility can be used to reassemble the fields and mesh from the
decomposed parallel run.

Reconstruction of results
<< reconstructPar

Visualise results
<< ls
<< paraFoam

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 107
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Exercise III: Parallel performance

Analyse parallel performance of the interFoam solver.

Clean script
<< find $FOAM_TUTORIALS -name Allclean | sort
<< cp
$FOAM_TUTORIALS/stressAnalysis/solidDisplacementFoam/plateHole/Allclean .
<< ./Allclean

Parallel speedup

Tserial
Speedup ratio =
Tparallel

No. of processors Simulation time (s) Speedup

1. 1 ≈ 330 —–
2. 4 ≈ 108 3.055
3. 9 ≈ 80 4.125

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 108
Introduction Code Simulations Programming Tutorials Conclusions Appendix

What have we learnt in this tutorial?

X Setup decomposeParDict with appropriate number of processors.


X Use decomposePar utility to decompose to domain.
X Launch a parallel run on a single node using mpirun with ’-parallel’ option.
X Use reconstructPar utility to reassemble decomposed fields and mesh.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 109
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session


Meshing with blockMesh
Two-phase flow problem
OpenFOAM utilities
Parallel computing
Steady state flow problem
Meshing with snappyHexMesh

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 110
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Exercise IV: Selection of an OpenFOAM solver

Simulate the problem using an appropriate OpenFOAM solver.

Problem description
• Two dimensional, steady state flow.
• Incompressible and turbulent flow.
• Newtonion fluid with viscosity ν = 1 × 10−5 m2 /s.

Case setup

Inlet Wall Oulet


U = 10 m/s U = 0 m/s p = 0 Pa
∂U
∂n =0
0.508 m 0.332 m

0.206 m 2.06 m 0.84 m

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 111
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Exercise IV: Selection of an OpenFOAM solver

Hints
• Choose a relevant OpenFOAM solver.

• Select an appropriate tutorial as a starting point.

• Don’t start writing input files from scratch.

• Please pay attention to the error messages displayed on your terminal.

• Remember to define the problem variables correctly.

Geometry and meshing

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 112
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Exercise IV: Selection of an OpenFOAM solver

Pressure distribution

Velocity vectors

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 113
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Exercise IV: Selection of an OpenFOAM solver

Increase the initial velocity to [10, 10, 0] m/s

Velocity vectors

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 114
Introduction Code Simulations Programming Tutorials Conclusions Appendix

What have we learnt in this tutorial?

X Select an OpenFOAM solver based on the problem definition.


X Understand additional OpenFOAM errors displayed on the terminal.
X Modify parameters in the input files according to our needs.
X Create input files by copying an already existing similar file.
X Use the glyph filter in ParaView to plot vectors.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 115
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session


Meshing with blockMesh
Two-phase flow problem
OpenFOAM utilities
Parallel computing
Steady state flow problem
Meshing with snappyHexMesh

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 116
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Basics of snappyHexMesh I

Steps for creating a snappyHexMesh

1. Creation of a castellated mesh

(a) Background mesh (b) Refinement initiated

(c) Refinement completed (d) Cell removal

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 117
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Basics of snappyHexMesh II

2. Snap to surface

(e) Region refinement (f) Snapping

3. Addition of layers

(g) Layer addition

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 118
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Surface meshes I

Copy the motorbike tutorial


<< run
<< cp -r $FOAM_TUTORIALS/incompressible/simpleFoam/motorBike .
<< cd motorike
<< tree constant

Obtain motorbike geometry


<< cp $FOAM_TUTORIALS/resources/geometry/motorBike.obj.gz
constant/triSurface
<< gunzip motorBike.obj.gz

Visualise geometry
<< paraview
File −→ Open −→ "constant/triSurface/motorBike.obj"
Display surface with edges.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 119
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Surface meshes II

OpenFOAM utilities for surfaces


<< surface[TAB][TAB]

surfaceAdd surfaceFeatureExtract surfaceMeshExport surfaceSplitByPatch


su r f a c e A u t o P a t c h surfaceFeatures surfaceMeshImport surfaceSplitByTopology
su r f a c e B o o l e a n F e a t u r e s surfaceFind surfaceMeshInfo surfaceSplitNonManifolds
surfaceCheck surfaceHookUp surfaceMeshTriangulate surfaceSubset
surfaceClean surfaceInertia surfaceOrient surfaceToPatch
surfaceCoarsen surfaceLambdaMuSmooth surfacePointMerge surfaceTransformPoints
surfaceConvert surfaceMeshConvert surfaceRedistributePar
surfaceFeatureConvert surfaceMeshConvertTesting surfaceRefineRedGreen

Surface mesh information


<< surfaceMeshInfo -help
<< surfaceMeshInfo constant/triSurface/motorBike.obj
nPoints : 132871
nFaces : 331653
area : 12.1486

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 120
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Surface meshes III

Extract surface features from the geometry


<< surfaceFeatureExtract
<< tree

View patches
<< surfaceConvert constant/triSurface/motorBike.obj
constant/triSurface/motorBike.vtk
<< paraview
File −→ Open −→ "constant/triSurface/motorBike.vtk"
Display region.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 121
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Castellated mesh I

Meshing using snappyHexMesh utility requires a base background mesh to start with.

Notes
• Must only consist of hexahedral cells.
• The aspect ratio of cells should be close to 1 for optimal snapping process.

Create background mesh


<< blockMesh

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 122
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Castellated mesh II

Run snappyHexMesh in parallel


<< decomposePar
<< foamJob -parallel -screen snappyHexMesh
<< less system/snappyHexMeshDict

Local mesh refinement

(a) Level 0 (b) Level 1 (c) Level 2 (d) Level 4

Reconstruct decomposed mesh


<< reconstructParMesh -time 1
<< checkMesh
<< tree

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 123
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Castellated mesh III

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 124
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Snapping I

Cell faces are projected down onto the surface geometry.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 125
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Snapping II

Check the quality of snapping process.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 126
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Exercise V: Run motorBike tutorial I

• Clean tutorial: << ./Allclean


• Run tutorial: << ./Allrun

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 127
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Exercise V: Run motorBike tutorial II

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 128
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 129
Introduction Code Simulations Programming Tutorials Conclusions Appendix

What have we learnt in this course?

Commands

X Effective usage of bash shell commands.


X Navigate through important directories by using environment variables and aliases.

Basics of OpenFOAM

X Understand the organisation of OpenFOAM installation directory structure.


X Understand the tutorial directory structure of OpenFOAM.
X A brief introduction to programming in OpenFOAM.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 130
Introduction Code Simulations Programming Tutorials Conclusions Appendix

What have we learnt in this course?

Meshing

X Use blockMesh utility to generate simple meshes.


X Use snappyHexMesh utility for generating complex meshes.
X Use mesh conversion utilities in OpenFOAM to use meshes from external software.

Solving

X Use OpenFOAM solvers to run simulations.


X Use various OpenFOAM utilities for parallel simulations.

Visualising

X Use ParaView for post-processing results.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 131
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Outline

1 Introduction

2 The OpenFOAM code

3 OpenFOAM simulations

4 Programming in OpenFOAM

5 Hands-on training session

6 Conclusions

7 Appendix

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 132
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Basic Linux commands I

• ls <directory>
• cd <directory> List contents of a directory.
Change to a directory location.
• tree
• cd Print the current directory structure.
Change to the home directory.
• clear
• cd .. Clears the terminal window.
Go one level up from current directory.
• ps / top
• cd ../.. Displays information about current running
Go two levels up from current directory. processes.

• mkdir <directory> • kill <PID>


Create a directory. Kills a process ID.

• rm -r <directory> • <command> -help


Remove directory with all its contents. See help for a particular command.

• pwd • man <command>


Print the present working directory path. Manual for a particular command, press ’q’
to quit.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 133
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Basic Linux commands II

• cp <directory1> <directory2>
Copy a directory from directory path 1 to 2.

• mv <file1> <file2>
Rename file1 to file2.

• vi <file>
Open a text file, press ’:i’ to insert text, ’:w’ to save, ’:q’ to quit, ’SHIFT+zz’ to save and quit.

• cat <file>
Print contents of the file..

• head -10 <file>


Print first 10 lines of a file.

• tail -50 <file>


Print last 50 lines of a file.

Jibran Haider (PhD Computational Mechanics) OpenFOAM course for beginners 134
Introduction Code Simulations Programming Tutorials Conclusions Appendix

Useful Linux commands for OpenFOAM

• blockMesh -help
See usage of an OpenFOAM command.

• find $FOAM_TUTORIALS -name sampleDict


Search sampleDict file in ’$FOAM_TUTORIALS’ directory.

• find $FOAM_SRC -type f | xargs grep -l "::findNearestCell"


Search the definition of ’findNearestCell’ function in ’$FOAM_SRC’ directory.

• find $FOAM_SRC -type l | xargs grep -l typedef | xargs grep -l


"volScalarField;"
Find the typedef , ’volScalarField’ in ’$FOAM_SRC’ directory.

• find $FOAM_SOLVERS -name "*FvPatch*"


Find solver specific boundary conditions in ’$FOAM_SOLVERS’ directory.

• find $FOAM_SOLVERS -name *.C | xargs grep -l incompressible | xargs grep -l


multiphase
Find ’*.C’ files in ’$FOAM_SOLVERS’ directory which contain the words ’incompressible’ and
’multiphase’.

View publication
Jibran Haider statsComputational Mechanics)
(PhD OpenFOAM course for beginners 135

You might also like