unitconv.cpp 10.9 KB
Newer Older
1
2
3
4
//================================================================================
// Name        : unitconv.cpp
// Author      : Axel Auweter
// Copyright   : Leibniz Supercomputing Centre
Michael Ott's avatar
Michael Ott committed
5
// Description : C++ API implementation for conversion of units in libdcdb.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2011-2016 Leibniz Supercomputing Centre
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//================================================================================
Axel Auweter's avatar
Axel Auweter committed
26

27
#include "dcdb/unitconv.h"
Axel Auweter's avatar
Axel Auweter committed
28
29
30

#include <list>

31
32
using namespace DCDB;

Axel Auweter's avatar
Axel Auweter committed
33
/*
Axel Auweter's avatar
Axel Auweter committed
34
  Undefined
35
  Unit_None,
Axel Auweter's avatar
Axel Auweter committed
36

Axel Auweter's avatar
Axel Auweter committed
37
  Base units
38
39
40
41
42
43
  Unit_Meter,
  Unit_Second,
  Unit_Ampere,
  Unit_Kelvin,
  Unit_Watt,
  Unit_Volt,
Axel Auweter's avatar
Axel Auweter committed
44

Axel Auweter's avatar
Axel Auweter committed
45
  Others
46
47
  Unit_Celsius,
  Unit_Fahrenheit,
Axel Auweter's avatar
Axel Auweter committed
48

49
  1e-2
50
51
  Unit_CentiMeter,
  Unit_CentiCelsius,
52

Axel Auweter's avatar
Axel Auweter committed
53
  1e-3
54
55
56
57
58
59
60
  Unit_MilliMeter,
  Unit_MilliSecond,
  Unit_MilliAmpere,
  Unit_MilliKelvin,
  Unit_MilliWatt,
  Unit_MilliVolt,
  Unit_MilliCelsius,
Axel Auweter's avatar
Axel Auweter committed
61
62

  1e-6
63
64
65
66
67
68
69
  Unit_MicroMeter,
  Unit_MicroSecond,
  Unit_MicroAmpere,
  Unit_MicroKelvin,
  Unit_MicroWatt,
  Unit_MicroVolt,
  Unit_MicroCelsius,
Axel Auweter's avatar
Axel Auweter committed
70
71
72
*/

typedef struct {
73
  Unit unit;
Axel Auweter's avatar
Axel Auweter committed
74
  std::string str;
75
  Unit baseUnit;
Axel Auweter's avatar
Axel Auweter committed
76
77
78
79
80
81
82
83
84
  int64_t baseConvFactor;   /* a baseConFactor of -1000 indicates that the value has to be divided by 1000 to convert to base, +1000 means to multiply with 1000 */
  double baseConvOffset;   /* Conversion always first multiplies/divides, then adds the offset */
} ConversionTableEntry;

/*
 * Note: since we are using negative offsets into this table to indicate reverse conversion,
 * the first entry (index 0) in the table must be an identity conversion.
 */
ConversionTableEntry conversionTable[] = {
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*  0 */    { Unit_None, "none", Unit_None, 1, 0},
/*  1 */    { Unit_Meter, "m", Unit_Meter, 1, 0 },
/*  2 */    { Unit_CentiMeter, "cm", Unit_Meter, -100, 0},
/*  3 */    { Unit_MilliMeter, "mm", Unit_Meter, -1000, 0 },
/*  4 */    { Unit_MicroMeter, "um", Unit_Meter, -1000000, 0 },
/*  5 */    { Unit_Second, "s", Unit_Second, 1, 0 },
/*  6 */    { Unit_MilliSecond, "ms", Unit_Second, -1000, 0 },
/*  7 */    { Unit_MicroSecond, "us", Unit_Second, -1000000, 0 },
/*  8 */    { Unit_Ampere, "A", Unit_Ampere, 1, 0 },
/*  9 */    { Unit_MilliAmpere, "mA", Unit_Ampere, -1000, 0 },
/* 10 */    { Unit_MicroAmpere, "uA", Unit_Ampere, -1000000, 0 },
/* 11 */    { Unit_Kelvin, "K", Unit_Kelvin, 1, 0 },
/* 12 */    { Unit_MilliKelvin, "mK", Unit_Kelvin, -1000, 0 },
/* 13 */    { Unit_MicroKelvin, "uK", Unit_Kelvin, -1000000, 0 },
/* 14 */    { Unit_Watt, "W", Unit_Watt, 1, 0 },
/* 15 */    { Unit_MilliWatt, "mW", Unit_Watt, -1000, 0 },
/* 16 */    { Unit_MicroWatt, "uW", Unit_Watt, -1000000, 0 },
Michael Ott's avatar
Michael Ott committed
102
103
104
105
106
107
/* 17 */    { Unit_KiloWatt, "kW", Unit_Watt, 1000, 0 },
/* 18 */    { Unit_MegaWatt, "MW", Unit_Watt, 1000000, 0 },
/* 19 */    { Unit_Volt, "V", Unit_Volt, 1, 0 },
/* 20 */    { Unit_MilliVolt, "mV", Unit_Volt, -1000, 0 },
/* 21 */    { Unit_MicroVolt, "uV", Unit_Volt, -1000000, 0 },
/* 22 */    { Unit_Celsius, "C", Unit_MilliKelvin, 1000, 273150 },
108
/* 23 */    { Unit_DeciCelsius, "dC", Unit_Celsius, -10, 0 },
Michael Ott's avatar
Michael Ott committed
109
110
111
112
113
114
115
/* 24 */    { Unit_CentiCelsius, "cC", Unit_Celsius, -100, 0},
/* 25 */    { Unit_MilliCelsius, "mC", Unit_Celsius, -1000, 0 },
/* 26 */    { Unit_MicroCelsius, "uC", Unit_Celsius, -1000000, 0},
/* 27 */    { Unit_Fahrenheit, "F", Unit_MilliKelvin, 555, 255116 },
/* 28 */    { Unit_Hertz, "Hz", Unit_Hertz, 1, 0},
/* 29 */    { Unit_KiloHertz, "kHz", Unit_Hertz, 1000, 0},
/* 30 */    { Unit_MegaHertz,"MHz", Unit_Hertz, 1000000, 0},
116
117
118
119
120
121
/* 31 */    { Unit_Joules, "J", Unit_Joules, 3600, 0},
/* 32 */    { Unit_KiloJoules, "kJ", Unit_Joules, 1000, 0},
/* 33 */    { Unit_MegaJoules, "MJ", Unit_Joules, 1000000, 0},
/* 34 */    { Unit_WattHours, "Wh", Unit_Joules, 3600, 0},
/* 35 */    { Unit_KiloWattHours, "kWh", Unit_WattHours, 1000, 0},
/* 36 */    { Unit_MegaWattHours, "MWh", Unit_WattHours, 1000000, 0},
Axel Auweter's avatar
Axel Auweter committed
122
123
124
125
};

#define ConversionTableSize ((int)(sizeof(conversionTable)/sizeof(ConversionTableEntry)))

126
Unit UnitConv::fromString(std::string unit) {
Axel Auweter's avatar
Axel Auweter committed
127
128
129
130
131
132
133
134
  /* Walk the table to find the string */
  for (int i=0; i<ConversionTableSize; i++) {
      if ((unit.length() == conversionTable[i].str.length()) && (conversionTable[i].str.compare(unit) == 0)) {
          return conversionTable[i].unit;
      }
  }

  /* No match --> return None */
135
  return Unit_None;
Axel Auweter's avatar
Axel Auweter committed
136
137
}

138
std::string UnitConv::toString(Unit unit) {
Axel Auweter's avatar
Axel Auweter committed
139
140
141
142
143
144
145
146
147
148
  /* Walk the table to find the unit */
  for (int i=0; i<ConversionTableSize; i++) {
      if (conversionTable[i].unit == unit) {
          return conversionTable[i].str;
      }
  }

  return std::string("");
}

149
static bool dfs(std::list<int>& chain, Unit in, Unit out) {
Axel Auweter's avatar
Axel Auweter committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  /* If there is nothing to convert, we don't have to search */
  if (in == out) {
      return true;
  }

  /* If the search depth exceeds the number of entries in the conversion table, abort */
  if (chain.size() > ConversionTableSize) {
      return false;
  }

  for (int i=0; i<ConversionTableSize; i++) {
      if ((conversionTable[i].unit == in) && (conversionTable[i].unit != conversionTable[i].baseUnit)) {
          /* Make sure this is not just the reverse of the last operation */
          if ((chain.size() == 0) || (chain.back() != -i)) {
              /* If this conversion leads us to the result, we're done */
              chain.push_back(i);
              if (conversionTable[i].baseUnit == out) {
                  return true;
              }
              /* If this conversion can handle our in unit, search for possible follow-up conversions */
              if (dfs(chain, conversionTable[i].baseUnit, out)) {
                  return true;
              }
              else {
                  chain.pop_back();
              }
          }
      }
      if (conversionTable[i].baseUnit == in && (conversionTable[i].unit != conversionTable[i].baseUnit)) {
          if ((chain.size() == 0) || (chain.back() != i)) {
              chain.push_back(-i);
              if (conversionTable[i].unit == out) {
                  return true;
              }
              if (dfs(chain, conversionTable[i].unit, out)) {
                  return true;
              }
              else {
                  chain.pop_back();
              }
          }
      }
  }
  return false;
}

196
bool UnitConv::convert(int64_t& value, Unit in, Unit out) {
Axel Auweter's avatar
Axel Auweter committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  /* Do a depth-first search to find a suitable conversion path */
  std::list<int> convChain;
  if (dfs(convChain, in, out))  {
      /* Convert */
      double factor = 1;
      double offset = 0;

      for (std::list<int>::iterator it = convChain.begin(); it != convChain.end(); it++) {
          double newFact = (*it) < 0 ? -conversionTable[-(*it)].baseConvFactor : conversionTable[*it].baseConvFactor;
          double  newOff = (*it) < 0 ? -conversionTable[-(*it)].baseConvOffset : conversionTable[*it].baseConvOffset;
          double absFactor = factor > 0 ? factor : -factor;
          double absNewFact = newFact > 0 ? newFact : -newFact;

          /* Do offset calculation */
          if (newFact > 0) {
              offset = (offset * newFact) + newOff;
          }
          else {
              offset = (offset + newOff) / (-newFact);
          }

          /* Do factor calculation */
          if (factor > 0 && newFact > 0) {
              factor = factor * newFact;
          }
          else if (factor < 0 && newFact < 0) {
              factor = -(factor * newFact);
          }
          else if (absFactor == absNewFact) {
              factor = 1;
          }
          else if (absFactor > absNewFact && factor > 0) {
              factor = (absFactor / absNewFact);
          }
          else if (absFactor < absNewFact && newFact > 0) {
              factor = (absNewFact / absFactor);
          }
          else if (absFactor > absNewFact && factor < 0) {
              factor = -(absFactor / absNewFact);
          }
          else if (absFactor < absNewFact && newFact < 0) {
              factor = -(absNewFact / absFactor);
          }
          else {
              return false;
          }
      }

      /* Calculate final result */
      if (factor > 0) {
          value = (value * factor) + (int64_t)offset;
      }
      else {
          value = (value / (-factor)) + (int64_t)offset;
      }

      return true;
  }

  return false;
}

259
bool UnitConv::convert(double& value, Unit in, Unit out) {
Axel Auweter's avatar
Axel Auweter committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
  /* Do a depth-first search to find a suitable conversion path */
  std::list<int> convChain;
  if (dfs(convChain, in, out))  {
      /* Convert */
      double factor = 1;
      double offset = 0;

      for (std::list<int>::iterator it = convChain.begin(); it != convChain.end(); it++) {
          double newFact = (*it) < 0 ? -conversionTable[-(*it)].baseConvFactor : conversionTable[*it].baseConvFactor;
          double  newOff = (*it) < 0 ? -conversionTable[-(*it)].baseConvOffset : conversionTable[*it].baseConvOffset;
          double absFactor = factor > 0 ? factor : -factor;
          double absNewFact = newFact > 0 ? newFact : -newFact;

          /* Do offset calculation */
          if (newFact > 0) {
              offset = (offset * newFact) + newOff;
          }
          else {
              offset = (offset + newOff) / (-newFact);
          }

          /* Do factor calculation */
          if (factor > 0 && newFact > 0) {
              factor = factor * newFact;
          }
          else if (factor < 0 && newFact < 0) {
              factor = -(factor * newFact);
          }
          else if (absFactor == absNewFact) {
              factor = 1;
          }
          else if (absFactor > absNewFact && factor > 0) {
              factor = (absFactor / absNewFact);
          }
          else if (absFactor < absNewFact && newFact > 0) {
              factor = (absNewFact / absFactor);
          }
          else if (absFactor > absNewFact && factor < 0) {
              factor = -(absFactor / absNewFact);
          }
          else if (absFactor < absNewFact && newFact < 0) {
              factor = -(absNewFact / absFactor);
          }
          else {
              return false;
          }
      }

      /* Calculate final result */
      if (factor > 0) {
          value = (value * factor) + (int64_t)offset;
      }
      else {
          value = (value / (-factor)) + (int64_t)offset;
      }

      return true;
  }

  return false;
}