energy   xtlang


Defined in:  https://github.com/digego/extempore/tree/v0.8.9/examples/core/nbody_lang_shootout.xtm

Implementation

(bind-func energy
  (lambda (nbodies:i32 bodies:planet*)
    (let ((i:i32 0) (j:i32 0) (e 0.0)
          (dx 0.0) (dy 0.0) (dz 0.0) (distance 0.0)
          (b:planet* null) (b2:planet* null))
      (dotimes (i nbodies)
        (set! b (pref-ptr bodies i))
        (set! e (+ e (* 0.5 (tref b 6)
                        (+ (* (tref b 3) (tref b 3))
                           (* (tref b 4) (tref b 4))
                           (* (tref b 5) (tref b 5))))))
        (dotimes (j (+ i 1) (- nbodies (+ i 1)))
          (set! b2 (pref-ptr bodies j))
          (set! dx (- (tref b 0) (tref b2 0)))
          (set! dy (- (tref b 1) (tref b2 1)))
          (set! dz (- (tref b 2) (tref b2 2)))
          (set! distance (sqrt (+ (* dx dx) (* dy dy) (* dz dz))))
          (set! e (- e (/ (* (tref b 6) (tref b2 6)) distance)))
          ))
      e)))


Back to Index