Skip to content

Commit

Permalink
Merge pull request #104 from SallyDa/random
Browse files Browse the repository at this point in the history
Generate random numbers for each radiation call
  • Loading branch information
JoyMonteiro authored Jun 4, 2019
2 parents 23d0ce8 + 86ba966 commit ddc6530
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion climt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
EmanuelConvection, SlabSurface, GFSDynamicalCore, DcmipInitialConditions,
IceSheet, Instellation, DryConvectiveAdjustment)

__version__ = '0.16.2'
__version__ = '0.16.3'
34 changes: 18 additions & 16 deletions climt/_components/rrtmg/lw/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ def __init__(
calculate_interface_temperature=True,
mcica=False,
random_number_generator=0,
permute_seed=112,
**kwargs):
"""
Expand Down Expand Up @@ -233,9 +232,6 @@ def __init__(
* random_number_generator = 0: kissvec
* random_number_generator = 1: Mersenne Twister
permute_seed (int):
For McICA, permute the seed between each call to the cloud generator.
.. _[Ebert and Curry 1992]:
http://onlinelibrary.wiley.com/doi/10.1029/91JD02472/abstract
Expand All @@ -254,8 +250,8 @@ def __init__(

self._mcica = mcica
if mcica:
self._permute_seed = None
self._random_number_generator = random_number_generator
self._permute_seed = permute_seed
if type(cloud_overlap_method) is str:
if cloud_overlap_method.lower() == 'clear_only':
logging.info(
Expand Down Expand Up @@ -307,17 +303,7 @@ def __init__(
self._stef_boltz,
self._secs_per_day)

if mcica:
_rrtmg_lw.initialise_rrtm_radiation_mcica(
self._Cpd,
self._cloud_overlap,
self._calc_dflxdt,
self._cloud_optics,
self._ice_props,
self._liq_props,
self._permute_seed,
self._random_number_generator)
else:
if not mcica:
# TODO Add all other flags as well
_rrtmg_lw.initialise_rrtm_radiation(
self._Cpd,
Expand Down Expand Up @@ -378,6 +364,22 @@ def array_call(self, state):
(mid_levels, num_cols, num_reduced_g_intervals))
}

# Change parameter for random number generator - each time the
# radiation is called, with the same state / input properties,
# a different result is obtained, because the wavelengths which
# see cloud differ between each call.
self._permute_seed = np.random.randint(0, 500)

_rrtmg_lw.initialise_rrtm_radiation_mcica(
self._Cpd,
self._cloud_overlap,
self._calc_dflxdt,
self._cloud_optics,
self._ice_props,
self._liq_props,
self._permute_seed,
self._random_number_generator)

_rrtmg_lw.rrtm_calculate_longwave_fluxes_mcica(
self.rrtm_iplon,
n_columns,
Expand Down
41 changes: 22 additions & 19 deletions climt/_components/rrtmg/sw/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,6 @@ def __init__(
* random_number_generator = 0: kissvec
* random_number_generator = 1: Mersenne Twister
permute_seed (int):
For McICA, permute the seed between each call to the cloud generator.
.. _[Ebert and Curry 1992]:
http://onlinelibrary.wiley.com/doi/10.1029/91JD02472/abstract
Expand All @@ -332,8 +329,8 @@ def __init__(
self._mcica = mcica

if mcica:
self._permute_seed = None
self._random_number_generator = random_number_generator # TODO: make dictionary
self._permute_seed = permute_seed
if type(cloud_overlap_method) is str:
if cloud_overlap_method.lower() == 'clear_only':
logging.info(
Expand Down Expand Up @@ -410,21 +407,7 @@ def __init__(
self._stef_boltz,
self._secs_per_day)

if mcica:
_rrtmg_sw.initialise_rrtm_radiation_mcica(
self._Cpd,
self._solar_const,
self._fac_sunspot_coeff,
self._solar_var_by_band,
self._cloud_overlap,
self._cloud_optics,
self._ice_props,
self._liq_props,
self._aerosol_type,
self._solar_var_flag,
self._random_number_generator,
self._permute_seed)
else:
if not mcica:
_rrtmg_sw.initialise_rrtm_radiation(
self._Cpd,
self._solar_const,
Expand Down Expand Up @@ -515,6 +498,26 @@ def array_call(self, state):
(mid_levels, num_cols, num_reduced_g_intervals))
}

# Change parameter for random number generator - each time the
# radiation is called, with the same state / input properties,
# a different result is obtained, because the wavelengths which
# see cloud differ between each call.
self._permute_seed = np.random.randint(0, 500)

_rrtmg_sw.initialise_rrtm_radiation_mcica(
self._Cpd,
self._solar_const,
self._fac_sunspot_coeff,
self._solar_var_by_band,
self._cloud_overlap,
self._cloud_optics,
self._ice_props,
self._liq_props,
self._aerosol_type,
self._solar_var_flag,
self._random_number_generator,
self._permute_seed)

_rrtmg_sw.rrtm_calculate_shortwave_fluxes_mcica(
self.rrtm_iplon,
state['air_temperature'].shape[1],
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.16.2
current_version = 0.16.3
commit = True
tag = False

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def run(self):

setup(
name='climt',
version='0.16.2',
version='0.16.3',
description='CliMT is a Toolkit for building Earth system models in Python.',
long_description=readme + '\n\n' + history,
author="Rodrigo Caballero",
Expand Down
Binary file modified tests/cached_component_output/TestRRTMGLongwaveMCICA-3d-0.cache
Binary file not shown.
Binary file modified tests/cached_component_output/TestRRTMGLongwaveMCICA-3d-1.cache
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def transpose_state(state, dims=None):

def call_with_timestep_if_needed(
component, state, timestep=timedelta(seconds=10.)):
np.random.seed(0)
if isinstance(component, (Stepper, TendencyStepper, ImplicitTendencyComponent)):
return component(state, timestep=timestep)
else:
Expand Down

0 comments on commit ddc6530

Please sign in to comment.