FEDEM Solver  R8.0
Source code of the dynamics solver
FFaUnitCalculator.H
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2023 SAP SE
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 // This file is part of FEDEM - https://openfedem.org
7 
8 #ifndef FFA_UNIT_CALCULATOR_H
9 #define FFA_UNIT_CALCULATOR_H
10 
11 #include <string>
12 #include <vector>
13 #include <map>
14 
16 
17 
19 {
20  struct SingleUnit
21  {
22  double factor;
23  std::string origUnit;
24  std::string convUnit;
25  };
26 
27  typedef std::map<std::string,SingleUnit> SingleUnitMap;
28  typedef SingleUnitMap::const_iterator SingleUnitMapIter;
29 
30 public:
31  FFaUnitCalculator(const char* n = NULL) : origGroup("SI"), convGroup("SI")
32  {
33  if (n) name = n;
34  }
35 
36  bool operator==(const FFaUnitCalculator& cal) const;
37 
38  bool isValid() const { return !name.empty(); }
39 
40  const std::string& getName() const { return name; }
41  const std::string& getOrigUnits() const { return origGroup; }
42  const std::string& getConvUnits() const { return convGroup; }
43 
44  template <class T> bool convert(T& value, const std::string& aName) const
45  {
46  SingleUnitMapIter it = myConvFactors.find(aName);
47  if (it == myConvFactors.end()) return false;
48 
49  value *= it->second.factor;
50  return true;
51  }
52 
54  bool convert(double& value, const std::string& aName, int prec) const;
55 
57  template <class T> bool inverse(T& value, const std::string& aName) const
58  {
59  SingleUnitMapIter it = myConvFactors.find(aName);
60  if (it == myConvFactors.end()) return false;
61 
62  value /= it->second.factor;
63  return true;
64  }
65 
66  double convFactor(const std::string& aName) const
67  {
68  SingleUnitMapIter it = myConvFactors.find(aName);
69  return it == myConvFactors.end() ? 0.0 : it->second.factor;
70  }
71 
72  std::string origUnitName(const std::string& aName) const
73  {
74  SingleUnitMapIter it = myConvFactors.find(aName);
75  return it == myConvFactors.end() ? std::string("") : it->second.origUnit;
76  }
77 
78  std::string convUnitName(const std::string& aName) const
79  {
80  SingleUnitMapIter it = myConvFactors.find(aName);
81  return it == myConvFactors.end() ? std::string("") : it->second.convUnit;
82  }
83 
84  void addConversion(const std::string& propName, double sf,
85  const std::string& origUnit = "",
86  const std::string& convUnit = "");
87 
88 private:
89  std::string name;
90  std::string origGroup;
91  std::string convGroup;
93 
94  friend std::ostream& operator<< (std::ostream& os, const FFaUnitCalculator& field);
95  friend std::istream& operator>> (std::istream& is, FFaUnitCalculator& field);
96 };
97 
98 
99 class FFaUnitCalculatorProvider : public FFaSingelton<FFaUnitCalculatorProvider>
100 {
101 public:
102  const FFaUnitCalculator* getCalculator(const std::string& calcName) const;
103 
104  void getCalculatorNames(std::vector<std::string>& definedCalcs) const;
105  std::vector<const FFaUnitCalculator*> getCalculators() const;
106 
107  void addCalculator(const FFaUnitCalculator& calc);
108 
110  bool readCalculatorDefs(const std::string& filename);
112  bool printCalculatorDefs(const std::string& filename) const;
113 
114 protected:
117 
118 private:
119  std::map<std::string,FFaUnitCalculator> myCalcs;
120 
122 };
123 
124 #endif
Template class for singleton classes.
Definition: FFaSingelton.H:20
static T * instance()
Returns the actual instance of this class.
Definition: FFaSingelton.H:32
Definition: FFaUnitCalculator.H:100
FFaUnitCalculatorProvider()
Definition: FFaUnitCalculator.H:115
virtual ~FFaUnitCalculatorProvider()
Definition: FFaUnitCalculator.H:116
void addCalculator(const FFaUnitCalculator &calc)
Definition: FFaUnitCalculator.C:167
std::map< std::string, FFaUnitCalculator > myCalcs
Definition: FFaUnitCalculator.H:119
bool readCalculatorDefs(const std::string &filename)
Reads/writes calculator data from file.
Definition: FFaUnitCalculator.C:177
bool printCalculatorDefs(const std::string &filename) const
Writes calculator data to file.
Definition: FFaUnitCalculator.C:214
void getCalculatorNames(std::vector< std::string > &definedCalcs) const
Definition: FFaUnitCalculator.C:159
std::vector< const FFaUnitCalculator * > getCalculators() const
Definition: FFaUnitCalculator.C:148
const FFaUnitCalculator * getCalculator(const std::string &calcName) const
Definition: FFaUnitCalculator.C:133
Definition: FFaUnitCalculator.H:19
bool inverse(T &value, const std::string &aName) const
Reversed conversion.
Definition: FFaUnitCalculator.H:57
std::string origGroup
Definition: FFaUnitCalculator.H:90
const std::string & getConvUnits() const
Definition: FFaUnitCalculator.H:42
bool operator==(const FFaUnitCalculator &cal) const
Definition: FFaUnitCalculator.C:16
std::string convUnitName(const std::string &aName) const
Definition: FFaUnitCalculator.H:78
bool convert(T &value, const std::string &aName) const
Definition: FFaUnitCalculator.H:44
std::map< std::string, SingleUnit > SingleUnitMap
Definition: FFaUnitCalculator.H:27
friend std::istream & operator>>(std::istream &is, FFaUnitCalculator &field)
Definition: FFaUnitCalculator.C:87
SingleUnitMap myConvFactors
Definition: FFaUnitCalculator.H:92
const std::string & getName() const
Definition: FFaUnitCalculator.H:40
std::string origUnitName(const std::string &aName) const
Definition: FFaUnitCalculator.H:72
std::string convGroup
Definition: FFaUnitCalculator.H:91
bool isValid() const
Definition: FFaUnitCalculator.H:38
FFaUnitCalculator(const char *n=NULL)
Definition: FFaUnitCalculator.H:31
SingleUnitMap::const_iterator SingleUnitMapIter
Definition: FFaUnitCalculator.H:28
double convFactor(const std::string &aName) const
Definition: FFaUnitCalculator.H:66
const std::string & getOrigUnits() const
Definition: FFaUnitCalculator.H:41
void addConversion(const std::string &propName, double sf, const std::string &origUnit="", const std::string &convUnit="")
Definition: FFaUnitCalculator.C:57
std::string name
Definition: FFaUnitCalculator.H:89
friend std::ostream & operator<<(std::ostream &os, const FFaUnitCalculator &field)
Definition: FFaUnitCalculator.C:67
character(lfnam_p) filename
Name of function data file.
Definition: initiateFunctionTypeModule.f90:34
Definition: FFaUnitCalculator.H:21
std::string origUnit
Definition: FFaUnitCalculator.H:23
double factor
Definition: FFaUnitCalculator.H:22
std::string convUnit
Definition: FFaUnitCalculator.H:24