Skip to content

Commit

Permalink
Automatically try to reshape arrays when encountering DimensionMismat…
Browse files Browse the repository at this point in the history
…ch (#3535)

* Automatically try to reshape arrays when encountering DimensionMismatch

* Unnecessary sizes
  • Loading branch information
glwagner committed Apr 4, 2024
1 parent f7042b7 commit 84357d0
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Fields/set!.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,23 @@ end

function set!(u::Field, f::Union{Array, CuArray, OffsetArray})
f = on_architecture(architecture(u), f)
u .= f

try
u .= f
catch err
if err isa DimensionMismatch
Nx, Ny, Nz = size(u)
u .= reshape(f, Nx, Ny, Nz)

msg = string("Reshaped ", summary(f),
" to set! its data to ", '\n',
summary(u))
@warn msg
else
throw(err)
end
end

return u
end

Expand Down

0 comments on commit 84357d0

Please sign in to comment.