Mercurial > repos > public > sbplib_julia
annotate src/Grids/manifolds.jl @ 1869:20bd8887db0d feature/grids/manifolds
Fix Aqua and JET fails
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Fri, 24 Jan 2025 23:40:28 +0100 |
parents | de4b4f2aee4f |
children | 04c251bccbd4 |
rev | line source |
---|---|
1558 | 1 """ |
2 ParameterSpace{D} | |
3 | |
4 A space of parameters of dimension `D`. Used with `Chart` to indicate which | |
5 parameters are valid for that chart. | |
6 | |
7 Common parameter spaces are created using the functions unit sized spaces | |
8 * `unitinterval` | |
9 * `unitrectangle` | |
10 * `unitbox` | |
11 * `unittriangle` | |
12 * `unittetrahedron` | |
13 * `unithyperbox` | |
14 * `unitsimplex` | |
15 | |
16 See also: [`Interval`](@ref), [`Rectangle`](@ref), [`Box`](@ref), | |
17 [`Triangle`](@ref), [`Tetrahedron`](@ref), [`HyperBox`](@ref), | |
18 [`Simplex`](@ref), | |
19 """ | |
20 abstract type ParameterSpace{D} end | |
1580
fdee60ab8c4e
Add ndims method for ParameterSpace
Jonatan Werpers <jonatan@werpers.com>
parents:
1579
diff
changeset
|
21 Base.ndims(::ParameterSpace{D}) where D = D |
1558 | 22 |
1779
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
23 struct Interval{T} <: ParameterSpace{1} |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
24 a::T |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
25 b::T |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
26 |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
27 function Interval(a,b) |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
28 a, b = promote(a, b) |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
29 new{typeof(a)}(a,b) |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
30 end |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
31 end |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
32 |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
33 limits(i::Interval) = (i.a, i.b) |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
34 |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
35 unitinterval(T=Float64) = Interval(zero(T), one(T)) |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
36 |
2a8a2b52a112
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
Jonatan Werpers <jonatan@werpers.com>
parents:
1644
diff
changeset
|
37 |
1558 | 38 struct HyperBox{T,D} <: ParameterSpace{D} |
39 a::SVector{D,T} | |
40 b::SVector{D,T} | |
41 end | |
42 | |
43 function HyperBox(a,b) | |
1780
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
44 ET = promote_type(eltype(a),eltype(b)) |
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
45 T = SVector{length(a),ET} |
1558 | 46 HyperBox(convert(T,a), convert(T,b)) |
47 end | |
48 | |
49 Rectangle{T} = HyperBox{T,2} | |
50 Box{T} = HyperBox{T,3} | |
51 | |
52 limits(box::HyperBox, d) = (box.a[d], box.b[d]) | |
53 limits(box::HyperBox) = (box.a, box.b) | |
54 | |
55 unitsquare(T=Float64) = unithyperbox(T,2) | |
56 unitcube(T=Float64) = unithyperbox(T,3) | |
57 unithyperbox(T, D) = HyperBox((@SVector zeros(T,D)), (@SVector ones(T,D))) | |
58 unithyperbox(D) = unithyperbox(Float64,D) | |
59 | |
60 | |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
61 struct Simplex{T,D,NV} <: ParameterSpace{D} |
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
62 verticies::NTuple{NV,SVector{D,T}} |
1869
20bd8887db0d
Fix Aqua and JET fails
Jonatan Werpers <jonatan@werpers.com>
parents:
1867
diff
changeset
|
63 |
20bd8887db0d
Fix Aqua and JET fails
Jonatan Werpers <jonatan@werpers.com>
parents:
1867
diff
changeset
|
64 Simplex(verticies::Tuple{SVector{D,T}, Vararg{SVector{D,T},N}}) where {T,D,N} = new{T,D,N+1}(verticies) |
20bd8887db0d
Fix Aqua and JET fails
Jonatan Werpers <jonatan@werpers.com>
parents:
1867
diff
changeset
|
65 Simplex(::Tuple{}) = throw(ArgumentError("Must provide at least one vertex.")) |
1558 | 66 end |
67 | |
1780
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
68 function Simplex(verticies::Vararg{AbstractArray}) |
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
69 ET = mapreduce(eltype,promote_type,verticies) |
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
70 T = SVector{length(verticies[1]),ET} |
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
71 |
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
72 return Simplex(Tuple(convert(T,v) for v ∈ verticies)) |
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
73 end |
1564
35fe4375b35f
Export things and fix ConcreteChart and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1558
diff
changeset
|
74 |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
75 verticies(s::Simplex) = s.verticies |
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
76 |
1558 | 77 Triangle{T} = Simplex{T,2} |
78 Tetrahedron{T} = Simplex{T,3} | |
79 | |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
80 unittriangle(T=Float64) = unitsimplex(T,2) |
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
81 unittetrahedron(T=Float64) = unitsimplex(T,3) |
1558 | 82 function unitsimplex(T,D) |
83 z = @SVector zeros(T,D) | |
84 unitelement = one(eltype(z)) | |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
85 verticies = ntuple(i->setindex(z, unitelement, i), D) |
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
86 return Simplex((z,verticies...)) |
1558 | 87 end |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
88 unitsimplex(D) = unitsimplex(Float64, D) |
1558 | 89 |
90 """ | |
1581
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
91 Chart{D} |
1558 | 92 |
93 A parametrized description of a manifold or part of a manifold. | |
94 """ | |
1581
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
95 struct Chart{D, PST<:ParameterSpace{D}, MT} |
1564
35fe4375b35f
Export things and fix ConcreteChart and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1558
diff
changeset
|
96 mapping::MT |
1558 | 97 parameterspace::PST |
98 end | |
99 | |
1781
a73838c9ef94
Let Chart implement Base.ndims instead of domain_dim
Jonatan Werpers <jonatan@werpers.com>
parents:
1780
diff
changeset
|
100 Base.ndims(::Chart{D}) where D = D |
1581
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
101 (c::Chart)(ξ) = c.mapping(ξ) |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
102 parameterspace(c::Chart) = c.parameterspace |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
103 |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
104 """ |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
105 jacobian(c::Chart, ξ) |
1558 | 106 |
1581
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
107 The jacobian of the mapping evaluated at `ξ`. This defers to the |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
108 implementation of `jacobian` for the mapping itself. If no implementation is |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
109 available one can easily be specified for either the mapping function or the |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
110 chart itself. |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
111 ```julia |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
112 c = Chart(f, ps) |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
113 jacobian(f::typeof(f), ξ) = f′(ξ) |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
114 ``` |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
115 or |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
116 ```julia |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
117 c = Chart(f, ps) |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
118 jacobian(c::typeof(c),ξ) = f′(ξ) |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
119 ``` |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
120 which will both allow calling `jacobian(c,ξ)`. |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
121 """ |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
122 jacobian(c::Chart, ξ) = jacobian(c.mapping, ξ) |
1644
e213bd857f3f
Add some todos and tbds
Jonatan Werpers <jonatan@werpers.com>
parents:
1625
diff
changeset
|
123 # TBD: Can we register a error hint for when jacobian is called with a function that doesn't have a registered jacobian? |
1581
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
124 |
1558 | 125 |
1867
de4b4f2aee4f
Add some tests for CartesianAtlas
Jonatan Werpers <jonatan@werpers.com>
parents:
1844
diff
changeset
|
126 # TBD: Should Charts, parameterspaces, Atlases, have boundary names? |
1644
e213bd857f3f
Add some todos and tbds
Jonatan Werpers <jonatan@werpers.com>
parents:
1625
diff
changeset
|
127 |
1558 | 128 """ |
129 Atlas | |
130 | |
131 A collection of charts and their connections. | |
1844
1987347752ef
Add stub for connections(::Atlas)
Jonatan Werpers <jonatan@werpers.com>
parents:
1782
diff
changeset
|
132 Should implement methods for `charts` and `connections`. |
1558 | 133 """ |
134 abstract type Atlas end | |
135 | |
136 """ | |
137 charts(::Atlas) | |
138 | |
139 The colloction of charts in the atlas. | |
140 """ | |
141 function charts end | |
142 | |
143 """ | |
1844
1987347752ef
Add stub for connections(::Atlas)
Jonatan Werpers <jonatan@werpers.com>
parents:
1782
diff
changeset
|
144 connections(::Atlas) |
1558 | 145 |
146 TBD: What exactly should this return? | |
147 """ | |
1844
1987347752ef
Add stub for connections(::Atlas)
Jonatan Werpers <jonatan@werpers.com>
parents:
1782
diff
changeset
|
148 function connections end |
1558 | 149 |
150 struct CartesianAtlas <: Atlas | |
151 charts::Matrix{Chart} | |
152 end | |
153 | |
154 charts(a::CartesianAtlas) = a.charts | |
1782
614f731af685
Add stubs for connections method
Jonatan Werpers <jonatan@werpers.com>
parents:
1781
diff
changeset
|
155 connections(a::CartesianAtlas) = nothing |
1558 | 156 |
157 struct UnstructuredAtlas <: Atlas | |
158 charts::Vector{Chart} | |
159 connections | |
160 end | |
161 | |
162 charts(a::UnstructuredAtlas) = a.charts | |
1782
614f731af685
Add stubs for connections method
Jonatan Werpers <jonatan@werpers.com>
parents:
1781
diff
changeset
|
163 connections(a::UnstructuredAtlas) = nothing |
1558 | 164 |
165 | |
166 ### | |
167 # Geometry | |
168 ### | |
169 | |
170 abstract type Curve end | |
171 abstract type Surface end | |
172 | |
173 | |
174 struct Line{PT} <: Curve | |
175 p::PT | |
176 tangent::PT | |
177 end | |
178 | |
179 (c::Line)(s) = c.p + s*c.tangent | |
180 | |
181 | |
182 struct LineSegment{PT} <: Curve | |
183 a::PT | |
184 b::PT | |
185 end | |
186 | |
187 (c::LineSegment)(s) = (1-s)*c.a + s*c.b | |
188 | |
189 | |
1625
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
190 function linesegments(ps...) |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
191 return [LineSegment(ps[i], ps[i+1]) for i ∈ 1:length(ps)-1] |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
192 end |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
193 |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
194 |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
195 function polygon_edges(ps...) |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
196 n = length(ps) |
1869
20bd8887db0d
Fix Aqua and JET fails
Jonatan Werpers <jonatan@werpers.com>
parents:
1867
diff
changeset
|
197 return [LineSegment(ps[i], ps[mod1(i+1,n)]) for i ∈ eachindex(ps)] |
1625
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
198 end |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
199 |
1558 | 200 struct Circle{T,PT} <: Curve |
201 c::PT | |
202 r::T | |
203 end | |
204 | |
1869
20bd8887db0d
Fix Aqua and JET fails
Jonatan Werpers <jonatan@werpers.com>
parents:
1867
diff
changeset
|
205 function (C::Circle)(θ) |
20bd8887db0d
Fix Aqua and JET fails
Jonatan Werpers <jonatan@werpers.com>
parents:
1867
diff
changeset
|
206 (;c, r) = C |
20bd8887db0d
Fix Aqua and JET fails
Jonatan Werpers <jonatan@werpers.com>
parents:
1867
diff
changeset
|
207 c + r*@SVector[cos(θ), sin(θ)] |
20bd8887db0d
Fix Aqua and JET fails
Jonatan Werpers <jonatan@werpers.com>
parents:
1867
diff
changeset
|
208 end |
1558 | 209 |
210 struct TransfiniteInterpolationSurface{T1,T2,T3,T4} <: Surface | |
211 c₁::T1 | |
212 c₂::T2 | |
213 c₃::T3 | |
214 c₄::T4 | |
215 end | |
216 | |
217 function (s::TransfiniteInterpolationSurface)(u,v) | |
218 c₁, c₂, c₃, c₄ = s.c₁, s.c₂, s.c₃, s.c₄ | |
219 P₀₀ = c₁(0) | |
220 P₁₀ = c₂(0) | |
221 P₁₁ = c₃(0) | |
222 P₀₁ = c₄(0) | |
223 return (1-v)*c₁(u) + u*c₂(v) + v*c₃(1-u) + (1-u)*c₄(1-v) - ( | |
224 (1-u)*(1-v)*P₀₀ + u*(1-v)*P₁₀ + u*v*P₁₁ + (1-u)*v*P₀₁ | |
225 ) | |
226 end | |
227 | |
228 function (s::TransfiniteInterpolationSurface)(ξ̄::AbstractArray) | |
229 s(ξ̄...) | |
230 end | |
231 | |
1644
e213bd857f3f
Add some todos and tbds
Jonatan Werpers <jonatan@werpers.com>
parents:
1625
diff
changeset
|
232 # TODO: Implement jacobian() for the different mapping helpers |
1558 | 233 |