ENH: consistency improvements for interpolationTable, table readers

- avoid stealing autoPtr in interpolationTable copy operations

- improve local memory requirements of readers

- make OpenFOAM table reader default constructible

- more code alignment between csvTableReader and Function1::CSV
  (fix #1498 for csvTableReader as well)
This commit is contained in:
Mark Olesen
2020-01-21 15:17:04 +01:00
parent ee96dba0cf
commit 59ed3ba18d
12 changed files with 364 additions and 274 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,64 +28,73 @@ License
#include "CSV.H"
#include "DynamicList.H"
#include "ListOps.H"
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class Type>
Foam::labelList Foam::Function1Types::CSV<Type>::getComponentColumns
(
const word& name,
const dictionary& dict
)
{
// Writing of columns was forced to be ASCII,
// do the same when reading
labelList cols;
ITstream& is = dict.lookup(name);
is.format(IOstream::ASCII);
is >> cols;
dict.checkITstream(is, name);
if (cols.size() != pTraits<Type>::nComponents)
{
FatalIOErrorInFunction(dict)
<< name << " with " << cols
<< " does not have the expected length "
<< pTraits<Type>::nComponents << nl
<< exit(FatalIOError);
}
return cols;
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<>
Foam::label Foam::Function1Types::CSV<Foam::label>::readValue
(
const List<string>& splitted
const List<string>& strings
) const
{
if (componentColumns_[0] >= splitted.size())
{
FatalErrorInFunction
<< "No column " << componentColumns_[0] << " in "
<< splitted << endl
<< exit(FatalError);
}
return readLabel(splitted[componentColumns_[0]]);
return readLabel(strings[componentColumns_[0]]);
}
template<>
Foam::scalar Foam::Function1Types::CSV<Foam::scalar>::readValue
(
const List<string>& splitted
const List<string>& strings
) const
{
if (componentColumns_[0] >= splitted.size())
{
FatalErrorInFunction
<< "No column " << componentColumns_[0] << " in "
<< splitted << endl
<< exit(FatalError);
}
return readScalar(splitted[componentColumns_[0]]);
return readScalar(strings[componentColumns_[0]]);
}
template<class Type>
Type Foam::Function1Types::CSV<Type>::readValue
(
const List<string>& splitted
const List<string>& strings
) const
{
Type result;
for (label i = 0; i < pTraits<Type>::nComponents; ++i)
{
if (componentColumns_[i] >= splitted.size())
{
FatalErrorInFunction
<< "No column " << componentColumns_[i] << " in "
<< splitted << endl
<< exit(FatalError);
}
result[i] = readScalar(splitted[componentColumns_[i]]);
result[i] = readScalar(strings[componentColumns_[i]]);
}
return result;
@ -106,38 +115,45 @@ void Foam::Function1Types::CSV<Type>::read()
<< exit(FatalIOError);
}
DynamicList<Tuple2<scalar, Type>> values;
const label maxEntry =
max(refColumn_, componentColumns_[findMax(componentColumns_)]);
// skip header
for (label i = 0; i < nHeaderLine_; i++)
label lineNo = 0;
// Skip header
for (label i = 0; i < nHeaderLine_; ++i)
{
string line;
is.getLine(line);
++lineNo;
}
label nEntries = max(componentColumns_);
DynamicList<Tuple2<scalar, Type>> values;
DynamicList<string> strings(maxEntry+1); // reserve
// read data
while (is.good())
{
string line;
is.getLine(line);
++lineNo;
strings.clear();
label n = 0;
std::size_t pos = 0;
DynamicList<string> splitted;
if (mergeSeparators_)
for
(
label n = 0;
(pos != std::string::npos) && (n <= maxEntry);
++n
)
{
std::size_t nPos = 0;
while ((pos != std::string::npos) && (n <= nEntries))
if (mergeSeparators_)
{
bool found = false;
while (!found)
{
nPos = line.find(separator_, pos);
const auto nPos = line.find(separator_, pos);
if ((nPos != std::string::npos) && (nPos - pos == 0))
{
@ -148,52 +164,38 @@ void Foam::Function1Types::CSV<Type>::read()
found = true;
}
}
nPos = line.find(separator_, pos);
if (nPos == std::string::npos)
{
splitted.append(line.substr(pos));
pos = nPos;
n++;
}
else
{
splitted.append(line.substr(pos, nPos - pos));
pos = nPos + 1;
n++;
}
}
}
else
{
while ((pos != std::string::npos) && (n <= nEntries))
const auto nPos = line.find(separator_, pos);
if (nPos == std::string::npos)
{
std::size_t nPos = line.find(separator_, pos);
if (nPos == std::string::npos)
{
splitted.append(line.substr(pos));
pos = nPos;
n++;
}
else
{
splitted.append(line.substr(pos, nPos - pos));
pos = nPos + 1;
n++;
}
strings.append(line.substr(pos));
pos = nPos;
}
else
{
strings.append(line.substr(pos, nPos - pos));
pos = nPos + 1;
}
}
if (splitted.size() <= 1)
if (strings.size() <= 1)
{
break;
}
scalar x = readScalar(splitted[refColumn_]);
Type value = readValue(splitted);
if (strings.size() <= maxEntry)
{
FatalErrorInFunction
<< "Not enough columns near line " << lineNo
<< ". Require " << (maxEntry+1) << " but found "
<< strings << nl
<< exit(FatalError);
}
scalar x = readScalar(strings[refColumn_]);
Type value = readValue(strings);
values.append(Tuple2<scalar,Type>(x, value));
}
@ -215,26 +217,11 @@ Foam::Function1Types::CSV<Type>::CSV
TableBase<Type>(entryName, dict),
nHeaderLine_(dict.get<label>("nHeaderLine")),
refColumn_(dict.get<label>("refColumn")),
componentColumns_(),
componentColumns_(getComponentColumns("componentColumns", dict)),
separator_(dict.getOrDefault<string>("separator", ",")[0]),
mergeSeparators_(dict.get<bool>("mergeSeparators")),
fName_(fName.empty() ? dict.get<fileName>("file") : fName)
{
// Writing of "componentColumns" was forced to be ASCII,
// do the same when reading
ITstream& is = dict.lookup("componentColumns");
is.format(IOstream::ASCII);
is >> componentColumns_;
dict.checkITstream(is, "componentColumns");
if (componentColumns_.size() != pTraits<Type>::nComponents)
{
FatalIOErrorInFunction(dict)
<< componentColumns_ << " does not have the expected length of "
<< pTraits<Type>::nComponents << nl
<< exit(FatalIOError);
}
read();
TableBase<Type>::check();

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -60,7 +60,6 @@ SourceFiles
#include "TableBase.H"
#include "Tuple2.H"
#include "labelList.H"
#include "ISstream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -78,22 +77,22 @@ class CSV
:
public TableBase<Type>
{
// Private data
// Private Data
//- Number header lines
label nHeaderLine_;
const label nHeaderLine_;
//- Column of the time
label refColumn_;
const label refColumn_;
//- Labels of the components
labelList componentColumns_;
const labelList componentColumns_;
//- Separator character
char separator_;
const char separator_;
//- Merge separators flag, e.g. ',,,' becomes ','
bool mergeSeparators_;
const bool mergeSeparators_;
//- File name for csv table
fileName fName_;
@ -101,11 +100,18 @@ class CSV
// Private Member Functions
//- Get component columns entry
static labelList getComponentColumns
(
const word& name,
const dictionary& dict
);
//- Read csv data table
void read();
//- Read the next value from the splitted string
Type readValue(const List<string>& splitted) const;
//- Read component values from the split string
Type readValue(const List<string>& strings) const;
//- No copy assignment
void operator=(const CSV<Type>&) = delete;
@ -113,7 +119,7 @@ class CSV
public:
//- Runtime type information
//- Declare type-name, virtual type (with debug switch)
TypeName("csvFile");
@ -127,7 +133,7 @@ public:
const fileName& fName = fileName::null
);
//- Copy constructor
//- Copy construct
explicit CSV(const CSV<Type>& csv);
//- Construct and return a clone
@ -153,10 +159,10 @@ public:
// Template specialisations
template<>
label CSV<label>::readValue(const List<string>& splitted) const;
label CSV<label>::readValue(const List<string>& strings) const;
template<>
Foam::scalar CSV<scalar>::readValue(const List<string>& splitted) const;
scalar CSV<scalar>::readValue(const List<string>& strings) const;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //