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