-
Notifications
You must be signed in to change notification settings - Fork 30
Use SI units for internal computations #735
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces SI units for internal computations within the Wflow model to improve code clarity and consistency. The main changes include:
- Introduction of a custom
Unitsystem to handle unit conversions, including model-specific timestep units - Conversion of input data to SI units at read time and conversion back for output
- Addition of unit annotations throughout computation functions
- Reorganization of standard name mappings into separate files by model component
- Replacement of implicit unit conversions with explicit ones
Reviewed changes
Copilot reviewed 64 out of 65 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| docs/model_docs/routing/sediment_flux.qmd | Fixed unit typos in documentation (ton/m³ and formula correction) |
| Wflow/test/*.jl | Updated tests to use unit conversion functions for consistency |
| Wflow/src/units.jl | New file defining the Unit system and conversion functions |
| Wflow/src/standard_name/*.jl | Split standard name mappings into component-specific files with unit information |
| Wflow/src/vegetation/*.jl | Added unit annotations and timestep parameter to functions |
| Wflow/src/soil/*.jl | Updated soil model functions with explicit unit handling |
| Wflow/src/snow/*.jl | Updated snow model with unit conversions |
| Wflow/src/sediment/*.jl | Updated sediment transport functions with SI unit conversions |
| Wflow/src/utils.jl | Added helper functions, removed BASETIMESTEP constant |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| powers = ntuple( | ||
| i -> begin | ||
| unit = Units[i] | ||
| powers = return if unit in keys(kwargs) |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return keyword is unnecessary here. The result of the if expression will be automatically returned from the block.
| powers = return if unit in keys(kwargs) | |
| powers = if unit in keys(kwargs) |
Wflow/src/sediment/sediment_transport/transport_capacity_process.jl
Outdated
Show resolved
Hide resolved
Wflow/src/sediment/sediment_transport/transport_capacity_process.jl
Outdated
Show resolved
Hide resolved
| (; sediment_rate) = transport_capacity_model.variables | ||
| @. transport_capacity = sediment_rate |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable naming inconsistency: transport_capacity_model.variables contains sediment_rate, but it should likely be sediment_transport_capacity based on the struct definition in transport_capacity.jl. This creates confusion about whether it's a rate or capacity being referenced.
| (; sediment_rate) = transport_capacity_model.variables | |
| @. transport_capacity = sediment_rate | |
| (; sediment_transport_capacity) = transport_capacity_model.variables | |
| @. transport_capacity = sediment_transport_capacity |
Issue addressed
Fixes #326
Explanation
My apologies for the bloated PR. Here's an overview of what I did.
Units
units.jlis a new script which describes unit behavior. It defines theUnitobject, and as well as:UnitobjectSIunitsBMIstandardstandard_name.jl, which is now subdivided into 4 files as the amount of data in this file was getting quite large:standard_name_sbm.jlstandard_name_domain.jlstandard_name_routing.jlstandard_name_sediment.jlget_atset_states!ncreadrainfall_erosion_eurosem).write_csv_rowwrite_netcdf_timestepBMI.get_var_unitsnow returns SI units, as the internal arrays which are accessed through the BMI are expressed in SI units*_avvectors (although not all of them have this suffix) in place by accumulatingq * dt_subcontributions and then finally dividing by the model time step. I didn't like this because this way those vectors do not have a consistent unit. Therefore I introduced theAverageVectorstruct, which has separate vectors for the cumulative and average values.The timestep unit made this PR quite tricky. That is because in many places rates (something per timestep) was implicitly converted to amounts (something), which was possible because the conversion factor is then 1, but it makes for hard to understand code unit-wise.
Others
@infomessages for initialized data withprettytables.jl. See the functionto_table. Here's an example:n. These can easily be replaced by the constructors generated by@with_kw(split out in Constructor cleanup #781)amountin the sediment module into more descriptive ones (soil_erosion_rate,sediment_rate,sediment_transport_capacity) (split out in Sediment code cleanup #786)InputEntryfor IO convenience, but I ultimately decided against it. During this process I did come up with a simplified interface to modify to config:storfuncandoutflowfuncintegersFinal notes
Checklist
master