def Body(self, idx: int): DeviceAllocator.rand_init(kSeed, idx, 0) self.pos = Vector(2.0 * DeviceAllocator.rand_uniform() - 1.0, 2.0 * DeviceAllocator.rand_uniform() - 1.0) self.vel = Vector(0.0, 0.0) self.force = Vector(0.0, 0.0) self.mass = (DeviceAllocator.rand_uniform() / 2.0 + 0.5) * kMaxMass
def update(self): cid: int = self.cell_id if self.is_new: self.create_candidates() elif self.action == 1: cells[cid].agent_ = DeviceAllocator.new(Candidate, cid) DeviceAllocator.destroy(self)
def Body(self, idx: int): DeviceAllocator.rand_init(kSeed, idx, 0) self.pos_x = 2.0 * DeviceAllocator.rand_uniform() - 1.0 self.pos_y = 2.0 * DeviceAllocator.rand_uniform() - 1.0 self.vel_x = 0.0 self.vel_y = 0.0 self.mass = (DeviceAllocator.rand_uniform() / 2.0 + 0.5) * kMaxMass self.force_x = 0.0 self.force_y = 0.0
def maybe_create_candidate(self, x: int, y: int): dx: int = -1 dy: int = -1 while dx < 2: while dy < 2: nx: int = x + dx ny: int = y + dy if -1 < nx < SIZE_X and -1 < ny < SIZE_Y: if cells[ny * SIZE_X + nx].agent().is_alive: alive: Alive = cells[ny * SIZE_X + nx].agent() if alive.is_new: if alive is self: cells[y * SIZE_X + x].agent_ = DeviceAllocator.new(Candidate, y * SIZE_X + x) return dy += 1 dx += 1
def _update(): DeviceAllocator.parallel_do(Body, Body.compute_force) DeviceAllocator.parallel_do(Body, Body.body_update)
def kernel_initialize_bodies(): DeviceAllocator.device_class(Body)
def compute_force(self): self.force_x = 0.0 self.force_y = 0.0 DeviceAllocator.device_do(Body, Body.apply_force, self)
def compute_force(self): self.force.to_zero() DeviceAllocator.device_do(Body, Body.apply_force, self)
def kernel_initialize_bodies(): DeviceAllocator.device_class(Cell, Agent, Candidate, Alive)
def update(self): cid: int = self.cell_id if self.action == 2: cells[cid].agent_ = DeviceAllocator.new(Alive, cid) cells[cid].agent_.is_alive = True DeviceAllocator.destroy(self)
from __future__ import annotations import math from typing import List from sanajeh import DeviceAllocator SIZE_X: int = 100 SIZE_Y: int = 100 cells: List[Cell] DeviceAllocator.array_size(cells, 1000) class Cell: agent_: Agent def __init__(self, idx: int): self.agent_ = None cells[idx] = self # todo cell[], alive cell def agent(self) -> Agent: return self.agent_ def is_empty(self) -> bool: return self.agent_ is None class Agent: