Cross section files¶
The crosssection module provides the specific logic for accessing cross section files (location and definition) for a D-Flow FM model. Generic parsing and serializing functionality comes from the generic hydrolib.core.dflowfm.ini modules.
The cross section files are represented by the classes below.
Model¶
CircleCrsDef (CrossSectionDefinition)
pydantic-model
¶
Crosssection definition with type=circle, to be included in a crossdef file.
Typically inside the definition list of a FMModel.geometry.crossdeffile.definition[..]
All lowercased attributes match with the circle input as described in UM Sec.C.16.1.1.
CrossDefGeneral (INIGeneral)
pydantic-model
¶
The crosssection definition file's [General] section with file meta data.
CrossDefModel (INIModel)
pydantic-model
¶
The overall crosssection definition model that contains the contents of one crossdef file.
This model is typically referenced under a FMModel.geometry.crossdeffile.
Attributes:
| Name | Type | Description |
|---|---|---|
general |
CrossdefGeneral |
|
definition |
List[CrossSectionDefinition] |
List of |
CrossLocGeneral (INIGeneral)
pydantic-model
¶
The crosssection location file's [General] section with file meta data.
CrossLocModel (INIModel)
pydantic-model
¶
The overall crosssection location model that contains the contents of one crossloc file.
This model is typically referenced under a FMModel.geometry.crosslocfile.
Attributes:
| Name | Type | Description |
|---|---|---|
general |
CrossLocGeneral |
|
crosssection |
List[CrossSection] |
List of |
CrossSection (INIBasedModel)
pydantic-model
¶
A [CrossSection] block for use inside a crosssection location file,
i.e., a CrossLocModel.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str |
Unique cross-section location id. |
branchid |
str |
Branch on which the cross section is located. |
chainage |
str |
Chainage on the branch (m). |
x |
str |
x-coordinate of the location of the cross section. |
y |
str |
y-coordinate of the location of the cross section. |
shift |
float |
Vertical shift of the cross section definition [m]. Defined positive upwards. |
definitionid |
str |
Id of cross section definition. |
validate_that_location_specification_is_correct(values: Dict) -> Dict
classmethod
¶
Validates that the correct location specification is given.
Source code in hydrolib/core/dflowfm/crosssection/models.py
@root_validator(allow_reuse=True)
def validate_that_location_specification_is_correct(cls, values: Dict) -> Dict:
"""Validates that the correct location specification is given."""
return validate_location_specification(
values,
config=LocationValidationConfiguration(
validate_node=False, validate_num_coordinates=False
),
fields=LocationValidationFieldNames(x_coordinates="x", y_coordinates="y"),
)
CrossSectionDefinition (INIBasedModel)
pydantic-model
¶
A [Definition] block for use inside a crosssection definition file,
i.e., a CrossDefModel.
This class is intended as an abstract class: various subclasses should define they actual types of crosssection definitions.
validate(v)
classmethod
¶
Try to initialize subclass based on the type field.
This field is compared to each type field of the derived models of CrossSectionDefinition.
The derived model with an equal crosssection definition type will be initialized.
Exceptions:
| Type | Description |
|---|---|
ValueError |
When the given type is not a known crosssection definition type. |
Source code in hydrolib/core/dflowfm/crosssection/models.py
@classmethod
def validate(cls, v):
"""Try to initialize subclass based on the `type` field.
This field is compared to each `type` field of the derived models of `CrossSectionDefinition`.
The derived model with an equal crosssection definition type will be initialized.
Raises:
ValueError: When the given type is not a known crosssection definition type.
"""
# should be replaced by discriminated unions once merged
# https://github.com/samuelcolvin/pydantic/pull/2336
if isinstance(v, dict):
for c in cls.__subclasses__():
if (
c.__fields__.get("type").default.lower()
== v.get("type", "").lower()
):
v = c(**v)
break
else:
raise ValueError(
f"Type of {cls.__name__} with id={v.get('id', '')} and type={v.get('type', '')} is not recognized."
)
return super().validate(v)
RectangleCrsDef (CrossSectionDefinition)
pydantic-model
¶
Crosssection definition with type=rectangle, to be included in a crossdef file.
Typically inside the definition list of a FMModel.geometry.crossdeffile.definition[..]
All lowercased attributes match with the rectangle input as described in UM Sec.C.16.1.2.
XYZCrsDef (YZCrsDef, CrossSectionDefinition)
pydantic-model
¶
Crosssection definition with type=xyz, to be included in a crossdef file.
Typically inside the definition list of a FMModel.geometry.crossdeffile.definition[..]
All lowercased attributes match with the xyz input as described in UM Sec.C.16.1.5.
This class extends the YZCrsDef class with x-coordinates and an optional branchId field. Most other attributes are inherited, but the coordcount is overridden under the Pydantic alias "xyzCount".
Attributes:
| Name | Type | Description |
|---|---|---|
yzcount |
Optional[int] |
dummy attribute that should not be set nor used. Only present to mask the inherited attribute from parent class YZCrsDef. |
xyzcount |
int |
Number of XYZ-coordinates. Always use this instead of yzcount. |
check_list_lengths_coordinates(values)
classmethod
¶
Validates that the length of the xcoordinates, ycoordinates and zcoordinates field are as expected.
Source code in hydrolib/core/dflowfm/crosssection/models.py
@root_validator(allow_reuse=True)
def check_list_lengths_coordinates(cls, values):
"""Validates that the length of the xcoordinates, ycoordinates and zcoordinates field are as expected."""
return validate_correct_length(
values,
"xcoordinates",
"ycoordinates",
"zcoordinates",
length_name="xyzcount",
)
validate_xyzcount_without_yzcount(field_value: int, values: dict) -> int
classmethod
¶
Validates whether this XYZCrsDef does have attribute xyzcount, but not the parent class's yzcount.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_value |
Optional[Path] |
Value given for xyzcount. |
required |
values |
dict |
Dictionary of values already validated. |
required |
Exceptions:
| Type | Description |
|---|---|
ValueError |
When yzcount is present. |
Returns:
| Type | Description |
|---|---|
int |
The value given for xyzcount. |
Source code in hydrolib/core/dflowfm/crosssection/models.py
@validator("xyzcount")
@classmethod
def validate_xyzcount_without_yzcount(cls, field_value: int, values: dict) -> int:
"""
Validates whether this XYZCrsDef does have attribute xyzcount,
but not the parent class's yzcount.
Args:
field_value (Optional[Path]): Value given for xyzcount.
values (dict): Dictionary of values already validated.
Raises:
ValueError: When yzcount is present.
Returns:
int: The value given for xyzcount.
"""
# Retrieve the algorithm value (if not found use 0).
yzcount_value = values.get("yzcount")
if field_value is not None and yzcount_value is not None:
# yzcount should not be set, when xyzcount is set.
raise ValueError(
f"xyz cross section definition should not contain field yzCount (rather: xyzCount), current value: {yzcount_value}."
)
return field_value
YZCrsDef (CrossSectionDefinition)
pydantic-model
¶
Crosssection definition with type=yz, to be included in a crossdef file.
Typically inside the definition list of a FMModel.geometry.crossdeffile.definition[..]
All lowercased attributes match with the yz input as described in UM Sec.C.16.1.6.
check_list_length_frictionpositions(values)
classmethod
¶
Validates that the length of the frictionpositions field is as expected.
Source code in hydrolib/core/dflowfm/crosssection/models.py
@root_validator(allow_reuse=True)
def check_list_length_frictionpositions(cls, values):
"""Validates that the length of the frictionpositions field is as expected."""
return validate_correct_length(
values,
"frictionpositions",
length_name="sectioncount",
length_incr=1, # 1 extra for frictionpositions
)
check_list_lengths_coordinates(values)
classmethod
¶
Validates that the length of the ycoordinates and zcoordinates fields are as expected.
Source code in hydrolib/core/dflowfm/crosssection/models.py
@root_validator(allow_reuse=True)
def check_list_lengths_coordinates(cls, values):
"""Validates that the length of the ycoordinates and zcoordinates fields are as expected."""
return validate_correct_length(
values,
"ycoordinates",
"zcoordinates",
length_name="yzcount",
)
check_list_lengths_friction(values)
classmethod
¶
Validates that the length of the frictionids, frictiontypes and frictionvalues field are as expected.
Source code in hydrolib/core/dflowfm/crosssection/models.py
@root_validator(allow_reuse=True)
def check_list_lengths_friction(cls, values):
"""Validates that the length of the frictionids, frictiontypes and frictionvalues field are as expected."""
return validate_correct_length(
values,
"frictionids",
"frictiontypes",
"frictionvalues",
length_name="sectioncount",
)
ZWCrsDef (CrossSectionDefinition)
pydantic-model
¶
Crosssection definition with type=zw, to be included in a crossdef file.
Typically inside the definition list of a FMModel.geometry.crossdeffile.definition[..]
All lowercased attributes match with the zw input as described in UM Sec.C.16.1.4.
check_list_lengths(values)
classmethod
¶
Validates that the length of the levels, flowwidths and totalwidths fields are as expected.
Source code in hydrolib/core/dflowfm/crosssection/models.py
@root_validator(allow_reuse=True)
def check_list_lengths(cls, values):
"""Validates that the length of the levels, flowwidths and totalwidths fields are as expected."""
return validate_correct_length(
values,
"levels",
"flowwidths",
"totalwidths",
length_name="numlevels",
)
ZWRiverCrsDef (CrossSectionDefinition)
pydantic-model
¶
Crosssection definition with type=zwRiver, to be included in a crossdef file.
Typically inside the definition list of a FMModel.geometry.crossdeffile.definition[..]
All lowercased attributes match with the zwRiver input as described in UM Sec.C.16.1.3.
check_list_lengths(values)
classmethod
¶
Validates that the length of the levels, flowwidths and totalwidths fields are as expected.
Source code in hydrolib/core/dflowfm/crosssection/models.py
@root_validator(allow_reuse=True)
def check_list_lengths(cls, values):
"""Validates that the length of the levels, flowwidths and totalwidths fields are as expected."""
return validate_correct_length(
values,
"levels",
"flowwidths",
"totalwidths",
length_name="numlevels",
)