fluent udf 15.0 l01 introduction

Upload: zoya-ye

Post on 27-Feb-2018

280 views

Category:

Documents


8 download

TRANSCRIPT

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    1/37

    1 2013 ANSYS, Inc. June 4, 2014

    Lecture 1:

    Introduction

    User Defined Functions

    in ANSYS Fluent

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    2/37

    2 2013 ANSYS, Inc. June 4, 2014

    Why use User Defined Functions (UDF

    The Fluent solver is a general-purpose code. In order to customize the

    Fluent solver, users can use their own C-codes called user-definedfunctions (UDFs)to accomplish:

    Special boundary conditions

    Customized or solution dependent material properties

    New physical models

    Reaction rates

    Source terms Customized post-processing

    Solving user-supplied partial differential equations

    More .

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    3/37

    3 2013 ANSYS, Inc. June 4, 2014

    What are User Defined Functions?

    Programs written in C

    Similar mechanism used to develop models in Fluent Powerful and efficient

    Extends the functionality of ANSYS Fluent

    Initial and boundary conditions

    Material properties

    Material and energy transfer through source terms

    Modify solution variables

    Adding extra flow equations

    And many more

    Typically compiled to DLLs/shared object libraries

    Loaded only when required

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    4/37

    4 2013 ANSYS, Inc. June 4, 2014

    Sinusoidal Wall Temperature Variation

    T(x) = +

    .

    Heated wall T = T(x)

    Adiabatic wall

    inlet

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    5/375 2013 ANSYS, Inc. June 4, 2014

    What Does It Take to Create a Model?

    Ability to access and modify

    Interact with the user through a GUI/TUI

    Field variables

    Geometrical and mesh data

    Hooks to the solver at appropriate stages of the solution cycle

    Convenience macros

    Vector macros Looping macros

    Parallel programming

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    6/376 2013 ANSYS, Inc. June 4, 2014

    Basics of UDFs

    User Defined Functions

    in ANSYS Fluent

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    7/377 2013 ANSYS, Inc. June 4, 2014

    CThe Foundation for UDFs

    UDFs when compiled create C libraries that Fluent can use

    Resources to learn C ANSYS Fluent UDF Manual (Appendix A. C Programming Basics)

    E.g. The C-Programming Language, B. Kernighan & D. Ritchie

    Programming practices

    Simplicity, Clarity and Generality

    Naming conventions

    Macros and comments

    Algorithms and data structures

    Design principles

    Testing

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    8/378 2013 ANSYS, Inc. June 4, 2014

    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 Mas

    Momentum

    Energy,

    Species

    User-

    defined

    ADJUST

    Source terms

    Source terms

    SOLVERS

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    9/37

    9 2013 ANSYS, Inc. June 4, 2014

    Data Structures

    Each type of UDF is passed different values and data structures

    depending on its function.

    Most UDFs will need solver data. This is stored in a hierarchy:

    Domainsare collections of threads that make the whole mesh

    Threads(or zones) are collections of cells or faces

    Cellsand facesare made of Nodes

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    10/37

    10 2013 ANSYS, Inc. June 4, 2014

    Geometrical Entities

    The parts of a mesh are:

    Collection of cells and faces are stored in threads.

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    11/37

    11 2013 ANSYS, Inc. June 4, 2014

    Data Structures

    The terms Thread and Zone are interchangeable. The bounda

    and cell zones defined in the User Interface are stored internally

    as threads.

    Zoneshave an associated integer IDseen in the settings

    panel

    In a UDF, the corresponding threadcan be identified using

    this ID

    Threadsare an internal data type which stores information

    about the mesh, models, properties, BCs all in one place

    A cell has information about its bounding faces and a face has

    access to its neighbouring cells.

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    12/37

    12 2013 ANSYS, Inc. June 4, 2014

    Introduction to the

    C Programming Language

    User Defined Functions

    in ANSYS Fluent

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    13/37

    13 2013 ANSYS, Inc. June 4, 2014

    C ProgrammingBasic Syntax Rules

    Each statement must be terminated with a semicolon ;

    Comments can be inserted anywhere between /* and */ All variables must be explicitly declared (unlike in FORTRAN

    Compound statements can be created by enclosing multiple

    statements by braces: { }

    Function definitions have the following format:

    return-value-type function-name(parameter-lis{

    function body}

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    14/37

    14 2013 ANSYS, Inc. June 4, 2014

    C Programming Data Types

    Built-in data types: int, float, double, char, enum

    Ansys Fluent: real, cxboolean

    int niter, a; /* Declare niter and a as integefloat dx[10]; /* dx is an array of numbers indexed

    from 0 to 9: dx[0],dx[1],,dx[9]

    enum {X, Y, Z} /* X,Y,Z are enumeration constants

    real a; /* real is a typedef that switches bfloat for single-precision anddouble for double-precision arith

    cxboolean flag=TRUE; /* Pre-defined enumeration ofFALSE and TRUE*/

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    15/37

    15 2013 ANSYS, Inc. June 4, 2014

    C ProgrammingPointer

    A pointer is a special kind of variable that contains an addressin me

    content, of another variable

    Pointers are declared using the *notation

    We can make a pointer point to the address of pre-defined variable

    int *ip; /* declares a pointer named ip that points to aninteger variable*/

    int a=1;

    int *ip; /* (*ip) contains an integer */

    ip = &a;/* &a returns the address of variable a */Message(ip is a pointer with value %p\n,ip);Message(The integer a pointed to by ip has value = %d\n,*

    ip is a pointer with value 0x40b2

    The integer a pointed to by ip has value = 1

    Output:

    Message

    Define Simila Recomm

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    16/37

    16 2013 ANSYS, Inc. June 4, 2014

    C ProgrammingPointers and Arrays

    Pointers and arrays are closely related:

    The name of an array without an index can be used as a pointthe array

    BUT: An array name cannot be set to a new value. It is fixed tset when it is defined as an array.

    int ar[10]; /* ar is an array of 10 integers */int *ip; /* (*ip) contains an integer */

    ip = ar; /* ar is the address of its first integeso this is equivalent to ip = &(ar[0]

    ar = ip; /* This will cause a compilation error.

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    17/37

    17 2013 ANSYS, Inc. June 4, 2014

    C ProgrammingOperators

    Arithmetic Operators

    Logical Operators

    Assignments

    = (assignment)

    +, -, *, /,% (modulo reduction)++ (increment) /*i++ is post-increment (i=i+1) */-- (decrement) /* j- is post-decrement (j=j-1) */

    , >=,

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    18/37

    18 2013 ANSYS, Inc. June 4, 2014

    C ProgrammingControl Statements

    if-else statement: for loop:

    while loop:

    if ( logical-expression )

    { statements}else{ statements}

    if ( x < 0 )

    y = x/50;else{

    x = -x;y = x/25;

    }

    for (begin ; end ; in

    {statements}

    while ( logical-expre{statements}

    for ( k=0; k < 10; k++){statements}

    while ( x < 5 ){ executed while the condition i

    E.g.:

    E.g.:

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    19/37

    19 2013 ANSYS, Inc. June 4, 2014

    C ProgrammingConditional Operato

    If the condition/logical-expression is true, result1 is returned

    else result2 is returned.

    (logical-expression ? result1 : result2)

    real y = 2;real x1 = (y < 0 ? 5 : 10);real x2 = (y >= 0 ? 5 : 10);Result: x1 = 10 and x2 = 5

    E.g.:

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    20/37

    20 2013 ANSYS, Inc. June 4, 2014

    C ProgrammingUser-defined Data Ty

    Structures are collection of data that are of different type.

    Semicolon (;) at the end of structure declaration is needed!

    Set and get values

    struct {Element 1;

    Element n;};

    struct car_struct{char model_name[2int number_of_whreal top_speed;

    };

    struct car_struct alices_car, bobs_car;alices_car.top_speed = 120.0;if (bobs_car.top_speed > alices_car.top_speed)

    E.g.:

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    21/37

    21 2013 ANSYS, Inc. June 4, 2014

    C ProgrammingUser-defined Data Ty

    To simplify a declaration

    If the example from previous slide is defined as follows

    then the variable can be declared as

    instead of

    typedef ;

    Combined example:typedefstruct car_struct{char model_name[25];

    int number_of_wheels;real top_speed;

    } car_model;

    car_model alices_car; struct car_struct alic

    Simplified:typedefstruct{char model_name[2

    int number_of_whereal top_speed;

    } car_model;

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    22/37

    22 2013 ANSYS, Inc. June 4, 2014

    C ProgrammingFluent Structures

    The most common structure in Fluent is Thread.

    Usually we use pointers to s

    Pre-defined Marcos can be used to access structure elements.

    Note operator: is the same as

    typedefstruct thread_struct{int id;} Thread;

    #define THREAD_ID(t) ((t)->id)

    -> t->id (*t).id

    Thread *ct;

    E.g.:

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    23/37

    23 2013 ANSYS, Inc. June 4, 2014

    Introduction to UDF

    Implementation Using C

    User Defined Functions

    in ANSYS Fluent

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    24/37

    24 2013 ANSYS, Inc. June 4, 2014

    Why UDFs are written in the C Language

    Fluent is written in C because it is a very powerful language. It provides

    level operations such as graphics and networking and low level capab

    mathematical functions and memory operations.

    It enables the linking of extra functionality using shared objects in Uni

    Linked Libraries (DLLs) in windows systems.

    This is a very convenient mechanism for linking in UDFs which allows a sconnection between the main program and the user functions

    Users familiar with other procedural languages such as FORTRAN will

    most of the ideas and syntax used in C

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    25/37

    25 2013 ANSYS, Inc. June 4, 2014

    Mesh Data Types (1) There are several Fluent-specific data types that are associated with mes

    Some of the more commonly used ones are:

    Node stores data associated with a grid point

    face_t identifies a face within a face thread

    cell_t identifies a cell within a cell thread

    Thread stores data that is common to a collection of cells or faces

    Domain stores data associated with a collection of face and cell threa

    Each individual cell can be accessed by using a cell index of type cell_t an

    (the zone which contains the cell)

    Each individual face (boundary or internal) can be accessed by using a fa

    face_t and the face thread (the zone which contains the face)

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    26/37

    26 2013 ANSYS, Inc. June 4, 2014

    Mesh Data Types (2)

    Type Example Declaration

    Domain *d; dis a pointer to a Domain structure

    Thread *t; t is apointer to a Thread structurecell_t c; c iscell index, a simple integerface_t f; f is aface index, a simple integer

    Node *node; node ispointer to a Node structure

    Some mesh or solver data types have a capital letter. These are actual

    structures in the C language and are usually passed between functions

    these structures.

    Examples of how these data types are defined are shown below:

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    27/37

    27 2013 ANSYS, Inc. June 4, 2014

    Mesh Data Types (3)

    Every Zone is associated to a single ID available in the BC panel or the TUI

    Given the ID, the thread associated with that zone

    can be retrieved as:

    Once we have the thread we can access its associated data

    Similarly, a threads ID can be retrieved using:

    /grid/modify-zones/list-zones

    int t_id = 7;Thread *ft;ft = Lookup_Thread(domain, t_id);

    t_id = THREAD_ID(ft);

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    28/37

    28 2013 ANSYS, Inc. June 4, 2014

    C macros and UDF Programming

    C has very powerful macro definition capabilities. These are

    extensively used in Fluent in many ways. Notable example incl

    definitions of: Data structure looping macro

    Geometry macros

    Field data macros

    Logic and status control macros

    Safe arithmetic and trigonometry functions

    Complete set of vector arithmetic operations

    The definition of each UDF also uses a specific DEFINE_ macro

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    29/37

    29 2013 ANSYS, Inc. June 4, 2014

    Looping Macros in a UDF

    thread_loop_c(t,d){} Loop over cell threads in a doma thread_loop_f(t,d){} Loop over face threads in a doma

    begin_c_loop(c,t) Loop over the cells in a given thre

    {}

    end_c_loop(c,t)

    begin_f_loop(f,t) Loop over the faces in a given thr

    {}

    end_f_loop(f,t)

    Fluent provides a set of pre-defined macros to accomplish looping tasks

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    30/37

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    31/37

    31 2013 ANSYS, Inc. June 4, 2014

    Geometry Macros

    Location of

    C_NNODESC_NFACESF_NNODES

    Faces

    Nodes

    C_CENTROID(pos,c,t) x, y, z coords of cell centroid

    F_CENTROID(pos,f,t) x, y, z coords of face centroid

    F_AREA(A,f,t) Area vector of a face; NV_MAG(A) Vector magnitude

    C_VOLUME(c,t) Volume of a cell

    C_VOLUME_2D(c,t) Volume of a 2D cell

    pos and A are vectors which are discussed later.

    Depth is 1m in 2D; 2

    radians in axi-symmetric .

    Node Coordinates:

    NODE_X(nn) Node xcoord; (with Node *nn;)

    NODE_Y(nn) Node ycoord;

    NODE_Z(nn) Node zcoord;

    M

    m

    s

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    32/37

    32 2013 ANSYS, Inc. June 4, 2014

    Cell-Face Connectivity (1)

    The cells on either side of a face (f,ft) can be accessed using the mac

    The macros THREAD_T0(ft)andTHREAD_T1(ft)only need theface thread, not the face index.

    Boundary zones such as inlets will connect to a cell zone on one sidewith THREAD_T0(ft)but THREAD_T1(ft)will returnNULL, aspecial pointer which signifies there is no zone on the other side.

    ct0 = THREAD_T0(ft);ct1 = THREAD_T1(ft);c0 = F_C0(f,ft);c1 = F_C1(f,ft);

    C

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    33/37

    33 2013 ANSYS, Inc. June 4, 2014

    Cell-Face Connectivity (2)

    A faces area can be obtained by

    where F_AREA returns the face normal vectorAandNV_MAG(A)the magnitude of the vectorA.

    The faces that bound a cell can be accessed using this loop :

    real A[3], area;

    F_AREA(A,f,ft);area = NV_MAG(A);

    C0

    A faces

    points f

    int nf;for(nf = 0; nf < C_NFACES(c,ct); nf++){

    f = C_FACE(c,ct,nf);ft= C_FACE_THREAD(c,ct,nf);

    }

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    34/37

    34 2013 ANSYS, Inc. June 4, 2014

    Macros used for Control

    Data_Valid_P()

    TRUE if data is available, FALSE if not.

    FLUID_THREAD_P(t0)

    TRUE if threat t0is a fluid thread, FALSE if not.

    NULLP(ct)

    TRUE if a pointer is NULL, FALSE if not. NNULLP(ct)

    TRUE if a pointer is NOT NULL, FALSE if not.

    if (!Data_Valid_P()) r

    if(NNULLP(THREA{ ... Do things ocell thread.

    }

    f

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    35/37

    35 2013 ANSYS, Inc. June 4, 2014

    UDF Defining Macros

    All UDFs that will be used must be defined using a specific

    DEFINE_macro

    Common examples:

    Profiles : DEFINE_PROFILE

    Source Terms : DEFINE_SOURCE

    Properties : DEFINE_PROPERTY

    User-defined Scalars : DEFINE_UDS_UNSTEADY , DEFINE_UDS_FLUXDEFINE_DIFFUSIVITY

    Initialization : DEFINE_INIT

    Global Functions : DEFINE_ADJUST

    DEFINE_ON_DEMAND

    DEFINE_RW_FILE

    F

    s

    ddi i l f i

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    36/37

    36 2013 ANSYS, Inc. June 4, 2014

    Additional Information

    Many additional macros are available to implement different physic

    models including:

    Combustion models Particle-based models

    Turbulence models

    Radiation models

    etc.

    It is also possible to develop additional numerical methods, particulwhen using User-Defined Scalars (see Lecture 4). Access to low-level

    operations is possible, such as

    Flux discretization

    Variable reconstruction and clipping

    O i f Fl M

  • 7/25/2019 Fluent UDF 15.0 L01 Introduction

    37/37

    37 2013 ANSYS, Inc. June 4, 2014

    Overview of Fluent Macros

    UDF macros are defined in the udf.h file, for instance

    The udf.his a Fluent header file in the

    path\ANSYS Inc\v150\fluent\fluent15.0.0\src

    directory.

    At the beginning of every UDF source code file, the header file MUST be incl

    There are many more header files stored in the same directory which can be

    browsed by users but most are already included automatically within udf.h.

    #include udf.h

    #define DEFINE_PROFILE(name, t, i) void name(Thread *t, int

    #define DEFINE_PROPERTY(name,c,t) real name(cell_t c, Thread