CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   how to convert string type to scalar type (https://www.cfd-online.com/Forums/openfoam-programming-development/69225-how-convert-string-type-scalar-type.html)

zhajingjing October 15, 2009 22:48

how to convert string type to scalar type
 
Hello everyone,

In a small post-processing program, I have to convert a string into a scalar:
Code:

int main(int argc, char *argv[])
{
    timeSelector::addOptions();
    argList::validArgs.append("fieldName");
    argList::validArgs.append("xValue");
#  include "setRootCase.H"
#  include "createTime.H"
    instantList timeDirs = timeSelector::select0(runTime, args);
#  include "createMesh.H"

    word fieldName(args.additionalArgs()[0]);
  word xValue(args.additionalArgs()[1]);
   
    Info <<xValue<<endl;
   
    ......    // ?
  scalar  x=      // 
get the value from xValue, but xValue is a string---How???

Thanks for any help!

l_r_mcglashan October 16, 2009 04:20

You could use 'strtod' from the cstdlib.

http://www.cplusplus.com/reference/c...stdlib/strtod/

zhajingjing October 16, 2009 07:23

I add the following,It worked.Thank you very much.
Code:

#include <cstdlib>
...
  char* pEnd;
  scalar x=strtod(xValue.c_str(),&pEnd);


Rudiy July 31, 2023 09:57

Convert to double
 
Hello, i got a similar problem.

I am trying to read from values from the thermophysical properties file.

Here is my code:
const fvMesh &PoP99 = V_.db().parent().objectRegistry::lookupObject<fvMe sh>(region2);
const dictionary t1 = PoP99.thisDb().lookupObject<IOdictionary>("thermop hysicalProperties");

const dictionary t2(t1.subDict("mixture"));
const dictionary transpks(t2.subDict("transport"));
const auto sigmaLayer = transpks.lookup("sigmaL");
const auto st = sigmaLayer[0];

double q = 10.0;




Info << st*q <<endl;

obviously this dosent work because st has the datatype Foam::token amd q double.

is there a way to convert from Foam::token to double?

olesen July 31, 2023 10:57

Quote:

Originally Posted by Rudiy (Post 854390)
Hello, i got a similar problem.

I am trying to read from values from the thermophysical properties file.

Here is my code:
const fvMesh &PoP99 = V_.db().parent().objectRegistry::lookupObject<fvMe sh>(region2);
const dictionary t1 = PoP99.thisDb().lookupObject<IOdictionary>("thermop hysicalProperties");

const dictionary t2(t1.subDict("mixture"));
const dictionary transpks(t2.subDict("transport"));
const auto sigmaLayer = transpks.lookup("sigmaL");
const auto st = sigmaLayer[0];

double q = 10.0;


Info << st*q <<endl;

obviously this dosent work because st has the datatype Foam::token amd q double.

is there a way to convert from Foam::token to double?


Not really sure what your "sigmaL" represents. At the moment it simply represents a copy of a tokenList (which is the ITstream that the lookup method returns). If we assume that "sigmaL" is actually supposed to be a list of scalars I would write it like this:
Code:

const fvMesh &PoP99 = V_.db().parent().objectRegistry::lookupObject<fvMe  sh>(region2);
  const dictionary& t1 = PoP99.thisDb().lookupObject<IOdictionary>("thermop  hysicalProperties");

  const dictionary& t2 = t1.subDict("mixture");
  const dictionary& transpks = t2.subDict("transport");
  const scalarList sigmaLayer = transpks.get<scalarList>("sigmaL");
  const scalar st = sigmaLayer[0];

Note that this also avoids making copies of the dictionaries like your original code did.

Rudiy August 1, 2023 06:43

Hello,

Thanks for your code, i manged to get it to work with a similar version.
.get... did not work for me. I used this insted:

scalar sigmaLayer (readScalar(transpks.lookup("sigmaL")));

thanks for the help

olesen August 1, 2023 09:31

Quote:

Originally Posted by Rudiy (Post 854459)
Hello,

Thanks for your code, i manged to get it to work with a similar version.
.get... did not work for me. I used this insted:

scalar sigmaLayer (readScalar(transpks.lookup("sigmaL")));

thanks for the help


Glad you have it working. If you get a newer version, the dictionary get method is preferred since it will also check if there were any trailing tokens.

Rudiy August 2, 2023 07:46

ok good to know, thaks for the advice.

Unfortunately the get() function didnt work. The code compiled without any errors, but when i run the openFoam case i get the following Error:

--> FOAM FATAL IO ERROR: (openfoam-2212)
attempt to read beyond EOF

file: constant/solidR/thermophysicalProperties.electricalProperties.elek transport.sigmaL at line 60.

From virtual Foam::Istream& Foam::ITstream::read(Foam::token&)
in file db/IOstreams/Tstreams/ITstream.C at line 450.

FOAM exiting


All times are GMT -4. The time now is 15:36.