Mercurial > repos > public > sbplib_julia
annotate src/Grids/manifolds.jl @ 1579:14d79b13b54f feature/grids/manifolds
Add tests, fix bugs, add exports, for Simplex and friends
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 25 Apr 2024 22:14:46 +0200 |
parents | 56da785ab576 |
children | fdee60ab8c4e |
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 | |
21 | |
22 struct HyperBox{T,D} <: ParameterSpace{D} | |
23 a::SVector{D,T} | |
24 b::SVector{D,T} | |
25 end | |
26 | |
27 function HyperBox(a,b) | |
28 T = SVector{length(a)} | |
29 HyperBox(convert(T,a), convert(T,b)) | |
30 end | |
31 | |
32 Interval{T} = HyperBox{T,1} | |
33 Rectangle{T} = HyperBox{T,2} | |
34 Box{T} = HyperBox{T,3} | |
35 | |
36 limits(box::HyperBox, d) = (box.a[d], box.b[d]) | |
37 limits(box::HyperBox) = (box.a, box.b) | |
38 | |
39 unitinterval(T=Float64) = unithyperbox(T,1) | |
40 unitsquare(T=Float64) = unithyperbox(T,2) | |
41 unitcube(T=Float64) = unithyperbox(T,3) | |
42 unithyperbox(T, D) = HyperBox((@SVector zeros(T,D)), (@SVector ones(T,D))) | |
43 unithyperbox(D) = unithyperbox(Float64,D) | |
44 | |
45 | |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
46 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
|
47 verticies::NTuple{NV,SVector{D,T}} |
1558 | 48 end |
49 | |
1564
35fe4375b35f
Export things and fix ConcreteChart and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1558
diff
changeset
|
50 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
|
51 |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
52 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
|
53 |
1558 | 54 Triangle{T} = Simplex{T,2} |
55 Tetrahedron{T} = Simplex{T,3} | |
56 | |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
57 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
|
58 unittetrahedron(T=Float64) = unitsimplex(T,3) |
1558 | 59 function unitsimplex(T,D) |
60 z = @SVector zeros(T,D) | |
61 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
|
62 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
|
63 return Simplex((z,verticies...)) |
1558 | 64 end |
1579
14d79b13b54f
Add tests, fix bugs, add exports, for Simplex and friends
Jonatan Werpers <jonatan@werpers.com>
parents:
1578
diff
changeset
|
65 unitsimplex(D) = unitsimplex(Float64, D) |
1558 | 66 |
67 """ | |
68 | |
69 A parametrized description of a manifold or part of a manifold. | |
70 | |
71 Should implement a methods for | |
72 * `parameterspace` | |
73 * `(::Chart)(ξs...)` | |
74 """ | |
75 abstract type Chart{D} end | |
76 # abstract type Chart{D,R} end | |
77 | |
78 domain_dim(::Chart{D}) where D = D | |
79 # range_dim(::Chart{D,R}) where {D,R} = R | |
80 | |
81 """ | |
82 The parameterspace of a chart | |
83 """ | |
84 function parameterspace end | |
85 | |
86 | |
1568 | 87 # TODO: Add trait for if there is a jacobian available? |
88 # Add package extension to allow calling the getter function anyway if it's not available | |
89 # And can we add an informative error that ForwardDiff could be loaded to make it work? | |
90 # Or can we handle this be custom implementations? For sometypes in the library it can be implemented explicitly. | |
91 # And as an example for ConcreteChart it can be implemented by the user like | |
92 # c = ConcreteChart(...) | |
93 # jacobian(c::typeof(c)) = ... | |
94 | |
1564
35fe4375b35f
Export things and fix ConcreteChart and Simplex
Jonatan Werpers <jonatan@werpers.com>
parents:
1558
diff
changeset
|
95 struct ConcreteChart{D, PST<:ParameterSpace{D}, MT} <: Chart{D} |
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 | |
1578
56da785ab576
Change to using xi for ConcreteChart
Jonatan Werpers <jonatan@werpers.com>
parents:
1577
diff
changeset
|
100 (c::ConcreteChart)(ξ) = c.mapping(ξ) |
1572
157c43966b06
Add some tests and implement parameterspace for ConcreteChart
Jonatan Werpers <jonatan@werpers.com>
parents:
1568
diff
changeset
|
101 parameterspace(c::ConcreteChart) = c.parameterspace |
1558 | 102 |
1578
56da785ab576
Change to using xi for ConcreteChart
Jonatan Werpers <jonatan@werpers.com>
parents:
1577
diff
changeset
|
103 jacobian(c::ConcreteChart, ξ) = jacobian(c.mapping, ξ) |
1558 | 104 |
105 """ | |
106 Atlas | |
107 | |
108 A collection of charts and their connections. | |
109 Should implement methods for `charts` and | |
110 """ | |
111 abstract type Atlas end | |
112 | |
113 """ | |
114 charts(::Atlas) | |
115 | |
116 The colloction of charts in the atlas. | |
117 """ | |
118 function charts end | |
119 | |
120 """ | |
121 connections | |
122 | |
123 TBD: What exactly should this return? | |
124 | |
125 """ | |
126 | |
127 struct CartesianAtlas <: Atlas | |
128 charts::Matrix{Chart} | |
129 end | |
130 | |
131 charts(a::CartesianAtlas) = a.charts | |
132 | |
133 struct UnstructuredAtlas <: Atlas | |
134 charts::Vector{Chart} | |
135 connections | |
136 end | |
137 | |
138 charts(a::UnstructuredAtlas) = a.charts | |
139 | |
140 | |
141 ### | |
142 # Geometry | |
143 ### | |
144 | |
145 abstract type Curve end | |
146 abstract type Surface end | |
147 | |
148 | |
149 struct Line{PT} <: Curve | |
150 p::PT | |
151 tangent::PT | |
152 end | |
153 | |
154 (c::Line)(s) = c.p + s*c.tangent | |
155 | |
156 | |
157 struct LineSegment{PT} <: Curve | |
158 a::PT | |
159 b::PT | |
160 end | |
161 | |
162 (c::LineSegment)(s) = (1-s)*c.a + s*c.b | |
163 | |
164 | |
165 struct Circle{T,PT} <: Curve | |
166 c::PT | |
167 r::T | |
168 end | |
169 | |
170 (c::Circle)(θ) = c.c + r*@SVector[cos(Θ), sin(Θ)] | |
171 | |
172 struct TransfiniteInterpolationSurface{T1,T2,T3,T4} <: Surface | |
173 c₁::T1 | |
174 c₂::T2 | |
175 c₃::T3 | |
176 c₄::T4 | |
177 end | |
178 | |
179 function (s::TransfiniteInterpolationSurface)(u,v) | |
180 c₁, c₂, c₃, c₄ = s.c₁, s.c₂, s.c₃, s.c₄ | |
181 P₀₀ = c₁(0) | |
182 P₁₀ = c₂(0) | |
183 P₁₁ = c₃(0) | |
184 P₀₁ = c₄(0) | |
185 return (1-v)*c₁(u) + u*c₂(v) + v*c₃(1-u) + (1-u)*c₄(1-v) - ( | |
186 (1-u)*(1-v)*P₀₀ + u*(1-v)*P₁₀ + u*v*P₁₁ + (1-u)*v*P₀₁ | |
187 ) | |
188 end | |
189 | |
190 function (s::TransfiniteInterpolationSurface)(ξ̄::AbstractArray) | |
191 s(ξ̄...) | |
192 end | |
193 | |
194 | |
195 function polygon_sides(Ps...) | |
196 n = length(Ps) | |
197 return [t->line(t,Ps[i],Ps[mod1(i+1,n)]) for i ∈ eachindex(Ps)] | |
198 end |