Mercurial > repos > public > sbplib_julia
comparison test/Grids/geometry_test.jl @ 2003:524a52f190d7 feature/sbp_operators/laplace_curvilinear
Merge feature/grids/geometry_functions
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 29 Apr 2025 09:03:05 +0200 |
parents | 486b3c6f919e |
children | cf0fa2967361 |
comparison
equal
deleted
inserted
replaced
1962:496b8d3903c6 | 2003:524a52f190d7 |
---|---|
1 using Diffinitive.Grids | |
2 using Diffinitive.Grids: Line, LineSegment, linesegments, polygon_edges, Circle, TransfiniteInterpolationSurface, check_transfiniteinterpolation | |
3 using StaticArrays | |
4 | |
1 @testset "Line" begin | 5 @testset "Line" begin |
2 @test_broken false | 6 @testset "Constructors" begin |
7 @test Line([1,2],[2,3]) isa Line{SVector{2,Int}} | |
8 @test Line((1,2),(2,3)) isa Line{SVector{2,Int}} | |
9 @test Line(@SVector[1,2],[2,3]) isa Line{SVector{2,Int}} | |
10 @test Line(@SVector[1,2],@SVector[2,3]) isa Line{SVector{2,Int}} | |
11 | |
12 @test Line([1,2],[2.,3]) isa Line{SVector{2,Float64}} | |
13 @test Line(@SVector[1,2.],@SVector[2,3]) isa Line{SVector{2,Float64}} | |
14 @test Line((1,2.),(2,3)) isa Line{SVector{2,Float64}} | |
15 end | |
16 | |
17 @testset "Evaluation" begin | |
18 l = Line([1,2],[2,3]) | |
19 | |
20 @test l(0) == [1,2] | |
21 @test l(1) == [1,2] + [2,3] | |
22 @test l(1/2) == [1,2] + [2,3]/2 | |
23 end | |
24 | |
25 @testset "Grids.jacobian" begin | |
26 l = Line([1,2],[2,3]) | |
27 | |
28 @test Grids.jacobian(l,0) == [2,3] | |
29 @test Grids.jacobian(l,1) == [2,3] | |
30 @test Grids.jacobian(l,1/2) == [2,3] | |
31 end | |
3 end | 32 end |
4 | 33 |
5 @testset "LineSegment" begin | 34 @testset "LineSegment" begin |
6 @test_broken false | 35 @testset "Constructors" begin |
36 @test LineSegment([1,2],[2,3]) isa LineSegment{SVector{2,Int}} | |
37 @test LineSegment((1,2),(2,3)) isa LineSegment{SVector{2,Int}} | |
38 @test LineSegment(@SVector[1,2],[2,3]) isa LineSegment{SVector{2,Int}} | |
39 @test LineSegment(@SVector[1,2],@SVector[2,3]) isa LineSegment{SVector{2,Int}} | |
40 | |
41 @test LineSegment([1,2],[2.,3]) isa LineSegment{SVector{2,Float64}} | |
42 @test LineSegment(@SVector[1,2.],@SVector[2,3]) isa LineSegment{SVector{2,Float64}} | |
43 @test LineSegment((1,2.),(2,3)) isa LineSegment{SVector{2,Float64}} | |
44 end | |
45 | |
46 @testset "Evaluation" begin | |
47 l = LineSegment([1,2],[2,3]) | |
48 | |
49 @test l(0) == [1,2] | |
50 @test l(1) == [2,3] | |
51 @test l(1/2) == [1,2]/2 + [2,3]/2 | |
52 end | |
53 | |
54 @testset "Grids.jacobian" begin | |
55 a, b = [1,2], [2,3] | |
56 l = LineSegment(a,b) | |
57 d = b-a | |
58 | |
59 @test Grids.jacobian(l,0) == d | |
60 @test Grids.jacobian(l,1) == d | |
61 @test Grids.jacobian(l,1/2) == d | |
62 end | |
7 end | 63 end |
8 | 64 |
9 @testset "linesegments" begin | 65 @testset "linesegments" begin |
10 @test_broken false | 66 a,b,c,d = [1,1],[2,2],[3,3],[4,4] |
67 @test linesegments(a,b) == [ | |
68 LineSegment(a,b), | |
69 ] | |
70 | |
71 @test linesegments(a,b,c) == [ | |
72 LineSegment(a,b), | |
73 LineSegment(b,c), | |
74 ] | |
75 | |
76 @test linesegments(a,b,c,d) == [ | |
77 LineSegment(a,b), | |
78 LineSegment(b,c), | |
79 LineSegment(c,d), | |
80 ] | |
11 end | 81 end |
12 | 82 |
13 @testset "polygon_edges" begin | 83 @testset "polygon_edges" begin |
14 @test_broken false | 84 a,b,c,d = [1,1],[2,2],[3,3],[4,4] |
85 @test polygon_edges(a,b) == [ | |
86 LineSegment(a,b), | |
87 LineSegment(b,a), | |
88 ] | |
89 | |
90 @test polygon_edges(a,b,c) == [ | |
91 LineSegment(a,b), | |
92 LineSegment(b,c), | |
93 LineSegment(c,a), | |
94 ] | |
95 | |
96 @test polygon_edges(a,b,c,d) == [ | |
97 LineSegment(a,b), | |
98 LineSegment(b,c), | |
99 LineSegment(c,d), | |
100 LineSegment(d,a), | |
101 ] | |
15 end | 102 end |
16 | 103 |
17 @testset "Circle" begin | 104 @testset "Circle" begin |
18 @test_broken false | 105 @testset "Constructors" begin |
106 @test Circle([1,2], 1) isa Circle{SVector{2,Int},Int} | |
107 @test Circle([1,2], 1.) isa Circle{SVector{2,Int},Float64} | |
108 @test Circle([1,2.], 1.) isa Circle{SVector{2,Float64},Float64} | |
109 @test Circle([1,2.], 1) isa Circle{SVector{2,Float64},Int} | |
110 @test Circle((1,2.), 1.) isa Circle{SVector{2,Float64},Float64} | |
111 @test Circle((1,2), 1.) isa Circle{SVector{2,Int},Float64} | |
112 @test Circle((1.,2), 1) isa Circle{SVector{2,Float64},Int} | |
113 @test Circle((1,2), 1) isa Circle{SVector{2,Int},Int} | |
114 @test Circle(@SVector[1,2], 1.) isa Circle{SVector{2,Int},Float64} | |
115 @test Circle(@SVector[1,2.], 1.) isa Circle{SVector{2,Float64},Float64} | |
116 end | |
117 | |
118 @testset "Evaluation" begin | |
119 c = Circle([0,0], 1) | |
120 @test c(0) ≈ [1,0] | |
121 @test c(π/2) ≈ [0,1] | |
122 @test c(π) ≈ [-1,0] | |
123 @test c(3π/2) ≈ [0,-1] | |
124 @test c(π/4) ≈ [1/√(2),1/√(2)] | |
125 | |
126 c = Circle([0,0], 2) | |
127 @test c(0) ≈ [2,0] | |
128 @test c(π/2) ≈ [0,2] | |
129 @test c(π) ≈ [-2,0] | |
130 @test c(3π/2) ≈ [0,-2] | |
131 @test c(π/4) ≈ [√(2),√(2)] | |
132 end | |
133 | |
134 @testset "Grids.jacobian" begin | |
135 c = Circle([0,0], 1) | |
136 @test Grids.jacobian(c, 0) ≈ [0,1] | |
137 @test Grids.jacobian(c, π/2) ≈ [-1,0] | |
138 @test Grids.jacobian(c, π) ≈ [0,-1] | |
139 @test Grids.jacobian(c, 3π/2) ≈ [1,0] | |
140 @test Grids.jacobian(c, π/4) ≈ [-1/√(2),1/√(2)] | |
141 | |
142 c = Circle([0,0], 2) | |
143 @test Grids.jacobian(c, 0) ≈ [0,2] | |
144 @test Grids.jacobian(c, π/2) ≈ [-2,0] | |
145 @test Grids.jacobian(c, π) ≈ [0,-2] | |
146 @test Grids.jacobian(c, 3π/2) ≈ [2,0] | |
147 @test Grids.jacobian(c, π/4) ≈ [-√(2),√(2)] | |
148 | |
149 c = Circle([-1,1], 1) | |
150 @test Grids.jacobian(c, 0) ≈ [0,1] | |
151 @test Grids.jacobian(c, π/2) ≈ [-1,0] | |
152 @test Grids.jacobian(c, π) ≈ [0,-1] | |
153 @test Grids.jacobian(c, 3π/2) ≈ [1,0] | |
154 @test Grids.jacobian(c, π/4) ≈ [-1/√(2),1/√(2)] | |
155 | |
156 c = Circle([-1,1], 2) | |
157 @test Grids.jacobian(c, 0) ≈ [0,2] | |
158 @test Grids.jacobian(c, π/2) ≈ [-2,0] | |
159 @test Grids.jacobian(c, π) ≈ [0,-2] | |
160 @test Grids.jacobian(c, 3π/2) ≈ [2,0] | |
161 @test Grids.jacobian(c, π/4) ≈ [-√(2),√(2)] | |
162 end | |
19 end | 163 end |
20 | 164 |
21 @testset "TransfiniteInterpolationSurface" begin | 165 @testset "TransfiniteInterpolationSurface" begin |
22 @test_broken false | 166 @testset "Constructors" begin |
23 end | 167 @test TransfiniteInterpolationSurface(t->[1,2], t->[2,1], t->[0,0], t->[1,1]) isa TransfiniteInterpolationSurface |
168 | |
169 cs = polygon_edges([0,0],[1,0],[1,1],[0,1]) | |
170 @test TransfiniteInterpolationSurface(cs...) isa TransfiniteInterpolationSurface | |
171 end | |
172 | |
173 @testset "Evaluation" begin | |
174 cs = polygon_edges([0,0],[1,0],[1,1],[0,1]) | |
175 ti = TransfiniteInterpolationSurface(cs...) | |
176 | |
177 @test ti(0,0) == [0,0] | |
178 @test ti([0,0]) == [0,0] | |
179 @test ti(1,0) == [1,0] | |
180 @test ti([1,0]) == [1,0] | |
181 @test ti(1,1) == [1,1] | |
182 @test ti([1,1]) == [1,1] | |
183 @test ti(0,1) == [0,1] | |
184 @test ti([0,1]) == [0,1] | |
185 | |
186 @test ti(1/2, 0) == [1/2, 0] | |
187 @test ti(1/2, 1) == [1/2, 1] | |
188 @test ti(0,1/2) == [0,1/2] | |
189 @test ti(1,1/2) == [1,1/2] | |
190 | |
191 | |
192 a, b, c, d = [1,0],[2,1/4],[2.5,1],[-1/3,1] | |
193 cs = polygon_edges(a,b,c,d) | |
194 ti = TransfiniteInterpolationSurface(cs...) | |
195 | |
196 @test ti(0,0) == a | |
197 @test ti(1,0) == b | |
198 @test ti(1,1) == c | |
199 @test ti(0,1) == d | |
200 | |
201 @test ti(1/2, 0) == (a+b)/2 | |
202 @test ti(1/2, 1) == (c+d)/2 | |
203 @test ti(0, 1/2) == (a+d)/2 | |
204 @test ti(1, 1/2) == (b+c)/2 | |
205 | |
206 # TODO: Some test with curved edges? | |
207 end | |
208 | |
209 @testset "check_transfiniteinterpolation" begin | |
210 cs = polygon_edges([0,0],[1,0],[1,1],[0,1]) | |
211 ti = TransfiniteInterpolationSurface(cs...) | |
212 | |
213 @test check_transfiniteinterpolation(ti) == nothing | |
214 @test check_transfiniteinterpolation(Bool, ti) == true | |
215 | |
216 bad_sides = [ | |
217 LineSegment([0,0],[1/2,0]), | |
218 LineSegment([1,0],[1,1/2]), | |
219 LineSegment([1,1],[0,1/2]), | |
220 LineSegment([0,1],[0,1/2]), | |
221 ] | |
222 | |
223 s1 = TransfiniteInterpolationSurface(bad_sides[1],cs[2],cs[3],cs[4]) | |
224 s2 = TransfiniteInterpolationSurface(cs[1],bad_sides[2],cs[3],cs[4]) | |
225 s3 = TransfiniteInterpolationSurface(cs[1],cs[2],bad_sides[3],cs[4]) | |
226 s4 = TransfiniteInterpolationSurface(cs[1],cs[2],cs[3],bad_sides[4]) | |
227 | |
228 @test check_transfiniteinterpolation(Bool, s1) == false | |
229 @test check_transfiniteinterpolation(Bool, s2) == false | |
230 @test check_transfiniteinterpolation(Bool, s3) == false | |
231 @test check_transfiniteinterpolation(Bool, s4) == false | |
232 | |
233 @test_throws ArgumentError check_transfiniteinterpolation(s1) | |
234 @test_throws ArgumentError check_transfiniteinterpolation(s2) | |
235 @test_throws ArgumentError check_transfiniteinterpolation(s3) | |
236 @test_throws ArgumentError check_transfiniteinterpolation(s4) | |
237 end | |
238 | |
239 @testset "Grids.jacobian" begin | |
240 cs = polygon_edges([0,0],[1,0],[1,1],[0,1]) | |
241 ti = TransfiniteInterpolationSurface(cs...) | |
242 | |
243 @test Grids.jacobian(ti, [0,0]) isa SMatrix | |
244 | |
245 @test Grids.jacobian(ti, [0,0]) == [1 0; 0 1] | |
246 @test Grids.jacobian(ti, [1,0]) == [1 0; 0 1] | |
247 @test Grids.jacobian(ti, [1,1]) == [1 0; 0 1] | |
248 @test Grids.jacobian(ti, [0,1]) == [1 0; 0 1] | |
249 | |
250 @test Grids.jacobian(ti, [1/2, 0]) == [1 0; 0 1] | |
251 @test Grids.jacobian(ti, [1/2, 1]) == [1 0; 0 1] | |
252 @test Grids.jacobian(ti, [0,1/2]) == [1 0; 0 1] | |
253 @test Grids.jacobian(ti, [1,1/2]) == [1 0; 0 1] | |
254 | |
255 | |
256 a, b, c, d = [1,0],[2,1/4],[2.5,1],[-1/3,1] | |
257 cs = polygon_edges(a,b,c,d) | |
258 ti = TransfiniteInterpolationSurface(cs...) | |
259 | |
260 @test Grids.jacobian(ti, [0,0]) == [b-a d-a] | |
261 @test Grids.jacobian(ti, [1,0]) == [b-a c-b] | |
262 @test Grids.jacobian(ti, [1,1]) == [c-d c-b] | |
263 @test Grids.jacobian(ti, [0,1]) == [c-d d-a] | |
264 | |
265 | |
266 mid(x,y) = (x+y)/2 | |
267 @test Grids.jacobian(ti, [1/2, 0]) ≈ [b-a mid(c,d)-mid(a,b)] | |
268 @test Grids.jacobian(ti, [1/2, 1]) ≈ [c-d mid(c,d)-mid(a,b)] | |
269 @test Grids.jacobian(ti, [0, 1/2]) ≈ [mid(b,c)-mid(a,d) d-a] | |
270 @test Grids.jacobian(ti, [1, 1/2]) ≈ [mid(b,c)-mid(a,d) c-b] | |
271 | |
272 # TODO: Some test with curved edges? | |
273 end | |
274 end |