CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Fluent UDF and Scheme Programming (
-   -   How to Read a "Profile" format file in UDF? (

gary7 June 28, 2010 17:27

How to Read a "Profile" format file in UDF?
Hi buddies,

does anybody know how can I read the boundary data which is in profile file format into the intended boundary points by using UDF coding?
I know about reading a profile from Define->Profiles... menu on the boundaries but I want to know how to do that by UDF, scheme, ... coding...any idea?

I'd really appreciate your help.

dmoroian June 29, 2010 11:06

As far as I remember, the profile file format consist in a list of scheme objects, and is in ascii format. Each object is ordered the same way as the face list in the boundary from wich the profile was saved.
In other words, if you loop in a udf over the faces of the boundary and read line by line the parameter from the profile file, they are in the same order.
Using scheme is even simpler: read the entire file in one go and put it into a variable. This variable will be a list of all objects saved inside the profile (coordinates and parameters), which you can refere sequentially.

gary7 June 29, 2010 14:03

Thank you dmoroian; yes profile file format is a list of scheme objects and it is in ASCII. The problem is that how can I read in that profile file and just take out the required info (values) from it by reading a file?
Would you please give an example of the procedure you mentioned about reading the file and storing it in variable and referring sequentially in a scheme language because I'm a newbie in scheme and couldn't find much info on it.

I appreciate your help...

dmoroian June 30, 2010 03:17

Code snippet
Hello Gary,
I don't know how are you going to use the scheme script, but here it comes a snippet:


(define bubu (open-input-file ""))
(define cucu (read bubu))
(close-input-port bubu)
(format #t "there are ~a objects inside the file ~%" (length cucu))
(format #t "header info: ~a~%" (list-ref cucu 0))
(format #t "name of the first object: ~a~%" (list-ref (list-ref cucu 1) 0)

Have a look at any book about scheme language (e.g.

gary7 June 30, 2010 22:43

Thank you so much dmoroian, I really appreciate your help.

gary7 July 1, 2010 21:46

Hi dmoroian, now with the info you gave I can read the profile into a variable, for storing each part of the profile which is actually a "list" I tried defining new variables using "rp-var-define" and use "rpvarset" so I can save each part of the profile list in a new variable (e.g. positions: x, y, z and velocities: u, v, w ....). Now for example x is a list which contains x-coordinates of the points in profile...
As an example I defined a profile file as:


((tes point 4)
  4.00000E+00  4.00000E+00  4.00000E+00  4.00000E+00)
  3.00000E+00  3.00000E+00  3.00000E+00  3.00000E+00)
  2.00000E+00  2.00000E+00  2.00000E+00  2.00000E+00) 
  5.00000E+00  5.00000E+00  5.00000E+00  5.00000E+00)
  6.00000E+00  6.00000E+00  6.00000E+00  6.00000E+00)
  7.00000E+00  7.00000E+00  7.00000E+00  7.00000E+00)

when I load the scheme file for the first time I get following error:

Error: %prf-set-var: invalid flonum
Error Object: (4 4 4 4)
--> (these are the values of the first variable list x )

if I load the scheme file for the second time I get:

Error: %prf-set-var: invalid flonum
Error Object: (3 3 3 3)

I get this error up to loading the scheme file for the 6th time(which is the last variable in the list):

Error: %prf-set-var: invalid flonum
Error Object: (7 7 7 7)

But loading more than 6 times gives no more error and everything seems to be fine?!!! and when I check the variables with "rp-var-object" it gives the correct list for each variable.
Now my questions are:
1. Do you know what that error is and is that important?
2. Why after loading the scheme file up to the number of defined variables it no more generates that error?
3. Now that I have extracted each variable in a separate list! is that possible to read them in an array in UDF?

I appreciate your help in advance.

dmoroian July 2, 2010 02:13

1. Well, you define a list of 4 elements but put 5 into it (x 4 4 4 4), and you get an error! No surprize here :)
I suspect that fluent will consider only the last 4 elements (due to the declared length of 4), so for instance the first list will be (4 4 4 4) instead of (x 4 4 4 4), and the same for the rest.
2. I'm not sure what you mean by "...after loading the scheme file..." (I suspect you read the profile and not interpret the scheme file), but my guess is that once fluent allocates the memory for all the declared objects, there is no more problem from its point of view.
3. As far as I know, there is no documented function that reads a list from RP variables, although there are few RP_Get_List_* functions in the var.h header. On top of that, your approach seems a bit peculiar, why do you need to read the list using the scheme environment whereas you need it in the udf? Why not reading it directly from the udf? The profile, is a simple text file, you can parse it accordingly.

gary7 July 2, 2010 12:51

1. No I don't put 5 into it because I use, for example,
(rpsetvar 'x (cdr (list-ref cucu 1))) --> (cucu is based on the naming that you used)

which automatically disregard the first element of the list with the "cdr".

2. By loading the scheme file I mean that after preparing the scheme in a text editor and saving with a *.scm, I go to fluent: File->Read->Scheme to load the scheme that I've developed, but I have to do this more than 6 times to get no more error as I mentioned before.

3.I thought Since profile is in scheme format, it'll easier to read it through scheme script and extracting its data . However, I have no idea how to extract the required data (x, y, z, u, v, w, ...) from a profile file in UDF because it also has other unwanted data as, parentheses, variable names,.... How I can just extract the variable values and store them in an array in UDF? (maybe that's a bit C programming question which I'm not an expert in it because I've used Fortran for my coding stuff :)).

dmoroian July 2, 2010 14:53

Ok, so beside the "tes" profile file, you wrote a scheme script that reads the profile and puts some of the objects into rp variables.
It would be very helpful if you post your scheme code. However, I still think it would be much easier to write your on udf to read in the profile. If you disregard the pointers, C is very much like FORTRAN.

gary7 July 2, 2010 15:34

1. This is the scheme body that I've developed:


(define cucu (open-input-file ""))
(define bubu (read cucu))
(close-input-port cucu)
(if (not (rp-var-object 'bc/np))
(rp-var-define 'bc/np 0 'integer #f))
(if (not (rp-var-object 'bc/x))
(rp-var-define 'bc/x 0 'real #f))
(if (not (rp-var-object 'bc/y))
(rp-var-define 'bc/y 0 'real #f))
(if (not (rp-var-object 'bc/z))
(rp-var-define 'bc/z 0 'real #f))
(if (not (rp-var-object 'bc/u))
(rp-var-define 'bc/u 0 'real #f))
(if (not (rp-var-object 'bc/v))
(rp-var-define 'bc/v 0 'real #f))
(if (not (rp-var-object 'bc/w))
(rp-var-define 'bc/w 0 'real #f))

(rpsetvar 'bc/np (list-ref (list-ref md 0) 2))
(rpsetvar 'bc/x (cdr (list-ref bubu 1)))
(rpsetvar 'bc/y (cdr (list-ref bubu 2)))
(rpsetvar 'bc/z (cdr (list-ref bubu 3)))
(rpsetvar 'bc/u (cdr (list-ref bubu 4)))
(rpsetvar 'bc/v (cdr (list-ref bubu 5)))
(rpsetvar 'bc/w (cdr (list-ref bubu 6)))

do yo know why that error message is generated? and is that possible to read these lists (bc/x, bc/y, ....) into arrays in UDF?

2. Can you tell me how I can read in just the profile values (not parentheses and names) into UDF? I know about fscanf but I don't know how to read just the numbers!

dmoroian July 3, 2010 04:06

1. You have to be a bit more organized (let other benefit from your experience, too). Your script shouldn't work since variable md is not defined. However, your error seems to be produced by the fact that you define 'real' variables and assign 'list' to them.
In any case, check what are the values contained in your variables 'bc/x for instance, and see if they are correct

%rpgetvar 'bc/x

FILE *pf;
char *buff, *p, name[255], point[255];
long int dim;
int objDim;

if(NULL == (pf = fopen("","r")))
  Error("Could not open the profile for reading!\n");
dim = ftell(pf);

if(NULL == (buff = (char*) calloc(dim,sizeof(char))))
  Error("Could not allocate the buffer!\n");

if(NULL == (pf = fopen("","r")))
  Error("Could not open the profile for reading!\n");

p = strstr(buff,"(");
p = strstr(p,"(");
sscanf(p,"%s %s %d",name,point,&objDim);/*this should put in variable
                                                            name value "tes", in point
                                                            value "point", and in objDim
                                                            value 4*/

You'll get the picture from here.

gary7 July 4, 2010 01:10

Oh I'm sorry that was a typo because of copy and pasting I corrected the variable name. Is that possible to define a list also?

dmoroian July 5, 2010 10:35

This is just a wild guess, since I've never used it, but try:

(if (not (rp-var-object 'bc/x))
(rp-var-define 'bc/x (cdr (list-ref bubu 1)) 'list #f)

gary7 July 6, 2010 16:45

Hi dmoroian, I appreciate all your help, I applied your suggestion of defining a 'list' instead of 'real' and it's working fine :)

gary7 July 8, 2010 17:16

Hi, I'm trying to see how to compile a C++ program using udf in Fluent. I'm trying to add my very simple developed code to Fluent...

  • First of all I found out that I have to use Fluent DEFINE macros in order to the code be complied by Fluent, is that right?
  • Second, when I use C++ instead of C, I'll get the error that:
The system cannot find the file specified.

knowing that, the c++ code is in my working directory and I've run the Fluent from MSVS command prompt. Would you please tell me how to make the code complied in Fluent, here is my sample code, (it's not intended to do anything special I'm just trying to figure out what is going on...):


#include <iostream>
#include "udf.h"
using namespace std;

int main()
    cout << "Test is OK!\n";

    return 0;
double time_step;
double flow_time = CURRENT_TIME;
if (flow_time < 2.2e-10)
time_step = 2.2e-12;
time_step = 2.2e-3;
return time_step;

I highly appreciate any help.

dmoroian July 9, 2010 02:45

1. Fluent udf's must be written in C not C++.
2. You have to create a library not a stand alone program, which means, among other things, that you cannot include a "main" function in your library, otherwise it will conflict with fluent's "main" function.
An example of your udf, that is syntacticaly working is:

#include "udf.h"
  real time_step;
  real flow_time = CURRENT_TIME;
  if (flow_time < 2.2e-10)
      time_step = 2.2e-12;
      time_step = 2.2e-3;
  return time_step;

gary7 July 9, 2010 17:24

Thanks dmoroian, I really appreciate your inputs. The problem that I had with reading a profile which I somehow was able to solve with your suggestions now I'm trying to develop a UDF which applies these entered values from profile on BC which is doable by using DEFINE_PROFILE, however, after running the simulation for a specific time steps, say 10 time steps, I need to read a new profile file and put its values on BC (replace the previous BC values) and continue the simulation again...Do you have any suggestion?
I'm trying to use DEFINE_EXECUTE_AT_END but I don't know if it is possible to set it to be executed at the end of each 10 time steps or it is just executed at the end of every time steps?
Can I tell that to set the new BC and restart the simulation?

I appreciate any help.

dmoroian July 10, 2010 06:37

DEFINE_EXECUTE_AT_END will be executed every iteration or time step (depending on the steady/unsteady calculation).
Why don't you check inside if the iteration is multiple of 10, and if it is, then read a new file, otherwise do something else?

gary7 July 10, 2010 11:12

Ok thanks, I'll try that. What about the first part of my question: reading the new profile again and continuing the simulation with new BC, is that possible to tell the DEFINE_EXECUTE_AT_END to set the new BC and continue simulation?

dmoroian July 12, 2010 14:03

I think that rp variables are available for the udf once per session. That means, if you change an rp variable using your scheme script, the change will not be seen by your udf unless you make a save and read case in between:
- read profile
- set the rp variables
- save case
- read case
- now the new values of the rp variables are available for the udf
If you want to do this every 10 time steps, then you have to check that in your scheme script and do the above steps every 10 time steps. But in my opinion this is totally opposite to an efficient algorithm.

All times are GMT -4. The time now is 10:17.