Mercurial > repos > public > sbplib_julia
comparison src/Grids/parameter_space.jl @ 1956:b0fcb29e3620 feature/grids/multiblock_boundaries
Merge default
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 11 Feb 2025 08:54:18 +0100 |
parents | 449cce897da9 |
children | 77ff0a2acbe5 |
comparison
equal
deleted
inserted
replaced
1912:e68669552ed8 | 1956:b0fcb29e3620 |
---|---|
1 """ | |
2 ParameterSpace{D} | |
3 | |
4 A space of parameters of dimension `D`. | |
5 | |
6 Common parameter spaces are created using functions for unit sized spaces | |
7 * [`unitinterval`](@ref) | |
8 * [`unitsquare`](@ref) | |
9 * [`unitcube`](@ref) | |
10 * [`unithyperbox`](@ref) | |
11 * [`unittriangle`](@ref) | |
12 * [`unittetrahedron`](@ref) | |
13 * [`unitsimplex`](@ref) | |
14 | |
15 See also: [`Interval`](@ref), [`HyperBox`](@ref), | |
16 [`Simplex`](@ref). | |
17 """ | |
18 abstract type ParameterSpace{D} end | |
19 Base.ndims(::ParameterSpace{D}) where D = D | |
20 | |
21 """ | |
22 Interval{T} <: ParameterSpace{1} | |
23 | |
24 A `ParameterSpace` representing an interval. | |
25 """ | |
26 struct Interval{T} <: ParameterSpace{1} | |
27 a::T | |
28 b::T | |
29 end | |
30 | |
31 """ | |
32 Interval(a,b) | |
33 | |
34 An interval with limits `a` and `b`. | |
35 """ | |
36 function Interval(a,b) | |
37 a, b = promote(a, b) | |
38 Interval{typeof(a)}(a,b) | |
39 end | |
40 | |
41 """ | |
42 limits(i::Interval) | |
43 | |
44 The limits of the interval. | |
45 """ | |
46 limits(i::Interval) = (i.a, i.b) | |
47 | |
48 boundary_identifiers(::Interval) = (LowerBoundary(), UpperBoundary()) | |
49 | |
50 """ | |
51 unitinterval(T=Float64) | |
52 | |
53 The interval ``(0,1)``. | |
54 """ | |
55 unitinterval(T=Float64) = Interval(zero(T), one(T)) | |
56 | |
57 | |
58 """ | |
59 HyperBox{T,D} <: ParameterSpace{D} | |
60 | |
61 A `ParameterSpace` representing a hyper box. | |
62 """ | |
63 struct HyperBox{T,D} <: ParameterSpace{D} | |
64 a::SVector{D,T} | |
65 b::SVector{D,T} | |
66 end | |
67 | |
68 """ | |
69 HyperBox(a,b) | |
70 | |
71 A `HyperBox` with lower limits `a` and upper limits `b` for each dimension. | |
72 """ | |
73 function HyperBox(a,b) | |
74 ET = promote_type(eltype(a),eltype(b)) | |
75 T = SVector{length(a),ET} | |
76 HyperBox(convert(T,a), convert(T,b)) | |
77 end | |
78 | |
79 Rectangle{T} = HyperBox{T,2} | |
80 Box{T} = HyperBox{T,3} | |
81 | |
82 """ | |
83 limits(box::HyperBox, d) | |
84 | |
85 Limits of `box` along dimension `d`. | |
86 """ | |
87 limits(box::HyperBox, d) = (box.a[d], box.b[d]) | |
88 | |
89 """ | |
90 limits(box::HyperBox) | |
91 | |
92 The lower and upper limits of `box` as tuples. | |
93 """ | |
94 limits(box::HyperBox) = (box.a, box.b) | |
95 | |
96 function boundary_identifiers(box::HyperBox) | |
97 mapreduce(vcat, 1:ndims(box)) do d | |
98 [ | |
99 CartesianBoundary{d, LowerBoundary}(), | |
100 CartesianBoundary{d, UpperBoundary}(), | |
101 ] | |
102 end | |
103 end | |
104 | |
105 | |
106 """ | |
107 unitsquare(T=Float64) | |
108 | |
109 The square limited by 0 and 1 in each dimension. | |
110 """ | |
111 unitsquare(T=Float64) = unithyperbox(T,2) | |
112 | |
113 """ | |
114 unitcube(T=Float64) | |
115 | |
116 The cube limited by 0 and 1 in each dimension. | |
117 """ | |
118 unitcube(T=Float64) = unithyperbox(T,3) | |
119 | |
120 """ | |
121 unithyperbox(T=Float64, D) | |
122 | |
123 The hypercube limited by 0 and 1 in each dimension. | |
124 """ | |
125 unithyperbox(T, D) = HyperBox((@SVector zeros(T,D)), (@SVector ones(T,D))) | |
126 unithyperbox(D) = unithyperbox(Float64,D) | |
127 | |
128 | |
129 """ | |
130 Simplex{T,D,NV} <: ParameterSpace{D} | |
131 | |
132 A `ParameterSpace` representing a simplex. | |
133 """ | |
134 struct Simplex{T,D,NV} <: ParameterSpace{D} | |
135 verticies::NTuple{NV,SVector{D,T}} | |
136 | |
137 Simplex(verticies::Tuple{SVector{D,T}, Vararg{SVector{D,T},N}}) where {T,D,N} = new{T,D,N+1}(verticies) | |
138 Simplex(::Tuple{}) = throw(ArgumentError("Must provide at least one vertex.")) | |
139 end | |
140 | |
141 """ | |
142 Simplex(verticies...) | |
143 | |
144 A simplex with the given verticies. | |
145 """ | |
146 function Simplex(verticies::Vararg{AbstractArray}) | |
147 ET = mapreduce(eltype,promote_type,verticies) | |
148 T = SVector{length(verticies[1]),ET} | |
149 | |
150 return Simplex(Tuple(convert(T,v) for v ∈ verticies)) | |
151 end | |
152 | |
153 """ | |
154 verticies(s::Simplex) | |
155 | |
156 Verticies of `s`. | |
157 """ | |
158 verticies(s::Simplex) = s.verticies | |
159 | |
160 Triangle{T} = Simplex{T,2} | |
161 Tetrahedron{T} = Simplex{T,3} | |
162 | |
163 """ | |
164 unittriangle(T=Float64) | |
165 | |
166 The simplex with verticies ``(0,0)``, ``(1,0)``, and ``(0,1)``. | |
167 """ | |
168 unittriangle(T=Float64) = unitsimplex(T,2) | |
169 | |
170 """ | |
171 unittetrahedron(T=Float64) | |
172 | |
173 The simplex with verticies ``(0,0,0)``, ``(1,0,0)``, ``(0,1,0)``, and ``(0,0,1)``. | |
174 """ | |
175 unittetrahedron(T=Float64) = unitsimplex(T,3) | |
176 | |
177 """ | |
178 unitsimplex(T=Float64,D) | |
179 | |
180 The unit simplex in dimension `D` with verticies ``(0,0,0,...)``, ``(1,0,0,...)``, ``(0,1,0,...)``, ``(0,0,1,...)``... | |
181 """ | |
182 function unitsimplex(T,D) | |
183 z = @SVector zeros(T,D) | |
184 unitelement = one(eltype(z)) | |
185 verticies = ntuple(i->setindex(z, unitelement, i), D) | |
186 return Simplex((z,verticies...)) | |
187 end | |
188 unitsimplex(D) = unitsimplex(Float64, D) |