Merge branch 'upstream/OpenFOAM' into master
[freefoam.git] / src / OpenFOAM / graph / graph.C
blob1c0a501b91d8a772985ecd745ab43cf4179ef0bf
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
27 \*---------------------------------------------------------------------------*/
29 #include "graph.H"
30 #include <OpenFOAM/OFstream.H>
31 #include <OpenFOAM/IOmanip.H>
32 #include <OpenFOAM/Pair.H>
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 namespace Foam
39 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
41 defineTypeNameAndDebug(graph::writer, 0);
42 defineRunTimeSelectionTable(graph::writer, word);
44 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
46 void graph::readCurves(Istream& is)
48     List<xy> xyData(is);
50     x_.setSize(xyData.size());
51     scalarField y(xyData.size());
53     forAll (xyData, i)
54     {
55         x_[i] = xyData[i].x_;
56         y[i] = xyData[i].y_;
57     }
59     insert(yName_, new curve(yName_, curve::curveStyle::CONTINUOUS, y));
63 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
65 graph::graph
67     const string& title,
68     const string& xName,
69     const string& yName,
70     const scalarField& x
73     title_(title),
74     xName_(xName),
75     yName_(yName),
76     x_(x)
80 graph::graph
82     const string& title,
83     const string& xName,
84     const string& yName,
85     const scalarField& x,
86     const scalarField& y
89     title_(title),
90     xName_(xName),
91     yName_(yName),
92     x_(x)
94     insert(yName, new curve(yName, curve::curveStyle::CONTINUOUS, y));
98 graph::graph
100     const string& title,
101     const string& xName,
102     const string& yName,
103     Istream& is
106     title_(title),
107     xName_(xName),
108     yName_(yName)
110     readCurves(is);
114 graph::graph(Istream& is)
116     title_(is),
117     xName_(is),
118     yName_(is)
120     readCurves(is);
124 const scalarField& graph::y() const
126     if (size() != 1)
127     {
128         FatalErrorIn("const scalarField& graph::y() const")
129             << "y field requested for graph containing " << size()
130             << "ys" << exit(FatalError);
131     }
133     return *begin()();
136 scalarField& graph::y()
138     if (size() != 1)
139     {
140         FatalErrorIn("scalarField& graph::y()")
141             << "y field requested for graph containing " << size()
142             << "ys" << exit(FatalError);
143     }
145     return *begin()();
149 autoPtr<graph::writer> graph::writer::New(const word& graphFormat)
151     if (!wordConstructorTablePtr_)
152     {
153         FatalErrorIn
154         (
155             "graph::writer::New(const word&)"
156         )   << "Graph writer table is empty"
157             << exit(FatalError);
158     }
160     wordConstructorTable::iterator cstrIter =
161         wordConstructorTablePtr_->find(graphFormat);
163     if (cstrIter == wordConstructorTablePtr_->end())
164     {
165         FatalErrorIn
166         (
167             "graph::writer::New(const word&)"
168         )   << "Unknown graph format " << graphFormat
169             << endl << endl
170             << "Valid graph formats are : " << endl
171             << wordConstructorTablePtr_->toc()
172             << exit(FatalError);
173     }
175     return autoPtr<graph::writer>(cstrIter()());
179 void graph::writer::writeXY
181     const scalarField& x,
182     const scalarField& y,
183     Ostream& os
184 ) const
186     forAll(x, xi)
187     {
188         os << setw(10) << x[xi] << token::SPACE << setw(10) << y[xi]<< endl;
189     }
193 void graph::writeTable(Ostream& os) const
195     forAll(x_, xi)
196     {
197         os << setw(10) << x_[xi];
199         for
200         (
201             graph::const_iterator iter = begin();
202             iter != end();
203             ++iter
204         )
205         {
206             os << token::SPACE << setw(10) << (*iter())[xi];
207         }
208         os << endl;
209     }
213 void graph::write(Ostream& os, const word& format) const
215     writer::New(format)().write(*this, os);
219 void graph::write(const fileName& fName, const word& format) const
221     autoPtr<writer> graphWriter(writer::New(format));
223     OFstream graphFile(fName + '.' + graphWriter().ext());
225     if (graphFile.good())
226     {
227         write(graphFile, format);
228     }
229     else
230     {
231         WarningIn("graph::write(const word& format, const fileName& dir)")
232             << "Could not open graph file " << graphFile.name()
233             << endl;
234     }
238 Ostream& operator<<(Ostream& os, const graph& g)
240     g.writeTable(os);
241     os.check("Ostream& operator<<(Ostream&, const graph&)");
242     return os;
246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
248 } // End namespace Foam
250 // ************************ vim: set sw=4 sts=4 et: ************************ //