22 "Tests BedThermalUnit using Test K, without IceModel.\n\n";
24 #include "pism/util/pism_options.hh"
25 #include "pism/util/Grid.hh"
26 #include "pism/util/io/File.hh"
27 #include "pism/verification/BTU_Verification.hh"
28 #include "pism/energy/BTU_Minimal.hh"
29 #include "pism/util/Time.hh"
30 #include "pism/util/ConfigInterface.hh"
32 #include "pism/verification/tests/exactTestK.h"
34 #include "pism/util/petscwrappers/PetscInitializer.hh"
35 #include "pism/util/error_handling.hh"
36 #include "pism/util/io/io_helpers.hh"
37 #include "pism/util/Context.hh"
38 #include "pism/util/EnthalpyConverter.hh"
39 #include "pism/util/MaxTimestep.hh"
40 #include "pism/util/Logger.hh"
43 std::shared_ptr<pism::Context>
btutest_context(MPI_Comm com,
const std::string &prefix) {
56 config->set_number(
"grid.Mbz", 11);
57 config->set_number(
"grid.Lbz", 1000);
60 config->set_string(
"time.start",
"0s");
61 config->set_number(
"time.run_length", 1.0);
64 config->resolve_filenames();
68 Time::Ptr time = std::make_shared<Time>(com, config, *logger, sys);
72 return std::shared_ptr<Context>(
new Context(com, sys, config, EC, time, logger, prefix));
75 int main(
int argc,
char *argv[]) {
79 MPI_Comm com = MPI_COMM_WORLD;
89 " btutest -Mbz NN -Lbz 1000.0 [-o OUT.nc -ys A -ye B -dt C -Mz D -Lz E]\n"
90 "where these are required because they are used in BedThermalUnit:\n"
91 " -Mbz number of bedrock thermal layer levels to use\n"
92 " -Lbz 1000.0 depth of bedrock thermal layer (required; Lbz=1000.0 m in Test K)\n"
93 "and these are allowed:\n"
94 " -o output file name; NetCDF format\n"
95 " -ys start year in using Test K\n"
96 " -ye end year in using Test K\n"
97 " -dt time step B (= positive float) in years\n";
106 " initializing Grid from options ...\n");
120 std::shared_ptr<Grid> grid(
new Grid(ctx, P));
122 auto outname = config->get_string(
"output.file");
125 "-dt",
"Time-step, in years",
"years", 1.0);
131 array::Scalar heat_flux_at_ice_base(grid,
"upward_heat_flux_at_ice_base");
133 .
long_name(
"upward geothermal flux at bedrock thermal layer base")
142 if (bedrock_grid.
Mbz > 1) {
151 double dt_seconds =
units::convert(ctx->unit_system(), dt_years,
"years",
"seconds");
154 int N = (int)ceil((ctx->time()->end() - ctx->time()->start()) / dt_seconds);
155 dt_seconds = (ctx->time()->end() - ctx->time()->start()) / (
double)N;
157 " user set timestep of %.4f years ...\n"
158 " reset to %.4f years to get integer number of steps ... \n",
162 " BedThermalUnit reports max timestep of %.4f years ...\n",
166 log->message(2,
" running ...\n");
167 for (
int n = 0;
n < N;
n++) {
169 const double time = ctx->time()->start() + dt_seconds * (double)
n;
174 for (
auto p = grid->points(); p; p.next()) {
175 const int i = p.i(), j = p.j();
177 bedtoptemp(i,j) =
exactK(time, 0.0, 0).
T;
183 btu->update(bedtoptemp, time, dt_seconds);
187 log->message(2,
"\n done ...\n");
190 heat_flux_at_ice_base.
copy_from(btu->flux_through_top_surface());
192 auto time = ctx->time();
195 const double FF =
exactK(time->end(), 0.0, 0).
F;
197 " exact Test K reports upward heat flux at z=0, at end time %s, as G_0 = %.7f W m-2;\n",
198 time->date(time->end()).c_str(), FF);
201 heat_flux_at_ice_base.
shift(-FF);
202 double max_error = heat_flux_at_ice_base.
norm(NORM_INFINITY)[0];
203 double avg_error = heat_flux_at_ice_base.
norm(NORM_1)[0];
204 heat_flux_at_ice_base.
shift(+FF);
205 avg_error /= (grid->Mx() * grid->My());
207 "case dt = %.5f:\n", dt_years.
value());
209 "NUMERICAL ERRORS in upward heat flux at z=0 relative to exact solution:\n");
211 "bheatflx0 : max prcntmax av\n");
213 " %11.7f %11.7f %11.7f\n",
214 max_error, 100.0*max_error/FF, avg_error);
215 log->message(1,
"NUM ERRORS DONE\n");
221 ctx->pio_iosys_id());
226 btu->write_model_state(file);
228 bedtoptemp.
write(file);
229 heat_flux_at_ice_base.
write(file);
231 log->message(2,
"done.\n");
int main(int argc, char *argv[])
std::shared_ptr< pism::Context > btutest_context(MPI_Comm com, const std::string &prefix)
Allocate the PISMV (verification) context. Uses ColdEnthalpyConverter.
An EnthalpyConverter for use in verification tests.
std::shared_ptr< Config > Ptr
std::shared_ptr< EnthalpyConverter > Ptr
High-level PISM I/O class.
Describes the PISM grid and the distribution of data across processors.
std::shared_ptr< Logger > Ptr
double value() const
Get the value of the maximum time step.
Combines the max. time step with the flag indicating if a restriction is active. Makes is possible to...
std::shared_ptr< Time > Ptr
Makes sure that we call begin_access() and end_access() for all accessed array::Arrays.
void copy_from(const Array2D< T > &source)
void shift(double alpha)
Result: v[j] <- v[j] + alpha for all j. Calls VecShift.
void write(const std::string &filename) const
std::vector< double > norm(int n) const
Computes the norm of all the components of an Array.
SpatialVariableMetadata & metadata(unsigned int N=0)
Returns a reference to the SpatialVariableMetadata object containing metadata for the compoment N.
std::shared_ptr< BedThermalUnit > Ptr
void vertical_grid_from_options(std::shared_ptr< const Config > config)
Process -Mz and -Lz; set z;.
void ownership_ranges_from_options(unsigned int size)
Re-compute ownership ranges. Uses current values of Mx and My.
unsigned int Mx
Number of grid points in the X direction.
double Ly
Domain half-width in the Y direction.
double Lx
Domain half-width in the X direction.
unsigned int My
Number of grid points in the Y direction.
Grid parameters; used to collect defaults before an Grid is allocated.
std::shared_ptr< System > Ptr
struct TestKParameters exactK(double t, double z, int bedrock_is_ice)
void append_time(const File &file, const Config &config, double time_seconds)
Prepare a file for output.
@ PISM_READWRITE_MOVE
create a file for writing, move foo.nc to foo.nc~ if present
void define_time(const File &file, const Context &ctx)
Prepare a file for output.
double convert(System::Ptr system, double input, const std::string &spec1, const std::string &spec2)
Convert a quantity from unit1 to unit2.
io::Backend string_to_backend(const std::string &backend)
InputOptions process_input_options(MPI_Comm com, Config::ConstPtr config)
Logger::Ptr logger_from_options(MPI_Comm com)
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...
void print_config(const Logger &log, int verbosity_threshhold, const Config &config)
Report configuration parameters to stdout.
static BTUGrid FromOptions(std::shared_ptr< const Context > ctx)