URI: 
       tComponent.cc - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
  HTML git clone git://src.adamsgaard.dk/pism
   DIR Log
   DIR Files
   DIR Refs
   DIR LICENSE
       ---
       tComponent.cc (5679B)
       ---
            1 // Copyright (C) 2008-2019 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 <cassert>
           20 
           21 #include "Component.hh"
           22 #include "pism/util/io/File.hh"
           23 #include "IceGrid.hh"
           24 #include "pism_utilities.hh"
           25 #include "VariableMetadata.hh"
           26 #include "iceModelVec.hh"
           27 #include "pism_options.hh"
           28 #include "error_handling.hh"
           29 #include "ConfigInterface.hh"
           30 #include "MaxTimestep.hh"
           31 #include "pism/util/Time.hh"
           32 
           33 namespace pism {
           34 
           35 InputOptions::InputOptions(InitializationType t, const std::string &file, unsigned int index) {
           36   type     = t;
           37   filename = file;
           38   record   = index;
           39 }
           40 
           41 /*! Process command-line options -i and -bootstrap.
           42  *
           43  */
           44 InputOptions process_input_options(MPI_Comm com, Config::ConstPtr config) {
           45   InitializationType type   = INIT_OTHER;
           46   unsigned int       record = 0;
           47 
           48   std::string input_filename = config->get_string("input.file");
           49 
           50   bool bootstrap = config->get_flag("input.bootstrap") and (not input_filename.empty());
           51   bool restart   = (not config->get_flag("input.bootstrap")) and (not input_filename.empty());
           52 
           53   if (restart) {
           54     // re-start a run by initializing from an input file
           55     type = INIT_RESTART;
           56   } else if (bootstrap) {
           57     // initialize from an input file using bootstrapping heuristics
           58     type = INIT_BOOTSTRAP;
           59   } else {
           60     // other types of initialization (usually from formulas)
           61     type = INIT_OTHER;
           62   }
           63 
           64   // get the index of the last record in the input file
           65   if (not input_filename.empty()) {
           66     File input_file(com, input_filename, PISM_NETCDF3, PISM_READONLY);
           67 
           68     // Find the index of the last record in the input file.
           69     unsigned int last_record = input_file.nrecords();
           70     if (last_record > 0) {
           71       last_record -= 1;
           72     }
           73 
           74     record = last_record;
           75   } else {
           76     record = 0;
           77   }
           78 
           79   return InputOptions(type, input_filename, record);
           80 }
           81 
           82 Component::Component(IceGrid::ConstPtr g)
           83   : m_grid(g), m_config(g->ctx()->config()), m_sys(g->ctx()->unit_system()),
           84     m_log(g->ctx()->log()) {
           85   // empty
           86 }
           87 
           88 Component::~Component() {
           89   // empty
           90 }
           91 
           92 DiagnosticList Component::diagnostics() const {
           93   return this->diagnostics_impl();
           94 }
           95 
           96 TSDiagnosticList Component::ts_diagnostics() const {
           97   return this->ts_diagnostics_impl();
           98 }
           99 
          100 DiagnosticList Component::diagnostics_impl() const {
          101   return {};
          102 }
          103 
          104 TSDiagnosticList Component::ts_diagnostics_impl() const {
          105   return {};
          106 }
          107 
          108 IceGrid::ConstPtr Component::grid() const {
          109   return m_grid;
          110 }
          111 
          112 /*! @brief Define model state variables in an output file. */
          113 /*!
          114  * This is needed to allow defining all the variables in an output file before any data is written
          115  * (an optimization needed to get decent performance writing NetCDF-3).
          116  */
          117 void Component::define_model_state(const File &output) const {
          118   this->define_model_state_impl(output);
          119 }
          120 
          121 /*! @brief Write model state variables to an output file. */
          122 void Component::write_model_state(const File &output) const {
          123   // define variables, if needed (this is a no-op if they are already defined)
          124   this->define_model_state(output);
          125 
          126   this->write_model_state_impl(output);
          127 }
          128 
          129 /*! @brief The default (empty implementation). */
          130 void Component::define_model_state_impl(const File &output) const {
          131   (void) output;
          132 }
          133 
          134 /*! @brief The default (empty implementation). */
          135 void Component::write_model_state_impl(const File &output) const {
          136   (void) output;
          137 }
          138 
          139 /**
          140  * Regrid a variable by processing -regrid_file and -regrid_vars.
          141  *
          142  * @param[in] module_name Module name, used to annotate options when run with -help.
          143  *
          144  * @param[out] variable pointer to an IceModelVec; @c variable has to
          145  *             have metadata set for this to work.
          146  *
          147  * @param[in] flag Regridding flag. If set to
          148  *            REGRID_WITHOUT_REGRID_VARS, regrid this variable by
          149  *            default, if `-regrid_vars` was not set. Otherwise a
          150  *            variable is only regridded if both `-regrid_file` and
          151  *            `-regrid_vars` are set *and* the name of the variable is
          152  *            found in the set of names given with `-regrid_vars`.
          153  */
          154 void Component::regrid(const std::string &module_name, IceModelVec &variable,
          155                        RegriddingFlag flag) {
          156 
          157   auto regrid_file = m_config->get_string("input.regrid.file");
          158   auto regrid_vars = set_split(m_config->get_string("input.regrid.vars"), ',');
          159 
          160   if (regrid_file.empty()) {
          161     return;
          162   }
          163 
          164   SpatialVariableMetadata &m = variable.metadata();
          165 
          166   if (((not regrid_vars.empty()) and member(m.get_string("short_name"), regrid_vars)) or
          167       (regrid_vars.empty() and flag == REGRID_WITHOUT_REGRID_VARS)) {
          168 
          169     m_log->message(2,
          170                "  %s: regridding '%s' from file '%s' ...\n",
          171                module_name.c_str(),
          172                m.get_string("short_name").c_str(), regrid_file.c_str());
          173 
          174     variable.regrid(regrid_file, CRITICAL);
          175   }
          176 }
          177 
          178 MaxTimestep Component::max_timestep(double t) const {
          179   return this->max_timestep_impl(t);
          180 }
          181 
          182 MaxTimestep Component::max_timestep_impl(double t) const {
          183   (void) t;
          184   return MaxTimestep();
          185 }
          186 
          187 
          188 } // end of namespace pism