Skip to content

Commit

Permalink
Straightforward fixes in get_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
margrietpalm committed Mar 27, 2024
1 parent 0b90c13 commit af51ee6
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 80 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_version():

install_requires = [
"numpy>=1.15",
"threedi-schema==0.220.*",
"threedi-schema@https://github.com/nens/threedi-schema/archive/refs/heads/margriet_46_schema_300.zip",
"shapely>=2",
"pyproj>=3",
"condenser[geo]>=0.1.1",
Expand Down
10 changes: 5 additions & 5 deletions threedigrid_builder/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def _make_gridadmin(
progress_callback(0.7, "Constructing 2D computational grid...")
quadtree = QuadTree(
subgrid_meta,
grid_settings.kmax,
grid_settings.grid_space,
grid_settings.nr_grid_levels,
grid_settings.minimum_cell_size,
grid_settings.use_2d_flow,
refinements,
)
Expand Down Expand Up @@ -115,7 +115,7 @@ def _make_gridadmin(
objects=channels,
fixed_nodes=breach_points,
cell_tree=grid.cell_tree if grid_settings.use_2d else None,
global_dist_calc_points=grid_settings.dist_calc_points,
global_dist_calc_points=grid_settings.calculation_point_distance_1d,
embedded_cutoff_threshold=grid_settings.embedded_cutoff_threshold,
node_id_counter=node_id_counter,
embedded_node_id_counter=embedded_node_id_counter,
Expand All @@ -135,7 +135,7 @@ def _make_gridadmin(
objects=pipes,
fixed_nodes=None,
cell_tree=grid.cell_tree if grid_settings.use_2d else None,
global_dist_calc_points=grid_settings.dist_calc_points,
global_dist_calc_points=grid_settings.calculation_point_distance_1d,
embedded_cutoff_threshold=grid_settings.embedded_cutoff_threshold,
node_id_counter=node_id_counter,
embedded_node_id_counter=embedded_node_id_counter,
Expand All @@ -149,7 +149,7 @@ def _make_gridadmin(
objects=culverts,
fixed_nodes=None,
cell_tree=grid.cell_tree if grid_settings.use_2d else None,
global_dist_calc_points=grid_settings.dist_calc_points,
global_dist_calc_points=grid_settings.calculation_point_distance_1d,
embedded_cutoff_threshold=grid_settings.embedded_cutoff_threshold,
node_id_counter=node_id_counter,
embedded_node_id_counter=embedded_node_id_counter,
Expand Down
21 changes: 11 additions & 10 deletions threedigrid_builder/base/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class GridSettings:
use_1d_flow: bool
use_2d_flow: bool
use_0d_inflow: int
grid_space: float
dist_calc_points: float
kmax: int
minimum_cell_size: float
calculation_point_distance_1d: float
nr_grid_levels: int
embedded_cutoff_threshold: float = 0.05
max_angle_1d_advection: float = 0.4 * np.pi

Expand All @@ -56,10 +56,11 @@ class TablesSettings:
"""Settings necessary for threedi-tables."""

## from GlobalSettings
table_step_size: float
frict_coef: float
frict_coef_type: InitializationType
frict_type: FrictionType = FrictionType.MANNING
minimum_table_step_size: float
friction_coefficient: float
# TODO figure out how to fix this
# frict_coef_type: InitializationType
friction_type: FrictionType = FrictionType.MANNING
interception_global: Optional[float] = None
interception_type: Optional[InitializationType] = None
table_step_size_1d: float = None # actual default is set in __post_init__
Expand Down Expand Up @@ -117,9 +118,9 @@ class TablesSettings:
def __post_init__(self):
# defaults
if self.table_step_size_1d is None:
self.table_step_size_1d = self.table_step_size
self.table_step_size_1d = self.minimum_table_step_size
if self.maximum_table_step_size is None:
self.maximum_table_step_size = 100 * self.table_step_size
self.maximum_table_step_size = 100 * self.minimum_table_step_size

# validations
for field in (
Expand All @@ -129,7 +130,7 @@ def __post_init__(self):
):
greater_zero_check(self, field)

if self.maximum_table_step_size < self.table_step_size:
if self.maximum_table_step_size < self.minimum_table_step_size:
raise SchematisationError(
f"'maximum_table_step_size' must not be less than 'table_step_size'."
)
Expand Down
2 changes: 1 addition & 1 deletion threedigrid_builder/grid/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def from_quadtree(cls, quadtree, area_mask, node_id_counter, line_id_counter):
# Some general quadtree grid statistics we need in the .h5 later on.
quadtree_stats = QuadtreeStats(
lgrmin=quadtree.lgrmin,
kmax=quadtree.kmax,
kmax=quadtree.nr_grid_levels,
mmax=quadtree.mmax,
nmax=quadtree.nmax,
dx=quadtree.dx,
Expand Down
4 changes: 2 additions & 2 deletions threedigrid_builder/grid/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def get_lines(
zoom_category = np.take(objs.zoom_category, segment_idx)
connection_node_start_id = np.take(objs.connection_node_start_id, segment_idx)
connection_node_end_id = np.take(objs.connection_node_end_id, segment_idx)
dist_calc_points = np.take(objs.dist_calc_points, segment_idx)
dist_calc_points = np.take(objs.calculation_point_distance_1d, segment_idx)

# set the right node indices for each segment
first_idx, last_idx = counts_to_ranges(np.bincount(segments.linestring_idx))
Expand Down Expand Up @@ -247,7 +247,7 @@ def get_lines(

# conditionally add friction type and value (for pipes and culverts only)
try:
frict_type = objs.friction_type[segment_idx]
frict_type = objs.friction_coefficient[segment_idx]
frict_value = objs.friction_value[segment_idx]
except AttributeError:
frict_type = -9999
Expand Down
73 changes: 31 additions & 42 deletions threedigrid_builder/interface/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
# hardcoded source projection
SOURCE_EPSG = 4326

MIN_SQLITE_VERSION = 217
MIN_SQLITE_VERSION = 300

DAY_IN_SECONDS = 24.0 * 3600.0

Expand Down Expand Up @@ -145,60 +145,45 @@ def get_settings(self) -> dict:
"""

with self.get_session() as session:
global_ = session.query(models.GlobalSetting).order_by("id").first()
if global_.groundwater_settings_id is not None:
groundwater = _object_as_dict(
session.query(models.GroundWater)
.filter_by(id=global_.groundwater_settings_id)
.one()
)
model_settings = session.query(models.ModelSettings).order_by("id").first()
if model_settings.use_groundwater_flow or model_settings.use_groundwater_storage:
groundwater = _object_as_dict(session.query(models.GroundWater).one())
else:
groundwater = {}
if global_.interflow_settings_id is not None:
interflow = _object_as_dict(
session.query(models.Interflow)
.filter_by(id=global_.interflow_settings_id)
.one()
)
if model_settings.use_interflow:
interflow = _object_as_dict(session.query(models.Interflow).one())
else:
interflow = {}
if global_.simple_infiltration_settings_id is not None:
infiltration = _object_as_dict(
session.query(models.SimpleInfiltration)
.filter_by(id=global_.simple_infiltration_settings_id)
.one()
)
if model_settings.use_simple_infiltration:
infiltration = _object_as_dict(session.query(models.SimpleInfiltration).one())
# older sqlites have no max_infiltration_capacity field
infiltration.setdefault("max_infiltration_capacity", None)
else:
infiltration = {}
if global_.vegetation_drag_settings_id is not None:
vegetation_drag = _object_as_dict(
session.query(models.VegetationDrag)
.filter_by(id=global_.vegetation_drag_settings_id)
.one(),
)
if model_settings.use_vegetation_drag_2d:
vegetation_drag = _object_as_dict(session.query(models.VegetationDrag).one())
else:
vegetation_drag = {}
global_ = _object_as_dict(global_)
model_settings = _object_as_dict(model_settings)

# record if there is a DEM file to be expected
# Note: use_2d_flow only determines whether there are flow lines
global_["use_2d"] = bool(global_["dem_file"])
model_settings["use_2d"] = bool(model_settings["dem_file"])

# set/adapt initialization types to include information about file presence
NO_AGG = InitializationType.NO_AGG
AVERAGE = InitializationType.AVERAGE
_set_initialization_type(
global_, "frict_coef", default=AVERAGE if global_["frict_avg"] else NO_AGG
)
_set_initialization_type(
global_,
"interception_global",
file_field="interception_file",
type_field="interception_type",
default=NO_AGG,
model_settings, "friction_coefficient", default=AVERAGE if model_settings["friction_averaging"] else NO_AGG
)
# TODO: figure out what to do here; there could be no interception
# _set_initialization_type(
# model_settings,
# "interception_global",
# file_field="interception_file",
# type_field="interception_type",
# default=NO_AGG,
# )
if interflow:
_set_initialization_type(interflow, "porosity", default=NO_AGG)
_set_initialization_type(
Expand All @@ -207,7 +192,7 @@ def get_settings(self) -> dict:
if infiltration:
_set_initialization_type(infiltration, "infiltration_rate", default=NO_AGG)
_set_initialization_type(
infiltration, "max_infiltration_capacity", default=NO_AGG
infiltration, "max_infiltration_volume", default=NO_AGG
)

if groundwater:
Expand All @@ -217,7 +202,7 @@ def get_settings(self) -> dict:
_set_initialization_type(groundwater, "equilibrium_infiltration_rate")
_set_initialization_type(groundwater, "initial_infiltration_rate")
_set_initialization_type(groundwater, "infiltration_decay_period")
_set_initialization_type(groundwater, "groundwater_hydro_connectivity")
_set_initialization_type(groundwater, "groundwater_hydraulic_conductivity")

if vegetation_drag:
_set_initialization_type(
Expand All @@ -232,14 +217,18 @@ def get_settings(self) -> dict:
_set_initialization_type(
vegetation_drag, "vegetation_drag_coefficient", default=NO_AGG
)
# Copy Simulation Template Settings to model_settings dict
template_settings = _object_as_dict(session.query(models.SimulationTemplateSettings).order_by("id").first())
model_settings["name"] = template_settings["name"]
model_settings["use_0d_inflow"] = template_settings["use_0d_inflow"]

grid_settings = GridSettings.from_dict(global_)
grid_settings = GridSettings.from_dict(model_settings)
tables_settings = TablesSettings.from_dict(
{**groundwater, **interflow, **infiltration, **vegetation_drag, **global_}
{**groundwater, **interflow, **infiltration, **vegetation_drag, **model_settings}
)
return {
"epsg_code": global_["epsg_code"],
"model_name": global_["name"],
"epsg_code": model_settings["epsg_code"],
"model_name": model_settings["name"],
"grid_settings": grid_settings,
"tables_settings": tables_settings,
}
Expand Down
10 changes: 5 additions & 5 deletions threedigrid_builder/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ def grid_all():
use_1d_flow=True,
use_2d_flow=True,
use_0d_inflow=True,
grid_space=20.0,
dist_calc_points=25.0,
kmax=4,
minimum_cell_size=20.0,
calculation_point_distance_1d=25.0,
nr_grid_levels=4,
),
tables_settings=TablesSettings(
table_step_size=0.05,
frict_coef=0.03,
minimum_table_step_size=0.05,
friction_coefficient=0.03,
frict_coef_type=9,
),
)
Expand Down
12 changes: 6 additions & 6 deletions threedigrid_builder/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,19 @@ def test_get_settings(db):
# some settings copied over from SQLite:
assert g.use_1d_flow is True
assert g.use_2d_flow is True
assert g.grid_space == 20.0
assert g.dist_calc_points == 15.0
assert g.kmax == 4
assert g.minimum_cell_size == 20.0
assert g.calculation_point_distance_1d == 15.0
assert g.nr_grid_levels == 4
assert g.embedded_cutoff_threshold == 0.05
assert g.max_angle_1d_advection == 0.4 * np.pi
# use_2d is based on the presence of dem_file:
assert g.use_2d is True

s = result["tables_settings"]
assert isinstance(s, TablesSettings)
assert s.table_step_size == 0.05
assert s.frict_type == 2
assert s.frict_coef == 0.03
assert s.minimum_table_step_size == 0.05
assert s.friction_type == 2
assert s.friction_coefficient == 0.03
assert s.frict_coef_type == InitializationType.GLOBAL
assert s.interception_global == 100.0
assert s.interception_type == InitializationType.NO_AGG
Expand Down
10 changes: 5 additions & 5 deletions threedigrid_builder/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ def meta():
use_1d_flow=True,
use_2d_flow=True,
use_0d_inflow=0,
grid_space=20.0,
dist_calc_points=25.0,
kmax=4,
minimum_cell_size=20.0,
calculation_point_distance_1d=25.0,
nr_grid_levels=4,
),
tables_settings=TablesSettings(
table_step_size=0.05,
frict_coef=0.03,
minimum_table_step_size=0.05,
friction_coefficient=0.03,
frict_coef_type=9,
),
)
Expand Down
6 changes: 3 additions & 3 deletions threedigrid_builder/tests/test_quadtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def quadtree_poly_refinement(subgrid_meta):


def test_quadtree_no_refinement(quadtree_no_refinement):
assert quadtree_no_refinement.kmax == 1
assert quadtree_no_refinement.nr_grid_levels == 1
assert np.size(quadtree_no_refinement.mmax) == 1
assert quadtree_no_refinement.mmax[0] == 3
assert quadtree_no_refinement.nmax[0] == 2
Expand All @@ -107,7 +107,7 @@ def test_quadtree_no_even_pixels(subgrid_meta):


def test_quadtree_line_refinement(quadtree_line_refinement):
assert quadtree_line_refinement.kmax == 3
assert quadtree_line_refinement.nr_grid_levels == 3
assert np.size(quadtree_line_refinement.mmax) == 3
assert quadtree_line_refinement.mmax[2] == 3
assert quadtree_line_refinement.nmax[2] == 2
Expand All @@ -129,7 +129,7 @@ def test_quadtree_line_refinement(quadtree_line_refinement):


def test_quadtree_poly_refinement(quadtree_poly_refinement):
assert quadtree_poly_refinement.kmax == 2
assert quadtree_poly_refinement.nr_grid_levels == 2
assert np.size(quadtree_poly_refinement.mmax) == 2
assert quadtree_poly_refinement.mmax[1] == 3
assert quadtree_poly_refinement.nmax[1] == 2
Expand Down

0 comments on commit af51ee6

Please sign in to comment.