CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   FLUENT (http://www.cfd-online.com/Forums/fluent/)
-   -   read file in UDF (http://www.cfd-online.com/Forums/fluent/48592-read-file-udf.html)

Echo June 24, 2008 11:05

read file in UDF
 
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

Rami June 25, 2008 01:46

Re: read file in UDF
 
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

Echo June 25, 2008 10:58

Re: read file in UDF
 
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)

}

}

Rami June 29, 2008 02:41

Re: read file in UDF
 
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.


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