|
[Sponsors] |
June 28, 2010, 18:27 |
How to Read a "Profile" format file in UDF?
|
#1 |
Member
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 16 |
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. |
|
June 29, 2010, 12:06 |
|
#2 |
Senior Member
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20 |
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. |
|
June 29, 2010, 15:03 |
|
#3 |
Member
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 16 |
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... |
|
June 30, 2010, 04:17 |
Code snippet
|
#4 |
Senior Member
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20 |
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) |
|
June 30, 2010, 23:43 |
|
#5 |
Member
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 16 |
Thank you so much dmoroian, I really appreciate your help.
|
|
July 1, 2010, 22:46 |
|
#6 |
Member
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 16 |
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) ) 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. |
|
July 2, 2010, 03:13 |
|
#7 |
Senior Member
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20 |
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. |
|
July 2, 2010, 13:51 |
|
#8 |
Member
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 16 |
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 ). |
|
July 2, 2010, 15:53 |
|
#9 |
Senior Member
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20 |
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. |
|
July 2, 2010, 16:34 |
|
#10 |
Member
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 16 |
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))) 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. |
|
July 3, 2010, 05:06 |
|
#11 |
Senior Member
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20 |
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 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*/ |
|
July 4, 2010, 02:10 |
|
#12 |
Member
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 16 |
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?
|
|
July 5, 2010, 11:35 |
|
#13 |
Senior Member
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20 |
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) ... |
|
July 6, 2010, 17:45 |
|
#14 |
Member
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 16 |
Hi dmoroian, I appreciate all your help, I applied your suggestion of defining a 'list' instead of 'real' and it's working fine
|
|
July 8, 2010, 18:16 |
|
#15 |
Member
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 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...
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; } |
|
July 9, 2010, 03:45 |
|
#16 |
Senior Member
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20 |
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; } |
|
July 9, 2010, 18:24 |
|
#17 |
Member
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 16 |
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. |
|
July 10, 2010, 07:37 |
|
#18 |
Senior Member
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20 |
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? |
|
July 10, 2010, 12:12 |
|
#19 |
Member
Kasra
Join Date: Jun 2010
Location: USA
Posts: 44
Rep Power: 16 |
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?
|
|
July 12, 2010, 15:03 |
|
#20 |
Senior Member
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20 |
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. |
|
|
|
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 |