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