Skip to content

Commit

Permalink
Use trixi_include from TrixiBase.jl (#50)
Browse files Browse the repository at this point in the history
* use trixi_include from TrixiBase.jl

* fix copy-paste error

* bump compat of Reexport to v1.2

* format
  • Loading branch information
JoshuaLampert authored May 13, 2024
1 parent 1122497 commit 45af2ef
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 51 deletions.
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
ReadVTK = "dc215faf-f008-4882-a9f7-a79a826fadc3"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SimpleUnPack = "ce78b400-467f-4804-87d8-8f486da07d0a"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
TrixiBase = "9a0f1c46-06d5-4909-a5a3-ce25d3fa3284"
TypedPolynomials = "afbbf031-7a57-5f58-a1b9-b774a0fad08d"
WriteVTK = "64499a7a-5c06-52f2-abe2-ccb03c286192"

Expand All @@ -25,11 +27,13 @@ LinearAlgebra = "1"
Printf = "1"
ReadVTK = "0.2"
RecipesBase = "1.3.4"
Reexport = "1.2"
SciMLBase = "2.26"
SimpleUnPack = "1.1"
SpecialFunctions = "2"
StaticArrays = "1"
TimerOutputs = "0.5.23"
TrixiBase = "0.1.1"
TypedPolynomials = "0.4.1"
WriteVTK = "1.18"
julia = "1.10"
4 changes: 3 additions & 1 deletion src/KernelInterpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ using SciMLBase: ODEFunction, ODEProblem, ODESolution, DiscreteCallback, u_modif
using SimpleUnPack: @unpack
using SpecialFunctions: besselk, loggamma
using StaticArrays: StaticArrays, MVector
using Reexport: @reexport
using TimerOutputs: TimerOutputs, TimerOutput, @timeit, print_timer, reset_timer!
@reexport using TrixiBase: trixi_include
using TypedPolynomials: Variable, monomials, degree
using WriteVTK: WriteVTK, vtk_grid, paraview_collection, MeshCell, VTKCellTypes,
CollectionFile
Expand Down Expand Up @@ -46,6 +48,6 @@ export interpolation_kernel, nodeset, coefficients, kernel_coefficients,
TemporalInterpolation
export AliveCallback, SaveSolutionCallback, SummaryCallback
export vtk_save, vtk_read
export examples_dir, get_examples, default_example, include_example
export examples_dir, get_examples, default_example

end
45 changes: 0 additions & 45 deletions src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,51 +39,6 @@ function default_example()
joinpath(examples_dir(), "interpolation", "interpolation_2d.jl")
end

# Note: We can't call the method below `KernelInterpolation.include` since that is created automatically
# inside `module KernelInterpolation` to `include` source files and evaluate them within the global scope
# of `KernelInterpolation`. However, users will want to evaluate in the global scope of `Main` or something
# similar to manage dependencies on their own.
"""
include_example([mod::Module=Main,] example::AbstractString; kwargs...)
`include` the file `example` and evaluate its content in the global scope of module `mod`.
You can override specific assignments in `example` by supplying keyword arguments.
It's basic purpose is to make it easier to modify some parameters while running KernelInterpolation from the
REPL. Additionally, this is used in tests to reduce the computational burden for CI while still
providing examples with sensible default values for users.
Copied from [Trixi.jl](https://github.com/trixi-framework/Trixi.jl).
"""
function include_example(mod::Module, example::AbstractString; kwargs...)
Base.include(ex -> replace_assignments(ex; kwargs...), mod, example)
end

function include_example(example::AbstractString; kwargs...)
include_example(Main, example; kwargs...)
end

# 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)

# 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

# Create `d` polyvars from `TypedPolynomials.jl`, don't use `@polyvars` because of
# https://github.com/JuliaAlgebra/TypedPolynomials.jl/issues/51, instead use the
# workaround from there
Expand Down
3 changes: 2 additions & 1 deletion test/test_aqua.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ using KernelInterpolation
@testset "Aqua.jl" begin
Aqua.test_all(ambiguities = false, KernelInterpolation)
@test isnothing(check_no_implicit_imports(KernelInterpolation))
@test isnothing(check_no_stale_explicit_imports(KernelInterpolation))
@test isnothing(check_no_stale_explicit_imports(KernelInterpolation;
ignore = (:trixi_include,)))
end

end #module
4 changes: 3 additions & 1 deletion test/test_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ using OrdinaryDiffEq: solve, Rodas5P
using StaticArrays: MVector
using Plots

include("test_util.jl")

@testset "Unit tests" begin
@testset "util" begin
@test_nowarn get_examples()
@test_nowarn include_example(default_example(), n = 10)
@test_nowarn_mod trixi_include(default_example(), n = 10)
end

@testset "Kernels" begin
Expand Down
48 changes: 45 additions & 3 deletions test/test_util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ macro test_include_example(example, args...)
local kwargs = Pair{Symbol, Any}[]
for arg in args
if (arg.head == :(=) &&
!(arg.args[1] in (:l2, :linf, :atol, :rtol)))
!(arg.args[1] in (:l2, :linf, :atol, :rtol, :pde_test)))
push!(kwargs, Pair(arg.args...))
end
end
Expand All @@ -24,7 +24,7 @@ macro test_include_example(example, args...)
println($example)

# evaluate examples in the scope of the module they're called from
@test_nowarn include_example(@__MODULE__, $example; $kwargs...)
@test_nowarn_mod trixi_include(@__MODULE__, $example; $kwargs...)
# if present, compare l2 and linf against reference values
if !isnothing($l2) || !isnothing($linf)
if !$pde_test
Expand Down Expand Up @@ -88,6 +88,48 @@ function get_kwarg(args, keyword, default_value)
return val
end

# Copied from TrixiBase.jl. See https://github.com/trixi-framework/TrixiBase.jl/issues/9.
"""
@test_nowarn_mod expr
Modified version of `@test_nowarn expr` that prints the content of `stderr` when
it is not empty and ignores some common info statements printed in KernelInterpolation.jl
uses.
"""
macro test_nowarn_mod(expr, additional_ignore_content = String[])
quote
let fname = tempname()
try
ret = open(fname, "w") do f
redirect_stderr(f) do
$(esc(expr))
end
end
stderr_content = read(fname, String)
if !isempty(stderr_content)
println("Content of `stderr`:\n", stderr_content)
end

# Patterns matching the following ones will be ignored. Additional patterns
# passed as arguments can also be regular expressions, so we just use the
# type `Any` for `ignore_content`.
ignore_content = Any["[ Info: You just called `trixi_include`. Julia may now compile the code, please be patient.\n"]
append!(ignore_content, $additional_ignore_content)
for pattern in ignore_content
stderr_content = replace(stderr_content, pattern => "")
end

# We also ignore simple module redefinitions for convenience. Thus, we
# check whether every line of `stderr_content` is of the form of a
# module replacement warning.
@test occursin(r"^(WARNING: replacing module .+\.\n)*$", stderr_content)
ret
finally
rm(fname, force = true)
end
end
end
end

"""
@ki_testset "name of the testset" #= code to test #=
Expand All @@ -102,7 +144,7 @@ macro ki_testset(name, expr)
@eval module $mod
using Test
using KernelInterpolation
using LinearAlgebra: norm # We use `norm` is all `@ki_testset`s
using LinearAlgebra: norm # We use `norm` in all `@ki_testset`s
include(@__FILE__)
# We define `EXAMPLES_DIR` in (nearly) all test modules and use it to
# get the path to the examples to be tested. However, that's not required
Expand Down

0 comments on commit 45af2ef

Please sign in to comment.