OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

graph.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "graph.H"
00030 #include "OFstream.H"
00031 #include "IOmanip.H"
00032 #include "Pair.H"
00033 
00034 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00035 
00036 namespace Foam
00037 {
00038 
00039 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00040 
00041 defineTypeNameAndDebug(graph::writer, 0);
00042 defineRunTimeSelectionTable(graph::writer, word);
00043 
00044 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00045 
00046 void graph::readCurves(Istream& is)
00047 {
00048     List<xy> xyData(is);
00049 
00050     x_.setSize(xyData.size());
00051     scalarField y(xyData.size());
00052 
00053     forAll (xyData, i)
00054     {
00055         x_[i] = xyData[i].x_;
00056         y[i] = xyData[i].y_;
00057     }
00058 
00059     insert(yName_, new curve(yName_, curve::curveStyle::CONTINUOUS, y));
00060 }
00061 
00062 
00063 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00064 
00065 graph::graph
00066 (
00067     const string& title,
00068     const string& xName,
00069     const string& yName,
00070     const scalarField& x
00071 )
00072 :
00073     title_(title),
00074     xName_(xName),
00075     yName_(yName),
00076     x_(x)
00077 {}
00078 
00079 
00080 graph::graph
00081 (
00082     const string& title,
00083     const string& xName,
00084     const string& yName,
00085     const scalarField& x,
00086     const scalarField& y
00087 )
00088 :
00089     title_(title),
00090     xName_(xName),
00091     yName_(yName),
00092     x_(x)
00093 {
00094     insert(yName, new curve(yName, curve::curveStyle::CONTINUOUS, y));
00095 }
00096 
00097 
00098 graph::graph
00099 (
00100     const string& title,
00101     const string& xName,
00102     const string& yName,
00103     Istream& is
00104 )
00105 :
00106     title_(title),
00107     xName_(xName),
00108     yName_(yName)
00109 {
00110     readCurves(is);
00111 }
00112 
00113 
00114 graph::graph(Istream& is)
00115 :
00116     title_(is),
00117     xName_(is),
00118     yName_(is)
00119 {
00120     readCurves(is);
00121 }
00122 
00123 
00124 const scalarField& graph::y() const
00125 {
00126     if (size() != 1)
00127     {
00128         FatalErrorIn("const scalarField& graph::y() const")
00129             << "y field requested for graph containing " << size()
00130             << "ys" << exit(FatalError);
00131     }
00132 
00133     return *begin()();
00134 }
00135 
00136 scalarField& graph::y()
00137 {
00138     if (size() != 1)
00139     {
00140         FatalErrorIn("scalarField& graph::y()")
00141             << "y field requested for graph containing " << size()
00142             << "ys" << exit(FatalError);
00143     }
00144 
00145     return *begin()();
00146 }
00147 
00148 
00149 autoPtr<graph::writer> graph::writer::New(const word& graphFormat)
00150 {
00151     if (!wordConstructorTablePtr_)
00152     {
00153         FatalErrorIn
00154         (
00155             "graph::writer::New(const word&)"
00156         )   << "Graph writer table is empty"
00157             << exit(FatalError);
00158     }
00159 
00160     wordConstructorTable::iterator cstrIter =
00161         wordConstructorTablePtr_->find(graphFormat);
00162 
00163     if (cstrIter == wordConstructorTablePtr_->end())
00164     {
00165         FatalErrorIn
00166         (
00167             "graph::writer::New(const word&)"
00168         )   << "Unknown graph format " << graphFormat
00169             << endl << endl
00170             << "Valid graph formats are : " << endl
00171             << wordConstructorTablePtr_->toc()
00172             << exit(FatalError);
00173     }
00174 
00175     return autoPtr<graph::writer>(cstrIter()());
00176 }
00177 
00178 
00179 void graph::writer::writeXY
00180 (
00181     const scalarField& x,
00182     const scalarField& y,
00183     Ostream& os
00184 ) const
00185 {
00186     forAll(x, xi)
00187     {
00188         os << setw(10) << x[xi] << token::SPACE << setw(10) << y[xi]<< endl;
00189     }
00190 }
00191 
00192 
00193 void graph::writeTable(Ostream& os) const
00194 {
00195     forAll(x_, xi)
00196     {
00197         os << setw(10) << x_[xi];
00198 
00199         for
00200         (
00201             graph::const_iterator iter = begin();
00202             iter != end();
00203             ++iter
00204         )
00205         {
00206             os << token::SPACE << setw(10) << (*iter())[xi];
00207         }
00208         os << endl;
00209     }
00210 }
00211 
00212 
00213 void graph::write(Ostream& os, const word& format) const
00214 {
00215     writer::New(format)().write(*this, os);
00216 }
00217 
00218 
00219 void graph::write(const fileName& fName, const word& format) const
00220 {
00221     autoPtr<writer> graphWriter(writer::New(format));
00222 
00223     OFstream graphFile(fName + '.' + graphWriter().ext());
00224 
00225     if (graphFile.good())
00226     {
00227         write(graphFile, format);
00228     }
00229     else
00230     {
00231         WarningIn("graph::write(const word& format, const fileName& dir)")
00232             << "Could not open graph file " << graphFile.name()
00233             << endl;
00234     }
00235 }
00236 
00237 
00238 Ostream& operator<<(Ostream& os, const graph& g)
00239 {
00240     g.writeTable(os);
00241     os.check("Ostream& operator<<(Ostream&, const graph&)");
00242     return os;
00243 }
00244 
00245 
00246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00247 
00248 } // End namespace Foam
00249 
00250 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd