Unformatted text preview:

© Fluent Inc. 2/20/01G1Fluent Software TrainingTRN-99-003User Defined Functions© Fluent Inc. 2/20/01G2Fluent Software TrainingTRN-99-003Introductionu What is a User Defined Function?l A UDF is a routine (programmed by the user) written in C which can bedynamically linked with the solver.n Standard C functionss e.g., trigonometric, exponential, control blocks, do-loops, file i/o, etc.n Pre-Defined Macross Allows access to field variable, material property, and cell geometry data.u Why build UDF’s?l Standard interface cannot be programmed to anticipate all needs.n Customization of boundary conditions, source terms, reaction rates, materialproperties, etc.n Adjust functions (once per iteration)n Execute on Demand functionsn Solution Initialization© Fluent Inc. 2/20/01G3Fluent Software TrainingTRN-99-003UDF Basicsu UDF’s assigns values (e.g., boundary data,source terms) to individual cells and cell facesin fluid and boundary zones.l In a UDF, zones are referred to as threads.l A looping macro is used to access individualcells belonging to a thread.n e.g., a face-loop macro visits 563 faceson face zone 3 (velocity-inlet).s Position of each face is availableto calculate and assign spatiallyvarying properties.n Thread and variable references areautomatically passed to UDF whenassigned to boundary in GUI.u Values returned to the solver by UDFs must be in SI units.© Fluent Inc. 2/20/01G4Fluent Software TrainingTRN-99-003Using UDFs in the Solversu The basic steps for using UDFs in FLUENT are as follows:STEP 1: Create a file containing the UDF source codeSTEP 2: Start the solver and read in your case/data filesSTEP 3: Interpret or Compile the UDFSTEP 4: Assign the UDF to the appropriate variable and zone in BC panel.STEP 5: Set the UDF update frequency in the Iterate panelSTEP 6: Run the calculation© Fluent Inc. 2/20/01G5Fluent Software TrainingTRN-99-003Example: Non-Uniform Inlet Velocityu A non-uniform inlet velocity is to be imposed on the 2D turbine vaneshown below. The x-velocity variation is to be specified as:u(y) = 20 [ 1 - (y/0.0745)2]y = 0© Fluent Inc. 2/20/01G6Fluent Software TrainingTRN-99-003Example: Source Codeu The DEFINE_PROFILE macro allowsthe function inlet_x_velocity tobe defined.l All UDFs begin with a DEFINE_macro.l inlet_x_velocity will beidentifiable in solver GUI.u thread and nv are dynamicreferences, input to the UDF toidentify the zone and variablebeing defined, respectively.u The macro begin_f_loop loopsover all faces, f, on thread.u The F_CENTROID macro assigns cell position vector to x[].u The F_PROFILE macro applies the velocity component to face f.#include "udf.h"DEFINE_PROFILE(inlet_x_velocity, thread, nv){ float x[3]; /* this will hold the position vector*/ float y; face_t f; 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) }© Fluent Inc. 2/20/01G7Fluent Software TrainingTRN-99-003Example: Interpreting the UDFu The UDF is saved as velprof.cu Define ÕUser DefinedÕFunctions ÕInterpreted…u Click Compileu The assembly language code willscroll past window.velocity_profile: .local.pointer thread(r0) .local.int position(r1) 0 .local.end 0 save.local.int f (r6) 8 push.int 0 10 save .local.int.....L1:132 restore 133 restore 134 ret.v© Fluent Inc. 2/20/01G8Fluent Software TrainingTRN-99-003Example: Activating the UDFu Access the boundary conditionpanel.u Switch from constant to the UDFfunction in the X-Velocitydropdown list.© Fluent Inc. 2/20/01G9Fluent Software TrainingTRN-99-003Example: Run the Calculationu Run the calculation as usual.u You can change the UDF ProfileUpdate Interval in the Iterate panel(here it is set to1).© Fluent Inc. 2/20/01G10Fluent Software TrainingTRN-99-003Example: Numerical Solutionu The figure at right shows velocityfield throughout turbine bladepassage.u The bottom figure shows thevelocity vectors at the inlet. Noticethe imposed parabolic profile.© Fluent Inc. 2/20/01G11Fluent Software TrainingTRN-99-003Macrosu Macros are pre-defined (Fluent) functions:l Allows definition of UDF functionality and function name (DEFINE_ macro)l Allows access to field variables, cell information, looping capabilities, etc.u Macros are defined in header files.l The udf.h header file must be included in your source code.n #include “udf.h”l The header files must be accessible in your path.n Typically stored in Fluent.Inc/src/ directory.l Other “.h” header files may need to be included.n Depends upon relevant variables and macros needed in your UDF, e.g.,s dpm.h for DPM variable access© Fluent Inc. 2/20/01G12Fluent Software TrainingTRN-99-003 DEFINE Macrosu Any UDF you write must begin with a DEFINE macro:l 14 general purpose macros and 10 DPM-related macros (not listed):DEFINE_ADJUST(name,domain); general purpose UDF called every iterationDEFINE_INIT(name,domain); UDF used to initialize field variablesDEFINE_ON_DEMAND(name); defines an ‘execute-on-demand’ functionDEFINE_RW_FILE(name,face,thread,index); customize reads/writes to case/data filesDEFINE_PROFILE(name,thread,index); defines boundary profilesDEFINE_SOURCE(name,cell,thread,dS,index); defines source termsDEFINE_HEAT_FLUX(name,face,thread,c0,t0,cid,cir); defines heat fluxDEFINE_PROPERTY(name,cell,thread); defines material propertiesDEFINE_DIFFUSIVITY(name,cell,thread,index); defines UDS and species diffusivitiesDEFINE_UDS_FLUX(name,face,thread,index); defines UDS flux termsDEFINE_UDS_UNSTEADY(name,face,thread,index); defines UDS transient termsDEFINE_SR_RATE(name,face,thread,r,mw,yi,rr); defines surface reaction ratesDEFINE_VR_RATE(name,cell,thread,r,mw,yi,rr,rr_t); defines vol. reaction ratesDEFINE_SCAT_PHASE_FUNC(name,cell,face); defines scattering phase function for DOM© Fluent Inc. 2/20/01G13Fluent Software TrainingTRN-99-003Looping and Thread Macroscell_t c; defines a cellface_t f; defines a faceThread *t; pointer to a threadDomain *d; pointer to collection of all threadsthread_loop_c(t, d){} loop that steps through all cell threads in domainthread_loop_f(t, d){} loop that steps through all face threads in domainbegin_c_loop(c, t){} end_c_loop(c, t) loop that steps through all cells in a threadbegin_f_loop(f, t){} end_f_loop(f, t) loop that steps through all faces in a


View Full Document

SMU ME 7337 - User Defined Functions

Download User Defined Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view User Defined Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view User Defined Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?