|
[Sponsors] |
October 15, 2009, 23:48 |
how to convert string type to scalar type
|
#1 |
Member
jingjing
Join Date: Mar 2009
Location: shanghai,China
Posts: 30
Rep Power: 17 |
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??? |
|
October 16, 2009, 08:23 |
|
#3 |
Member
jingjing
Join Date: Mar 2009
Location: shanghai,China
Posts: 30
Rep Power: 17 |
I add the following,It worked.Thank you very much.
Code:
#include <cstdlib> ... char* pEnd; scalar x=strtod(xValue.c_str(),&pEnd); |
|
July 31, 2023, 10:57 |
Convert to double
|
#4 |
New Member
Peter Vorauer
Join Date: Jun 2023
Posts: 5
Rep Power: 3 |
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? |
|
July 31, 2023, 11:57 |
|
#5 | |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40 |
Quote:
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]; |
||
August 1, 2023, 07:43 |
|
#6 |
New Member
Peter Vorauer
Join Date: Jun 2023
Posts: 5
Rep Power: 3 |
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 |
|
August 1, 2023, 10:31 |
|
#7 | |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40 |
Quote:
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. |
||
August 2, 2023, 08:46 |
|
#8 |
New Member
Peter Vorauer
Join Date: Jun 2023
Posts: 5
Rep Power: 3 |
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 |
|
|
|
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 |