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

File I/O in Fluent UDF

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By pakk

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 16, 2014, 12:48
Default File I/O in Fluent UDF
  #1
New Member
 
Join Date: Dec 2011
Posts: 4
Rep Power: 14
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, 38 views)
park.1148 is offline   Reply With Quote

Old   January 16, 2014, 12:53
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
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, 13:01
Default
  #3
New Member
 
Join Date: Dec 2011
Posts: 4
Rep Power: 14
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, 13:43
Default
  #4
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
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 13:46. Reason: interpreter, not compiler
pakk is offline   Reply With Quote

Old   January 16, 2014, 14:00
Default
  #5
New Member
 
Join Date: Dec 2011
Posts: 4
Rep Power: 14
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, 16:11
Default
  #6
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
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, 03:52
Default
  #7
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
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 04:26. Reason: make clear that I used version with relocated fp
pakk 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
[Other] OpenFOAM Installation for navalFoam sachinlb OpenFOAM Community Contributions 22 July 28, 2017 05:26
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 11:44
Version 15 on Mac OS X gschaider OpenFOAM Installation 113 December 2, 2009 10:23
DxFoam reader update hjasak OpenFOAM Post-Processing 69 April 24, 2008 01:24


All times are GMT -4. The time now is 07:27.