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

Assigning Values from Text File

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By pakk

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 8, 2016, 11:54
Default Assigning Values from Text File
  #1
Member
 
Join Date: Jun 2016
Posts: 64
Rep Power: 10
Baden is on a distinguished road
Hello all.

Let me start with an overview of what I am trying to accomplish, how I attempted to accomplish it, and the errors resulting from my attempt.

My goal is to read 3 values from each line of a text file (cell ID, porosity, and permeability) and assign them to the specified cell's UDMI. It is important that I be able to specify which cell the values are assigned to within the text file so I can generate custom data profiles in MATLAB and have them preserved when I put them into Fluent.

I've attempted to accomplish this by reading all of the text file's values, storing them in a properly sized array, and then looping through all the cells to assign the the values of porosity and permeability to the proper cell as specified by the cell ID. The main problem I encounter is creating the array since the array is not a fixed size. I've included the code I attempted to compile (along with the errors in red) and a sample of the data file I am using.

The data in the text file is cell ID, porosity, and permeability in that order.
Quote:
1 0.20 9.86923E-16
2 0.13 1.86923E-16
3 0.06 7.86923E-16
4 0.26 5.86923E-16
5 0.21 2.86923E-16
Code:
#include "udf.h"
#include <stdio.h> /* this might be optional if the used I/O functions are included in udf.h */

/* data file properties */
int file_lines = 0;
fp = fopen("data.txt", "r");  error C2099: initializer is not a constant
while(!feof(fp))  error C2059: syntax error : 'while'
{
    file_lines += 1;
}
fclose(fp);

/* hooked functions */
DEFINE_INIT(initialize_UDM, d)
{
    Thread *t;
    cell_t c;
    FILE *fp;
    int cell_ID, i;
    float phi, k;
    float array_name[file_lines][2];  error C2057: expected constant expression, error C2466: cannot allocate an array of constant size 0, error C2133: 'array_name' : unknown size
    
    fp = fopen("data.txt", "r");
    for(i = 0; i <= file_lines; i++)
    {
        fscanf(fp, "%d %f %f", &cell_ID, &phi, &k);
        array_name[cell_ID][0] = phi;  error C2109: subscript requires array or pointer type
        array_name[cell_ID][1] = k;  error C2109: subscript requires array or pointer type
    }
    fclose(fp);
    
    thread_loop_c(t, d)
    {
        if(FLUID_THREAD_P(t))
        {
            begin_c_loop(c, t)
            {
                C_UDMI(c, t, 0) = C_UDMI(c, t, 1) = C_UDMI(c, t, 2) = array_name[c][0];
                C_UDMI(c, t, 3) = C_UDMI(c, t, 4) = array_name[c][1];
            }
            end_c_loop(c, t)
        }
    }
}
I recognize that the main cause of these errors is because I try to initialize an array with a non-constant size, but I don't know how to work around this. Any help in fixing my code or suggestions for an alternative method for coding this problem would be greatly appreciated.
Baden is offline   Reply With Quote

Old   October 10, 2016, 05:59
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
You forgot to assign fp, so you should add:

Code:
FILE * fp;
The following line is not allowed:
Code:
float array_name[file_lines][2];
Because at compile-time, the compiler does not know the value of file_lines so it does not know how much memory to reserve for this array.
The solution around this is to define it as

Code:
float * array_name[2];
And use malloc to allocate memory to this array. I always struggle with this... The easier but less memory-efficient solution: if you know that your file has maximally 1000 lines, use
Code:
float array_name[1000][2];
Some (maybe all?) of the other errors are consequences of these two errors.
Baden likes this.
pakk is offline   Reply With Quote

Old   October 11, 2016, 13:30
Default
  #3
Member
 
Join Date: Jun 2016
Posts: 64
Rep Power: 10
Baden is on a distinguished road
Quote:
Originally Posted by pakk View Post
You forgot to assign fp, so you should add:

Code:
FILE * fp;
The following line is not allowed:
Code:
float array_name[file_lines][2];
Because at compile-time, the compiler does not know the value of file_lines so it does not know how much memory to reserve for this array.
The solution around this is to define it as

Code:
float * array_name[2];
And use malloc to allocate memory to this array. I always struggle with this... The easier but less memory-efficient solution: if you know that your file has maximally 1000 lines, use
Code:
float array_name[1000][2];
Some (maybe all?) of the other errors are consequences of these two errors.
Thank you very much for your response. Is there any chance you could elaborate on how to use malloc? I've done by best to understand it by reading questions on StackExchange, but I've failed to fully grasp how to apply it to my scenario. All the examples I have seen have been significantly more complex than I can understand.
Baden is offline   Reply With Quote

Old   October 12, 2016, 05:31
Default
  #4
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
No, as I said I always struggle with this myself, I know enough to use it, but not to explain it.

But don't you know how many lines your files are? It would really be much easier for you if you could just use that number...
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
Custom Thermophysical Properties wsmith02 OpenFOAM 4 June 1, 2023 15:30
[swak4Foam] funkyDoCalc with OF2.3 massflow NiFl OpenFOAM Community Contributions 14 November 25, 2020 04:30
wmake compiling new solver mksca OpenFOAM Programming & Development 14 June 22, 2018 07:29
[swak4Foam] swak4foam building problem GGerber OpenFOAM Community Contributions 54 April 24, 2015 17:02
DxFoam reader update hjasak OpenFOAM Post-Processing 69 April 24, 2008 02:24


All times are GMT -4. The time now is 21:25.