initial commit of particle cloud classes
This commit is contained in:
13
pyproject.toml
Normal file
13
pyproject.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=61"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "particle-cloud"
|
||||
version = "0.0.1"
|
||||
description = "particle cloud computations"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = []
|
||||
|
||||
[tool.setuptools.packages.find]
|
||||
where = ["src"]
|
||||
3
src/particle_cloud/__init__.py
Normal file
3
src/particle_cloud/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .stuff import Particle, cloud
|
||||
|
||||
__all__ = ["Particle", "cloud"]
|
||||
33
src/particle_cloud/stuff.py
Normal file
33
src/particle_cloud/stuff.py
Normal file
@@ -0,0 +1,33 @@
|
||||
class Particle:
|
||||
def __init__(self, mass=1.0, velocity=None, position=None):
|
||||
self.mass = mass
|
||||
self.velocity = list(velocity if velocity is not None else [0.0, 0.0, 0.0])
|
||||
self.position = list(position if position is not None else [0.0, 0.0, 0.0])
|
||||
|
||||
def __repr__(self):
|
||||
return f"Particle(mass={self.mass}, velocity={self.velocity}, position={self.position})"
|
||||
|
||||
|
||||
class cloud:
|
||||
def __init__(self, particles=None):
|
||||
self.particles = list(particles or [])
|
||||
|
||||
def add(self, p):
|
||||
self.particles.append(p)
|
||||
|
||||
def step(self, dt=1.0):
|
||||
for p in self.particles:
|
||||
p.position = [x + v * dt for x, v in zip(p.position, p.velocity)]
|
||||
return self
|
||||
|
||||
def center(self):
|
||||
total = sum(p.mass for p in self.particles)
|
||||
if total == 0:
|
||||
return [0.0, 0.0, 0.0]
|
||||
answer = [0.0, 0.0, 0.0]
|
||||
for p in self.particles:
|
||||
answer = [a + p.mass * x for a, x in zip(answer, p.position)]
|
||||
return [x / total for x in answer]
|
||||
|
||||
def __repr__(self):
|
||||
return f"cloud({self.particles})"
|
||||
Reference in New Issue
Block a user