Flow123d  release_2.2.0-41-g0958a8d
unit_converter.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file unit_converter.hh
15  * @brief
16  */
17 
18 #ifndef UNIT_CONVERTER_HH_
19 #define UNIT_CONVERTER_HH_
20 
21 #include <vector>
22 #include <string>
23 #include <map>
24 
25 #include "input/input_exception.hh"
26 #include "fields/unit_si.hh"
27 #include "system/xio.h"
28 
29 // Declaration of exceptions
30 TYPEDEF_ERR_INFO(EI_UnitDefinition, std::string);
31 TYPEDEF_ERR_INFO(EI_ExpectedUnit, std::string);
32 TYPEDEF_ERR_INFO(EI_UnitError, std::string);
33 DECLARE_INPUT_EXCEPTION(ExcInvalidUnit,
34  << "Invalid definition of unit: " << EI_UnitDefinition::qval << "\n" << EI_UnitError::val << ".\n");
35 DECLARE_INPUT_EXCEPTION(ExcNoncorrespondingUnit,
36  << "Non-corresponding definition of unit: " << EI_UnitDefinition::qval << "\nExpected: unit with base format "
37  << EI_ExpectedUnit::qval << ".\n");
38 
39 
40 /// Store structure given by parser
41 struct Factor {
42  /// Constructor
43  Factor() : exponent_(1), basic_(true) {}
44  Factor(std::string factor, int exponent, bool basic = true) : factor_(factor), exponent_(exponent), basic_(basic) {}
45 
46  std::string factor_; //!< string represantation of unit or user defined constant
47  int exponent_; //!< exponent
48  bool basic_; //!< unit is basic (strict defined in application) / derived (defined by user as formula)
49 };
50 struct Formula {
51  /// Constructor
52  Formula() : coef_(1.0) {}
53 
54  double coef_; //!< multiplicative coeficient
55  std::vector<struct Factor> factors_; //!< factors of formula
56 };
58 
59 
60 
61 /**
62  * @brief Helper class. Defines basic factors of SI, non-SI and derived units.
63  *
64  * Class is instantiated as static object of UnitConverter class and provides check
65  * of user defined units.
66  */
67 class BasicFactors {
68 public:
69  struct DerivedUnit {
70  double coef_; //!< multiplicative coeficient
71  UnitSI unit_; //!< derived SI unit
72  };
73 
75 
76  /// Define all base and derived units given by their symbol.
77  UnitsMap units_map_;
78 
79  /// Constructor
80  BasicFactors();
81 
82 };
83 
84 
86 public:
87  /// Define all base and derived units given by their symbol.
89 
90  /// Constructor
91  UnitConverter();
92 
93  /// Convert string to coeficient and UnitSI representation, return coeficient
94  double convert(std::string actual_unit);
95 
96  /// Return @p unit_si_
97  inline UnitSI unit_si() const {
98  ASSERT(unit_si_.is_def()).error("UnitSI is not defined, first call convert method.");
99  return unit_si_;
100  }
101 
102 protected:
103  /**
104  * @brief Parse and check unit defined in string format.
105  *
106  * Return data in format \p UnitData
107  */
108  UnitData read_unit(std::string s);
109 
110  /// Calculates UnitSi and coeficient of Factor, recursively calls this method for user defined formula
111  void add_converted_unit(Factor factor, UnitData &unit_data, UnitSI &unit_si, double &coef);
112 
113  /**
114  * Coeficient of unit.
115  *
116  * Coeficient is used if unit is not in basic format. Example: if the unit is specified
117  * in minutes, coeficient has value 60.
118  */
119  double coef_;
120 
121  /**
122  * Basic format of converted SI unit
123  */
125 
126 };
127 
128 
129 #endif /* UNIT_CONVERTER_HH_ */
UnitSI unit_
derived SI unit
Helper class. Defines basic factors of SI, non-SI and derived units.
UnitsMap units_map_
Define all base and derived units given by their symbol.
bool basic_
unit is basic (strict defined in application) / derived (defined by user as formula) ...
std::vector< struct Factor > factors_
factors of formula
UnitSI unit_si() const
Return unit_si_.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:346
I/O functions with filename storing, able to track current line in opened file. All standard stdio fu...
std::map< std::string, struct DerivedUnit > UnitsMap
Yes & convert(fmt::ULongLong)
TYPEDEF_ERR_INFO(EI_UnitDefinition, std::string)
Formula()
Constructor.
Factor(std::string factor, int exponent, bool basic=true)
int exponent_
exponent
std::string factor_
string represantation of unit or user defined constant
Store structure given by parser.
DECLARE_INPUT_EXCEPTION(ExcInvalidUnit,<< "Invalid definition of unit: "<< EI_UnitDefinition::qval<< "\n"<< EI_UnitError::val<< ".\n")
std::map< std::string, struct Formula > UnitData
Class for representation SI units of Fields.
Definition: unit_si.hh:40
Factor()
Constructor.
static const BasicFactors basic_factors
Define all base and derived units given by their symbol.
double coef_
multiplicative coeficient
double coef_
multiplicative coeficient