Skip to content

Commit cb59bed

Browse files
committed
Add check for inflow boundary
check trracers boundary for Inflow/birdirectional boundary
1 parent ccd4e7e commit cb59bed

1 file changed

Lines changed: 71 additions & 19 deletions

File tree

schimpy/bctide.py

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,33 @@ def _handle_tidal_constituents_val(self,vals,consts_name,const_type_str,boundary
516516
tidal constituent {consts_name} at boundary {boundary_id} \n")
517517
return val
518518

519+
def check_inflow_boundary_spec(self,var_dic, bound_name,tracer_name):
520+
521+
if self.mode == "barotropic":
522+
if not self.computation_temperature_salinity:
523+
if var_dic is not None:
524+
raise ValueError(
525+
f"Boundary '{bound_name}' has specified '{tracer_name}' boundary conditions, but 'temperature_salinity_computation' \
526+
is set to false in barotropic mode. Please remove '{tracer_name}' specification or set 'temperature_salinity_computation' \
527+
to true."
528+
)
529+
else:
530+
if var_dic is None and (tracer_name in ["temperature", "salinity"]):
531+
raise ValueError(
532+
f"Boundary '{bound_name}' is missing required '{tracer_name}' specification for inflow/bidirectional boundary in \
533+
barotropic mode with 'temperature_salinity_computation' enabled. Please specify '{tracer_name}' boundary conditions."
534+
)
535+
else:
536+
if var_dic is not None and not (tracer_name in ["temperature", "salinity"]):
537+
raise ValueError(
538+
f"Boundary '{bound_name}' has specified '{tracer_name}' boundary conditions in barotropic mode. \
539+
Please remove '{tracer_name}' specification" )
540+
541+
else: # baroclinic mode,
542+
if var_dic is None:
543+
raise ValueError(
544+
f"boundary '{bound_name}' is missing variable specification. Please specify {tracer_name} for inflow/bidirectional boundary."
545+
)
519546

520547
def write_bctides(self, bctides_file):
521548

@@ -708,26 +735,12 @@ def write_bctides(self, bctides_file):
708735
## this boundary shouldn't have any tracer boundary, such
709736
## as temperature, salinity or other scalars
710737

711-
if "flow_direction" in self.open_boundaries[i].keys():
712-
flow_direction = self.open_boundaries[i]["flow_direction"]
713-
if "flow_direction" == "outflow":
714-
if "temperature" in self.open_boundaries[i].keys():
715-
raise ValueError(
716-
f"Boundary '{bound_name}' is configured as an outflow; a temperature boundary "
717-
"specification is not allowed."
718-
)
719-
if "salinity" in self.open_boundaries[i].keys():
720-
raise ValueError(
721-
f"Boundary '{bound_name}' is configured as an outflow; a salinity boundary "
722-
"specification is not allowed."
723-
)
724-
if "tracers" in self.open_boundaries[i].keys():
725-
raise ValueError(
726-
f"Boundary '{bound_name}' is configured as an outflow; tracer "
727-
"specification is not allowed."
728-
)
729-
730738

739+
## flow_direction is must for open boundary, if not given, raise error
740+
if not("flow_direction" in self.open_boundaries[i].keys()):
741+
raise ValueError(
742+
f"Boundary '{bound_name}' is missing required 'flow_direction' specification. Please specify 'flow_direction' as either 'inflow' or 'outflow'."
743+
)
731744

732745
# variables in YAML are provided as a list of dicts; helper to find a variable entry
733746
vars_list = self.open_boundaries[i].get("variables", [])
@@ -736,6 +749,45 @@ def _get_var(name):
736749
if isinstance(vv, dict) and name in vv:
737750
return vv[name]
738751
return None
752+
753+
flow_direction = self.open_boundaries[i]["flow_direction"]
754+
if flow_direction == "outflow":
755+
if "temperature" in self.open_boundaries[i].keys():
756+
raise ValueError(
757+
f"Boundary '{bound_name}' is configured as an outflow; a temperature boundary "
758+
"specification is not allowed."
759+
)
760+
if "salinity" in self.open_boundaries[i].keys():
761+
raise ValueError(
762+
f"Boundary '{bound_name}' is configured as an outflow; a salinity boundary "
763+
"specification is not allowed."
764+
)
765+
if "tracers" in self.open_boundaries[i].keys():
766+
raise ValueError(
767+
f"Boundary '{bound_name}' is configured as an outflow; tracer "
768+
"specification is not allowed."
769+
)
770+
elif flow_direction == "inflow" or flow_direction == "bidirection":
771+
772+
check_list = ["temperature", "salinity","tracers"]
773+
if len(self.tracer_modules) == 0: ## if no tracer module is defined, then tracers should not be specified in the boundary
774+
check_list = ["temperature", "salinity"]
775+
## if tracers is specified in the boundary but no tracer module is defined, raise error
776+
if "tracers" in self.open_boundaries[i].keys():
777+
raise ValueError(
778+
f"Boundary '{bound_name}' has specified 'tracers' boundary conditions, but no tracer modules are defined in the bctides YAML. \
779+
Please add tracer modules to the top-level 'modules' section or remove 'tracers' specification from the boundary."
780+
)
781+
for var in check_list:
782+
self.check_inflow_boundary_spec(_get_var(var), bound_name,var)
783+
else:
784+
raise ValueError(
785+
f"Boundary '{bound_name}' has invalid 'flow_direction' value: {flow_direction}. "
786+
"Expected 'inflow', 'outflow', or 'bidirection'."
787+
)
788+
789+
790+
739791

740792
if _get_var("elevation") is not None:
741793
elev_boundary = _get_var("elevation")

0 commit comments

Comments
 (0)