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 | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd. Copyright (C) 2016-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -84,7 +84,7 @@ Foam::interpolation2DTable<Type>::interpolation2DTable(const fileName& fName)
List<value_type>(), List<value_type>(),
bounding_(bounds::normalBounding::WARN), bounding_(bounds::normalBounding::WARN),
fileName_(fName), fileName_(fName),
reader_(new openFoamTableReader<Type>(dictionary())) reader_(new openFoamTableReader<Type>())
{ {
readTable(); readTable();
} }
@ -114,13 +114,13 @@ Foam::interpolation2DTable<Type>::interpolation2DTable(const dictionary& dict)
template<class Type> template<class Type>
Foam::interpolation2DTable<Type>::interpolation2DTable Foam::interpolation2DTable<Type>::interpolation2DTable
( (
const interpolation2DTable& interpTable const interpolation2DTable& tbl
) )
: :
List<value_type>(interpTable), List<value_type>(tbl),
bounding_(interpTable.bounding_), bounding_(tbl.bounding_),
fileName_(interpTable.fileName_), fileName_(tbl.fileName_),
reader_(interpTable.reader_) // note: steals reader. Used in write(). reader_(tbl.reader_.clone())
{} {}
@ -218,6 +218,24 @@ Foam::label Foam::interpolation2DTable<Type>::Xi
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * 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> template<class Type>
Type Foam::interpolation2DTable<Type>::operator() Type Foam::interpolation2DTable<Type>::operator()
( (

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -63,7 +63,7 @@ class interpolation2DTable
//- File name //- File name
fileName fileName_; fileName fileName_;
//- The actual reader //- Table reader
autoPtr<tableReader<Type>> reader_; autoPtr<tableReader<Type>> reader_;
@ -102,7 +102,7 @@ public:
// Constructors // Constructors
//- Construct null //- Default construct
interpolation2DTable(); interpolation2DTable();
//- Construct from components //- Construct from components
@ -120,7 +120,7 @@ public:
explicit interpolation2DTable(const dictionary& dict); explicit interpolation2DTable(const dictionary& dict);
//- Copy construct //- Copy construct
interpolation2DTable(const interpolation2DTable& interpTable); interpolation2DTable(const interpolation2DTable& tbl);
// Member Functions // Member Functions
@ -135,6 +135,9 @@ public:
// Member Operators // Member Operators
//- Copy assignment
void operator=(const interpolation2DTable<Type>& rhs);
//- Return an interpolated value //- Return an interpolated value
Type operator()(const scalar valueX, const scalar valueY) const; Type operator()(const scalar valueX, const scalar valueY) const;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -88,7 +88,7 @@ Foam::interpolationTable<Type>::interpolationTable(const fileName& fName)
List<value_type>(), List<value_type>(),
bounding_(bounds::repeatableBounding::WARN), bounding_(bounds::repeatableBounding::WARN),
fileName_(fName), fileName_(fName),
reader_(new openFoamTableReader<Type>(dictionary())) reader_(new openFoamTableReader<Type>())
{ {
readTable(); readTable();
} }
@ -118,17 +118,16 @@ Foam::interpolationTable<Type>::interpolationTable(const dictionary& dict)
template<class Type> template<class Type>
Foam::interpolationTable<Type>::interpolationTable Foam::interpolationTable<Type>::interpolationTable
( (
const interpolationTable& interpTable const interpolationTable& tbl
) )
: :
List<value_type>(interpTable), List<value_type>(tbl),
bounding_(interpTable.bounding_), bounding_(tbl.bounding_),
fileName_(interpTable.fileName_), fileName_(tbl.fileName_),
reader_(interpTable.reader_) // note: steals reader. Used in write(). reader_(tbl.reader_.clone())
{} {}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type> template<class Type>
@ -491,6 +490,24 @@ Foam::interpolationTable<Type>::interpolateValues
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * 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> template<class Type>
const Foam::Tuple2<Foam::scalar, Type>& const Foam::Tuple2<Foam::scalar, Type>&
Foam::interpolationTable<Type>::operator[](label idx) const Foam::interpolationTable<Type>::operator[](label idx) const

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -92,7 +92,7 @@ class interpolationTable
//- File name //- File name
fileName fileName_; fileName fileName_;
//- The actual reader //- Table reader
autoPtr<tableReader<Type>> reader_; autoPtr<tableReader<Type>> reader_;
@ -115,7 +115,7 @@ public:
// Constructors // Constructors
//- Construct null //- Default construct
interpolationTable(); interpolationTable();
//- Construct from components //- Construct from components
@ -135,7 +135,7 @@ public:
explicit interpolationTable(const dictionary& dict); explicit interpolationTable(const dictionary& dict);
//- Copy construct //- Copy construct
interpolationTable(const interpolationTable& interpTable); interpolationTable(const interpolationTable& tbl);
// Member Functions // Member Functions
@ -172,6 +172,9 @@ public:
// Member Operators // Member Operators
//- Copy assignment
void operator=(const interpolationTable<Type>& rhs);
//- Return an element of constant Tuple2<scalar, Type> //- Return an element of constant Tuple2<scalar, Type>
const Tuple2<scalar, Type>& operator[](label idx) const; const Tuple2<scalar, Type>& operator[](label idx) const;

View File

@ -6,6 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,6 +29,82 @@ License
#include "csvTableReader.H" #include "csvTableReader.H"
#include "fileOperation.H" #include "fileOperation.H"
#include "DynamicList.H" #include "DynamicList.H"
#include "ListOps.H"
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class Type>
Foam::labelList Foam::csvTableReader<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 * * * * * * * * * * * //
namespace Foam
{
template<>
label csvTableReader<label>::readValue
(
const List<string>& strings
) const
{
return readLabel(strings[componentColumns_[0]]);
}
template<>
scalar csvTableReader<scalar>::readValue
(
const List<string>& strings
) const
{
return readScalar(strings[componentColumns_[0]]);
}
} // End namespace Foam
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 * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
@ -36,70 +113,14 @@ Foam::csvTableReader<Type>::csvTableReader(const dictionary& dict)
: :
tableReader<Type>(dict), tableReader<Type>(dict),
headerLine_(dict.get<bool>("hasHeaderLine")), headerLine_(dict.get<bool>("hasHeaderLine")),
timeColumn_(dict.get<label>("timeColumn")), refColumn_(dict.get<label>("timeColumn")),
componentColumns_(dict.lookup("valueColumns")), componentColumns_(getComponentColumns("valueColumns", dict)),
separator_(dict.lookupOrDefault<string>("separator", ",")[0]) separator_(dict.getOrDefault<string>("separator", ",")[0])
{
if (componentColumns_.size() != pTraits<Type>::nComponents)
{
FatalErrorInFunction
<< componentColumns_ << " does not have the expected length "
<< pTraits<Type>::nComponents << endl
<< exit(FatalError);
}
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Type>
Foam::csvTableReader<Type>::~csvTableReader()
{} {}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
namespace Foam
{
// doesn't recognize specialization otherwise
template<>
scalar csvTableReader<scalar>::readValue(const List<string>& splitted)
{
if (componentColumns_[0] >= splitted.size())
{
FatalErrorInFunction
<< "No column " << componentColumns_[0] << " in "
<< splitted << endl
<< exit(FatalError);
}
return readScalar(splitted[componentColumns_[0]]);
}
template<class Type>
Type csvTableReader<Type>::readValue(const List<string>& splitted)
{
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]]);
}
return result;
}
}
template<class Type> template<class Type>
void Foam::csvTableReader<Type>::operator() void Foam::csvTableReader<Type>::operator()
( (
@ -107,52 +128,74 @@ void Foam::csvTableReader<Type>::operator()
List<Tuple2<scalar, Type>>& data List<Tuple2<scalar, Type>>& data
) )
{ {
//IFstream in(fName); autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fName));
autoPtr<ISstream> inPtr(fileHandler().NewIFstream(fName)); ISstream& is = isPtr();
ISstream& in = inPtr();
DynamicList<Tuple2<scalar, Type>> values; const label maxEntry =
max(refColumn_, componentColumns_[findMax(componentColumns_)]);
label lineNo = 0;
// Skip header // Skip header
if (headerLine_) if (headerLine_)
{ {
string line; 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; string line;
in.getLine(line); is.getLine(line);
++lineNo;
DynamicList<string> splitted; strings.clear();
std::size_t pos = 0; 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) if (nPos == std::string::npos)
{ {
splitted.append(line.substr(pos)); strings.append(line.substr(pos));
pos = nPos; pos = nPos;
} }
else else
{ {
splitted.append(line.substr(pos, nPos-pos)); strings.append(line.substr(pos, nPos-pos));
pos = nPos + 1; pos = nPos + 1;
} }
} }
if (splitted.size() <= 1) if (strings.size() <= 1)
{ {
break; break;
} }
scalar time = readScalar(splitted[timeColumn_]); if (strings.size() <= maxEntry)
Type value = readValue(splitted); {
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); data.transfer(values);
@ -176,21 +219,13 @@ void Foam::csvTableReader<Type>::write(Ostream& os) const
tableReader<Type>::write(os); tableReader<Type>::write(os);
os.writeEntry("hasHeaderLine", headerLine_); os.writeEntry("hasHeaderLine", headerLine_);
os.writeEntry("timeColumn", timeColumn_); os.writeEntry("timeColumn", refColumn_);
// Force writing labelList in ascii // Force writing labelList in ascii
os.writeKeyword("valueColumns"); const enum IOstream::streamFormat fmt = os.format();
if (os.format() == IOstream::BINARY)
{
os.format(IOstream::ASCII); os.format(IOstream::ASCII);
os << componentColumns_; os.writeEntry("componentColumns", componentColumns_);
os.format(IOstream::BINARY); os.format(fmt);
}
else
{
os << componentColumns_;
}
os << token::END_STATEMENT << nl;
os.writeEntry("separator", string(separator_)); os.writeEntry("separator", string(separator_));
} }

View File

@ -6,6 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -54,27 +55,37 @@ class csvTableReader
: :
public tableReader<Type> public tableReader<Type>
{ {
// Private data // Private Data
//- Does the file have a header line? //- Does the file have a header line?
const bool headerLine_; const bool headerLine_;
//- Column of the time //- Column for reference (lookup) value
const label timeColumn_; const label refColumn_;
//- Labels of the components //- Labels of the components
const labelList componentColumns_; const labelList componentColumns_;
//- Read the next value from the splitted string
Type readValue(const List<string>&);
//- Separator character //- Separator character
const char separator_; 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: public:
//- Runtime type information //- Declare type-name, virtual type (with debug switch)
TypeName("csv"); TypeName("csv");
@ -97,19 +108,23 @@ public:
//- Destructor //- Destructor
virtual ~csvTableReader(); virtual ~csvTableReader() = default;
// Member Functions // Member Functions
//- Read the table //- Read 1D table
virtual void operator()(const fileName&, List<Tuple2<scalar, Type>>&);
//- Read 2D table
virtual void operator() virtual void operator()
( (
const fileName&, const fileName& fName,
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& List<Tuple2<scalar, Type>>& data
);
//- Read 2D table - NotImplemented
virtual void operator()
(
const fileName& fName,
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& data
); );
//- Write the remaining parameters //- 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 } // End namespace Foam

View File

@ -37,13 +37,6 @@ Foam::openFoamTableReader<Type>::openFoamTableReader(const dictionary& dict)
{} {}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Type>
Foam::openFoamTableReader<Type>::~openFoamTableReader()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type> template<class Type>

View File

@ -6,6 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -53,17 +54,25 @@ class openFoamTableReader
: :
public tableReader<Type> public tableReader<Type>
{ {
public: public:
//- Runtime type information //- Declare type-name, virtual type (with debug switch)
TypeName("openFoam"); TypeName("openFoam");
// Generated Methods
//- Default construct
openFoamTableReader() = default;
//- Destructor
virtual ~openFoamTableReader() = default;
// Constructors // Constructors
//- Construct from dictionary //- Construct from dictionary
openFoamTableReader(const dictionary &dict); explicit openFoamTableReader(const dictionary& dict);
//- Construct and return a copy //- Construct and return a copy
virtual autoPtr<tableReader<Type>> clone() const virtual autoPtr<tableReader<Type>> clone() const
@ -78,20 +87,20 @@ public:
} }
//- Destructor // Member Functions
virtual ~openFoamTableReader();
//- Read 1D table
// Member functions virtual void operator()
(
//- Read the table const fileName& fName,
virtual void operator()(const fileName&, List<Tuple2<scalar, Type>> &); List<Tuple2<scalar, Type>>& data
);
//- Read 2D table //- Read 2D table
virtual void operator() virtual void operator()
( (
const fileName&, const fileName& fName,
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& data
); );
}; };
@ -106,7 +115,6 @@ public:
#include "openFoamTableReader.C" #include "openFoamTableReader.C"
#endif #endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif #endif

View File

@ -66,13 +66,6 @@ Foam::tableReader<Type>::tableReader(const dictionary&)
{} {}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Type>
Foam::tableReader<Type>::~tableReader()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type> template<class Type>

View File

@ -6,6 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -59,12 +60,12 @@ namespace Foam
template<class Type> template<class Type>
class tableReader class tableReader
{ {
public: public:
//- Runtime type information //- Declare type-name, virtual type (with debug switch)
TypeName("tableReader"); TypeName("tableReader");
// Declare run-time constructor selection table // Declare run-time constructor selection table
declareRunTimeSelectionTable declareRunTimeSelectionTable
@ -79,6 +80,9 @@ public:
// Constructors // Constructors
//- Default construct
tableReader() = default;
//- Construct from dictionary //- Construct from dictionary
tableReader(const dictionary& dict); tableReader(const dictionary& dict);
@ -93,23 +97,23 @@ public:
//- Destructor //- Destructor
virtual ~tableReader(); virtual ~tableReader() = default;
// Member functions // Member Functions
//- Read the table //- Read 1D table
virtual void operator() virtual void operator()
( (
const fileName&, const fileName& fName,
List<Tuple2<scalar, Type>>& List<Tuple2<scalar, Type>>& data
) = 0; ) = 0;
//- Read the 2D table //- Read 2D table
virtual void operator() virtual void operator()
( (
const fileName&, const fileName& fName,
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& tbl
) = 0; ) = 0;
//- Write additional information //- Write additional information

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd. Copyright (C) 2016-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,64 +28,73 @@ License
#include "CSV.H" #include "CSV.H"
#include "DynamicList.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 * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<> template<>
Foam::label Foam::Function1Types::CSV<Foam::label>::readValue Foam::label Foam::Function1Types::CSV<Foam::label>::readValue
( (
const List<string>& splitted const List<string>& strings
) const ) const
{ {
if (componentColumns_[0] >= splitted.size()) return readLabel(strings[componentColumns_[0]]);
{
FatalErrorInFunction
<< "No column " << componentColumns_[0] << " in "
<< splitted << endl
<< exit(FatalError);
}
return readLabel(splitted[componentColumns_[0]]);
} }
template<> template<>
Foam::scalar Foam::Function1Types::CSV<Foam::scalar>::readValue Foam::scalar Foam::Function1Types::CSV<Foam::scalar>::readValue
( (
const List<string>& splitted const List<string>& strings
) const ) const
{ {
if (componentColumns_[0] >= splitted.size()) return readScalar(strings[componentColumns_[0]]);
{
FatalErrorInFunction
<< "No column " << componentColumns_[0] << " in "
<< splitted << endl
<< exit(FatalError);
}
return readScalar(splitted[componentColumns_[0]]);
} }
template<class Type> template<class Type>
Type Foam::Function1Types::CSV<Type>::readValue Type Foam::Function1Types::CSV<Type>::readValue
( (
const List<string>& splitted const List<string>& strings
) const ) const
{ {
Type result; Type result;
for (label i = 0; i < pTraits<Type>::nComponents; ++i) for (label i = 0; i < pTraits<Type>::nComponents; ++i)
{ {
if (componentColumns_[i] >= splitted.size()) result[i] = readScalar(strings[componentColumns_[i]]);
{
FatalErrorInFunction
<< "No column " << componentColumns_[i] << " in "
<< splitted << endl
<< exit(FatalError);
}
result[i] = readScalar(splitted[componentColumns_[i]]);
} }
return result; return result;
@ -106,38 +115,45 @@ void Foam::Function1Types::CSV<Type>::read()
<< exit(FatalIOError); << exit(FatalIOError);
} }
DynamicList<Tuple2<scalar, Type>> values; const label maxEntry =
max(refColumn_, componentColumns_[findMax(componentColumns_)]);
// skip header label lineNo = 0;
for (label i = 0; i < nHeaderLine_; i++)
// Skip header
for (label i = 0; i < nHeaderLine_; ++i)
{ {
string line; string line;
is.getLine(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()) while (is.good())
{ {
string line; string line;
is.getLine(line); is.getLine(line);
++lineNo;
strings.clear();
label n = 0;
std::size_t pos = 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; if (mergeSeparators_)
while ((pos != std::string::npos) && (n <= nEntries))
{ {
bool found = false; bool found = false;
while (!found) while (!found)
{ {
nPos = line.find(separator_, pos); const auto nPos = line.find(separator_, pos);
if ((nPos != std::string::npos) && (nPos - pos == 0)) if ((nPos != std::string::npos) && (nPos - pos == 0))
{ {
@ -148,52 +164,38 @@ void Foam::Function1Types::CSV<Type>::read()
found = true; found = true;
} }
} }
}
nPos = line.find(separator_, pos); const auto nPos = line.find(separator_, pos);
if (nPos == std::string::npos) if (nPos == std::string::npos)
{ {
splitted.append(line.substr(pos)); strings.append(line.substr(pos));
pos = nPos; pos = nPos;
n++;
} }
else else
{ {
splitted.append(line.substr(pos, nPos - pos)); strings.append(line.substr(pos, nPos - pos));
pos = nPos + 1; pos = nPos + 1;
n++;
}
}
}
else
{
while ((pos != std::string::npos) && (n <= nEntries))
{
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++;
}
} }
} }
if (strings.size() <= 1)
if (splitted.size() <= 1)
{ {
break; break;
} }
scalar x = readScalar(splitted[refColumn_]); if (strings.size() <= maxEntry)
Type value = readValue(splitted); {
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)); values.append(Tuple2<scalar,Type>(x, value));
} }
@ -215,26 +217,11 @@ Foam::Function1Types::CSV<Type>::CSV
TableBase<Type>(entryName, dict), TableBase<Type>(entryName, dict),
nHeaderLine_(dict.get<label>("nHeaderLine")), nHeaderLine_(dict.get<label>("nHeaderLine")),
refColumn_(dict.get<label>("refColumn")), refColumn_(dict.get<label>("refColumn")),
componentColumns_(), componentColumns_(getComponentColumns("componentColumns", dict)),
separator_(dict.getOrDefault<string>("separator", ",")[0]), separator_(dict.getOrDefault<string>("separator", ",")[0]),
mergeSeparators_(dict.get<bool>("mergeSeparators")), mergeSeparators_(dict.get<bool>("mergeSeparators")),
fName_(fName.empty() ? dict.get<fileName>("file") : fName) 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(); read();
TableBase<Type>::check(); TableBase<Type>::check();

View File

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