FreeFOAM The Cross-Platform CFD Toolkit
graph.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "graph.H"
29 #include <OpenFOAM/OFstream.H>
30 #include <OpenFOAM/IOmanip.H>
31 #include <OpenFOAM/Pair.H>
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 
40 defineTypeNameAndDebug(graph::writer, 0);
41 defineRunTimeSelectionTable(graph::writer, word);
42 
43 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
44 
45 void graph::readCurves(Istream& is)
46 {
47  List<xy> xyData(is);
48 
49  x_.setSize(xyData.size());
50  scalarField y(xyData.size());
51 
52  forAll (xyData, i)
53  {
54  x_[i] = xyData[i].x_;
55  y[i] = xyData[i].y_;
56  }
57 
58  insert(yName_, new curve(yName_, curve::curveStyle::CONTINUOUS, y));
59 }
60 
61 
62 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
63 
65 (
66  const string& title,
67  const string& xName,
68  const string& yName,
69  const scalarField& x
70 )
71 :
72  title_(title),
73  xName_(xName),
74  yName_(yName),
75  x_(x)
76 {}
77 
78 
80 (
81  const string& title,
82  const string& xName,
83  const string& yName,
84  const scalarField& x,
85  const scalarField& y
86 )
87 :
88  title_(title),
89  xName_(xName),
90  yName_(yName),
91  x_(x)
92 {
93  insert(yName, new curve(yName, curve::curveStyle::CONTINUOUS, y));
94 }
95 
96 
98 (
99  const string& title,
100  const string& xName,
101  const string& yName,
102  Istream& is
103 )
104 :
105  title_(title),
106  xName_(xName),
107  yName_(yName)
108 {
109  readCurves(is);
110 }
111 
112 
114 :
115  title_(is),
116  xName_(is),
117  yName_(is)
118 {
119  readCurves(is);
120 }
121 
122 
123 const scalarField& graph::y() const
124 {
125  if (size() != 1)
126  {
127  FatalErrorIn("const scalarField& graph::y() const")
128  << "y field requested for graph containing " << size()
129  << "ys" << exit(FatalError);
130  }
131 
132  return *begin()();
133 }
134 
136 {
137  if (size() != 1)
138  {
139  FatalErrorIn("scalarField& graph::y()")
140  << "y field requested for graph containing " << size()
141  << "ys" << exit(FatalError);
142  }
143 
144  return *begin()();
145 }
146 
147 
149 {
150  if (!wordConstructorTablePtr_)
151  {
153  (
154  "graph::writer::New(const word&)"
155  ) << "Graph writer table is empty"
156  << exit(FatalError);
157  }
158 
159  wordConstructorTable::iterator cstrIter =
160  wordConstructorTablePtr_->find(graphFormat);
161 
162  if (cstrIter == wordConstructorTablePtr_->end())
163  {
165  (
166  "graph::writer::New(const word&)"
167  ) << "Unknown graph format " << graphFormat
168  << endl << endl
169  << "Valid graph formats are : " << endl
170  << wordConstructorTablePtr_->sortedToc()
171  << exit(FatalError);
172  }
173 
174  return autoPtr<graph::writer>(cstrIter()());
175 }
176 
177 
179 (
180  const scalarField& x,
181  const scalarField& y,
182  Ostream& os
183 ) const
184 {
185  forAll(x, xi)
186  {
187  os << setw(10) << x[xi] << token::SPACE << setw(10) << y[xi]<< endl;
188  }
189 }
190 
191 
192 void graph::writeTable(Ostream& os) const
193 {
194  forAll(x_, xi)
195  {
196  os << setw(10) << x_[xi];
197 
198  for
199  (
200  graph::const_iterator iter = begin();
201  iter != end();
202  ++iter
203  )
204  {
205  os << token::SPACE << setw(10) << (*iter())[xi];
206  }
207  os << endl;
208  }
209 }
210 
211 
212 void graph::write(Ostream& os, const word& format) const
213 {
214  writer::New(format)().write(*this, os);
215 }
216 
217 
218 void graph::write(const fileName& fName, const word& format) const
219 {
221 
222  OFstream graphFile(fName + '.' + graphWriter().ext());
223 
224  if (graphFile.good())
225  {
226  write(graphFile, format);
227  }
228  else
229  {
230  WarningIn("graph::write(const word& format, const fileName& dir)")
231  << "Could not open graph file " << graphFile.name()
232  << endl;
233  }
234 }
235 
236 
238 {
239  g.writeTable(os);
240  os.check("Ostream& operator<<(Ostream&, const graph&)");
241  return os;
242 }
243 
244 
245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
246 
247 } // End namespace Foam
248 
249 // ************************ vim: set sw=4 sts=4 et: ************************ //