Flow123d  3.9.0-139cfa9f4
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 
22 #include <map> // for map, map<>::value_compare
23 #include <string> // for string
24 #include <vector> // for vector
25 
26 #include "tools/unit_si.hh" // for UnitSI
27 #include "input/input_exception.hh" // for DECLARE_INPUT_EXCEPTION, Exception
28 #include "system/asserts.hh" // for Assert, ASSERT
29 #include "system/exceptions.hh" // for operator<<, ExcStream, EI, TYPED...
30 
31 namespace Input {
32  namespace Type {
33  class Record;
34  }
35 }
36 
37 // Declaration of exceptions
38 TYPEDEF_ERR_INFO(EI_UnitDefinition, std::string);
39 TYPEDEF_ERR_INFO(EI_ExpectedUnit, std::string);
40 TYPEDEF_ERR_INFO(EI_UnitError, std::string);
41 DECLARE_INPUT_EXCEPTION(ExcInvalidUnit,
42  << "Invalid definition of unit: " << EI_UnitDefinition::qval << "\n" << EI_UnitError::val << ".\n");
43 DECLARE_INPUT_EXCEPTION(ExcNoncorrespondingUnit,
44  << "Non-corresponding definition of unit: " << EI_UnitDefinition::qval << "\nExpected: unit with base format "
45  << EI_ExpectedUnit::qval << ".\n");
46 
47 
48 /// Store structure given by parser
49 struct Factor {
50  /// Constructor
51  Factor() : exponent_(1), basic_(true) {}
52  Factor(std::string factor, int exponent, bool basic = true) : factor_(factor), exponent_(exponent), basic_(basic) {}
53 
54  std::string factor_; //!< string represantation of unit or user defined constant
55  int exponent_; //!< exponent
56  bool basic_; //!< unit is basic (strict defined in application) / derived (defined by user as formula)
57 };
58 struct Formula {
59  /// Constructor
60  Formula() : coef_(1.0) {}
61 
62  double coef_; //!< multiplicative coeficient
63  std::vector<struct Factor> factors_; //!< factors of formula
64 };
66 
67 
68 
69 /**
70  * @brief Helper class. Defines basic factors of SI, non-SI and derived units.
71  *
72  * Class is instantiated as static object of UnitConverter class and provides check
73  * of user defined units.
74  */
75 class BasicFactors {
76 public:
77  struct DerivedUnit {
78  double coef_; //!< multiplicative coeficient
79  UnitSI unit_; //!< derived SI unit
80  };
81 
83 
84  /// Define all base and derived units given by their symbol.
86 
87  /// Constructor
88  BasicFactors();
89 
90 };
91 
92 
94 public:
95  /// Define all base and derived units given by their symbol.
97 
98  /// Constructor
99  UnitConverter();
100 
101  static const Input::Type::Record & get_input_type();
102 
103  /* Compute a multiplicative constant to convert from 'actual_unit' to the SI unit.
104  *
105  * The 'actual_unit' string convains a unit expression the is parsed and converted to the 'coef_' and 'unit_si_' so that
106  * 'actual unit' == coef_ * unit_si_
107  *
108  * The coefficient coef_ is returned.
109  *
110  * TODO: pass the actual_unit to the constructor so that we avoid object modifications.
111  */
112  double convert(std::string actual_unit);
113 
114  /// Return @p unit_si_
115  inline UnitSI unit_si() const {
116  ASSERT(unit_si_.is_def()).error("UnitSI is not defined, first call convert method.");
117  return unit_si_;
118  }
119 
120 protected:
121  /**
122  * @brief Parse and check unit defined in string format.
123  *
124  * Return data in format \p UnitData
125  */
126  UnitData read_unit(std::string s);
127 
128  /// Calculates UnitSi and coeficient of Factor, recursively calls this method for user defined formula
129  void add_converted_unit(Factor factor, UnitData &unit_data, UnitSI &unit_si, double &coef);
130 
131  /**
132  * Coeficient of unit.
133  *
134  * Coeficient is used if unit is not in basic format. Example: if the unit is specified
135  * in minutes, coeficient has value 60.
136  */
137  double coef_;
138 
139  /**
140  * Basic format of converted SI unit
141  */
143 
144 };
145 
146 
147 #endif /* UNIT_CONVERTER_HH_ */
Formula::coef_
double coef_
multiplicative coeficient
Definition: unit_converter.hh:62
UnitConverter::read_unit
UnitData read_unit(std::string s)
Parse and check unit defined in string format.
Definition: unit_converter.cc:133
UnitConverter::unit_si
UnitSI unit_si() const
Return unit_si_.
Definition: unit_converter.hh:115
Factor::Factor
Factor(std::string factor, int exponent, bool basic=true)
Definition: unit_converter.hh:52
UnitConverter::add_converted_unit
void add_converted_unit(Factor factor, UnitData &unit_data, UnitSI &unit_si, double &coef)
Calculates UnitSi and coeficient of Factor, recursively calls this method for user defined formula.
Definition: unit_converter.cc:173
BasicFactors::DerivedUnit
Definition: unit_converter.hh:77
BasicFactors
Helper class. Defines basic factors of SI, non-SI and derived units.
Definition: unit_converter.hh:75
Input
Abstract linear system class.
Definition: balance.hh:40
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:350
asserts.hh
Definitions of ASSERTS.
Factor::Factor
Factor()
Constructor.
Definition: unit_converter.hh:51
Factor::factor_
std::string factor_
string represantation of unit or user defined constant
Definition: unit_converter.hh:54
std::vector< struct Factor >
UnitData
std::map< std::string, struct Formula > UnitData
Definition: unit_converter.hh:65
BasicFactors::DerivedUnit::unit_
UnitSI unit_
derived SI unit
Definition: unit_converter.hh:79
exceptions.hh
BasicFactors::DerivedUnit::coef_
double coef_
multiplicative coeficient
Definition: unit_converter.hh:78
UnitConverter
Definition: unit_converter.hh:93
Formula::factors_
std::vector< struct Factor > factors_
factors of formula
Definition: unit_converter.hh:63
UnitConverter::UnitConverter
UnitConverter()
Constructor.
Definition: unit_converter.cc:126
DECLARE_INPUT_EXCEPTION
DECLARE_INPUT_EXCEPTION(ExcInvalidUnit,<< "Invalid definition of unit: "<< EI_UnitDefinition::qval<< "\n"<< EI_UnitError::val<< ".\n")
UnitSI
Class for representation SI units of Fields.
Definition: unit_si.hh:40
UnitConverter::coef_
double coef_
Definition: unit_converter.hh:137
BasicFactors::UnitsMap
std::map< std::string, struct DerivedUnit > UnitsMap
Definition: unit_converter.hh:82
Formula
Definition: unit_converter.hh:58
UnitConverter::convert
double convert(std::string actual_unit)
Definition: unit_converter.cc:159
input_exception.hh
std::map< std::string, struct Formula >
UnitConverter::get_input_type
static const Input::Type::Record & get_input_type()
Definition: unit_converter.cc:100
Factor::basic_
bool basic_
unit is basic (strict defined in application) / derived (defined by user as formula)
Definition: unit_converter.hh:56
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
BasicFactors::BasicFactors
BasicFactors()
Constructor.
Definition: unit_converter.cc:27
unit_si.hh
UnitConverter::basic_factors
static const BasicFactors basic_factors
Define all base and derived units given by their symbol.
Definition: unit_converter.hh:96
UnitConverter::unit_si_
UnitSI unit_si_
Definition: unit_converter.hh:142
TYPEDEF_ERR_INFO
TYPEDEF_ERR_INFO(EI_UnitDefinition, std::string)
Formula::Formula
Formula()
Constructor.
Definition: unit_converter.hh:60
Factor
Store structure given by parser.
Definition: unit_converter.hh:49
BasicFactors::units_map_
UnitsMap units_map_
Define all base and derived units given by their symbol.
Definition: unit_converter.hh:85
UnitSI::is_def
bool is_def() const
Return true if the unit is defined.
Definition: unit_si.cc:201
Factor::exponent_
int exponent_
exponent
Definition: unit_converter.hh:55