0

I am writing a CFD solver in C++ but I am in the very beginning. Now I am coding a solver for the linear convection. The math is ok, working well, but I need to write also a code for reading variables from a .txt file. My code is:

//Code for solving the convection linear equation from Navier-Stokes

#include <iostream>

using namespace std;

int main()
{
    float endtime=0.3; //simulation time
    float dx=0.025;  //element size
    float xmax=2;  //domain size
    float c=1;  //wave velocity
    float C=0.3;  //Courant-Friedrich-Lewy number
    float dt=C*dx/c;  //defining timestep by the Courant equantion
    int nx=xmax/dx + 1;  //defining number of elements
    int nt=endtime/dt;  //defining number of time points

    float u[nx] = { }; //defining an initial velocity array.
    float un[nx] = { };

    for(int i = 4; i <= 9; i++) //defining contour conditions
    {
        u[i] = 2;
    }
    for(int n=1; n<=nt; n++)
    {
        std::copy(u, u + nx, un);
        for(int i=1; i<=nx; i++)
        {
            u[i] = un[i] - c*(dt/dx)*(un[i]-un[i-1]);

        }
    }
    for (int i = 0; i <= nx; i++) 
    {
       cout << u[i] << endl;
    }

    return 0;
}

I need to take these variables values from a .txt, like the end time, element size, etc. Then, I have a .txt called "parameters", which is exactly written like that:

endtime=0.3
dx=0.025
xmax=2
c=1
C=0.3

What's the most effiecient way to get these variables values from this .txt and use it in the code?

Mehdi Mostafavi
  • 880
  • 1
  • 12
  • 25
Iago Lemos
  • 85
  • 1
  • 5
  • 1
    You probably don't need the most efficient way. I am trying to google search for this type of data format. – drescherjm Mar 14 '20 at 15:33
  • I do need. The lab is asking for that and it's easier for changing parameters. – Iago Lemos Mar 14 '20 at 15:37
  • 1
    What I was saying is worrying about saving a few nano seconds (or other very small time unit) is probably a waste of time. – drescherjm Mar 14 '20 at 15:38
  • Got it @drescherjm. But I really need doing that :/ – Iago Lemos Mar 14 '20 at 15:39
  • I assume you can't use a library, like boost. – drescherjm Mar 14 '20 at 15:40
  • I was searching for something like slice in Python, but didn't find anything. With that I could read the whole line, slice it and take the numerical value. – Iago Lemos Mar 14 '20 at 15:43
  • There are `.ini` file readers but that is more advanced than you need. [https://stackoverflow.com/questions/1417765/parse-config-file-in-c-c](https://stackoverflow.com/questions/1417765/parse-config-file-in-c-c) – drescherjm Mar 14 '20 at 15:44
  • You can use a quick (and dirty) trick to access variables in your program: environment variables. You read them in C by name, using `getenv`, and translate the resulting string with `atof`. As for transforming your text file to environment variables, you can usually just source it to your shell (in bash it is `. file.txt`). – rslemos Mar 14 '20 at 15:44
  • @rslemos I understand it could help me a lot. Actually I was searching for something not too dirty haha. – Iago Lemos Mar 14 '20 at 15:47
  • Will your variables in that file always be in the same order? – JohnFilleau Mar 14 '20 at 15:48
  • This would be a `boost` solution: [https://stackoverflow.com/questions/45253886/read-var-in-config-file](https://stackoverflow.com/questions/45253886/read-var-in-config-file) – drescherjm Mar 14 '20 at 15:48
  • @John yes, for sure. – Iago Lemos Mar 14 '20 at 15:49
  • 1
    Here's a simple solution that uses only standard features: https://ideone.com/ws6WA1 – Chad Mar 14 '20 at 15:51
  • @Chad going to implement this, thanks! I think it can be the solution. – Iago Lemos Mar 14 '20 at 15:53
  • @IagoLemos then you can look at [this question](https://stackoverflow.com/questions/874052/properties-file-library-for-c-or-c). Some answers point to libraries that do the job. – rslemos Mar 14 '20 at 15:53

1 Answers1

4

Using only standard features:

#include <iostream>
#include <tuple>
using namespace std;

tuple<bool, string, string> read_one_value(istream& in)
{
    string name;

    if(getline(in, name, '='))
    {
        string value;

        if(getline(in, value))
        {
            return make_tuple(true, name, value);
        }
    }

    return make_tuple(false, "", "");

}


int main()
{
    for(auto val = read_one_value(cin); get<0>(val); val = read_one_value(cin))
    {   
        std::cout << get<1>(val) << " -> " << get<2>(val) << '\n';
    }

    return 0;
}

This leaves converting from the value string objects to the needed type as an exercise for the reader, and assumes your format of name=value is consistent.

Chad
  • 18,706
  • 4
  • 46
  • 63