PISM, A Parallel Ice Sheet Model 2.3.3-e0e7dfb06 committed by Constantine Khrulev on 2026-10-20
Loading...
Searching...
No Matches
Units.hh
Go to the documentation of this file.
1/* Copyright (C) 2013, 2014, 2015, 2016, 2017, 2020, 2025 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
26namespace pism {
27
28namespace 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
44class System {
45public:
46 System(const std::string &path = "");
47 typedef std::shared_ptr<System> Ptr;
48private:
49 friend class Unit;
50
51 struct Impl;
52 std::shared_ptr<Impl> m_impl;
53
54 System(const System &);
56};
57
58double convert(System::Ptr system, double input,
59 const std::string &spec1, const std::string &spec2);
60
61struct DateTime {
62 DateTime(int year_, int month_, int day_, int hour_, int minute_, double second_,
63 const std::string &calendar_);
64 // parse date of the form Y-M-D and set hour, minute, second to zero
65 DateTime(const std::string &input, const std::string &calendar);
66
68 double second;
69 std::string calendar;
70};
71
72class Unit {
73public:
74 Unit(System::Ptr system, const std::string &spec);
75 Unit(const Unit &other);
76
77 bool is_convertible(const Unit &other) const;
78
79 DateTime date(double T, const std::string &calendar) const;
80 double time(const DateTime &d) const;
81
82 Unit& operator=(const Unit& other);
83 std::string string() const;
84
85 operator std::string() const;
86
87 System::Ptr system() const;
88private:
89 friend class Converter;
90 void reset();
91
92 struct Impl;
93 std::shared_ptr<Impl> m_impl;
94};
95
96/** Check if units are convertible without creating a converter.
97 *
98 * @param[in] u1 first Unit instance
99 * @param[in] u2 second Unit instance
100 *
101 * @return true if units are convertible, false otherwise
102 */
103bool are_convertible(const Unit &u1, const Unit &u2);
104
105/** Unit converter.
106 *
107 * Throws pism::RuntimeError() if the conversion is not possible.
108 *
109 */
111public:
112 Converter();
113 Converter(const Unit &u1, const Unit &u2);
114 Converter(System::Ptr sys, const std::string &u1, const std::string &u2);
115 /** Convert an array of doubles in place
116 *
117 * @param[in,out] data array to process
118 * @param length length of the array
119 */
120 void convert_doubles(double *data, size_t length) const;
121 double operator()(double input) const;
122private:
123
124 struct Impl;
125 std::shared_ptr<Impl> m_impl;
126
127 // hide copy constructor and the assignment operator
130};
131
132} // end of namespace units
133
134} // end of namespace pism
135
136#endif /* _PISMUNITS_H_ */
std::shared_ptr< Impl > m_impl
Definition Units.hh:125
Converter & operator=(Converter const &)
double operator()(double input) const
Definition Units.cc:258
void convert_doubles(double *data, size_t length) const
Definition Units.cc:262
Converter(const Converter &)
System & operator=(System const &)
std::shared_ptr< Impl > m_impl
Definition Units.hh:52
System(const System &)
std::shared_ptr< System > Ptr
Definition Units.hh:47
double time(const DateTime &d) const
Definition Units.cc:180
DateTime date(double T, const std::string &calendar) const
Definition Units.cc:164
Unit & operator=(const Unit &other)
Definition Units.cc:125
System::Ptr system() const
Definition Units.cc:160
std::string string() const
Definition Units.cc:147
std::shared_ptr< Impl > m_impl
Definition Units.hh:93
bool is_convertible(const Unit &other) const
Definition Units.cc:143
bool are_convertible(const Unit &u1, const Unit &u2)
Definition Units.cc:254
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:76
static std::string calendar(const File *input_file, const Config &config, const Logger &log)
Definition Time.cc:158
std::string calendar
Definition Units.hh:69