Skip to content

Commit

Permalink
Check for Array indices, which will indicate a need for broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
lrennels committed Jun 21, 2024
1 parent 7b09f61 commit f719197
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/mcs/montecarlo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,6 @@ function _param_indices(param::ArrayModelParameter{T}, md::ModelDef, i::Int, tra
for (dim_name, dim_values) in zip(pdims, tdims)
dim = dimension(md, dim_name)
dim_indices = dim[dim_values]

# reduce a singleton Vector to just an Integer
isa(dim_indices, Vector{Int64}) && size(dim_indices) == (1,) ? dim_indices = dim_indices[1] : nothing

dim_name == :time ? dim_indices = TimestepIndex.(dim_indices) : nothing
push!(indices, dim_indices)
end
Expand Down Expand Up @@ -381,14 +377,16 @@ function _perturb_param!(param::ArrayModelParameter{T}, md::ModelDef, i::Int,

# If there is no time index we have all methods needed to broadcast normally
if isnothing(ti)
broadcast_flag = sum(map(x -> length(x) > 1, indices)) > 0
# broadcast_flag = sum(map(x -> length(x) > 1, indices)) > 0
broadcast_flag = any(map(x -> isa(x, Array), indices)) # check if any of the elements of the indices Vector are Arrays (likely Vectors)
broadcast_flag ? pvalue[indices...] .= rvalue : pvalue[indices...] = rvalue

else
indices1, ts, indices2 = indices[1:ti - 1], indices[ti], indices[ti + 1:end]
non_ts_indices = [indices1..., indices2...]
broadcast_flag = isempty(non_ts_indices) ? false : sum(map(x -> length(x) > 1, non_ts_indices)) > 0

# broadcast_flag = isempty(non_ts_indices) ? false : sum(map(x -> length(x) > 1, non_ts_indices)) > 0
broadcast_flag = isempty(non_ts_indices) ? false : any(map(x -> isa(x, Array), non_ts_indices)) # check if any of the elements of the indices Vector are Arrays (likely Vectors)

# Loop over the Array of TimestepIndex
if isa(ts, Array)
for el in ts
Expand Down

0 comments on commit f719197

Please sign in to comment.