Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor types and model initialization #517

Merged
merged 22 commits into from
Jan 14, 2025
Merged

Refactor types and model initialization #517

merged 22 commits into from
Jan 14, 2025

Conversation

SouthEndMusic
Copy link
Contributor

@SouthEndMusic SouthEndMusic commented Dec 9, 2024

Explanation

When starting to look into water balance calculations for Wflow, I came across some aspects of the code that I think can be improved:

  • Cleaning up dispatching on model type via AbstractModel
  • Explicitly write out struct(s) for Model.Network instead of using a nested NamedTuple
  • Add inline documentation to network.jl
  • Explicitly write out struct(s) for Model.Lateral
  • Also structs needed for Model.vertical?
  • Clean up initialize_sbm_gwf_model
  • Clean up initialize_sbm_model
  • Clean up initialize_sediment_model
  • Update documentation where required

@vers-w
Copy link
Collaborator

vers-w commented Dec 9, 2024

Nice, some of these improvements (e.g. write out structs for Network and Lateral) are part of remaining refactoring work for v1.0.!

About the naming I think it would be good to replace the terms lateral and vertical with routing and land_surface (or a similar term), respectively. Both terms are quite common for land surface models (LSM), hydrological models, sediment models etc., used in scientific literature, and we also already use the term routing quite a lot in the code and file/dir names.

What are your thoughts on this @SouthEndMusic and @JoostBuitink ?

@SouthEndMusic SouthEndMusic marked this pull request as draft December 10, 2024 07:28
@SouthEndMusic
Copy link
Contributor Author

I don't have opinions yet about hydrological jargon 😋

@SouthEndMusic
Copy link
Contributor Author

@vers-w I'm done refactoring for this PR :) The structs I introduced also need some inline documentation, but I'm not the right person to do that. I leave it up to you whether you want to do this within this PR or in a follow up.

I see there is a lot of duplicate code in the model initialization code, but I don't understand it well enough at the moment to split it op into sensible sub functions.

@SouthEndMusic SouthEndMusic requested a review from vers-w December 10, 2024 15:30
@JoostBuitink
Copy link
Contributor

Nice, some of these improvements (e.g. write out structs for Network and Lateral) are part of remaining refactoring work for v1.0.!

About the naming I think it would be good to replace the terms lateral and vertical with routing and land_surface (or a similar term), respectively. Both terms are quite common for land surface models (LSM), hydrological models, sediment models etc., used in scientific literature, and we also already use the term routing quite a lot in the code and file/dir names.

What are your thoughts on this @SouthEndMusic and @JoostBuitink ?

I agree that replacing the terms lateral and vertical is a good idea. I'll think a bit more about it, but agree that routing and land_surface are good options!

@vers-w
Copy link
Collaborator

vers-w commented Dec 11, 2024

I agree that replacing the terms lateral and vertical is a good idea. I'll think a bit more about it, but agree that routing and land_surface are good options!

Yes, I think replacing the term lateral by routing is a good idea. Not completely sure about the use of land_surface. I think the term land is better (that then for example refers to an AbstractLandModel). With the term land_surface it seems (to me) more about surface processes (e.g. LSMs) and for the CSDMS standard names surface is used to point to processes near the land surface (e.g. infiltration) or on the land (e.g. land_surface_water (water above the land surface)).

@JoostBuitink
Copy link
Contributor

I agree that replacing the terms lateral and vertical is a good idea. I'll think a bit more about it, but agree that routing and land_surface are good options!

Yes, I think replacing the term lateral by routing is a good idea. Not completely sure about the use of land_surface. I think the term land is better (that then for example refers to an AbstractLandModel). With the term land_surface it seems (to me) more about surface processes (e.g. LSMs) and for the CSDMS standard names surface is used to point to processes near the land surface (e.g. infiltration) or on the land (e.g. land_surface_water (water above the land surface)).

I also agree that land_surface doesn't fully "feel" right, hence why I wanted to think a bit more about it. Currently we call the SBM concept land_hydrology (see line below), so perhaps land would indeed be a better name. If I get any other idea's, I'll post them here!

land_hydrology = LandHydrologySBM(dataset, config, river_fraction, indices)

I fully agree that routing is a logical option!

@vers-w
Copy link
Collaborator

vers-w commented Dec 11, 2024

@vers-w I'm done refactoring for this PR :) The structs I introduced also need some inline documentation, but I'm not the right person to do that. I leave it up to you whether you want to do this within this PR or in a follow up.

Yes, I can do that in this PR, that's fine. When we have decided what terms to use for vertical and lateral, I can change these and add some inline documentation in one go.

I see there is a lot of duplicate code in the model initialization code, but I don't understand it well enough at the moment to split it op into sensible sub functions.

Yes, definitely true and also something we would like to improve for v1.0. Did not look into detail at this yet. Probably good to check if we can use structs here that we can set partly (I see that you added Accessors as a dependency, so that should be easier/possible now?) and also pass around in the different functions (mostly the routing options have a lot of args). I would say this is for another PR?

@SouthEndMusic
Copy link
Contributor Author

Yes, in this PR I also make quite heavy use of partly setting structs. With @kwdef I add trivial default values which are set if no data is supplied. Accessors can then be used to set a new value (it makes a new immutable struct from the old one with the new value for a particular field).

'land` refers to `AbstractLandModel`.
- For `Routing` struct use more explicit names (`river_flow`, `overland_flow` and `subsurface_flow`), also easier to distinguish from `land` (vertical) component.
- Changed mapping of groundwater flow model (boundaries).
Align with `sbm` and `sbm_gwf` model types. `frac_to_river` is not used
by the `sediment` model type and removed from the `network`
initialization.
Additionally, replace `nothing` defaults of `Routing` by dummy types and move `Routing` code to separate file `routing/routing.jl`.
vers-w added 2 commits January 8, 2025 11:01
The name `Indices` is too general. Improved inline documentation of this struct.
`Model` field `vertical` has been replaced by `land`.
Copy link
Collaborator

@vers-w vers-w left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refactoring LGTM. I did add the inline documentation to the introduced network structs, renamed vertical to land and lateral to routing of the Model struct, and made some small improvements.

Some general comments:

  • The introduced network structs have some common fields. I think it could be useful to restructure these a bit, this could be part of another PR. For example, for reservoirs and lakes I did introduce a NetworkWaterBody struct.
  • You already mentioned that the model initialization functions have quite some duplicate code. As suggested we could also do this in another PR, including the initialization of the network structs (e.g. move the initialization to network.jl).
  • The documentation is not updated (except for the Model struct). We could do this in another PR, probably when the work on using standard names is finished.

@vers-w
Copy link
Collaborator

vers-w commented Jan 8, 2025

I also agree that land_surface doesn't fully "feel" right, hence why I wanted to think a bit more about it. Currently we call the SBM concept land_hydrology (see line below), so perhaps land would indeed be a better name. If I get any other idea's, I'll post them here!

Hi @JoostBuitink , I did rename vertical to land and included the abstract type AbstractLandModel. If you have any other ideas, let us know!

@SouthEndMusic SouthEndMusic marked this pull request as ready for review January 13, 2025 10:22
@vers-w vers-w merged commit 5e1f7f6 into master Jan 14, 2025
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants