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

Fix BMI.update_until (InexactError: Int64) #387

Merged
merged 8 commits into from
Apr 17, 2024
Merged

Conversation

verseve
Copy link
Contributor

@verseve verseve commented Apr 5, 2024

Issue addressed

Fixes #386

Explanation

BMI.update_until could throw an InexactError: Int64 caused by a not whole number. This is fixed by using divrem for the computation of the number of steps in BMI.update_until, an error is thrown when the absolute remainder is larger than eps(). An error is also thrown when steps is a negative number.

Checklist

  • Updated tests or added new tests
  • Branch is up to date with master
  • Tests & pre-commit hooks pass
  • Updated documentation if needed
  • Updated changelog.md if needed

@verseve verseve requested a review from CFBaptista April 5, 2024 07:34
@verseve verseve marked this pull request as ready for review April 5, 2024 07:34
Copy link
Member

@visr visr left a comment

Choose a reason for hiding this comment

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

Choosing round here means that you can either undershoot or overshoot if the desired time does not fall on a model timestep. I feel like either an error or a cld, where there can never be undershoot, may be a better option, though I'm not sure what the BMI best practices are for this.

@verseve
Copy link
Contributor Author

verseve commented Apr 5, 2024

Thanks for your comment @visr, good point. I think an improved error message may indeed be a better option than rounding. Will convert this PR back to draft, as it is not yet clear where the deviation from a whole number for the time argument is coming from (wflow or openda).

@verseve verseve marked this pull request as draft April 5, 2024 12:42
@visr
Copy link
Member

visr commented Apr 5, 2024

Perhaps it is almost exactly and integer, but not quite since it comes from floating point math.

julia> t = 10 * (0.1 + 0.2)
3.0000000000000004

julia> Int(t)
ERROR: InexactError: Int64(3.0000000000000004)

julia> round(t)
3.0

julia> isapprox(round(t), t)
true

That should probably be allowed at least, and a better error message for the not so close times would also be an improvement.

@verseve
Copy link
Contributor Author

verseve commented Apr 5, 2024

Perhaps it is almost exactly and integer, but not quite since it comes from floating point math.

Yes, if time is almost equal to an allowed model timestamp this probably happens for steps (almost exactly and integer). I think the issue is probably caused by the time input, because both current model time and model time step are always whole numbers. First a bit more testing with openda-wflow is required (timestamps are rounded in openda) to check where the problem occurs and to decide if we need to allow almost exactly and integer steps. Besides that, if the problem is the external time input, you could also argue that is the responsibility of the external software package to deal with this properly (based on the error message).

Copy link
Contributor

@CFBaptista CFBaptista left a comment

Choose a reason for hiding this comment

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

I have a few suggestions regarding

  1. How to compute steps
  2. The exact conditional statements
  3. Code style

src/bmi.jl Outdated Show resolved Hide resolved
src/bmi.jl Outdated Show resolved Hide resolved
src/bmi.jl Outdated Show resolved Hide resolved
- Use `divrem` for computing `steps` in `BMI.update_until` function. The remainder `rem` is used to throw an error message.
- take the for loop in function `BMI.update_until` out of the conditional block.

Co-authored-by: Carlos Fernando Baptista <[email protected]>
@verseve verseve force-pushed the fix_bmi_update_until branch from c550628 to da8948c Compare April 15, 2024 09:31
@verseve verseve marked this pull request as ready for review April 15, 2024 16:25
@verseve verseve requested a review from CFBaptista April 16, 2024 14:19
@verseve verseve requested a review from visr April 17, 2024 13:20
@visr visr merged commit 6b8a5d0 into master Apr 17, 2024
10 checks passed
@verseve verseve deleted the fix_bmi_update_until branch April 18, 2024 15:59
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.

BMI.update_until can throw an InexactError
4 participants