@@ -42,11 +42,11 @@ AdaptiveSky(V::Type, calc_v, check_refine; pole_offset = 1e-6) = AdaptiveSky(
4242)
4343
4444"""
45- refine_all !(sky::AdaptiveSky, to_refine::Vector{Int})
45+ trace_all !(sky::AdaptiveSky, to_refine::Vector{Int})
4646
4747Refine all cells in `to_refine` using a multi-threaded loop.
4848"""
49- function refine_all ! (
49+ function trace_all ! (
5050 sky:: AdaptiveSky ,
5151 to_refine:: Vector{Int} ;
5252 verbose = false ,
@@ -120,25 +120,16 @@ function trace_initial!(sky::AdaptiveSky; level = 3)
120120end
121121
122122"""
123- trace_step!(sky::AdaptiveSky; check_refine = sky.check_refine, verbose = false)
124-
125- Apply the refinement metric across each cell boundary and refine the cells
126- where the metric is `true`.
127-
128- A different refinemenet metric from the default can be used by passing the
129- `check_refine` kwarg, using the same function signature as documented in
130- [`AdaptiveSky`](@ref).
123+ find_need_refine(sky::AdaptiveSky, check_refine::Function)::Vector{Int}
131124
132- If `verbose` is true, a progress bar will be displayed during refinement.
125+ Check all cells with `check_refine`, and return those cell IDs that would need
126+ to refined.
133127"""
134- function trace_step! (
135- sky:: AdaptiveSky{T,V} ;
136- check_refine = sky. check_refine,
137- kwargs... ,
138- ) where {T,V}
128+ function find_need_refine (sky:: AdaptiveSky , check_refine:: Function ):: Vector{Int}
139129 N = lastindex (sky. grid. cells)
140130
141131 to_refine = Set {Int} ()
132+
142133 # TODO : multi-thread using a divide and conquer approach
143134 for cell_index = 1 : N
144135 # skip those we've already refined
@@ -155,32 +146,70 @@ function trace_step!(
155146 end
156147 end
157148
149+ sort! (collect (to_refine))
150+ end
151+
152+ """
153+ function refine_and_trace!(
154+ sky::AdaptiveSky,
155+ cell_ids::Vector{Int};
156+ kwargs...,
157+ )
158+
159+ Given a list of cell IDs, refine them using [`refine!`](@ref), and then trace
160+ the children to populate their values.
161+
162+ All kwargs are forwarded to [`trace_all!`](@ref).
163+ """
164+ function refine_and_trace! (
165+ sky:: AdaptiveSky{T,V} ,
166+ cell_ids:: Vector{Int} ;
167+ kwargs... ,
168+ ) where {T,V}
169+ N = lastindex (sky. grid. cells)
170+
171+ to_trace = Int[]
172+ sizehint! (to_trace, length (cell_ids) * 8 )
173+
158174 # now that all have been reaped, apply refining
159- to_trace = map (collect (to_refine)) do index
160- if Grids. has_children (sky. grid, index)
161- Int[]
162- else
163- _children = Grids. refine! (sky. grid, index)
164- for (j, _) in enumerate (_children)
165- if j == 5
166- push! (sky. values, sky. values[index])
167- else
168- push! (sky. values, make_null (V))
169- end
175+ for index in cell_ids
176+ @assert ! Grids. has_children (sky. grid, index)
177+ _children = Grids. refine! (sky. grid, index)
178+ for (j, child_id) in enumerate (_children)
179+ if j == 5
180+ push! (sky. values, sky. values[index])
181+ else
182+ push! (to_trace, child_id)
183+ push! (sky. values, make_null (V))
170184 end
171- # return all but the middle for tracing
172- Int[_children[[1 , 2 , 3 , 4 , 6 , 7 , 8 , 9 ]]. .. ]
173185 end
174186 end
175187
176188 if ! isempty (to_trace)
177- all_trace = reduce (vcat, to_trace)
178- refine_all ! (sky, all_trace ; kwargs... )
189+
190+ trace_all ! (sky, to_trace ; kwargs... )
179191 end
180192
181193 sky
182194end
183195
196+ """
197+ trace_step!(sky::AdaptiveSky; check_refine = sky.check_refine, verbose = false)
198+
199+ Apply the refinement metric across each cell boundary and refine the cells
200+ where the metric is `true`.
201+
202+ A different refinemenet metric from the default can be used by passing the
203+ `check_refine` kwarg, using the same function signature as documented in
204+ [`AdaptiveSky`](@ref).
205+
206+ If `verbose` is true, a progress bar will be displayed during refinement.
207+ """
208+ function trace_step! (sky:: AdaptiveSky ; check_refine = sky. check_refine, kwargs... )
209+ to_refine = find_need_refine (sky, check_refine)
210+ refine_and_trace! (sky, to_refine; kwargs... )
211+ end
212+
184213vector_average (distances, values) = sum (i -> i[1 ] * i[2 ], zip (distances, values))
185214
186215# TODO : use a buffer
0 commit comments