ttesting.py - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch) (HTM) git clone git://src.adamsgaard.dk/pism (DIR) Log (DIR) Files (DIR) Refs (DIR) LICENSE --- ttesting.py (3682B) --- 1 """Utility functions used by PISM's regression tests""" 2 3 import PISM 4 import numpy as np 5 6 def shallow_grid(Mx=3, My=5, Lx=10e3, Ly=20e3): 7 "Create a shallow computational grid for testing" 8 return PISM.IceGrid_Shallow(PISM.Context().ctx, 9 Lx, Ly, 0, 0, Mx, My, 10 PISM.CELL_CORNER, 11 PISM.NOT_PERIODIC) 12 13 def sample(vec, i=0, j=0): 14 "Sample a PISM array" 15 with PISM.vec.Access(nocomm=vec): 16 return vec[i, j] 17 18 def check(vec, value): 19 "Check if values of vec are almost equal to value." 20 np.testing.assert_almost_equal(sample(vec), value) 21 22 def check_difference(A, B, value): 23 "Check if the difference between A and B is almost equal to value." 24 np.testing.assert_almost_equal(sample(A) - sample(B), value) 25 26 def check_ratio(A, B, value): 27 "Check if the ratio of A and B is almost equal to value." 28 b = sample(B) 29 if b != 0: 30 np.testing.assert_almost_equal(sample(A) / b, value) 31 else: 32 np.testing.assert_almost_equal(sample(A), 0.0) 33 34 def create_scalar_forcing(file_name, variable_name, units, values, times, time_bounds=None): 35 "Create a dummy scalar forcing file (delta_T, etc)." 36 37 ctx = PISM.Context() 38 39 time_name = ctx.config.get_string("time.dimension_name") 40 forcing = PISM.Timeseries(ctx.com, ctx.unit_system, variable_name, 41 time_name) 42 forcing.variable().set_string("units", units) 43 44 if time_bounds is not None: 45 for k in range(len(values)): 46 # NB: ignores times 47 bounds = np.array(time_bounds, dtype=np.float64) 48 forcing.append(values[k], bounds[2*k], bounds[2*k + 1]) 49 forcing.set_use_bounds(True) 50 else: 51 forcing.set_use_bounds(False) 52 for k in range(len(values)): 53 # NB: we are not using bounds, so it is OK to use times[k] for both endpoints 54 t = np.array(times, dtype=np.float64) 55 forcing.append(values[k], times[k], times[k]) 56 57 output = PISM.util.prepare_output(file_name, append_time=False) 58 if time_bounds is not None: 59 output.write_attribute(time_name, "bounds", time_name + "_bounds") 60 forcing.write(output) 61 62 def create_forcing(grid, file_name, variable_name, units, values, 63 time_units=None, times=None, time_bounds=None): 64 "Create a dummy 2D forcing without spatial variability" 65 66 if times is None and time_bounds is None: 67 raise ValueError("both times and time_bounds are None") 68 69 ctx = grid.ctx() 70 config = grid.ctx().config() 71 72 time_name = config.get_string("time.dimension_name") 73 time_bounds_name = time_name + "_bounds" 74 75 if time_units is None: 76 time_units = "seconds since 1-1-1" 77 78 v = PISM.IceModelVec2S(grid, variable_name, PISM.WITHOUT_GHOSTS) 79 v.metadata().set_string("units", units) 80 81 output = PISM.util.prepare_output(file_name, append_time=False) 82 output.write_attribute(time_name, "units", time_units) 83 84 if time_bounds is not None: 85 output.write_attribute(time_name, "bounds", time_bounds_name) 86 87 bounds = PISM.TimeBoundsMetadata(time_bounds_name, time_name, ctx.unit_system()) 88 89 assert len(values) + 1 == len(time_bounds) 90 for k in range(len(values)): 91 PISM.append_time(output, "time", time_bounds[k + 1]) 92 93 PISM.write_time_bounds(output, bounds, k, (time_bounds[k], time_bounds[k + 1])) 94 95 v.set(values[k]) 96 v.write(output) 97 else: 98 for k in range(len(values)): 99 PISM.append_time(output, "time", times[k]) 100 v.set(values[k]) 101 v.write(output) 102 103 output.close()