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

File I/O in Fluent UDF

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

Like Tree1Likes
  • 1 Post By pakk

Reply
 
LinkBack Thread Tools Display Modes
Old   January 16, 2014, 13:48
Default File I/O in Fluent UDF
  #1
New Member
 
Join Date: Dec 2011
Posts: 4
Rep Power: 7
park.1148 is on a distinguished road
Hello,

My name is Stephen, and I am trying to get my UDF to read data from a text file. For convenience purposes, I omitted any part of the UDF that is irrelevant to the file I/O issue. Basically the UDF below should be assign user-defined scalars (UDS) X and Y to all of the cells in my domain, where X and Y are imported data points from my text file (light2.txt; see attached).

I hope someone has encountered this issue before and found a solution. I have searched the internet, including cfd-online.com, but so far, no avail.

Thank you.

---------------------------------------------------------------------------

#include "udf.h"

#define X(c,t)C_UDSI(c,t,0)
#define Y(c,t)C_UDSI(c,t,1)

DEFINE_INIT(system_init, d)
{
Thread *t;
cell_t c;

thread_loop_c(t,d)
{
if(NULL != THREAD_STORAGE(t,SV_UDS_I(1)))
begin_c_loop(c,t)
{
X(c,t) = 10;
Y(c,t) = 1;
}
end_c_loop(c,t)
}
}

DEFINE_ADJUST(system_adj, d)
{
Thread *t;
cell_t c;
real time, h, iter, light;

time = CURRENT_TIME/86400;
h = CURRENT_TIMESTEP/86400;

FILE *fp;
fp = fopen("C:\\Users\\park.1148\\Desktop\\light2.txt", "r");

fscanf(file, "%f %f", &iter, &light);
while (iter < time/h)
{
fscanf(fp, "%f %f", &iter, &light);
}
fclose(fp);

thread_loop_c(t,d)
{
if(NULL != THREAD_STORAGE(t,SV_UDS_I(1)))

begin_c_loop(c,t)
{
X(c,t) = light*10;
Y(c,t) = iter;
}
end_c_loop (c,t)
}
}

---------------------------------------------------------------------------
Attached Files
File Type: txt light2.txt (598 Bytes, 22 views)
park.1148 is offline   Reply With Quote

Old   January 16, 2014, 13:53
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,074
Rep Power: 14
pakk will become famous soon enough
You don't mention what the problem is.

It does not compile? It compiles, but gives an error when you run it? It runs without errors, but gives the wrong results?
pakk is offline   Reply With Quote

Old   January 16, 2014, 14:01
Default
  #3
New Member
 
Join Date: Dec 2011
Posts: 4
Rep Power: 7
park.1148 is on a distinguished road
Sorry about that! I meant to describe the errors that appear after interpretation.

The errors occur on two lines:

Line 32: FILE *fp;
Line 33: fp = fopen("C:\\Users\\park.1148\\Desktop\\light2.txt", "r");

In my output window I see the following:

Line 32: parse error.
Line 33: invalid lvalue in assignment.

According to the UDF manual I think I am using the correct syntax, and the two lines work when I compile them in a custom C program that prints the data read via fscanf and outputted via fprintf. I am a bit stuck here...

pakk, thank you for your prompt reply.
park.1148 is offline   Reply With Quote

Old   January 16, 2014, 14:43
Default
  #4
Senior Member
 
Join Date: Nov 2013
Posts: 1,074
Rep Power: 14
pakk will become famous soon enough
That helps to find a solution

Looking at those lines, I think the problem is that you define fp in the wrong place, the interpreter expects a statement in line 32 and not a definition, because you have already given statements in the preceding lines. As a result of not being able to parse line 32, line 33 is not possible because the interpreter does not know what fp is.
In other words: replace
Code:
Thread *t;
cell_t c;	
real time, h, iter, light;

time = CURRENT_TIME/86400;
h = CURRENT_TIMESTEP/86400;

FILE *fp;
fp = fopen("C:\\Users\\park.1148\\Desktop\\light2.txt", "r");
by
Code:
Thread *t;
cell_t c;	
real time, h, iter, light;
FILE *fp;

time = CURRENT_TIME/86400;
h = CURRENT_TIMESTEP/86400;

fp = fopen("C:\\Users\\park.1148\\Desktop\\light2.txt", "r");
Good luck!
park.1148 likes this.

Last edited by pakk; January 16, 2014 at 14:46. Reason: interpreter, not compiler
pakk is offline   Reply With Quote

Old   January 16, 2014, 15:00
Default
  #5
New Member
 
Join Date: Dec 2011
Posts: 4
Rep Power: 7
park.1148 is on a distinguished road
Strange, I tried to relocate the FILE pointer definition as advised, but the same two errors occur. In addition, when I try to put the "FILE *fp" line in front of the other definitions, I see a parse error for all definitions that follow.

I will continue to troubleshoot this problem - but was wondering if you had any more speculations on this matter.
park.1148 is offline   Reply With Quote

Old   January 16, 2014, 17:11
Default
  #6
Senior Member
 
Join Date: Nov 2013
Posts: 1,074
Rep Power: 14
pakk will become famous soon enough
Hmmm, no speculation now...
I'll try to interpret your code tomorrow, to see if I get the same error.
pakk is offline   Reply With Quote

Old   January 17, 2014, 04:52
Default
  #7
Senior Member
 
Join Date: Nov 2013
Posts: 1,074
Rep Power: 14
pakk will become famous soon enough
Your script (with the relocated definition of fp) is interpreted on my pc without errors or warnings... (After I replaced
fscanf(file, "%f %f", &iter, &light);
by
fscanf(fp, "%f %f", &iter, &light);
but your problem was apparently before that...)

Last edited by pakk; January 17, 2014 at 05:26. Reason: make clear that I used version with relocated fp
pakk is offline   Reply With Quote

Reply

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
OpenFOAM Installation for navalFoam sachinlb OpenFOAM Installation 21 June 23, 2014 08:07
friction forces icoFoam ofslcm OpenFOAM 3 April 7, 2012 10:57
"parabolicVelocity" in OpenFoam 2.1.0 ? sawyer86 OpenFOAM Running, Solving & CFD 21 February 7, 2012 12:44
Version 15 on Mac OS X gschaider OpenFOAM Installation 120 December 2, 2009 11:23
DxFoam reader update hjasak OpenFOAM Post-Processing 69 April 24, 2008 01:24


All times are GMT -4. The time now is 20:19.