Newer
Older
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# @Author: Felix Kramer
# @Date: 2021-05-22T13:11:37+02:00
# @Email: kramer@mpi-cbg.de
# @Project: go-with-the-flow
# @Last modified by: Felix Kramer
# @Last modified time: 2021-05-22T22:15:12+02:00
# @License: MIT
import networkx as nx
import scipy as sc
from scipy.spatial import Voronoi
import random as rd
def init_graph_from_random(random_type,periods,sidelength):
choose_constructor_option={ 'default': networkx_voronoi_planar, 'voronoi_planar': networkx_voronoi_planar, 'voronoi_volume': networkx_voronoi_volume}
if random_type in choose_constructor_option:
random=choose_constructor_option[random_type](periods,sidelength)
else :
print('Warning, crystal type unknown, set default: simple')
random=choose_constructor_option['default'](periods,sidelength)
return random.G
class networkx_random():
def __init__(self):
self.G=nx.Graph()
def mirror_boxpoints(self,points,sl):
points_matrix=points
intervall=[-1,0,1]
for i in intervall:
for j in intervall:
if (i!=0 or j!=0):
points_matrix=sc.concatenate((points_matrix,points+(i*sl,j*sl)))
return points_matrix
def mirror_cubepoints(self,points,sl):
points_matrix=points
intervall=[-1,0,1]
for i in intervall:
for j in intervall:
for k in intervall:
if (i!=0 or j!=0 or k!=0):
points_matrix=sc.concatenate((points_matrix,points+(i*sl,j*sl,k*sl)))
return points_matrix
# construct random 3d graph, confined in a box
def is_in_box(self,v,sl):
answer=True
if (v[0] > sl) or (v[0] < -sl):
answer=False
if (v[1] > sl) or (v[1] < -sl):
answer=False
if (v[2] > sl) or (v[2] < -sl):
answer=False
return answer
class networkx_voronoi_planar(networkx_random,object):
def __init__(self, num_periods,sidelength):
super(networkx_voronoi_planar,self).__init__()
self.random_voronoi_periodic(num_periods,sidelength)
# construct random 2d graph, confined in a certain spherical boundary, connections set via voronoi tesselation
def construct_voronoi_periodic(self,number,sidelength):
V=0
# create points for voronoi tesselation
XY=[]
for i in range(number):
x=rd.uniform(0,sidelength)
y=rd.uniform(0,sidelength)
XY.append((x,y))
self.XY=XY
XY=self.mirror_boxpoints(sc.array(XY),sidelength)
self.XY_periodic=XY
V=Voronoi(XY)
return V
def random_voronoi_periodic(self,number,sidelength):
#construct a core of reandom points in 2D box for voronoi tesselation, mirror the primary box so a total of 9 cells is created with the initial as core
V=self.construct_voronoi_periodic(number,sidelength)
#pick up the face of the core which correspond to a periodic voronoi lattice
faces=[]
for j,i in enumerate(V.point_region):
faces.append(sc.asarray(V.regions[i]))
if j==number-1:
break
#use periodic kernel to construct the correponding network
faces=sc.asarray(faces)
f=faces[0]
for i in range(len(faces[:])):
if i+1==len(faces[:]):
break
f=sc.concatenate((f,faces[i+1]))
for i in faces:
for j in i:
v=V.vertices[j]
self.G.add_node(j,pos=v,lablel=j)
k=0
for i in V.ridge_vertices:
mask=sc.in1d(i,f)
if sc.all( mask == True ):
for l in range(len(i)):
h=len(i)-1
self.G.add_edge(i[h-(l+1)],i[h-l],slope=(V.vertices[i[h-(l+1)]],V.vertices[i[h-l]]), label=k)
k+=1
if len(i)==2:
break
class networkx_voronoi_volume(networkx_random,object):
def __init__(self, num_periods,sidelength):
super(networkx_voronoi_volume,self).__init__()
self.random_voronoi_periodic(num_periods,sidelength)
# construct random 3d graph, confined in a certain spherical boundary, connections set via voronoi tesselation
def construct_voronoi_periodic(self,number,sidelength):
V=0
# create points for voronoi tesselation
XYZ=[]
for i in range(number):
x=rd.uniform(0,sidelength)
y=rd.uniform(0,sidelength)
z=rd.uniform(0,sidelength)
XYZ.append((x,y,z))
self.XYZ=XYZ
XYZ=self.mirror_cubepoints(sc.array(XYZ),sidelength)
self.XYZ_periodic=XYZ
V=Voronoi(XYZ)
return V
def random_voronoi_periodic(self,number,sidelength):
#construct a core of reandom points in 2D box for voronoi tesselation, mirror the primary box so a total of 9 cells is created with the initial as core
V=self.construct_voronoi_periodic(number,sidelength)
#pick up the face of the core which correspond to a periodic voronoi lattice
faces=[]
for j,i in enumerate(V.point_region):
faces.append(sc.asarray(V.regions[i]))
if j==number-1:
break
#use periodic kernel to construct the correponding network
faces=sc.asarray(faces)
f=faces[0]
for i in range(len(faces[:])):
if i+1==len(faces[:]):
break
f=sc.concatenate((f,faces[i+1]))
for i in faces:
for j in i:
v=V.vertices[j]
self.G.add_node(j,pos=v,lablel=j)
k=0
for i in V.ridge_vertices:
mask=sc.in1d(i,f)
if sc.all( mask == True ):
for l in range(len(i)):
h=len(i)-1
self.G.add_edge(i[h-(l+1)],i[h-l],slope=(V.vertices[i[h-(l+1)]],V.vertices[i[h-l]]), label=k)
k+=1
if len(i)==2:
break