ENH: support find via keyType for coordinateSystems

- findAll() method returns a labelList of all matching names
- find() method returns the index to the first matching name

  For example, use a regex to specify alternative coordinate systems
  in porousZones
  (
      "cat1?(Back|Front)*"
      {
          coordinateSystem    "(cat1|system_10)";
          porosity    0.781;
          ...
      }
  )
This commit is contained in:
Mark Olesen
2010-07-21 11:11:42 +02:00
parent 6a03ebdd24
commit 82dec4824e
4 changed files with 64 additions and 24 deletions

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -136,37 +136,36 @@ Foam::coordinateSystem::coordinateSystem
{ {
const entry* entryPtr = dict.lookupEntryPtr(typeName_(), false, false); const entry* entryPtr = dict.lookupEntryPtr(typeName_(), false, false);
// a simple entry is a lookup into global coordinateSystems // non-dictionary entry is a lookup into global coordinateSystems
if (entryPtr && !entryPtr->isDict()) if (entryPtr && !entryPtr->isDict())
{ {
word csName; keyType key(entryPtr->stream());
entryPtr->stream() >> csName;
const coordinateSystems& csLst = coordinateSystems::New(obr); const coordinateSystems& lst = coordinateSystems::New(obr);
const label id = lst.find(key);
label csId = csLst.find(csName);
if (debug) if (debug)
{ {
Info<< "coordinateSystem::coordinateSystem" Info<< "coordinateSystem::coordinateSystem"
"(const dictionary&, const objectRegistry&):" "(const dictionary&, const objectRegistry&):"
<< nl << "using global coordinate system: " << nl << "using global coordinate system: "
<< csName << "=" << csId << endl; << key << "=" << id << endl;
} }
if (csId < 0) if (id < 0)
{ {
FatalErrorIn FatalErrorIn
( (
"coordinateSystem::coordinateSystem" "coordinateSystem::coordinateSystem"
"(const dictionary&, const objectRegistry&)" "(const dictionary&, const objectRegistry&)"
) << "could not find coordinate system: " << csName << nl ) << "could not find coordinate system: " << key << nl
<< "available coordinate systems: " << csLst.toc() << nl << nl << "available coordinate systems: " << lst.toc() << nl << nl
<< exit(FatalError); << exit(FatalError);
} }
// copy coordinateSystem, but assign the name as the typeName // copy coordinateSystem, but assign the name as the typeName
// to avoid strange things in writeDict() // to avoid strange things in writeDict()
operator=(csLst[csId]); operator=(lst[id]);
name_ = typeName_(); name_ = typeName_();
} }
else else

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -26,6 +26,7 @@ License
#include "coordinateSystems.H" #include "coordinateSystems.H"
#include "IOPtrList.H" #include "IOPtrList.H"
#include "Time.H" #include "Time.H"
#include "stringListOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -97,23 +98,60 @@ const Foam::coordinateSystems& Foam::coordinateSystems::New
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::label Foam::coordinateSystems::find(const word& keyword) const Foam::label Foam::coordinateSystems::find(const keyType& key) const
{ {
if (key.isPattern())
{
labelList allFound = findAll(key);
// return first element
if (!allFound.empty())
{
return allFound[0];
}
}
else
{
forAll(*this, i) forAll(*this, i)
{ {
if (keyword == operator[](i).name()) if (key == operator[](i).name())
{ {
return i; return i;
} }
} }
}
return -1; return -1;
} }
bool Foam::coordinateSystems::found(const word& keyword) const Foam::labelList Foam::coordinateSystems::findAll(const keyType& key) const
{ {
return find(keyword) >= 0; labelList allFound;
if (key.isPattern())
{
allFound = findStrings(key, toc());
}
else
{
allFound.setSize(size());
label nFound = 0;
forAll(*this, i)
{
if (key == operator[](i).name())
{
allFound[nFound++] = i;
}
}
allFound.setSize(nFound);
}
return allFound;
}
bool Foam::coordinateSystems::found(const keyType& key) const
{
return find(key) >= 0;
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -97,11 +97,14 @@ public:
// Member Functions // Member Functions
//- Find and return index for a given keyword, returns -1 if not found //- Find and return index for the first match, returns -1 if not found
label find(const word& key) const; label find(const keyType& key) const;
//- Search for given keyword //- Find and return indices for all matches
bool found(const word& keyword) const; labelList findAll(const keyType& key) const;
//- Search for given key
bool found(const keyType& key) const;
//- Return the table of contents (list of all keywords) //- Return the table of contents (list of all keywords)
wordList toc() const; wordList toc() const;