Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00039
00040 defineTypeNameAndDebug(graph::writer, 0);
00041 defineRunTimeSelectionTable(graph::writer, word);
00042
00043
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
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 }
00248
00249