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?

15
  • 1
    You probably don't need the most efficient way. I am trying to google search for this type of data format. Commented Mar 14, 2020 at 15:33
  • I do need. The lab is asking for that and it's easier for changing parameters. Commented Mar 14, 2020 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. Commented Mar 14, 2020 at 15:38
  • Got it @drescherjm. But I really need doing that :/ Commented Mar 14, 2020 at 15:39
  • 1
    Here's a simple solution that uses only standard features: ideone.com/ws6WA1 Commented Mar 14, 2020 at 15:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

just clarify me, how do I use the .txt file in this?
Replace cin with an open ifstream
the exact problem is in converting tuple into float.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.