OpenFCST: The open-source Fuel Cell Simulation Toolbox
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fcst_units.h
Go to the documentation of this file.
1 //---------------------------------------------------------------------------
2 //
3 // FCST: Fuel Cell Simulation Toolbox
4 //
5 // Copyright (C) 2013 by Energy Systems Design Laboratory, University of Alberta
6 //
7 // This software is distributed under the MIT License.
8 // For more information, see the README file in /doc/LICENSE
9 //
10 // - Class: fcst_units.h
11 // - Description: A cpp object that can be used by fcst programmers
12 // when manipulating units in order to preserve
13 // standards and prevent confusion.
14 // - Developers: Philip Wardlaw and M. Secanell
15 // - $Id: fcst_units.h 1354 2013-08-17 00:01:22Z secanell $
16 //
17 //---------------------------------------------------------------------------
18 
19 #include <string>
20 #include <iostream>
21 #include <stdexcept>
22 
23 #ifndef _FCST__UNITS
24 #define _FCST__UNITS
25 
50 class Units {
51 
52 public:
58  inline static double convert(double unitToConvert, double to, double from){
59 
60  if (to > 0 && from > 0){
61 
62  return unitToConvert*(to/from);
63 
64  }
65  else if (to < 0 && from < 0){
66 
67  return unitToConvert*(from/to);
68 
69  }
70  else {
71  //invalid use of the convert function. To and from must be of the same sign
72  throw std::logic_error("Incorrect unit conversion");
73  }
74 
75 
76 
77 
78  }
79 
85  inline static double convert(double unitToConvert, int specificCase){
86 
87  switch (specificCase)
88  {
89  case KJ_to_BTU:
90  return unitToConvert/1.054;
91  case BTU_to_KJ:
92  return unitToConvert*1.054;
93  case ATM_to_PA:
94  return unitToConvert*1.01325e5;
95  }
96 
97  //Invalid case, throw error
98  throw std::invalid_argument("Specific case not implemented");
99  }
100 
101  //Static Integers denoting specific conversion cases (Typically Non Metric)
102  static const unsigned int KJ_to_BTU =1;
103  static const unsigned int BTU_to_KJ =2;
104  static const unsigned int ATM_to_PA =3;
105 
106  //Static doubles describing generic conversions
107 
108  static double PER_UNIT; // 1;
109  static double PER_C_UNIT; // 1E-2;
110  static double PER_MILLI_UNIT; // 1E-3;
111  static double PER_MICRO_UNIT; // 1E-6;
112  static double PER_N_UNIT; // 1E-9;
113  static double PER_P_UNIT; // 1E-12;
114 
115  static double PER_UNIT2; // 1;
116  static double PER_C_UNIT2; // 1E-4;
117  static double PER_MILLI_UNIT2; // 1E-6;
118  static double PER_MICRO_UNIT2; // 1E-12;
119  static double PER_N_UNIT2; // 1E-18;
120  static double PER_P_UNIT2; // 1E-24;
121 
122  static double PER_UNIT3; // 1;
123  static double PER_C_UNIT3; // 1E-6;
124  static double PER_MILLI_UNIT3; // 1E-9;
125  static double PER_MICRO_UNIT3; // 1E-18;
126  static double PER_N_UNIT3; // 1E-27;
127  static double PER_P_UNIT3; // 1E-36;
128 
129  //Sign is to differentiate between "Per unit" and "by unit"
130 
131  static double UNIT; // -1;
132  static double C_UNIT; //- 1E-2;
133  static double MILLI_UNIT; //- 1E-3;
134  static double MICRO_UNIT; //- 1E-6;
135  static double N_UNIT; //- 1E-9;
136  static double P_UNIT; // -1E-12;
137 
138  static double UNIT2; //- 1;
139  static double C_UNIT2; // -1E-4;
140  static double MILLI_UNIT2; // -1E-6;
141  static double MICRO_UNIT2; //- 1E-12;
142  static double N_UNIT2; //- 1E-18;
143  static double P_UNIT2; //- 1E-24;
144 
145  static double UNIT3; // -1;
146  static double C_UNIT3; // -1E-6;
147  static double MILLI_UNIT3; // -1E-9;
148  static double MICRO_UNIT3; // -1E-18;
149  static double N_UNIT3; //- 1E-27;
150  static double P_UNIT3; // -1E-36;
151 };
152 
153 
154 
155 
156 #endif