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
ConfigInterface.hh
Go to the documentation of this file.
1/* Copyright (C) 2015, 2016, 2017, 2018, 2021, 2022, 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 _PISMCONFIGINTERFACE_H_
21#define _PISMCONFIGINTERFACE_H_
22
23#include <memory>
24#include <set>
25#include <map>
26#include <string>
27#include <vector>
28#include <utility> // std::pair
29
30#include <mpi.h> // MPI_Comm
31
32#include "pism/util/Units.hh"
33
34namespace pism {
35
36class File;
37class Logger;
38
39
40//! Flag used by `set_...()` methods.
41/** Meanings:
42 *
43 * - `DEFAULT`: set the default value; has no effect if a parameter was set by a user at the time
44 * of the call
45 * - `FORCE`: forcibly set a parameter; unconditionally overrides previous values
46 * - `USER`: forcibly set a parameter; unconditionally overrides previous values and marks this
47 * parameter as set by the user. This affects future `set_...()` calls using the `DEFAULT` flag
48 * value and results of `parameters_set_by_user()`.
49 */
51
52
53//! A class for storing and accessing PISM configuration flags and parameters.
54class Config {
55public:
56 typedef std::shared_ptr<Config> Ptr;
57 typedef std::shared_ptr<const Config> ConstPtr;
58
59 Config(units::System::Ptr unit_system);
60 virtual ~Config();
61
62 //! Flag used by `get_...()` methods.
63 /** Meanings:
64 *
65 * - `REMEMBER_THIS_USE` (the default): add the name of a parameter to the list of parameters used
66 * by a model run.
67 * - `FORGET_THIS_USE`: don't add the name of a parameter to the list of used parameters. This is
68 * necessary to be able to get the current value of a parameter to be used as the default when
69 * processing a command-line option.
70 */
72
73 // Import settings from an override file
74 void import_from(const Config &other);
75
76 // Use `realpath()` to resolve relative file names.
77 void resolve_filenames();
78
79 const std::set<std::string>& parameters_set_by_user() const;
80 const std::set<std::string>& parameters_used() const;
81
82 void read(MPI_Comm com, const std::string &filename);
83 void write(MPI_Comm com, const std::string &filename, bool append = true) const;
84 std::string filename() const;
85
86 void read(const File &file);
87 void write(const File &file) const;
88
89 bool is_set(const std::string &name) const;
90
91 //! Return true if the numeric parameter `name` is set to a value within its valid
92 //! range.
93 bool is_valid_number(const std::string &name) const;
94
95 // doubles
96 typedef std::map<std::string, std::vector<double> > Doubles;
97 Doubles all_doubles() const;
98
99 double get_number(const std::string &name, UseFlag flag = REMEMBER_THIS_USE) const;
100 double get_number(const std::string &name, const std::string &units,
101 UseFlag flag = REMEMBER_THIS_USE) const;
102 std::vector<double> get_numbers(const std::string &name, UseFlag flag = REMEMBER_THIS_USE) const;
103 std::vector<double> get_numbers(const std::string &name, const std::string &units,
104 UseFlag flag = REMEMBER_THIS_USE) const;
105
106 void set_number(const std::string &name, double value, ConfigSettingFlag flag = CONFIG_FORCE);
107 void set_numbers(const std::string &name, const std::vector<double> &values,
109
110 // strings
111 typedef std::map<std::string, std::string> Strings;
112 Strings all_strings() const;
113
114 std::string get_string(const std::string &name, UseFlag flag = REMEMBER_THIS_USE) const;
115 void set_string(const std::string &name, const std::string &value, ConfigSettingFlag flag = CONFIG_FORCE);
116
117 // flags
118 typedef std::map<std::string, bool> Flags;
119 Flags all_flags() const;
120
121 std::set<std::string> keys() const;
122
123 bool get_flag(const std::string& name, UseFlag flag = REMEMBER_THIS_USE) const;
124 void set_flag(const std::string& name, bool value, ConfigSettingFlag flag = CONFIG_FORCE);
125
126 std::string doc(const std::string &parameter) const;
127 std::string units(const std::string &parameter) const;
128 std::string type(const std::string &parameter) const;
129 std::string option(const std::string &parameter) const;
130 std::string choices(const std::string &parameter) const;
131 std::pair<bool, double> valid_min(const std::string &parameter) const;
132 std::pair<bool, double> valid_max(const std::string &parameter) const;
133
134 // Implementations
135protected:
136 virtual void read_impl(const File &nc) = 0;
137 virtual void write_impl(const File &nc) const = 0;
138
139 virtual bool is_set_impl(const std::string &name) const = 0;
140
141 virtual Doubles all_doubles_impl() const = 0;
142 virtual double get_number_impl(const std::string &name) const = 0;
143 virtual std::vector<double> get_numbers_impl(const std::string &name) const = 0;
144
145 virtual void set_number_impl(const std::string &name, double value) = 0;
146 virtual void set_numbers_impl(const std::string &name,
147 const std::vector<double> &values) = 0;
148
149 virtual Strings all_strings_impl() const = 0;
150 virtual std::string get_string_impl(const std::string &name) const = 0;
151 virtual void set_string_impl(const std::string &name, const std::string &value) = 0;
152
153 virtual Flags all_flags_impl() const = 0;
154
155 virtual bool get_flag_impl(const std::string& name) const = 0;
156 virtual void set_flag_impl(const std::string& name, bool value) = 0;
157private:
158 struct Impl;
160};
161
163public:
164 ConfigWithPrefix(Config::ConstPtr c, const std::string &prefix);
165
166 double get_number(const std::string &name) const;
167 double get_number(const std::string &name, const std::string &units) const;
168
169 std::string get_string(const std::string &name) const;
170
171 bool get_flag(const std::string& name) const;
172
173 void reset_prefix(const std::string &prefix);
174
175private:
176 std::string m_prefix;
178};
179
180Config::Ptr config_from_options(MPI_Comm com, const Logger &log, units::System::Ptr unit_system);
181
182//! Set configuration parameters using command-line options.
183void set_config_from_options(units::System::Ptr unit_system, Config &config);
184
185//! Set one parameter using command-line options.
186void set_parameter_from_options(units::System::Ptr unit_system, Config &config, const std::string &name);
187
188//! Set one flag parameter using command-line options.
189void set_flag_from_option(Config &config,
190 const std::string &option,const std::string &parameter);
191
192//! Set one scalar parameter using command-line options.
193void set_number_from_option(units::System::Ptr unit_system, Config &config,
194 const std::string &option, const std::string &parameter);
195
196//! Set one free-form string parameter using command-line options.
197void set_string_from_option(Config &config,
198 const std::string &option, const std::string &parameter);
199
200//! Set one keyword parameter using command-line options.
202 const std::string &option, const std::string &parameter,
203 const std::string &choices);
204
205//! Report configuration parameters to `stdout`.
206void print_config(const Logger &log, int verbosity_threshhold, const Config &config);
207
208//! Report unused configuration parameters to `stdout`.
209void print_unused_parameters(const Logger &log, int verbosity_threshhold,
210 const Config &config);
211
212} // end of namespace pism
213
214#endif /* _PISMCONFIGINTERFACE_H_ */
void reset_prefix(const std::string &prefix)
bool get_flag(const std::string &name) const
std::string get_string(const std::string &name) const
double get_number(const std::string &name) const
Config::ConstPtr m_config
virtual bool is_set_impl(const std::string &name) const =0
std::map< std::string, std::string > Strings
const std::set< std::string > & parameters_used() const
std::shared_ptr< Config > Ptr
bool is_set(const std::string &name) const
std::string choices(const std::string &parameter) const
virtual Strings all_strings_impl() const =0
std::set< std::string > keys() const
std::pair< bool, double > valid_max(const std::string &parameter) const
std::map< std::string, std::vector< double > > Doubles
void set_string(const std::string &name, const std::string &value, ConfigSettingFlag flag=CONFIG_FORCE)
double get_number(const std::string &name, UseFlag flag=REMEMBER_THIS_USE) const
virtual void set_numbers_impl(const std::string &name, const std::vector< double > &values)=0
Flags all_flags() const
std::vector< double > get_numbers(const std::string &name, UseFlag flag=REMEMBER_THIS_USE) const
virtual void read_impl(const File &nc)=0
std::shared_ptr< const Config > ConstPtr
virtual Flags all_flags_impl() const =0
virtual std::vector< double > get_numbers_impl(const std::string &name) const =0
const std::set< std::string > & parameters_set_by_user() const
virtual void set_flag_impl(const std::string &name, bool value)=0
void read(MPI_Comm com, const std::string &filename)
bool is_valid_number(const std::string &name) const
virtual void set_string_impl(const std::string &name, const std::string &value)=0
std::string units(const std::string &parameter) const
void import_from(const Config &other)
void set_flag(const std::string &name, bool value, ConfigSettingFlag flag=CONFIG_FORCE)
virtual std::string get_string_impl(const std::string &name) const =0
std::map< std::string, bool > Flags
std::string get_string(const std::string &name, UseFlag flag=REMEMBER_THIS_USE) const
std::string type(const std::string &parameter) const
virtual ~Config()
virtual Doubles all_doubles_impl() const =0
virtual void set_number_impl(const std::string &name, double value)=0
void write(MPI_Comm com, const std::string &filename, bool append=true) const
bool get_flag(const std::string &name, UseFlag flag=REMEMBER_THIS_USE) const
void set_number(const std::string &name, double value, ConfigSettingFlag flag=CONFIG_FORCE)
std::string option(const std::string &parameter) const
std::string filename() const
Returns the name of the file used to initialize the database.
Strings all_strings() const
virtual void write_impl(const File &nc) const =0
void set_numbers(const std::string &name, const std::vector< double > &values, ConfigSettingFlag flag=CONFIG_FORCE)
Doubles all_doubles() const
std::string doc(const std::string &parameter) const
UseFlag
Flag used by get_...() methods.
std::pair< bool, double > valid_min(const std::string &parameter) const
virtual double get_number_impl(const std::string &name) const =0
virtual bool get_flag_impl(const std::string &name) const =0
A class for storing and accessing PISM configuration flags and parameters.
High-level PISM I/O class.
Definition File.hh:55
A basic logging class.
Definition Logger.hh:40
std::shared_ptr< System > Ptr
Definition Units.hh:47
Config::Ptr config_from_options(MPI_Comm com, const Logger &log, units::System::Ptr unit_system)
Create a configuration database using command-line options.
void set_number_from_option(units::System::Ptr unit_system, Config &config, const std::string &option, const std::string &parameter)
Sets a configuration parameter from a command-line option.
void set_string_from_option(Config &config, const std::string &option, const std::string &parameter)
Set one free-form string parameter using command-line options.
void set_config_from_options(units::System::Ptr unit_system, Config &config)
Set configuration parameters using command-line options.
void set_flag_from_option(Config &config, const std::string &option, const std::string &parameter_name)
Get a flag from a command-line option.
ConfigSettingFlag
Flag used by set_...() methods.
void print_unused_parameters(const Logger &log, int verbosity_threshhold, const Config &config)
Report unused configuration parameters to stdout.
void set_keyword_from_option(Config &config, const std::string &option, const std::string &parameter, const std::string &choices)
Set a keyword parameter from a command-line option.
void set_parameter_from_options(units::System::Ptr unit_system, Config &config, const std::string &name)
Set one parameter using command-line options.
void print_config(const Logger &log, int verbosity_threshhold, const Config &config)
Report configuration parameters to stdout.