Commit 6217691b authored by mattijs's avatar mattijs
Browse files

ENH: csv reading for interpolationTable and writing for sampledSet

parent 0b694909
......@@ -571,6 +571,10 @@ interpolations = interpolations
interpolation = $(interpolations)/interpolation
$(interpolations)/patchToPatchInterpolation/PatchToPatchInterpolationName.C
$(interpolations)/interpolationTable/tableReaders/tableReaders.C
$(interpolations)/interpolationTable/tableReaders/openFoam/openFoamTableReaders.C
$(interpolations)/interpolationTable/tableReaders/csv/csvTableReaders.C
algorithms/MeshWave/MeshWaveName.C
algorithms/MeshWave/FaceCellWaveName.C
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -25,6 +25,7 @@ License
#include "interpolationTable.H"
#include "IFstream.H"
#include "openFoamTableReader.H"
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
......@@ -38,19 +39,19 @@ void Foam::interpolationTable<Type>::readTable()
fName.expand();
// Read data from file
IFstream(fName)() >> *this;
// Check that the data are okay
check();
reader_()(fName, *this);
if (this->empty())
{
FatalErrorIn
(
"Foam::interpolationTable<Type>::readTable()"
) << "table is empty" << nl
) << "table read from " << fName << " is empty" << nl
<< exit(FatalError);
}
// Check that the data are okay
check();
}
......@@ -61,7 +62,8 @@ Foam::interpolationTable<Type>::interpolationTable()
:
List<Tuple2<scalar, Type> >(),
boundsHandling_(interpolationTable::WARN),
fileName_("fileNameIsUndefined")
fileName_("fileNameIsUndefined"),
reader_(NULL)
{}
......@@ -75,7 +77,8 @@ Foam::interpolationTable<Type>::interpolationTable
:
List<Tuple2<scalar, Type> >(values),
boundsHandling_(bounds),
fileName_(fName)
fileName_(fName),
reader_(NULL)
{}
......@@ -84,7 +87,8 @@ Foam::interpolationTable<Type>::interpolationTable(const fileName& fName)
:
List<Tuple2<scalar, Type> >(),
boundsHandling_(interpolationTable::WARN),
fileName_(fName)
fileName_(fName),
reader_(new openFoamTableReader<Type>())
{
readTable();
}
......@@ -95,7 +99,8 @@ Foam::interpolationTable<Type>::interpolationTable(const dictionary& dict)
:
List<Tuple2<scalar, Type> >(),
boundsHandling_(wordToBoundsHandling(dict.lookup("outOfBounds"))),
fileName_(dict.lookup("fileName"))
fileName_(dict.lookup("fileName")),
reader_(tableReader<Type>::New(dict))
{
readTable();
}
......@@ -109,7 +114,8 @@ Foam::interpolationTable<Type>::interpolationTable
:
List<Tuple2<scalar, Type> >(interpTable),
boundsHandling_(interpTable.boundsHandling_),
fileName_(interpTable.fileName_)
fileName_(interpTable.fileName_),
reader_(interpTable.reader_) // note: steals reader. Used in write().
{}
......@@ -233,6 +239,10 @@ void Foam::interpolationTable<Type>::write(Ostream& os) const
<< fileName_ << token::END_STATEMENT << nl;
os.writeKeyword("outOfBounds")
<< boundsHandlingToWord(boundsHandling_) << token::END_STATEMENT << nl;
if (reader_.valid())
{
reader_->write(os);
}
}
......
......@@ -34,6 +34,19 @@ Description
If \a REPEAT is chosen for the out-of-bounds handling, the final time
value is treated as being equivalent to time=0 for the following periods.
The construct from dictionary reads a filename from a dictionary and
has an optional readerType. Default is to read OpenFOAM format. The only
other format is csv (comma separated values):
Read csv format:
readerType csv;
fileName "$FOAM_CASE/constant/p0vsTime.csv";
hasHeaderLine true; // skip first line
timeColumn 0; // time is in column 0
valueColumns (1); // value starts in column 1
Note
- Accessing an empty list results in an error.
- Accessing a list with a single element always returns the same value.
......@@ -49,6 +62,9 @@ SourceFiles
#include "List.H"
#include "Tuple2.H"
#include "tableReader.H"
#include "autoPtr.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
......@@ -87,6 +103,8 @@ private:
//- File name
fileName fileName_;
//- the actual reader
autoPtr<tableReader<Type> > reader_;
// Private Member Functions
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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 "csvTableReader.H"
#include "IFstream.H"
#include "DynamicList.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::csvTableReader<Type>::csvTableReader(const dictionary& dict)
:
tableReader<Type>(dict),
headerLine_(readBool(dict.lookup("hasHeaderLine"))),
timeColumn_(readLabel(dict.lookup("timeColumn"))),
componentColumns_(dict.lookup("valueColumns")),
separator_(dict.lookupOrDefault<string>("separator", string(","))[0])
{
if (componentColumns_.size() != pTraits<Type>::nComponents)
{
FatalErrorIn("csvTableReader<Type>::csvTableReader(const dictionary&)")
<< componentColumns_ << " does not have the expected length "
<< pTraits<Type>::nComponents << endl
<< exit(FatalError);
}
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Type>
Foam::csvTableReader<Type>::~csvTableReader()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
namespace Foam
{
// doesn't recognize specialization otherwise
template<>
scalar csvTableReader<scalar>::readValue(const List<string>& splitted)
{
if (componentColumns_[0] >= splitted.size())
{
FatalErrorIn
(
"csvTableReader<scalar>::readValue(const List<string>&)"
) << "No column " << componentColumns_[0] << " in "
<< splitted << endl
<< exit(FatalError);
}
return readScalar(IStringStream(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())
{
FatalErrorIn
(
"csvTableReader<Type>::readValue(const List<string>&)"
) << "No column " << componentColumns_[i] << " in "
<< splitted << endl
<< exit(FatalError);
}
result[i] = readScalar
(
IStringStream(splitted[componentColumns_[i]])()
);
}
return result;
}
}
template<class Type>
void Foam::csvTableReader<Type>::operator()
(
const fileName& fName,
List<Tuple2<scalar, Type> >& data
)
{
IFstream in(fName);
DynamicList<Tuple2<scalar, Type> > values;
// Skip header
if (headerLine_)
{
string line;
in.getLine(line);
}
while (in.good())
{
string line;
in.getLine(line);
DynamicList<string> splitted;
std::size_t pos = 0;
while (pos != std::string::npos)
{
std::size_t nPos = line.find(separator_, pos);
if (nPos == std::string::npos)
{
splitted.append(line.substr(pos));
pos=nPos;
}
else
{
splitted.append(line.substr(pos, nPos-pos));
pos=nPos+1;
}
}
if (splitted.size() <= 1)
{
break;
}
scalar time = readScalar(IStringStream(splitted[timeColumn_])());
Type value = readValue(splitted);
values.append(Tuple2<scalar,Type>(time, value));
}
data.transfer(values);
}
template<class Type>
void Foam::csvTableReader<Type>::write(Ostream& os) const
{
tableReader<Type>::write(os);
os.writeKeyword("hasHeaderLine")
<< headerLine_ << token::END_STATEMENT << nl;
os.writeKeyword("timeColumn")
<< timeColumn_ << token::END_STATEMENT << nl;
os.writeKeyword("valueColumns")
<< componentColumns_ << token::END_STATEMENT << nl;
os.writeKeyword("separator")
<< string(separator_) << token::END_STATEMENT << nl;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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/>.
Class
Foam::tableReader
Description
Reads an interpolation table from a file - CSV-format
SourceFiles
tableReader.C
\*---------------------------------------------------------------------------*/
#ifndef csvTableReader_H
#define csvTableReader_H
#include "tableReader.H"
#include "labelList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class csvTableReader Declaration
\*---------------------------------------------------------------------------*/
template<class Type>
class csvTableReader
:
public tableReader<Type>
{
//- does the file have a header line?
const bool headerLine_;
//- column of the time
const label timeColumn_;
//- 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_;
public:
//- Runtime type information
TypeName("csv");
// Constructors
//- Construct from dictionary
csvTableReader(const dictionary& dict);
//- Construct and return a copy
virtual autoPtr<tableReader<Type> > clone() const
{
return autoPtr<tableReader<Type> >
(
new csvTableReader<Type>
(
*this
)
);
}
//- Destructor
virtual ~csvTableReader();
// Member Functions
//- Read the table
virtual void operator()(const fileName&, List<Tuple2<scalar, Type> >&);
//- write the remaining parameters
virtual void write(Ostream& os) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
# include "csvTableReader.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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 "csvTableReader.H"
#include "tableReaders.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
makeTableReaders(csvTableReader);
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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 "openFoamTableReader.H"
#include "IFstream.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::openFoamTableReader<Type>::openFoamTableReader(const dictionary& dict)
:
tableReader<Type>(dict)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Type>
Foam::openFoamTableReader<Type>::~openFoamTableReader()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
void Foam::openFoamTableReader<Type>::operator()
(
const fileName& fName,
List<Tuple2<scalar, Type> >& data
)
{
// Read data from file
IFstream(fName)() >> data;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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/>.
Class
Foam::tableReader
Description
Reads an interpolation table from a file - OpenFOAM-format
SourceFiles
tableReader.C
\*---------------------------------------------------------------------------*/
#ifndef openFoamTableReader_H
#define openFoamTableReader_H
#include "tableReader.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //