Skip to content

Unify boundary conditions#2761

Merged
ranocha merged 39 commits intomainfrom
unify-bc
Feb 7, 2026
Merged

Unify boundary conditions#2761
ranocha merged 39 commits intomainfrom
unify-bc

Conversation

@JoshuaLampert
Copy link
Copy Markdown
Member

@JoshuaLampert JoshuaLampert commented Jan 26, 2026

I would like to tackle #1510 to make it more attractive to create a new breaking release rather sooner than later, such that #2753 can be merged soon.
This PR allows to pass the boundary conditions as Dict or as NamedTuple and allows to not pass periodic boundary conditions to make it the default. For the first part I'm not sure if this is what we want or if we would prefer to allow only one of the possibilities – Dict or NamedTuple.
I used the help of Claude to generate this PR.

Closes #1510.

@github-actions
Copy link
Copy Markdown
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.

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.06%. Comparing base (21e5d0d) to head (8bf0f1e).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2761      +/-   ##
==========================================
+ Coverage   97.04%   97.06%   +0.02%     
==========================================
  Files         574      573       -1     
  Lines       44765    44823      +58     
==========================================
+ Hits        43439    43505      +66     
+ Misses       1326     1318       -8     
Flag Coverage Δ
unittests 97.06% <ø> (+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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JoshuaLampert
Copy link
Copy Markdown
Member Author

The CI errors are really weird, but they seem to appear consistently on the CI runner. I cannot reproduce locally.

@JoshuaLampert
Copy link
Copy Markdown
Member Author

JoshuaLampert commented Jan 26, 2026

Ah, it's also failing in other PRs, see #2754 and #2733. The last successful CI run was 6 hours ago (9:17 AM UTC+1). It seems like the error happens first in performance_specializations here:
https://github.com/trixi-framework/Trixi.jl/actions/runs/21359613034/job/61479451985?pr=2761#step:7:724, which also has the most information in the stacktrace (the other errors only say Package Trixi errored during testing (received signal: 6)). Seems to be some problem with LLVM. I found JuliaLang/julia#47176 and https://discourse.julialang.org/t/segmentation-fault-edit-signal-6-has-somebody-a-clue/66206, but this didn't really help me figured out what's happening. Do you have any idea, @vchuravy?
I think it first happened 5 hours ago (10:37 AM UTC+1) here. Since then it's failing consistently.

Edit: I looked at the diff of the used package versions between the last successful and the first failing CI run. Only SciMLBase.jl updated from v2.134.0 to v2.135.0 and DifferentiationInterface.jl updated from v0.7.14 to v0.7.15. Given that the error happens in examples/tree_2d_dgsem/elixir_euler_ec.jl:47, which is

callbacks = CallbackSet(summary_callback,
analysis_callback, alive_callback,
save_solution,
stepsize_callback)
I am currently suspecting it has something to do with SciMLBase.jl.

Edit2: No, it's not SciMLBase.jl. It's also failing with the old version.
Edit3: Seems to work again...

Copy link
Copy Markdown
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.

Thank you very much for tackling this long-standing issue!

In the very end, everything ends up being type-stable using tuples also for the unstructured BCs:

function UnstructuredSortedBoundaryTypes(boundary_conditions::Dict, cache)
# extract the unique boundary function routines from the dictionary
boundary_condition_types = Tuple(unique(collect(values(boundary_conditions))))
n_boundary_types = length(boundary_condition_types)
boundary_indices = ntuple(_ -> [], n_boundary_types)
# Initialize `boundary_symbol_indices` as an empty dictionary, filled later in `initialize!`
boundary_symbol_indices = Dict{Symbol, Vector{Int}}()
container = UnstructuredSortedBoundaryTypes{n_boundary_types,
typeof(boundary_condition_types),
Vector{Int}}(boundary_condition_types,
boundary_indices,
boundary_conditions,
boundary_symbol_indices)
return initialize!(container, cache)
end

Thus, I strongly prefer using NamedTuples - and only them, no Dicts - to keep type stability in the whole computation. Or do I miss a good reason for using Dicts?

We also need to remember to add a NEWS.md entry when the design has been finalized.

@JoshuaLampert
Copy link
Copy Markdown
Member Author

JoshuaLampert commented Jan 27, 2026

I agree that consistently using NamedTuples (and not using Dicts anymore) is a good idea. I did that. A couple of remarks and questions:

  • The DGMulti solver used NamedTupless for the boundary conditions, but for some reason another type of syntax to construct them (of the type (; :symbol => boundary_condition) instead of (; name = boundary_condition). I also unified that, but this PR should not be breaking for DGMulti.
  • DGMulti still uses Dicts for is_on_boundary. Is that fine or should we also use NamedTuples there?
  • Did I understand your comment above correctly that UnstructuredSortedBoundaryTypes should also accept NamedTuples instead of Dicts, @ranocha? If yes, I'll also address that. Edit: Addressed.
  • The failing Downstream tests are expected because this PR is breaking for TrixiAtmo.jl and TrixiShallowWater.jl. Once this PR is ready, I can create corresponding PRs in the downstream packages. Edit: I already created Unify boundary conditions TrixiAtmo.jl#141 and Unify boundary conditions TrixiShallowWater.jl#126.

@ranocha
Copy link
Copy Markdown
Member

ranocha commented Jan 27, 2026

Great, thanks!

  • Did I understand your comment above correctly that UnstructuredSortedBoundaryTypes should also accept NamedTuples instead of Dicts, @ranocha? If yes, I'll also address that.

Yes - if we keep working with NamedTuples (and Tuples etc.) but not Dicts in all steps, we should be able to avoid type instabilities completely.

@ranocha
Copy link
Copy Markdown
Member

ranocha commented Jan 27, 2026

allows to not pass periodic boundary conditions to make it the default

This is something we might discuss in the next Trixi.jl meeting as well. I have observed that it can be a source of annoying-to-debug errors, that everything is periodic by default. Maybe it's time to change this and require passing periodic = true and the corresponding periodic BC?

@JoshuaLampert
Copy link
Copy Markdown
Member Author

Thanks for the review! I have one open question above

DGMulti still uses Dicts for is_on_boundary. Is that fine or should we also use NamedTuples there?

before I move to the periodic boundary conditions.

I discussed with @jlchan that switching to NamedTuples is preferred. And it turned out StartUpDG.jl was already perfectly preferred for accepting NamedTuples. So this is done in 552beb3.

@jlchan
Copy link
Copy Markdown
Contributor

jlchan commented Feb 6, 2026

Thanks for the review! I have one open question above

DGMulti still uses Dicts for is_on_boundary. Is that fine or should we also use NamedTuples there?

before I move to the periodic boundary conditions.

I discussed with @jlchan that switching to NamedTuples is preferred. And it turned out StartUpDG.jl was already perfectly preferred for accepting NamedTuples. So this is done in 552beb3.

Thanks for adding that @JoshuaLampert!

@JoshuaLampert JoshuaLampert requested a review from ranocha February 6, 2026 15:48
@JoshuaLampert
Copy link
Copy Markdown
Member Author

This PR is finished from my point of view, @ranocha. It left it in draft mode so that it does not get accidentally merged.

Copy link
Copy Markdown
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!

@ranocha
Copy link
Copy Markdown
Member

ranocha commented Feb 6, 2026

Please see also the comment #2761 (comment) above

JoshuaLampert and others added 4 commits February 6, 2026 21:04
Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
ranocha
ranocha previously approved these changes Feb 7, 2026
ranocha and others added 2 commits February 7, 2026 12:06
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
ranocha
ranocha previously approved these changes Feb 7, 2026
* no default boundary conditions

* format

* some fixes

* more fixes

* format

* more fixes

* fix docs

* fix parabolic

* remove comma

* error if wrong boundary conditions supplied

* set periodicity to false by default

* format

* fix some periodicity of meshes

* format

* add periodicity = true in elixirs and more tests

* add docstring fir TreeMesh constructor

* periodicity = true for StructuredMesh

* fix some unit tests

* fix more unit tests

* unit test fixes

* fix error type for Julia <v1.12

* fix some tests

* fix bug that periodicity not saved to file for StructuredMesh

* fix comments

* fixes

* add NEWS entry

* add tests for partially periodic meshes

* also test StructuredMesh

* add another NEWS entry

* allow not passing periodic boundary conditions in NamedTuple for periodic parts of tree and structured mesh

* test an elixir without specifying periodic boundary conditions in periodic direction

* disallow passing boundary conditions as tuple

* remove boundary_condition_default

* Apply suggestions from code review

Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
Co-authored-by: Jesse Chan <1156048+jlchan@users.noreply.github.com>

* docstring for SemidiscretizationHyperbolicParabolic

* format

* format

* fix typo in NEWS

* boundary_condition_periodic instead of BoundaryConditionPeriodc()

* improve error handling for passing boundary conditions as Tuples and add tests

* remove both_ again

* resolve method ambiguities

* resolve more ambiguities

* return UnstructuredSortedBoundaryTypes

* fix test

if UnstructuredSortedBoundaryTypes directly passed as boundary_conditions (e.g., by remake) keep this boundary condition

---------

Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
Co-authored-by: Jesse Chan <1156048+jlchan@users.noreply.github.com>
@ranocha ranocha marked this pull request as ready for review February 7, 2026 15:45
@ranocha ranocha merged commit 225db47 into main Feb 7, 2026
37 of 39 checks passed
@ranocha ranocha deleted the unify-bc branch February 7, 2026 16:24
DanielDoehring pushed a commit to DanielDoehring/Trixi.jl that referenced this pull request Feb 19, 2026
* allow Dict and NamedTuple as boundary conditions

* unify handling of periodic boundary conditions

* add tests for T8codeMesh and UnstructuredMesh2D

* test actual boundary conditions

* move new test file to unit tests

* format

* enforce SciMLBase.jl v2.134.0

* revert restriction on SciMLBase.jl

* also test other dimensions for coverage

* coverage

* always use NamedTuples instead of Dicts

* use same syntax to for DGMulti to create NamedTuples

* fix

* fix more

* format

* fixed another one

* fix one more

* consistently use semicolon for NamedTuples

* Add NEWS entry

* fix structured

* use NamedTuple also in UnstructuredSortedBoundaryTypes

* fixes

* remove periodic bcs by default

* simplify default boundary conditions

* Apply suggestions from code review

Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>

* use NamedTuple for is_on_boundary

* add some semicolons for consistency

* Apply suggestion from @ranocha

Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>

* Apply suggestion from @ranocha

Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>

* improve type stability of UnstructuredSortedBoundaryTypes

* fix order of type parameters

* Update src/solvers/dgsem_unstructured/sort_boundary_conditions.jl

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* No default boundary conditions (trixi-framework#2770)

* no default boundary conditions

* format

* some fixes

* more fixes

* format

* more fixes

* fix docs

* fix parabolic

* remove comma

* error if wrong boundary conditions supplied

* set periodicity to false by default

* format

* fix some periodicity of meshes

* format

* add periodicity = true in elixirs and more tests

* add docstring fir TreeMesh constructor

* periodicity = true for StructuredMesh

* fix some unit tests

* fix more unit tests

* unit test fixes

* fix error type for Julia <v1.12

* fix some tests

* fix bug that periodicity not saved to file for StructuredMesh

* fix comments

* fixes

* add NEWS entry

* add tests for partially periodic meshes

* also test StructuredMesh

* add another NEWS entry

* allow not passing periodic boundary conditions in NamedTuple for periodic parts of tree and structured mesh

* test an elixir without specifying periodic boundary conditions in periodic direction

* disallow passing boundary conditions as tuple

* remove boundary_condition_default

* Apply suggestions from code review

Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
Co-authored-by: Jesse Chan <1156048+jlchan@users.noreply.github.com>

* docstring for SemidiscretizationHyperbolicParabolic

* format

* format

* fix typo in NEWS

* boundary_condition_periodic instead of BoundaryConditionPeriodc()

* improve error handling for passing boundary conditions as Tuples and add tests

* remove both_ again

* resolve method ambiguities

* resolve more ambiguities

* return UnstructuredSortedBoundaryTypes

* fix test

if UnstructuredSortedBoundaryTypes directly passed as boundary_conditions (e.g., by remake) keep this boundary condition

---------

Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
Co-authored-by: Jesse Chan <1156048+jlchan@users.noreply.github.com>

---------

Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
Co-authored-by: Hendrik Ranocha <mail@ranocha.de>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Jesse Chan <1156048+jlchan@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unify boundary condition specification

4 participants