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

Read txt file and import values to source term.

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 22, 2010, 14:18
Default
  #21
New Member
 
C.K.
Join Date: Mar 2010
Posts: 18
Rep Power: 16
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: 16
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: 16
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
Senior Member
 
Micael
Join Date: Mar 2009
Location: Canada
Posts: 157
Rep Power: 18
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: 16
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
Senior Member
 
Micael
Join Date: Mar 2009
Location: Canada
Posts: 157
Rep Power: 18
Micael is on a distinguished road
Ça m'a fait plaisir.
Micael is offline   Reply With Quote

Old   June 9, 2011, 04:33
Default reading external data
  #27
Member
 
EH
Join Date: Dec 2010
Posts: 61
Rep Power: 16
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: 38
Rep Power: 15
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: 38
Rep Power: 15
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
Senior Member
 
Micael
Join Date: Mar 2009
Location: Canada
Posts: 157
Rep Power: 18
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: 38
Rep Power: 15
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
Senior Member
 
Micael
Join Date: Mar 2009
Location: Canada
Posts: 157
Rep Power: 18
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: 38
Rep Power: 15
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: 38
Rep Power: 15
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
Senior Member
 
Micael
Join Date: Mar 2009
Location: Canada
Posts: 157
Rep Power: 18
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: 38
Rep Power: 15
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

Old   August 26, 2015, 08:26
Default
  #37
New Member
 
Batuhan Savaskan
Join Date: Aug 2015
Posts: 9
Rep Power: 11
Batuhan is on a distinguished road
Hello Everyone;

I am using fluent and I have transient experimental temperature data for a surface in excel format. I want to import them to fluent and run. How can I do that? Is there anyone who has the code of this.

Thank you very much.
Batuhan is offline   Reply With Quote

Old   July 20, 2016, 12:36
Default
  #38
New Member
 
Sagar
Join Date: Apr 2016
Posts: 23
Rep Power: 10
ksgr 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.
Hello Micael!
Its already 6 years since this post. I'm currently having similar kind of problem.
I am stuck with problem for long time. I have written a UDF to read property of DPM from a file which contains temperature and density values in tabulated fashion. I want to first read the file and assign the read-in values to the arrays. After this through another UDF I want to find the density by linear interpolation (which will happen wherever the particle is during run-time). I followed this thread which is very similar to my case.
but the problem is that it is not getting compiled. I am able to compile other UDF's but I'm not able to specifically build this UDF. I'm suspecting that it has to do with the extern variable, because if I remove this everything works fine. Do you have any idea how this can be solved?
Any help would be appreciated.

Regards
ksgr is offline   Reply With Quote

Old   July 20, 2016, 13:09
Default
  #39
New Member
 
Sagar
Join Date: Apr 2016
Posts: 23
Rep Power: 10
ksgr is on a distinguished road
I will post both the codes, one to read file, which reads temperature, density values (temperature values are written in 1K steps, denisty values are also tabulated corresponding to each of temperature values) and other to carry out the value of density, which should happen during run-time when solver gives temperature value to my UDF.

UDF to read file:
#include "udf.h"
extern float temparr[725],rhoarr[725];
DEFINE_ON_DEMAND(read_data)
{
int i=0;
FILE *fp;
fp = fopen("liq_prop.txt", "r");
printf("file opened\n");
for (i;i<726;i++)
{
fscanf (fp, "%f %f %*f %*f %*f", &temparr[i], &rhoarr[i]); /*read only temperature and density and other columns are suppressed. Even If I read all the columns and assign them to different arrays error remains"
}
fclose(fp);
printf("file closed\n");
}
UDF to calculate density:
#include "udf.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"

float temparr[725],rhoarr[725];

DEFINE_DPM_PROPERTY(particle_density,c,t,p,T)
{
double tgiven = P_T(p);
int i, j;
/*double temparr[725], rhoarr[725];*/
double frac,intpart_temp;
double rhocalc,tempup,templow, tcrit=725.9;
frac = modf(tgiven, &intpart_temp); /*to get integer part of real number*/

for(i=0;i<726;i++)
{
if(tgiven>tcrit)
{
rhocalc = rhoarr[725];
break;
}
else if(temparr[i] == intpart_temp)
{
j = i;
templow = temparr[i];
tempup = templow + 1;
rhocalc = rhoarr[j] + ((rhoarr[j+1]-rhoarr[j]) * (tgiven - templow)/(tempup-templow));
}
}
return rhocalc;
}
If I compile both UDF's together, after I press build I don't get any link messages. When I press load, I get an error: Not able to find libudf.dll. This is not the problem with other UDF's though. When I interpret the read_data UDF, I don't get any file open and close messages on console and I get error: file_readd.c: line 18: label "temparr" not found (pc=44). file_readd.c: line 18: label "rhoparr" not found (pc=52).

Please suggest what can I do to correct it.
ksgr is offline   Reply With Quote

Old   July 20, 2016, 15:04
Default
  #40
Senior Member
 
Micael
Join Date: Mar 2009
Location: Canada
Posts: 157
Rep Power: 18
Micael is on a distinguished road
I am not a fscanf expert so looking at your code I would first verify fscanf by making thing simpler by creating by hand two files, one for temperature and one for density. Each file have only one column in it (you will have to create those file yourself)

then:
Code:
fp = fopen("liq_prop.txt", "r");
would becomes:
Code:
fp1 = fopen("temp.txt", "r");
fp2 = fopen("rho.txt", "r");
and
Code:
fscanf (fp, "%f %f %*f %*f %*f", &temparr[i], &rhoarr[i]);
would becomes:
Code:
fscanf (fp, "%f", temparr[i]);
fscanf (fp, "%f", rhoarr[i]);
I hope these changes can at least highlight something.

Last edited by Micael; July 20, 2016 at 18:01.
Micael is offline   Reply With Quote

Reply

Tags
array, file, source, txt, udf


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
How can I import a momentum source term from a dat May Huang FLUENT 1 September 5, 2011 06:50
help: How can I import a source? May Huang FLUENT 1 September 5, 2011 06: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 14:22


All times are GMT -4. The time now is 16:58.