Cantera  2.0
PrintCtrl.h
Go to the documentation of this file.
1 /**
2  * @file PrintCtrl.h
3  * Declarations for a simple class that augments the streams printing capabilities
4  * (see \ref Cantera::PrintCtrl).
5  */
6 /*
7  * Copyright 2004 Sandia Corporation. Under the terms of Contract
8  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
9  * retains certain rights in this software.
10  * See file License.txt for licensing information.
11  */
12 
13 #ifndef CT_PRINTCTRL_H
14 #define CT_PRINTCTRL_H
15 
16 #include <iostream>
17 
18 namespace Cantera
19 {
20 
21 //! This class provides some printing and cropping utilities
22 /*!
23  * The class is used to provide some formatting options for
24  * printing out real numbers to files and to standard output.
25  * Specifically, it can make sure that a max and min field
26  * width is honored when conducting IO of numbers and strings.
27  * Basically, it's the spot to house all wrappers around
28  * commonly used printing facilities.
29  *
30  * It can also handle cropping of numbers below a certain
31  * decade level. This is useful for IO for testing purposes.
32  * For example, if you don't care about anything below
33  * 1.0E-20, you can set up the IO so that it won't print out
34  * any digits below 1.0E-20, even digits that are in numbers
35  * greater than 1.0E-20. In other words the number
36  *
37  * 1.12345E-19
38  *
39  * would be cropped to the value
40  *
41  * 1.1000E-19
42  *
43  * The class wraps around a single std::ostream class. Its
44  * cropping functions are also available as a "double"
45  * conversion utility.
46  *
47  *
48  * @ingroup globalUtilFuncs
49  *
50  */
51 class PrintCtrl
52 {
53 public:
54 
55  //! enum for cropping control
56  enum CROP_TYPE {
57  //! Turn off cropping always
58  CT_OFF=0,
59  //! Turn off cropping, unless the global toggle is turned on
61  //! Turn on cropping unless the global toggle is turned off
63  //! Turn on cropping always
65  };
66 
67  //! enum for global cropping control
69  //! no preference for global cropping
71  //! global toggle for turning on cropping
73  //! global toggle for turning off cropping
75  };
76 
77  //! static enum for turning on and off cropping
78  /*!
79  * The default is to not have a preference for cropping
80  */
82 
83  //! Constructor
84  /*!
85  * This also serves to initialize the ticks within the object
86  *
87  * @param coutProxy This is a reference to the ostream
88  * to use for all IO from ths object.
89  * @param Ndec value of Ndec. Defaults to -1000, i.e.,
90  * no decade cropping
91  * @param ctlocal The default is to turn on cropping all the time.
92  */
93  PrintCtrl(std::ostream& coutProxy = std::cout, int Ndec = -1000,
94  CROP_TYPE ctlocal = CT_ON);
95 
96  //! Print a double using scientific notation
97  /*!
98  * Prints a double using scientific notation in a
99  * fixed number of spaces.
100  *
101  * The precision of the number will be adjusted to
102  * fit into the maximum space.
103  *
104  * @param d double to be printed
105  * @param sigDigits Number of significant digits
106  * (-1 = default, means to use the default
107  * number for the object, which is initially
108  * set to 13.
109  * @param wMin Minimum number of spaces to print out
110  * @param wMax Maximum number of spaces to print out
111  */
112  void pr_de(const double d, int sigDigits = -1,
113  const int wMin = -1, const int wMax = -1);
114 
115  //! Print a double using scientific notation cropping
116  //! decade values
117  /*!
118  * Prints a double using scientific notation in a
119  * fixed number of spaces. This routine also crops
120  * number below the default decade level.
121  *
122  * The precision of the number will be adjusted to
123  * fit into the maximum space.
124  *
125  * @param d double to be printed
126  * @param sigDigits Number of significant digits
127  * (-1 = default, means to use the default
128  * number for the object, which is initially
129  * set to 13.
130  * @param wMin Minimum number of spaces to print out
131  * @param wMax Maximum number of spaces to print out
132  */
133  void pr_de_c10(const double d, int sigDigits = -1,
134  const int wMin = -1, const int wMax = -1);
135 
136  //! Crop a double at a certain number of significant digits
137  /*!
138  * This routine will crop a floating point number at a certain
139  * number of significant digits. Note, it does
140  * rounding up of the last digit.
141  *
142  * @param d Double to be cropped
143  * @param sigDigits Number of significant digits
144  * example:
145  * d = 1.0305E-15;
146  * nsig = 3;
147  * This routine will return 1.03E-15
148  */
149  double cropSigDigits(const double d, int sigDigits) const;
150 
151  //! Crop a double at a certain decade level
152  /*!
153  * This routine will crop a floating point number at a certain
154  * decade lvl. In other words everything below a power of 10^Ndec
155  * will be deleted.
156  * Note, it does rounding up of the last digit.
157  *
158  * @param d Double to be cropped
159  * @param nDecades Number of significant digits
160  * example:
161  * d = 1.1305E-15;
162  * nDecades = -16;
163  * This routine will return 1.1E-15
164  *
165  * d = 8.0E-17
166  * nDecades = -16
167  * This routine will return 0.0
168  */
169  double cropAbs10(const double d, const int nDecades) const;
170 
171  //! Set the default value of N decade
172  /*!
173  * @param nDecades new value of Ndec
174  *
175  * @return returns the old value of Ndec
176  */
177  int setNdec(int nDecades);
178 
179 
180  //! Set the default significant digits to output
181  /*!
182  * @param sigDigits new value of the sig digits
183  *
184  * @return returns the old value of Ndec
185  */
186  int setSigDigits(int sigDigits);
187 
188  //! Set the default minimum width
189  /*!
190  * @param wMin Default minimum width
191  *
192  * @return returns the old default
193  */
194  int setWmin(int wMin);
195 
196  //! Set the default maximum width
197  /*!
198  * @param wMax Default maximum width
199  *
200  * @return returns the old default
201  */
202  int setWmax(int wMax);
203 
204  //! Set the cropping control flag
205  /*!
206  * @param ctlocal Local enum value for the cropping type
207  */
208  void setCropCntrl(CROP_TYPE ctlocal);
209 
210 
211 private:
212 
213  //! private function to figure out cropping logic
214  /*!
215  * @return Returns the decision as to whether to crop or not
216  */
217  bool doCrop() const;
218 
219  //! This is the ostream to send all output from the object
220  /*!
221  * It defaults to cout
222  */
223  std::ostream& m_cout;
224 
225  //! Default decade level to use for decade cropping
226  /*!
227  * This is initially set to -1000, which means that
228  * no cropping will be carried out
229  */
230  int m_Ndec;
231 
232  //! default precision level to use in printing
233  /*!
234  * This actually is one less than the number of significant digits.
235  *
236  * Initially set to 12
237  */
239 
240  //! default minimimum field width
241  /*!
242  * Initially, this is set to 9
243  */
244  int m_wMin;
245 
246  //! Default maximum field width
247  /*!
248  * Initially this is set to 19
249  */
250  int m_wMax;
251 
252  //! Local Cropping Control
254 };
255 }
256 
257 #endif
258