Commit 55e5a777 authored by shunbo's avatar shunbo
Browse files

initial commit

parents
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
Application
Test-dictionaryCopy
Description
Test copying a dictionary with filtering
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "IOobject.H"
#include "IOstreams.H"
#include "IFstream.H"
#include "dictionaryContent.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noParallel();
argList::addArgument("dict .. dictN");
argList args(argc, argv, false, true);
const wordRes allow;
const wordRes deny
({
wordRe("boundary.*", wordRe::REGEX)
});
Info<< nl
<< "allow: " << flatOutput(allow) << nl
<< "deny: " << flatOutput(deny) << nl << nl;
if (args.size() <= 1)
{
const string dictFile = "testDictCopy";
IFstream is(dictFile);
dictionary input(is);
dictionary copied
(
dictionaryContent::copyDict
(
input,
allow,
deny
)
);
IOobject::writeDivider(Info);
input.writeEntry("input", Info);
copied.writeEntry("copied", Info);
}
for (label argi=1; argi < args.size(); ++argi)
{
const auto dictFile = args.get<fileName>(argi);
IFstream is(dictFile);
dictionary input(is);
dictionary copied
(
dictionaryContent::copyDict
(
input,
allow,
deny
)
);
IOobject::writeDivider(Info);
input.writeEntry("input", Info);
copied.writeEntry("copied", Info);
}
IOobject::writeEndDivider(Info);
return 0;
}
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: v2112 |
| \\ / A nd | Website: www.openfoam.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object testDictCopy;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [ 1 2 -2 0 0 0 0 ];
internalField uniform 1;
boundaryField
{
inlet_1 { type patch; }
inlet_2 { type patch; }
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Test-dictionaryTokens.C
dictionaryTokens.C
EXE = $(FOAM_USER_APPBIN)/Test-dictionaryTokens
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
printDictionary
Description
Test dictionaryTokens
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "IOstreams.H"
#include "IOobject.H"
#include "IFstream.H"
#include "dictionaryTokens.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noBanner();
argList::noParallel();
argList::noFunctionObjects();
argList::addBoolOption("info", "report token info");
argList::addBoolOption("value", "report token value");
argList::addArgument("dict .. dictN");
argList args(argc, argv, false, true);
const bool optInfo = args.found("info");
const bool optValue = args.found("value");
for (label argi=1; argi < args.size(); ++argi)
{
IFstream is(args.get<fileName>(argi));
dictionary dict(is);
dictionaryTokens dictTokens(dict);
while (dictTokens.good())
{
if (optInfo)
{
// Token info
Info<< (*dictTokens).info() << nl;
}
else if (optValue)
{
// Token value
Info<< *dictTokens << nl;
}
else
{
// Token type
Info<< (*dictTokens).name() << nl;
}
++dictTokens;
}
Info<< nl;
}
return 0;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "dictionaryTokens.H"
#include "IOstream.H"
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::token Foam::dictionaryTokens::keywordToken(const entry& e)
{
const keyType& k = e.keyword();
if (k.empty())
{
return token();
}
if (k.isPattern())
{
return token(static_cast<string>(k)); // quoted
}
else
{
return token(static_cast<word>(k)); // unquoted
}
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
bool Foam::dictionaryTokens::setIterator() const
{
primIter_.reset(nullptr);
dictIter_.reset(nullptr);
if (entryIter_ != dict_.cend())
{
const entry& base = *entryIter_;
{
const auto* eptr = isA<primitiveEntry>(base);
if (eptr)
{
primIter_.reset
(
new dictionaryTokens::primitive_iterator(*eptr)
);
return true;
}
}
// NB: Must check for isA<dictionaryListEntry> before checking
// for isA<dictionaryEntry> !
{
const auto* eptr = isA<dictionaryListEntry>(base);
if (eptr)
{
dictIter_.reset
(
new dictionaryTokens::dictionary_iterator(*eptr)
);
return true;
}
}
{
const auto* eptr = isA<dictionaryEntry>(base);
if (eptr)
{
dictIter_.reset
(
new dictionaryTokens::dictionary_iterator(*eptr)
);
return true;
}
}
}
return false;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::dictionaryTokens::dictionaryTokens(const dictionary& dict)
:
dict_(dict),
entryIter_(dict_.cbegin()),
primIter_(nullptr),
dictIter_(nullptr)
{
rewind();
}
Foam::dictionaryTokens::primitive_iterator::primitive_iterator
(
const primitiveEntry& e
)
:
tokensPtr_(&(static_cast<const tokenList&>(e))),
key_(dictionaryTokens::keywordToken(e)),
end_(token::punctuationToken::END_STATEMENT),
pos_((key_.good() ? -1 : 0))
{}
Foam::dictionaryTokens::dictionary_iterator::dictionary_iterator
(
const dictionaryEntry& e
)
:
key_(dictionaryTokens::keywordToken(e)),
lbrace_(token::punctuationToken::BEGIN_BLOCK),
rbrace_(token::punctuationToken::END_BLOCK),
state_(key_.good() ? states::KEY : states::OPEN),
dictTokens_(e.dict())
{}
Foam::dictionaryTokens::dictionary_iterator::dictionary_iterator
(
const dictionaryListEntry& e
)
:
key_(e.size()),
lbrace_(token::punctuationToken::BEGIN_LIST),
rbrace_(token::punctuationToken::END_LIST),
state_(states::KEY),
dictTokens_(e.dict())
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::dictionaryTokens::good() const
{
return
(
entryIter_ != dict_.cend()
&&
(
(primIter_ && primIter_->good())
|| (dictIter_ && dictIter_->good())
)
);
}
bool Foam::dictionaryTokens::primitive_iterator::good() const
{
return (tokensPtr_ && pos_ <= tokensPtr_->size());
}
bool Foam::dictionaryTokens::dictionary_iterator::good() const
{
return (state_ != states::END);
}
const Foam::token& Foam::dictionaryTokens::operator*() const
{
if (good())
{
if (primIter_) return *(*primIter_);
if (dictIter_) return *(*dictIter_);
}
return token::undefinedToken;
}
const Foam::token&
Foam::dictionaryTokens::primitive_iterator::operator*() const
{
if (good())
{
if (pos_ == -1)
{
return key_;
}
else if (pos_ >= tokensPtr_->size())
{
return end_; // The trailing ';'
}
return tokensPtr_->operator[](pos_);
}
return token::undefinedToken;
}
const Foam::token&
Foam::dictionaryTokens::dictionary_iterator::operator*() const
{
if (good())
{
if (state_ == states::KEY)
{
return key_; // keyword
}
if (state_ == states::OPEN)
{
return lbrace_; // Opening '{'
}
if (state_ == states::CONTENT)
{
return *(dictTokens_);
}
if (state_ == states::CLOSE)
{
return rbrace_; // Closing '}'
}
}
return token::undefinedToken;
}
bool Foam::dictionaryTokens::operator++()
{
bool ok = good();
if (ok)
{
if (primIter_) ok = ++(*primIter_);
if (dictIter_) ok = ++(*dictIter_);
if (!ok)
{
++entryIter_; // Next entry
setIterator();
}
}
return ok;
}
bool Foam::dictionaryTokens::primitive_iterator::operator++()
{
// Advance good iterators.
//
// Going beyond trailing ';' makes it into an end iterator
if (tokensPtr_ && (++pos_ > tokensPtr_->size()))
{
tokensPtr_ = nullptr;
return false;
}
return this->good();
}
bool Foam::dictionaryTokens::dictionary_iterator::operator++()
{
if
(
state_ == states::KEY
|| state_ == states::OPEN
|| state_ == states::CLOSE
)
{
++state_;
}
else if (state_ == states::CONTENT && !(++dictTokens_))
{
++state_;
}
return good();
}
void Foam::dictionaryTokens::rewind()
{
entryIter_ = dict_.cbegin();
setIterator();
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::dictionaryTokens
Description
Provides a stream of tokens from a dictionary.
This can be used to return a stream of tokens from a dictionary
without overhead or needing to reparse information.
For example,
\code
OPstream os = ...;
dictionaryTokens toks(dict);
while (toks.good())
{
os.write(*toks);
++toks;
}
\endcode
Or alternatively,
\code
dictionaryTokens toks(dict);
while (toks.good())
{
os << *toks << nl;
++toks;
}
\endcode
SourceFiles
dictionaryTokens.C
\*---------------------------------------------------------------------------*/
#ifndef dictionaryTokens_H
#define dictionaryTokens_H
#include "dictionary.H"
#include "primitiveEntry.H"
#include "dictionaryEntry.H"
#include "dictionaryListEntry.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declarations
class dictionaryTokens;
/*---------------------------------------------------------------------------*\
Class dictionaryTokens Declaration
\*---------------------------------------------------------------------------*/
class dictionaryTokens
{
public:
// Forward declarations
class primitive_iterator;
class dictionary_iterator;
private:
// Private Member Data
//- Reference to the dictionary being streamed.
const dictionary& dict_;
//- The current entry iterator
IDLList<entry>::const_iterator entryIter_;
//- The current entry iterator for primitiveEntry types
mutable autoPtr<primitive_iterator> primIter_;
//- The current entry iterator for dictionaryEntry and
//- dictionaryListEntry types
mutable autoPtr<dictionary_iterator> dictIter_;
//- Set/unset primitive and dictionary when changing to next entry
bool setIterator() const;
// Private Member Functions
//- No copy construct
dictionaryTokens(const dictionaryTokens&) = delete;
//- No copy assignment
void operator=(const dictionaryTokens&) = delete;
public:
// Static Member Functions
//- The entry keyword as word or string token
static token keywordToken(const entry& e);
// Constructors
//- Construct from reference to dictionary to be streamed
dictionaryTokens(const dictionary& dict);
// Member Functions
//- True if the token stream is in a valid state
bool good() const;
//- The current token, or undefined if the stream is in an invalid
//- invalid state.
const token& operator*() const;
//- Advance to the next token and return the updated stream stream.
bool operator++();
//- Reset to beginning
void rewind();
};
/*---------------------------------------------------------------------------*\
Class dictionaryTokens::primitive_iterator Declaration
\*---------------------------------------------------------------------------*/
//- An iterator for a primitiveEntry
//
// The token stream output has the form
//
// \verbatim
// keyword content tokens ';'
// \endverbatim
//
class dictionaryTokens::primitive_iterator
{
// Private Member Data
//- Reference to the tokenList being streamed.
const tokenList* tokensPtr_;
//- The keyword as a token (string, word or undefined)
const token key_;
//- The closing ';' as a token
const token end_;
//- The current position within the tokenList
label pos_;
// Private Member Functions
//- No copy construct
primitive_iterator(const primitive_iterator&) = delete;
//- No copy assignment
void operator=(const primitive_iterator&) = delete;
public:
// Constructors
//- Construct from reference to primitiveEntry
primitive_iterator(const primitiveEntry& e);
// Member Functions
//- True if the entry has keyword or tokens and has not indexed beyond
//- the final trailing ';'
bool good() const;
//- The current token, or undefined if the stream is invalid.
const token& operator*() const;
//- Advance to the next token and return the updated stream stream.
bool operator++();
};
/*---------------------------------------------------------------------------*\
Class dictionaryTokens::dictionary_iterator Declaration
\*---------------------------------------------------------------------------*/
//- An iterator for a dictionaryEntry and dictionaryListEntry
//
// The token stream output has the form
//
// \verbatim
// keyword '{' content '}'
// \endverbatim
//
// or for the dictionaryListEntry the form
//
// \verbatim
// size '(' content ')'
// \endverbatim
//
class dictionaryTokens::dictionary_iterator
{
// Private Member Data
//- The possible output states
enum states { KEY=0, OPEN, CONTENT, CLOSE, END };
//- The keyword or the size (dictionaryListEntry) as a token
const token key_;
//- The opening brace '{' or bracket '('
const token lbrace_;
//- The closing brace ')' or bracket ')'
const token rbrace_;
//- The current output state
int state_;
//- A streamer for the dictionary content
dictionaryTokens dictTokens_;
// Private Member Functions
//- No copy construct
dictionary_iterator(const dictionary_iterator&) = delete;
//- No copy assignment
void operator=(const dictionary_iterator&) = delete;
public:
// Constructors
//- Construct from reference to dictionaryEntry
dictionary_iterator(const dictionaryEntry& e);
//- Construct from reference to dictionaryListEntry
dictionary_iterator(const dictionaryListEntry& e);
// Member Functions
//- In a valid state
bool good() const;
//- The current token, or undefined if the stream is invalid.
const token& operator*() const;
//- Advance to the next token and return the updated stream stream.
bool operator++();
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
#endif
// ************************************************************************* //
Test-dimField.C
EXE = $(FOAM_USER_APPBIN)/Test-dimField
EXE_INC = \
-I$(LIB_SRC)/finiteVolume/lnInclude \
-I$(LIB_SRC)/meshTools/lnInclude
EXE_LIBS = \
-lfiniteVolume \
-lmeshTools
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-dimField
Description
Simple tests for DimensionedField
\*---------------------------------------------------------------------------*/
#include "fvCFD.H"
#include "GeometricFields.H"
// #undef TEST_UINT8_FIELD
#ifdef TEST_UINT8_FIELD
namespace Foam
{
// Something like an internal state field. Probably only dimensionless
typedef DimensionedField<uint8_t, volMesh> dimUint8Field;
defineTemplate2TypeNameAndDebug(dimUint8Field, 0);
} // End namespace Foam
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
#include "setRootCase.H"
#include "createTime.H"
#include "createMesh.H"
{
Info<< "Tensor field\n" << endl;
DimensionedField<tensor, volMesh> tensorfld
(
IOobject
(
"tensor",
runTime.timeName(),
mesh,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh,
dimensioned<tensor>(dimless, tensor(1,2,3,4,5,6,7,8,9))
);
Info().beginBlock("transformed")
<< tensorfld.T() << nl;
Info().endBlock();
}
#ifdef TEST_UINT8_FIELD
{
Info<< "uint8 field\n" << endl;
DimensionedField<uint8_t, volMesh> statefld
(
IOobject
(
"state",
runTime.timeName(),
mesh,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh,
dimensioned<uint8_t>(dimless, uint8_t{100})
);
Info().beginBlock("stateField")
<< statefld << nl;
Info().endBlock();
}
#endif
Info<< "\nEnd\n" << nl;
return 0;
}
// ************************************************************************* //
Test-dimensionSet.C
EXE = $(FOAM_USER_APPBIN)/Test-dimensionSet
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
Print values of predefined dimensionSets, and some other tests
\*---------------------------------------------------------------------------*/
#include "dimensionSet.H"
#include "dimensionedScalar.H"
#include "IOmanip.H"
#include <tuple>
using namespace Foam;
// Macros to stringify macro contents.
#define STRINGIFY(content) #content
#define STRING_QUOTE(input) STRINGIFY(input)
#define PRINT_DIMSET(arg) \
Info<< STRING_QUOTE(arg) << " " << arg << nl
bool hadDimensionError
(
const std::tuple<bool, dimensionSet, dimensionSet>& input,
bool dimsOk,
std::string errMsg
)
{
if (dimsOk)
{
if (std::get<0>(input))
{
Info<< "(pass) dimension check ok ";
}
else
{
Info<< "(fail) unexpected success for dimension check ";
}
Info<< std::get<1>(input) << " == " << std::get<2>(input) << nl;
}
else
{
if (std::get<0>(input))
{
Info<< "(fail) unexpected";
}
else
{
Info<< "(pass) expected";
}
Info<< " failure" << nl << errMsg.c_str() << nl;
}
return (std::get<0>(input) != dimsOk);
}
unsigned checkDimensions
(
std::initializer_list
<
std::tuple<bool, dimensionSet, dimensionSet>
> tests
)
{
Info<< nl << "Verify dimension checks" << nl << nl;
unsigned nFail = 0;
std::string errMsg;
// Expect some failures
const bool oldThrowingError = FatalError.throwing(true);
for
(
const std::tuple<bool, dimensionSet, dimensionSet>& test
: tests
)
{
const bool expected = std::get<0>(test);
const dimensionSet& a = std::get<1>(test);
const dimensionSet& b = std::get<2>(test);
bool dimsOk = false;
try
{
// min(a, b);
clip(a, b);
dimsOk = true;
}
catch (const Foam::error& err)
{
errMsg = err.message();
}
if (expected != dimsOk)
{
++nFail;
}
hadDimensionError(test, dimsOk, errMsg);
}
FatalError.throwing(oldThrowingError);
return nFail;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<< "Predefined dimensionSets" << nl << nl;
PRINT_DIMSET(dimless);
PRINT_DIMSET(dimMass);
PRINT_DIMSET(dimLength);
PRINT_DIMSET(dimTime);
PRINT_DIMSET(dimTemperature);
PRINT_DIMSET(dimMoles);
PRINT_DIMSET(dimCurrent);
PRINT_DIMSET(dimLuminousIntensity);
PRINT_DIMSET(dimArea);
PRINT_DIMSET(dimVolume);
PRINT_DIMSET(dimVol);
PRINT_DIMSET(dimVelocity);
PRINT_DIMSET(dimAcceleration);
PRINT_DIMSET(dimDensity);
PRINT_DIMSET(dimForce);
PRINT_DIMSET(dimEnergy);
PRINT_DIMSET(dimPower);
PRINT_DIMSET(dimPressure);
PRINT_DIMSET(dimCompressibility);
PRINT_DIMSET(dimGasConstant);
PRINT_DIMSET(dimSpecificHeatCapacity);
PRINT_DIMSET(dimViscosity);
PRINT_DIMSET(dimDynamicViscosity);
Info<< nl << "Operators" << nl << nl;
// Operators
{
Info<< "dimLength/dimTime " << (dimLength/dimTime) << nl;
Info<< "1/dimTime " << (dimless/dimTime) << nl;
Info<< "-dimTime " << (-dimTime) << nl;
Info<< "~dimTime " << (~dimTime) << nl;
Info<< "sqr(dimVelocity) " << sqr(dimVelocity) << nl;
Info<< "pow2(dimVelocity) " << pow2(dimVelocity) << nl;
Info<< "sqrt(dimVelocity) " << sqrt(dimVelocity) << nl;
Info<< "pow025(dimVelocity) " << pow025(dimVelocity) << nl;
Info<< "sqrt(sqrt(dimVelocity)) " << sqrt(sqrt(dimVelocity)) << nl;
}
unsigned nFail = 0;
nFail += checkDimensions
({
{ true, dimless, dimless },
{ false, dimless, dimPressure }
});
if (nFail)
{
Info<< nl << "failed " << nFail << " tests" << nl;
return 1;
}
Info<< nl << "End\n" << endl;
return 0;
}
// ************************************************************************* //
Test-dimensionedType.C
EXE = $(FOAM_USER_APPBIN)/Test-dimensionedType
/* EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude */
/* EXE_LIBS = -lfiniteVolume */
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2013 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "dictionary.H"
#include "primitiveEntry.H"
#include "dimensionedScalar.H"
#include "dimensionedTensor.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
dimensionedTensor dt("dt", dimLength, tensor(0, 1, 2, 3, 4, 5, 6, 7, 8));
Info<< "dt.component(tensor::XX): " << dt.component(tensor::XX) << endl;
dimensionedScalar ds("ds", dimTime, 1.0);
Info<< "ds*dt dt*ds: " << ds*dt << " " << dt*ds << endl;
dimensionedTensor dt2("dt2", dimLength, tensor(1, 1, 2, 3, 4, 5, 6, 7, 8));
Info<< nl
<< "cmptMultiply(dt, dt2): " << cmptMultiply(dt, dt2) << nl
<< "cmptDivide(dt, dt2): " << cmptDivide(dt, dt2) << endl;
{
string dimString("[Pa m^2 s^-2]");
IStringStream is("[Pa m^2 s^-2]");
Pout<< nl;
Pout<< "dimensionSet construct from (is) with contents "
<< dimString << " = " << dimensionSet(is)
<< endl;
is.rewind();
dimensionSet dset(dimless);
is >> dset;
Pout<< "dimensionSet read:" << dset << endl;
}
{
Pout<< nl
<< "construct from (is) = "
<< dimensionedScalar(IStringStream("bla [Pa mm^2 s^-2] 3.0")())
<< endl;
Pout<< "construct from (name,is) = "
<< dimensionedScalar
(
"ABC",
IStringStream("[Pa mm^2 s^-2] 3.0")()
) << endl;
Pout<< "construct from (name,dims,is) = "
<< dimensionedScalar
(
"ABC",
dimLength,
IStringStream("bla [mm] 3.0")()
) << endl;
{
IStringStream is("bla [mm] 3.0");
dimensionedScalar ds;
is >> ds;
Pout<< "read:" << ds << endl;
Info<< "writeEntry:" << nl;
Info<< "abc> "; ds.writeEntry("abc", Info);
Info<< endl;
Info<< "bla> "; ds.writeEntry("bla", Info);
Info<< endl;
}
}
Pout<< "zero scalar (time): " << dimensionedScalar(dimTime) << endl;
Pout<< "zero vector: " << dimensionedVector(dimLength) << endl;
Pout<< "zero tensor: " << dimensionedTensor(dimLength) << endl;
dictionary dict;
{
dict.add("test1", scalar(10));
dict.add("test2a", scalar(21));
dict.add("test5", dimensionedScalar("", 50));
dict.add("carp1", dimensionedScalar("test1", 11));
// This will fail to tokenize:
// dict.add("test5", dimensionedScalar(50));
}
Info<< nl << "Get from dictionary: " << dict << nl;
Info<< "test1 : " << dimensionedScalar("test1", dict) << nl;
Info<< "test2 : " << dimensionedScalar("test2", dimless, 20, dict) << nl;
Info<< "test2a : " << dimensionedScalar("test2a", dimless, 20, dict) << nl;
Info<< "test3 : "
<< dimensionedScalar::lookupOrDefault("test3", dict, 30) << nl;
Info<< "test4 : "
<< dimensionedScalar::lookupOrAddToDict("test4", dict, 40) << nl;
Info<< "test5 : "
<< dimensionedScalar::lookupOrAddToDict("test5", dict, -50) << nl;
// Deprecated
Info<< "Deprecated constructors" << nl;
Info<< "carp : "
<< dimensionedScalar(dict.lookup("carp1")) << nl;
Info<< "carp : "
<< dimensionedScalar("other", dict.lookup("test5")) << nl;
Info<< "carp : "
<< dimensionedScalar("carp", dimless, dict.lookup("carp1")) << nl;
Info<< "alt : "
<< dimensionedScalar("myName", dimless, dict, "carp1") << nl;
Info<< "alt : "
<< dimensionedScalar("myName", dimless, dict, "test5") << nl;
{
dimensionedScalar scalar1("myName", dimless, Zero);
scalar1.read("test5", dict);
Info<< "read in : " << scalar1 << nl;
scalar1.readIfPresent("test4", dict);
Info<< "read in : " << scalar1 << nl;
scalar1.readIfPresent("test5", dict);
Info<< "read in : " << scalar1 << nl;
}
Info<< nl << "Dictionary is now: " << dict << nl;
{
primitiveEntry entry1("scalar1", token(15.0));
// The same:
// primitiveEntry entry1("scalar1", ITstream::parse(" 15.0 "));
// This fails (as it should):
// primitiveEntry entry1("scalar1", ITstream::parse(" 15.0 25.0 "));
dimensionedScalar ds1(entry1);
Info<< "construct from entry: "
<< entry1 << nl
<< " = " << ds1 << nl;
}
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-dynamicIndexedOctree.C
EXE = $(FOAM_USER_APPBIN)/Test-dynamicIndexedOctree
EXE_INC = \
-I$(LIB_SRC)/meshTools/lnInclude
EXE_LIBS = \
-lmeshTools
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012 OpenFOAM Foundation
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
Test the construction, insertion and removal of points from the dynamic
indexed octree.
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "cpuTime.H"
#include "treeBoundBox.H"
#include "dynamicIndexedOctree.H"
#include "dynamicTreeDataPoint.H"
#include "indexedOctree.H"
#include "treeDataPoint.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
cpuTime timer;
treeBoundBox overallBb
(
point(0,0,0),
point(1,1,1)
);
label size = 5e6;
Info<< "Creating indexed octrees with " << size
<< " points in bounding box: " << overallBb << endl;
// Create the data
DynamicList<point> pointList(size);
pointField pointFieldList(size);
for (label pI = size - 1; pI >= 0; --pI)
{
scalar factor = pI/scalar(size);
pointList.append(0.99*point(factor, factor, factor));
pointFieldList[pI] = 0.99*point(factor, factor, factor);
}
for (label i=0; i<5; ++i)
{
pointList.append(point(0.95, 0.95,0.95));
pointFieldList.append(point(0.95, 0.95,0.95));
}
Info<< "Time to construct lists of points: "
<< timer.cpuTimeIncrement() << endl;
// Construct the trees
dynamicIndexedOctree<dynamicTreeDataPoint> tree
(
dynamicTreeDataPoint(pointList),
overallBb, // overall search domain
20, // max levels
100, // maximum ratio of cubes v.s. cells
100.0 // max. duplicity; n/a since no bounding boxes.
);
Info<< "Time to construct dynamic tree: "
<< timer.cpuTimeIncrement() << endl;
indexedOctree<treeDataPoint> tree2
(
treeDataPoint(pointFieldList),
overallBb, // overall search domain
20, // max levels
100, // maximum ratio of cubes v.s. cells
100.0 // max. duplicity; n/a since no bounding boxes.
);
Info<< "Time to construct normal tree: "
<< timer.cpuTimeIncrement() << endl;
Info<< "Statistics of the dynamic tree:" << endl;
tree.writeTreeInfo();
// Test calls to findNearest()
point p(0.94,0.94,0.94);
Info<< "Nearest point to " << p << " = "
<< tree.findNearest(p, 0.4) << endl;
for (label i = 0; i < size; ++i)
{
tree.findNearest
(
point(scalar(i)/size, scalar(i)/size, scalar(i)/size),
0.4
);
}
Info<< "Time to perform " << size
<< " findNearest() calls on the dynamic tree: "
<< timer.cpuTimeIncrement() << endl;
for (label i = 0; i < size; ++i)
{
tree2.findNearest
(
point(scalar(i)/size, scalar(i)/size, scalar(i)/size),
0.4
);
}
Info<< "Time to perform " << size
<< " findNearest() calls on the normal tree: "
<< timer.cpuTimeIncrement() << endl;
// Test point insertion
label index = pointList.size();
pointList.append(p);
Info<< nl << "Inserting point " << p << " with index " << index << endl;
// Now insert a point into the tree.
tree.insert(index, pointList.size());
Info<< "Nearest point to " << p << " = "
<< tree.findNearest(p, 0.4) << endl;
index = pointList.size();
pointList.append(p);
Info<< "Inserting same point " << p << " with index " << index << endl;
tree.insert(index, pointList.size());
Info<< "Nearest point to " << p << " = "
<< tree.findNearest(p, 0.4) << endl;
Info<< nl << "Tree after insertion:" << endl;
tree.writeTreeInfo();
// Test point removal
label nRemoved = 0;
for (label pI = size - 5; pI >= 5; pI--)
{
tree.remove(pI);
++nRemoved;
}
Info<< "Time to remove " << nRemoved << " points from the tree: "
<< timer.cpuTimeIncrement() << endl;
Info<< nl << "Tree after removal:" << endl;
tree.writeTreeInfo();
Info<< "Nearest point to " << p << " = "
<< tree.findNearest(p, 0.4) << endl;
return 0;
}
// ************************************************************************* //
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment