Increment some components of a vector

Loops aren’t slow in Julia!

It sounds like you may be translating an existing “vectorized” code from some other language like Matlab or Python? You may not get much benefit from Julia unless you write in a completely different style.

For finite-volume/finite-difference algorithms, my feeling is that a simple loop is typically the clearest as well as the fastest method — just loop over your grid, and apply your stencil each point, e.g. u[i,j] += c[i,j] * (u[i+1,j] + u[i-1,j] + u[i,j+1] + u[i,j-1] - 4u[i,j]) for a diffusion-like equation. Use ghost cells for boundary conditions.

3 Likes