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:
parent
ee96dba0cf
commit
59ed3ba18d
@ -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,6 +29,82 @@ License
|
||||
#include "csvTableReader.H"
|
||||
#include "fileOperation.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 * * * * * * * * * * * * * * //
|
||||
|
||||
@ -36,70 +113,14 @@ 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])
|
||||
{
|
||||
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()
|
||||
refColumn_(dict.get<label>("timeColumn")),
|
||||
componentColumns_(getComponentColumns("valueColumns", dict)),
|
||||
separator_(dict.getOrDefault<string>("separator", ",")[0])
|
||||
{}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * 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>
|
||||
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 2D table
|
||||
//- Read 1D table
|
||||
virtual void operator()
|
||||
(
|
||||
const fileName&,
|
||||
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>&
|
||||
const fileName& fName,
|
||||
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
|
||||
@ -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>
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
//- Runtime type information
|
||||
//- Declare type-name, virtual type (with debug switch)
|
||||
TypeName("openFoam");
|
||||
|
||||
|
||||
// Generated Methods
|
||||
|
||||
//- Default construct
|
||||
openFoamTableReader() = default;
|
||||
|
||||
//- Destructor
|
||||
virtual ~openFoamTableReader() = default;
|
||||
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Construct from dictionary
|
||||
openFoamTableReader(const dictionary &dict);
|
||||
explicit openFoamTableReader(const dictionary& dict);
|
||||
|
||||
//- Construct and return a copy
|
||||
virtual autoPtr<tableReader<Type>> clone() const
|
||||
@ -78,20 +87,20 @@ public:
|
||||
}
|
||||
|
||||
|
||||
//- Destructor
|
||||
virtual ~openFoamTableReader();
|
||||
// Member Functions
|
||||
|
||||
|
||||
// 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
|
||||
virtual void operator()
|
||||
(
|
||||
const fileName&,
|
||||
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>&
|
||||
const fileName& fName,
|
||||
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& data
|
||||
);
|
||||
};
|
||||
|
||||
@ -106,7 +115,6 @@ public:
|
||||
#include "openFoamTableReader.C"
|
||||
#endif
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#endif
|
||||
|
@ -66,13 +66,6 @@ Foam::tableReader<Type>::tableReader(const dictionary&)
|
||||
{}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
|
||||
|
||||
template<class Type>
|
||||
Foam::tableReader<Type>::~tableReader()
|
||||
{}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * 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.
|
||||
@ -59,12 +60,12 @@ namespace Foam
|
||||
template<class Type>
|
||||
class tableReader
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
//- Runtime type information
|
||||
//- Declare type-name, virtual type (with debug switch)
|
||||
TypeName("tableReader");
|
||||
|
||||
|
||||
// Declare run-time constructor selection table
|
||||
|
||||
declareRunTimeSelectionTable
|
||||
@ -79,6 +80,9 @@ public:
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Default construct
|
||||
tableReader() = default;
|
||||
|
||||
//- Construct from dictionary
|
||||
tableReader(const dictionary& dict);
|
||||
|
||||
@ -93,23 +97,23 @@ public:
|
||||
|
||||
|
||||
//- Destructor
|
||||
virtual ~tableReader();
|
||||
virtual ~tableReader() = default;
|
||||
|
||||
|
||||
// Member functions
|
||||
// Member Functions
|
||||
|
||||
//- Read the table
|
||||
//- Read 1D table
|
||||
virtual void operator()
|
||||
(
|
||||
const fileName&,
|
||||
List<Tuple2<scalar, Type>>&
|
||||
const fileName& fName,
|
||||
List<Tuple2<scalar, Type>>& data
|
||||
) = 0;
|
||||
|
||||
//- Read the 2D table
|
||||
//- Read 2D table
|
||||
virtual void operator()
|
||||
(
|
||||
const fileName&,
|
||||
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>&
|
||||
const fileName& fName,
|
||||
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& tbl
|
||||
) = 0;
|
||||
|
||||
//- Write additional information
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
Loading…
Reference in New Issue
Block a user