Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / OpenFOAM / graph / graph.C
blob7dbefcb424a79f022c613d2d6e398df0d57cddb4
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2011 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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "graph.H"
27 #include "OFstream.H"
28 #include "IOmanip.H"
29 #include "Pair.H"
30 #include "OSspecific.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 namespace Foam
36     defineTypeNameAndDebug(graph::writer, 0);
37     defineRunTimeSelectionTable(graph::writer, word);
41 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
43 void Foam::graph::readCurves(Istream& is)
45     List<xy> xyData(is);
47     x_.setSize(xyData.size());
48     scalarField y(xyData.size());
50     forAll(xyData, i)
51     {
52         x_[i] = xyData[i].x_;
53         y[i] = xyData[i].y_;
54     }
56     insert(yName_, new curve(yName_, curve::curveStyle::CONTINUOUS, y));
60 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
62 Foam::graph::graph
64     const string& title,
65     const string& xName,
66     const string& yName,
67     const scalarField& x
70     title_(title),
71     xName_(xName),
72     yName_(yName),
73     x_(x)
77 Foam::graph::graph
79     const string& title,
80     const string& xName,
81     const string& yName,
82     const scalarField& x,
83     const scalarField& y
86     title_(title),
87     xName_(xName),
88     yName_(yName),
89     x_(x)
91     insert(yName, new curve(yName, curve::curveStyle::CONTINUOUS, y));
95 Foam::graph::graph
97     const string& title,
98     const string& xName,
99     const string& yName,
100     Istream& is
103     title_(title),
104     xName_(xName),
105     yName_(yName)
107     readCurves(is);
111 Foam::graph::graph(Istream& is)
113     title_(is),
114     xName_(is),
115     yName_(is)
117     readCurves(is);
121 const Foam::scalarField& Foam::graph::y() const
123     if (size() != 1)
124     {
125         FatalErrorIn("const scalarField& graph::y() const")
126             << "y field requested for graph containing " << size()
127             << "ys" << exit(FatalError);
128     }
130     return *begin()();
134 Foam::scalarField& Foam::graph::y()
136     if (size() != 1)
137     {
138         FatalErrorIn("scalarField& graph::y()")
139             << "y field requested for graph containing " << size()
140             << "ys" << exit(FatalError);
141     }
143     return *begin()();
147 Foam::autoPtr<Foam::graph::writer> Foam::graph::writer::New
149     const word& graphFormat
152     if (!wordConstructorTablePtr_)
153     {
154         FatalErrorIn
155         (
156             "graph::writer::New(const word&)"
157         )   << "Graph writer table is empty"
158             << exit(FatalError);
159     }
161     wordConstructorTable::iterator cstrIter =
162         wordConstructorTablePtr_->find(graphFormat);
164     if (cstrIter == wordConstructorTablePtr_->end())
165     {
166         FatalErrorIn
167         (
168             "graph::writer::New(const word&)"
169         )   << "Unknown graph format " << graphFormat
170             << endl << endl
171             << "Valid graph formats are : " << endl
172             << wordConstructorTablePtr_->sortedToc()
173             << exit(FatalError);
174     }
176     return autoPtr<graph::writer>(cstrIter()());
180 void Foam::graph::writer::writeXY
182     const scalarField& x,
183     const scalarField& y,
184     Ostream& os
185 ) const
187     forAll(x, xi)
188     {
189         os << setw(10) << x[xi] << token::SPACE << setw(10) << y[xi]<< endl;
190     }
194 void Foam::graph::writeTable(Ostream& os) const
196     forAll(x_, xi)
197     {
198         os  << setw(10) << x_[xi];
200         forAllConstIter(graph, *this, iter)
201         {
202             os  << token::SPACE << setw(10) << (*iter())[xi];
203         }
204         os  << endl;
205     }
209 void Foam::graph::write(Ostream& os, const word& format) const
211     writer::New(format)().write(*this, os);
215 void Foam::graph::write(const fileName& pName, const word& format) const
217     autoPtr<writer> graphWriter(writer::New(format));
219     OFstream graphFile(pName + '.' + graphWriter().ext());
221     if (graphFile.good())
222     {
223         write(graphFile, format);
224     }
225     else
226     {
227         WarningIn("graph::write(const word& format, const fileName& dir)")
228             << "Could not open graph file " << graphFile.name()
229             << endl;
230     }
234 void Foam::graph::write
236     const fileName& path,
237     const word& name,
238     const word& format
239 ) const
241     mkDir(path);
242     write(path/name, format);
246 Foam::Ostream& Foam::operator<<(Ostream& os, const graph& g)
248     g.writeTable(os);
249     os.check("Ostream& operator<<(Ostream&, const graph&)");
250     return os;
254 // ************************************************************************* //