CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM (
-   -   programming issue (

Schag July 27, 2009 04:51

[Solve]programming issue

I think it's quite trivial, but I spent time on finding how to get a float as argument for an application.

It's a little application dealing with boundary conditions, let's name it JulienFoam

I want to be able to use it as:
JulienFoam <Boundary> -numericalarg <float>

for the moment, I have that:

int main(int argc, char *argv[])
float numericalarg;
argList::validOptions.insert("numericalarg",numeri calarg);

word patchName(args.args()[1]);



and I don't find how to get my numericalarg as a float, it seems that argList only gets strings.

If someone can give me any piece of advice, I think it would be very usefull for my future using, not just for that program.



henrik July 27, 2009 05:01


yes, arguments are passed only as strings.

Have a look into fluentMeshToFoam.L where it says:

    scalar scaleFactor = 1.0;
    if (args.options().found("scale"))
        scaleFactor = atof(args.options()["scale"].c_str());


Schag July 27, 2009 05:27

Perfect, that is exactly what I was looking for! I looked over other applications but not that one.

Thanks a lot!

olesen July 28, 2009 02:31

Just for completeness (to followup on what Henrik mentioned), you might note that readScalar operates on an Istream, and IStringStream can be used to create an Istream from a string.
Thus the following would also work:

if (args.options().found("scale"))
    scaleFactor = readScalar(IStringStream(args.options()["scale"])());

The extra '()' on the IStringStream constructor casts away constness.

Of course, both solutions look fairly messy.

olesen July 28, 2009 02:40

In OpenFOAM-1.6, the same thing becomes much more convenient:

    scalar scaleFactor = 1.0;
    args.optionReadIfPresent("scale", scaleFactor);

All times are GMT -4. The time now is 11:55.