pyodide/benchmark/benchmarks/periodic_dist.py

41 lines
1.2 KiB
Python
Raw Normal View History

2018-10-03 18:59:01 +00:00
# setup: import numpy as np ; N = 20 ; x = y = z = np.arange(0., N, 0.1) ; L = 4 ; periodic = True # noqa
# run: periodic_dist(x, x, x, L,periodic, periodic, periodic)
# pythran export periodic_dist(float [], float[], float[], int, bool,
# bool, bool)
import numpy as np
2018-04-05 22:07:33 +00:00
def periodic_dist(x, y, z, L, periodicX, periodicY, periodicZ):
2018-10-03 18:59:01 +00:00
"""Computes distances between all particles and places the result
in a matrix such that the ij th matrix entry corresponds to the
distance between particle i and j"""
2018-04-05 22:07:33 +00:00
N = len(x)
2018-10-03 18:59:01 +00:00
xtemp = np.tile(x, (N, 1))
2018-04-05 22:07:33 +00:00
dx = xtemp - xtemp.T
2018-10-03 18:59:01 +00:00
ytemp = np.tile(y, (N, 1))
2018-04-05 22:07:33 +00:00
dy = ytemp - ytemp.T
2018-10-03 18:59:01 +00:00
ztemp = np.tile(z, (N, 1))
2018-04-05 22:07:33 +00:00
dz = ztemp - ztemp.T
# Particles 'feel' each other across the periodic boundaries
if periodicX:
2018-10-03 18:59:01 +00:00
dx[dx > L / 2] = dx[dx > L / 2] - L
dx[dx < -L / 2] = dx[dx < -L / 2] + L
2018-04-05 22:07:33 +00:00
if periodicY:
2018-10-03 18:59:01 +00:00
dy[dy > L / 2] = dy[dy > L / 2] - L
dy[dy < -L / 2] = dy[dy < -L / 2] + L
2018-04-05 22:07:33 +00:00
if periodicZ:
2018-10-03 18:59:01 +00:00
dz[dz > L / 2] = dz[dz > L / 2] - L
dz[dz < -L / 2] = dz[dz < -L / 2] + L
2018-04-05 22:07:33 +00:00
# Total Distances
2018-10-03 18:59:01 +00:00
d = np.sqrt(dx**2 + dy**2 + dz**2)
2018-04-05 22:07:33 +00:00
# Mark zero entries with negative 1 to avoid divergences
2018-10-03 18:59:01 +00:00
d[d == 0] = -1
2018-04-05 22:07:33 +00:00
return d, dx, dy, dz