26
26
@inline field_dim (:: VIJFHSingleton ) = 4
27
27
@inline field_dim (:: VIFHSingleton ) = 3
28
28
29
- struct FieldArray{N, T <: AbstractArray }
29
+ struct FieldArray{FD, N, T <: AbstractArray }
30
30
arrays:: NTuple{N, T}
31
31
end
32
- function Adapt. adapt_structure (to, fa:: FieldArray{N, T} ) where {N, T}
32
+ function Adapt. adapt_structure (to, fa:: FieldArray{FD, N, T} ) where {FD, N, T}
33
33
arrays = ntuple (i -> Adapt. adapt (to, fa. arrays[i]), N)
34
- FieldArray {N, eltype(arrays)} (arrays)
34
+ FieldArray {FD, N, eltype(arrays)} (arrays)
35
35
end
36
- function rebuild (fa:: FieldArray{N, T} , :: Type{DA} ) where {N, T, DA}
36
+ function rebuild (fa:: FieldArray{FD, N, T} , :: Type{DA} ) where {FD, N, T, DA}
37
37
arrays = ntuple (i -> DA (fa. arrays[i]), N)
38
- FieldArray {N, eltype(arrays)} (arrays)
38
+ FieldArray {FD, N, eltype(arrays)} (arrays)
39
39
end
40
40
41
- Base. length (fa:: FieldArray{N, T} ) where {N, T} = N
42
- Base. copy (fa:: FieldArray{N, T} ) where {N, T} =
43
- FieldArray {N, T} (ntuple (i -> copy (fa. arrays[i]), N))
41
+ Base. length (fa:: FieldArray{FD, N, T} ) where {FD, N, T} = N
42
+ Base. copy (fa:: FieldArray{FD, N, T} ) where {FD, N, T} =
43
+ FieldArray {FD, N, T} (ntuple (i -> copy (fa. arrays[i]), N))
44
44
45
- float_type (:: Type{FieldArray{N, T}} ) where {N, T} = eltype (T)
46
- parent_array_type (:: Type{FieldArray{N, T}} ) where {N, T} = parent_array_type (T)
45
+ float_type (:: Type{FieldArray{FD, N, T}} ) where {FD, N, T} = eltype (T)
46
+ parent_array_type (:: Type{FieldArray{FD, N, T}} ) where {FD, N, T} = parent_array_type (T)
47
47
# field_array_type(::Type{FieldArray{N,T}}, ::Val{Nf}) where {N,T, Nf} = FieldArray{Nf, parent_array_type(T, Val(ndims(T)))}
48
48
field_array_type (
49
- :: Type{FieldArray{N, T}} ,
49
+ :: Type{FieldArray{FD, N, T}} ,
50
50
:: Val{Nf} ,
51
51
:: Val{ND} ,
52
- ) where {N, T, Nf, ND} = FieldArray{Nf, parent_array_type (T, Val (ND))}
52
+ ) where {FD, N, T, Nf, ND} = FieldArray{FD, Nf, parent_array_type (T, Val (ND))}
53
53
# field_array_type(::Type{T}, ::Val{Nf}) where {T <: AbstractArray, Nf} = FieldArray{Nf, parent_array_type(T, Val(ndims(T)))}
54
54
field_array_type (
55
55
:: Type{T} ,
56
+ :: Val{FD} ,
56
57
:: Val{Nf} ,
57
58
:: Val{ND} ,
58
- ) where {T <: AbstractArray , Nf, ND} =
59
- FieldArray{Nf, parent_array_type (T, Val (ND))}
59
+ ) where {T <: AbstractArray , FD, Nf, ND} =
60
+ FieldArray{FD, Nf, parent_array_type (T, Val (ND))}
60
61
# Base.ndims(::Type{FieldArray{N,T}}) where {N,T} = Base.ndims(T)+1
61
- Base. eltype (:: Type{FieldArray{N, T}} ) where {N, T} = eltype (T)
62
- array_type (:: Type{FieldArray{N, T}} ) where {N, T} = T
63
- ncomponents (:: Type{FieldArray{N, T}} ) where {N, T} = N
64
- ncomponents (:: FieldArray{N, T} ) where {N, T} = N
62
+ Base. eltype (:: Type{FieldArray{FD, N, T}} ) where {FD, N, T} = eltype (T)
63
+ array_type (:: Type{FieldArray{FD, N, T}} ) where {FD, N, T} = T
64
+ ncomponents (:: Type{FieldArray{FD, N, T}} ) where {FD, N, T} = N
65
+ ncomponents (:: FieldArray{FD, N, T} ) where {FD, N, T} = N
65
66
# Base.size(fa::FieldArray{N,T}) where {N,T} = size(fa.arrays[1])
66
- to_parent_array_type (:: Type{FA} ) where {N, T, FA <: FieldArray{N, T} } =
67
- FieldArray{N, parent_array_type (FA)}
67
+ to_parent_array_type (:: Type{FA} ) where {FD, N, T, FA <: FieldArray{FD, N, T} } =
68
+ FieldArray{FD, N, parent_array_type (FA)}
68
69
69
70
promote_parent_array_type (
70
- :: Type{FieldArray{NA, A}} ,
71
- :: Type{FieldArray{NB, B}} ,
72
- ) where {NA, NB, A, B} =
71
+ :: Type{FieldArray{FDA, NA, A}} ,
72
+ :: Type{FieldArray{FDB, NB, B}} ,
73
+ ) where {FDA, FDB, NA, NB, A, B} =
73
74
# FieldArray{N, promote_parent_array_type(A, B)} where {N}
74
75
promote_parent_array_type (A, B)
75
76
76
- elsize (fa:: FieldArray{N, T} ) where {N, T} = size (fa. arrays[1 ])
77
+ elsize (fa:: FieldArray{FD, N, T} ) where {FD, N, T} = size (fa. arrays[1 ])
77
78
78
- @inline function Base. copyto! (x:: FA , y:: FA ) where {N, T, FA <: FieldArray{N, T} }
79
+ @inline function Base. copyto! (x:: FA , y:: FA ) where {FD, N, T, FA <: FieldArray{FD, N, T} }
79
80
@inbounds for i in 1 : N
80
81
Base. copyto! (x. arrays[i], y. arrays[i])
81
82
end
82
83
end
83
84
84
- Base. similar (fa:: FieldArray{N, T} ) where {N, T} =
85
- FieldArray {N, T} (ntuple (i -> similar (T, ndims (T)), N))
85
+ Base. similar (fa:: FieldArray{FD, N, T} ) where {FD, N, T} =
86
+ FieldArray {FD, N, T} (ntuple (i -> similar (T, ndims (T)), N))
86
87
87
- function Base. similar (:: Type{<:FieldArray{N, T}} , s) where {N, T}
88
- FieldArray {N} (ntuple (i -> similar (T, s), N))
88
+ function Base. similar (:: Type{<:FieldArray{FD, N, T}} , s) where {FD, N, T}
89
+ FieldArray {FD, N} (ntuple (i -> similar (T, s), N))
89
90
end
90
91
91
92
pre_post_colons (f, dim) = (;
@@ -96,8 +97,11 @@ pre_post_colons(f, dim) = (;
96
97
field_array (array:: AbstractArray , s:: AbstractDataLayoutSingleton ) =
97
98
field_array (array, field_dim (s))
98
99
99
- FieldArray {N} (fa:: FA ) where {N, T <: AbstractArray , FA <: NTuple{N, T} } =
100
- FieldArray {N, T} (fa)
100
+ FieldArray {FD, N} (fa:: FA ) where {FD, N, T <: AbstractArray , FA <: NTuple{N, T} } =
101
+ FieldArray {FD, N, T} (fa)
102
+
103
+ FieldArray {FD} (fa:: FA ) where {FD, N, T <: AbstractArray , FA <: NTuple{N, T} } =
104
+ FieldArray {FD, N, T} (fa)
101
105
102
106
function field_array (array:: AbstractArray , fdim:: Integer )
103
107
Nf = size (array, fdim)
@@ -116,5 +120,23 @@ function field_array(array::AbstractArray, fdim::Integer)
116
120
arrays[i] .= array[Ipre... , i, Ipost... ]
117
121
end
118
122
end
119
- return FieldArray {Nf, eltype(arrays)} (arrays)
123
+ return FieldArray {fdim, Nf, eltype(arrays)} (arrays)
124
+ end
125
+
126
+ function Base. getindex (fa:: FieldArray{FD} , I:: CartesianIndex ) where {FD}
127
+ fdim = I. I[FD]
128
+ ND = length (I. I)
129
+ Ipre = ntuple (i -> I. I[i], Val (fdim - 1 ))
130
+ Ipost = ntuple (i -> I. I[i], Val (ND - fdim))
131
+ IA = CartesianIndex ((Ipre... ,Ipost... ))
132
+ return fa. arrays[fdim][IA]
133
+ end
134
+
135
+ function Base. setindex! (fa:: FieldArray{FD} , val, I:: CartesianIndex ) where {FD}
136
+ fdim = I. I[FD]
137
+ ND = length (I. I)
138
+ Ipre = ntuple (i -> I. I[i], Val (fdim - 1 ))
139
+ Ipost = ntuple (i -> I. I[i], Val (ND - fdim))
140
+ IA = CartesianIndex ((Ipre... ,Ipost... ))
141
+ return fa. arrays[fdim][IA]
120
142
end
0 commit comments