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

read file in UDF

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 24, 2008, 11:05
Default read file in UDF
  #1
Echo
Guest
 
Posts: n/a
Hi guys, I need to read some discrete data from a file for all the cells in the domain. Do you have a UDF which can do this or could you please tell me how to achieve this.

thanx
  Reply With Quote

Old   June 25, 2008, 01:46
Default Re: read file in UDF
  #2
Rami
Guest
 
Posts: n/a
Hello Echo,

Here is an example extracted from my UDF, where I add convective heat flux at certain coupled wall zones.

The input file, named fin-9.inp, is

__________________________________________________ _______________

10 3.42 300 .01 ! id, HTC, Tamb, thick side-mi v-wall (metal) 23 3.42 300 .01 ! id, HTC, Tamb, thick side-mi-shadow v-wall (air ) 12 12.14 300 .01 ! id, HTC, Tamb, thick top-m h-wall (air ) 25 12.14 300 .01 ! id, HTC, Tamb, thick top-m-shadow h-wall (metal)

__________________________________________________ _______________

and the relevant part of the UDF that reads it is

__________________________________________________ _______________

#include "udf.h" #include <stdio.h>

float HTCv[100], Tambv[100], thickv[100], HTC, Tamb, thick; int id; char line[80]; FILE *finp, *fout; /* declare file pointers */

DEFINE_EXECUTE_ON_LOADING(report_version, libname) /************************************************** ****************** UDF called on loading of libname ************************************************** *******************/ { /* identify UDF version, function entry print and input echo */ fout = fopen("fin-9.out", "a"); /* open fout for append */ fprintf(fout, "report_version:\n\

***** external convection on coupled walls - udf-9.c of 23/06/08 *****\n\

open file for writing\n");

/* read input */ finp = fopen("fin-9.inp", "r"); /* open finp for read */

while( feof(finp) == 0 ) /* read until EOF reached in input */ {

fgets(line, 70, finp); /* read 70 characters */

sscanf(line, "%d %g %g %g", &id, &HTC, &Tamb, &thick);

HTCv [id] = HTC;

Tambv [id] = Tamb;

thickv[id] = thick; } fclose(finp); /* close finp */ fclose(fout); /* close fout */ }

__________________________________________________ _______________

NOTE: Being an absolute beginner in C, I am sure that the coding is extremely clumsy, but it works fine, and you can get the idea and improve the coding.

I hope it helps, Rami
  Reply With Quote

Old   June 25, 2008, 10:58
Default Re: read file in UDF
  #3
Echo
Guest
 
Posts: n/a
Hi Rami, thanks for your reply. My actual goal was to use the read data for initialization of a UDS. I wrote the following UDF for this purpose but it doesn't work. Do you have an idea where the problem arises from?

#include "udf.h"

DEFINE_INIT(my_init_func,d)

{

cell_t c;

Thread *t;

real xc[ND_ND];

FILE *file;

real numbers[800];

int i,j;

file = fopen("fscanf.txt", "r");

i = 1 ;

while(!feof(file))

{

/* loop through and store the numbers into the array */

fscanf(file, "%f", &numbers[i]);

i++;

}

fclose(file);

/* loop over all cell threads in the domain */

thread_loop_c(t,d)

{

j=0;

/* loop over all cells */

begin_c_loop_all(c,t)

{

j=j+1;

C_CENTROID(xc,c,t);

C_UDSI(c,t,0) = numbers[j];

}

end_c_loop_all(c,t)

}

}
  Reply With Quote

Old   June 29, 2008, 02:41
Default Re: read file in UDF
  #4
Rami
Guest
 
Posts: n/a
As I already said, I am a beginner in C (and also quite new to FLUENT), so I cannot be of much help in debugging. I suggest you add details to explain what you mean by "it doesn't work" so others may help.
  Reply With Quote

Old   July 4, 2017, 12:37
Default
  #5
asc
New Member
 
Sri Charan Akula
Join Date: Jun 2017
Posts: 8
Rep Power: 8
asc is on a distinguished road
Hello Echo.
Did you find out how to make your udf work?
asc is offline   Reply With Quote

Reply


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
using UDF to read file Ellen FLUENT 5 November 25, 2008 17:38
How to read a file from a macro StarCD Siemens 2 July 8, 2004 21:31
Why I can't read one external file? Lee Siemens 7 April 2, 2004 12:25
Read file RBM FLUENT 0 July 14, 2002 13:48
Read data file R.B.M FLUENT 0 July 10, 2002 23:16


All times are GMT -4. The time now is 01:35.