Loading [MathJax]/extensions/tex2jax.js
PISM, A Parallel Ice Sheet Model 2.2.1-cd005eec8 committed by Constantine Khrulev on 2025-03-07
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utilities.cc
Go to the documentation of this file.
1// Copyright (C) 2004-2019, 2021, 2023, 2024 Jed Brown, Ed Bueler and Constantine Khroulev
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#include <cstring>
20#include <petscsys.h>
21
22#include "pism/icemodel/IceModel.hh"
23
24#include "pism/util/Grid.hh"
25#include "pism/util/ConfigInterface.hh"
26#include "pism/util/Time.hh"
27#include "pism/util/io/File.hh"
28#include "pism/util/pism_utilities.hh"
29#include "pism/util/pism_signal.h"
30
31namespace pism {
32
33//! Catch signals -USR1, -USR2 and -TERM.
34/*!
35Signal `SIGTERM` makes PISM end, saving state under original `-o` name
36(or default name). We also add an indication to the history attribute
37of the output NetCDF file.
38
39Signal `SIGUSR1` makes PISM save state under a filename based on the
40the name of the executable (e.g. `pism`) and the current
41model year. In addition the time series (`-ts_file`, etc.) is flushed out
42There is no indication of these actions in the history attribute of the output (`-o`)
43NetCDF file because there is no effect on it, but there is an indication at `stdout`.
44
45Signal `SIGUSR2` makes PISM flush time-series, without saving model state.
46 */
48
49 if (pism_signal == SIGTERM) {
50 m_log->message(1,
51 "\ncaught signal SIGTERM: EXITING EARLY and saving with original filename.\n");
52
53 prepend_history(pism::printf("EARLY EXIT caused by signal SIGTERM. Completed timestep at time=%s.",
54 m_time->date(m_time->current()).c_str()));
55 // Tell the caller that the user requested an early termination of
56 // the run.
57 return 1;
58 }
59
60 if (pism_signal == SIGUSR1) {
61 auto date_without_spaces = replace_character(m_time->date(m_time->current()), ' ', '_');
62 auto file_name = pism::printf("pism-%s.nc", date_without_spaces.c_str());
63 m_log->message(1,
64 "\ncaught signal SIGUSR1: Writing intermediate file `%s' and flushing time series.\n\n",
65 file_name.c_str());
66 pism_signal = 0;
67
68 File file(m_grid->com,
69 file_name,
70 string_to_backend(m_config->get_string("output.format")),
73
74 // flush all the time-series buffers:
76 }
77
78 if (pism_signal == SIGUSR2) {
79 m_log->message(1,
80 "\ncaught signal SIGUSR2: Flushing time series.\n\n");
81 pism_signal = 0;
82
83 // flush all the time-series buffers:
85 }
86
87 return 0;
88}
89
90
92
93 VariableMetadata result("run_stats", m_sys);
94
95 result["source"] = std::string("PISM ") + pism::revision;
96 result["long_name"] = "Run statistics";
97
98 // timing stats
99 double
101 proc_hours = m_grid->size() * wall_clock_hours,
102 model_years = units::convert(m_sys, m_time->current() - m_time->start(),
103 "seconds", "years");
104
105 result["wall_clock_hours"] = { wall_clock_hours };
106 result["processor_hours"] = { proc_hours };
107 result["model_years_per_processor_hour"] = { model_years / proc_hours };
108 result["number_of_time_steps"] = { (double)m_step_counter };
109
110 return result;
111}
112
113//! Get time and user/host name and add it to the given string.
114void IceModel::prepend_history(const std::string &str) {
116 username_prefix(m_grid->com) + (str + "\n") +
118}
119
120//! Return the grid used by this model.
121std::shared_ptr<Grid> IceModel::grid() const {
122 return m_grid;
123}
124
125//! Return the context this model is running in.
126std::shared_ptr<Context> IceModel::ctx() const {
127 return m_ctx;
128}
129
130} // end of namespace pism
High-level PISM I/O class.
Definition File.hh:55
virtual int process_signals()
Catch signals -USR1, -USR2 and -TERM.
Definition utilities.cc:47
double m_start_time
Definition IceModel.hh:474
VariableMetadata run_stats() const
Definition utilities.cc:91
unsigned int m_step_counter
Definition IceModel.hh:323
const Time::Ptr m_time
Time manager.
Definition IceModel.hh:246
std::shared_ptr< Context > m_ctx
Execution context.
Definition IceModel.hh:240
std::shared_ptr< Grid > grid() const
Return the grid used by this model.
Definition utilities.cc:121
const Logger::Ptr m_log
Logger.
Definition IceModel.hh:244
VariableMetadata m_output_global_attributes
stores global attributes saved in a PISM output file
Definition IceModel.hh:249
void flush_timeseries()
Flush scalar time-series.
Definition output_ts.cc:124
virtual void prepend_history(const std::string &string)
Get time and user/host name and add it to the given string.
Definition utilities.cc:114
Config::Ptr m_config
Configuration flags and parameters.
Definition IceModel.hh:238
virtual void save_variables(const File &file, OutputKind kind, const std::set< std::string > &variables, double time, io::Type default_diagnostics_type=io::PISM_FLOAT) const
Definition output.cc:150
const units::System::Ptr m_sys
Unit system.
Definition IceModel.hh:242
std::shared_ptr< Context > ctx() const
Return the context this model is running in.
Definition utilities.cc:126
std::set< std::string > m_output_vars
Definition IceModel.hh:422
const std::shared_ptr< Grid > m_grid
Computational grid.
Definition IceModel.hh:236
std::string get_string(const std::string &name) const
Get a string attribute.
VariableMetadata & set_string(const std::string &name, const std::string &value)
Set a string attribute.
@ PISM_READWRITE_MOVE
create a file for writing, move foo.nc to foo.nc~ if present
Definition IO_Flags.hh:74
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:70
io::Backend string_to_backend(const std::string &backend)
Definition File.cc:56
double wall_clock_hours(MPI_Comm com, double start_time)
Return time since the beginning of the run, in hours.
std::string printf(const char *format,...)
std::string username_prefix(MPI_Comm com)
Creates a string with the user name, hostname and the time-stamp (for history strings).
std::string replace_character(const std::string &input, char from, char to)
volatile sig_atomic_t pism_signal
Definition pism_signal.c:23