Udf

6
See section 10 of fluent user manual for user-defined functions and examples… 1. Steady velocity function A UDF is used to introduce this parabolic profile at the inlet. The C source code ( vprofile.c) is shown below. The function makes use of Fluent-provided solver functions that are described in Section 5.3. /***********************************************************************/ /* vprofile.c */ /* UDF for specifying steady-state velocity profile boundary condition */ /***********************************************************************/ #include "udf.h" DEFINE_PROFILE(inlet_x_velocity, thread, position) { real x[ND_ND]; /* this will hold the position vector */ real y; face_t f; begin_f_loop(f, thread) { F_CENTROID(x,f,thread); y = x[1]; F_PROFILE(f, thread, position) = 20. - y*y/(.0745*.0745)*20.; } end_f_loop(f, thread) } The function, named inlet_x_velocity, is defined using DEFINE_PROFILE and has two arguments: thread and position. Thread is a pointer to the face's thread, and position is an integer that is a numerical label for the variable being set within each loop. The function begins by declaring variable f as a face_t data type. A one-dimensional array x and variable y are declared as real data types. A looping macro is then used to loop over each face in the zone to create a profile, or an array of data. Within each loop, F_CENTROID outputs the value of the face centroid (array x) for the face with index f that is on the thread pointed to by thread. The y coordinate stored in x[1] is assigned to variable y, and is then used to calculate the x velocity. This value is then assigned to F_PROFILE, which uses the integer position (passed to it by the solver based on your selection of the UDF as the boundary condition for x velocity in the Velocity Inlet panel) to set the x velocity face value in memory. To make use of this interpreted UDF in FLUENT, you must first compile it. Define User-Defined Functions Interpreted... In the Interpreted UDFs panel, name your function in the Source File Name field. If necessary, enter your C preprocessor type in the CPP Command Name

description

udf fluent

Transcript of Udf

Page 1: Udf

See section 10 of fluent user manual for user-defined functions andexamples…

1. Steady velocity function

A UDF is used to introduce this parabolic profile at the inlet. The C source code ( vprofile.c) is shownbelow. The function makes use of Fluent-provided solverfunctions that are described in Section 5.3.

/***********************************************************************//* vprofile.c *//* UDF for specifying steady-state velocity profile boundary condition *//***********************************************************************/#include "udf.h"

DEFINE_PROFILE(inlet_x_velocity, thread, position){

real x[ND_ND]; /* this will hold the position vector */real y;face_t f;

begin_f_loop(f, thread){

F_CENTROID(x,f,thread);y = x[1];F_PROFILE(f, thread, position) = 20. - y*y/(.0745*.0745)*20.;

}end_f_loop(f, thread)

}

The function, named inlet_x_velocity, is defined using DEFINE_PROFILE and has two arguments: threadand position. Thread is a pointer to the face'sthread, and position is an integer that is a numerical label for the variable being set within each loop.

The function begins by declaring variable f as a face_t data type. A one-dimensional array x and variable yare declared as real data types. A looping macro isthen used to loop over each face in the zone to create a profile, or an array of data. Within each loop,F_CENTROID outputs the value of the face centroid (array x)for the face with index f that is on the thread pointed to by thread. The y coordinate stored in x[1] isassigned to variable y, and is then used to calculate the xvelocity. This value is then assigned to F_PROFILE, which uses the integer position (passed to it by thesolver based on your selection of the UDF as theboundary condition for x velocity in the Velocity Inlet panel) to set the x velocity face value in memory.

To make use of this interpreted UDF in FLUENT, you must first compile it.

Define User-Defined Functions Interpreted...

In the Interpreted UDFs panel, name your function in the Source File Name field. If necessary, enter your Cpreprocessor type in the CPP Command Name

Page 2: Udf

field and the size of the stack under Stack Size. Turn on Display Assembly Listing to see an assemblylisting in your console window as the function compiles.Click on Compile and then Close the panel.

To select this user-defined function as the velocity boundary condition for the zone of choice, open theVelocity Inlet panel.

In the X-Velocity drop-down list, select udf inlet_x_velocity, the name that was given to the functionabove. This function will now be used, rather than the valueof (in this example) 0 that appears in the X-Velocity field. Click on OK to accept the new boundarycondition and close the panel.

In this example, a temporally periodic velocity boundary condition will be applied to the inlet of a tubeusing a UDF. The velocity has the form

(10.1.1)

The tube is 1 m long with a radius of 0.2 m. It is assumed to be filled with air with a density of 1 kg/m 3anda viscosity of 2 kg/m-s. The velocity of the airfluctuates about an equilibrium value, v 0, of 20 m/s, with an amplitude of 5 m/s and at a frequency of 10rad/s.

2. Unsteady Velocity specification :

The source code for the velocity profile UDF ( unsteady.c) is shown below.

/**********************************************************************//* unsteady.c *//* UDF for specifying a transient velocity profile boundary condition *//**********************************************************************/

#include "udf.h"

DEFINE_PROFILE(unsteady_velocity, thread, position){

face_t f;

begin_f_loop(f, thread){

real t = RP_Get_Real("flow-time");F_PROFILE(f, thread, position) = 20. + 5.0*sin(10.*t);

}end_f_loop(f, thread)

}

The function, named unsteady_velocity, is defined using the DEFINE_PROFILE macro. The utilityRP_Get_Real("flow-time") is used to look up the realflow time, which is assigned to the variable t. (See Section 6.9 for details on RP_Get_Real.)

Page 3: Udf

Before you can compile this UDF, you must specify an unsteady flow calculation in the Solver panel.

Define Models Solver...

Next you can open the Interpreted UDFs panel. Enter the name of the function in the text entry box underSource File Name and, if necessary, the name of yourC preprocessor. Turn on Display Assembly Listing. Click on Compile and then Close the panel.

Define User-Defined Functions Interpreted...

The sinusoidal velocity boundary condition defined by the UDF can now be selected for the X-Velocity inthe inlet zone. In the Velocity Inlet panel, simply selectudf unsteady_velocity in the drop-down list to the right of the X-Velocity field, and click on OK.

3. Momentum Source :

/******************************************************************//* UDF that adds momentum source term and derivative to duct flow *//******************************************************************/

#include "udf.h"

#define CON 20.0

DEFINE_SOURCE(cell_x_source, cell, thread, dS, eqn){

real source;

if (C_T(cell,thread) <= 288.){/* source term */source = -CON*C_U(cell,thread);

/* derivative of source term w.r.t. x-velocity. */dS[eqn] = -CON;

}else

source = dS[eqn] = 0.;

return source;}

The function, named cell_x_source, is defined on a cell using DEFINE_SOURCE. The constant C inEquation 10.2-1 is called CON in the function, and it is given anumerical value of 20 kg/m 3-s, which will result in the desired units of N/m 3 for the source. Thetemperature at the cell is returned by C_T(cell,thread). Thefunction checks to see if the temperature is below (or equal to) 288 K. If it is, the source is computedaccording to Equation 10.2-1 ( C_U returns the value of the xvelocity of the cell). If it is not, the source is set to 0.0. At the end of the function, the appropriate value forthe source is returned to the FLUENT solver. The UDF

Page 4: Udf

is executed as interpreted (see Section 7.2).

4. Solidification via a Temperature-Dependent Viscosity

UDFs for properties (as well as sources) are called from within a loop on cells. For this reason, functionsthat specify properties are only required to compute theproperty for a single cell, and return the value to the FLUENT solver.

The UDF in this example generates a variable viscosity profile to simulate solidification, and is applied tothe same problem that was presented in Section 10.2.1.The viscosity in the warm ( T > 288 K) fluid has a molecular value for the liquid (5.5 kg/m-s), while theviscosity for the cooler region ( T < 286 K) has amuch larger value (1.0 kg/m-s). In the intermediate temperature range (286 K 288 K), the viscosity followsa linear profile that extends between the twovalues given above:

(10.3.1)

This model is based on the assumption that as the liquid cools and rapidly becomes more viscous, itsvelocity will decrease, thereby simulating solidification. Here,no correction is made for the energy field to include the latent heat of freezing. The C source code for theUDF is shown below.

/*********************************************************************//* UDF for specifying a temperature-dependent viscosity property *//*********************************************************************/

#include "udf.h"

DEFINE_PROPERTY(cell_viscosity, cell, thread){

real mu_lam;real temp = C_T(cell, thread);

if (temp > 288.)mu_lam = 5.5e-3;

else if (temp > 286.)mu_lam = 143.2135 - 0.49725 * temp;

elsemu_lam = 1.;

return mu_lam;}

The function, named cell_viscosity, is defined on a cell using DEFINE_PROPERTY. Two real variablesare introduced: temp, the value ofC_T(cell,thread), and mu_lam, the laminar viscosity computed by the function. The value of thetemperature is checked, and based upon the range into which itfalls, the appropriate value of mu_lam is computed. At the end of the function, the computed value formu_lam is returned to the solver.

Page 5: Udf

To make use of a user-defined property, you will use the Materials panel. In the drop-down list forViscosity, select the user-defined option.

Once you select this option, the User-Defined Functions panel opens, from which you can select theappropriate function name. In this example, only one isavailable, but in another example, you might have several functions from which to choose. (Recall that ifyou need to compile more than one interpreted UDF, thefunctions can be concatenated prior to compiling. See Section 7.2.1 for details.)

5. Wall Heat Flux

The wall heat flux function ( DEFINE_HEAT_FLUX) can be used to modifythe way that the solver computes the heat flux betweena wall and the neighboring fluid cells. For example, you can customizethe heat transfer coefficient or the law-of-the-wall fortemperature. The UDF presented below uses an internal heat transfercoefficient to specify the wall heat flux. It is another exampleof a UDF that utilizes the ADJUST function to adjust a computed value,and it is executed as a compiled UDF.

The diffusive heat flux coefficients ( cid) are specified in this UDF.The diffusive heat flux ( qid) will then be computed byFLUENT using the following equation:

qid = cid[0] + cid[1]*C_T(c0,t0) - cid[2]*F_T(f,t) -cid[3]*pow(F_T(f,t),4)

/***********************************************************************//* UDF for specifying the diffusive heat flux between a wall and*//* neighboring cells*//***********************************************************************/

#include "udf.h"

static real h = 0.; /* heat transfer coefficient (W/m^2 C)*/

DEFINE_ADJUST(htc_adjust, domain){

/* Define the heat transfer coefficient. */

h = 120;}

DEFINE_HEAT_FLUX(heat_flux, f, t, c0, t0, cid, cir){

cid[0] = 0.;cid[1] = h;

Page 6: Udf

cid[2] = h;cid[3] = 0.;

}