PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
YearlyCycle.cc
Go to the documentation of this file.
1 // Copyright (C) 2008-2020, 2023 Ed Bueler, Constantine Khroulev, Ricarda Winkelmann,
2 // Gudfinna Adalgeirsdottir and Andy Aschwanden
3 //
4 // This file is part of PISM.
5 //
6 // PISM is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3 of the License, or (at your option) any later
9 // version.
10 //
11 // PISM is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 // details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with PISM; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 // Implementation of the atmosphere model using constant-in-time precipitation
21 // and a cosine yearly cycle for near-surface air temperatures.
22 
23 #include <gsl/gsl_math.h> // M_PI
24 
25 #include "pism/coupler/atmosphere/YearlyCycle.hh"
26 #include "pism/util/Time.hh"
27 #include "pism/util/Grid.hh"
28 #include "pism/util/ConfigInterface.hh"
29 #include "pism/util/io/io_helpers.hh"
30 #include "pism/util/pism_utilities.hh"
31 #include "pism/util/Context.hh"
32 
33 namespace pism {
34 namespace atmosphere {
35 
36 YearlyCycle::YearlyCycle(std::shared_ptr<const Grid> g)
37  : AtmosphereModel(g),
38  m_air_temp_mean_annual(m_grid, "air_temp_mean_annual"),
39  m_air_temp_mean_summer(m_grid, "air_temp_mean_summer"),
40  m_precipitation(m_grid, "precipitation") {
41 
42  m_snow_temp_summer_day = m_config->get_number("atmosphere.fausto_air_temp.summer_peak_day");
43 
45  .long_name(
46  "mean annual near-surface air temperature (without sub-year time-dependence or forcing)")
47  .units("K");
49 
51  .long_name(
52  "mean summer (NH: July/ SH: January) near-surface air temperature (without sub-year time-dependence or forcing)")
53  .units("Kelvin");
55 
57  .long_name("precipitation rate")
58  .units("kg m-2 second-1")
59  .output_units("kg m-2 year-1")
60  .standard_name("precipitation_flux")
61  .set_time_independent(true);
62 }
63 
64 //! Reads in the precipitation data from the input file.
65 void YearlyCycle::init_impl(const Geometry &geometry) {
66  (void) geometry;
67 
69  init_internal(opts.filename, opts.type == INIT_BOOTSTRAP, opts.record);
70 }
71 
72 //! Read precipitation data from a given file.
73 void YearlyCycle::init_internal(const std::string &input_filename, bool do_regrid,
74  unsigned int start) {
75  // read precipitation rate from file
76  m_log->message(2,
77  " reading mean annual ice-equivalent precipitation rate 'precipitation'\n"
78  " from %s ... \n",
79  input_filename.c_str());
80  if (do_regrid == true) {
81  m_precipitation.regrid(input_filename, io::Default::Nil()); // fails if not found!
82  } else {
83  m_precipitation.read(input_filename, start); // fails if not found!
84  }
85 }
86 
87 void YearlyCycle::define_model_state_impl(const File &output) const {
89 }
90 
91 void YearlyCycle::write_model_state_impl(const File &output) const {
92  m_precipitation.write(output);
93 }
94 
95 //! Copies the stored precipitation field into result.
97  return m_precipitation;
98 }
99 
100 //! Copies the stored mean annual near-surface air temperature field into result.
102  return m_air_temp_mean_annual;
103 }
104 
105 //! Copies the stored mean summer near-surface air temperature field into result.
107  return m_air_temp_mean_summer;
108 }
109 
110 void YearlyCycle::init_timeseries_impl(const std::vector<double> &ts) const {
111  // constants related to the standard yearly cycle
112  const double
114 
115  size_t N = ts.size();
116 
117  m_ts_times.resize(N);
118  m_cosine_cycle.resize(N);
119  for (unsigned int k = 0; k < m_ts_times.size(); k++) {
120  double tk = time().year_fraction(ts[k]) - summerday_fraction;
121 
122  m_ts_times[k] = ts[k];
123  m_cosine_cycle[k] = cos(2.0 * M_PI * tk);
124  }
125 }
126 
127 void YearlyCycle::precip_time_series_impl(int i, int j, std::vector<double> &result) const {
128  result.resize(m_ts_times.size());
129  for (unsigned int k = 0; k < m_ts_times.size(); k++) {
130  result[k] = m_precipitation(i,j);
131  }
132 }
133 
134 void YearlyCycle::temp_time_series_impl(int i, int j, std::vector<double> &result) const {
135  result.resize(m_ts_times.size());
136  for (unsigned int k = 0; k < m_ts_times.size(); ++k) {
138  }
139 }
140 
145 }
146 
151 }
152 
153 namespace diagnostics {
154 
155 /*! @brief Mean summer near-surface air temperature. */
156 class MeanSummerTemperature : public Diag<YearlyCycle>
157 {
158 public:
160  m_vars = { { m_sys, "air_temp_mean_summer" } };
161  m_vars[0]
162  .long_name("mean summer near-surface air temperature used in the cosine yearly cycle")
163  .units("Kelvin");
164  }
165 
166 private:
167  std::shared_ptr<array::Array> compute_impl() const {
168  auto result = allocate<array::Scalar>("air_temp_mean_summer");
169 
170  result->copy_from(model->mean_summer_temp());
171 
172  return result;
173  }
174 };
175 } // end of namespace diagnostics
176 
179 
180  result["air_temp_mean_summer"] = Diagnostic::Ptr(new diagnostics::MeanSummerTemperature(this));
181 
182  return result;
183 }
184 
185 } // end of namespace atmosphere
186 } // end of namespace pism
const Time & time() const
Definition: Component.cc:109
const Config::ConstPtr m_config
configuration database used by this component
Definition: Component.hh:158
const Logger::ConstPtr m_log
logger (for easy access)
Definition: Component.hh:162
const std::shared_ptr< const Grid > m_grid
grid used by this component
Definition: Component.hh:156
const YearlyCycle * model
Definition: Diagnostic.hh:172
A template derived from Diagnostic, adding a "Model".
Definition: Diagnostic.hh:166
const units::System::Ptr m_sys
the unit system
Definition: Diagnostic.hh:116
std::vector< SpatialVariableMetadata > m_vars
metadata corresponding to NetCDF variables
Definition: Diagnostic.hh:120
std::shared_ptr< Diagnostic > Ptr
Definition: Diagnostic.hh:65
High-level PISM I/O class.
Definition: File.hh:56
double year_fraction(double T) const
Returns the fraction of a year that passed since the last beginning of a year. Only useful in codes w...
Definition: Time.cc:820
double day_of_the_year_to_year_fraction(unsigned int day) const
Convert the day number to the year fraction.
Definition: Time.cc:505
VariableMetadata & standard_name(const std::string &input)
VariableMetadata & long_name(const std::string &input)
VariableMetadata & output_units(const std::string &input)
VariableMetadata & units(const std::string &input)
VariableMetadata & set_time_independent(bool flag)
void read(const std::string &filename, unsigned int time)
Definition: Array.cc:809
virtual void end_access() const
Checks if an Array is allocated and calls DAVecRestoreArray.
Definition: Array.cc:667
void define(const File &file, io::Type default_type) const
Define variables corresponding to an Array in a file opened using file.
Definition: Array.cc:540
virtual void begin_access() const
Checks if an Array is allocated and calls DAVecGetArray.
Definition: Array.cc:646
void write(const std::string &filename) const
Definition: Array.cc:800
void regrid(const std::string &filename, io::Default default_value)
Definition: Array.cc:814
SpatialVariableMetadata & metadata(unsigned int N=0)
Returns a reference to the SpatialVariableMetadata object containing metadata for the compoment N.
Definition: Array.cc:553
virtual DiagnosticList diagnostics_impl() const
std::vector< double > m_ts_times
A purely virtual class defining the interface of a PISM Atmosphere Model.
array::Scalar m_air_temp_mean_summer
Definition: YearlyCycle.hh:63
virtual void define_model_state_impl(const File &output) const
The default (empty implementation).
Definition: YearlyCycle.cc:87
void init_internal(const std::string &input_filename, bool regrid, unsigned int start)
Read precipitation data from a given file.
Definition: YearlyCycle.cc:73
YearlyCycle(std::shared_ptr< const Grid > g)
Definition: YearlyCycle.cc:36
virtual const array::Scalar & mean_summer_temp() const
Copies the stored mean summer near-surface air temperature field into result.
Definition: YearlyCycle.cc:106
virtual void init_impl(const Geometry &geometry)
Reads in the precipitation data from the input file.
Definition: YearlyCycle.cc:65
std::vector< double > m_cosine_cycle
Definition: YearlyCycle.hh:64
virtual void init_timeseries_impl(const std::vector< double > &ts) const
Definition: YearlyCycle.cc:110
virtual void end_pointwise_access_impl() const
Definition: YearlyCycle.cc:147
virtual void write_model_state_impl(const File &output) const
The default (empty implementation).
Definition: YearlyCycle.cc:91
virtual void begin_pointwise_access_impl() const
Definition: YearlyCycle.cc:141
virtual DiagnosticList diagnostics_impl() const
Definition: YearlyCycle.cc:177
array::Scalar m_air_temp_mean_annual
Definition: YearlyCycle.hh:63
virtual void temp_time_series_impl(int i, int j, std::vector< double > &result) const
Definition: YearlyCycle.cc:134
virtual const array::Scalar & air_temperature_impl() const
Copies the stored mean annual near-surface air temperature field into result.
Definition: YearlyCycle.cc:101
virtual void precip_time_series_impl(int i, int j, std::vector< double > &result) const
Definition: YearlyCycle.cc:127
virtual const array::Scalar & precipitation_impl() const
Copies the stored precipitation field into result.
Definition: YearlyCycle.cc:96
std::shared_ptr< array::Array > compute_impl() const
Definition: YearlyCycle.cc:167
Mean summer near-surface air temperature.
Definition: YearlyCycle.cc:157
static Default Nil()
Definition: IO_Flags.hh:97
@ PISM_DOUBLE
Definition: IO_Flags.hh:52
InputOptions process_input_options(MPI_Comm com, Config::ConstPtr config)
Definition: Component.cc:43
static const double g
Definition: exactTestP.cc:36
@ INIT_BOOTSTRAP
Definition: Component.hh:56
std::map< std::string, Diagnostic::Ptr > DiagnosticList
Definition: Diagnostic.hh:125
static const double k
Definition: exactTestP.cc:42
InitializationType type
initialization type
Definition: Component.hh:61
std::string filename
name of the input file (if applicable)
Definition: Component.hh:63
unsigned int record
index of the record to re-start from
Definition: Component.hh:65