PISM, A Parallel Ice Sheet Model  stable v2.1.1 committed by Constantine Khrulev on 2024-12-04 13:36:58 -0900
pismv.cc
Go to the documentation of this file.
1 // Copyright (C) 2004-2017, 2019, 2020, 2021, 2022, 2023 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 static char help[] =
20 "Ice sheet driver for PISM (SIA and SSA) verification. Uses exact solutions\n"
21 " to various coupled subsystems. Computes difference between exact solution\n"
22 " and numerical solution.\n"
23 " Currently implements tests A, B, C, D, E, F, G, H, K, L.\n\n";
24 
25 #include <string>
26 
27 #include "pism/util/Grid.hh"
28 #include "pism/util/Config.hh"
29 #include "pism/util/error_handling.hh"
30 #include "pism/util/petscwrappers/PetscInitializer.hh"
31 #include "pism/util/pism_options.hh"
32 #include "pism/verification/iceCompModel.hh"
33 #include "pism/util/Context.hh"
34 #include "pism/util/Logger.hh"
35 #include "pism/util/Time.hh"
36 #include "pism/util/EnthalpyConverter.hh"
37 
38 using namespace pism;
39 
40 //! Allocate the PISMV (verification) context. Uses ColdEnthalpyConverter.
41 std::shared_ptr<Context> pismv_context(MPI_Comm com, const std::string &prefix) {
42  // unit system
44 
45  // logger
46  auto logger = logger_from_options(com);
47 
48  // configuration parameters
49  auto config = config_from_options(com, *logger, sys);
50 
51  config->set_string("grid.periodicity", "none");
52  config->set_string("grid.registration", "corner");
53 
54  set_config_from_options(sys, *config);
55  config->resolve_filenames();
56 
57  print_config(*logger, 3, *config);
58 
59  Time::Ptr time = std::make_shared<Time>(com, config, *logger, sys);
60 
62 
63  return std::make_shared<Context>(com, sys, config, EC, time, logger, prefix);
64 }
65 
67  // This sets the defaults for each test; command-line options can override this.
68 
69  grid::Parameters P(*config);
70 
71  // use the cell corner grid registration
73  // use the non-periodic grid:
75  // equal spacing is the default for all the tests except K
76  P.Lx = config->get_number("grid.Lx");
77  P.Ly = config->get_number("grid.Ly");
78 
79  P.Mx = config->get_number("grid.Mx");
80  P.My = config->get_number("grid.My");
81 
82  auto spacing = pism::grid::EQUAL;
83  double Lz = config->get_number("grid.Lz");
84  unsigned int Mz = config->get_number("grid.Mz");
85 
86  switch (testname) {
87  case 'A':
88  case 'B':
89  case 'H':
90  // use 2400km by 2400km by 4000m rectangular domain
91  P.Lx = 1200e3;
92  P.Ly = P.Lx;
93  Lz = 4000;
94  break;
95  case 'C':
96  case 'D':
97  // use 2000km by 2000km by 4000m rectangular domain
98  P.Lx = 1000e3;
99  P.Ly = P.Lx;
100  Lz = 4000;
101  break;
102  case 'F':
103  case 'G':
104  case 'L':
105  // use 1800km by 1800km by 4000m rectangular domain
106  P.Lx = 900e3;
107  P.Ly = P.Lx;
108  Lz = 4000;
109  break;
110  case 'K':
111  case 'O':
112  // use 2000km by 2000km by 4000m rectangular domain, but make truely periodic
113  config->set_number("grid.Mbz", 2);
114  config->set_number("grid.Lbz", 1000);
115  P.Lx = 1000e3;
116  P.Ly = P.Lx;
117  Lz = 4000;
119  spacing = pism::grid::QUADRATIC;
120  break;
121  case 'V':
122  P.My = 3; // it's a flow-line setup
123  P.Lx = 500e3; // 500 km long
125  break;
126  default:
127  throw RuntimeError(PISM_ERROR_LOCATION, "desired test not implemented\n");
128  }
129 
130  P.z = grid::compute_vertical_levels(Lz, Mz, spacing, config->get_number("grid.lambda"));
131  return P;
132 }
133 
134 std::shared_ptr<Grid> pismv_grid(std::shared_ptr<Context> ctx, char testname) {
135  auto config = ctx->config();
136 
137  auto input_file = config->get_string("input.file");
138 
139  if (config->get_flag("input.bootstrap")) {
140  throw RuntimeError(PISM_ERROR_LOCATION, "pismv does not support bootstrapping");
141  }
142 
143  if (not input_file.empty()) {
144  auto r = grid::string_to_registration(ctx->config()->get_string("grid.registration"));
145 
146  // get grid from a PISM input file
147  return Grid::FromFile(ctx, input_file, { "enthalpy", "temp" }, r);
148  }
149 
150  // use defaults set by pismv_grid_defaults()
151  auto P = pismv_grid_defaults(ctx->config(), testname);
152  P.horizontal_size_from_options();
153  P.horizontal_extent_from_options(ctx->unit_system());
154  P.vertical_grid_from_options(ctx->config());
155  P.ownership_ranges_from_options(ctx->size());
156 
157  return std::make_shared<Grid>(ctx, P);
158 }
159 
160 int main(int argc, char *argv[]) {
161  MPI_Comm com = MPI_COMM_WORLD;
162 
163  petsc::Initializer petsc(argc, argv, help);
164  com = MPI_COMM_WORLD;
165 
166  /* This explicit scoping forces destructors to be called before PetscFinalize() */
167  try {
168  std::shared_ptr<Context> ctx = pismv_context(com, "pismv");
169  Logger::Ptr log = ctx->log();
170 
171  std::string usage =
172  " pismv -test x [-no_report] [OTHER PISM & PETSc OPTIONS]\n"
173  "where:\n"
174  " -test x SIA-type verification test (x = A|B|C|D|F|G|H|K|L)\n"
175  " -no_report do not give error report at end of run\n"
176  "(see User's Manual for tests I and J).\n";
177 
178  std::vector<std::string> required(1, "-test");
179 
180  bool done = show_usage_check_req_opts(*log, "PISMV (verification mode)", required, usage);
181  if (done) {
182  return 0;
183  }
184 
185  Config::Ptr config = ctx->config();
186 
187  // determine test (and whether to report error)
188  std::string testname = options::Keyword("-test", "Specifies PISM verification test",
189  "A,B,C,D,F,G,H,K,L,V", "A");
190 
191  auto g = pismv_grid(ctx, testname[0]);
192 
193  IceCompModel m(g, ctx, testname[0]);
194 
195  m.init();
196 
197  m.run();
198  log->message(2, "done with run\n");
199 
200  if (not options::Bool("-no_report", "do not print the error report")) {
201  m.reportErrors();
202  }
203 
204  // provide a default output file name if no -o option is given.
205  m.save_results();
206 
207  print_unused_parameters(*log, 3, *config);
208  }
209  catch (...) {
210  handle_fatal_errors(com);
211  return 1;
212  }
213 
214  return 0;
215 }
216 
An EnthalpyConverter for use in verification tests.
std::shared_ptr< Config > Ptr
std::shared_ptr< EnthalpyConverter > Ptr
static std::shared_ptr< Grid > FromFile(std::shared_ptr< const Context > ctx, const std::string &filename, const std::vector< std::string > &var_names, grid::Registration r)
Create a grid using one of variables in var_names in file.
Definition: Grid.cc:255
IceModelTerminationReason run()
Definition: IceModel.cc:767
void init()
Manage the initialization of the IceModel object.
Definition: IceModel.cc:854
virtual void save_results()
Save model state in NetCDF format.
Definition: output.cc:98
std::shared_ptr< Logger > Ptr
Definition: Logger.hh:45
std::shared_ptr< Time > Ptr
Definition: Time.hh:62
Periodicity periodicity
Grid periodicity.
Definition: Grid.hh:156
std::vector< double > z
Vertical levels.
Definition: Grid.hh:158
Registration registration
Grid registration.
Definition: Grid.hh:154
unsigned int Mx
Number of grid points in the X direction.
Definition: Grid.hh:150
double Ly
Domain half-width in the Y direction.
Definition: Grid.hh:144
double Lx
Domain half-width in the X direction.
Definition: Grid.hh:142
unsigned int My
Number of grid points in the Y direction.
Definition: Grid.hh:152
Grid parameters; used to collect defaults before an Grid is allocated.
Definition: Grid.hh:117
std::shared_ptr< System > Ptr
Definition: Units.hh:47
#define PISM_ERROR_LOCATION
@ EQUAL
Definition: Grid.hh:50
@ QUADRATIC
Definition: Grid.hh:50
@ XY_PERIODIC
Definition: Grid.hh:51
@ Y_PERIODIC
Definition: Grid.hh:51
@ NOT_PERIODIC
Definition: Grid.hh:51
Registration string_to_registration(const std::string &keyword)
Definition: Grid.cc:986
@ CELL_CORNER
Definition: Grid.hh:53
std::vector< double > compute_vertical_levels(double new_Lz, unsigned int new_Mz, grid::VerticalSpacing spacing, double lambda)
Set the vertical levels in the ice according to values in Mz (number of levels), Lz (domain height),...
Definition: Grid.cc:879
bool Bool(const std::string &option, const std::string &description)
Definition: options.cc:240
static const double g
Definition: exactTestP.cc:36
Logger::Ptr logger_from_options(MPI_Comm com)
Definition: Logger.cc:107
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 handle_fatal_errors(MPI_Comm com)
void set_config_from_options(units::System::Ptr unit_system, Config &config)
Set configuration parameters using command-line options.
bool show_usage_check_req_opts(const Logger &log, const std::string &execname, const std::vector< std::string > &required_options, const std::string &usage)
In a single call a driver program can provide a usage string to the user and check if required option...
Definition: pism_options.cc:51
void print_unused_parameters(const Logger &log, int verbosity_threshhold, const Config &config)
Report unused configuration parameters to stdout.
void print_config(const Logger &log, int verbosity_threshhold, const Config &config)
Report configuration parameters to stdout.
std::shared_ptr< Grid > pismv_grid(std::shared_ptr< Context > ctx, char testname)
Definition: pismv.cc:134
int main(int argc, char *argv[])
Definition: pismv.cc:160
static char help[]
Definition: pismv.cc:19
std::shared_ptr< Context > pismv_context(MPI_Comm com, const std::string &prefix)
Allocate the PISMV (verification) context. Uses ColdEnthalpyConverter.
Definition: pismv.cc:41
grid::Parameters pismv_grid_defaults(Config::Ptr config, char testname)
Definition: pismv.cc:66