A variable (or object) is a place in memory where you can store
a value. Every variable has a type (for example, real
), a name, and a value, and may have a storage class identifier (static
or extern
). All variables
must be declared before they can be used. By declaring a variable
ahead of time, the C compiler knows what kind of storage to allocate
for the value.
Global variables are variables that are defined outside of any
single function and are visible to all function(s) within a UDF source
file. Global variables can also be used by other functions outside
of the source file unless they are declared as static
(see Static Variables). Global variables
are typically declared at the beginning of a file, after preprocessor
directives as in
#include "udf.h" real volume; /* real variable named volume is declared globally */ DEFINE_ADJUST(compute_volume, domain) { /* code that computes volume of some zone */ volume = .... }
Local variables are variables that are used in a single function.
They are created when the function is called, and are destroyed when
the function returns unless they are declared as static
(see Static Variables). Local variables
are declared within the body of a function (inside the curly braces
"{" and "}"). In the example below, mu_lam
and temp
are local variables.
The value of these variables is not preserved after the function returns.
DEFINE_PROPERTY(cell_viscosity, cell, thread) { real mu_lam; /* local variable */ real temp = C_T(cell, thread); /* local variable */ if (temp > 288.) mu_lam = 5.5e-3; else if (temp > 286.) mu_lam = 143.2135 - 0.49725 * temp; else mu_lam = 1.; return mu_lam; }
A variable declaration begins with the data type (for example, int
), followed by the name of one or more variables
of the same type that are separated by commas. A variable declaration
can also contain an initial value, and always ends with a semicolon
(;
). Variable names must begin with a letter
in C. A name can include letters, numbers, and the underscore (_
) character. Note that the C preprocessor is case-sensitive
(recognizes uppercase and lowercase letters as being different). Below
are some examples of variable declarations.
int n; /* declaring variable n as an integer */ int i1, i2; /* declaring variables i1 and i2 as integers */ float tmax = 0.; /* tmax is a floating point real number that is initialized to 0 */ real average_temp = 0.0; /* average_temp is a real number initialized to 0.0 */
If you have a global variable that is declared in one source
code file, but a function in another source file needs to use it,
then it must be defined in the other source file as an external variable.
To do this, simply precede the variable declaration by the word extern
as in
extern real volume;
If there are several files referring to that variable then it
is convenient to include the extern
definition
in a header (.h) file, and include the header
file in all of the .c files that want to use
the external variable. Only one .c file should
have the declaration of the variable without the extern
keyword. Below is an example that demonstrates the use of a header
file.
Important:
extern
can be used only in compiled
UDFs.
Suppose that there is a global variable named volume
that is declared in a C source file named file1.c
#include "udf.h" real volume; /* real variable named volume is declared globally */ DEFINE_ADJUST(compute_volume, domain) { /* code that computes volume of some zone */ volume = .... }
If multiple source files want to use volume
in their computations, then volume
can
be declared as an external variable in a header file (for example, extfile.h)
/* extfile.h Header file that contains the external variable declaration for volume */ extern real volume;
Now another file named file2.c can declare volume
as an external variable by simply including extfile.h.
/* file2.c #include "udf.h" #include "extfile.h" /* header file containing extern declaration is included */ DEFINE_SOURCE(heat_source,c,t,ds,eqn) { /* code that computes the per unit volume source using the total volume computed in the compute_volume function from file1.c */ real total_source = ...; real source; source = total_source/volume; return source; }
The static
operator has different effects depending on whether it is
applied to local or global variables. When a local variable is declared as
static
the variable is prevented from being destroyed when a
function returns from a call. In other words, the value of the variable is preserved. When a
global variable is declared as static
the variable is "file
global." It can be used by any function within the source file in which it is declared, but
is prevented from being used outside the file, even if is declared as external. Functions
can also be declared as static
. A static function is visible only
to the source file in which it is defined.
Important: static
variables and functions can
be declared only in compiled UDF source files.
/* mysource.c /* #include "udf.h" static real abs_coeff = 1.0; /* static global variable */ /* used by both functions in this source file but is not visible to the outside */ DEFINE_SOURCE(energy_source, c, t, dS, eqn) { real source; /* local variable int P1 = ....; /* local variable value is not preserved when function returns */ dS[eqn] = -16.* abs_coeff * SIGMA_SBC * pow(C_T(c,t),3.); source =-abs_coeff *(4.* SIGMA_SBC * pow(C_T(c,t),4.) - C_UDSI(c,t,P1)); return source; } DEFINE_SOURCE(p1_source, c, t, dS, eqn) { real source; int P1 = ...; dS[eqn] = -abs_coeff; source = abs_coeff *(4.* SIGMA_SBC * pow(C_T(c,t),4.) - C_UDSI(c,t,P1)); return source; }