|
[Sponsors] |
October 8, 2016, 11:54 |
Assigning Values from Text File
|
#1 | |
Member
Join Date: Jun 2016
Posts: 64
Rep Power: 10 |
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:
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) } } } |
||
October 10, 2016, 05:59 |
|
#2 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
You forgot to assign fp, so you should add:
Code:
FILE * fp; Code:
float array_name[file_lines][2]; The solution around this is to define it as Code:
float * array_name[2]; Code:
float array_name[1000][2]; |
|
October 11, 2016, 13:30 |
|
#3 | |
Member
Join Date: Jun 2016
Posts: 64
Rep Power: 10 |
Quote:
|
||
October 12, 2016, 05:31 |
|
#4 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
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... |
|
|
|
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 |