[OpenFOAM] 编程基础 03 Command Line Arguments And Options

发布于 2023-05-06  246 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

Command Line Arguments And Options

1. 求解器帮助界面的结构

OpenFOAM中已有的求解器,在终端中输入求解器名称 + -help后我们可以得到如下图所示的帮助信息。这些信息可以分为执行功能选项的Options信息和用于添加笔记的Notes信息:

2. 添加帮助信息(笔记Notes)

添加笔记(Notes)信息可以采用argList::addNote()实现:

#include "fvCFD.H"

int main(int argc, char *argv[])
{
    // Define the help message for this application
    argList::addNote
    (
        "Demonstrates how to handle command line options.\n"
        "\n"
        "Input arguments:\n"
        "----------------\n"
        "  someWord - does stuff\n"
        "  someScalar - does more things\n"
    );

3. 添加执行选项(Options)

当所编写程序不支持并行计算时,可以使用argList::noParallel();隐藏所有与并行相关的选项(如-parallel)。

    // prepare argument list
    argList::noParallel();
    argList::validArgs.append("someWord");
    argList::validArgs.append("someScalar");

    // prepare options
    argList::addOption // string variable
    (
        "dict",
        "word",
        "Path to an additional dictionary (not really used now)"
    ); // 输出效果: -dict <word>  Path to an additional dictionary (not really used now)

    argList::addBoolOption // on/off depending on whether option is given or not
    (
        "someSwitch",
        "Switches from A to B"
    ); // 输出效果: -someSwitch       Switches from A to B

    argList::addOption // integer variable
    (
        "someInt",
        "label",
        "Optional integer"
    ); // 输出效果:  -someInt <label>  Optional integer

注意,仅仅执行以上初始化帮助信息(Notes)和选项(Options)的代码,以上内容并未添加进-help信息中,还需要执行以下代码:

    Foam::argList args(argc, argv);
    if (!args.checkRootCase())
    {
        Foam::FatalError.exit();
    }

实际上,以上5行代码已经包含在setRootCase.H头文件中,因此,只需要引用该头文件即可:

    // create argument list
    // This is normally defined inside setRootCase.H
    #include "setRootCase.H"

以上代码都是旨在丰富求解器的帮助信息(-help)。以下代码则确定了该求解器运行时需要两个参数,第一个是一个字符串,第二个是一个标量。

// read arguments
    const word someWord = args[1];
    // NOTE: the built-in method for converting strings to other data types
    const scalar someScalar = args.argRead<scalar>(2);

    Info << "Got argument word " << someWord << " and scalar " << someScalar << endl;

将以上代码进行编译(wmake)后,在终端中执行:

cmdArgsAndOptions someWordIPassed 5.

应该能在终端中获得以下输出:

Got argument word someWordIPassed and scalar 5

如果没有给出两个参数,运行cmdArgsAndOptions后会得到以下错误信息:

--> FOAM FATAL ERROR:
Wrong number of arguments, expected 2 found 0

FOAM exiting

4. 选项信息读取

如果执行时,对-dict参数给出了值(文件路径),则该文件路径会被赋值给变量dictPath,否则,该变量的值为./system/defaultDict(默认值)。

    // default path to some dictionary
    fileName dictPath("./system/defaultDict");

    // conditional execution based on an option being passed
    if (args.optionFound("dict"))
    {
        args.optionReadIfPresent("dict", dictPath);
        Info << "Got an override flag for dictionary path" << endl;
    }
    Info << "Would read dict from " << dictPath << endl;

下面的代码则分别设置了-someSwitch-someInt两个选项的功能。

    // switch option, 出现为1,没有出现则为0
    const bool someConstBool = args.optionFound("someSwitch");
    Info << "Boolean switch set to " << someConstBool << endl;

    // numeric value option - same as string variables really
    label someInt(0);
    args.optionReadIfPresent("someInt", someInt);
    Info << "Integer option value " << someInt << endl;

    Info<< "End\n" << endl;

    return 0;
}

最后,运行

cmdArgsAndOptions someWordIPassed 5. -someSwitch -someInt 2 -dict path_to_nowhere_in_particular

若得到以下输出,恭喜完成本节的学习。

Got argument word someWordIPassed and scalar 5
Would read dict from "./system/defaultDict"
Boolean switch set to 1
Integer option value 2
End

5. 案例文件下载

点击下载

Everything not saved will be lost.
最后更新于 2023-05-06