CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

How to Read a "Profile" format file in UDF?

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree4Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 28, 2010, 18:27
Default How to Read a "Profile" format file in UDF?
  #1
Member
 
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 15
gary7 is on a distinguished road
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.
gary7 is offline   Reply With Quote

Old   June 29, 2010, 12:06
Default
  #2
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
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.

Last edited by dmoroian; June 30, 2010 at 04:20.
dmoroian is offline   Reply With Quote

Old   June 29, 2010, 15:03
Default
  #3
Member
 
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 15
gary7 is on a distinguished road
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...
gary7 is offline   Reply With Quote

Old   June 30, 2010, 04:17
Default Code snippet
  #4
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
Hello Gary,
I don't know how are you going to use the scheme script, but here it comes a snippet:

Code:
(define bubu (open-input-file "interestingProfile.prof"))
(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. http://www.scheme.com/tspl3).
UpperLeft likes this.
dmoroian is offline   Reply With Quote

Old   June 30, 2010, 23:43
Default
  #5
Member
 
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 15
gary7 is on a distinguished road
Thank you so much dmoroian, I really appreciate your help.
gary7 is offline   Reply With Quote

Old   July 1, 2010, 22:46
Default
  #6
Member
 
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 15
gary7 is on a distinguished road
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:

Code:
((tes point 4)
(x
   4.00000E+00   4.00000E+00   4.00000E+00   4.00000E+00)
(y
   3.00000E+00   3.00000E+00   3.00000E+00   3.00000E+00)
(z
   2.00000E+00   2.00000E+00   2.00000E+00   2.00000E+00)   
(u
   5.00000E+00   5.00000E+00   5.00000E+00   5.00000E+00)
(v
   6.00000E+00   6.00000E+00   6.00000E+00   6.00000E+00)
(w
   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.
UpperLeft likes this.
gary7 is offline   Reply With Quote

Old   July 2, 2010, 03:13
Default
  #7
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
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.
dmoroian is offline   Reply With Quote

Old   July 2, 2010, 13:51
Default
  #8
Member
 
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 15
gary7 is on a distinguished road
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 ).
gary7 is offline   Reply With Quote

Old   July 2, 2010, 15:53
Default
  #9
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
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.
dmoroian is offline   Reply With Quote

Old   July 2, 2010, 16:34
Default
  #10
Member
 
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 15
gary7 is on a distinguished road
1. This is the scheme body that I've developed:

Code:
(define cucu (open-input-file "BProfile.prof"))
(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!

Last edited by gary7; July 3, 2010 at 15:42.
gary7 is offline   Reply With Quote

Old   July 3, 2010, 05:06
Default
  #11
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
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
Code:
%rpgetvar 'bc/x
2.
Code:
FILE *pf;
char *buff, *p, name[255], point[255];
long int dim;
int objDim;

if(NULL == (pf = fopen("BProfile.prof","r")))
   Error("Could not open the profile for reading!\n");
fseek(pf,0L,SEEK_END);
dim = ftell(pf);
fclose(pf);

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

if(NULL == (pf = fopen("BProfile.prof","r")))
   Error("Could not open the profile for reading!\n");
fread(buff,sizeof(char),dim,pf);
fclose(pf);

p = strstr(buff,"(");
p++;
p = strstr(p,"(");
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.
dmoroian is offline   Reply With Quote

Old   July 4, 2010, 02:10
Default
  #12
Member
 
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 15
gary7 is on a distinguished road
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?
gary7 is offline   Reply With Quote

Old   July 5, 2010, 11:35
Default
  #13
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
This is just a wild guess, since I've never used it, but try:
Code:
...
(if (not (rp-var-object 'bc/x))
(rp-var-define 'bc/x (cdr (list-ref bubu 1)) 'list #f)
...
dmoroian is offline   Reply With Quote

Old   July 6, 2010, 17:45
Default
  #14
Member
 
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 15
gary7 is on a distinguished road
Hi dmoroian, I appreciate all your help, I applied your suggestion of defining a 'list' instead of 'real' and it's working fine
gary7 is offline   Reply With Quote

Old   July 8, 2010, 18:16
Default
  #15
Member
 
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 15
gary7 is on a distinguished road
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...):

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

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

    return 0;
}
DEFINE_DELTAT(mydeltat,d)
{
double time_step;
double flow_time = CURRENT_TIME;
if (flow_time < 2.2e-10)
time_step = 2.2e-12;
else
time_step = 2.2e-3;
return time_step;
}
I highly appreciate any help.
gary7 is offline   Reply With Quote

Old   July 9, 2010, 03:45
Default
  #16
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
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:
Code:
#include "udf.h"
DEFINE_DELTAT(mydeltat,d)
{
   real time_step;
   real flow_time = CURRENT_TIME;
   if (flow_time < 2.2e-10)
      time_step = 2.2e-12;
   else
      time_step = 2.2e-3;
   
   return time_step;
}
dmoroian is offline   Reply With Quote

Old   July 9, 2010, 18:24
Default
  #17
Member
 
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 15
gary7 is on a distinguished road
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.
gary7 is offline   Reply With Quote

Old   July 10, 2010, 07:37
Default
  #18
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
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?
dmoroian is offline   Reply With Quote

Old   July 10, 2010, 12:12
Default
  #19
Member
 
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 15
gary7 is on a distinguished road
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?
gary7 is offline   Reply With Quote

Old   July 12, 2010, 15:03
Default
  #20
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
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.
dmoroian is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Gmsh] Compiling gmshFoam with OpenFOAM-1.5 BlGene OpenFOAM Meshing & Mesh Conversion 10 August 6, 2009 05:26
UDF to read data from a file for VOF model Pablo FLUENT 0 October 23, 2008 08:02
Phase locked average in run time panara OpenFOAM 2 February 20, 2008 15:37
Results saving in CFD hawk Main CFD Forum 16 July 21, 2005 21:51
Format file for visualisation Ricardo Nunez Main CFD Forum 2 July 30, 1998 05:12


All times are GMT -4. The time now is 07:18.