# We need this for 2D vectors # Make sure you pass the correct include path to "bzzc -I ..." include "include/vec2.bzz" # Lennard-Jones parameters TARGET_KIN = 283.0 EPSILON_KIN = 150.0 TARGET_NONKIN = 200.0 EPSILON_NONKIN = 100.0 # Lennard-Jones interaction magnitude function lj_magnitude(dist, target, epsilon) { return -(epsilon / dist) * ((target / dist)^4 - (target / dist)^2) } # Neighbor data to LJ interaction vector function lj_vector_kin(rid, data) { return math.vec2.newp(lj_magnitude(data.distance, TARGET_KIN, EPSILON_KIN), data.azimuth) } # Neighbor data to LJ interaction vector function lj_vector_nonkin(rid, data) { return math.vec2.newp(lj_magnitude(data.distance, TARGET_NONKIN, EPSILON_NONKIN), data.azimuth) } # Accumulator of neighbor LJ interactions function lj_sum(rid, data, accum) { return math.vec2.add(data, accum) } # Calculates and actuates the flocking interaction function square() { # Calculate accumulator var accum = neighbors.kin().map(lj_vector_kin).reduce(lj_sum, math.vec2.new(0.0, 0.0)) accum = neighbors.nonkin().map(lj_vector_nonkin).reduce(lj_sum, accum) if(neighbors.count() > 0) math.vec2.scale(accum, 1.0 / neighbors.count()) # Move according to vector goto(accum.x, accum.y) } # Executed at init time function init() { # Divide the swarm in two sub-swarms s1 = swarm.create(1) s1.select(id % 2 == 0) s2 = s1.others(2) } # Executed every time step function step() { s1.exec(square) s2.exec(square) } # Execute at exit function destroy() { }