Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial preprocessor implementation #52

Merged
merged 15 commits into from
Jun 9, 2020
5 changes: 2 additions & 3 deletions pyDeltaRCM/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from .deltaRCM_tools import Tools
from .deltaRCM_driver import pyDeltaRCM
from .model import DeltaModel
from .bmi_delta import BmiDelta
from .shared_tools import _get_version

__all__ = ['Tools', 'pyDeltaRCM', 'BmiDelta']
__all__ = ['DeltaModel', 'BmiDelta']
__version__ = _get_version()
4 changes: 2 additions & 2 deletions pyDeltaRCM/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyDeltaRCM import command_line
from . import preprocessor

if __name__ == '__main__':
command_line.run_model()
preprocessor.preprocessor_wrapper()
4 changes: 2 additions & 2 deletions pyDeltaRCM/bmi_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
from basic_modeling_interface import Bmi

from .deltaRCM_driver import pyDeltaRCM
from .model import DeltaModel

"""Basic Model Interface implementation for pyDeltaRCM."""

Expand Down Expand Up @@ -178,7 +178,7 @@ def initialize(self, filename = 'deltaRCM.yaml'):
yaml.dump(input_file_vars, inbetweenYAML)
inbetweenYAML.close()

self._delta = pyDeltaRCM(input_file = tmpFile)
self._delta = DeltaModel(input_file = tmpFile)

self._values = {
'channel_exit_water_flow__speed': self._delta.u0,
Expand Down
33 changes: 0 additions & 33 deletions pyDeltaRCM/command_line.py

This file was deleted.

3 changes: 0 additions & 3 deletions pyDeltaRCM/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,3 @@ coeff_U_ero_sand:
alpha:
type: ['float', 'int']
default: 0.1
timesteps:
type: ['float', 'int']
default: 10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come we remove the notion of a default timestepping?

15 changes: 7 additions & 8 deletions pyDeltaRCM/deltaRCM_driver.py → pyDeltaRCM/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
import os


class pyDeltaRCM(Tools):
"""Main pyDeltaRCM model class.

This is the main model class defined by the package. Instantiating the
model class is described in the :meth:`__init__` method below in detail,
but generally model instantiation occurs via a model run YAML
configuration file. These YAML configuration files define model parameters
which are used in the run; read more about creating input YAML
class DeltaModel(Tools):
"""Main model class.

Instantiating the model class is described in the :meth:`__init__` method
below in detail, but generally model instantiation occurs via a model run
YAML configuration file. These YAML configuration files define model
parameters which are used in the run; read more about creating input YAML
configuration files in the :doc:`../../guides/userguide`.

Once the model has been instantiated, the model is updated via the
Expand Down
111 changes: 111 additions & 0 deletions pyDeltaRCM/preprocessor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import os
import argparse
import abc

import yaml

from .shared_tools import _get_version
from .model import DeltaModel

_ver = _get_version()


class BasePreprocessor(abc.ABC):
"""Base preprocessor class.

Defines a prelimiary yaml file parsing, the model instatiation,
and timestep loop routine.

Subclasses handle the high-level command line API
and the python API.
"""

def __init__(self):
pass

def preliminary_yaml_parsing(self):
"""A very preliminiary yaml parsing step.

Here, we check whether

1) the file is valid yaml
2) for a field named ``matrix`` in the yaml file
3) for a field named ``timesteps`` in the yaml file, which is used
for time looping if it is given. If ``timesteps`` is not given,
an error is raised, and the user is directed to either fix the
issue or use the low-level API.

"""

# open the file, an error will be thrown if invalid yaml?
user_file = open(self.input_file, mode='r')
user_dict = yaml.load(user_file, Loader=yaml.FullLoader)
user_file.close()

if 'matrix' in user_dict.keys():
raise NotImplementedError('Matrix expansion not yet implemented...')

if 'timesteps' in user_dict.keys():
self.timesteps = user_dict['timesteps']
else:
raise ValueError('You must specify timesteps to use the high-level API.')

def instatiate_model(self):
self.deltamodel = DeltaModel(input_file=self.input_file)

def run_model(self):
"""Loop the model.

Iterate the timestep ``update`` routine for the specified number of
iterations.
"""

for _t in range(0, self.timesteps):
self.deltamodel.update()

self.deltamodel.finalize()


class CLI_API(BasePreprocessor):

def __init__(self):

super().__init__()

self.process_arguments()

if self.args['config']:
self.input_file = self.args['config']
self.preliminary_yaml_parsing()
else:
self.input_file = None

self.instatiate_model()

self.run_model()

def process_arguments(self):
parser = argparse.ArgumentParser(
description='Options for running pyDeltaRCM from command line')

parser.add_argument(
'--config', help='Path to a config file that you would like to use.')
parser.add_argument('--version', action='version',
version=_ver, help='Print pyDeltaRCM version.')

args = parser.parse_args()

self.args = vars(args)


class Python_API(BasePreprocessor):

def __init__(self):
raise NotImplementedError
super().__init__(self)
pass


if __name__ == '__main__':

CLI_API()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
'basic-modeling-interface', 'scipy', 'numpy', 'pyyaml',
'numba'],
entry_points={
'console_scripts': ['run_pyDeltaRCM=pyDeltaRCM.command_line:run_model'],
'console_scripts': ['run_pyDeltaRCM=pyDeltaRCM.preprocessor:CLI_API'],
}
)
4 changes: 2 additions & 2 deletions tests/test_consistent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import os
import numpy as np

from pyDeltaRCM.deltaRCM_driver import pyDeltaRCM
from pyDeltaRCM.model import DeltaModel

# need to create a simple case of pydeltarcm object to test these functions


def test_bed_after_one_update():
"""
"""
delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta.update()
# slice is: delta.eta[:5, 4]
# print(delta.eta[:5, 4])
Expand Down
21 changes: 10 additions & 11 deletions tests/test_deltaRCM_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import numpy as np

from pyDeltaRCM.deltaRCM_driver import pyDeltaRCM
from pyDeltaRCM.model import DeltaModel

# need to create a simple case of pydeltarcm object to test these functions

Expand All @@ -15,27 +15,26 @@ def test_init():
"""
test the deltaRCM_driver init (happened when delta.initialize was run)
"""
delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
assert delta._time == 0.
assert delta._is_finalized == False


def test_update():
delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))

delta.update()
assert delta._time == 1.0
assert delta._is_finalized == False


def test_finalize():
delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta.finalize()
assert delta._is_finalized == True


def test_multifinalization_error():
err_delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
err_delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
err_delta.update()
# test will Fail if any assertion is wrong
assert err_delta._time == 1.0
Expand All @@ -49,7 +48,7 @@ def test_multifinalization_error():

def test_getters_setters():

delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))

assert np.all(delta.sea_surface_elevation == 0)
assert delta.water_depth[0, 2] == 0
Expand All @@ -71,27 +70,27 @@ def test_getters_setters():
delta.bedload_fraction = 0.25
assert delta.bedload_fraction == 0.25

delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))

assert delta.channel_flow_velocity == 1
delta.channel_flow_velocity = 3
assert delta.channel_flow_velocity == 3
assert delta.u_max == 6

delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))

assert delta.channel_width == 2
delta.channel_width = 10
assert delta.channel_width == 10
assert delta.N0 == 10

delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))

assert delta.channel_flow_depth == 1
delta.channel_flow_depth = 2
assert delta.channel_flow_depth == 2

delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))

assert delta.influx_sediment_concentration == 0.1
delta.influx_sediment_concentration = 2
Expand Down
5 changes: 2 additions & 3 deletions tests/test_deltaRCM_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import os
import numpy as np

from pyDeltaRCM.deltaRCM_driver import pyDeltaRCM
from pyDeltaRCM import Tools
from pyDeltaRCM.model import DeltaModel

# need to create a simple case of pydeltarcm object to test these functions
delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))

# now that it is initiated can access the shared_tools via the inherited object
# delta._delta.**Tools_function**
Expand Down
4 changes: 2 additions & 2 deletions tests/test_init_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import os
import numpy as np

from pyDeltaRCM.deltaRCM_driver import pyDeltaRCM
from pyDeltaRCM.model import DeltaModel
from pyDeltaRCM import init_tools

# need to create a simple case of pydeltarcm object to test these functions
delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))
delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))

# now that it is initiated can access the init_tools via the inherited object
# delta.**init_tools_function**
Expand Down
Loading