This code is not thread-safe: ins_force is defnied outside of the multithreaded part, but used inside without accounting for the thread-loop index i.
Note that the Matlab version does not have this issue, there Force_temp is defined inside the parfor loop.
Furthermore, this code should be wrapped in a function to avoid usage of global variables, which are type-unstable.
2 Likes