Skip to content

Commit

Permalink
add verbosity and random seed options to the yaml parser, change the …
Browse files Browse the repository at this point in the history
…behavior of the verbose flag to be an integer, default is 0, silent, 1 is some output on each timestep, 2 is lots of output all over the place
  • Loading branch information
amoodie committed May 6, 2020
1 parent f01fec9 commit 0772b4a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 27 deletions.
6 changes: 6 additions & 0 deletions pyDeltaRCM/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ case_prefix:
out_dir:
type: 'str'
default: 'deltaRCM_Output'
verbose:
type: 'int'
default: 0
seed:
type: 'int'
default: -1
Length:
type: 'float'
default: 1000.
Expand Down
9 changes: 2 additions & 7 deletions pyDeltaRCM/deltaRCM_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

class pyDeltaRCM(Tools):

#############################################
################## __init__ #################
#############################################

def __init__(self, input_file = None):
def __init__(self, input_file=None):
"""Creates an instance of the pyDeltaRCM model.
This method handles setting up the run, including parsing input files,
Expand All @@ -33,7 +29,6 @@ def __init__(self, input_file = None):
self._time = 0.
self._time_step = 1.

self.verbose = False
self.input_file = input_file
self._file_dir = os.path.realpath(os.path.dirname(__file__))
self.default_file = os.path.join(self._file_dir, 'default.yml')
Expand Down Expand Up @@ -71,7 +66,7 @@ def finalize(self):

try:
self.output_netcdf.close()
if self.verbose:
if self.verbose >= 1:
print('Closed output netcdf file.')
except Exception:
pass
Expand Down
26 changes: 12 additions & 14 deletions pyDeltaRCM/deltaRCM_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
from .water_tools import water_tools
from .init_tools import init_tools

np.random.seed(0)


class Tools(sed_tools, water_tools, init_tools, object):

Expand All @@ -35,9 +33,9 @@ def run_one_timestep(self):

timestep = self._time

if self.verbose:
if self.verbose > 0:
print('-' * 20)
print('Time = ' + str(self._time))
print('Timestep: ' + str(self._time))

for iteration in range(self.itermax):

Expand Down Expand Up @@ -100,7 +98,7 @@ def record_stratigraphy(self):
if self.strata_eta.shape[1] <= timestep:
self.expand_stratigraphy()

if self.verbose:
if self.verbose >= 2:
self.logger.info('Storing stratigraphy data')

# ------------------ sand frac ------------------
Expand Down Expand Up @@ -161,7 +159,7 @@ def apply_subsidence(self):
timestep = self._time

if self.start_subsidence <= timestep:
if self.verbose:
if self.verbose >= 2:
self.logger.info('Applying subsidence')
self.eta[:] = self.eta - self.sigma

Expand Down Expand Up @@ -223,27 +221,27 @@ def output_data(self):

# ------------------ grids ------------------
if self.save_eta_grids:
if self.verbose:
if self.verbose >= 2:
self.logger.info('Saving grid: eta')
self.save_grids('eta', self.eta, shape[0])

if self.save_depth_grids:
if self.verbose:
if self.verbose >= 2:
self.logger.info('Saving grid: depth')
self.save_grids('depth', self.depth, shape[0])

if self.save_stage_grids:
if self.verbose:
if self.verbose >= 2:
self.logger.info('Saving grid: stage')
self.save_grids('stage', self.stage, shape[0])

if self.save_discharge_grids:
if self.verbose:
if self.verbose >= 2:
self.logger.info('Saving grid: discharge')
self.save_grids('discharge', self.qw, shape[0])

if self.save_velocity_grids:
if self.verbose:
if self.verbose >= 2:
self.logger.info('Saving grid: velocity')
self.save_grids('velocity', self.uw, shape[0])

Expand All @@ -254,7 +252,7 @@ def output_strata(self):

if self.save_strata:

if self.verbose:
if self.verbose >= 2:
self.logger.info('\nSaving final stratigraphy to netCDF file')

self.strata_eta = self.strata_eta[:, :self.strata_counter]
Expand Down Expand Up @@ -295,7 +293,7 @@ def output_strata(self):

self.output_netcdf.variables['strata_depth'][i, :, :] = sz

if self.verbose:
if self.verbose >= 2:
self.logger.info('Stratigraphy data saved.')

def save_figure(self, path, ext='png', close=True):
Expand All @@ -318,7 +316,7 @@ def save_figure(self, path, ext='png', close=True):
directory = '.'

if not os.path.exists(directory):
if self.verbose:
if self.verbose >= 2:
self.logger.info('Creating output directory')
os.makedirs(directory)

Expand Down
22 changes: 17 additions & 5 deletions pyDeltaRCM/init_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class init_tools(object):

def init_logger(self):

if self.verbose:
if self.verbose >= 1:

self.logger = logging.getLogger("driver")
self.logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -85,6 +85,9 @@ def import_files(self):
for k, v in list(input_file_vars.items()):
setattr(self, k, v)

if self.seed >= 0:
np.random.seed(self.seed)

def set_constants(self):

self.g = 9.81 # (gravitation const.)
Expand Down Expand Up @@ -128,6 +131,15 @@ def set_constants(self):
[1, 1, 1]])

def create_other_variables(self):
"""Model implementation variables.
Creates variables for model implementation, from specified boundary
condition variables. This method is run during initial model
instantition, but it is also run any time an inlet flow condition
variable is changed, including ``channel_flow_velocity``,
``channel_width``, ``channel_flow_depth``, and
``influx_sediment_concentration``.
"""

self.init_Np_water = self.Np_water
self.init_Np_sed = self.Np_sed
Expand Down Expand Up @@ -323,21 +335,21 @@ def init_output_grids(self):
self.save_velocity_grids or
self.save_strata):

if self.verbose:
if self.verbose >= 2:
self.logger.info('Generating netCDF file for output grids...')

directory = self.prefix
filename = 'pyDeltaRCM_output.nc'

if not os.path.exists(directory):
if self.verbose:
if self.verbose >= 2:
self.logger.info('Creating output directory')
os.makedirs(directory)

file_path = os.path.join(directory, filename)

if os.path.exists(file_path):
if self.verbose:
if self.verbose >= 2:
self.logger.info('*** Replaced existing netCDF file ***')
os.remove(file_path)

Expand Down Expand Up @@ -396,7 +408,7 @@ def init_output_grids(self):
('total_time', 'length', 'width'))
velocity.units = 'meters per second'

if self.verbose:
if self.verbose >= 2:
self.logger.info('Output netCDF file created.')

def init_subsidence(self):
Expand Down
1 change: 1 addition & 0 deletions pyDeltaRCM/sed_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

# tools for sediment routing algorithms and deposition/erosion


class sed_tools(shared_tools):

def sed_route(self):
Expand Down
2 changes: 1 addition & 1 deletion pyDeltaRCM/water_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def check_size_of_indices_matrix(self, it):
self.iter for all further timesteps
"""

if self.verbose:
if self.verbose >= 2:
self.logger.info('Increasing size of self.indices')

indices_blank = np.zeros(
Expand Down
1 change: 1 addition & 0 deletions tests/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ case_prefix: ''
out_dir: test
Length: 10.0
Width: 10.0
seed: 0
dx: 1.0
L0_meters: 1.0
S0: 0.0002
Expand Down
8 changes: 8 additions & 0 deletions tests/test_init_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
# now that it is initiated can access the init_tools via the inherited object
# delta.**init_tools_function**

# Tests for attrs set during yaml parsing
def test_set_verbose():
assert delta.verbose == 0


def test_set_seed():
assert delta.seed == 0


# Tests for all of the constants
delta.set_constants()
Expand Down

0 comments on commit 0772b4a

Please sign in to comment.