forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcevolve.pyx
More file actions
57 lines (44 loc) · 1.45 KB
/
cevolve.pyx
File metadata and controls
57 lines (44 loc) · 1.45 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
import numpy as np
cimport cython
from libc.math cimport sqrt
from cython.parallel cimport prange
@cython.boundscheck(False)
@cython.cdivision(True)
def c_evolve(double[:, :] r_i,double[:] ang_speed_i,
double timestep,int nsteps):
cdef int i
cdef int j
cdef int nparticles = r_i.shape[0]
cdef double norm, x, y, dx, dy, vx, vy, ang_speed
for i in range(nsteps):
for j in range(nparticles):
x = r_i[j, 0]
y = r_i[j, 1]
ang_speed = ang_speed_i[j]
norm = sqrt(x ** 2 + y ** 2)
vx = (-y)/norm
vy = x/norm
dx = timestep * ang_speed * vx
dy = timestep * ang_speed * vy
r_i[j, 0] += dx
r_i[j, 1] += dy
@cython.boundscheck(False)
@cython.cdivision(True)
def c_evolve_openmp(double[:, :] r_i,double[:] ang_speed_i,
double timestep,int nsteps):
cdef int i
cdef int j
cdef int nparticles = r_i.shape[0]
cdef double norm, x, y, dx, dy, vx, vy, ang_speed
for j in prange(nparticles, nogil=True):
for i in range(nsteps):
x = r_i[j, 0]
y = r_i[j, 1]
ang_speed = ang_speed_i[j]
norm = sqrt(x ** 2 + y ** 2)
vx = (-y)/norm
vy = x/norm
dx = timestep * ang_speed * vx
dy = timestep * ang_speed * vy
r_i[j, 0] += dx
r_i[j, 1] += dy