CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   Defining a parameter for UDF (https://www.cfd-online.com/Forums/fluent-udf/87229-defining-parameter-udf.html)

Philipp_Sch April 14, 2011 10:11

Defining a parameter for UDF
 
Hi all,

I have a UDF which requires a constant, real parameter. Is there a way to give this parameter a value, without having to modify and recompile the source?

Thanks,
Philipp

Amir April 14, 2011 15:28

Hi,
refer to:
"Defining a Scheme Variable in the Text Interface" &
"Accessing a Scheme Variable in a UDF"
in UDF manual.

Philipp_Sch April 15, 2011 03:56

Thanks a lot, Amir!!

Is there a way to check whether a variable (with the given name) has been defined in the TUI? The manual shows how to check this in TUI (which is already quite useful). But I would like to do the same in the UDF, something like

int myVar;
...
if(isDefined("variable-name")
myVar = RP_Get_Integer("variable-name");
else
myVar = DefaultValue;

Amir April 15, 2011 10:55

I'm not sure, but if you check whether the retrieved value in UDF is equal to NULL or not, may help you.

Philipp_Sch April 15, 2011 10:59

A call RP_Get_Real with an invalid scheme variable name produces a runtime error :(

gearboy April 18, 2011 21:23

Quote:

Originally Posted by Philipp_Sch (Post 303722)
Thanks a lot, Amir!!

Is there a way to check whether a variable (with the given name) has been defined in the TUI? The manual shows how to check this in TUI (which is already quite useful). But I would like to do the same in the UDF, something like

int myVar;
...
if(isDefined("variable-name")
myVar = RP_Get_Integer("variable-name");
else
myVar = DefaultValue;

You can use "CX_Interpret_String" function to perform scheme command in UDF.

Philipp_Sch April 19, 2011 04:53

I use the following construction

int ret=0;
const int default_value;
...
ret = (int) CX_Interpret_String("( (if (rp-var-object 'var-name)) (%rpgetvar 'var-name))");
if(!ret)
ret = default_value;
return ret;

The UDF shows correct behaviour, but it returns error messages
Error : eval : unbound variable
Error object: %rpgetvar

These error messages make the execution extremely slow (but do not terminate it!). How can I avoid it?

I have no idea of scheme programming, can anyone suggest me how to write a line that does

" if variable "var_name" is defined, return 1, else return 0 "

gearboy April 19, 2011 21:28

Quote:

Originally Posted by Philipp_Sch (Post 304180)
I use the following construction

int ret=0;
const int default_value;
...
ret = (int) CX_Interpret_String("( (if (rp-var-object 'var-name)) (%rpgetvar 'var-name))");
if(!ret)
ret = default_value;
return ret;

The UDF shows correct behaviour, but it returns error messages
Error : eval : unbound variable
Error object: %rpgetvar

These error messages make the execution extremely slow (but do not terminate it!). How can I avoid it?

I have no idea of scheme programming, can anyone suggest me how to write a line that does

" if variable "var_name" is defined, return 1, else return 0 "

Just use:
CX_Interpret_String("(if (rp-var-object 'var-name) (rpgetvar 'var-name)(rpsetvar 'var-name default-value))");

ret=RP_Get_Integer("var-name");

Philipp_Sch April 20, 2011 04:35

Quote:

Originally Posted by gearboy (Post 304311)
Just use:
CX_Interpret_String("(if (rp-var-object 'var-name) (rpgetvar 'var-name)(rpsetvar 'var-name default-value))");

ret=RP_Get_Integer("var-name");

This gives an error message because rpsetvar tries to set an undefined variable.

I also tried
CX_Interpret_String("(if (not (rp-var-object 'var-name))(rp-var-define 'var-name default-value 'integer #f))");
ret=RP_Get_Integer("var-name");

but it gives me an error "var-name" undefined variable.

I should also mention that I am using a compiled UDF.

Can it be that I need to include certain headers that are not present in udf.h?

gearboy April 27, 2011 21:19

Quote:

Originally Posted by Philipp_Sch (Post 304351)
This gives an error message because rpsetvar tries to set an undefined variable.

I also tried
CX_Interpret_String("(if (not (rp-var-object 'var-name))(rp-var-define 'var-name default-value 'integer #f))");
ret=RP_Get_Integer("var-name");

but it gives me an error "var-name" undefined variable.

I should also mention that I am using a compiled UDF.

Can it be that I need to include certain headers that are not present in udf.h?

"default-value " is a representation of the default value of 'var-name, e.g. -1.

CX_Interpret_String("(if (not (rp-var-object 'var-name))(rp-var-define 'var-name -1 'integer #f))");
ret=RP_Get_Integer("var-name");

Philipp_Sch May 2, 2011 11:07

Quote:

Originally Posted by gearboy (Post 305353)
"default-value " is a representation of the default value of 'var-name, e.g. -1.

CX_Interpret_String("(if (not (rp-var-object 'var-name))(rp-var-define 'var-name -1 'integer #f))");
ret=RP_Get_Integer("var-name");


Yes, that's what I meant. In the actual code, I have something like

CX_Interpret_String("(if (not (rp-var-object 'var-name))(rp-var-define 'var-name -1 'integer #f))");

The error still occurs.

gearboy May 2, 2011 22:32

Quote:

Originally Posted by Philipp_Sch (Post 305954)
Yes, that's what I meant. In the actual code, I have something like

CX_Interpret_String("(if (not (rp-var-object 'var-name))(rp-var-define 'var-name -1 'integer #f))");

The error still occurs.

CX_Interpret_String("(if(not(rp-var-object 'var-name))(rp-var-define 'var-name -1 'integer #f)())");


All times are GMT -4. The time now is 01:45.