Commit 59ed3ba1 authored by Mark Olesen's avatar Mark Olesen

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)
parent ee96dba0
......@@ -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.
......@@ -84,7 +84,7 @@ Foam::interpolation2DTable<Type>::interpolation2DTable(const fileName& fName)
List<value_type>(),
bounding_(bounds::normalBounding::WARN),
fileName_(fName),
reader_(new openFoamTableReader<Type>(dictionary()))
reader_(new openFoamTableReader<Type>())
{
readTable();
}
......@@ -114,13 +114,13 @@ Foam::interpolation2DTable<Type>::interpolation2DTable(const dictionary& dict)
template<class Type>
Foam::interpolation2DTable<Type>::interpolation2DTable
(
const interpolation2DTable& interpTable
const interpolation2DTable& tbl
)
:
List<value_type>(interpTable),
bounding_(interpTable.bounding_),
fileName_(interpTable.fileName_),
reader_(interpTable.reader_) // note: steals reader. Used in write().
List<value_type>(tbl),
bounding_(tbl.bounding_),
fileName_(tbl.fileName_),
reader_(tbl.reader_.clone())
{}
......@@ -218,6 +218,24 @@ Foam::label Foam::interpolation2DTable<Type>::Xi
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Type>
void Foam::interpolation2DTable<Type>::operator=
(
const interpolation2DTable<Type>& rhs
)
{
if (this == &rhs)
{
return;
}
static_cast<List<value_type>&>(*this) = rhs;
bounding_ = rhs.bounding_;
fileName_ = rhs.fileName_;
reader_.reset(rhs.reader_.clone());
}
template<class Type>
Type Foam::interpolation2DTable<Type>::operator()
(
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -63,7 +63,7 @@ class interpolation2DTable
//- File name
fileName fileName_;
//- The actual reader
//- Table reader
autoPtr<tableReader<Type>> reader_;
......@@ -102,7 +102,7 @@ public:
// Constructors
//- Construct null
//- Default construct
interpolation2DTable();
//- Construct from components
......@@ -120,7 +120,7 @@ public:
explicit interpolation2DTable(const dictionary& dict);
//- Copy construct
interpolation2DTable(const interpolation2DTable& interpTable);
interpolation2DTable(const interpolation2DTable& tbl);
// Member Functions
......@@ -135,6 +135,9 @@ public:
// Member Operators
//- Copy assignment
void operator=(const interpolation2DTable<Type>& rhs);
//- Return an interpolated value
Type operator()(const scalar valueX, const scalar valueY) const;
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -88,7 +88,7 @@ Foam::interpolationTable<Type>::interpolationTable(const fileName& fName)
List<value_type>(),
bounding_(bounds::repeatableBounding::WARN),
fileName_(fName),
reader_(new openFoamTableReader<Type>(dictionary()))
reader_(new openFoamTableReader<Type>())
{
readTable();
}
......@@ -118,17 +118,16 @@ Foam::interpolationTable<Type>::interpolationTable(const dictionary& dict)
template<class Type>
Foam::interpolationTable<Type>::interpolationTable
(
const interpolationTable& interpTable
const interpolationTable& tbl
)
:
List<value_type>(interpTable),
bounding_(interpTable.bounding_),
fileName_(interpTable.fileName_),
reader_(interpTable.reader_) // note: steals reader. Used in write().
List<value_type>(tbl),
bounding_(tbl.bounding_),
fileName_(tbl.fileName_),
reader_(tbl.reader_.clone())
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
......@@ -491,6 +490,24 @@ Foam::interpolationTable<Type>::interpolateValues
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Type>
void Foam::interpolationTable<Type>::operator=
(
const interpolationTable<Type>& rhs
)
{
if (this == &rhs)
{
return;
}
static_cast<List<value_type>&>(*this) = rhs;
bounding_ = rhs.bounding_;
fileName_ = rhs.fileName_;
reader_.reset(rhs.reader_.clone());
}
template<class Type>
const Foam::Tuple2<Foam::scalar, Type>&
Foam::interpolationTable<Type>::operator[](label idx) const
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -92,7 +92,7 @@ class interpolationTable
//- File name
fileName fileName_;
//- The actual reader
//- Table reader
autoPtr<tableReader<Type>> reader_;
......@@ -115,7 +115,7 @@ public:
// Constructors
//- Construct null
//- Default construct
interpolationTable();
//- Construct from components
......@@ -135,7 +135,7 @@ public:
explicit interpolationTable(const dictionary& dict);
//- Copy construct
interpolationTable(const interpolationTable& interpTable);
interpolationTable(const interpolationTable& tbl);
// Member Functions
......@@ -172,6 +172,9 @@ public:
// Member Operators
//- Copy assignment
void operator=(const interpolationTable<Type>& rhs);
//- Return an element of constant Tuple2<scalar, Type>
const Tuple2<scalar, Type>& operator[](label idx) const;
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -28,78 +29,98 @@ License
#include "csvTableReader.H"
#include "fileOperation.H"
#include "DynamicList.H"
#include "ListOps.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class Type>
Foam::csvTableReader<Type>::csvTableReader(const dictionary& dict)
:
tableReader<Type>(dict),
headerLine_(dict.get<bool>("hasHeaderLine")),
timeColumn_(dict.get<label>("timeColumn")),
componentColumns_(dict.lookup("valueColumns")),
separator_(dict.lookupOrDefault<string>("separator", ",")[0])
Foam::labelList Foam::csvTableReader<Type>::getComponentColumns
(
const word& name,
const dictionary& dict
)
{
if (componentColumns_.size() != pTraits<Type>::nComponents)
{
FatalErrorInFunction
<< componentColumns_ << " does not have the expected length "
<< pTraits<Type>::nComponents << endl
<< exit(FatalError);
}
}
// Writing of columns was forced to be ASCII,
// do the same when reading
labelList cols;
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
ITstream& is = dict.lookup(name);
is.format(IOstream::ASCII);
is >> cols;
dict.checkITstream(is, name);
template<class Type>
Foam::csvTableReader<Type>::~csvTableReader()
{}
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;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
namespace Foam
{
// doesn't recognize specialization otherwise
template<>
scalar csvTableReader<scalar>::readValue(const List<string>& splitted)
label csvTableReader<label>::readValue
(
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 readLabel(strings[componentColumns_[0]]);
}
template<class Type>
Type csvTableReader<Type>::readValue(const List<string>& splitted)
template<>
scalar csvTableReader<scalar>::readValue
(
const List<string>& strings
) const
{
Type result;
return readScalar(strings[componentColumns_[0]]);
}
for (label i = 0; i < pTraits<Type>::nComponents; ++i)
{
if (componentColumns_[i] >= splitted.size())
{
FatalErrorInFunction
<< "No column " << componentColumns_[i] << " in "
<< splitted << endl
<< exit(FatalError);
}
} // End namespace Foam
result[i] = readScalar(splitted[componentColumns_[i]]);
}
return result;
template<class Type>
Type Foam::csvTableReader<Type>::readValue
(
const List<string>& strings
) const
{
Type result;
for (label i = 0; i < pTraits<Type>::nComponents; ++i)
{
result[i] = readScalar(strings[componentColumns_[i]]);
}
return result;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::csvTableReader<Type>::csvTableReader(const dictionary& dict)
:
tableReader<Type>(dict),
headerLine_(dict.get<bool>("hasHeaderLine")),
refColumn_(dict.get<label>("timeColumn")),
componentColumns_(getComponentColumns("valueColumns", dict)),
separator_(dict.getOrDefault<string>("separator", ",")[0])
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
void Foam::csvTableReader<Type>::operator()
(
......@@ -107,52 +128,74 @@ void Foam::csvTableReader<Type>::operator()
List<Tuple2<scalar, Type>>& data
)
{
//IFstream in(fName);
autoPtr<ISstream> inPtr(fileHandler().NewIFstream(fName));
ISstream& in = inPtr();
autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fName));
ISstream& is = isPtr();
DynamicList<Tuple2<scalar, Type>> values;
const label maxEntry =
max(refColumn_, componentColumns_[findMax(componentColumns_)]);
label lineNo = 0;
// Skip header
if (headerLine_)
{
string line;
in.getLine(line);
is.getLine(line);
++lineNo;
}
while (in.good())
DynamicList<Tuple2<scalar, Type>> values;
DynamicList<string> strings(maxEntry+1); // reserve
while (is.good())
{
string line;
in.getLine(line);
is.getLine(line);
++lineNo;
DynamicList<string> splitted;
strings.clear();
std::size_t pos = 0;
while (pos != std::string::npos)
for
(
label n = 0;
(pos != std::string::npos) && (n <= maxEntry);
++n
)
{
std::size_t nPos = line.find(separator_, pos);
const auto nPos = line.find(separator_, pos);
if (nPos == std::string::npos)
{
splitted.append(line.substr(pos));
pos=nPos;
strings.append(line.substr(pos));
pos = nPos;
}
else
{
splitted.append(line.substr(pos, nPos-pos));
pos=nPos+1;
strings.append(line.substr(pos, nPos-pos));
pos = nPos + 1;
}
}
if (splitted.size() <= 1)
if (strings.size() <= 1)
{
break;
}
scalar time = readScalar(splitted[timeColumn_]);
Type value = readValue(splitted);
if (strings.size() <= maxEntry)
{
FatalErrorInFunction
<< "Not enough columns near line " << lineNo
<< ". Require " << (maxEntry+1) << " but found "
<< strings << nl
<< exit(FatalError);
}
values.append(Tuple2<scalar,Type>(time, value));
scalar x = readScalar(strings[refColumn_]);
Type value = readValue(strings);
values.append(Tuple2<scalar,Type>(x, value));
}
data.transfer(values);
......@@ -176,21 +219,13 @@ void Foam::csvTableReader<Type>::write(Ostream& os) const
tableReader<Type>::write(os);
os.writeEntry("hasHeaderLine", headerLine_);
os.writeEntry("timeColumn", timeColumn_);
os.writeEntry("timeColumn", refColumn_);
// Force writing labelList in ascii
os.writeKeyword("valueColumns");
if (os.format() == IOstream::BINARY)
{
os.format(IOstream::ASCII);
os << componentColumns_;
os.format(IOstream::BINARY);
}
else
{
os << componentColumns_;
}
os << token::END_STATEMENT << nl;
const enum IOstream::streamFormat fmt = os.format();
os.format(IOstream::ASCII);
os.writeEntry("componentColumns", componentColumns_);
os.format(fmt);
os.writeEntry("separator", string(separator_));
}
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -54,27 +55,37 @@ class csvTableReader
:
public tableReader<Type>
{
// Private data
// Private Data
//- Does the file have a header line?
const bool headerLine_;
//- Column of the time
const label timeColumn_;
//- Column for reference (lookup) value
const label refColumn_;
//- Labels of the components
const labelList componentColumns_;
//- Read the next value from the splitted string
Type readValue(const List<string>&);
//- Separator character
const char separator_;
// Private Member Functions
//- Get component columns entry
static labelList getComponentColumns
(
const word& name,
const dictionary& dict
);
//- Read component values from the split string
Type readValue(const List<string>& strings) const;
public:
//- Runtime type information
//- Declare type-name, virtual type (with debug switch)
TypeName("csv");
......@@ -97,19 +108,23 @@ public:
//- Destructor
virtual ~csvTableReader();
virtual ~csvTableReader() = default;
// Member Functions
//- Read the table
virtual void operator()(const fileName&, List<Tuple2<scalar, Type>>&);
//- Read 1D table
virtual void operator()
(
const fileName& fName,
List<Tuple2<scalar, Type>>& data
);
//- Read 2D table
//- Read 2D table - NotImplemented
virtual void operator()
(
const fileName&,
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>&
const fileName& fName,
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& data
);
//- Write the remaining parameters
......@@ -117,6 +132,14 @@ public:
};
// Template specialisations
template<>
label csvTableReader<label>::readValue(const List<string>& strings) const;
template<>
scalar csvTableReader<scalar>::readValue(const List<string>& strings) const;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
......
......@@ -37,13 +37,6 @@ Foam::openFoamTableReader<Type>::openFoamTableReader(const dictionary& dict)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Type>
Foam::openFoamTableReader<Type>::~openFoamTableReader()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -53,17 +54,25 @@ class openFoamTableReader
:
public tableReader<Type>
{