PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
utilities.cc
Go to the documentation of this file.
1// Copyright (C) 2004-2019, 2021, 2023, 2024, 2025, 2026 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 <memory>
21#include <petscsys.h>
22
23#include "pism/icemodel/IceModel.hh"
24
25#include "pism/util/Grid.hh"
26#include "pism/util/Time.hh"
27#include "pism/util/pism_utilities.hh"
28#include "pism/util/pism_signal.h"
29#include "pism/util/io/SynchronousOutputWriter.hh"
30#include "pism/util/io/io_helpers.hh"
31
32namespace pism {
33
34//! Catch signals -USR1, -USR2 and -TERM.
35/*!
36Signal `SIGTERM` makes PISM end, saving state under original `-o` name
37(or default name). We also add an indication to the history attribute
38of the output NetCDF file.
39
40Signal `SIGUSR1` makes PISM save state under a filename based on the
41the name of the executable (e.g. `pism`) and the current
42model year. In addition the time series (`-scalar_file`, etc.) is flushed out
43There is no indication of these actions in the history attribute of the output (`-o`)
44NetCDF file because there is no effect on it, but there is an indication at `stdout`.
45
46Signal `SIGUSR2` makes PISM flush time-series, without saving model state.
47 */
49
50 if (pism_signal == SIGTERM) {
51 m_log->message(1,
52 "\ncaught signal SIGTERM: EXITING EARLY and saving with original filename.\n");
53
54 append_history(pism::printf("EARLY EXIT caused by signal SIGTERM. Completed timestep at time=%s.",
55 m_time->date(m_time->current()).c_str()));
56 // Tell the caller that the user requested an early termination of
57 // the run.
58 return 1;
59 }
60
61 if (pism_signal == SIGUSR1) {
62 auto date_without_spaces = replace_character(m_time->date(m_time->current()), ' ', '_');
63 auto file_name = pism::printf("pism-%s.nc", date_without_spaces.c_str());
64 m_log->message(1,
65 "\ncaught signal SIGUSR1: Writing intermediate file `%s' and flushing time series.\n\n",
66 file_name.c_str());
67 pism_signal = 0;
68
69 std::shared_ptr<OutputWriter> writer =
70 std::make_shared<SynchronousOutputWriter>(m_grid->com, *m_config);
71 writer->initialize({}, true);
72
73 OutputFile file(writer, file_name);
74
75 {
76 define_time(file);
78 }
79
80 {
81 io::write_config(*m_config, "pism_config", file);
82 file.append_time(m_time->current());
83 write_state(file);
86 write_run_stats(file);
87 }
88
89 // flush all the time-series buffers:
91 }
92
93 if (pism_signal == SIGUSR2) {
94 m_log->message(1,
95 "\ncaught signal SIGUSR2: Flushing time series.\n\n");
96 pism_signal = 0;
97
98 // flush all the time-series buffers:
100 }
101
102 return 0;
103}
104
105//! Get time and user/host name and add it to the given string.
106void IceModel::append_history(const std::string &str) {
108}
109
110//! Return the grid used by this model.
111std::shared_ptr<Grid> IceModel::grid() const {
112 return m_grid;
113}
114
115//! Return the context this model is running in.
116std::shared_ptr<Context> IceModel::ctx() const {
117 return m_ctx;
118}
119
120} // end of namespace pism
virtual int process_signals()
Catch signals -USR1, -USR2 and -TERM.
Definition utilities.cc:48
void define_variables(const OutputFile &file, const std::set< VariableMetadata > &variables) const
Definition output.cc:206
std::shared_ptr< Config > m_config
Configuration flags and parameters.
Definition IceModel.hh:278
void define_time(const OutputFile &file, bool with_bounds=false) const
Definition output.cc:73
void write_run_stats(const OutputFile &file) const
Definition output.cc:182
std::shared_ptr< Context > m_ctx
Execution context.
Definition IceModel.hh:280
std::shared_ptr< Logger > m_log
Logger.
Definition IceModel.hh:284
std::shared_ptr< Grid > grid() const
Return the grid used by this model.
Definition utilities.cc:111
std::set< VariableMetadata > m_output_file_contents
Set of variables that will be written to the output file.
Definition IceModel.hh:239
std::shared_ptr< Time > m_time
Time manager.
Definition IceModel.hh:286
void write_state_diagnostics(const OutputFile &file, const std::set< std::string > &variable_names) const
void write_diagnostics(const OutputFile &file, const std::set< std::string > &variable_names) const
Writes variables listed in variable_names to file.
void scalar_diagnostics_flush_buffers()
Flush scalar time-series.
std::shared_ptr< Context > ctx() const
Return the context this model is running in.
Definition utilities.cc:116
std::string m_output_history
Definition IceModel.hh:294
virtual void write_state(const OutputFile &file) const
Definition output.cc:240
virtual void append_history(const std::string &string)
Get time and user/host name and add it to the given string.
Definition utilities.cc:106
std::set< std::string > m_output_vars
Definition IceModel.hh:236
const std::shared_ptr< Grid > m_grid
Computational grid.
Definition IceModel.hh:276
void append_time(double time_seconds) const
Definition OutputFile.cc:41
void write_config(const Config &config, const std::string &variable_name, const OutputFile &file)
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