Cantera  2.1.2
PrintCtrl.cpp
Go to the documentation of this file.
1 /**
2  * @file PrintCtrl.cpp
3  * Definitions 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 #include "cantera/base/PrintCtrl.h"
14 #include "cantera/base/global.h"
15 #include <cmath>
16 
17 using namespace std;
18 
19 namespace Cantera
20 {
21 
22 // Storage for the global crop flag
23 PrintCtrl::CROP_TYPE_GLOBAL PrintCtrl::GlobalCrop = GCT_NOPREF;
24 
25 PrintCtrl::PrintCtrl(std::ostream& coutProxy, int Ndec,
26  CROP_TYPE ctlocal) :
27  m_cout(coutProxy),
28  m_Ndec(Ndec),
29  m_precision(12),
30  m_wMin(9),
31  m_wMax(19),
32  m_cropCntrl(ctlocal)
33 {
34  warn_deprecated("class PrintCtrl");
35 }
36 
37 void PrintCtrl::pr_de_c10(const double din, int p, const int wMin,
38  const int wMax)
39 {
40  double d = cropAbs10(din, m_Ndec);
41  pr_de(d, p, wMin, wMax);
42 }
43 
44 void PrintCtrl::pr_de(const double d, int sigDigIn, const int wMinIn,
45  const int wMaxIn)
46 {
47  int p = m_precision;
48  if (sigDigIn != -1) {
49  p = sigDigIn-1;
50  if (p < 0) {
51  p = 0;
52  }
53  }
54 
55  int wMin = m_wMin;
56  if (wMinIn != -1) {
57  wMin = wMinIn;
58  if (wMin < 1) {
59  wMin = 1;
60  }
61  }
62 
63  int wMax = m_wMax;
64  if (wMaxIn != -1) {
65  wMax = wMaxIn;
66  if (wMax < 1) {
67  wMax = 1;
68  }
69  }
70 
71  if (wMin > wMax) {
72  wMax = wMin;
73  }
74 
75  // Have to do the wMax ourselves, since C++ doesn't seem to
76  // have a streams manipulator to do this !?!
77  double dfabs = fabs(d);
78  // This is the normal length assuming no sign and an 1.0E+04
79  // formated exponented
80  int requestedLength = 6 + p;
81  if (d < 0.0) {
82  requestedLength++;
83  }
84  if (dfabs < 9.9999999999E-99) {
85  requestedLength++;
86  }
87  if (dfabs > 9.9999999999E99) {
88  requestedLength++;
89  }
90  if (requestedLength > wMax) {
91  p -= (requestedLength - wMax);
92  if (p < 0) {
93  p = 0;
94  }
95  }
96 
97  // Set to upper case and scientific notation
98  m_cout.setf(ios_base::scientific | ios_base::uppercase);
99  int wold = (int) m_cout.width(wMin);
100  int pold = (int) m_cout.precision(p);
101 
102  m_cout << d;
103  // Return the precision to the previous value;
104  m_cout.precision(pold);
105  m_cout.unsetf(ios_base::scientific);
106 
107  // Return width to original
108  m_cout.width(wold);
109 }
110 
111 double PrintCtrl::cropAbs10(const double d, int Ndec) const
112 {
113  if (!doCrop()) {
114  return d;
115  }
116  if (Ndec < -301 || Ndec > 301) {
117  return d;
118  }
119  double dfabs = fabs(d);
120  double pdec = pow(10.0, (double) Ndec);
121  if (dfabs < pdec) {
122  return 0.0;
123  }
124  double dl10 = log10(dfabs);
125  int N10 = (int) dl10;
126  if (dl10 > -0.0) {
127  N10 += 1;
128  }
129  int nsig = N10 - Ndec;
130  return cropSigDigits(d, nsig);
131 }
132 
133 double PrintCtrl::cropSigDigits(const double d, int nSig) const
134 {
135  if (!doCrop()) {
136  return d;
137  }
138  if (nSig <=0) {
139  nSig = 1;
140  }
141  if (nSig >=9) {
142  nSig = 9;
143  }
144  double sgn = 1.0;
145  if (d < 0.0) {
146  sgn = -1.0;
147  }
148  double dfabs = fabs(d);
149  double dl10 = log10(dfabs);
150  int N10 = (int) dl10;
151  if (dl10 > -0.0) {
152  N10 += 1;
153  }
154  int E10 = -N10 + nSig ;
155  double pfabs = dfabs * pow(10.0, (double) E10);
156  pfabs *= (1.0 + 1.0E-14);
157  long int nfabs = (long int) pfabs;
158  double remainder = pfabs - nfabs;
159  if (remainder > 0.5) {
160  nfabs++;
161  }
162  double paltabs = (double) nfabs;
163  double daltabs = paltabs * pow(10.0, (double) -E10);
164  return sgn * daltabs;
165 }
166 
167 int PrintCtrl::setNdec(int Ndec)
168 {
169  int nold = m_Ndec;
170  m_Ndec = Ndec;
171  return nold;
172 }
173 
174 int PrintCtrl::setSigDigits(int nSigDigits)
175 {
176  int nold = m_precision + 1;
177  m_precision = nSigDigits - 1;
178  if (m_precision < 0) {
179  m_precision = 0;
180  }
181  return nold;
182 }
183 
184 int PrintCtrl::setWmin(int wmin)
185 {
186  int nold = m_wMin;
187  m_wMin = wmin;
188  return nold;
189 }
190 
191 int PrintCtrl::setWmax(int wmax)
192 {
193  int nold = m_wMax;
194  m_wMax = wmax;
195  return nold;
196 }
197 
198 bool PrintCtrl::doCrop() const
199 {
200  bool retn = ((m_cropCntrl == CT_ON) || (m_cropCntrl == CT_ON_GLOBALOBEY));
201  if (m_cropCntrl == CT_ON_GLOBALOBEY) {
202  if (GlobalCrop == GCT_NOCROP) {
203  retn = false;
204  }
205  } else if (m_cropCntrl == CT_OFF_GLOBALOBEY) {
206  if (GlobalCrop == GCT_CROP) {
207  retn = true;
208  }
209  }
210  return retn;
211 }
212 
214 {
215  m_cropCntrl = ctlocal;
216 }
217 }
Turn on cropping unless the global toggle is turned off.
Definition: PrintCtrl.h:60
CROP_TYPE
enum for cropping control
Definition: PrintCtrl.h:54
global toggle for turning off cropping
Definition: PrintCtrl.h:72
int m_wMin
default minimimum field width
Definition: PrintCtrl.h:231
double cropAbs10(const double d, const int nDecades) const
Crop a double at a certain decade level.
Definition: PrintCtrl.cpp:111
void setCropCntrl(CROP_TYPE ctlocal)
Set the cropping control flag.
Definition: PrintCtrl.cpp:213
bool doCrop() const
private function to figure out cropping logic
Definition: PrintCtrl.cpp:198
Declarations for a simple class that augments the streams printing capabilities (see Cantera::PrintCt...
void pr_de_c10(const double d, int sigDigits=-1, const int wMin=-1, const int wMax=-1)
Print a double using scientific notation cropping decade values.
Definition: PrintCtrl.cpp:37
Turn off cropping, unless the global toggle is turned on.
Definition: PrintCtrl.h:58
int setWmin(int wMin)
Set the default minimum width.
Definition: PrintCtrl.cpp:184
double cropSigDigits(const double d, int sigDigits) const
Crop a double at a certain number of significant digits.
Definition: PrintCtrl.cpp:133
int m_wMax
Default maximum field width.
Definition: PrintCtrl.h:237
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:76
int m_Ndec
Default decade level to use for decade cropping.
Definition: PrintCtrl.h:217
This file contains definitions for utility functions and text for modules, inputfiles, logs, textlogs, HTML_logs (see Input File Handling, Diagnostic Output, Writing messages to the screen and Writing HTML Logfiles).
global toggle for turning on cropping
Definition: PrintCtrl.h:70
void pr_de(const double d, int sigDigits=-1, const int wMin=-1, const int wMax=-1)
Print a double using scientific notation.
Definition: PrintCtrl.cpp:44
std::ostream & m_cout
This is the ostream to send all output from the object.
Definition: PrintCtrl.h:210
int setSigDigits(int sigDigits)
Set the default significant digits to output.
Definition: PrintCtrl.cpp:174
CROP_TYPE m_cropCntrl
Local Cropping Control.
Definition: PrintCtrl.h:240
int setWmax(int wMax)
Set the default maximum width.
Definition: PrintCtrl.cpp:191
int setNdec(int nDecades)
Set the default value of N decade.
Definition: PrintCtrl.cpp:167
int m_precision
default precision level to use in printing
Definition: PrintCtrl.h:225
static CROP_TYPE_GLOBAL GlobalCrop
static enum for turning on and off cropping
Definition: PrintCtrl.h:79
Turn on cropping always.
Definition: PrintCtrl.h:62