Mercurial > repos > public > sbplib_julia
annotate src/Grids/manifolds.jl @ 1780:8ecdc5bb46be feature/grids/manifolds
Allow mixed types in constructors for HyperBox and Simplex
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Mon, 16 Sep 2024 08:58:12 +0200 |
parents | 2a8a2b52a112 |
children | a73838c9ef94 |
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 |
1644
e213bd857f3f
Add some todos and tbds
Jonatan Werpers <jonatan@werpers.com>
parents:
1625
diff
changeset
|
22 # TBD: Should implement domain_dim? |
1558 | 23 |
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
|
24 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
|
25 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
|
26 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
|
27 |
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 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
|
29 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
|
30 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
|
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 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
|
33 |
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 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
|
35 |
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 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
|
37 |
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
|
38 |
1558 | 39 struct HyperBox{T,D} <: ParameterSpace{D} |
40 a::SVector{D,T} | |
41 b::SVector{D,T} | |
42 end | |
43 | |
44 function HyperBox(a,b) | |
1780
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
45 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
|
46 T = SVector{length(a),ET} |
1558 | 47 HyperBox(convert(T,a), convert(T,b)) |
48 end | |
49 | |
50 Rectangle{T} = HyperBox{T,2} | |
51 Box{T} = HyperBox{T,3} | |
52 | |
53 limits(box::HyperBox, d) = (box.a[d], box.b[d]) | |
54 limits(box::HyperBox) = (box.a, box.b) | |
55 | |
56 unitsquare(T=Float64) = unithyperbox(T,2) | |
57 unitcube(T=Float64) = unithyperbox(T,3) | |
58 unithyperbox(T, D) = HyperBox((@SVector zeros(T,D)), (@SVector ones(T,D))) | |
59 unithyperbox(D) = unithyperbox(Float64,D) | |
60 | |
61 | |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
62 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
|
63 verticies::NTuple{NV,SVector{D,T}} |
1558 | 64 end |
65 | |
1780
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
66 function Simplex(verticies::Vararg{AbstractArray}) |
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
67 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
|
68 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
|
69 |
8ecdc5bb46be
Allow mixed types in constructors for HyperBox and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1779
diff
changeset
|
70 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
|
71 end |
1564
35fe4375b35f
Export things and fix ConcreteChart and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1558
diff
changeset
|
72 |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
73 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
|
74 |
1558 | 75 Triangle{T} = Simplex{T,2} |
76 Tetrahedron{T} = Simplex{T,3} | |
77 | |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
78 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
|
79 unittetrahedron(T=Float64) = unitsimplex(T,3) |
1558 | 80 function unitsimplex(T,D) |
81 z = @SVector zeros(T,D) | |
82 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
|
83 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
|
84 return Simplex((z,verticies...)) |
1558 | 85 end |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
86 unitsimplex(D) = unitsimplex(Float64, D) |
1558 | 87 |
88 """ | |
1581
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
89 Chart{D} |
1558 | 90 |
91 A parametrized description of a manifold or part of a manifold. | |
92 """ | |
1581
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
93 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
|
94 mapping::MT |
1558 | 95 parameterspace::PST |
96 end | |
97 | |
1581
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
98 domain_dim(::Chart{D}) where D = D |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
99 (c::Chart)(ξ) = c.mapping(ξ) |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
100 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
|
101 |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
102 """ |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
103 jacobian(c::Chart, ξ) |
1558 | 104 |
1581
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
105 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
|
106 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
|
107 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
|
108 chart itself. |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
109 ```julia |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
110 c = Chart(f, ps) |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
111 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
|
112 ``` |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
113 or |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
114 ```julia |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
115 c = Chart(f, ps) |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
116 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
|
117 ``` |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
118 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
|
119 """ |
f77c5309dd2b
Rename ConcreteChart to Chart and remove the abstarct chart type
Jonatan Werpers <jonatan@werpers.com>
parents:
1580
diff
changeset
|
120 jacobian(c::Chart, ξ) = jacobian(c.mapping, ξ) |
1644
e213bd857f3f
Add some todos and tbds
Jonatan Werpers <jonatan@werpers.com>
parents:
1625
diff
changeset
|
121 # 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
|
122 |
1558 | 123 |
1644
e213bd857f3f
Add some todos and tbds
Jonatan Werpers <jonatan@werpers.com>
parents:
1625
diff
changeset
|
124 # TBD: Should Charts, parameterspaces have boundary names? |
e213bd857f3f
Add some todos and tbds
Jonatan Werpers <jonatan@werpers.com>
parents:
1625
diff
changeset
|
125 |
1558 | 126 """ |
127 Atlas | |
128 | |
129 A collection of charts and their connections. | |
130 Should implement methods for `charts` and | |
131 """ | |
132 abstract type Atlas end | |
133 | |
134 """ | |
135 charts(::Atlas) | |
136 | |
137 The colloction of charts in the atlas. | |
138 """ | |
139 function charts end | |
140 | |
141 """ | |
142 connections | |
143 | |
144 TBD: What exactly should this return? | |
145 | |
146 """ | |
147 | |
148 struct CartesianAtlas <: Atlas | |
149 charts::Matrix{Chart} | |
150 end | |
151 | |
152 charts(a::CartesianAtlas) = a.charts | |
153 | |
154 struct UnstructuredAtlas <: Atlas | |
155 charts::Vector{Chart} | |
156 connections | |
157 end | |
158 | |
159 charts(a::UnstructuredAtlas) = a.charts | |
160 | |
161 | |
162 ### | |
163 # Geometry | |
164 ### | |
165 | |
166 abstract type Curve end | |
167 abstract type Surface end | |
168 | |
169 | |
170 struct Line{PT} <: Curve | |
171 p::PT | |
172 tangent::PT | |
173 end | |
174 | |
175 (c::Line)(s) = c.p + s*c.tangent | |
176 | |
177 | |
178 struct LineSegment{PT} <: Curve | |
179 a::PT | |
180 b::PT | |
181 end | |
182 | |
183 (c::LineSegment)(s) = (1-s)*c.a + s*c.b | |
184 | |
185 | |
1625
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
186 function linesegments(ps...) |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
187 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
|
188 end |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
189 |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
190 |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
191 function polygon_edges(ps...) |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
192 n = length(ps) |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
193 return [LineSegment(ps[i], ps[mod1(i+1,n)]) for i ∈ eachindex(Ps)] |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
194 end |
84c3b9d71218
Add linesegments function
Jonatan Werpers <jonatan@werpers.com>
parents:
1581
diff
changeset
|
195 |
1558 | 196 struct Circle{T,PT} <: Curve |
197 c::PT | |
198 r::T | |
199 end | |
200 | |
201 (c::Circle)(θ) = c.c + r*@SVector[cos(Θ), sin(Θ)] | |
202 | |
203 struct TransfiniteInterpolationSurface{T1,T2,T3,T4} <: Surface | |
204 c₁::T1 | |
205 c₂::T2 | |
206 c₃::T3 | |
207 c₄::T4 | |
208 end | |
209 | |
210 function (s::TransfiniteInterpolationSurface)(u,v) | |
211 c₁, c₂, c₃, c₄ = s.c₁, s.c₂, s.c₃, s.c₄ | |
212 P₀₀ = c₁(0) | |
213 P₁₀ = c₂(0) | |
214 P₁₁ = c₃(0) | |
215 P₀₁ = c₄(0) | |
216 return (1-v)*c₁(u) + u*c₂(v) + v*c₃(1-u) + (1-u)*c₄(1-v) - ( | |
217 (1-u)*(1-v)*P₀₀ + u*(1-v)*P₁₀ + u*v*P₁₁ + (1-u)*v*P₀₁ | |
218 ) | |
219 end | |
220 | |
221 function (s::TransfiniteInterpolationSurface)(ξ̄::AbstractArray) | |
222 s(ξ̄...) | |
223 end | |
224 | |
1644
e213bd857f3f
Add some todos and tbds
Jonatan Werpers <jonatan@werpers.com>
parents:
1625
diff
changeset
|
225 # TODO: Implement jacobian() for the different mapping helpers |
1558 | 226 |