PISM, A Parallel Ice Sheet Model  stable v2.1.1 committed by Constantine Khrulev on 2024-12-04 13:36:58 -0900
Units.hh
Go to the documentation of this file.
1 /* Copyright (C) 2013, 2014, 2015, 2016, 2017, 2020 PISM Authors
2  *
3  * This file is part of PISM.
4  *
5  * PISM is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your option) any later
8  * version.
9  *
10  * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with PISM; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #ifndef _PISMUNITS_H_
21 #define _PISMUNITS_H_
22 
23 #include <string>
24 #include <memory>
25 
26 namespace pism {
27 
28 namespace units {
29 
30 /** @file Units.hh This file contains thin wrappers around
31  * UDUNITS-2 objects. Nothing fancy. The only purpose is to simplify
32  * memory management for objects that are stored as data members of
33  * other C++ classes.
34  *
35  * One thing is worth mentioning, though: in UDUNITS-2, every ut_unit
36  * object contains a pointer to the unit system that was used to create it.
37  *
38  * We use C++ shared pointers to make sure that the system a
39  * Unit instance needs is allocated during the whole life span of
40  * this instance. (De-allocating the unit system too early results in
41  * having a "dangling" pointer.)
42  */
43 
44 class System {
45 public:
46  System(const std::string &path = "");
47  typedef std::shared_ptr<System> Ptr;
48 private:
49  friend class Unit;
50 
51  struct Impl;
52  std::shared_ptr<Impl> m_impl;
53 
54  System(const System &);
55  System& operator=(System const &);
56 };
57 
58 double convert(System::Ptr system, double input,
59  const std::string &spec1, const std::string &spec2);
60 
61 struct DateTime {
63  double second;
64 };
65 
66 class Unit {
67 public:
68  Unit(System::Ptr system, const std::string &spec);
69  Unit(const Unit &other);
70 
71  bool is_convertible(const Unit &other) const;
72 
73  DateTime date(double T, const std::string &calendar) const;
74  double time(const DateTime &d, const std::string &calendar) const;
75 
76  Unit& operator=(const Unit& other);
77  std::string format() const;
78 
79  System::Ptr system() const;
80 private:
81  friend class Converter;
82  void reset();
83 
84  struct Impl;
85  std::shared_ptr<Impl> m_impl;
86 };
87 
88 /** Check if units are convertible without creating a converter.
89  *
90  * @param[in] u1 first Unit instance
91  * @param[in] u2 second Unit instance
92  *
93  * @return true if units are convertible, false otherwise
94  */
95 bool are_convertible(const Unit &u1, const Unit &u2);
96 
97 /** Unit converter.
98  *
99  * Throws pism::RuntimeError() if the conversion is not possible.
100  *
101  */
102 class Converter {
103 public:
104  Converter();
105  Converter(const Unit &u1, const Unit &u2);
106  Converter(System::Ptr sys, const std::string &u1, const std::string &u2);
107  /** Convert an array of doubles in place
108  *
109  * @param[in,out] data array to process
110  * @param length length of the array
111  */
112  void convert_doubles(double *data, size_t length) const;
113  double operator()(double input) const;
114 private:
115 
116  struct Impl;
117  std::shared_ptr<Impl> m_impl;
118 
119  // hide copy constructor and the assignment operator
122 };
123 
124 } // end of namespace units
125 
126 } // end of namespace pism
127 
128 #endif /* _PISMUNITS_H_ */
std::shared_ptr< Impl > m_impl
Definition: Units.hh:116
double operator()(double input) const
Definition: Units.cc:246
void convert_doubles(double *data, size_t length) const
Definition: Units.cc:250
Converter(const Converter &)
Converter & operator=(Converter const &)
System(const std::string &path="")
Definition: Units.cc:60
std::shared_ptr< Impl > m_impl
Definition: Units.hh:51
System(const System &)
System & operator=(System const &)
std::shared_ptr< System > Ptr
Definition: Units.hh:47
DateTime date(double T, const std::string &calendar) const
Definition: Units.cc:153
std::string format() const
Definition: Units.cc:141
Unit & operator=(const Unit &other)
Definition: Units.cc:119
System::Ptr system() const
Definition: Units.cc:149
double time(const DateTime &d, const std::string &calendar) const
Definition: Units.cc:168
Unit(System::Ptr system, const std::string &spec)
Definition: Units.cc:111
std::shared_ptr< Impl > m_impl
Definition: Units.hh:84
bool is_convertible(const Unit &other) const
Definition: Units.cc:137
void reset()
Definition: Units.cc:145
bool are_convertible(const Unit &u1, const Unit &u2)
Definition: Units.cc:242
double convert(System::Ptr system, double input, const std::string &spec1, const std::string &spec2)
Convert a quantity from unit1 to unit2.
Definition: Units.cc:70
static std::string calendar(const File *input_file, const Config &config, const Logger &log)
Definition: Time.cc:146