Skip to content

Commit

Permalink
format examples (#1531)
Browse files Browse the repository at this point in the history
* format examples

* check formatting of examples in CI

* update style guide

* fix weird formatting

* fix formatting of binary operators

* format again
  • Loading branch information
ranocha authored Nov 3, 2023
1 parent 2ec512c commit c79fa43
Show file tree
Hide file tree
Showing 352 changed files with 12,593 additions and 12,638 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/FormatCheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
# format(".")
run: |
julia -e 'using Pkg; Pkg.add(PackageSpec(name = "JuliaFormatter"))'
julia -e 'using JuliaFormatter; format(["benchmark", "ext", "src", "test", "utils"])'
julia -e 'using JuliaFormatter; format(["benchmark", "examples", "ext", "src", "test", "utils"])'
- name: Format check
run: |
julia -e '
Expand Down
18 changes: 9 additions & 9 deletions docs/src/styleguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ conventions, we apply and enforce automated source code formatting
* Maximum line length (strictly): **92**.
* Functions that mutate their *input* are named with a trailing `!`.
* Functions order their parameters [similar to Julia Base](https://docs.julialang.org/en/v1/manual/style-guide/#Write-functions-with-argument-ordering-similar-to-Julia-Base-1).
* The main modified argument comes first. For example, if the right-hand side `du` is modified,
it should come first. If only the `cache` is modified, e.g., in `prolong2interfaces!`
* The main modified argument comes first. For example, if the right-hand side `du` is modified,
it should come first. If only the `cache` is modified, e.g., in `prolong2interfaces!`
and its siblings, put the `cache` first.
* Otherwise, use the order `mesh, equations, solver, cache`.
* If something needs to be specified in more detail for dispatch, put the additional argument before the general one
* If something needs to be specified in more detail for dispatch, put the additional argument before the general one
that is specified in more detail. For example, we use `have_nonconservative_terms(equations), equations`
and `dg.mortar, dg`.
* Prefer `for i in ...` to `for i = ...` for better semantic clarity and greater flexibility.
Expand Down Expand Up @@ -55,7 +55,7 @@ julia -e 'using Pkg; Pkg.add("JuliaFormatter")'
```
You can then recursively format the core Julia files in the Trixi.jl repo by executing
```shell
julia -e 'using JuliaFormatter; format(["benchmark", "ext", "src", "utils"])'
julia -e 'using JuliaFormatter; format(["benchmark", "examples", "ext", "src", "test", "utils"])'
```
from inside the Trixi.jl repository. For convenience, there is also a script you can
directly run from your terminal shell, which will automatically install JuliaFormatter in a
Expand All @@ -67,12 +67,12 @@ You can get more information about using the convenience script by running it wi
`--help`/`-h` flag.

### Checking formatting before committing
It can be convenient to check the formatting of source code automatically before each commit.
It can be convenient to check the formatting of source code automatically before each commit.
We use git-hooks for it and provide a `pre-commit` script in the `utils` folder. The script uses
[JuliaFormatter.jl](https://github.com/domluna/JuliaFormatter.jl) just like formatting script that
runs over the whole Trixi.jl directory.
You can copy the `pre-commit`-script into `.git/hooks/pre-commit` and it will check your formatting
[JuliaFormatter.jl](https://github.com/domluna/JuliaFormatter.jl) just like formatting script that
runs over the whole Trixi.jl directory.
You can copy the `pre-commit`-script into `.git/hooks/pre-commit` and it will check your formatting
before each commit. If errors are found the commit is aborted and you can add the corrections via
```shell
```shell
git add -p
```
13 changes: 6 additions & 7 deletions examples/dgmulti_1d/elixir_advection_gauss_sbp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ dg = DGMulti(polydeg = 3,

cells_per_dimension = (8,)
mesh = DGMultiMesh(dg, cells_per_dimension,
coordinates_min=(-1.0,), coordinates_max=(1.0,),
periodicity=true)
coordinates_min = (-1.0,), coordinates_max = (1.0,),
periodicity = true)

###############################################################################
# setup the test problem (no source term needed for linear advection)
Expand All @@ -49,20 +49,19 @@ ode = semidiscretize(semi, tspan)
summary_callback = SummaryCallback()

# analyse the solution in regular intervals and prints the results
analysis_callback = AnalysisCallback(semi, interval=100, uEltype=real(dg))
analysis_callback = AnalysisCallback(semi, interval = 100, uEltype = real(dg))

# handles the re-calculation of the maximum Δt after each time step
stepsize_callback = StepsizeCallback(cfl=0.75)
stepsize_callback = StepsizeCallback(cfl = 0.75)

# collect all callbacks such that they can be passed to the ODE solver
callbacks = CallbackSet(summary_callback, analysis_callback, stepsize_callback)

###############################################################################
# run the simulation

sol = solve(ode, CarpenterKennedy2N54(williamson_condition=false),
dt=1.0, save_everystep=false, callback=callbacks);
sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false),
dt = 1.0, save_everystep = false, callback = callbacks);

# Print the timer summary
summary_callback()

23 changes: 13 additions & 10 deletions examples/dgmulti_1d/elixir_euler_fdsbp_periodic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
using Trixi, OrdinaryDiffEq

dg = DGMulti(element_type = Line(),
approximation_type = periodic_derivative_operator(
derivative_order=1, accuracy_order=4, xmin=0.0, xmax=1.0, N=50),
approximation_type = periodic_derivative_operator(derivative_order = 1,
accuracy_order = 4,
xmin = 0.0, xmax = 1.0,
N = 50),
surface_flux = flux_hll,
volume_integral = VolumeIntegralWeakForm())

equations = CompressibleEulerEquations1D(1.4)
initial_condition = initial_condition_convergence_test
source_terms = source_terms_convergence_test

mesh = DGMultiMesh(dg, coordinates_min=(-1.0,),
coordinates_max=( 1.0,))
mesh = DGMultiMesh(dg, coordinates_min = (-1.0,),
coordinates_max = (1.0,))

semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, dg,
source_terms = source_terms)
Expand All @@ -21,15 +23,16 @@ tspan = (0.0, 0.4)
ode = semidiscretize(semi, tspan)

summary_callback = SummaryCallback()
alive_callback = AliveCallback(alive_interval=10)
alive_callback = AliveCallback(alive_interval = 10)
analysis_interval = 100
analysis_callback = AnalysisCallback(semi, interval=analysis_interval, uEltype=real(dg))
stepsize_callback = StepsizeCallback(cfl=1.0)
callbacks = CallbackSet(summary_callback, alive_callback, stepsize_callback, analysis_callback)
analysis_callback = AnalysisCallback(semi, interval = analysis_interval, uEltype = real(dg))
stepsize_callback = StepsizeCallback(cfl = 1.0)
callbacks = CallbackSet(summary_callback, alive_callback, stepsize_callback,
analysis_callback)

###############################################################################
# run the simulation

sol = solve(ode, CarpenterKennedy2N54(williamson_condition=false),
dt = 0.5 * estimate_dt(mesh, dg), save_everystep=false, callback=callbacks);
sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false),
dt = 0.5 * estimate_dt(mesh, dg), save_everystep = false, callback = callbacks);
summary_callback() # print the timer summary
12 changes: 6 additions & 6 deletions examples/dgmulti_1d/elixir_euler_flux_diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ source_terms = source_terms_convergence_test

cells_per_dimension = (8,)
mesh = DGMultiMesh(dg, cells_per_dimension,
coordinates_min=(-1.0,), coordinates_max=(1.0,), periodicity=true)
coordinates_min = (-1.0,), coordinates_max = (1.0,), periodicity = true)
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, dg;
source_terms=source_terms)
source_terms = source_terms)

tspan = (0.0, 1.1)
ode = semidiscretize(semi, tspan)

summary_callback = SummaryCallback()
alive_callback = AliveCallback(alive_interval=10)
alive_callback = AliveCallback(alive_interval = 10)
analysis_interval = 100
analysis_callback = AnalysisCallback(semi, interval=analysis_interval, uEltype=real(dg))
analysis_callback = AnalysisCallback(semi, interval = analysis_interval, uEltype = real(dg))
callbacks = CallbackSet(summary_callback,
analysis_callback,
alive_callback)

###############################################################################
# run the simulation

sol = solve(ode, CarpenterKennedy2N54(williamson_condition=false),
dt = 0.5 * estimate_dt(mesh, dg), save_everystep=false, callback=callbacks);
sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false),
dt = 0.5 * estimate_dt(mesh, dg), save_everystep = false, callback = callbacks);

summary_callback() # print the timer summary
38 changes: 21 additions & 17 deletions examples/dgmulti_2d/elixir_advection_diffusion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,52 @@ initial_condition_zero(x, t, equations::LinearScalarAdvectionEquation2D) = SVect
initial_condition = initial_condition_zero

# tag different boundary segments
left(x, tol=50*eps()) = abs(x[1] + 1) < tol
right(x, tol=50*eps()) = abs(x[1] - 1) < tol
bottom(x, tol=50*eps()) = abs(x[2] + 1) < tol
top(x, tol=50*eps()) = abs(x[2] - 1) < tol
left(x, tol = 50 * eps()) = abs(x[1] + 1) < tol
right(x, tol = 50 * eps()) = abs(x[1] - 1) < tol
bottom(x, tol = 50 * eps()) = abs(x[2] + 1) < tol
top(x, tol = 50 * eps()) = abs(x[2] - 1) < tol
is_on_boundary = Dict(:left => left, :right => right, :top => top, :bottom => bottom)

cells_per_dimension = (16, 16)
mesh = DGMultiMesh(dg, cells_per_dimension; is_on_boundary)

# BC types
boundary_condition_left = BoundaryConditionDirichlet((x, t, equations) -> SVector(1 + 0.1 * x[2]))
boundary_condition_left = BoundaryConditionDirichlet((x, t, equations) -> SVector(1 +
0.1 *
x[2]))
boundary_condition_zero = BoundaryConditionDirichlet((x, t, equations) -> SVector(0.0))
boundary_condition_neumann_zero = BoundaryConditionNeumann((x, t, equations) -> SVector(0.0))

# define inviscid boundary conditions
boundary_conditions = (; :left => boundary_condition_left,
:bottom => boundary_condition_zero,
:top => boundary_condition_do_nothing,
:right => boundary_condition_do_nothing)
:bottom => boundary_condition_zero,
:top => boundary_condition_do_nothing,
:right => boundary_condition_do_nothing)

# define viscous boundary conditions
boundary_conditions_parabolic = (; :left => boundary_condition_left,
:bottom => boundary_condition_zero,
:top => boundary_condition_zero,
:right => boundary_condition_neumann_zero)
:bottom => boundary_condition_zero,
:top => boundary_condition_zero,
:right => boundary_condition_neumann_zero)

semi = SemidiscretizationHyperbolicParabolic(mesh, (equations, equations_parabolic), initial_condition, dg;
boundary_conditions=(boundary_conditions, boundary_conditions_parabolic))
semi = SemidiscretizationHyperbolicParabolic(mesh, (equations, equations_parabolic),
initial_condition, dg;
boundary_conditions = (boundary_conditions,
boundary_conditions_parabolic))

tspan = (0.0, 1.5)
ode = semidiscretize(semi, tspan)

summary_callback = SummaryCallback()
alive_callback = AliveCallback(alive_interval=10)
alive_callback = AliveCallback(alive_interval = 10)
analysis_interval = 100
analysis_callback = AnalysisCallback(semi, interval=analysis_interval, uEltype=real(dg))
analysis_callback = AnalysisCallback(semi, interval = analysis_interval, uEltype = real(dg))
callbacks = CallbackSet(summary_callback, alive_callback)

###############################################################################
# run the simulation

time_int_tol = 1e-6
sol = solve(ode, RDPK3SpFSAL49(); abstol=time_int_tol, reltol=time_int_tol,
ode_default_options()..., callback=callbacks)
sol = solve(ode, RDPK3SpFSAL49(); abstol = time_int_tol, reltol = time_int_tol,
ode_default_options()..., callback = callbacks)
summary_callback() # print the timer summary
54 changes: 28 additions & 26 deletions examples/dgmulti_2d/elixir_advection_diffusion_nonperiodic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,63 @@ equations_parabolic = LaplaceDiffusion2D(diffusivity(), equations)
# to numerical partial differential equations.
# [DOI](https://doi.org/10.1007/978-3-319-41640-3_6).
function initial_condition_erikkson_johnson(x, t, equations)
l = 4
epsilon = diffusivity() # Note: this requires epsilon < 0.6 due to the sqrt
lambda_1 = (-1 + sqrt(1 - 4 * epsilon * l)) / (-2 * epsilon)
lambda_2 = (-1 - sqrt(1 - 4 * epsilon * l)) / (-2 * epsilon)
r1 = (1 + sqrt(1 + 4 * pi^2 * epsilon^2)) / (2 * epsilon)
s1 = (1 - sqrt(1 + 4 * pi^2 * epsilon^2)) / (2 * epsilon)
u = exp(-l * t) * (exp(lambda_1 * x[1]) - exp(lambda_2 * x[1])) +
cos(pi * x[2]) * (exp(s1 * x[1]) - exp(r1 * x[1])) / (exp(-s1) - exp(-r1))
return SVector{1}(u)
l = 4
epsilon = diffusivity() # Note: this requires epsilon < 0.6 due to the sqrt
lambda_1 = (-1 + sqrt(1 - 4 * epsilon * l)) / (-2 * epsilon)
lambda_2 = (-1 - sqrt(1 - 4 * epsilon * l)) / (-2 * epsilon)
r1 = (1 + sqrt(1 + 4 * pi^2 * epsilon^2)) / (2 * epsilon)
s1 = (1 - sqrt(1 + 4 * pi^2 * epsilon^2)) / (2 * epsilon)
u = exp(-l * t) * (exp(lambda_1 * x[1]) - exp(lambda_2 * x[1])) +
cos(pi * x[2]) * (exp(s1 * x[1]) - exp(r1 * x[1])) / (exp(-s1) - exp(-r1))
return SVector{1}(u)
end
initial_condition = initial_condition_erikkson_johnson

# tag different boundary segments
left(x, tol=50*eps()) = abs(x[1] + 1) < tol
right(x, tol=50*eps()) = abs(x[1]) < tol
bottom(x, tol=50*eps()) = abs(x[2] + 0.5) < tol
top(x, tol=50*eps()) = abs(x[2] - 0.5) < tol
entire_boundary(x, tol=50*eps()) = true
left(x, tol = 50 * eps()) = abs(x[1] + 1) < tol
right(x, tol = 50 * eps()) = abs(x[1]) < tol
bottom(x, tol = 50 * eps()) = abs(x[2] + 0.5) < tol
top(x, tol = 50 * eps()) = abs(x[2] - 0.5) < tol
entire_boundary(x, tol = 50 * eps()) = true
is_on_boundary = Dict(:left => left, :right => right, :top => top, :bottom => bottom,
:entire_boundary => entire_boundary)

cells_per_dimension = (16, 16)
mesh = DGMultiMesh(dg, cells_per_dimension;
coordinates_min=(-1.0, -0.5),
coordinates_max=(0.0, 0.5),
coordinates_min = (-1.0, -0.5),
coordinates_max = (0.0, 0.5),
is_on_boundary)

# BC types
boundary_condition = BoundaryConditionDirichlet(initial_condition)

# define inviscid boundary conditions, enforce "do nothing" boundary condition at the outflow
boundary_conditions = (; :left => boundary_condition,
:top => boundary_condition,
:bottom => boundary_condition,
:right => boundary_condition_do_nothing)
boundary_conditions = (; :left => boundary_condition,
:top => boundary_condition,
:bottom => boundary_condition,
:right => boundary_condition_do_nothing)

# define viscous boundary conditions
boundary_conditions_parabolic = (; :entire_boundary => boundary_condition)

semi = SemidiscretizationHyperbolicParabolic(mesh, (equations, equations_parabolic), initial_condition, dg;
boundary_conditions=(boundary_conditions, boundary_conditions_parabolic))
semi = SemidiscretizationHyperbolicParabolic(mesh, (equations, equations_parabolic),
initial_condition, dg;
boundary_conditions = (boundary_conditions,
boundary_conditions_parabolic))

tspan = (0.0, 1.5)
ode = semidiscretize(semi, tspan)

summary_callback = SummaryCallback()
alive_callback = AliveCallback(alive_interval=10)
alive_callback = AliveCallback(alive_interval = 10)
analysis_interval = 100
analysis_callback = AnalysisCallback(semi, interval=analysis_interval, uEltype=real(dg))
analysis_callback = AnalysisCallback(semi, interval = analysis_interval, uEltype = real(dg))
callbacks = CallbackSet(summary_callback, alive_callback)

###############################################################################
# run the simulation

time_int_tol = 1e-8
sol = solve(ode, RDPK3SpFSAL49(); abstol=time_int_tol, reltol=time_int_tol,
ode_default_options()..., callback=callbacks)
sol = solve(ode, RDPK3SpFSAL49(); abstol = time_int_tol, reltol = time_int_tol,
ode_default_options()..., callback = callbacks)
summary_callback() # print the timer summary
12 changes: 6 additions & 6 deletions examples/dgmulti_2d/elixir_advection_diffusion_periodic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ equations = LinearScalarAdvectionEquation2D(0.0, 0.0)
equations_parabolic = LaplaceDiffusion2D(5.0e-1, equations)

function initial_condition_sharp_gaussian(x, t, equations::LinearScalarAdvectionEquation2D)
return SVector(exp(-100 * (x[1]^2 + x[2]^2)))
return SVector(exp(-100 * (x[1]^2 + x[2]^2)))
end
initial_condition = initial_condition_sharp_gaussian

cells_per_dimension = (16, 16)
mesh = DGMultiMesh(dg, cells_per_dimension, periodicity=true)
mesh = DGMultiMesh(dg, cells_per_dimension, periodicity = true)
semi = SemidiscretizationHyperbolicParabolic(mesh, (equations, equations_parabolic),
initial_condition, dg)

tspan = (0.0, 0.1)
ode = semidiscretize(semi, tspan)

summary_callback = SummaryCallback()
alive_callback = AliveCallback(alive_interval=10)
alive_callback = AliveCallback(alive_interval = 10)
analysis_interval = 100
analysis_callback = AnalysisCallback(semi, interval=analysis_interval, uEltype=real(dg))
analysis_callback = AnalysisCallback(semi, interval = analysis_interval, uEltype = real(dg))
callbacks = CallbackSet(summary_callback, alive_callback, analysis_callback)

###############################################################################
# run the simulation

time_int_tol = 1e-6
sol = solve(ode, RDPK3SpFSAL49(); abstol=time_int_tol, reltol=time_int_tol,
dt = time_int_tol, ode_default_options()..., callback=callbacks)
sol = solve(ode, RDPK3SpFSAL49(); abstol = time_int_tol, reltol = time_int_tol,
dt = time_int_tol, ode_default_options()..., callback = callbacks)

summary_callback() # print the timer summary
Loading

0 comments on commit c79fa43

Please sign in to comment.