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

Use trixi_include from TrixiBase.jl #1832

Merged
merged 12 commits into from
Feb 15, 2024
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ TimerOutputs = "0.5.7"
Triangulate = "2.0"
TriplotBase = "0.1"
TriplotRecipes = "0.1"
TrixiBase = "0.1"
julia = "1.8"

[extras]
Expand Down
2 changes: 1 addition & 1 deletion docs/src/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ will yield the following plot:
* The [`VisualizationCallback`](@ref) can be used for in-situ visualization. See
[Visualizing results during a simulation](@ref).
* The [`TrivialCallback`](@ref) does nothing and can be used to easily disable some callbacks
via [`trixi_include`](@ref).
via [`trixi_include`](https://trixi-framework.github.io/TrixiBase.jl/stable/reference/#TrixiBase.trixi_include-Tuple{Module,%20AbstractString}).

### Equation-specific callbacks
Some callbacks provided by Trixi.jl implement specific features for certain equations:
Expand Down
10 changes: 6 additions & 4 deletions docs/src/conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ Trixi.jl is distributed with several examples in the form of elixirs, small
Julia scripts containing everything to set up and run a simulation. Working
interactively from the Julia REPL with these scripts can be quite convenient
while for exploratory research and development of Trixi.jl. For example, you
can use the convenience function [`trixi_include`](@ref) to `include` an elixir
with some modified arguments. To enable this, it is helpful to use a consistent
naming scheme in elixirs, since [`trixi_include`](@ref) can only perform simple
replacements. Some standard variables names are
can use the convenience function
[`trixi_include`](https://trixi-framework.github.io/TrixiBase.jl/stable/reference/#TrixiBase.trixi_include-Tuple{Module,%20AbstractString})
to `include` an elixir with some modified arguments. To enable this, it is
helpful to use a consistent naming scheme in elixirs, since
[`trixi_include`](https://trixi-framework.github.io/TrixiBase.jl/stable/reference/#TrixiBase.trixi_include-Tuple{Module,%20AbstractString})
can only perform simple replacements. Some standard variables names are

- `polydeg` for the polynomial degree of a solver
- `surface_flux` for the numerical flux at surfaces
Expand Down
79 changes: 4 additions & 75 deletions src/auxiliary/special_elixirs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Run `iterations` Trixi.jl simulations using the setup given in `elixir` and comp
the experimental order of convergence (EOC) in the ``L^2`` and ``L^\\infty`` norm.
In each iteration, the resolution of the respective mesh will be doubled.
Additional keyword arguments `kwargs...` and the optional module `mod` are passed directly
to [`trixi_include`](@ref).
to [`trixi_include`](https://trixi-framework.github.io/TrixiBase.jl/stable/reference/#TrixiBase.trixi_include-Tuple{Module,%20AbstractString}).

This function assumes that the spatial resolution is set via the keywords
`initial_refinement_level` (an integer) or `cells_per_dimension` (a tuple of
Expand Down Expand Up @@ -125,86 +125,15 @@ end

# Helper methods used in the functions defined above

# Apply the function `f` to `expr` and all sub-expressions recursively.
walkexpr(f, expr::Expr) = f(Expr(expr.head, (walkexpr(f, arg) for arg in expr.args)...))
walkexpr(f, x) = f(x)

# Insert the keyword argument `maxiters` into calls to `solve` and `Trixi.solve`
# with default value `10^5` if it is not already present.
function insert_maxiters(expr)
maxiters_default = 10^5

expr = walkexpr(expr) do x
if x isa Expr
is_plain_solve = x.head === Symbol("call") && x.args[1] === Symbol("solve")
is_trixi_solve = (x.head === Symbol("call") && x.args[1] isa Expr &&
x.args[1].head === Symbol(".") &&
x.args[1].args[1] === Symbol("Trixi") &&
x.args[1].args[2] isa QuoteNode &&
x.args[1].args[2].value === Symbol("solve"))

if is_plain_solve || is_trixi_solve
# Do nothing if `maxiters` is already set as keyword argument...
for arg in x.args
# This detects the case where `maxiters` is set as keyword argument
# without or before a semicolon
if (arg isa Expr && arg.head === Symbol("kw") &&
arg.args[1] === Symbol("maxiters"))
return x
end

# This detects the case where maxiters is set as keyword argument
# after a semicolon
if (arg isa Expr && arg.head === Symbol("parameters"))
# We need to check each keyword argument listed here
for nested_arg in arg.args
if (nested_arg isa Expr &&
nested_arg.head === Symbol("kw") &&
nested_arg.args[1] === Symbol("maxiters"))
return x
end
end
end
end

# ...and insert it otherwise.
push!(x.args, Expr(Symbol("kw"), Symbol("maxiters"), maxiters_default))
end
end
return x
end

return expr
end

# Replace assignments to `key` in `expr` by `key = val` for all `(key,val)` in `kwargs`.
function replace_assignments(expr; kwargs...)
# replace explicit and keyword assignments
expr = walkexpr(expr) do x
if x isa Expr
for (key, val) in kwargs
if (x.head === Symbol("=") || x.head === :kw) &&
x.args[1] === Symbol(key)
x.args[2] = :($val)
# dump(x)
end
end
end
return x
end

return expr
end

# find a (keyword or common) assignment to `destination` in `expr`
# and return the assigned value
# Find a (keyword or common) assignment to `destination` in `expr`
# and return the assigned value.
function find_assignment(expr, destination)
# declare result to be able to assign to it in the closure
local result
found = false

# find explicit and keyword assignments
walkexpr(expr) do x
TrixiBase.walkexpr(expr) do x
if x isa Expr
if (x.head === Symbol("=") || x.head === :kw) &&
x.args[1] === Symbol(destination)
Expand Down
4 changes: 2 additions & 2 deletions src/callbacks_step/trivial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"""
TrivialCallback()

A callback that does nothing. This can be useful to disable some callbacks
easily via [`trixi_include`](@ref).
A callback that does nothing. This can be useful to disable some callbacks easily via
[`trixi_include`](https://trixi-framework.github.io/TrixiBase.jl/stable/reference/#TrixiBase.trixi_include-Tuple{Module,%20AbstractString}).
"""
function TrivialCallback()
DiscreteCallback(trivial_callback, trivial_callback,
Expand Down
Loading