00001 /** 00002 * @file PrintCtrl.h 00003 * Declarations for a simple class that augments the streams printing capabilities 00004 * (see \ref Cantera::PrintCtrl). 00005 */ 00006 /* 00007 * $Revision: 368 $ 00008 * $Date: 2010-01-03 19:46:26 -0500 (Sun, 03 Jan 2010) $ 00009 */ 00010 /* 00011 * Copywrite 2004 Sandia Corporation. Under the terms of Contract 00012 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 00013 * retains certain rights in this software. 00014 * See file License.txt for licensing information. 00015 */ 00016 00017 #ifndef CT_PRINTCTRL_H 00018 #define CT_PRINTCTRL_H 00019 00020 #include <iostream> 00021 00022 namespace Cantera { 00023 00024 //! This class provides some printing and cropping utilities 00025 /*! 00026 * The class is used to provide some formatting options for 00027 * printing out real numbers to files and to standard output. 00028 * Specifically, it can make sure that a max and min field 00029 * width is honored when conducting IO of numbers and strings. 00030 * Basically, its the spot to house all wrappers around 00031 * commonly used printing facilities. 00032 * 00033 * It can also handle cropping of numbers below a certain 00034 * decade level. This is useful for IO for testing purposes. 00035 * For example, if you don't care about anything below 00036 * 1.0E-20, you can set up the IO so that it won't print out 00037 * any digits below 1.0E-20, even digits that are in numbers 00038 * greater than 1.0E-20. In other words the number 00039 * 00040 * 1.12345E-19 00041 * 00042 * whould be cropped to the value 00043 * 00044 * 1.1000E-19 00045 * 00046 * The class wraps aroud a single std::ostream class. It's 00047 * cropping functions are also available as a "double" 00048 * conversion utility. 00049 * 00050 * 00051 * @ingroup globalUtilFuncs 00052 * 00053 */ 00054 class PrintCtrl { 00055 public: 00056 00057 //! enum for cropping control 00058 enum CROP_TYPE { 00059 //! Turn off cropping always 00060 CT_OFF=0, 00061 //! Turn off cropping, unless the global toggle is turned on 00062 CT_OFF_GLOBALOBEY, 00063 //! Turn on cropping unless the global toggle is turned off 00064 CT_ON_GLOBALOBEY, 00065 //! Turn on cropping always 00066 CT_ON 00067 }; 00068 00069 //! enum for global cropping control 00070 enum CROP_TYPE_GLOBAL { 00071 //! no preference for global cropping 00072 GCT_NOPREF = 0, 00073 //! global toggle for turning on cropping 00074 GCT_CROP, 00075 //! global toggle for turning off cropping 00076 GCT_NOCROP 00077 }; 00078 00079 //! static enum for turning on and off cropping 00080 /*! 00081 * The default is to not have a preference for cropping 00082 */ 00083 static CROP_TYPE_GLOBAL GlobalCrop; 00084 00085 //! Constructor 00086 /*! 00087 * This also serves to initialize the ticks within the object 00088 * 00089 * @param coutProxy This is a reference to the ostream 00090 * to use for all IO from ths object. 00091 * @param Ndec value of Ndec. Defaults to -1000, i.e., 00092 * no decade cropping 00093 * @param ctlocal The default is to turn on cropping all the time. 00094 */ 00095 PrintCtrl(std::ostream &coutProxy = std::cout, int Ndec = -1000, 00096 CROP_TYPE ctlocal = CT_ON); 00097 00098 //! Print a double using scientific notation 00099 /*! 00100 * Prints a double using scientific notation in a 00101 * fixed number of spaces. 00102 * 00103 * The precision of the number will be adjusted to 00104 * fit into the maximum space. 00105 * 00106 * @param d double to be printed 00107 * @param sigDigits Number of significant digits 00108 * (-1 = default, means to use the default 00109 * number for the object, which is initially 00110 * set to 13. 00111 * @param wMin Minimum number of spaces to print out 00112 * @param wMax Maximum number of spaces to print out 00113 */ 00114 void pr_de(const double d, int sigDigits = -1, 00115 const int wMin = -1, const int wMax = -1); 00116 00117 //! Print a double using scientific notation cropping 00118 //! decade values 00119 /*! 00120 * Prints a double using scientific notation in a 00121 * fixed number of spaces. This routine also crops 00122 * number below the default decade level. 00123 * 00124 * The precision of the number will be adjusted to 00125 * fit into the maximum space. 00126 * 00127 * @param d double to be printed 00128 * @param sigDigits Number of significant digits 00129 * (-1 = default, means to use the default 00130 * number for the object, which is initially 00131 * set to 13. 00132 * @param wMin Minimum number of spaces to print out 00133 * @param wMax Maximum number of spaces to print out 00134 */ 00135 void pr_de_c10(const double d, int sigDigits = -1, 00136 const int wMin = -1, const int wMax = -1); 00137 00138 //! Crop a double at a certain number of significant digits 00139 /*! 00140 * This routine will crop a floating point number at a certain 00141 * number of significant digits. Note, it does 00142 * rounding up of the last digit. 00143 * 00144 * @param d Double to be cropped 00145 * @param sigDigits Number of significant digits 00146 * example: 00147 * d = 1.0305E-15; 00148 * nsig = 3; 00149 * This routine will return 1.03E-15 00150 */ 00151 double cropSigDigits(const double d, int sigDigits) const; 00152 00153 //! Crop a double at a certain decade level 00154 /*! 00155 * This routine will crop a floating point number at a certain 00156 * decade lvl. In other words everything below a power of 10^Ndec 00157 * will be deleted. 00158 * Note, it does rounding up of the last digit. 00159 * 00160 * @param d Double to be cropped 00161 * @param nDecades Number of significant digits 00162 * example: 00163 * d = 1.1305E-15; 00164 * nDecades = -16; 00165 * This routine will return 1.1E-15 00166 * 00167 * d = 8.0E-17 00168 * nDecades = -16 00169 * This routine will return 0.0 00170 */ 00171 double cropAbs10(const double d, const int nDecades) const; 00172 00173 //! Set the default value of N decade 00174 /*! 00175 * @param nDecades new value of Ndec 00176 * 00177 * @return returns the old value of Ndec 00178 */ 00179 int setNdec(int nDecades); 00180 00181 00182 //! Set the default significant digits to output 00183 /*! 00184 * @param sigDigits new value of the sig digits 00185 * 00186 * @return returns the old value of Ndec 00187 */ 00188 int setSigDigits(int sigDigits); 00189 00190 //! Set the default minimum width 00191 /*! 00192 * @param wMin Default minimum width 00193 * 00194 * @return returns the old default 00195 */ 00196 int setWmin(int wMin); 00197 00198 //! Set the default maximum width 00199 /*! 00200 * @param wMax Default maximum width 00201 * 00202 * @return returns the old default 00203 */ 00204 int setWmax(int wMax); 00205 00206 //! Set the cropping control flag 00207 /*! 00208 * @param ctlocal Local enum value for the cropping type 00209 */ 00210 void setCropCntrl(CROP_TYPE ctlocal); 00211 00212 00213 private: 00214 00215 //! private function to figure out cropping logic 00216 /*! 00217 * @return Returns the decision as to whether to crop or not 00218 */ 00219 bool doCrop() const; 00220 00221 //! This is the ostream to send all output from the object 00222 /*! 00223 * It defaults to cout 00224 */ 00225 std::ostream &m_cout; 00226 00227 //! Default decade level to use for decade cropping 00228 /*! 00229 * This is initially set to -1000, which means that 00230 * no cropping will be carried out 00231 */ 00232 int m_Ndec; 00233 00234 //! default precision level to use in printing 00235 /*! 00236 * This actually is one less than the number of significant digits. 00237 * 00238 * Initially set to 12 00239 */ 00240 int m_precision; 00241 00242 //! default minimimum field width 00243 /*! 00244 * Initially, this is set to 9 00245 */ 00246 int m_wMin; 00247 00248 //! Default maximum field width 00249 /*! 00250 * Initially this is set to 19 00251 */ 00252 int m_wMax; 00253 00254 //! Local Cropping Control 00255 CROP_TYPE m_cropCntrl; 00256 }; 00257 } 00258 00259 #endif 00260