forked from floli/PyRBF
-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo_PoU.py
305 lines (268 loc) · 8.49 KB
/
demo_PoU.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
""" Generates data to show the effect of rescaling. Low density basisfunctions used. """
import pandas
import os
import logging
from rbf import *
import basisfunctions, testfunctions
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import time
import mesh
import math
from random import randint
from scipy import spatial
from halton import *
import vtk
import mesh_io
class Mesh:
"""
A Mesh consists of:
- Points: A list of tuples of floats representing coordinates of points
- Cells: A list of tuples of ints representing mesh elements
- Pointdata: A list of floats representing data values at the respective point
"""
def __init__(self, points = None, cells = None, cell_types = None, pointdata = None):
if points is not None:
self.points = points
else:
self.points = []
if cells is not None:
assert(cell_types is not None)
self.cells = cells
self.cell_types = cell_types
else:
self.cells = []
self.cell_types = []
if pointdata is not None:
self.pointdata = pointdata
else:
self.pointdata = []
def __str__(self):
return "Mesh with {} Points and {} Cells ({} Cell Types)".format(len(self.points), len(self.cells), len(self.cell_types))
def read_mesh(filename):
points, cells, cell_types, pointdata = mesh_io.read_mesh(filename)
#print("Points: ", len(points))
#print("Point data: ", pointdata)
return Mesh(points, cells, cell_types, pointdata)
#mesh_name = "Mesh/Plate/l1Data.vtk"
#mesh = read_mesh(mesh_name)
#print("Points: ", mesh.points)
dimension_M = 2 # dimension of problem
#nPoints = pow(90,2)
nPoints = 40
#nPoints = len(mesh.points) # number of points
start = time.time()
j = 0
nPointsOut = 1
print("Number of points: ",nPoints)
in_mesh = np.random.random((nPoints,2))
haltonPoints = halton_sequence(nPoints, 2)
for i in range(0,nPoints):
#in_mesh[i,0] = mesh.points[i][0]
#in_mesh[i,1] = mesh.points[i][1]
in_mesh[i,0] = haltonPoints[0][i]
in_mesh[i,1] = haltonPoints[1][i]
# Find Lbox
x_min = min(in_mesh[:,0])
x_max = max(in_mesh[:,0])
y_min = min(in_mesh[:,1])
y_max = max(in_mesh[:,1])
l_min = min(x_min,y_min)
l_max = max(x_max,y_max)
x_range = x_max - x_min
y_range = y_max - y_min
max_values = [x_max, y_max]
print("x min: ", x_min)
print("x max: ", x_max)
print("y min: ", y_min)
print("y max: ", y_max)
hyperVolume = (x_max-x_min)*(y_max-y_min)
print("Hypervolume: ", hyperVolume)
# Number of patches per dimension
#hpParameter = 60 # Find value to automate parameter
hpParameter = 0.5*nPoints/pow(nPoints,0.5)
#nPatchCentres = math.floor(0.5*(l_max - l_min)*pow(nPoints/(hpParameter*hyperVolume),1/dimension_M))
nPatchCentres = math.floor(1*pow(nPoints/2,1/dimension_M))
print("n Patch Centres: ", nPatchCentres)
#Radius of the patch (half the diameter)
patchRadii = (l_max - l_min)/nPatchCentres
print("n Patch Radii: ", patchRadii)
puCentres = np.random.random((pow(nPatchCentres,dimension_M),2))
j = 0
k = 0
patchOffset = 1/nPatchCentres
for i in range(0,pow(nPatchCentres,dimension_M)):
if (j == nPatchCentres):
j = 0
k += 1
puCentres[i,0] = j*1.*patchRadii + patchOffset*patchRadii
puCentres[i,1] = k*1.*patchRadii + patchOffset*patchRadii
#print("PU coords: ", puCentres[i,0], puCentres[i,1])
j += 1
# find q^M blocks
q = math.ceil((l_max - l_min)/patchRadii)
print("q: ", q)
### Find the block where the PU domain centre lies in. Similar to linked cell method
'''
km = 0
kthBlock = 0
puKBlocks = []
for i in range(0,pow(nPatchCentres,2)):
kthBlock = 0
for k in range(0,dimension_M-1):
km = math.ceil(puCentres[i,k]/patchRadii)
if (km < 1):
km = 1
kthBlock += (km - 1)*pow(q,dimension_M-k-1)
kthBlock += math.ceil(puCentres[i,dimension_M-1]/patchRadii)
#if (kthBlock < 1):
# kthBlock += q
print("kthBlock: ", kthBlock)
puKBlocks.append(kthBlock)
'''
### Repeat the method for all in_mesh points
'''
km = 0
kthBlock = 0
in_mesh_KBlocks = []
for i in range(0,nPoints):
kthBlock = 0
for k in range(0,dimension_M-1):
km = math.ceil(in_mesh[i,k]/patchRadii)
kthBlock += (km - 1)*pow(q,dimension_M-k-1)
kthBlock += math.ceil(in_mesh[i,dimension_M-1]/patchRadii)
in_mesh_KBlocks.append(kthBlock)
print("in_mesh_KBlocks: ", in_mesh_KBlocks)
'''
### Old method works fine. Add new linked cell algorithm from - Linked-List Cell Molecular Dynamics-
'''
km = 0
kthBlock = 0
puKBlocks = []
for i in range(0,pow(nPatchCentres,dimension_M)):
km = 0
km = math.floor(puCentres[i,1]/patchRadii)*nPatchCentres
km += math.floor(puCentres[i,0]/patchRadii)
puKBlocks.append(km)
print("puKBlocks: ", puKBlocks)
km = 0
kthBlock = 0
in_mesh_KBlocks = []
for i in range(0,nPoints):
km = 0
km = math.floor(in_mesh[i,1]/patchRadii)*nPatchCentres
km += math.floor(in_mesh[i,0]/patchRadii)
in_mesh_KBlocks.append(km)
print("in_mesh_KBlocks: ", in_mesh_KBlocks)
'''
#headPatches = -1*np.ones(pow(nPatchCentres,2))
#print("headPatches: ", headPatches)
#lsclPatches = -1*np.ones(nPoints)
#print("lsclPatches: ", lsclPatches)
headPatches = []
lsclPatches = []
for k in range(0,pow(nPatchCentres,dimension_M)):
headPatches.append(-1)
for k in range(0,pow(nPatchCentres,dimension_M)):
lsclPatches.append(-1)
#print("headPatches: ", headPatches)
for i in range(0,pow(nPatchCentres,dimension_M)):
mc = [0,0,0]
c = 0
for j in range(0,dimension_M):
mc[j] = math.floor(puCentres[i,j]/patchRadii)
if puCentres[i,j] >= max_values[j]:
mc[j] -= 1
c = mc[0]*pow(nPatchCentres,0) + mc[1]*nPatchCentres + mc[2]
#print("c: ", c,mc[0], mc[1], mc[2])
lsclPatches[i] = headPatches[int(c)]
headPatches[int(c)] = i
#print("headPatches: ", headPatches)
#print("lsclPatches: ", lsclPatches)
headInterpPoints = []
lsclInterpPoints = []
for k in range(0,pow(nPatchCentres,dimension_M)):
headInterpPoints.append(-1)
for k in range(0,nPoints):
lsclInterpPoints.append(-1)
#print("headInterpPoints: ", headInterpPoints)
regionWidth = patchRadii + 0.001*patchRadii
print("regionWidth: ", regionWidth)
for i in range(0,nPoints):
mc = [0,0,0]
c = 0
for j in range(0,dimension_M):
mc[j] = math.floor(in_mesh[i,j]/regionWidth)
if in_mesh[i,j] >= max_values[j]:
mc[j] -= 1
c = mc[0]*1 + mc[1]*nPatchCentres + mc[2]
#print("c: ", c,mc[0], mc[1], mc[2])
lsclInterpPoints[i] = headInterpPoints[int(c)]
headInterpPoints[int(c)] = i
#print("headInterpPoints: ", headInterpPoints)
#print("lsclInterpPoints: ", lsclInterpPoints)
# -------------------------------
#
# Loop through all blocks defined by q. Loop through each vertex in blocks k-q-1, k-q, k-q+1,
# k-1, k, k+1, k+q-1, k+q, k+q+1.
#
# -------------------------------
surroundBlocks = [-q-1, -q, -q+1, -1, 0, 1, q-1, q, q+1]
print("surroundBlocks: ",surroundBlocks)
# Loop through each patch
#for i in range(0,nPatchCentres): # loops in x direction
# for j in range(0,nPatchCentres): # loops in y direction
#pointsInPatchWithinRadius = []
for i in range(0,len(headPatches)):
pointsInPatchWithinRadius = []
xPatchCentre = puCentres[i,0]
yPatchCentre = puCentres[i,1]
for k in range(0,9):
blockNum = surroundBlocks[k] + i
#print("Block num: ", blockNum)
if (blockNum >= 0):
if (blockNum < len(headPatches)):
vertexID = lsclInterpPoints[headInterpPoints[blockNum]]
while (vertexID > 0):
#print("Vertex : ", vertexID)
x = in_mesh[vertexID,0]
y = in_mesh[vertexID,1]
r = math.sqrt(pow(x-xPatchCentre,2) + pow(y-yPatchCentre,2))
if (r <= patchRadii*1):
pointsInPatchWithinRadius.append(vertexID)
vertexID = lsclInterpPoints[vertexID]
#print("pointsInPatchWithinRadius: ", pointsInPatchWithinRadius)
### Find points in each cells using the arrays
'''
for i in range(0,pow(nPatchCentres,dimension_M)):
index = headInterpPoints[i]
indexLSCL = lsclInterpPoints[index]
while (indexLSCL > -1):
print("Value in cell: ", i, " - is: ", indexLSCL)
indexLSCL = lsclInterpPoints[indexLSCL]
'''
end = time.time()
print("Elapsed time to allocate linked list data structures: ", end - start)
out_mesh = np.random.random((nPointsOut,2))
#for i in range(0,nPointsOut):
# out_mesh[i,0] = haltonPoints[0][i] #+ 0.0001
# out_mesh[i,1] = haltonPoints[1][i] #+ 0.01
#in_mesh[0,0] = out_mesh[0,0]
#in_mesh[0,1] = out_mesh[0,1]
'''
km = 0
kthBlock = 0
out_mesh_KBlocks = []
for i in range(0,nPointsOut):
km = 0
km = math.floor(out_mesh[i,1]/patchRadii)*nPatchCentres
km += math.floor(out_mesh[i,0]/patchRadii)
out_mesh_KBlocks.append(km)
print("out_mesh_KBlocks: ", out_mesh_KBlocks)
'''
tree = spatial.KDTree(list(zip(in_mesh[:,0],in_mesh[:,1])))
nearest_neighbors = []
shape_params = []
plt.scatter(puCentres[:,0], puCentres[:,1], label = "In Mesh", s=20)
plt.scatter(in_mesh[:,0], in_mesh[:,1], label = "Out Mesh", s=2)
plt.show()