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

Run allocation first after BMI stop #1390

Merged
merged 6 commits into from
Apr 16, 2024
Merged

Run allocation first after BMI stop #1390

merged 6 commits into from
Apr 16, 2024

Conversation

visr
Copy link
Member

@visr visr commented Apr 15, 2024

This allows allocation over period (t, t + dt) to use variables set over BMI at time t.

We no longer run allocation as a callback, but call it ourselves, such that we can control that BMI runs allocation first before running the physical layer.

@visr visr marked this pull request as draft April 15, 2024 21:15
@visr
Copy link
Member Author

visr commented Apr 15, 2024

Some pretty large allocation test differences suggest we don't call allocation correctly yet.

No Captured Logs for test item "Allocation level control" at test\allocation_test.jl:308 on worker 26268
Error in testset "Allocation level control" on worker 26268:
Test Failed at D:\Ribasim\core\test\allocation_test.jl:334
  Expression: ≈(storage[stage_1], u_stage_1.(t[stage_1]), rtol = 0.0001)
   Evaluated: [500.0, 672.7963463605041] ≈ [500.0, 543.2] (rtol=0.0001)

@SouthEndMusic
Copy link
Collaborator

SouthEndMusic commented Apr 16, 2024

This might have to do with the fact that allocation uses the flows from the last water_balance! call, which depends (maybe among other things) on the used solver and might not correspond to the right state of the Model. To make this consistent, maybe always do a water_balance! call before allocation, which also would help with performing allocation at t=0 (#1389).
Alternatively, we should first pick up the issue about using average flows and levels as input for allocation (#1121, but this issue does not yet take basin levels into account).

@visr visr marked this pull request as ready for review April 16, 2024 08:57
@visr visr requested a review from SouthEndMusic April 16, 2024 08:57
@visr
Copy link
Member Author

visr commented Apr 16, 2024

It actually ran allocation on the wrong times, that is fixed now.

Copy link
Collaborator

@SouthEndMusic SouthEndMusic left a comment

Choose a reason for hiding this comment

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

Looks good, left some small comments

@@ -146,6 +146,17 @@ function sorted_table!(
return table
end

function valid_config(config::Config)::Bool
Copy link
Collaborator

Choose a reason for hiding this comment

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

Now that you have this, can it be removed below?

t_end = seconds_since(config.endtime, config.starttime)
if t_end <= 0
error("Model starttime is not before endtime.")
end

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, I forgot, thanks.

# set over BMI at time t before calling this function.
# Also, don't run allocation at t = 0 since there are no flows yet (#1389).
ntimes = t / config.allocation.timestep
if ntimes > 0 && round(ntimes) ≈ ntimes
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd say the more common way to do this is

if t % config.allocation.timestep  0.0
   ... 
end

and the other check could just be t > 0 if you actually need that.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Although the comparison tolerances work out differently

Copy link
Member Author

Choose a reason for hiding this comment

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

That would only work if the t is right after, but not right before, right?

julia> prevfloat(3.0) % 0.5
0.49999999999999956

@SouthEndMusic
Copy link
Collaborator

Oops

lol

@visr visr merged commit 184827a into main Apr 16, 2024
24 checks passed
@visr visr deleted the ribami branch April 16, 2024 12:06
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.

2 participants