Mercurial > repos > public > sbplib_julia
comparison src/Grids/grid.jl @ 1594:d68d02dd882f feature/boundary_conditions
Merge with default
author | Vidar Stiernström <vidar.stiernstrom@gmail.com> |
---|---|
date | Sat, 25 May 2024 16:07:10 -0700 |
parents | d641798539c2 |
children | 9113f437431d 611ae2308aa1 |
comparison
equal
deleted
inserted
replaced
1591:615eeb6e662e | 1594:d68d02dd882f |
---|---|
25 Base.getindex(g::Grid, I::CartesianIndex) = g[Tuple(I)...] | 25 Base.getindex(g::Grid, I::CartesianIndex) = g[Tuple(I)...] |
26 | 26 |
27 """ | 27 """ |
28 coordinate_size(g) | 28 coordinate_size(g) |
29 | 29 |
30 The lenght of the coordinate vector of `Grid` `g`. | 30 The length of the coordinate vector of `Grid` `g`. |
31 """ | 31 """ |
32 coordinate_size(::Type{<:Grid{T}}) where T = _ncomponents(T) | 32 coordinate_size(::Type{<:Grid{T}}) where T = _ncomponents(T) |
33 coordinate_size(g::Grid) = coordinate_size(typeof(g)) # TBD: Name of this function?! | 33 coordinate_size(g::Grid) = coordinate_size(typeof(g)) # TBD: Name of this function?! |
34 | 34 |
35 """ | 35 """ |
36 component_type(g) | 36 component_type(gf) |
37 | 37 |
38 The type of the components of the coordinate vector of `Grid` `g`. | 38 The type of the components of the elements of `gf`. I.e if `gf` is a vector |
39 valued grid function, `component_view(gf)` is the element type of the vectors | |
40 at each grid point. | |
41 | |
42 # Examples | |
43 ```julia-repl | |
44 julia> component_type([[1,2], [2,3], [3,4]]) | |
45 Int64 | |
46 ``` | |
39 """ | 47 """ |
40 component_type(::Type{<:Grid{T}}) where T = eltype(T) | 48 component_type(T::Type) = eltype(eltype(T)) |
41 component_type(g::Grid) = component_type(typeof(g)) | 49 component_type(t) = component_type(typeof(t)) |
50 | |
51 """ | |
52 componentview(gf, component_index...) | |
53 | |
54 A view of `gf` with only the components specified by `component_index...`. | |
55 | |
56 # Examples | |
57 ```julia-repl | |
58 julia> componentview([[1,2], [2,3], [3,4]],2) | |
59 3-element ArrayComponentView{Int64, Vector{Int64}, 1, Vector{Vector{Int64}}, Tuple{Int64}}: | |
60 2 | |
61 3 | |
62 4 | |
63 ``` | |
64 """ | |
65 componentview(gf, component_index...) = ArrayComponentView(gf, component_index) | |
66 | |
67 struct ArrayComponentView{CT,T,D,AT <: AbstractArray{T,D}, IT} <: AbstractArray{CT,D} | |
68 v::AT | |
69 component_index::IT | |
70 | |
71 function ArrayComponentView(v, component_index) | |
72 CT = typeof(first(v)[component_index...]) | |
73 return new{CT, eltype(v), ndims(v), typeof(v), typeof(component_index)}(v,component_index) | |
74 end | |
75 end | |
76 | |
77 Base.size(cv) = size(cv.v) | |
78 Base.getindex(cv::ArrayComponentView, i::Int) = cv.v[i][cv.component_index...] | |
79 Base.getindex(cv::ArrayComponentView, I::Vararg{Int}) = cv.v[I...][cv.component_index...] | |
80 IndexStyle(::Type{<:ArrayComponentView{<:Any,<:Any,AT}}) where AT = IndexStyle(AT) | |
81 | |
82 # TODO: Implement `setindex!`? | |
83 # TODO: Implement a more general ComponentView that can handle non-AbstractArrays. | |
42 | 84 |
43 """ | 85 """ |
44 refine(g::Grid, r) | 86 refine(g::Grid, r) |
45 | 87 |
46 The grid where `g` is refined by the factor `r`. | 88 The grid where `g` is refined by the factor `r`. |
97 eval_on(g::Grid, f) = eval_on(g, f, Base.IteratorSize(g)) | 139 eval_on(g::Grid, f) = eval_on(g, f, Base.IteratorSize(g)) |
98 function eval_on(g::Grid, f, ::Base.HasShape) | 140 function eval_on(g::Grid, f, ::Base.HasShape) |
99 if hasmethod(f, (Any,)) | 141 if hasmethod(f, (Any,)) |
100 return LazyTensors.LazyFunctionArray((I...)->f(g[I...]), size(g)) | 142 return LazyTensors.LazyFunctionArray((I...)->f(g[I...]), size(g)) |
101 else | 143 else |
144 # TBD This branch can be removed if we accept the trade off that we define f with the syntax f((x,y)) instead if we don't want to handle the vector in the body of f. (Add an example in the docs) | |
145 # Also see Notes.md | |
102 return LazyTensors.LazyFunctionArray((I...)->f(g[I...]...), size(g)) | 146 return LazyTensors.LazyFunctionArray((I...)->f(g[I...]...), size(g)) |
103 end | 147 end |
104 end | 148 end |
105 | 149 |
106 """ | 150 """ |