PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
pism_utilities.hh
Go to the documentation of this file.
1 /* Copyright (C) 2016, 2017, 2018, 2019, 2020, 2021, 2023 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 3 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 PISM_UTILITIES_H
21 #define PISM_UTILITIES_H
22 
23 #include <cstdint> // uint32_t
24 
25 #include <algorithm> // std::min, std::max
26 #include <set>
27 #include <string>
28 #include <vector>
29 
30 #include <mpi.h>
31 
32 namespace pism {
33 
34 /*!
35  * Compute vertically-integrated water column pressure.
36  */
37 double average_water_column_pressure(double ice_thickness, double bed,
38  double floatation_level,
39  double rho_ice, double rho_water, double g);
40 
41 // Utilities that do not expose PETSc's or PISM's API.
42 
43 #ifndef __GNUC__
44 # define __attribute__(x) /* nothing */
45 #endif
46 
47 double get_time(MPI_Comm comm);
48 std::string timestamp(MPI_Comm com);
49 std::string username_prefix(MPI_Comm com);
50 std::string args_string();
51 std::string filename_add_suffix(const std::string &filename,
52  const std::string &separator,
53  const std::string &suffix);
54 
55 double wall_clock_hours(MPI_Comm com, double start_time);
56 
57 
58 // array
59 bool is_increasing(const std::vector<double> &a);
60 
61 // string
62 bool ends_with(const std::string &str, const std::string &suffix);
63 
64 // remove leading and trailing whitespace
65 std::string string_strip(const std::string &input);
66 
67 std::string join(const std::vector<std::string> &strings, const std::string &separator);
68 
69 std::vector<std::string> split(const std::string &input, char separator);
70 
71 std::set<std::string> set_split(const std::string &input, char separator);
72 
73 std::string set_join(const std::set<std::string> &input, const std::string& separator);
74 
75 std::string replace_character(const std::string &input, char from, char to);
76 
77 // set
78 bool member(const std::string &string, const std::set<std::string> &set);
79 
80 /*! Helper template function for computing set unions.
81  * Ensures that elements of a take precedence. For example, if
82  *
83  * a = {{1, 2}, {3, 4}}
84  * b = {{1, 4}, {5, 6}}
85  *
86  * combine(a, b) will use the pair {1, 2} from a, not {1, 4} from b.
87  *
88  * This behavior relies on the fact that std::map::insert({a, b}) is a no-op if a key equivalent to
89  * a is already present.
90  *
91  * This is similar to a set union, but it is not symmetric. (I would expect set_union(a, b) to be
92  * the same as set_union(b, a)).
93  */
94 template<typename T>
95 T combine(const T &a, const T&b) {
96  T result = a;
97  for (const auto &element : b) {
98  result.insert(element);
99  }
100  return result;
101 }
102 
103 template<typename T>
104 inline T clip(T x, T a, T b) {
105  return std::min(std::max(a, x), b);
106 }
107 
108 double vector_min(const std::vector<double> &input);
109 
110 double vector_max(const std::vector<double> &input);
111 
112 // parallel
113 void GlobalReduce(MPI_Comm comm, double *local, double *result, int count, MPI_Op op);
114 
115 void GlobalReduce(MPI_Comm comm, int *local, int *result, int count, MPI_Op op);
116 
117 void GlobalMin(MPI_Comm comm, double *local, double *result, int count);
118 
119 void GlobalMax(MPI_Comm comm, double *local, double *result, int count);
120 
121 void GlobalMax(MPI_Comm comm, int *local, int *result, int count);
122 
123 void GlobalSum(MPI_Comm comm, double *local, double *result, int count);
124 
125 void GlobalSum(MPI_Comm comm, int *local, int *result, int count);
126 
127 double GlobalMin(MPI_Comm comm, double local);
128 
129 double GlobalMax(MPI_Comm comm, double local);
130 
131 double GlobalSum(MPI_Comm comm, double local);
132 
133 unsigned int GlobalSum(MPI_Comm comm, unsigned int input);
134 
135 int GlobalSum(MPI_Comm comm, int input);
136 
137 std::string version();
138 
139 std::string printf(const char *format, ...) __attribute__((format(printf, 1, 2)));
140 
141 void validate_format_string(const std::string &format);
142 
143 uint64_t fletcher64(const uint32_t *data, size_t len);
144 
145 void print_checksum(MPI_Comm com,
146  const std::vector<double> &data,
147  const char *label);
148 
149 void print_vector(MPI_Comm com,
150  const std::vector<double> &data,
151  const char *label);
152 
153 void print_vector(MPI_Comm com,
154  const std::vector<int> &data,
155  const char *label);
156 
157 double parse_number(const std::string &input);
158 
159 long int parse_integer(const std::string &input);
160 
161 } // end of namespace pism
162 
163 
164 #endif /* PISM_UTILITIES_H */
const double rho_ice
Definition: exactTestK.c:31
double max(const array::Scalar &input)
Finds maximum over all the values in an array::Scalar object. Ignores ghosts.
Definition: Scalar.cc:165
double min(const array::Scalar &input)
Finds minimum over all the values in an array::Scalar object. Ignores ghosts.
Definition: Scalar.cc:193
double get_time(MPI_Comm comm)
bool is_increasing(const std::vector< double > &a)
Checks if a vector of doubles is strictly increasing.
double parse_number(const std::string &input)
double average_water_column_pressure(double ice_thickness, double bed, double floatation_level, double rho_ice, double rho_water, double g)
double wall_clock_hours(MPI_Comm com, double start_time)
Return time since the beginning of the run, in hours.
static double start_time(const Config &config, const Logger &log, const File *file, const std::string &reference_date, const std::string &calendar, const units::Unit &time_units)
Definition: Time.cc:348
void print_checksum(MPI_Comm com, const std::vector< double > &data, const char *label)
static const double g
Definition: exactTestP.cc:36
bool ends_with(const std::string &str, const std::string &suffix)
Returns true if str ends with suffix and false otherwise.
void GlobalMax(MPI_Comm comm, double *local, double *result, int count)
T clip(T x, T a, T b)
std::string set_join(const std::set< std::string > &input, const std::string &separator)
std::string printf(const char *format,...)
void print_vector(MPI_Comm com, const std::vector< double > &data, const char *label)
uint64_t fletcher64(const uint32_t *data, size_t length)
void validate_format_string(const std::string &format)
std::set< std::string > set_split(const std::string &input, char separator)
Transform a separator-separated list (a string) into a set of strings.
double vector_min(const std::vector< double > &input)
std::string string_strip(const std::string &input)
std::string filename_add_suffix(const std::string &filename, const std::string &separator, const std::string &suffix)
Adds a suffix to a filename.
std::string version()
std::string timestamp(MPI_Comm com)
Creates a time-stamp used for the history NetCDF attribute.
bool member(const std::string &string, const std::set< std::string > &set)
void GlobalMin(MPI_Comm comm, double *local, double *result, int count)
std::string args_string()
Uses argc and argv to create the string with current PISM command-line arguments.
T combine(const T &a, const T &b)
long int parse_integer(const std::string &input)
std::string join(const std::vector< std::string > &strings, const std::string &separator)
Concatenate strings, inserting separator between elements.
void GlobalSum(MPI_Comm comm, double *local, double *result, int count)
std::string username_prefix(MPI_Comm com)
Creates a string with the user name, hostname and the time-stamp (for history strings).
void GlobalReduce(MPI_Comm comm, double *local, double *result, int count, MPI_Op op)
std::string replace_character(const std::string &input, char from, char to)
double vector_max(const std::vector< double > &input)
std::vector< std::string > split(const std::string &input, char separator)
Transform a separator-separated list (a string) into a vector of strings.
#define __attribute__(x)
int count
Definition: test_cube.c:16