PISM, A Parallel Ice Sheet Model  stable v2.0.4 committed by Constantine Khrulev on 2022-05-25 12:02:27 -0800
Context.cc
Go to the documentation of this file.
1 /* Copyright (C) 2014, 2015, 2017, 2019, 2021 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 #include "Context.hh"
21 #include "Profiling.hh"
22 #include "Units.hh"
23 #include "Config.hh"
24 #include "Time.hh"
25 #include "Logger.hh"
26 #include "pism/util/EnthalpyConverter.hh"
27 #include "pism/util/error_handling.hh"
28 #include "pism/pism_config.hh"
29 
30 #if (Pism_USE_PIO==1)
31 // Why do I need this???
32 #define _NETCDF
33 #include <pio.h>
34 #endif
35 
36 namespace pism {
37 
39 public:
40  Impl(MPI_Comm c,
41  UnitsSystemPtr sys,
42  ConfigPtr conf,
44  TimePtr t,
45  LoggerPtr log,
46  const std::string &p)
47  : com(c), unit_system(sys), config(conf), enthalpy_converter(EC), time(t), prefix(p),
48  logger(log), pio_iosys_id(-1) {
49  // empty
50  }
51  MPI_Comm com;
56  std::string prefix;
60 };
61 
64  LoggerPtr L,
65  const std::string &p)
66  : m_impl(new Impl(c, sys, config, EC, t, L, p)) {
67  // empty
68 }
69 
71 
72 #if (Pism_USE_PIO==1)
73  if (m_impl->pio_iosys_id != -1 and
74  PIOc_free_iosystem(m_impl->pio_iosys_id) != PIO_NOERR) {
75  m_impl->logger->message(1, "Error: failed to de-allocate a ParallelIO I/O system\n");
76  }
77 #endif
78 
79  delete m_impl;
80 }
81 
82 MPI_Comm Context::com() const {
83  return m_impl->com;
84 }
85 
86 int Context::size() const {
87  int S = 0;
88  MPI_Comm_size(m_impl->com, &S);
89  return S;
90 }
91 
92 int Context::rank() const {
93  int R = 0;
94  MPI_Comm_rank(m_impl->com, &R);
95  return R;
96 }
97 
99  return m_impl->unit_system;
100 }
101 
103  return m_impl->config;
104 }
105 
107  return m_impl->config;
108 }
109 
111  return m_impl->enthalpy_converter;
112 }
113 
115  return m_impl->time;
116 }
117 
119  return m_impl->time;
120 }
121 
122 const std::string& Context::prefix() const {
123  return m_impl->prefix;
124 }
125 
127  return m_impl->profiling;
128 }
129 
131  return m_impl->logger;
132 }
133 
135  return m_impl->logger;
136 }
137 
138 /*!
139  * I/O system id (the ParallelIO library)
140  */
142 #if (Pism_USE_PIO==1)
143  if (m_impl->pio_iosys_id == -1) {
144  int ierr = PIOc_set_iosystem_error_handling(PIO_DEFAULT, PIO_BCAST_ERROR, NULL);
145  if (ierr != 0) {
146  throw RuntimeError::formatted(PISM_ERROR_LOCATION, "Failed to initialize ParallelIO");
147  }
148 
149  int
150  base = config()->get_number("output.pio.base"),
151  stride = config()->get_number("output.pio.stride"),
152  n_writers = config()->get_number("output.pio.n_writers");
153 
154  if (n_writers > this->size()) {
156  "number of ParallelIO writers (%d)"
157  " exceeds the communicator size (%d)",
158  n_writers, this->size());
159  }
160 
161  ierr = PIOc_Init_Intracomm(m_impl->com, n_writers, stride, base, PIO_REARR_BOX,
162  &m_impl->pio_iosys_id);
163  if (ierr != 0) {
164  throw RuntimeError::formatted(PISM_ERROR_LOCATION, "Failed to initialize ParallelIO");
165  }
166  }
167 #endif
168  return m_impl->pio_iosys_id;
169 }
170 
171 std::shared_ptr<Context> context_from_options(MPI_Comm com,
172  const std::string &prefix,
173  bool print) {
174  // unit system
176 
177  // logger
178  Logger::Ptr logger = logger_from_options(com);
179 
180  // configuration parameters
181  Config::Ptr config = config_from_options(com, *logger, sys);
182 
183  if (print) {
184  print_config(*logger, 3, *config);
185  }
186 
187  // time manager
188  Time::Ptr time = std::make_shared<Time>(com, config, *logger, sys);
189 
190  // enthalpy converter
191  EnthalpyConverter::Ptr EC(new EnthalpyConverter(*config));
192 
193  return std::shared_ptr<Context>(new Context(com, sys, config, EC, time, logger, prefix));
194 }
195 
196 
197 } // end of namespace pism
std::shared_ptr< Config > Ptr
LoggerPtr logger
Definition: Context.cc:58
Profiling profiling
Definition: Context.cc:57
Impl(MPI_Comm c, UnitsSystemPtr sys, ConfigPtr conf, EnthalpyConverterPtr EC, TimePtr t, LoggerPtr log, const std::string &p)
Definition: Context.cc:40
std::string prefix
Definition: Context.cc:56
UnitsSystemPtr unit_system
Definition: Context.cc:52
ConfigPtr config
Definition: Context.cc:53
EnthalpyConverterPtr enthalpy_converter
Definition: Context.cc:54
std::shared_ptr< units::System > UnitsSystemPtr
Definition: Context.hh:42
MPI_Comm com() const
Definition: Context.cc:82
EnthalpyConverterPtr enthalpy_converter() const
Definition: Context.cc:110
Impl * m_impl
Definition: Context.hh:75
ConstConfigPtr config() const
Definition: Context.cc:106
std::shared_ptr< const Config > ConstConfigPtr
Definition: Context.hh:44
int pio_iosys_id() const
Definition: Context.cc:141
const std::string & prefix() const
Definition: Context.cc:122
int size() const
Definition: Context.cc:86
const Profiling & profiling() const
Definition: Context.cc:126
std::shared_ptr< const Logger > ConstLoggerPtr
Definition: Context.hh:49
Context(MPI_Comm c, UnitsSystemPtr sys, ConfigPtr conf, EnthalpyConverterPtr EC, TimePtr t, LoggerPtr log, const std::string &p)
Definition: Context.cc:62
int rank() const
Definition: Context.cc:92
UnitsSystemPtr unit_system() const
Definition: Context.cc:98
std::shared_ptr< EnthalpyConverter > EnthalpyConverterPtr
Definition: Context.hh:45
std::shared_ptr< Logger > LoggerPtr
Definition: Context.hh:48
std::shared_ptr< Config > ConfigPtr
Definition: Context.hh:43
std::shared_ptr< Time > TimePtr
Definition: Context.hh:46
std::shared_ptr< const Time > ConstTimePtr
Definition: Context.hh:47
ConstLoggerPtr log() const
Definition: Context.cc:130
ConstTimePtr time() const
Definition: Context.cc:118
std::shared_ptr< EnthalpyConverter > Ptr
Converts between specific enthalpy and temperature or liquid content.
std::shared_ptr< Logger > Ptr
Definition: Logger.hh:45
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
std::shared_ptr< Time > Ptr
Definition: Time.hh:62
std::shared_ptr< System > Ptr
Definition: Units.hh:47
#define PISM_ERROR_LOCATION
static const double L
Definition: exactTestL.cc:40
std::shared_ptr< Context > context_from_options(MPI_Comm com, const std::string &prefix, bool print)
Create a default context using options.
Definition: Context.cc:171
Logger::Ptr logger_from_options(MPI_Comm com)
Definition: Logger.cc:108
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 print_config(const Logger &log, int verbosity_threshhold, const Config &config)
Report configuration parameters to stdout.
static double S(unsigned n)
Definition: test_cube.c:58