FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Description
00025 
00026 \*---------------------------------------------------------------------------*/
00027 
00028 #include "graph.H"
00029 #include <OpenFOAM/OFstream.H>
00030 #include <OpenFOAM/IOmanip.H>
00031 #include <OpenFOAM/Pair.H>
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 defineTypeNameAndDebug(graph::writer, 0);
00041 defineRunTimeSelectionTable(graph::writer, word);
00042 
00043 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00044 
00045 void graph::readCurves(Istream& is)
00046 {
00047     List<xy> xyData(is);
00048 
00049     x_.setSize(xyData.size());
00050     scalarField y(xyData.size());
00051 
00052     forAll (xyData, i)
00053     {
00054         x_[i] = xyData[i].x_;
00055         y[i] = xyData[i].y_;
00056     }
00057 
00058     insert(yName_, new curve(yName_, curve::curveStyle::CONTINUOUS, y));
00059 }
00060 
00061 
00062 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00063 
00064 graph::graph
00065 (
00066     const string& title,
00067     const string& xName,
00068     const string& yName,
00069     const scalarField& x
00070 )
00071 :
00072     title_(title),
00073     xName_(xName),
00074     yName_(yName),
00075     x_(x)
00076 {}
00077 
00078 
00079 graph::graph
00080 (
00081     const string& title,
00082     const string& xName,
00083     const string& yName,
00084     const scalarField& x,
00085     const scalarField& y
00086 )
00087 :
00088     title_(title),
00089     xName_(xName),
00090     yName_(yName),
00091     x_(x)
00092 {
00093     insert(yName, new curve(yName, curve::curveStyle::CONTINUOUS, y));
00094 }
00095 
00096 
00097 graph::graph
00098 (
00099     const string& title,
00100     const string& xName,
00101     const string& yName,
00102     Istream& is
00103 )
00104 :
00105     title_(title),
00106     xName_(xName),
00107     yName_(yName)
00108 {
00109     readCurves(is);
00110 }
00111 
00112 
00113 graph::graph(Istream& is)
00114 :
00115     title_(is),
00116     xName_(is),
00117     yName_(is)
00118 {
00119     readCurves(is);
00120 }
00121 
00122 
00123 const scalarField& graph::y() const
00124 {
00125     if (size() != 1)
00126     {
00127         FatalErrorIn("const scalarField& graph::y() const")
00128             << "y field requested for graph containing " << size()
00129             << "ys" << exit(FatalError);
00130     }
00131 
00132     return *begin()();
00133 }
00134 
00135 scalarField& graph::y()
00136 {
00137     if (size() != 1)
00138     {
00139         FatalErrorIn("scalarField& graph::y()")
00140             << "y field requested for graph containing " << size()
00141             << "ys" << exit(FatalError);
00142     }
00143 
00144     return *begin()();
00145 }
00146 
00147 
00148 autoPtr<graph::writer> graph::writer::New(const word& graphFormat)
00149 {
00150     if (!wordConstructorTablePtr_)
00151     {
00152         FatalErrorIn
00153         (
00154             "graph::writer::New(const word&)"
00155         )   << "Graph writer table is empty"
00156             << exit(FatalError);
00157     }
00158 
00159     wordConstructorTable::iterator cstrIter =
00160         wordConstructorTablePtr_->find(graphFormat);
00161 
00162     if (cstrIter == wordConstructorTablePtr_->end())
00163     {
00164         FatalErrorIn
00165         (
00166             "graph::writer::New(const word&)"
00167         )   << "Unknown graph format " << graphFormat
00168             << endl << endl
00169             << "Valid graph formats are : " << endl
00170             << wordConstructorTablePtr_->sortedToc()
00171             << exit(FatalError);
00172     }
00173 
00174     return autoPtr<graph::writer>(cstrIter()());
00175 }
00176 
00177 
00178 void graph::writer::writeXY
00179 (
00180     const scalarField& x,
00181     const scalarField& y,
00182     Ostream& os
00183 ) const
00184 {
00185     forAll(x, xi)
00186     {
00187         os << setw(10) << x[xi] << token::SPACE << setw(10) << y[xi]<< endl;
00188     }
00189 }
00190 
00191 
00192 void graph::writeTable(Ostream& os) const
00193 {
00194     forAll(x_, xi)
00195     {
00196         os << setw(10) << x_[xi];
00197 
00198         for
00199         (
00200             graph::const_iterator iter = begin();
00201             iter != end();
00202             ++iter
00203         )
00204         {
00205             os << token::SPACE << setw(10) << (*iter())[xi];
00206         }
00207         os << endl;
00208     }
00209 }
00210 
00211 
00212 void graph::write(Ostream& os, const word& format) const
00213 {
00214     writer::New(format)().write(*this, os);
00215 }
00216 
00217 
00218 void graph::write(const fileName& fName, const word& format) const
00219 {
00220     autoPtr<writer> graphWriter(writer::New(format));
00221 
00222     OFstream graphFile(fName + '.' + graphWriter().ext());
00223 
00224     if (graphFile.good())
00225     {
00226         write(graphFile, format);
00227     }
00228     else
00229     {
00230         WarningIn("graph::write(const word& format, const fileName& dir)")
00231             << "Could not open graph file " << graphFile.name()
00232             << endl;
00233     }
00234 }
00235 
00236 
00237 Ostream& operator<<(Ostream& os, const graph& g)
00238 {
00239     g.writeTable(os);
00240     os.check("Ostream& operator<<(Ostream&, const graph&)");
00241     return os;
00242 }
00243 
00244 
00245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00246 
00247 } // End namespace Foam
00248 
00249 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines