Ribasim icon indicating copy to clipboard operation
Ribasim copied to clipboard

Refactor allocation code

Open SouthEndMusic opened this issue 1 year ago • 1 comments

These refactoring points came out of the knowledge sharing session (which was more of a code review session):

  • [x] Use function is_main_network here: https://github.com/Deltares/Ribasim/blob/61b3a173155647b78735a73357908cf8c48ed9ce/core/src/allocation_optim.jl#L1035
  • [ ] Introduce a separate allocation_util.jl file with allocation utility functions. This might need some discussion though, I'm not sure what counts as a utility function
  • [ ] The function allocate!: Dispatch on optimization_type, so several if statements can be removed
  • [ ] The functions allocate! and allocate_priority! need to be renamed, maybe simply to optimize! and optimize_priority!, as they are not always allocating, depending on the optimization_type
  • [ ] For the functions in allocate_priority
  • We now only have the AbstractParameterNode type for nodes. We can add a more specific abstract node type in between, say AbstractDemandNode, for nodes that define a demand.
  • For the adjust_demands_* functions, loop over the AbstractDemandNode subtypes and dispatch on them. Alternatively, for all the adjust_* functions, dispatch on the constraint set names.

SouthEndMusic avatar May 14 '24 10:05 SouthEndMusic

Hi @SouthEndMusic You now have one allocate function where you have multiple times and iff statement to see if you need to collect_demands. In callback.jl (update_allocation) you call allocate multiple times, sometimes to collect_demands, sometimes to allocate_demands. I would recommend to turn the current allocate function in two functions, one called collect_demands and one called allocate of allocate_demands [ ] split allocate into collect_demands and allocate_demands function

gijsber avatar May 14 '24 11:05 gijsber

Steps 1, 3, 4, 5 are finished.

As for step 2, the functions in allocation_optim.jl are utility functions. If we create a new file for utility functions, based on the definition of utility functions in programming, we might move everything from allocation_optim.jl.

I understand that we wanted to split allocation_optim.jl into multiple files, maybe we can come up a different categorization for the functions in there

Jingru923 avatar May 31 '24 12:05 Jingru923

Hi @Jingru923 , did you also split the current allocate function in two functions, one called collect_demands and one called allocate of allocate_demands ?

gijsber avatar May 31 '24 14:05 gijsber

Yes @gijsber

Jingru923 avatar Jun 03 '24 09:06 Jingru923