changeset 826:4433be383840 operator_storage_array_of_table

Add stencil constructor to change the type of the weights along with a convert method
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 21 Dec 2021 16:30:16 +0100
parents 69700b0b1452
children beae513ef8b3
files src/SbpOperators/stencil.jl test/SbpOperators/stencil_test.jl
diffstat 2 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/SbpOperators/stencil.jl	Tue Dec 21 16:02:11 2021 +0100
+++ b/src/SbpOperators/stencil.jl	Tue Dec 21 16:30:16 2021 +0100
@@ -22,6 +22,12 @@
     return Stencil(range, weights)
 end
 
+function Stencil{T}(s::Stencil) where T
+    return Stencil(s.range, T.(s.weights))
+end
+
+Base.convert(::Type{Stencil{T}}, stencil) where T = Stencil{T}(stencil)
+
 function CenteredStencil(weights::Vararg)
     if iseven(length(weights))
         throw(ArgumentError("a centered stencil must have an odd number of weights."))
--- a/test/SbpOperators/stencil_test.jl	Tue Dec 21 16:02:11 2021 +0100
+++ b/test/SbpOperators/stencil_test.jl	Tue Dec 21 16:30:16 2021 +0100
@@ -15,4 +15,17 @@
 
     @test CenteredStencil(1,2,3,4,5) == Stencil((-2, 2), (1,2,3,4,5))
     @test_throws ArgumentError CenteredStencil(1,2,3,4)
+
+    # Changing the type of the weights
+    @test Stencil{Float64}(Stencil(1,2,3,4,5; center=2)) == Stencil(1.,2.,3.,4.,5.; center=2)
+    @test Stencil{Float64}(CenteredStencil(1,2,3,4,5)) == CenteredStencil(1.,2.,3.,4.,5.)
+    @test Stencil{Int}(Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1,2,3,4,5; center=2)
+    @test Stencil{Rational}(Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1//1,2//1,3//1,4//1,5//1; center=2)
+
+    @testset "convert" begin
+        @test convert(Stencil{Float64}, Stencil(1,2,3,4,5; center=2)) == Stencil(1.,2.,3.,4.,5.; center=2)
+        @test convert(Stencil{Float64}, CenteredStencil(1,2,3,4,5)) == CenteredStencil(1.,2.,3.,4.,5.)
+        @test convert(Stencil{Int}, Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1,2,3,4,5; center=2)
+        @test convert(Stencil{Rational}, Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1//1,2//1,3//1,4//1,5//1; center=2)
+    end
 end