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

Specify Callback Argument Type in init Functions in ODE solvers (2N, 3Sstar, PairedExplicitRK, SSP) #2026

Merged

Conversation

warisa-r
Copy link
Contributor

This pull request improves type safety and clarity in the init functions of various integrator methods that are updated per issue #1886 by specifying the argument type for callback, according to @JoshuaLampert suggestion in #2008. The callback parameter, which can either be a CallbackSet or Nothing, is now explicitly typed as Union{CallbackSet, Nothing}.

Copy link
Contributor

Review checklist

This checklist is meant to assist creators of PRs (to let them know what reviewers will typically look for) and reviewers (to guide them in a structured review process). Items do not need to be checked explicitly for a PR to be eligible for merging.

Purpose and scope

  • The PR has a single goal that is clear from the PR title and/or description.
  • All code changes represent a single set of modifications that logically belong together.
  • No more than 500 lines of code are changed or there is no obvious way to split the PR into multiple PRs.

Code quality

  • The code can be understood easily.
  • Newly introduced names for variables etc. are self-descriptive and consistent with existing naming conventions.
  • There are no redundancies that can be removed by simple modularization/refactoring.
  • There are no leftover debug statements or commented code sections.
  • The code adheres to our conventions and style guide, and to the Julia guidelines.

Documentation

  • New functions and types are documented with a docstring or top-level comment.
  • Relevant publications are referenced in docstrings (see example for formatting).
  • Inline comments are used to document longer or unusual code sections.
  • Comments describe intent ("why?") and not just functionality ("what?").
  • If the PR introduces a significant change or new feature, it is documented in NEWS.md with its PR number.

Testing

  • The PR passes all tests.
  • New or modified lines of code are covered by tests.
  • New or modified tests run in less then 10 seconds.

Performance

  • There are no type instabilities or memory allocations in performance-critical parts.
  • If the PR intent is to improve performance, before/after time measurements are posted in the PR.

Verification

  • The correctness of the code was verified using appropriate tests.
  • If new equations/methods are added, a convergence test has been run and the results
    are posted in the PR.

Created with ❤️ by the Trixi.jl community.

Copy link

codecov bot commented Jul 29, 2024

Codecov Report

Attention: Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Project coverage is 96.25%. Comparing base (e1aabb3) to head (2c8f2e5).

Files Patch % Lines
src/time_integration/methods_2N.jl 0.00% 1 Missing ⚠️
src/time_integration/methods_3Sstar.jl 0.00% 1 Missing ⚠️
src/time_integration/methods_SSP.jl 0.00% 1 Missing ⚠️
...ation/paired_explicit_runge_kutta/methods_PERK2.jl 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2026      +/-   ##
==========================================
+ Coverage   96.23%   96.25%   +0.02%     
==========================================
  Files         462      462              
  Lines       37084    37076       -8     
==========================================
  Hits        35687    35687              
+ Misses       1397     1389       -8     
Flag Coverage Δ
unittests 96.25% <0.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@JoshuaLampert
Copy link
Member

Thanks, could you maybe also make more precise error messages regarding the continuous callbacks similar to #2008 (comment)? Also I think it would be better to throw an ArgumentError instead just throwing a generic error.

@warisa-r
Copy link
Contributor Author

warisa-r commented Jul 29, 2024

@JoshuaLampert By throwing an ArgumentError, do you mean by bringing back elseif and basically doing this?

if callback isa CallbackSet
        foreach(callback.continuous_callbacks) do cb
            error("Continuous callbacks are unsupported with the low-storage RK method (Carpenter, Kennedy 1994, 4th order, Solution 3).")
        end
        foreach(callback.discrete_callbacks) do cb
            cb.initialize(cb, integrator.u, integrator.t, integrator)
        end
    elseif !isnothing(callbacks)
        throw(ArgumentError("Invalid callback type. Expected CallbackSet or nothing."))
    end

Would that really be necessary since by this we are bringing back the redundant elseif? Since the current approach already throw this kind error: ERROR: LoadError: TypeError: in keyword argument callback, expected Union{Nothing, CallbackSet}, got a value of type Int64 which in my opinion already is sort of enough to let the user knows what has gone wrong. Or am I misunderstanding something?

@JoshuaLampert
Copy link
Member

Yes, you are totally right. I didn't want to bring back the elseif. My suggestion with the ArgumentError was also regarding the continuous callbacks, where a generic error is used. Sorry for not being clear enough.

@warisa-r
Copy link
Contributor Author

Got it! Thank you for the explanation.

@warisa-r warisa-r marked this pull request as ready for review July 29, 2024 19:31
Copy link
Member

@JoshuaLampert JoshuaLampert left a comment

Choose a reason for hiding this comment

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

Thanks! What about

if callback isa CallbackSet
foreach(callback.continuous_callbacks) do cb
error("unsupported")
end
foreach(callback.discrete_callbacks) do cb
cb.initialize(cb, integrator.u, integrator.t, integrator)
end
elseif !isnothing(callback)
error("unsupported")
end

@JoshuaLampert
Copy link
Member

I think the SSP method was also not covered in #1975. Is there a reason for that?

@warisa-r
Copy link
Contributor Author

You are right in that SSP method isn't covered at all. I personally am also not sure why but I just left it out since I wasn't instructed to make any changes there. But according to the issue #1886 s title, SSP method should also been updated right? @DanielDoehring

@DanielDoehring DanielDoehring self-requested a review August 3, 2024 13:31
@DanielDoehring
Copy link
Contributor

Hm the SSP one is a bit different as it heavily relies on stage callbacks, which is not supported by the the standard OrdinaryDiffEq.jl method. But @bennibolm is the guy to ask on this matter.

So from my side this looks alright!

DanielDoehring
DanielDoehring previously approved these changes Aug 8, 2024
@JoshuaLampert
Copy link
Member

Ok, if SSP is conceptually different to the others it's probably fine to have ignored it in #1975, but from what I can tell at least the changes from this PR also apply to the SSP method as well. At least the code block from this comment is pretty much the same as the corresponding code block in the other integrators. Thus, if you don't object @DanielDoehring I would also opt for applying the changes from this PR to the SSP method in order to keep consistency.

@DanielDoehring
Copy link
Contributor

Yeah I guess we should make the error messages more similar.

@warisa-r warisa-r changed the title Specify Callback Argument Type in init Functions in ODE solvers (SimpleIntegrator2N, SimpleIntegrator3Sstar, PairedExplicitRK) Specify Callback Argument Type in init Functions in ODE solvers (2N, 3Sstar, PairedExplicitRK, SSP) Aug 10, 2024
Copy link
Member

@JoshuaLampert JoshuaLampert left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@DanielDoehring DanielDoehring enabled auto-merge (squash) August 11, 2024 11:47
@ranocha ranocha disabled auto-merge August 13, 2024 17:34
@ranocha ranocha merged commit 5f5a232 into trixi-framework:main Aug 13, 2024
34 of 36 checks passed
@warisa-r warisa-r deleted the specify_init_arg_type_integrator branch August 14, 2024 19:40
warisa-r added a commit to warisa-r/Trixi.jl that referenced this pull request Oct 20, 2024
ranocha added a commit that referenced this pull request Nov 5, 2024
* make NLsolve a weapdep

* fmt

* add NEWS.md but TBD on the ref pull request

* add comments and adjustment on solve_a_unknown

* modular implementation with init, step!, and solve_step!

* fmt

* add test

* adda body of p3 constructor test

* changes according to test and correct variable names

* only check the values of a_matrix from second row to end

* adjust the the constructor of path coefficient and its test

* adjust the test and add a seed to the randomized initial guess for reproducibility

* add NLsolve as a dependency for testing

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* optimize the loop for step! by moving the condition outside

* fmt

* more type generic

* change some names

* update test

* Correcting steps!

* Apply suggestions from code review

Co-authored-by: Daniel Doehring <[email protected]>

* Update examples/structured_1d_dgsem/elixir_burgers_perk3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Update examples/structured_1d_dgsem/elixir_burgers_perk3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* add docstring about dt_opt

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* merge k_higher in the last stage to a bigger loop

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* change solve_step! to solve!

* Correct the logic of step!

* deprecation

* Optimize K_S1 away

* fmt

* remove dt_opt as an attribute of PERK3

* change the objective function to match the number of equations

* fmt

* minor comment fix

* delete some stuff left from random

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* minor adjustments

* minor change to the comment

* add proper comment and bring seed back

* update test values

* fmt

* change the keyword according to the error in the test pipeline and edit some values to match the test pipeline

* remove unused import

* fix test values in misc

* add max iteration

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Apply suggestions from code review

* remove the allocating part of is_sol_valid

* removing dt_opt and update test values

* Update NEWS.md

Co-authored-by: Daniel Doehring <[email protected]>

* update cfl number for the simulation

* Update examples/structured_1d_dgsem/elixir_burgers_perk3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* change from a_stages_stages.txt to a_stages.txt

* fixed step size should work with save solution now

* Update examples/structured_1d_dgsem/elixir_burgers_perk3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* add save solution to the example

* update test to be compatible with save_solution

* move comment regarding seed upwards

* Revert "Correct the logic of step!" only the part that meddles with methods_PERK2

* correct methods_PERK3

* move is_sol_valid closer to the for loop

* fmt

* Revert some random changes in other test unit

* add tolerance to the test

* modify functions so that they are also compatible with PERK3

* change function's name to be more descriptive

* change function's name to be more descriptive in all files

* Revert irrelevent change in TrixiConvexECOSExt.jl

* add PR number to NEWS.md

* fmt

* change from using Random to StableRNGs

* fix the value in unit test

* remove prints

* minor comment correction

* attempt to fix the error at fixed time step

* add the missing clause to test set

* adjust allocation values in test of perk3

* update test value

* move objective function to the extension

* minor fix with compute_c_coeffs

* remove explicit import of solve_a_unknown from line 18

* Apply suggestions from code review

* document why additional packages are loaded

* correct docstring

* use Float32

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Hendrik Ranocha <[email protected]>

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Hendrik Ranocha <[email protected]>

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Hendrik Ranocha <[email protected]>

* change some Flot32 back to the way they originally were

* add line that get the type that a_unknown should be

* due to some the change of type, print out some values of a_matrix that changes slighly from the original value (error value in other tests still remain the same)

* update test values

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Joshua Lampert <[email protected]>

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Joshua Lampert <[email protected]>

* Apply suggestions from code review

Co-authored-by: Joshua Lampert <[email protected]>

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Joshua Lampert <[email protected]>

* allocate c_eq once per solve_a_unknown is called

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Joshua Lampert <[email protected]>

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Joshua Lampert <[email protected]>

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Joshua Lampert <[email protected]>

* minor fix regarding recent changes witjh c_eq

* adjust a constructor to get num stages from reading the files directly

* Revert "adjust a constructor to get num stages from reading the files directly" since it is breaking

* Update TrixiNLsolveExt.jl to use forward autodiff in solve_a_butcher_coeffs_unknown! function

* Apply suggestions from code review

* Slight modifications a values

* add cfl number calculation for PERK3

* update CI values

* remove the example without cfl calculation

* Apply suggestions from code review

Co-authored-by: Daniel Doehring <[email protected]>

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Daniel Doehring <[email protected]>

* add DOI to a reference in TrixiNLsolveExt

* update the test of perk3

* Update examples/structured_1d_dgsem/elixir_burgers_perk3.jl

* Update examples/structured_1d_dgsem/elixir_burgers_perk3.jl

Co-authored-by: Joshua Lampert <[email protected]>

* Update examples/structured_1d_dgsem/elixir_burgers_perk3.jl

Co-authored-by: Joshua Lampert <[email protected]>

* add to docstring packages needed in order to use PERK3

* update NEWS.md

* changes according to #2026

* add comments to the test without stepsize callback

* Update ext/TrixiNLsolveExt.jl

Co-authored-by: Joshua Lampert <[email protected]>

* fmt

* slight modification according to #2123

* Update test/test_unit.jl

Co-authored-by: Joshua Lampert <[email protected]>

* Update src/time_integration/paired_explicit_runge_kutta/methods_PERK3.jl

Co-authored-by: Joshua Lampert <[email protected]>

* fmt + minor correction

* update NEWS.md

* make functions more general for PERK

* remove base.resize for perk3

* put all the general functions of PERK into paired_explicit_runge_kutta.jl

* Update src/time_integration/paired_explicit_runge_kutta/paired_explicit_runge_kutta.jl

Co-authored-by: Joshua Lampert <[email protected]>

* modify some functions to be useable for both multi and single scheme

* fmt

* ensure type consistency in compute_c_coeffs

* print out the full a_matrix

* print out the new a_matrix from the latest change

* remove the false line of PERK construction

* fix the test values

* move using statements to src/Trixi.jl

---------

Co-authored-by: Daniel Doehring <[email protected]>
Co-authored-by: Hendrik Ranocha <[email protected]>
Co-authored-by: Joshua Lampert <[email protected]>
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.

4 participants