Kernel simplicity and loops

I tried modifying the example as follows (in words - we have a point charges on a rubber band in a field). The code below does NOT work (it bombs out with “kernel simplicity rule”). However, writing the way it might be written in python (or C):

for i in range(N):
    r = x[i]- x[(i+1)%N)]
    U[None]+= spring * len0 * (r.norm()-len0)**2

Does not seem to work - it does not actually seem to have a spring between the first and the last mass.

Any words of wisdom would be appreciated.

def compute_U():
    for i, j in ti.ndrange(N, N):
        r = x[i] - x[j]
        # r.norm(1e-3) is equivalent to ti.sqrt(r.norm()**2 + 1e-3)                                                                                                                            
        # This is to prevent 1/0 error which can cause wrong derivative                                                                                                                        
        U[None] += -G / r.norm(1e-3)  # U += -1 / |r|                                                                                                                                          

    for i in range(N-1):
        r = x[i]- x[(i+1)]
        U[None]+= spring * len0 * (r.norm()-len0)**2

    r = x[N-1]-x[0]
    U[None]+= spring * len0 * (r.norm()-len0)**2

@igorrivin, thanks for your post.
I don’t quite get your question. I could give you some hints.
You could remove the gravity force and only test the spring force.
Or, could you paste your source code script?