|
[Sponsors] |
March 22, 2016, 10:12 |
Compare word variable to a string
|
#1 |
Member
|
Dear FOAMers,
I am working with a multi-region solver, and I want to read all the patches names from each region from a dictionary. So far I manage to do so, but then I need to compare each patch name with a string to check for a match, but when I use the strcmp() function I get the following error: error: cannot convert ‘const Foam::word’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’ if(strcmp(PatchesNamesList[i], "hell1_to_solid1") == 0) Code:
IOdictionary PatchesDict ( IOobject ( "boundarynames", // dictionary name runTime.constant(), // dict is found in "constant" fluidRegions[i], // registry for the dict IOobject::MUST_READ, // must exist, otherwise failure IOobject::NO_WRITE // dict is only read by the solver ) ); const wordList PatchesNamesList ( PatchesDict.lookup("patchnames") ); //const wordList currentPatchList(PatchesDict.toc()); forAll(PatchesNamesList, i) { if(strcmp(PatchesNamesList[i], "hell1_to_solid1") == 0) { ... Could you please give me your thoughts and help me with this issue ? Thanks a lot! |
|
July 20, 2016, 10:53 |
|
#2 |
New Member
Join Date: Oct 2014
Posts: 4
Rep Power: 12 |
Dear faab,
have you found any solution to convert Foam::word to const char*? Cheers, |
|
July 20, 2016, 13:49 |
|
#3 | |
Member
Jerry
Join Date: Oct 2013
Location: Salt Lake City, UT, USA
Posts: 52
Rep Power: 13 |
I don't know whether you have solved this problem or not. My idea is you since PatchesNamesList[i] returns a word object. You can create another word object by assigning "hell1_to_solid1" as the value of this object. And then compare these two word objects.
Quote:
|
||
July 20, 2016, 13:59 |
|
#4 |
Senior Member
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 18 |
||
July 20, 2016, 14:36 |
|
#5 |
New Member
Join Date: Oct 2014
Posts: 4
Rep Power: 12 |
I've figured it out, thanks though. Instead of a const char* i've created another wordlist.
Thanks for the quick reply |
|
August 3, 2016, 05:03 |
|
#6 |
Member
|
First of all, I'm sorry about the late reply, I only saw the message now.
Regarding the issue itself, yes I was able to solve this by accessing the patches directly through their ID, meaning for each region I iterate through the entire list of patch names (creating first a word list as it was suggested in a previous reply) with every patch having an associated patchID (a variable which is initialized as "label"). After that it's pretty simple because the OF code is well designed and I just had to use the "*.boundaryField()[patchID]" or "*.patch().faceCells()" tools to access the elements which needed to be modified. I hope it makes sense, and thanks anyway for the replies ! Faab |
|
October 9, 2017, 13:18 |
|
#7 | |
Senior Member
Elham
Join Date: Oct 2009
Posts: 184
Rep Power: 17 |
Quote:
How could you create a word list? I want to address to a word but still got the error that the word is not initialised. Do I have to use a list in a separate dictionary? Cheers, Elham |
||
October 9, 2017, 20:10 |
|
#8 |
Senior Member
Charles
Join Date: Aug 2016
Location: Vancouver, Canada
Posts: 151
Rep Power: 10 |
You can convert a string to a word and compare the two words.
Code:
word myPatch("hell1_to_solid1") ; forAll(PatchesNamesList, i) { if(PatchesNamesList[i] == myPatch) { ... |
|
October 9, 2017, 23:50 |
|
#9 |
Senior Member
Elham
Join Date: Oct 2009
Posts: 184
Rep Power: 17 |
||
October 10, 2017, 01:55 |
|
#10 |
Senior Member
Charles
Join Date: Aug 2016
Location: Vancouver, Canada
Posts: 151
Rep Power: 10 |
Can you show your code and show the error message?
Cheers, Charles |
|
October 10, 2017, 03:35 |
|
#11 | |
Senior Member
Elham
Join Date: Oct 2009
Posts: 184
Rep Power: 17 |
Quote:
This is the code : Code:
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::multiphaseMixture::multiphaseMixture ( const volVectorField& U, const surfaceScalarField& phi ) : IOdictionary ( IOobject ( "transportProperties", U.time().constant(), U.db(), IOobject::MUST_READ_IF_MODIFIED, IOobject::NO_WRITE ) ), phases_(lookup("phases"), phase::iNew(U, phi)), mesh_(U.mesh()), U_(U), phi_(phi), rhoPhi_ ( IOobject ( "rhoPhi", mesh_.time().timeName(), mesh_, IOobject::NO_READ, IOobject::NO_WRITE ), mesh_, dimensionedScalar("rhoPhi", dimMass/dimTime, 0.0) ), alphas_ ( IOobject ( "alphas", mesh_.time().timeName(), mesh_, IOobject::NO_READ, IOobject::AUTO_WRITE ), mesh_, dimensionedScalar("alphas", dimless, 0.0), zeroGradientFvPatchScalarField::typeName ), sigmas_(lookup("sigmas")), dimSigma_(1, 0, -2, 0, 0), deltaN_ ( "deltaN", 1e-8/pow(average(mesh_.V()), 1.0/3.0) ) { calcAlphas(); alphas_.write(); } // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // ... Foam::tmp<Foam::volScalarField> Foam::multiphaseMixture::C() const { PtrDictionary<phase>::const_iterator iter = phases_.begin(); tmp<volScalarField> tC = iter()*iter().C(); for (++iter; iter != phases_.end(); ++iter) { tC() += iter()*iter().C(); } return tC; } //Added Foam::tmp<Foam::scalarField> Foam::multiphaseMixture::C(const label patchi) const { PtrDictionary<phase>::const_iterator iter = phases_.begin(); tmp<scalarField> tC = iter().boundaryField()[patchi]*iter().C().value(); for (++iter; iter != phases_.end(); ++iter) { tC() += iter().boundaryField()[patchi]*iter().C().value(); } return tC; } ... Foam::tmp<Foam::volScalarField> Foam::multiphaseMixture::nu() const { return mu()/rho(); } Foam::tmp<Foam::scalarField> Foam::multiphaseMixture::nu(const label patchi) const { return mu(patchi)/rho(patchi); } ... void Foam::multiphaseMixture::correct() {} Foam::tmp<Foam::surfaceVectorField> Foam::multiphaseMixture::nHatfv ( const volScalarField& alpha1, const volScalarField& alpha2 ) const { /* // Cell gradient of alpha volVectorField gradAlpha = alpha2*fvc::grad(alpha1) - alpha1*fvc::grad(alpha2); // Interpolated face-gradient of alpha surfaceVectorField gradAlphaf = fvc::interpolate(gradAlpha); */ surfaceVectorField gradAlphaf ( fvc::interpolate(alpha2)*fvc::interpolate(fvc::grad(alpha1)) - fvc::interpolate(alpha1)*fvc::interpolate(fvc::grad(alpha2)) ); // Face unit interface normal return gradAlphaf/(mag(gradAlphaf) + deltaN_); } Foam::tmp<Foam::surfaceScalarField> Foam::multiphaseMixture::nHatf ( const volScalarField& alpha1, const volScalarField& alpha2 ) const { // Face unit interface normal flux return nHatfv(alpha1, alpha2) & mesh_.Sf(); } // Correction for the boundary condition on the unit normal nHat on // walls to produce the correct contact angle. // The dynamic contact angle is calculated from the component of the // velocity on the direction of the interface, parallel to the wall. void Foam::multiphaseMixture::correctContactAngle ( const phase& alpha1, const phase& alpha2, surfaceVectorField::GeometricBoundaryField& nHatb ) const { const volScalarField::GeometricBoundaryField& gbf = alpha1.boundaryField(); const fvBoundaryMesh& boundary = mesh_.boundary(); forAll(boundary, patchi) { if (isA<alphaContactAngleFvPatchScalarField>(gbf[patchi])) { const alphaContactAngleFvPatchScalarField& acap = refCast<const alphaContactAngleFvPatchScalarField>(gbf[patchi]); vectorField& nHatPatch = nHatb[patchi]; vectorField AfHatPatch ( mesh_.Sf().boundaryField()[patchi] /mesh_.magSf().boundaryField()[patchi] ); alphaContactAngleFvPatchScalarField::thetaPropsTable:: const_iterator tp = acap.thetaProps().find(interfacePair(alpha1, alpha2)); if (tp == acap.thetaProps().end()) { FatalErrorIn ( "multiphaseMixture::correctContactAngle" "(const phase& alpha1, const phase& alpha2, " "fvPatchVectorFieldField& nHatb) const" ) << "Cannot find interface " << interfacePair(alpha1, alpha2) << "\n in table of theta properties for patch " << acap.patch().name() << exit(FatalError); } bool matched = (tp.key().first() == alpha1.name()); scalar theta0 = convertToRad*tp().theta0(matched); scalarField theta(boundary[patchi].size(), theta0); scalar uTheta = tp().uTheta(); // Calculate the dynamic contact angle if required if (uTheta > SMALL) { scalar thetaA = convertToRad*tp().thetaA(matched); scalar thetaR = convertToRad*tp().thetaR(matched); // Calculated the component of the velocity parallel to the wall vectorField Uwall ( U_.boundaryField()[patchi].patchInternalField() - U_.boundaryField()[patchi] ); Uwall -= (AfHatPatch & Uwall)*AfHatPatch; // Find the direction of the interface parallel to the wall vectorField nWall ( nHatPatch - (AfHatPatch & nHatPatch)*AfHatPatch ); // Normalise nWall nWall /= (mag(nWall) + SMALL); // Calculate Uwall resolved normal to the interface parallel to // the interface scalarField uwall(nWall & Uwall); theta += (thetaA - thetaR)*tanh(uwall/uTheta); } // Reset nHatPatch to correspond to the contact angle scalarField a12(nHatPatch & AfHatPatch); scalarField b1(cos(theta)); scalarField b2(nHatPatch.size()); forAll(b2, facei) { b2[facei] = cos(acos(a12[facei]) - theta[facei]); } scalarField det(1.0 - a12*a12); scalarField a((b1 - a12*b2)/det); scalarField b((b2 - a12*b1)/det); nHatPatch = a*AfHatPatch + b*nHatPatch; nHatPatch /= (mag(nHatPatch) + deltaN_.value()); } } } Foam::tmp<Foam::volScalarField> Foam::multiphaseMixture::K ( const phase& alpha1, const phase& alpha2 ) const { tmp<surfaceVectorField> tnHatfv = nHatfv(alpha1, alpha2); correctContactAngle(alpha1, alpha2, tnHatfv().boundaryField()); // Simple expression for curvature return -fvc::div(tnHatfv & mesh_.Sf()); } Foam::tmp<Foam::volScalarField> Foam::multiphaseMixture::nearInterface() const { tmp<volScalarField> tnearInt ( new volScalarField ( IOobject ( "nearInterface", mesh_.time().timeName(), mesh_ ), mesh_, dimensionedScalar("nearInterface", dimless, 0.0) ) ); forAllConstIter(PtrDictionary<phase>, phases_, iter) { tnearInt() = max(tnearInt(), pos(iter() - 0.01)*pos(0.99 - iter())); } return tnearInt; } void Foam::multiphaseMixture::solveAlphas ( const scalar cAlpha ) { static label nSolves=-1; nSolves++; word alphaScheme("div(phi,alpha)"); word alpharScheme("div(phirb,alpha)"); surfaceScalarField phic(mag(phi_/mesh_.magSf())); phic = min(cAlpha*phic, max(phic)); PtrList<surfaceScalarField> phiAlphaCorrs(phases_.size()); int phasei = 0; forAllIter(PtrDictionary<phase>, phases_, iter) { phase& alpha = iter(); phiAlphaCorrs.set ( phasei, new surfaceScalarField ( "phi" + alpha.name() + "Corr", fvc::flux ( phi_, alpha, alphaScheme ) ) ); surfaceScalarField& phiAlphaCorr = phiAlphaCorrs[phasei]; forAllIter(PtrDictionary<phase>, phases_, iter2) { phase& alpha2 = iter2(); if (&alpha2 == &alpha) continue; surfaceScalarField phir(phic*nHatf(alpha, alpha2)); phiAlphaCorr += fvc::flux ( -fvc::flux(-phir, alpha2, alpharScheme), alpha, alpharScheme ); } const word& phaseName = word("air"); if (phases == air) { MULES::limit ( 1.0/mesh_.time().deltaT().value(), geometricOneField(), alpha, phi_, phiAlphaCorr, zeroField(), zeroField(), 1, 0, 3, true ); } const word& phaseName = word("vapor"); else if (phases == vapor) { if (phaseChange){ Pair<tmp<volScalarField> > vDotAlphav = twoPhaseProperties->vDotAlphav(); const volScalarField& vDotcAlphav = vDotAlphav[0]();//+ const volScalarField& vDotvAlphav = vDotAlphav[1]();//- volScalarField Spv ( IOobject ( "Spv", runTime.timeName(), mesh ), vDotvAlphal - vDotcAlphal ); volScalarField Suv ( IOobject ( "Suv", runTime.timeName(), mesh ), // Divergence term is handled explicitly to be // consistent with the explicit transport solution divU*alpha1 + vDotcAlphal ); MULES::limit ( 1.0/mesh_.time().deltaT().value(), geometricOneField(), alpha, phi_, phiAlphaCorr, Spv, Suv, 1, 0, 3, true ); } else{ Pair<tmp<volScalarField> > vDotAlphal = twoPhaseProperties->vDotAlphal(); const volScalarField& vDotcAlphal = vDotAlphal[0]();//+ const volScalarField& vDotvAlphal = vDotAlphal[1]();//- volScalarField Spl ( IOobject ( "Spl", runTime.timeName(), mesh ), vDotvAlphal - vDotcAlphal ); volScalarField Sul ( IOobject ( "Sul", runTime.timeName(), mesh ), // Divergence term is handled explicitly to be // consistent with the explicit transport solution divU*alpha1 + vDotcAlphal ); MULES::limit ( 1.0/mesh_.time().deltaT().value(), geometricOneField(), alpha, phi_, phiAlphaCorr, Spl, Sul, 1, 0, 3, true ); } } else { MULES::limit ( 1.0/mesh_.time().deltaT().value(), geometricOneField(), alpha, phi_, phiAlphaCorr, Spl, Sul, 1, 0, 3, true ); } phasei++; } MULES::limitSum(phiAlphaCorrs); rhoPhi_ = dimensionedScalar("0", dimensionSet(1, 0, -1, 0, 0), 0); volScalarField sumAlpha ( IOobject ( "sumAlpha", mesh_.time().timeName(), mesh_ ), mesh_, dimensionedScalar("sumAlpha", dimless, 0) ); phasei = 0; forAllIter(PtrDictionary<phase>, phases_, iter) { phase& alpha = iter(); surfaceScalarField& phiAlpha = phiAlphaCorrs[phasei]; phiAlpha += upwind<scalar>(mesh_, phi_).flux(alpha); if (phases == air) { MULES::explicitSolve ( geometricOneField(), alpha, phiAlpha, zeroField(), zeroField() ); } else if ( phases == vapor) { if (phaseChange){ MULES::explicitSolve ( geometricOneField(), alpha, phiAlpha, Spv, Suv ); } else { MULES::explicitSolve ( geometricOneField(), alpha, phiAlpha, zeroField(), zeroField() ); } } else { if (phaseChange){ MULES::explicitSolve ( geometricOneField(), alpha, phiAlpha, Spl, Sul ); } else { MULES::explicitSolve ( geometricOneField(), alpha, phiAlpha, zeroField(), zeroField() ); } } ... // ************************************************************************* // Code:
multiphaseMixture.C: In constructor ‘Foam::multiphaseMixture::multiphaseMixture(const volVectorField&, const surfaceScalarField&)’: multiphaseMixture.C:67:1: error: uninitialized reference member ‘Foam::multiphaseMixture::air’ [-fpermissive] Foam::multiphaseMixture::multiphaseMixture ^ multiphaseMixture.C: In member function ‘void Foam::multiphaseMixture::solveAlphas(Foam::scalar)’: multiphaseMixture.C:670:20: error: no match for ‘operator==’ (operand types are ‘<unresolved overloaded function type>’ and ‘const Foam::PtrDictionary<Foam::phase>’) if (phases == air) ^ multiphaseMixture.C:670:20: note: candidates are: In file included from /home/elham/OpenFOAM/OpenFOAM-2.3.0/src/OpenFOAM/lnInclude/lduAddressing.H:101:0, from /home/elham/OpenFOAM/OpenFOAM-2.3.0/src/OpenFOAM/lnInclude/lduMesh.H:36, from /home/elham/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/fvMesh.H:51, from /home/elham/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/volMesh.H:36, from /home/elham/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/volFields.H:38, from lnInclude/phase.H:40, from multiphaseMixture.H:47, from multiphaseMixture.C:26: /home/elham/OpenFOAM/OpenFOAM-2.3.0/src/OpenFOAM/lnInclude/Tuple2.H:146:13: note: template<class Type1, class Type2> bool Foam::operator==(const Foam::Tuple2<Type1, Type2>&, const Foam::Tuple2<Type1, Type2>&) inline bool operator== ^ /home/elham/OpenFOAM/OpenFOAM-2.3.0/src/OpenFOAM/lnInclude/Tuple2.H:146:13: note: template argument deduction/substitution failed: multiphaseMixture.C:670:23: note: ‘const Foam::PtrDictionary<Foam::phase>& (Foam::multiphaseMixture::*)()const’ is not derived from ‘const Foam::Tuple2<Type1, Type2>’ if (phases == air) ^ multiphaseMixture.C:670:23: note: could not resolve address from overloaded function ‘((Foam::multiphaseMixture*)this)->Foam::multiphaseMixture::phases’ In file included from /home/elham/OpenFOAM/OpenFOAM-2.3.0/src/OpenFOAM/lnInclude/septernion.H:196:0, Thanks in advance. Cheers, Elham |
||
October 10, 2017, 13:42 |
|
#12 |
Senior Member
Charles
Join Date: Aug 2016
Location: Vancouver, Canada
Posts: 151
Rep Power: 10 |
Hi Elham,
The error is not match for the operator == but not initialization issue. How do you declare "phases" and is it word? Cheers, Charles |
|
October 11, 2017, 05:07 |
|
#13 |
Senior Member
Elham
Join Date: Oct 2009
Posts: 184
Rep Power: 17 |
||
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Run time Selection Mechanism - Some help required to understand | jaswi | OpenFOAM Programming & Development | 3 | October 29, 2015 14:42 |
Variable name for heat flux | peterle | CFX | 4 | February 13, 2014 03:21 |
Clearing a string variable | eshimshi | Fluent UDF and Scheme Programming | 3 | July 20, 2010 20:32 |
how to tell the word length of a real variable? | Jiaying Xu | Siemens | 1 | January 30, 2003 21:41 |
what is LaTeX ?? | yfyap | Main CFD Forum | 28 | September 6, 2002 13:01 |