initial commit

parent 2178eea1
......@@ -165,110 +165,307 @@ class Simulation:
return results_recorder.results()
def from_input_dict(d):
"""Creates a Simulation object from an input dictionary"""
input_dict_validator(d)
# Simulation Clock
simulation_clock = SimulationClock(d['time_step'], d['duration'])
# Drifting Particles
initial_position = np.tile(
np.array([d['x'], d['y'], d['z']]), (d['num_eggs'], 1))
if d['varying_dd'] == 'constant':
drift = ConstantDriftingParticle(d['density'], d['diameter'],
initial_position)
elif d['species'] == 'grass':
drift = GrassCarpEggs(initial_position, simulation_clock)
elif d['species'] == 'silver':
drift = SilverCarpEggs(initial_position, simulation_clock)
elif d['species'] == 'bighead':
drift = BigheadCarpEggs(initial_position, simulation_clock)
# Transporter
transporter_model = init_transporter(
simulation_clock, drift, d['diffusivity'], d['direction'])
# Simulation
sim = Simulation(drift, transporter_model, simulation_clock)
# Hydraulic cells (csv vs. hecras)
if d['hydraulic_mode'] == 'csv':
# Hydraulic channel (CSV)
hydraulic_cells = from_csv(d['csv_path'])
# Hecras Mode
if d['hydraulic_mode'] == 'hecras':
with RASProject(d['hecras_path']) as rp:
plan_name = d['hecras_plan']
rp.set_current_plan(plan_name)
profile_name = d['hecras_profile']
temperature = d['hecras_temperature']
hydraulic_data_frame = rp.hydraulic_model_data(
profile_name, temperature)
hydraulic_cells = \
RoughBottomSeriesOfHydraulicCells. \
from_data_frame(hydraulic_data_frame,
start_time=d['hecras_start_time'],
simulation_clock=simulation_clock,
simulation=sim)
# Update sim & transporter with hydraulic cells
sim.set_hydraulic_model(hydraulic_cells)
transporter_model.set_hydraulic_model(hydraulic_cells)
# Return simulation
return sim
def input_dict_validator(d):
"""Validates a list of simulation inputs and runs the simlation if inputs
are valid
class SimulationParameters:
""" Class that generates an input dictionary and validates it as needed.
"""
# Hydraulic Input Mode
if not d['hydraulic_mode']:
raise ValueError('hydraulic_mode must be type str')
# CSV path
if not isinstance(d['csv_path'], str):
raise ValueError('csv_path must be type str')
# Hecras path
if not isinstance(d['hecras_path'], str):
raise ValueError('hecras_path must be type str')
# Diffusivity
if (d['diffusivity'] != 'parabolic') and \
(d['diffusivity'] != 'constant') and \
(d['diffusivity'] != 'parabolic-constant'):
raise ValueError(
'diffusivity must be parabolic, constant, or parabolic-constant')
# XYZ Position
if (not isinstance(d['x'], float)) and (not isinstance(d['x'], int)):
raise ValueError('x must be type float or int')
if (not isinstance(d['y'], float)) and (not isinstance(d['y'], int)):
raise ValueError('y must be type float or int')
if (not isinstance(d['z'], float)) and (not isinstance(d['z'], int)):
raise ValueError('z must be type float or int')
# Number of eggs
if not isinstance(d['num_eggs'], int):
raise ValueError('num_eggs must be type int')
# Species
if (d['species'] != 'grass') and \
(d['species'] != 'silver') and \
(d['species'] != 'bighead'):
raise ValueError('species must be grass, silver, or bighead')
# Varying density & diameter
if (d['varying_dd'] != 'constant') and (d['varying_dd'] != 'varying'):
raise ValueError('varying_dd must be constant or varying.')
# Direction of simulation
if (d['direction'] != 'forward') and (d['direction'] != 'reverse'):
raise ValueError('direction must be forward or reverse.')
# Duration
if (not isinstance(d['duration'], float)) and \
(not isinstance(d['duration'], int)):
raise ValueError('duration must be type float or int')
# Time step
if (not isinstance(d['time_step'], float)) and \
(not isinstance(d['time_step'], int)):
raise ValueError('time_step must be type float or int')
# Simulation name
if not isinstance(d['sim_name'], str):
raise ValueError('sim_name must be type str')
def __init__(self):
# valid keys for the input dictionary
valid_keys = [
'time_step',
'duration',
'x',
'y',
'z',
'num_eggs',
'species',
'diffusivity',
'direction',
'hydraulic_mode',
'csv_path',
'hecras_path',
'hecras_plan',
'hecras_profile',
'hecras_temperature',
'hecras_steadiness',
'hecras_start_time',
'hecras_project',
'varying_dd',
'density',
'diameter',
'sim_name']
# create dictionary from the valid keys
init_values = [None] * len(valid_keys)
self._dict = dict(zip(valid_keys, init_values))
def __eq__(self, other):
"""
:param other:
:return:
"""
return (
self.__class__.__name__ == other.__class__.__name__) and (
self._dict == other.get_dict())
def __getitem__(self, key):
"""Return the requested parameter value.
:param key: Parameter key
:return: Parameter corresponding to the given key
"""
return self._dict[key]
def __repr__(self):
return self._dict.__repr__()
def __setitem__(self, key, value):
"""Set the requested parameter value.
:param key: Parameter key
:param value: Value to be stored
:return: Nothing
"""
self._check_value(key, value)
self._dict[key] = value
def __str__(self):
return self._dict.__str__()
def _check_value(self, key, value):
"""Check if the provided value is valid for the given key. Raise an exception if not.
:param key: Keyword for configuration item
:param value: Value for corresponding key
:return: Nothing
"""
self._check_key(key, value)
def _check_key(self, key, value):
"""Check if the provided key exists in the _dict. Raise an exception if not.
:param key: Parameter key to check
:return: Nothing
"""
if key not in self._dict.keys():
raise KeyError(key)
# Hydraulic Input Mode
if key == "hydraulic_mode":
if isinstance(value, str):
return
else:
raise ValueError('hydraulic_mode must be type str')
# CSV path
elif key == "csv_path":
if isinstance(value, str):
return
else:
raise ValueError('csv_path must be type str')
# Hecras path
elif key == "hecras_path":
if isinstance(value, str):
return
else:
raise ValueError('hecras_path must be type str')
# Hecras Project - REDUNDANT. REMOVE.
elif key == "hecras_project":
if isinstance(value, str):
return
else:
raise ValueError('hecras_project must be type str')
# Hecras Plan
elif key == "hecras_plan":
if isinstance(value, str):
return
else:
raise ValueError('hecras_plan must be type str')
# Hecras Profile
elif key == "hecras_profile":
if isinstance(value, str):
return
else:
raise ValueError('hecras_profile must be type str')
# Hecras Steadiness
elif key == "hecras_steadiness":
if isinstance(value, str): # check
return
else:
raise ValueError('hecras_steadiness must be type str')
# Hecras Temperature
elif key == "hecras_temperature":
if (isinstance(value, float)) or (isinstance(value, int)):
return
else:
raise ValueError(
'hecras_temperature must be type float or int')
# Hecras Start Time
elif key == "hecras_start_time":
if (isinstance(value, np.datetime64)
or isinstance(value, pd.Timestamp)):
return
else:
raise ValueError(
'hecras_start_time must be type datetime.time')
# Diffusivity
elif key == "diffusivity":
if (value == 'parabolic') or \
(value == 'constant') or \
(value == 'parabolic-constant'):
return
else:
raise ValueError(
'diffusivity must be parabolic, constant, or parabolic-constant')
# XYZ Position
elif key == "x":
if (isinstance(value, float)) or (isinstance(value, int)):
return
else:
raise ValueError('x must be type float or int')
elif key == "y":
if (isinstance(value, float)) or (isinstance(value, int)):
return
else:
raise ValueError('y must be type float or int')
elif key == "z":
if (isinstance(value, float)) or (isinstance(value, int)):
return
else:
raise ValueError('z must be type float or int')
# Number of eggs
elif key == "num_eggs":
if isinstance(value, int):
return
else:
raise ValueError('num_eggs must be type int')
# Species
elif key == "species":
if (value == 'grass') or \
(value == 'silver') or \
(value == 'bighead'):
return
else:
raise ValueError('species must be grass, silver, or bighead')
# Varying density & diameter
elif key == "varying_dd":
if (value == 'constant') or (value == 'varying'):
return
else:
raise ValueError('varying_dd must be constant or varying.')
# Direction of simulation
elif key == "direction":
if (value == 'forward') or (value == 'reverse'):
return
else:
raise ValueError('direction must be forward or reverse.')
# Duration
elif key == "duration":
if (isinstance(value, float)) or \
(isinstance(value, int)):
return
else:
raise ValueError('duration must be type float or int')
# Time step
elif key == "time_step":
if (isinstance(value, float)) or \
(isinstance(value, int)):
return
else:
raise ValueError('time_step must be type float or int')
# Simulation name
elif key == "sim_name":
if isinstance(value, str):
return
else:
raise ValueError('sim_name must be type str')
def generate_sim(self):
d = self.get_dict()
# Simulation Clock
simulation_clock = SimulationClock(d['time_step'], d['duration'])
# Drifting Particles
initial_position = np.tile(
np.array([d['x'], d['y'], d['z']]), (d['num_eggs'], 1))
if d['species'] == 'grass':
drift = GrassCarpEggs(initial_position, simulation_clock)
elif d['species'] == 'silver':
drift = SilverCarpEggs(initial_position, simulation_clock)
elif d['species'] == 'bighead':
drift = BigheadCarpEggs(initial_position, simulation_clock)
# Transporter
transporter_model = init_transporter(
simulation_clock, drift, d['diffusivity'], d['direction'])
# Simulation
sim = Simulation(drift, transporter_model, simulation_clock)
# Hydraulic cells (csv vs. hecras)
if d['hydraulic_mode'] == 'csv':
# Hydraulic channel (CSV)
hydraulic_cells = from_csv(d['csv_path'])
# Hecras Mode
if d['hydraulic_mode'] == 'hecras':
with RASProject(d['hecras_path']) as rp:
plan_name = d['hecras_plan']
rp.set_current_plan(plan_name)
profile_name = d['hecras_profile']
temperature = d['hecras_temperature']
hydraulic_data_frame = rp.hydraulic_model_data(
profile_name, temperature)
hydraulic_cells = \
RoughBottomSeriesOfHydraulicCells. \
from_data_frame(hydraulic_data_frame,
start_time=d['hecras_start_time'],
simulation_clock=simulation_clock,
simulation=sim)
# Update sim & transporter with hydraulic cells
sim.set_hydraulic_model(hydraulic_cells)
transporter_model.set_hydraulic_model(hydraulic_cells)
# Return simulation
return sim
def items(self):
"""Return a set-like object providing a view on the contained parameters.
:return: Set-like object providing a view on the contained parameters
"""
return self._dict.items()
def keys(self):
"""Return the parameter keys.
:return: A set-like object providing a view on the parameter keys.
"""
return self._dict.keys()
def update(self, update_values):
"""Update the parameters.
:param update_values: Item containing key/value processing parameters
:return: Nothing
"""
for key, value in update_values.items():
self._check_value(key, value)
self._dict[key] = value
def get_dict(self):
"""Return a dictionary containing the processing parameters.
:return: Dictionary containing the processing parameters
"""
return deepcopy(self._dict)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment