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 support for plotting 1D function #2250

Merged
merged 25 commits into from
Feb 5, 2025
Merged

Add support for plotting 1D function #2250

merged 25 commits into from
Feb 5, 2025

Conversation

JoshuaLampert
Copy link
Member

Sometimes it might be of interest to not only plot the numerical solution, but also other functions on top of it, especially in a 1D plot. Often this is done with the initial condition/analytical solution as reference. Currently, as far as I know, there was no straightforward way to accomplish this in 1D (except for using the internal semi.cache.elements.node_coordinates).
This PR enables the possibility to call plot(func, semi), where func is a function accepting spatial coordinates x and the equations, i.e. it is called by func(x, equations). A common example would be (x, equations) -> initial_condition(x, last(tspan), equations).
If you agree that it would be nice to support this, I can clean up the PR a bit.

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 Jan 27, 2025

Codecov Report

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

Project coverage is 96.87%. Comparing base (06e8a20) to head (72cad15).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/visualization/recipes_plots.jl 66.67% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2250      +/-   ##
==========================================
- Coverage   96.87%   96.87%   -0.00%     
==========================================
  Files         490      490              
  Lines       39427    39454      +27     
==========================================
+ Hits        38194    38220      +26     
- Misses       1233     1234       +1     
Flag Coverage Δ
unittests 96.87% <88.24%> (-<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.

src/visualization/utilities.jl Outdated Show resolved Hide resolved
src/visualization/utilities.jl Outdated Show resolved Hide resolved
@JoshuaLampert JoshuaLampert marked this pull request as ready for review January 30, 2025 12:24
@JoshuaLampert
Copy link
Member Author

This is ready for a review from my point of view.

src/visualization/types.jl Outdated Show resolved Hide resolved
DanielDoehring
DanielDoehring previously approved these changes Feb 2, 2025
@JoshuaLampert JoshuaLampert added visualization enhancement New feature or request labels Feb 2, 2025
SimonCan
SimonCan previously approved these changes Feb 4, 2025
Copy link
Contributor

@SimonCan SimonCan 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 to me. Once the codecov is fine this can be merged.

@JoshuaLampert
Copy link
Member Author

Once the codecov is fine this can be merged.

I think it will be hard to make codecov happy on this one because lines like this, i.e., the function definition lines of the plot recipes are just not caught by codecov. So we can't do much against it. The only two lines that are not covered are the error messages, but I think it's fine to not cover them.

@DanielDoehring
Copy link
Contributor

I think it will be hard to make codecov happy on this one because lines like this, i.e., the function definition lines of the plot recipes are just not caught by codecov. So we can't do much against it. The only two lines that are not covered are the error messages, but I think it's fine to not cover them.

Yeah that is always with the case - ironically, code coverage punishes you for adding safety :D Would be nice to add comments for codecov s.t. some lines are tolerated to be skipped.

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! Nice feature to have 👍

src/visualization/recipes_plots.jl Outdated Show resolved Hide resolved
src/visualization/types.jl Outdated Show resolved Hide resolved
src/visualization/recipes_plots.jl Outdated Show resolved Hide resolved
@JoshuaLampert JoshuaLampert dismissed stale reviews from SimonCan and DanielDoehring via 5f29ace February 4, 2025 20:15
@ranocha
Copy link
Member

ranocha commented Feb 5, 2025

Could you please post the result of one or two examples? If that looks good, this PR should be ready to go 👍

@JoshuaLampert
Copy link
Member Author

Could you please post the result of one or two examples? If that looks good, this PR should be ready to go 👍

Sure:

julia> include("examples/tree_1d_dgsem/elixir_euler_density_wave.jl")
[...]
julia> plot((x, equations) -> initial_condition(x, last(tspan), equations), semi)

julia> plot((x, equations) -> cons2prim(initial_condition(x, last(tspan), equations), equations), semi)

julia> plot!(sol)

julia> plot((x, equations) -> sinpi.(x), semi, nvisnodes = 5)

gives
analytical_cons
and
analytical_and_numerical_prim
and
sin

The calls look the same for the StructuredMesh and the DGMultiMesh (with the differences that nvisnodes has no effect as it is the case for plotting the numerical solution on these mesh types).

@JoshuaLampert JoshuaLampert requested a review from ranocha February 5, 2025 15:27
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!

@ranocha ranocha merged commit c24ce91 into main Feb 5, 2025
38 of 39 checks passed
@ranocha ranocha deleted the plot-recipe-1d-function branch February 5, 2025 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request visualization
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants