CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

how to convert string type to scalar type

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 15, 2009, 23:48
Smile how to convert string type to scalar type
  #1
Member
 
jingjing
Join Date: Mar 2009
Location: shanghai,China
Posts: 30
Rep Power: 17
zhajingjing is on a distinguished road
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!
zhajingjing is offline   Reply With Quote

Old   October 16, 2009, 05:20
Default
  #2
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 23
l_r_mcglashan will become famous soon enough
You could use 'strtod' from the cstdlib.

http://www.cplusplus.com/reference/c...stdlib/strtod/
__________________
Laurence R. McGlashan :: Website
l_r_mcglashan is offline   Reply With Quote

Old   October 16, 2009, 08:23
Default
  #3
Member
 
jingjing
Join Date: Mar 2009
Location: shanghai,China
Posts: 30
Rep Power: 17
zhajingjing is on a distinguished road
I add the following,It worked.Thank you very much.
Code:
 #include <cstdlib>
...
   char* pEnd;
   scalar x=strtod(xValue.c_str(),&pEnd);
zhajingjing is offline   Reply With Quote

Old   July 31, 2023, 10:57
Default Convert to double
  #4
New Member
 
Peter Vorauer
Join Date: Jun 2023
Posts: 5
Rep Power: 2
Rudiy is on a distinguished road
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?
Rudiy is offline   Reply With Quote

Old   July 31, 2023, 11:57
Default
  #5
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,677
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by Rudiy View Post
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.
olesen is offline   Reply With Quote

Old   August 1, 2023, 07:43
Default
  #6
New Member
 
Peter Vorauer
Join Date: Jun 2023
Posts: 5
Rep Power: 2
Rudiy is on a distinguished road
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
Rudiy is offline   Reply With Quote

Old   August 1, 2023, 10:31
Default
  #7
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,677
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by Rudiy View Post
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.
olesen is offline   Reply With Quote

Old   August 2, 2023, 08:46
Default
  #8
New Member
 
Peter Vorauer
Join Date: Jun 2023
Posts: 5
Rep Power: 2
Rudiy is on a distinguished road
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
Rudiy is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pressure instability with rhoSimpleFoam daniel_mills OpenFOAM Running, Solving & CFD 44 February 17, 2011 18:08
Flow Around a Cylinder ronaldo OpenFOAM 5 September 18, 2009 09:13
LiftDrag tool nuovodna OpenFOAM Running, Solving & CFD 45 September 2, 2009 18:56
Problem with compile the setParabolicInlet ivanyao OpenFOAM Running, Solving & CFD 6 September 5, 2008 21:50
[Commercial meshers] Trimmed cell and embedded refinement mesh conversion issues michele OpenFOAM Meshing & Mesh Conversion 2 July 15, 2005 05:15


All times are GMT -4. The time now is 03:35.