Chapter 8 User-Defined Functions

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

4 Control Statements: Part 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Chapter 8 Exergy: A Measure of Work Potential Study Guide in PowerPoint to accompany Thermodynamics: An Engineering Approach, 7th edition by Yunus.
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
17 Copyright © 2005, Oracle. All rights reserved. Deploying Applications by Using Java Web Start.
Workshop 6 Electronics Cooling with Natural Convection and Radiation
Chapter 7: Arrays In this chapter, you will learn about
Chapter 10 Transient Flow Modeling
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Chapter 17 Linked Lists.
Vanderbilt Business Objects Users Group 1 Reporting Techniques & Formatting Beginning & Advanced.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
1 Application of for Predicting Indoor Airflow and Thermal Comfort.
1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
25 seconds left…...
Fluent Overview Ahmadi/Nazridoust ME 437/537/637.
Chapter 10: The Traditional Approach to Design
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
Systems Analysis and Design in a Changing World, Fifth Edition
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Lecture 20: Laminar Non-premixed Flames – Introduction, Non-reacting Jets, Simplified Description of Laminar Non- premixed Flames Yi versus f Experimental.
Workshop 7 Tank Flushing
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
Benchmark Series Microsoft Excel 2010 Level 1
Data Structures Using C++ 2E
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
UNICAMP GENERAL PARTICLE TRACKING ALGORITHM Sources of Information : Phoenics Lecture: GENTRAGENTRA Workshop: GENTRAGENTRA GENTRA user guide: (html) or.
Chapter 4 Boundary Conditions
D-1 ANSYS, Inc. Proprietary © 2009 ANSYS, Inc. All rights reserved. April 28, 2009 Inventory # Appendix D Profile Boundary Conditions Introduction.
© Fluent Inc. 5/10/2015N1 Fluids Review TRN Postprocessing and Visualization.
2003 International Congress of Refrigeration, Washington, D.C., August 17-22, 2003 CFD Modeling of Heat and Moisture Transfer on a 2-D Model of a Beef.
Workshop 6 Modeling of Catalytic Convertor
Workshop 5 Centrifugal Pump
© Fluent Inc. 5/17/ Introductory FLUENT Notes FLUENT v6.0 Jan 2002 Fluent User Services Center Boundary Conditions.
Chapter 1 Introduction to CFD
Thermo-fluid Analysis of Helium cooling solutions for the HCCB TBM Presented By: Manmeet Narula Alice Ying, Manmeet Narula, Ryan Hunt and M. Abdou ITER.
Workshop 3 Room Temperature Study
Instructor: André Bakker
Chapter 7 Interfaces, Sources and Additional Variables
Heat Transfer Modeling
Chapter 10 Heat Transfer Introduction to CFX.
Chapter 11 CFX Expression Language (CEL)
Heat Transfer and Thermal Boundary Conditions
Chapter 9 CAD & Parameters
© Fluent Inc. 9/20/ Introductory FLUENT Notes FLUENT v6.0 Jan 2002 Fluent User Services Center Solver Basics.
Workshop 4 Flow Through Porous Media
Workshop 3 Room Temperature Study (Part 1)
Appendix B Turbo Pre and Post
6-1 ANSYS, Inc. Proprietary © 2009 ANSYS, Inc. All rights reserved. April 28, 2009 Inventory # Chapter 6 Day 1 Review and Tips Introduction to CFX.
Introduction to Mfix M. Syamlal, Fluent, Inc. National Energy Technology Lab Morgantown, WV
CFX-10 Introduction Lecture 1.
WS7-1 ANSYS, Inc. Proprietary © 2009 ANSYS, Inc. All rights reserved. April 28, 2009 Inventory # Introductory FLUENT Training Workshop 7 Tank Flushing.
FALL 2015 Esra Sorgüven Öner
Discretization Methods Chapter 2. Training Manual May 15, 2001 Inventory # Discretization Methods Topics Equations and The Goal Brief overview.
Introduction Chapter 1. Training Manual March 15, 2001 Inventory # Prerequisites Prerequisites for the Heat Transfer Seminar include: –Successful.
Multiple Species Chapter 7. Training Manual May 15, 2001 Inventory # Objectives The FLOTRAN multiple species capabilities. Dilute mixture option.
COUPLED ANALYSES Chapter 7. Training Manual May 15, 2001 Inventory # Fluid-Structure Analysis “One Way” Analysis –Structural deformation effect.
13-1 ANSYS, Inc. Proprietary © 2009 ANSYS, Inc. All rights reserved. April 28, 2009 Inventory # Chapter 13 Solver.out File and CCL Introduction to.
Workshop 7 Tank Flushing
Workshop 7 Tank Flushing
Chapter 13 Solver .out File and CCL
Workshop 5 Centrifugal Pump
User-Defined Functions
Fluent Overview Ahmadi/Nazridoust ME 437/537/637.
Introductory FLUENT Training
Presentation transcript:

Chapter 8 User-Defined Functions Introductory FLUENT Training Sharif University of Technology Lecturer: Ehsan Saadati ehsan.saadati@gmail.com

Outline A brief introduction to FLUENT user-defined functions Overview of FLUENT Data Structure and Macros Two Examples Where to get more information and help UDF Support

Introduction What is a User Defined Function? Why program UDFs? A UDF is a function (programmed by the user) written in C which can be dynamically linked with the FLUENT solver. Standard C functions Trigonometric, exponential, control blocks, do-loops, file i/o, etc. Pre-Defined Macros Allows access to field variable, material property, and cell geometry data and many utilities Why program UDFs? Standard interface cannot be programmed to anticipate all needs: Customization of boundary conditions, source terms, reaction rates, material properties, etc. Customization of physical models User-supplied model equations Adjust functions (once per iteration) Execute on Demand functions Solution Initialization

User Access to the FLUENT Solver User-Defined Properties User-Defined BCs User Defined INITIALIZE Segregated PBCS Exit Loop Repeat Check Convergence Update Properties Solve Turbulence Equation(s) Solve Species Solve Energy Initialize Begin Loop DBCS Solve Other Transport Equations as required Solver? Solve Mass Continuity; Update Velocity Solve U-Momentum Solve V-Momentum Solve W-Momentum Solve Mass & Momentum Solve Mass, Momentum, Energy, Species User-defined ADJUST Source terms Source terms This slide takes a birds-eye-view of the FLUENT solvers. The solution procedure begins with initialization, following which an iteration loop is opened. For keeping things simple, we are considering only steady state analysis procedure here. Within the loop we solve the conservation equations for mass, momentum and energy either as coupled or in sequence based on the choice of the solver. Other scalar equations like turbulence, species and user-defined-scalars are solved sequentially after the basic flow equations. We then update boundary conditions, properties based on the recent solution Next the state of convergence is checked and a decision is made whether or not to continue through the iteration loop. Now, for users to customize the code, we needed to design ‘access-point’s for the user. Those are shown in blue boxes. These are: The user can initialize the fields his way; Can take control at the top of the iteration loop for making some special computation; Can add source terms to all equations; Can defined most properties with custom functions; Can prescribe special boundary conditions. And what are not shown here are that the user can make model specific changes - for example, can modify convective flux function for scalars, various multiphase functions, modify turbulent viscosity or even time steps in unsteady analysis.

Fluent UDF Data Structure (1) The cell zones and face zones of a model (in the finite-volume scheme) are accessed in UDFs as Thread data types Thread is a FLUENT-defined data type In order to access data in a thread (zone), we need to provide the correct thread pointer, and use FLUENT provided loop macro to access each member (cell or face) in that thread. Domain Cells Cell Thread face Thread Faces Domain Cell Boundary (face thread or zone) Fluid (cell thread or zone) 5

Fluent UDF Data Structure (2) cell_t declares an integer data type used to identify cells face_t declares an integer data type used to identify faces Type Variable Meaning of the declaration Domain *d; d is a pointer to domain thread Thread *t; t is a pointer to thread cell_t c; c is cell thread variable face_t f; f is a face thread variable Node *node; node is a pointer to a node. Boundary face-thread (boundary-face ensemble) Fluid cell-thread (control-volume ensemble) Internal face-thread (internal-face ensemble) associated with cell-threads Nodes 6

7 Loop Macros in UDF Several frequently used loop macros: Loop over all cell threads in domain d: thread_loop_c(ct,d) { } Loop over face threads in domain d: thread_loop_f(ft,d) { } Loop over all cells in a cell thread t: begin_c_loop(c, t) {…} end_c_loop (c,t) Loop over faces in a face thread f_thread: begin_f_loop(f, f_thread) { … } end_f_loop(f, f_thread) d: a domain pointer ct, t: a cell thread pointer ft,f_thread: a face thread pointer c: a cell thread variable f: a face thread variable 7

Example – Parabolic Inlet Velocity Profile We would like to impose a parabolic inlet velocity to the 2D elbow shown. The x velocity is to be specified as We need to know the centroids of the inlet faces via a macro, and another macro to perform the boundary condition assignment.

Step 1 – Prepare the Source Code The DEFINE_PROFILE macro allows the function x_velocity to be defined. All UDFs begin with a DEFINE_ macro x_velocity will appear in the solver GUI thread and nv are arguments of the DEFINE_PROFILE macro, which are used to identify the zone and variable being defined, respectively The macro begin_f_loop loops over all faces f, pointed by thread The F_CENTROID macro assigns cell position vector to x[] The F_PROFILE macro applies the velocity component to face f The code is store as a text file inlet_bc.c Header file “udf.h” must be included at the top of the program by the #include command #include "udf.h" DEFINE_PROFILE(x_velocity,thread,nv) { float x[3]; /* an array for the coordinates */ float y; face_t f; /* f is a face thread index */ begin_f_loop(f, thread) F_CENTROID(x,f,thread); y = x[1]; F_PROFILE(f, thread, nv) = 20.*(1.- y*y/(.0745*.0745)); } end_f_loop(f, thread)

Step 2 – Interpret or Compile the UDF Compiled UDF Add the UDF source code to the Source Files list Click Build to compile and link the code If no errors, click Load to load the library You can also unload a library if needed. /define/user-defined/functions/manage Interpreted UDF Add the UDF source code to the Source File Name list. Click Interpret The assembly language code will display in the FLUENT console Click Close if there is no error Define User-Defined Functions Compiled Define User-Defined Functions Interpreted

Interpreted vs. Compiled UDFs Functions can either be read and interpreted at run time or compiled and grouped into a shared library that is linked with the standard FLUENT executable. Interpreted code vs. compiled code Interpreted Interpreter is a large program that sits in the computer’s memory. Executes code on a “line by line” basis instantaneously. Advantage – Does not require a third-party compiler. Disadvantage – Interpreter is slow and takes up memory. Compiled (refer to the FLUENT User’s Guide for instructions) UDF code is translated once into machine language (object modules). Efficient way to run UDFs. Creates shared libraries which are linked with the rest of the solver. Overcomes many interpreter limitations such as mixed mode arithmetic, structure references, etc. Interpretation of your code should only be considered before the C/C++ compiler is installed on your system. Explain for simple problems interpreted codes is the way to go. Does not require Makefiles. However, handles only a subset of the C language.

Step 3 – Hook the UDF in FLUENT GUI Open the boundary condition panel for the surface to which you would like to apply the UDF Switch from Constant to udf x_velocity in the drop-down list. The macro name is the first argument of DEFINE_PROFILE in the UDF code

Step 4 – Run the Calculations You can change the Profile Update Interval in the Run Calculation panel (default value is 1). This setting controls how often (either iterations or time steps if unsteady) the UDF profile is updated. Run the calculation as usual.

Numerical Solution of the Example The figure on the left shows the velocity field through the 2D elbow. The figure on the right shows the velocity vectors at the inlet. Notice the imposed parabolic velocity profile.

Other UDF Hooks In addition to defining boundary values, source terms, and material properties, UDFs can be used for: Initialization Executes once per initialization. Solution adjustment Executes every iteration. Wall heat flux Defines fluid-side diffusive and radiative wall heat fluxes in terms of heat transfer coefficients Applies to all walls User-defined surface and volumetric reactions Read/write to/from case and data files Read order and write order must be same. Execute-on-Demand capability Does not participate in the solver iterations They are hooked into the solver using the User-Defined Function Hooks panel. Define User-Defined Function Hooks

Example 2 – Custom Initialization Initialize a temperature of 600 K inside a sphere, with its center at (0.5, 0.5, 0.5), radius of 0.25, and 300 K throughout the rest of the domain. The domain pointer is passed to this UDF through the argument thread_loop_c macro is used to access all cell threads (zones), and begin_c_loop macro is used to access cells within each cell thread Deploy this UDF as a user defined function hook. #include "udf.h“ DEFINE_INIT(my_init_function, domain) { cell_t c; Thread *ct; real xc[ND_ND]; thread_loop_c(ct,domain) begin_c_loop (c,ct) C_CENTROID(xc,c,ct); if (sqrt(ND_SUM(pow(xc[0]-0.5,2.), pow(xc[1] - 0.5,2.), pow(xc[2] - 0.5,2.))) < 0.25) C_T(c,ct) = 600.; else C_T(c,ct) = 300.; } end_c_loop (c,ct)

DEFINE Macros Examples of top-level DEFINE macros DEFINE_ADJUST(name,domain); general purpose UDF called every iteration DEFINE_INIT(name,domain); UDF used to initialize field variables DEFINE_ON_DEMAND(name); an ‘execute-on-demand’ function DEFINE_RW_FILE(name,fp); customize reads/writes to case/data files DEFINE_PROFILE(name,thread,index); boundary profiles DEFINE_SOURCE(name,cell,thread,dS,index); equation source terms DEFINE_HEAT_FLUX(name,face,thread,c0,t0,cid,cir); heat flux DEFINE_PROPERTY(name,cell,thread); material properties DEFINE_DIFFUSIVITY(name,cell,thread,index); UDS and species diffusivities DEFINE_UDS_FLUX(name,face,thread,index); defines UDS flux terms DEFINE_UDS_UNSTEADY(name,cell,thread,index,apu,su); UDS transient terms DEFINE_SR_RATE(name,face,thread,r,mw,yi,rr); surface reaction rates DEFINE_VR_RATE(name,cell,thread,r,mw,yi,rr,rr_t); volumetric reaction rates DEFINE_SCAT_PHASE_FUNC(name,cell,face); scattering phase function for DOM DEFINE_DELTAT(name,domain); variable time step size for unsteady problems DEFINE_TURBULENT_VISCOSITY(name,cell,thread); calculates turbulent viscosity DEFINE_TURB_PREMIX_SOURCE(name,cell,thread,turbflamespeed,source); turbulent flame speed DEFINE_NOX_RATE(name,cell,thread,nox); NOx production and destruction rates It is not necessary and you should not go into a detailed description on each of the above DEFINE macros. Pick a couple and describe what they are used for.

Geometry and Time Macros C_NNODES(c,t); Returns nodes/cell C_NFACES(c,t); Returns faces/cell F_NNODES(f,t); Returns nodes/face C_CENTROID(x,c,t); Returns coordinates of cell centroid in array x[] F_CENTROID(x,f,t); Returns coordinates of face centroid in array x[] F_AREA(A,f,t); Returns area vector in array A[] C_VOLUME(c,t); Returns cell volume C_VOLUME_2D(c,t); Returns cell volume (axisymmetric domain) real flow_time(); Returns actual time int time_step; Returns time step number RP_Get_Real(“physical-time-step”); Returns time step size Don’t discuss all in detail.

Cell Field Variable Macros C_R(c,t); Density C_P(c,t); Pressure C_U(c,t); U-velocity C_V(c,t); V-velocity C_W(c,t); W-velocity C_T(c,t); Temperature C_H(c,t); Enthalpy C_K(c,t); Turbulent kinetic energy (k) C_D(c,t); Turbulent dissipation rate (ε) C_O(c,t); Specific dissipation of k (ω) C_YI(c,t,i); Species mass fraction C_UDSI(c,t,i); UDS scalars C_UDMI(c,t,i); UDM scalars C_DUDX(c,t); Velocity derivative C_DUDY(c,t); Velocity derivative C_DUDZ(c,t); Velocity derivative C_DVDX(c,t); Velocity derivative C_DVDY(c,t); Velocity derivative C_DVDZ(c,t); Velocity derivative C_DWDX(c,t); Velocity derivative C_DWDY(c,t); Velocity derivative C_DWDZ(c,t); Velocity derivative C_MU_L(c,t); Laminar viscosity C_MU_T(c,t); Turbulent viscosity C_MU_EFF(c,t); Effective viscosity C_K_L(c,t); Laminar thermal conductivity C_K_T(c,t); Turbulent thermal conductivity C_K_EFF(c,t); Effective thermal conductivity C_CP(c,t); Specific heat C_RGAS(c,t); Gas constant Don’t discuss all in detail.

Cell Field Variable Macros C_R(c,t); Density C_P(c,t); Pressure C_U(c,t); U-velocity C_V(c,t); V-velocity C_W(c,t); W-velocity C_T(c,t); Temperature C_H(c,t); Enthalpy C_K(c,t); Turbulent kinetic energy (k) C_D(c,t); Turbulent dissipation rate (ε) C_O(c,t); Specific dissipation of k (ω) C_YI(c,t,i); Species mass fraction C_UDSI(c,t,i); UDS scalars C_UDMI(c,t,i); UDM scalars C_DUDX(c,t); Velocity derivative C_DUDY(c,t); Velocity derivative C_DUDZ(c,t); Velocity derivative C_DVDX(c,t); Velocity derivative C_DVDY(c,t); Velocity derivative C_DVDZ(c,t); Velocity derivative C_DWDX(c,t); Velocity derivative C_DWDY(c,t); Velocity derivative C_DWDZ(c,t); Velocity derivative C_MU_L(c,t); Laminar viscosity C_MU_T(c,t); Turbulent viscosity C_MU_EFF(c,t); Effective viscosity C_K_L(c,t); Laminar thermal conductivity C_K_T(c,t); Turbulent thermal conductivity C_K_EFF(c,t); Effective thermal conductivity C_CP(c,t); Specific heat C_RGAS(c,t); Gas constant C_DIFF_L(c,t); Laminar species diffusivity C_DIFF_EFF(c,t,i); Effective species diffusivity Don’t discuss all in detail.

User Defined Memory User-allocated memory for each cell Up to 500 field variables can be defined. Can be accessed by UDFs: C_UDMI(cell,thread,index); F_UDMI(face,thread,index); Can be accessed for any purposes, including user’s own numerical algorithms and postprocessing Information is stored in the FLUENT data file. Define User-Defined Memory

User Defined Scalars FLUENT can solve up to 50 generic transport equations for user-defined scalars Number of UDS variables In which zones the UDS is solved Flux Function DEFINE_UDS_FLUX(name,face,thread,index) Unsteady function DEFINE_UDS_UNSTEADY(name,cell,thread,index,apu,su) If statements are required in order to associate multiple flux and transient functions with each UDS Example Can be used to solve the electromagnetic field equations. Define User-Defined Scalars The setup for UDS are summarized in these two slides.

Additional Macros Still many more macros are available in the following categories and are documented in UDF Manual: Turbulence Models Multiphase models Reacting flows Dynamic mesh Input/Output

Where to Get More Information and Help UDF User Guide A very readable document which should serve as the main source of information. Contains macro definitions, numerous code examples and code fragments. New users of UDFs should quickly go through the UDF manual as a pre-requisite for UDF programming FLUENT’s UDF Archive accessible through www.fluentusers.com Start your own UDF program by modifying an existing UDF program which is close to what you want to do, then step by step add your own code to the program. Attend the Advanced UDF Programming course Submit your help requests through the ANSYS technical support

UDF Technical Support Because UDFs can be very complicated, ANSYS does not assume responsibility for the accuracy or stability of solutions obtained using user-generated UDFs. Support will be generally be limited to guidance related to communication between UDFs and the FLUENT solver. A consulting project provided by the ANSYS technical services engineers is an option to jumpstart your project. Explain for simple problems interpreted codes is the way to go. Does not require Makefiles. However, handles only a subset of the C language.