-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetwork.py
More file actions
68 lines (49 loc) · 1.63 KB
/
Network.py
File metadata and controls
68 lines (49 loc) · 1.63 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
from numba import njit
class Network:
def __init__(self, nodes, coords, links):
self.nodes = nodes
self.coords = coords
self.links = links
self.num_nodes = nodes.shape[0]
class Interaction_Network(Network):
def __init__(self, nodes, coords, r):
distances = self._get_distances(coords)
links = self._get_links(distances, r)
super().__init__(nodes, coords, links)
self.r = r
@staticmethod
@njit
def _get_distances(coords):
num_nodes = coords.shape[0]
distances = np.zeros((num_nodes, num_nodes))
for i in range(num_nodes):
for j in range(i):
d = np.sqrt(np.sum((coords[i] - coords[j])**2))
distances[i, j] = distances[j, i] = d
return distances
@staticmethod
def _get_links(distances, r):
links = distances < r
np.fill_diagonal(links, False)
return links
class Mesh(Interaction_Network):
def __init__(self, nodes, r):
n = nodes.shape[0]
coords = self._get_coords(n)
super().__init__(nodes, coords, r)
@staticmethod
def _get_coords(n):
x = y = np.linspace(0, 1, np.ceil(np.sqrt(n)).astype(np.int32))
X, Y = np.meshgrid(x, y)
coords = np.stack([X.flat, Y.flat], axis=1)[:n]
return coords
class RGG(Interaction_Network):
def __init__(self, nodes, r):
n = nodes.shape[0]
coords = self._get_coords(n)
super().__init__(nodes, coords, r)
@staticmethod
def _get_coords(n):
coords = np.random.rand(n, 2)
return coords