ENH: add boolIOField to allow registering

This commit is contained in:
Mark Olesen
2019-11-13 18:54:10 +01:00
committed by Andrew Heather
parent 98467036b3
commit 6798c61047
4 changed files with 175 additions and 41 deletions

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -31,8 +32,9 @@ Description
\*---------------------------------------------------------------------------*/
#include "IOField.H"
#include "argList.H"
#include "IOField.H"
#include "primitiveFields.H"
#include "polyMesh.H"
#include "Time.H"
@ -40,23 +42,38 @@ using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
void write(const IOobject& io, const label sz)
template<class Type>
void doWrite(const IOobject& io, const label sz)
{
IOField<label> fld(io, sz);
IOField<Type> fld(io, sz);
forAll(fld, i)
{
fld[i] = i+1000;
fld[i] = i + 1000.25 + (0.25 * i);
}
Pout<< "writing:" << fld << endl;
fld.write(sz > 0);
}
void read(const IOobject& io, const label sz)
template<>
void doWrite<bool>(const IOobject& io, const label sz)
{
IOField<bool> fld(io, sz);
forAll(fld, i)
{
fld[i] = i % 2;
}
Pout<< "writing:" << fld << endl;
fld.write(sz > 0);
}
template<class Type>
void doRead(const IOobject& io, const label sz)
{
bool valid = (sz > 0);
Pout<< " valid:" << valid << endl;
IOField<label> fld(io, valid);
IOField<Type> fld(io, valid);
Pout<< " wanted:" << sz << " actually read:" << fld.size() << endl;
if (fld.size() != sz)
@ -66,6 +83,7 @@ void read(const IOobject& io, const label sz)
}
template<class Type>
void writeAndRead
(
const IOobject& io,
@ -87,7 +105,7 @@ void writeAndRead
// Write
Pout<< "Writing:" << fileHandler().objectPath(io, io.name()) << endl;
write(io, sz);
doWrite<Type>(io, sz);
autoPtr<fileOperation> readHandler(fileOperation::New(readType, true));
fileHandler(readHandler);
@ -97,13 +115,14 @@ void writeAndRead
readIO.readOpt() = readOpt;
Pout<< "Reading:"
<< fileHandler().filePath(readIO.objectPath()) << endl;
read(readIO, sz);
doRead<Type>(readIO, sz);
Pout<< "** Done writing:" << writeType
<< " Reading:" << readType << nl << nl << endl;
}
template<class Type>
void readIfPresent
(
IOobject& io,
@ -117,15 +136,54 @@ void readIfPresent
// Read
Pout<< "Reading:" << fileHandler().filePath(io.objectPath()) << endl;
io.readOpt() = IOobject::READ_IF_PRESENT;
read(io, sz);
doRead<Type>(io, sz);
}
// Main program:
template<class Type>
void doTests(IOobject& io, const label sz)
{
const wordList handlers
(
Foam::fileOperation::wordConstructorTablePtr_->sortedToc()
);
Info<< "Found handlers: " << flatOutput(handlers) << nl
<< "Running tests with " << pTraits<Type>::typeName << nl << nl;
// for (const word& readHandler : handlers)
// {
// readIfPresent<Type>(io, sz, readHandler);
// }
for (const word& writeHandler : handlers)
{
for (const word& readHandler : handlers)
{
writeAndRead<Type>
(
io,
sz,
writeHandler,
IOobject::READ_IF_PRESENT,
readHandler
);
}
}
}
// Main program
int main(int argc, char *argv[])
{
argList::addBoolOption("bool", "Use bool for tests");
argList::addBoolOption("scalar", "Use scalar for tests");
argList::addBoolOption("label", "Use label for tests (default)");
#include "addTimeOptions.H"
#include "setRootCase.H"
#include "createTime.H"
#include "createPolyMesh.H"
@ -136,6 +194,12 @@ int main(int argc, char *argv[])
sz = 1;
}
if (!Pstream::parRun())
{
sz = 10;
Info<< "Serial: using " << sz << nl;
}
IOobject io
(
"bla",
@ -145,45 +209,26 @@ int main(int argc, char *argv[])
IOobject::NO_WRITE
);
wordList handlers
(
Foam::fileOperation::wordConstructorTablePtr_->sortedToc()
);
Info<< "Found handlers:" << handlers << endl;
label tested = 0;
/*
forAll(handlers, readi)
if (args.found("bool"))
{
const word& readHandler = handlers[readi];
readIfPresent(io, sz, readHandler);
doTests<bool>(io, sz);
++tested;
}
*/
forAll(handlers, writei)
if (args.found("scalar"))
{
const word& writeHandler = handlers[writei];
forAll(handlers, readi)
{
const word& readHandler = handlers[readi];
writeAndRead
(
io,
sz,
writeHandler,
IOobject::READ_IF_PRESENT,
readHandler
);
doTests<scalar>(io, sz);
++tested;
}
if (!tested || args.found("label"))
{
doTests<label>(io, sz);
}
Pout<< "End\n" << endl;
Pout<< "\nEnd\n" << endl;
return 0;
}

View File

@ -658,6 +658,7 @@ Fields = fields/Fields
$(Fields)/Field/FieldBase.C
$(Fields)/boolField/boolField.C
$(Fields)/boolField/boolIOField.C
$(Fields)/labelField/labelField.C
$(Fields)/labelField/labelIOField.C
$(Fields)/labelField/labelFieldIOField.C

View File

@ -0,0 +1,37 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 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 "boolIOField.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
defineTemplateTypeNameAndDebugWithName(boolIOField, "boolField", 0);
}
// ************************************************************************* //

View File

@ -0,0 +1,51 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 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/>.
Typedef
Foam::boolIOField
Description
A boolField with IO.
\*---------------------------------------------------------------------------*/
#ifndef boolIOField_H
#define boolIOField_H
#include "boolField.H"
#include "IOField.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
typedef IOField<bool> boolIOField;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //