【发布时间】:2020-03-14 15:32:14
【问题描述】:
我正在用 C++ 编写 CFD 求解器,但我还处于起步阶段。现在我正在编写线性对流的求解器。数学没问题,运行良好,但我还需要编写一个代码来从 .txt 文件中读取变量。 我的代码是:
//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;
}
我需要从 .txt 中获取这些变量值,例如结束时间、元素大小等。然后,我有一个名为“参数”的 .txt,它的写法完全一样:
endtime=0.3
dx=0.025
xmax=2
c=1
C=0.3
从这个 .txt 中获取这些变量值并在代码中使用它的最有效方法是什么?
【问题讨论】:
-
您可能不需要最有效的方法。我正在尝试用谷歌搜索这种类型的数据格式。
-
我确实需要。实验室要求这样做,更改参数更容易。
-
我的意思是担心节省几纳秒(或其他非常小的时间单位)可能是浪费时间。
-
知道了@drescherjm。但我真的需要这样做:/
-
这是一个仅使用标准功能的简单解决方案:ideone.com/ws6WA1