-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.py
More file actions
416 lines (324 loc) · 17.6 KB
/
cell.py
File metadata and controls
416 lines (324 loc) · 17.6 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import numpy as np
import numbaAccelerated
from constants import *
from coords import Coords
from thermo import getMeanSquareVelocity
class Cell:
coloringPolicy = "none" # might be "coll", "vx" or "fixed"
collision = True
def __init__(self, nbPart, effectiveTemp, left, right, startIndex, nbPartTarget=None, v_xYVelocityProfile=None,colorRatio=1):
"""
Create a cell
:param nbPart: effective number of part in cell
:param effectiveTemp: temperature required for this cell (It may be different from Cell.initTemp)
:param left: left coordinate of cell
:param right: right coordinate of cell
:param startIndex: first id of particle lying in this cell
:param nbPartTarget: target numer of particles. Useful if initial number of particle is way lower than the target
:param v_xYVelocityProfile: v_x(y) mean velocity profile to be imposed at startup
:param colorRatio : ratio of particles to colorize in white (1)
"""
self.lastNbCollide = 0
self.lastNbCollideInterface = 0
self.left = left
self.right = right
self.length = self.right - self.left
self.startIndex = startIndex
if v_xYVelocityProfile is not None:
self.v_xYVelocityProfile = v_xYVelocityProfile
else:
self.v_xYVelocityProfile = self.zeroV_xYVelocityProfile
self.nbPart = nbPart
if nbPartTarget is None:
nbPartTarget = self.nbPart
self.arraySize = int(nbPartTarget * INITSIZEEXTRARATIO)
# time and iterations count
self.it = 0
self.time = 0
# creation of arrays
self.coords = Coords(self.arraySize)
# creation of swap arrays
swapSize = int(0.1 * self.arraySize)
self.leftSwap = Coords(swapSize)
self.rightSwap = Coords(swapSize)
# thermodynamic instant and averaged variables
self.instantPressure = ComputedConstants.initPressure
self.averagedPressure = self.instantPressure
self.temperature = effectiveTemp
self.averagedTemperature = effectiveTemp
self.ecl = 0. # left of wall kinetic energy
self.ecr = 0.
# living particles
indices = np.linspace(0, self.arraySize - 1, self.nbPart, dtype=np.int32)
assert(self.nbPart == len(np.unique(indices)))
self.coords.wheres[indices] = range(1, self.nbPart + 1)
self.coords.wheres[indices] += self.startIndex
# output buffer
self.upToDate = False
self.positions = np.zeros((self.arraySize, 2), dtype=float) # not used for computations but for opengl draws
# init of locations and velocities
self.randomInit(effectiveTemp,colorRatio)
# neighboring cells
self.leftCell = None
self.rightCell = None
# wall
self.wall = None
def zeroV_xYVelocityProfile(self, y):
"""
initial velocity profile against y coordinate
zero by default ; can be overridden
"""
return 0.
def randomInit(self, effectiveTemp,colorRatio):
vStar = thermo.getMeanSquareVelocity(ComputedConstants.kbs, ComputedConstants.ms, effectiveTemp)
self.coords.vxs = np.random.normal(0, vStar / 2 ** 0.5, self.arraySize)
self.coords.vys = np.random.normal(0, vStar / 2 ** 0.5, self.arraySize)
self.coords.colors = np.random.choice([2.,1.], self.arraySize, p=[1-colorRatio, colorRatio])
# enforce true temperature
indices = np.nonzero(self.coords.wheres != DEAD)
vStarComputed = (np.average(self.coords.vxs[indices] ** 2 + self.coords.vys[indices] ** 2)) ** 0.5
ratio = (vStarComputed / vStar)
self.coords.vxs /= ratio
self.coords.vys /= ratio
# locations and states
if ComputedConstants.fillRatio < 0.1:
# random init
self.coords.xs = self.left + (np.random.random(self.arraySize)) * self.length
for i in range(self.arraySize):
self.coords.ys[i] = (i + 0.5) * ComputedConstants.width / self.arraySize
else:
print("cristal like init")
# cristal like init
L = self.length
H = ComputedConstants.width
deltax = math.sqrt(2 * L * H / self.nbPart / math.sqrt(3.))
deltay = deltax * math.sqrt(3) / 2
nx = int(0.5 + self.length / deltax)
ny = int(0.5 + self.length / deltay)
if (nx - 1) * ny >= self.nbPart:
nx = nx - 1
if nx * (ny - 1) >= self.nbPart:
ny = ny - 1
deltax = deltax * (nx / (nx + 1)) ** 0.5
deltay = deltay * (ny / (ny + 1)) ** 0.5
id0 = self.coords.wheres[indices[0][0]] - 1
for ind in indices[0]:
id = self.coords.wheres[ind] - 1
i = (id - id0) % nx
j = (id - id0) // nx
self.coords.xs[ind] = self.left + i * deltax + deltax / 4 * (-1) ** (j % 2) + 2 * deltax / 3
self.coords.ys[ind] = j * deltay + 2 * deltay / 3
for i in range(self.arraySize):
self.coords.vxs[i] += self.v_xYVelocityProfile(self.coords.ys[i])
self.sort()
self.coords.updateTuple()
##############################################################
################## Compute thermodynamic ################
##############################################################
def computeSumVelocityLeftOfWall(self, wallLocation):
return numbaAccelerated.computeSumVelocityLeftOfWall(self.coords.xs, self.coords.vxs, self.coords.wheres,
wallLocation)
def computeXVelocity(self):
"""
:return: the averaged X velocity of the cell
"""
return numbaAccelerated.ComputeXVelocity(self.coords.vxs, self.coords.wheres)
def computeXVelocityBins(self, nbBins):
"""
:param nbBins: number of bins
:return: the averaged X velocity in bins
"""
bins = np.array([0.] * nbBins)
return numbaAccelerated.ComputeXVelocityBins(self.coords.ys, ComputedConstants.width, self.coords.vxs,
self.coords.wheres, bins)
def ComputeXVelocityBeforeWall(self, nbBins, span, wallLoc):
"""
:param nbBins: number of bins
:param span: x span of measures (measures are performed between Xwall-span and Xwall
:param wallLoc : location of wall
:return: the averaged X velocity in bins
"""
bins = np.array([0.] * nbBins)
counts = np.array([0.] * nbBins)
return numbaAccelerated.ComputeXVelocityBeforeWall(self.coords.xs, wallLoc, span, self.coords.vxs,
self.coords.wheres, bins, counts)
def computeKineticEnergy(self):
"""
:param x: wall location
:return: kinetic energies (left and right of wall)
"""
self.ecl, self.ecr = numbaAccelerated.computeEcs(self.coords.vxs, self.coords.vys, self.coords.wheres,
ComputedConstants.ms)
self.ec = self.ecl + self.ecr
return self.ecl, self.ecr
def computeMacroscopicKineticEnergy(self):
"""
:param x: wall location
:return: kinetic energies (left and right of wall)
"""
self.ecl, self.ecr = numbaAccelerated.computeMacroEcs(self.coords.vxs, self.coords.vys, self.coords.wheres,
ComputedConstants.ms)
self.ec = self.ecl + self.ecr
return self.ecl, self.ecr
def computeTemperature(self):
ecl, ecr = numbaAccelerated.computeEcs(self.coords.vxs, self.coords.vys, self.coords.wheres,
ComputedConstants.ms)
self.temperature = (ecl + ecr) / (self.count() * ComputedConstants.kbs)
alpha = ComputedConstants.alphaAveraging
self.averagedTemperature = alpha * self.temperature + (1 - alpha) * self.averagedTemperature
return self.temperature
def computeColorRatio(self):
return numbaAccelerated.computeColorRatio(self.coords.wheres,self.coords.colors,1.5)
def computePressure(self, fup, fdown, fleft, fright):
"""
update instant and average pressure
:param fup: last computed force on upper wall (in N)
:param fdown: last computed force on lower wall (in N)
:param fleft: last computed force on left static wall (in N), negative if not provided
:param fright: last computed force on right static wall (in N), negative if not provided
:return: None
"""
self.instantPressure = (fup + fdown) / (2 * (self.right - self.left)) # two walls up and down
alpha = ComputedConstants.vStar * ComputedConstants.dt / ComputedConstants.length
self.averagedPressure = alpha * self.instantPressure + (1 - alpha) * self.averagedPressure
def count(self):
ct = numbaAccelerated.countAlive(self.coords.wheres)
if ct / self.arraySize > 0.92:
print("coords arrays arrays nearly saturated : ", ct / self.arraySize)
return ct
def countLeft(self, x):
out = numbaAccelerated.countAliveLeft(self.coords.xs, self.coords.wheres, x)
return out
def countRight(self, x):
out = numbaAccelerated.countAliveRight(self.coords.xs, self.coords.wheres, x)
return out
##############################################################
#################### Swapping ################
##############################################################
def applySwap(self):
"""
swap particles between cells when necessary
:return: None
"""
if self.leftCell is not None:
numbaAccelerated.moveSwapToNeighbor(*self.leftSwap.tpl, *self.leftCell.coords.tpl, self.leftSwap.alive,
ComputedConstants.width)
if self.rightCell is not None:
numbaAccelerated.moveSwapToNeighbor(*self.rightSwap.tpl, *self.rightCell.coords.tpl, self.rightSwap.alive,
ComputedConstants.width)
def applyPeriodic(self):
numbaAccelerated.movePeriodically(self.coords.xs,self.coords.wheres, self.left, self.right)
def prepareSwap(self):
"""
identify particles to be swapped and move them to swap arrays
:return: None
"""
if self.leftCell is not None:
self.leftSwap.alive = numbaAccelerated.moveToSwap(*self.coords.tpl, *self.leftSwap.tpl, self.left, False)
if self.rightCell is not None:
self.rightSwap.alive = numbaAccelerated.moveToSwap(*self.coords.tpl, *self.rightSwap.tpl, self.right, True)
##############################################################
################# Wall and Collisions #################
##############################################################
def wallBounce(self):
# up and down
vStarBoundary = -1
if ComputedConstants.boundaryTemperatureUp > 0:
vStarBoundary = getMeanSquareVelocity(ComputedConstants.kbs, ComputedConstants.ms,
ComputedConstants.boundaryTemperatureUp)
fup = numbaAccelerated.staticWallInterractionUp(self.coords.ys, self.coords.vxs, self.coords.vys,
self.coords.wheres,self.coords.lastColls,self.coords.colors, ComputedConstants.width,
ComputedConstants.dt, ComputedConstants.ms,
vStarBoundary,ComputedConstants.time)
vStarBoundary = -1
if ComputedConstants.boundaryTemperatureDown > 0:
vStarBoundary = getMeanSquareVelocity(ComputedConstants.kbs, ComputedConstants.ms,
ComputedConstants.boundaryTemperatureDown)
fdown = numbaAccelerated.staticWallInterractionDown(self.coords.ys, self.coords.vxs, self.coords.vys,
self.coords.wheres, self.coords.lastColls, self.coords.colors,
ComputedConstants.width,
ComputedConstants.dt, ComputedConstants.ms,
vStarBoundary, ComputedConstants.time)
# moving Wall
if self.wall is not None:
newX, newV = numbaAccelerated.movingWallInteraction(self.coords.xs, self.coords.vxs, self.coords.vys,
self.coords.wheres,self.coords.lastColls,self.coords.colors, self.wall.location(),
self.wall.velocity(),
ComputedConstants.dt, ComputedConstants.ms,
self.wall.mass(),ComputedConstants.time)
self.wall.setLocVel(newX,newV)
fleft = -1
fright = -1
# left wall
if self.leftCell is None and not ComputedConstants.periodic:
fleft = numbaAccelerated.staticWallInteractionLeft(self.coords.xs, self.coords.vxs, self.coords.vys,
self.coords.wheres,self.coords.lastColls,self.coords.colors, self.left, ComputedConstants.dt,
ComputedConstants.ms,ComputedConstants.time)
# right wall
if self.rightCell is None and not ComputedConstants.periodic:
fright = numbaAccelerated.staticWallInteractionRight(self.coords.xs, self.coords.vxs, self.coords.vys,
self.coords.wheres,self.coords.lastColls,self.coords.colors, self.right, ComputedConstants.dt,
ComputedConstants.ms,ComputedConstants.time)
self.computePressure(fup, fdown, fleft, fright)
def interfaceCollide(self):
if self.leftCell is not None:
self.lastNbCollideInterface = numbaAccelerated.detectCollisionsAtInterface(self.coords.indicesLeftOfCell,
self.leftCell.coords.indicesRightOfCell,
*self.coords.tpl,
*self.leftCell.coords.tpl,
ComputedConstants.dt,
ComputedConstants.ds,
ComputedConstants.time,
)
def collide(self):
"""
Compute collisions between particles
nbNeighbour is the number of neighbours par particle i which are checked
:return: None
"""
self.lastNbCollide = numbaAccelerated.detectAllCollisions(*self.coords.tplExtended,
ComputedConstants.dt,
ComputedConstants.ds,
None, Cell.coloringPolicy, self.left,
self.right, ComputedConstants.time
)
def sort(self):
return self.coords.sort()
##############################################################
################# Helper functions ##################
##############################################################
def checkCorrectSide(self):
numbaAccelerated.checkCorrectSide(self.coords.wheres, self.coords.xs, self.wall.location())
def updateConstants(self):
self.upToDate = False # invalidate position buffer
self.count()
# self.computeTemperature()
def getPositionsBuffer(self):
if not self.upToDate:
numbaAccelerated.twoArraysToOne(self.coords.xs, self.coords.ys, self.coords.wheres, self.positions)
return self.positions
def advect(self):
numbaAccelerated.advect(self.coords.xs, self.coords.ys, self.coords.vxs, self.coords.vys, ComputedConstants.dt,
ComputedConstants.forceX, ComputedConstants.ms)
def middle(self):
return (self.left + self.right) * 0.5
def updateIndicesAccordingToWall(self, x):
"""
Negates indices of particle initially left of wall
:param x: wall location
:return: None
"""
for i in range(len(self.coords.xs)):
if self.coords.xs[i] < x:
self.coords.wheres[i] *= -1
##############################################################
################### Update cell #################
##############################################################
def update(self):
# update everything before interface collisions
self.advect()
if Cell.collision:
self.sort()
self.collide()
self.wallBounce()
self.updateConstants()