Build a basic model¶
This Build a basic model tutorial illustrates how to build a simple D-Flow FM model from scratch using HYDROLIB-core.
# Import HYDROLIB-core functionalities and setup D-Flow FM model
from hydrolib.core.io.structure.models import FlowDirection, StructureModel, Weir
from hydrolib.core.io.mdu.models import FMModel
fm = FMModel()
fm.filepath = "test.mdu"
Add a hydraulic structure; note that this is invalid because the model does not have a 1D network with branches and coordinates yet, but it will work for demo purposes and the network may be added later.
# Add weir to the structure set/model and add this structure set to D-Flow FM model
struc = Weir(branchId='someBranch', chainage = 123.0, allowedflowdir=FlowDirection.none, crestlevel=0.0)
struc.comments.crestlevel = "This is a comment"
fm.geometry.structurefile = [StructureModel(structure=[struc])]
Note that the creation of a Weir
and other model objects requires several input arguments.
A ValidationError
will be raised when the model is invalid or incomplete.
For instance, in the above example, if the StructureModel
had been assigned directly to structurefile
instead of as a list, that would have triggered a ValidationError
.
Now let's add this model to a DIMR config and save it.
from hydrolib.core.io.dimr.models import DIMR, FMComponent
from pathlib import Path
dimr = DIMR()
dimr.component.append(
FMComponent(name="test", workingDir=".", inputfile=fm.filepath, model=fm)
)
dimr.save(recurse=True)
The save call on the highest-level DIMR object will result in recursive saves of all child models in the model hierarchy, so this results in four files (dimr_config.xml
, network.nc
, structures.ini
,test.mdu
) in the working directory.
Some more in-depth background about recursive saving of a model tree is given in another tutorial: Loading and saving.