Loading [MathJax]/extensions/tex2jax.js
PISM, A Parallel Ice Sheet Model 2.2.2-d6b3a29ca committed by Constantine Khrulev on 2025-03-28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pism_utilities.hh
Go to the documentation of this file.
1/* Copyright (C) 2016, 2017, 2018, 2019, 2020, 2021, 2023, 2024 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
32namespace pism {
33
34/*!
35 * Compute vertically-integrated water column pressure.
36 */
37double 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
47double get_time(MPI_Comm comm);
48std::string timestamp(MPI_Comm com);
49std::string username_prefix(MPI_Comm com);
50std::string args_string();
51std::string filename_add_suffix(const std::string &filename,
52 const std::string &separator,
53 const std::string &suffix);
54
55double wall_clock_hours(MPI_Comm com, double start_time);
56
57
58// array
59bool is_increasing(const std::vector<double> &a);
60
61// string
62bool ends_with(const std::string &str, const std::string &suffix);
63
64// remove leading and trailing whitespace
65std::string string_strip(const std::string &input);
66
67std::string join(const std::vector<std::string> &strings, const std::string &separator);
68
69std::vector<std::string> split(const std::string &input, char separator);
70
71std::set<std::string> set_split(const std::string &input, char separator);
72
73std::string set_join(const std::set<std::string> &input, const std::string& separator);
74
75std::string replace_character(const std::string &input, char from, char to);
76
77// set
78bool 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 */
94template<typename T>
95T 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
103template<typename T>
104inline T clip(T x, T a, T b) {
105 return std::min(std::max(a, x), b);
106}
107
108double vector_min(const std::vector<double> &input);
109
110double vector_max(const std::vector<double> &input);
111
112// parallel
113void GlobalReduce(MPI_Comm comm, double *local, double *result, int count, MPI_Op op);
114
115void GlobalReduce(MPI_Comm comm, int *local, int *result, int count, MPI_Op op);
116
117void GlobalMin(MPI_Comm comm, double *local, double *result, int count);
118
119void GlobalMax(MPI_Comm comm, double *local, double *result, int count);
120
121void GlobalMax(MPI_Comm comm, int *local, int *result, int count);
122
123void GlobalSum(MPI_Comm comm, double *local, double *result, int count);
124
125void GlobalSum(MPI_Comm comm, int *local, int *result, int count);
126
127double GlobalMin(MPI_Comm comm, double local);
128
129double GlobalMax(MPI_Comm comm, double local);
130
131double GlobalSum(MPI_Comm comm, double local);
132
133unsigned int GlobalSum(MPI_Comm comm, unsigned int input);
134
135int GlobalSum(MPI_Comm comm, int input);
136
137std::string version();
138
139//! return NetCDF version as an integer
140int netcdf_version();
141
142std::string printf(const char *format, ...) __attribute__((format(printf, 1, 2)));
143
144void validate_format_string(const std::string &format);
145
146uint64_t fletcher64(const uint32_t *data, size_t len);
147
148void print_checksum(MPI_Comm com,
149 const std::vector<double> &data,
150 const char *label);
151
152void print_vector(MPI_Comm com,
153 const std::vector<double> &data,
154 const char *label);
155
156void print_vector(MPI_Comm com,
157 const std::vector<int> &data,
158 const char *label);
159
160double parse_number(const std::string &input);
161
162std::vector<double> parse_number_list(const std::string &input);
163
164long int parse_integer(const std::string &input);
165
166std::vector<long> parse_integer_list(const std::string &input);
167
168} // end of namespace pism
169
170
171#endif /* PISM_UTILITIES_H */
const double rho_ice
Definition exactTestK.c:31
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.
std::vector< long > parse_integer_list(const std::string &input)
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.
std::vector< double > parse_number_list(const std::string &input)
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,...)
int netcdf_version()
return NetCDF version as an integer
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)
static double start_time(const Config &config, const File *file, const std::string &reference_date, const std::string &calendar, const units::Unit &time_units)
Definition Time.cc:349
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