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

Add numerical support of other real types (Float32) #1909

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

huiyuxie
Copy link
Member

@huiyuxie huiyuxie commented Apr 19, 2024

Address parts of #591 and redo #1604.

Tasks:

  • acoustic_perturbation_2d
  • compressible_euler_1d, compressible_euler_2d, compressible_euler_3d
  • unit tests for above

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.

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
Member

@ranocha ranocha left a comment

Choose a reason for hiding this comment

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

Thanks a lot for your contribution. Please find below some comments/suggestions.

src/equations/acoustic_perturbation_2d.jl Outdated Show resolved Hide resolved
src/equations/acoustic_perturbation_2d.jl Outdated Show resolved Hide resolved
src/equations/acoustic_perturbation_2d.jl Outdated Show resolved Hide resolved
@huiyuxie
Copy link
Member Author

huiyuxie commented Apr 23, 2024

General question here: Is it better to conduct unit testing while making changes, or to perform the tests after completing all the changes? Are there any good examples of testing from this repository? Thanks!

@ranocha
Copy link
Member

ranocha commented Apr 23, 2024

General question here: Is it better to conduct unit testing while making changes, or to perform the tests after completing all the changes? Are there any good examples of testing from this repository? Thanks!

When you're working on numerical fluxes, you can add some unit tests, e.g., to parts like

Trixi.jl/test/test_unit.jl

Lines 633 to 660 in 1745df4

@timed_testset "Consistency check for HLL flux (naive): CEE" begin
flux_hll = FluxHLL(min_max_speed_naive)
# Set up equations and dummy conservative variables state
equations = CompressibleEulerEquations1D(1.4)
u = SVector(1.1, 2.34, 5.5)
orientations = [1]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) flux(u, orientation, equations)
end
equations = CompressibleEulerEquations2D(1.4)
u = SVector(1.1, -0.5, 2.34, 5.5)
orientations = [1, 2]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) flux(u, orientation, equations)
end
equations = CompressibleEulerEquations3D(1.4)
u = SVector(1.1, -0.5, 2.34, 2.4, 5.5)
orientations = [1, 2, 3]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) flux(u, orientation, equations)
end
end

for the compressible Euler equations. Here, you could add additional tests using inputs with eltype(u) == Float32.

We should also add some full integration tests once something works completely - maybe by adding one or two new example elixirs showing how to run simulations with Float32.

@DanielDoehring DanielDoehring added the enhancement New feature or request label Apr 24, 2024
Copy link

codecov bot commented Apr 28, 2024

Codecov Report

Attention: Patch coverage is 99.59514% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 96.15%. Comparing base (b98b6f0) to head (b355951).

Files Patch % Lines
src/equations/compressible_euler_3d.jl 98.69% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1909   +/-   ##
=======================================
  Coverage   96.15%   96.15%           
=======================================
  Files         454      454           
  Lines       36509    36535   +26     
=======================================
+ Hits        35103    35129   +26     
  Misses       1406     1406           
Flag Coverage Δ
unittests 96.15% <99.60%> (+<0.01%) ⬆️

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.

@huiyuxie
Copy link
Member Author

Hi @ranocha I have revised the code based on our discussion. Please review it once more, and if this version is satisfactory, I will apply similar revisions to the subsequent tasks.

For dual numbers, I have no idea whether it will be copied to the GPU and I think CUDA.jl does not directly support this type. If they are to be executed on GPUs, I will explore alternative methods (probably through struct) to manage them.

For documentation, are you referring to a general overview that explains the purpose of using a function like oftype, or to detailed documentation that points to each specific instance in the code?

Copy link
Member

@ranocha ranocha left a comment

Choose a reason for hiding this comment

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

Thanks a lot! I just have some minor suggestions.

For documentation, are you referring to a general overview that explains the purpose of using a function like oftype, or to detailed documentation that points to each specific instance in the code?

I had something in mind like adding a new section to https://trixi-framework.github.io/Trixi.jl/stable/conventions/ where we explain

  • why we use 0.5f0 * instead of 0.5 *
  • why we use patterns such as RealT = eltype(x) and v1_prime = zero(RealT) or A = convert(RealT, 0.2)

There is no need to point to specific code lines from my point of view.

src/equations/acoustic_perturbation_2d.jl Outdated Show resolved Hide resolved
src/equations/acoustic_perturbation_2d.jl Outdated Show resolved Hide resolved
src/equations/acoustic_perturbation_2d.jl Outdated Show resolved Hide resolved
@ranocha
Copy link
Member

ranocha commented Apr 29, 2024

@sloede Could you please have a look as well?

@ranocha ranocha requested a review from sloede April 29, 2024 13:40
@huiyuxie huiyuxie self-assigned this Apr 30, 2024
src/equations/acoustic_perturbation_2d.jl Outdated Show resolved Hide resolved
@huiyuxie huiyuxie requested review from ranocha and benegee May 2, 2024 18:40
Copy link
Member

@ranocha ranocha left a comment

Choose a reason for hiding this comment

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

Thanks! Let's please wait for a review by @sloede before you start modifying more files.

src/equations/acoustic_perturbation_2d.jl Outdated Show resolved Hide resolved
src/equations/acoustic_perturbation_2d.jl Outdated Show resolved Hide resolved
src/equations/compressible_euler_1d.jl Outdated Show resolved Hide resolved
src/equations/compressible_euler_1d.jl Outdated Show resolved Hide resolved
src/equations/compressible_euler_1d.jl Outdated Show resolved Hide resolved
src/equations/compressible_euler_1d.jl Outdated Show resolved Hide resolved
src/equations/compressible_euler_1d.jl Outdated Show resolved Hide resolved
src/equations/compressible_euler_1d.jl Outdated Show resolved Hide resolved
huiyuxie and others added 2 commits May 3, 2024 10:48
Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
@ranocha
Copy link
Member

ranocha commented May 9, 2024

Maybe we can even provide a developer-only convenience function such as isexactf0 with isexactf0(0.5) == true and isexactf0(0.4) == false such that people can easily check whether they need to write convert(RealT, 0.4) or can just use 0.5f0?

I'm not sure it's worth writing something that can be achieved easily by

julia> 0.5 == 0.5f0
true

julia> 0.4 == 0.4f0
false

@sloede
Copy link
Member

sloede commented May 9, 2024

Maybe we can even provide a developer-only convenience function such as isexactf0 with isexactf0(0.5) == true and isexactf0(0.4) == false such that people can easily check whether they need to write convert(RealT, 0.4) or can just use 0.5f0?

I'm not sure it's worth writing something that can be achieved easily by

julia> 0.5 == 0.5f0
true

julia> 0.4 == 0.4f0
false

Fair enough. If it's properly documented somewhere, this should be sufficient.

@ranocha
Copy link
Member

ranocha commented May 10, 2024

Currently, I apply convert to integers case by case. But I think it is not friendly for later contributors to implement new functions (i.e., it is not something straightforward to think about).

If we want to have a simple rule that's easy to remember, we should just recommend using convert, one, zero, oftype etc. I'm not sure whether others havesimilar use cases, but convert etc. is at least used quite often, e.g., in the SciML ecosystem:

https://github.com/SciML/OrdinaryDiffEq.jl/blob/438f34ba24bab5a2f6aa478850a267478cc53dea/src/perform_step/low_order_rk_perform_step.jl#L749C6-L749C28
https://github.com/SciML/OrdinaryDiffEq.jl/blob/438f34ba24bab5a2f6aa478850a267478cc53dea/src/tableaus/low_order_rk_tableaus.jl#L539-L582

@huiyuxie huiyuxie requested a review from sloede May 14, 2024 21:20
Copy link
Member

@ranocha ranocha left a comment

Choose a reason for hiding this comment

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

Nice, thanks! I just have two comments

src/equations/acoustic_perturbation_2d.jl Outdated Show resolved Hide resolved
src/equations/acoustic_perturbation_2d.jl Outdated Show resolved Hide resolved
Copy link
Member

@ranocha ranocha left a comment

Choose a reason for hiding this comment

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

Thanks! The files you have changed here should be ready, aren't they? Thus, we can merge this?

@ranocha
Copy link
Member

ranocha commented May 16, 2024

I started some benchmarks locally to see whether there is any impact on our standard benchmark examples. I would like to check the results before merging this PR.

@huiyuxie
Copy link
Member Author

@ranocha Yes and you prefer to break the whole thing into small pieces and merge them step by step? I originally thought this PR is for all the files in src/equations. Please note that this PR depends on another small PR, which is not merged. If you test the type stability, it will fail.

Is the benchmark for precision or speed?

@huiyuxie huiyuxie removed the request for review from benegee May 16, 2024 16:22
Copy link
Member

@ranocha ranocha left a comment

Choose a reason for hiding this comment

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

Yes and you prefer to break the whole thing into small pieces and merge them step by step? I originally thought this PR is for all the files in src/equations. Please note that this PR depends on another small PR, which is not merged. If you test the type stability, it will fail.

I prefer smaller PRs with clear scope. Big PRs become very hard to handle and to review. Could you maybe start adding some unit tests for this PR already, please? Everything that does not work since it relies on other changes can be marked as @test_broken.

Is the benchmark for precision or speed?

Speed

@huiyuxie
Copy link
Member Author

Please wait. The test data normally covers as large range of inputs as possible. Why is a single set of data tested here for each equation?

Trixi.jl/test/test_unit.jl

Lines 633 to 660 in 1745df4

@timed_testset "Consistency check for HLL flux (naive): CEE" begin
flux_hll = FluxHLL(min_max_speed_naive)
# Set up equations and dummy conservative variables state
equations = CompressibleEulerEquations1D(1.4)
u = SVector(1.1, 2.34, 5.5)
orientations = [1]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) flux(u, orientation, equations)
end
equations = CompressibleEulerEquations2D(1.4)
u = SVector(1.1, -0.5, 2.34, 5.5)
orientations = [1, 2]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) flux(u, orientation, equations)
end
equations = CompressibleEulerEquations3D(1.4)
u = SVector(1.1, -0.5, 2.34, 2.4, 5.5)
orientations = [1, 2, 3]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) flux(u, orientation, equations)
end
end

@ranocha
Copy link
Member

ranocha commented May 19, 2024

We don't have infinitely many resources, so we just added some unit tests and checked whether the implementations behave generally well by running more involved simulation setups as a whole.

@huiyuxie
Copy link
Member Author

Codecov favors my main branch PR here ;)

Copy link
Member

@ranocha ranocha left a comment

Choose a reason for hiding this comment

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

Thanks! Please find below some suggestions

Copy link
Member

Choose a reason for hiding this comment

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

This also needs to be included in test/runtests.jl to be picked up by CI.

# Run unit tests for various equations
@testset "Float32 Type Stability" begin
@timed_testset "Acoustic Perturbation 2D" begin
v_mean_global = (0.0f0, 0.0f0)
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be nice to test type stability here for both Float32 and Float64, e.g.,

for RealT in (Float32, Float64)
    v_mean_global = (zero(RealT), zero(RealT))
    ...
end

orientations = [1, 2]
normal_direction = SVector(1.0f0, 1.0f0)

@test eltype(initial_condition_constant(x, t, equations)) == Float32
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@test eltype(initial_condition_constant(x, t, equations)) == Float32
@test eltype(@inferred initial_condition_constant(x, t, equations)) == Float32

We should also check type stability like this

Copy link
Member Author

@huiyuxie huiyuxie May 21, 2024

Choose a reason for hiding this comment

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

This is used for something like @test @inferred func(c1, c2, c3, ..) == c. Not necessary here

src/equations/acoustic_perturbation_2d.jl Show resolved Hide resolved
src/equations/acoustic_perturbation_2d.jl Show resolved Hide resolved
src/equations/compressible_euler_1d.jl Show resolved Hide resolved
(v_normal + sqrt(v_normal^2 + 4 * A * (p_local + B)))
end

# For the slip wall we directly set the flux as the normal velocity is zero
return SVector(zero(eltype(u_inner)),
return SVector(0,
Copy link
Member

Choose a reason for hiding this comment

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

Here, we use 0, but below... 👇

src/equations/compressible_euler_2d.jl Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants