def test_set_prognostic_update_frequency_updates_result_when_greater(): prognostic = UpdateFrequencyWrapper(MockPrognostic(), timedelta(hours=1)) state = {'time': timedelta(hours=0)} tendencies, diagnostics = prognostic({'time': timedelta(hours=0)}) tendencies, diagnostics = prognostic({'time': timedelta(hours=2)}) assert len(diagnostics) == 1 assert diagnostics['num_updates'] == 2
def test_set_update_frequency_updates_result_when_greater(self): component = UpdateFrequencyWrapper(self.get_component(), timedelta(hours=1)) assert isinstance(component, self.component_type) result = self.call_component(component, {'time': timedelta(hours=0)}) result = self.call_component(component, {'time': timedelta(hours=2)}) assert component.times_called == 2
def test_set_prognostic_update_frequency_caches_result_with_datetime(): prognostic = UpdateFrequencyWrapper(MockPrognostic(), timedelta(hours=1)) state = {'time': datetime(2000, 1, 1)} tendencies, diagnostics = prognostic(state) tendencies, diagnostics = prognostic(state) assert len(diagnostics) == 1 assert diagnostics['num_updates'] == 1
def test_set_update_frequency_calls_initially(self): component = UpdateFrequencyWrapper(self.get_component(), timedelta(hours=1)) assert isinstance(component, self.component_type) state = {'time': timedelta(hours=0)} result = self.call_component(component, state) assert component.times_called == 1
def test_set_update_frequency_does_not_update_when_less(self): component = UpdateFrequencyWrapper(self.get_component(), timedelta(hours=1)) assert isinstance(component, self.component_type) result = self.call_component(component, {'time': timedelta(hours=0)}) result = self.call_component(component, {'time': timedelta(minutes=59)}) assert component.times_called == 1
def test_set_update_frequency_does_not_repeat_call_at_same_datetime(self): component = UpdateFrequencyWrapper(self.get_component(), timedelta(hours=1)) assert isinstance(component, self.component_type) state = {'time': datetime(2010, 1, 1)} result = self.call_component(component, state) result = self.call_component(component, state) assert component.times_called == 1
def piecewise_constant_version(self, update_time): """ Returns component that updates once every :code:`update_time`. Args: update_time (timedelta): The time difference between updates. Returns: component (UpdateFrequencyWrapper): A "piecewise constant" component. """ return UpdateFrequencyWrapper(self, update_time)
def test_piecewise_constant_component(): radiation = UpdateFrequencyWrapper(RRTMGLongwave(), timedelta(seconds=1000)) state = climt.get_default_state([radiation]) current_tendency, current_diagnostic = radiation(state) # Perturb state_array state['air_temperature'] += 3 new_tendency, new_diagnostic = radiation(state) assert np.all(current_tendency['air_temperature'].values == new_tendency['air_temperature'].values) state['time'] += timedelta(seconds=1500) new_tendency, new_diagnostic = radiation(state) assert np.any(current_tendency['air_temperature'].values != new_tendency['air_temperature'].values)
def test_set_prognostic_update_frequency_calls_initially(): prognostic = UpdateFrequencyWrapper(MockPrognostic(), timedelta(hours=1)) state = {'time': timedelta(hours=0)} tendencies, diagnostics = prognostic(state) assert len(diagnostics) == 1 assert diagnostics['num_updates'] == 1
'latitude', 'longitude', 'convective_heating_rate'] climt.set_constants_from_dict({ 'stellar_irradiance': {'value': 200, 'units': 'W m^-2'}}) model_time_step = timedelta(seconds=600) # Create components convection = climt.EmanuelConvection() simple_physics = TimeDifferencingWrapper(climt.SimplePhysics()) constant_duration = 6 radiation_lw = UpdateFrequencyWrapper( climt.RRTMGLongwave(), constant_duration*model_time_step) radiation_sw = UpdateFrequencyWrapper( climt.RRTMGShortwave(), constant_duration*model_time_step) slab_surface = climt.SlabSurface() dycore = climt.GFSDynamicalCore( [simple_physics, slab_surface, radiation_sw, radiation_lw, convection], number_of_damped_levels=5 ) grid = climt.get_grid(nx=NUM_COLS, ny=NUM_ROWS) # Create model state my_state = climt.get_default_state([dycore], grid_state=grid)
ax.set_xlabel('longitude') ax.set_ylabel('latitude') ax.set_title('Lowest model level air temperature (K)') im = ax.pcolormesh( state['air_temperature'].to_units('degK').values[0, :, :], vmin=260., vmax=310.) cbar = fig.colorbar(im) plot_monitor = PlotFunctionMonitor(my_plot_function) state = get_initial_state(nx=256, ny=128, nz=64) state['time'] = datetime(2000, 1, 1) physics_stepper = AdamsBashforth( UpdateFrequencyWrapper(Radiation(), timedelta(hours=2)), BoundaryLayer(), DeepConvection(), ) implicit_dynamics = ImplicitDynamics() timestep = timedelta(minutes=30) while state['time'] < datetime(2010, 1, 1): physics_diagnostics, state_after_physics = physics_stepper(state) dynamics_diagnostics, next_state = implicit_dynamics(state_after_physics) state_after_physics.update(physics_diagnostics) state_after_physics.update(dynamics_diagnostics) plot_monitor.store(state_after_physics) next_state['time'] = state['time'] + timestep state = next_state
monitor = PlotFunctionMonitor(plot_function) netcdf_monitor = NetCDFMonitor('gcm_without_seasonal_cycle.nc', write_on_store=True, store_names=fields_to_store) set_constant('stellar_irradiance', value=200, units='W m^-2') model_time_step = timedelta(minutes=10) # Create components convection = climt.EmanuelConvection() simple_physics = TimeDifferencingWrapper(climt.SimplePhysics()) radiation_step = timedelta(hours=1) radiation_lw = UpdateFrequencyWrapper(climt.RRTMGLongwave(), radiation_step) radiation_sw = UpdateFrequencyWrapper(climt.RRTMGShortwave(), radiation_step) slab_surface = climt.SlabSurface() dycore = climt.GFSDynamicalCore( [simple_physics, slab_surface, radiation_sw, radiation_lw, convection], number_of_damped_levels=5) grid = climt.get_grid(nx=128, ny=62) # Create model state my_state = climt.get_default_state([dycore], grid_state=grid) # Set initial/boundary conditions latitudes = my_state['latitude'].values