From 5763b7e4041a1f4da6e49b027e55280afce40ba1 Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 21 Nov 2011 17:12:59 +0000 Subject: [PATCH] ENH: Added CSV table DataEntry --- .../primitives/functions/DataEntry/CSV/CSV.C | 285 ++++++++++++++++++ .../primitives/functions/DataEntry/CSV/CSV.H | 174 +++++++++++ .../functions/DataEntry/CSV/CSVIO.C | 85 ++++++ .../functions/DataEntry/Constant/Constant.C | 5 +- .../functions/DataEntry/Constant/Constant.H | 2 +- .../functions/DataEntry/DataEntry/DataEntry.H | 4 +- .../DataEntry/DataEntry/DataEntryNew.C | 3 +- .../functions/DataEntry/Table/Table.C | 9 +- .../functions/DataEntry/Table/Table.H | 2 +- .../functions/DataEntry/makeDataEntries.C | 24 +- .../DataEntry/polynomial/polynomial.C | 9 +- .../DataEntry/polynomial/polynomial.H | 2 +- 12 files changed, 591 insertions(+), 13 deletions(-) create mode 100644 src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C create mode 100644 src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.H create mode 100644 src/OpenFOAM/primitives/functions/DataEntry/CSV/CSVIO.C diff --git a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C new file mode 100644 index 0000000000..438c10f60b --- /dev/null +++ b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C @@ -0,0 +1,285 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\/ 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 . + +\*---------------------------------------------------------------------------*/ + +#include "CSV.H" +#include "DynamicList.H" +#include "IFstream.H" +//#include "IStringStream.H" + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +namespace Foam +{ + // doesn't recognize specialization otherwise + template<> + scalar CSV::readValue(const List& splitted) + { + if (componentColumns_[0] >= splitted.size()) + { + FatalErrorIn("CSV::readValue(const List&)") + << "No column " << componentColumns_[0] << " in " + << splitted << endl + << exit(FatalError); + } + + return readScalar(IStringStream(splitted[componentColumns_[0]])()); + } + + + template + Type CSV::readValue(const List& splitted) + { + Type result; + + for (label i = 0; i < pTraits::nComponents; i++) + { + if (componentColumns_[i] >= splitted.size()) + { + FatalErrorIn("CSV::readValue(const List&)") + << "No column " << componentColumns_[i] << " in " + << splitted << endl + << exit(FatalError); + } + + result[i] = + readScalar(IStringStream(splitted[componentColumns_[i]])()); + } + + return result; + } +} + + +template +void Foam::CSV::read() +{ + IFstream is(fName_.expand()); + + DynamicList > values; + + // skip header + if (headerLine_) + { + string line; + is.getLine(line); + } + + // read data + while (is.good()) + { + string line; + is.getLine(line); + + DynamicList 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 x = readScalar(IStringStream(splitted[refColumn_])()); + Type value = readValue(splitted); + + values.append(Tuple2(x, value)); + } + + table_.transfer(values); +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template +Foam::CSV::CSV(const word& entryName, const dictionary& dict) +: + DataEntry(entryName), + headerLine_(false), + refColumn_(0), + componentColumns_(), + separator_(string(",")[0]), + fName_("none"), + table_() +{ + const dictionary coeffs(dict.subDict(type() + "Coeffs")); + coeffs.lookup("hasHeaderLine") >> headerLine_; + coeffs.lookup("refColumn") >> refColumn_; + coeffs.lookup("componentColumns") >> componentColumns_; + coeffs.readIfPresent("separator", string(separator_)[0]); + coeffs.lookup("fileName") >> fName_; + + if (componentColumns_.size() != pTraits::nComponents) + { + FatalErrorIn("Foam::CSV::CSV(const word&, Istream&)") + << componentColumns_ << " does not have the expected length " + << pTraits::nComponents << endl + << exit(FatalError); + } + + read(); + + if (!table_.size()) + { + FatalErrorIn("Foam::CSV::CSV(const Istream&)") + << "CSV for entry " << this->name_ << " is invalid (empty)" + << nl << exit(FatalError); + } +} + + +template +Foam::CSV::CSV(const CSV& tbl) +: + DataEntry(tbl), + headerLine_(tbl.headerLine_), + refColumn_(tbl.refColumn_), + componentColumns_(tbl.componentColumns_), + separator_(tbl.separator_), + fName_(tbl.fName_), + table_(tbl.table_) +{} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +template +Foam::CSV::~CSV() +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template +Type Foam::CSV::value(const scalar x) const +{ + // Return zero if out of bounds + if (x < table_[0].first() || x > table_.last().first()) + { + return pTraits::zero; + } + + // Find i such that x(i) < x < x(i+1) + label i = 0; + while ((table_[i+1].first() < x) && (i+1 < table_.size())) + { + i++; + } + + // Linear interpolation to find value. Note constructor needed for + // CSV