我正在为高级 CFD 课程开发可压缩求解器。我将需要运行多个模拟,因此我希望从命令行获取输入,这将允许我使用一些脚本来同时运行我的所有模拟。我目前从命令行获取文件名,然后打开文件以循环遍历每一行。我使用以下代码执行此操作:
// If input file is properly provided, try to read values from it
else
{
std::ifstream input(input_file); // Read input file
// Check if mesh is opened, return error otherwise
if(input.is_open())
{
std::string var; // Store the variable that is being populated
std::string val; // Store the value as a string
// Loop through each line of the input file
while(true)
{
input >> var >> val;
// Check which variable is being populated and assign value appropriately
// Definitely better way to do this, but if it ain't broke don't fix it
if (var == "CFL:")
{
CFL = std::stod(val);
}
else if (var == "nx:")
{
nx = std::stoi(val);
}
else if (var == "xbegin:")
{
xbegin = std::stod(val);
}
else if (var == "xend:")
{
xend = std::stod(val);
}
else if(var == "dt:")
{
dt = std::stod(val);
}
else if (var == "gamma:")
{
dt = std::stod(val);
}
// Leave loop once end of line is reached
if(input.eof())
{
break;
}
}
// Close input file once we are done with it
input.close();
}
这在大多数情况下都有效,但我想知道是否有更好的方法来执行此操作,特别是如果我开始不得不添加要从输入文件中读取的额外变量。如果可能的话,我想避免使用多个 else if 语句,以便更容易地添加更多变量,但我不确定是否有办法做到这一点。谢谢你的帮助。