-
Notifications
You must be signed in to change notification settings - Fork 1
/
atom.py
47 lines (33 loc) · 1.13 KB
/
atom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from dataclasses import dataclass
from random import uniform
from copy import deepcopy
from math import dist
from numpy import array
@dataclass
class Atom:
position: array
A: float = 1
B: float = 1
def __post_init__(self) -> None:
self.old_positions = []
def get_distance_to(self, atom: "Atom") -> float:
return dist(self.position,
atom.position)
def get_energy_with(self, atom: "Atom") -> float:
A_total = self.A + atom.A
B_total = self.B + atom.B
r = self.get_distance_to(atom)
if r == 0:
return float("inf")
U = (A_total / (r**12)) - (B_total / (r**6))
return float(U)
def move(self, vector: array) -> None:
self.old_positions.append(deepcopy(self.position))
self.position += vector
def restore_previous_pos(self):
self.position = self.old_positions.pop()
@staticmethod
def generate_random_vector(min_=-.1, max_=.1, axes=2):
return array([uniform(min_, max_) for _ in range(axes)])
def __str__(self: "Atom") -> str:
return (f"[Pos] \n {self.position}\n")