|
[Sponsors] |
January 16, 2014, 13:48 |
File I/O in Fluent UDF
|
#1 |
New Member
Join Date: Dec 2011
Posts: 4
Rep Power: 15 |
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) } } --------------------------------------------------------------------------- |
|
January 16, 2014, 13:53 |
|
#2 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
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? |
|
January 16, 2014, 14:01 |
|
#3 |
New Member
Join Date: Dec 2011
Posts: 4
Rep Power: 15 |
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. |
|
January 16, 2014, 14:43 |
|
#4 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
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"); 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"); Last edited by pakk; January 16, 2014 at 14:46. Reason: interpreter, not compiler |
|
January 16, 2014, 15:00 |
|
#5 |
New Member
Join Date: Dec 2011
Posts: 4
Rep Power: 15 |
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. |
|
January 16, 2014, 17:11 |
|
#6 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
Hmmm, no speculation now...
I'll try to interpret your code tomorrow, to see if I get the same error. |
|
January 17, 2014, 04:52 |
|
#7 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
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 |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[Other] OpenFOAM Installation for navalFoam | sachinlb | OpenFOAM Community Contributions | 22 | July 28, 2017 06:26 |
friction forces icoFoam | ofslcm | OpenFOAM | 3 | April 7, 2012 11: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 | 113 | December 2, 2009 11:23 |
DxFoam reader update | hjasak | OpenFOAM Post-Processing | 69 | April 24, 2008 02:24 |