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

Read txt file and import values to source term.

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

Like Tree2Likes

Reply
 
LinkBack Thread Tools Display Modes
Old   March 22, 2010, 14:18
Default
  #21
New Member
 
C.K.
Join Date: Mar 2010
Posts: 18
Rep Power: 7
Constantine is on a distinguished road
I have checked it again , I can not find any mistakes in the procedure...I ll be waiting for you to check it out maybe is something that I dont understand...

Constantine
Constantine is offline   Reply With Quote

Old   March 22, 2010, 15:38
Default
  #22
New Member
 
C.K.
Join Date: Mar 2010
Posts: 18
Rep Power: 7
Constantine is on a distinguished road
I checked it again according also to the UDF manual step by step...but the error occurs again I also tried to build the code with nmake and then put it into fluent but i have the same problems...
Constantine is offline   Reply With Quote

Old   March 22, 2010, 18:14
Default
  #23
New Member
 
C.K.
Join Date: Mar 2010
Posts: 18
Rep Power: 7
Constantine is on a distinguished road
Ok i figured out one problem! In fscanf (rfile, "%f",&plasma[i]); the symbol & was missing.Now ive tried this code with the first simplified grid of the 8 cells and it worked fine! But when im going to 30000 the access violation error occurs after the 1st iteration!
And also when I execute on demand the readdata udf i cant see the messages on the fluent's screen ( of the open and close file) and also i can not see anything else so that i can check if the reading of the data is proper or not(ive tried to print on the screen some values of the plasma array but nothing showed up) !!
ill never manage it? :P
Constantine is offline   Reply With Quote

Old   March 23, 2010, 16:12
Default
  #24
Member
 
Micael Boulet
Join Date: Mar 2009
Location: Quebec, Canada
Posts: 95
Rep Power: 9
Micael is on a distinguished road
Well done! You find the error in the code.

I try the UDFs and do not have any problem. The problem can comes from your data.

To help you diagnose the problem, add the following lines into the DEFINE_ON_DEMAND function:

/* add this at the begining after similar lines */
FILE *wfile;
wfile = fopen("data_rewriten.txt", "w");

/* add this line into the loop, after the fscanf line */
fprintf(wfile, "plasma[%i] = %g\n", i, &plasma[i]);

/* add this after the current fclose line */
fclose(wfile);

These additions will produce a new file, data_rewriten.txt, when you will read the data. Then check into that file, it should be the same as data.txt, except that plasma[i] = is added at the beginning of each line (it maybe useful to know the line number). I would not be surprise if data_rewriten.txt doesn't contain what you expect. That can help to figure out the problem.

Do you have only one source (one value) per line? It should, otherwise the code will not work properly.

It is strange to you do not see the messages on the screen. Do you see them with your simple case (8 cells)?

--------------
Edit: I added a line to my post

Last edited by Micael; March 23, 2010 at 20:50.
Micael is offline   Reply With Quote

Old   March 23, 2010, 16:23
Default
  #25
New Member
 
C.K.
Join Date: Mar 2010
Posts: 18
Rep Power: 7
Constantine is on a distinguished road
Hurrayyyyyyyyyy!!!! I did it!!!Well i dont know if my solution is the best but i Interpreted first the readdata code and the data were written in the file that i specified!and then after executing it on demand i interpreted my other code! It seems that it works pretty good now!! And the computational time is reasonable not that far than without the source term on!!!
I am sure that something new will pop up soon though...so I hope that we will be on touch hehe
Thank you Mikael so so so much! I hope I can pay back the help someday!!
Greek cheers from France!
Constantine is offline   Reply With Quote

Old   March 23, 2010, 16:49
Default
  #26
Member
 
Micael Boulet
Join Date: Mar 2009
Location: Quebec, Canada
Posts: 95
Rep Power: 9
Micael is on a distinguished road
Ça m'a fait plaisir.
Micael is offline   Reply With Quote

Old   June 9, 2011, 03:33
Default reading external data
  #27
Member
 
EH
Join Date: Dec 2010
Posts: 61
Rep Power: 6
ehooi is on a distinguished road
Hi,
I have been following this thread as it relates to the problem I am facing. Initially you had a 2x4 grid and later a grid of 30000. May I know if this 30000 cells are uniformly distributed or are they grids made up of triangular cells. Is there still a pattern that fluent reads that determines which grid belong to which data?

Just a brief of what I am doing or need to do. At the moment, I have a grid in Fluent. I intend to export the grid points where in each grid point, I will calculate the force using my own codes. Once the force on each grid point is calculated, I would like FLUENT to read it as input in the NS equation. I am still trying to comprehend how FLUENT will make sure the data in my .txt file correspond to the exact grid in the model.

I look forward to your reply.

Thank you very much.

Sincerely,
EH

Quote:
Originally Posted by Constantine View Post
Hey Mikael,
I have managed to understand how the udf distributes the source in the grid's cells by 'playing' with my data values! I think I have understand its thinking,so I also think I would be able to apply this on my normal grid which is made of 30000 cells!
I have also thought of using the coordinates of each cell but I want my code as simplified as possible at for this moment it runs so im happy! :P
Thanks for the notes for the for loop and the source!
My source term is a force corresponding to the force thats being generated from a plasma actuator.The plasma flow modelling has already been done so the thing is to import the data to this small region (plasma region) for example on a flat plate!So for sure I can not model it inside the UDF ! Anyway its just a matter of pasting data!
Grrrr this thing drives me mad though...I still can't put the grid data into a text file without getting them all messed up!

Thanks anyway!!! Hope I have good results and news soon!

Constantine!
ehooi is offline   Reply With Quote

Old   March 6, 2012, 11:54
Default
  #28
Member
 
Yolk
Join Date: Nov 2011
Posts: 37
Rep Power: 5
Yannian is on a distinguished road
Quote:
Originally Posted by Constantine View Post
Hello again,
yes of course you are right to everything!Well i did some progress i think!Now i have the values i want to the different cells! my code is the following

#include "udf.h"


DEFINE_SOURCE(ymom_source,c,t,dS,eqn)
{

FILE *fp;
real source[8];
real plasma[8];

int i,j;

fp= fopen("force.txt","r");

for (i=0;i<8;i++)
{
fscanf (fp,"%g\n",&plasma[i]);
}

fclose(fp);

for(j=0;j<8;j++)
{

source[j]=plasma[j];
}

dS[eqn]=0.0;

return source[c];
}

The problem now is that my data is not a [2][4] matrix but way bigger! And when im trying to write the data from matlab to txt the numbers become really nonstructured!! But I think this is for a matlab's forum and not for here! :P
Anyway I would be glad if you could help me, and any comment to my code would be useful of course!
Thank you so much for your responses!!!I have a feeling that Ill return here veeeery soon LOL !

Cheers!
May I ask you where you put your 'force.txt' file, when I want to load txt file,the data in it isn't put into the array by fscanf.Can you give me suggestion?
Yannian
Yannian is offline   Reply With Quote

Old   March 6, 2012, 16:06
Default
  #29
Member
 
Yolk
Join Date: Nov 2011
Posts: 37
Rep Power: 5
Yannian is on a distinguished road
Quote:
Originally Posted by Micael View Post
I do not expect the repetitive loop to make it crash. It should just slow down the iterations. That can be something else.

Anyway, it sounds like you need to do it the correct way once for good.

I suggest to write a separate function for reading the data and assign them to an array. You really need to do it only once so it should not be done in a DEFINE_SOURCE function. DEFINE_ON_DEMAND can do the job. That can just look like that:


/************************************************
UDF for reading data and assign them to an array
************************************************/


#include "udf.h"

extern float plasma[30000];

DEFINE_ON_DEMAND(read_data)
{
int i;
FILE *rfile; /* declare a FILE pointer */

rfile = fopen("data.txt", "r"); /* open file for reading */
Message("file opened\n");

/* loop to read file */
i=0; /* initialize before to loop */
for (i=0;i<30000;i++)
{
fscanf (rfile, "%f", plasma[i]); /* that supposes one value per line */
}

fclose(rfile);
Message("file closed\n");
}

Then just use your previous UDF, but quite simplify:

/************************************************** *****
UDF to define the source for each cells
That suppose that values of "c" match proper index in plasma[30000]
************************************************** *****/

#include "udf.h"

float plasma[30000];

DEFINE_SOURCE(ymom_source,c,t,dS,eqn)
{
dS[eqn]=0.0;
return plasma[c];
}


Save each of these into a *.c file. Then compile (build then load) the 2 files together. Once it is done, go the the "Execute on Demand" panel and execute "read_data". That will assign your data to plasma[]. Then the other UDF can use it.

I am still not sure you will go free of trouble with the way you match cell index with your data. The solution may not be complete yet...

You may want to add some code to read_data so that it writes back plasma[] into a text file. Just to make sure it do what you want.

I am please if it helps you. Actually, I just modify a little bit a UDF I already use for reading data, so that was not that difficult to me.
Hi Micael
I have one question that which directory should I put the txt file into? Such as you use 'fopen data.txt' where is the 'data.txt', in the same directory as case file or somewhere else? When I follow your post to use execute on demand there is something wrong, so I guess the problem lies in the directory of 'data.txt'. thanks!
Yannian
Yannian is offline   Reply With Quote

Old   March 6, 2012, 17:57
Default
  #30
Member
 
Micael Boulet
Join Date: Mar 2009
Location: Quebec, Canada
Posts: 95
Rep Power: 9
Micael is on a distinguished road
I don't have stuff at hand now, but I think I put the file in the directory of the case file. What is the problem?
Micael is offline   Reply With Quote

Old   March 7, 2012, 05:12
Default
  #31
Member
 
Yolk
Join Date: Nov 2011
Posts: 37
Rep Power: 5
Yannian is on a distinguished road
Hi Micael,
I use execute on demand to generate a array follow your example. Compile work includes building and loading are both Okay(I use net framework sdk2.0), when I click 'execute on demand', but the fluent give error as follows:

Error:
FLUENT received fatal signal (ACCESS_VIOLATION)
1. Note exact events leading to error.
2. Save case/data under new name.
3. Exit program and restart to continue.
4. Report error to your distributor.
Error Object: #f
the code I write is as follows
/*generating an array pre[16]*/
#include "udf.h"
extern real pre[16];
DEFINE_ON_DEMAND(read_data)
{
int i;
FILE *rfile; /* declare a FILE pointer */
FILE *wfile;
rfile = fopen("p.txt", "r"); /* open file for reading */
wfile = fopen("data_rewriten.txt", "w");
Message("file opened\n");
/* loop to read file */
i=0; /* initialize before to loop */
for (i=0;i<15;i++)
{
fscanf (rfile, "%f", pre[i]); /* that supposes one value per line */
fprintf(wfile, "pre[%i] = %g\n", i, &pre[i]);
}
fclose(rfile);
fclose(wfile);
Message("file closed\n");
}
/*define profile load the array*/
#include "udf.h"
real pre[16];
DEFINE_PROFILE(inlet_velocity,thread,index)
{
int i;
face_t f;
i=1;
begin_f_loop(f,thread) /* loops over all faces in the thread passed in the DEFINE macro argument */
{
F_PROFILE(f,thread,index) = pre[i];
i=i+1;
}
end_f_loop(f,thread)
}

p.txt is as follows
0
-5
-10
-15
-20
-25
-30
-35
-35
-30
-25
-20
-15
-10
-5
0
Yannian is offline   Reply With Quote

Old   March 7, 2012, 10:46
Default
  #32
Member
 
Micael Boulet
Join Date: Mar 2009
Location: Quebec, Canada
Posts: 95
Rep Power: 9
Micael is on a distinguished road
Do these messages execute:?

Message("file opened\n");
Message("file closed\n");

That is, do you see the messages in the command window before you see the error message?

First of all, identify the line or lines causing the problem. Put a message command in the very first line of DEFINE_ON_DEMAND. Something like:

DEFINE_ON_DEMAND(read_data)
{
Message("test\n");

If the message display, then the problem is in some lines further down. In that case, downshift the message by one line until you cannot see the message before the error happen.
Micael is offline   Reply With Quote

Old   March 7, 2012, 13:27
Default
  #33
Member
 
Yolk
Join Date: Nov 2011
Posts: 37
Rep Power: 5
Yannian is on a distinguished road
There are nothing else except the ERROR on the command console, so I didn't see 'file opened' and any other meesage. I tried your advice of writing 'Message("test/n")'on the first line after '{', the thing is the same that I only get the ERROR of fluent gives. Do you have advices to the problem?
Yannian is offline   Reply With Quote

Old   March 7, 2012, 13:57
Default
  #34
Member
 
Yolk
Join Date: Nov 2011
Posts: 37
Rep Power: 5
Yannian is on a distinguished road
Hi Micael,
Fortunatelly I have get the message 'file opened' and 'file closed' on the command line, and 'data_rewriten.txt' which I write data into. However the data in it is not from the file 'p.txt', all the data in 'data_rewriten.txt' is pre[1] = 5.5335e-316 pre[2] = 5.5335e-316and so on. So the data is not from what I program. Thank you for your nice idea! I will continue on it.
Yannian is offline   Reply With Quote

Old   March 7, 2012, 16:19
Default
  #35
Member
 
Micael Boulet
Join Date: Mar 2009
Location: Quebec, Canada
Posts: 95
Rep Power: 9
Micael is on a distinguished road
As you can see in my old post (#24), this line:
Code:
 
fprintf(wfile, "plasma[%i] = %g\n", i, &plasma[i]);
was for problem diagnostic purpose only. What you got is what I actually intended to do, so that is pefectly normal. If you just want to save data from the file 'p.txt', to file 'data_rewriten.txt' (without the "pre[1]=") then you need to replace:
Code:
 
fprintf(wfile, "pre[%i] = %g\n", i, &pre[i]);
by:
Code:
 
fprintf(wfile, "%g\n", i, &pre[i]);
You may need to study C language further, that will help you a lot to write UDF.

Also, do you really need to write data back into another file? That was only suggested for verification purpose.
Micael is offline   Reply With Quote

Old   March 8, 2012, 04:28
Default
  #36
Member
 
Yolk
Join Date: Nov 2011
Posts: 37
Rep Power: 5
Yannian is on a distinguished road
Hi Micael,
Thank you very much!I pretty appreciate your patience to help.The problem has been figured out. The problems maybe are: the name of directory shouldn't has space;The array should be pre[15],not pre[16];the variable of type of pre[15] should be float and not be real;there is '&' in front of pre[i] in the function of fprintf; The code can be run is posted it as follows.
#include "udf.h"
extern real plasma[16];
DEFINE_ON_DEMAND(read_data)
{
int i;
FILE *rfile; /* declare a FILE pointer */
rfile = fopen("D:\yyang9\DISK\March\2d\p.txt", "r"); /* open file for reading */
Message("file opened\n");
/* loop to read file */
i=0; /* initialize before to loop */
for (i=0;i<15;i++)
{
fscanf (rfile, "%f", &plasma[i]); /* that supposes one value per line */
}
fclose(rfile);
Message("file closed\n");
}
/*the DEFINE_PROFILE array use array pre[15]*/
#include "udf.h"
float pre[15];
DEFINE_PROFILE(inlet_pressure,thread,index)
{
int i;
face_t f;
for (i=0; i<16; i++)
{
Message("%3.2f",pre[i]);/*15*/
}
printf("\n");
i=0;
begin_f_loop(f,thread) /* loops over all faces in the thread passed in the DEFINE macro argument */
{
F_PROFILE(f,thread,index) = pre[i];
i=i+1;
}
end_f_loop(f,thread)
}
the file of 'p.txt'
-20
-30
-40
-20
-20
-30
-40
-300
-500
-300
-400
-200
-20
-30
-40
-20
czhao86 likes this.
Yannian is offline   Reply With Quote

Reply

Tags
array, file, source, txt, udf

Thread Tools
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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I import a momentum source term from a dat May Huang FLUENT 1 September 5, 2011 05:50
help: How can I import a source? May Huang FLUENT 1 September 5, 2011 05:49
how do I import tecplot file to fluent Kangello FLUENT 0 December 2, 2009 09:28
Errors running allwmake in OpenFOAM141dev with WM_COMPILE_OPTION%3ddebug unoder OpenFOAM Installation 11 January 30, 2008 21:30
Importing I-DEAS unv file hbetb FLUENT 1 August 10, 2001 13:22


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