-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforce.py
58 lines (48 loc) · 1.91 KB
/
force.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
import numpy as np
from params import *
def accel(Phi):
'''
Description:
Calculate acceleration field using potential.
Input:
- Phi
Gravitational potential field given by the poisson solver.
Return: acc
Acceleration field.
'''
acc = np.zeros((3, Ng, Ng, Ng))
# main part
acc[iz][1: Ng - 1] = (Phi[:Ng-2] - Phi[2:Ng]) / 2
acc[iy][:,1:Ng - 1] = (Phi[:,:Ng-2] - Phi[:,2:Ng]) / 2
acc[ix][:,:,1:Ng-1] = (Phi[:,:,:Ng-2] - Phi[:,:,2:Ng]) / 2
acc[iz][0] = (Phi[-1] - Phi[1]) / 2
acc[iy][:,0] = (Phi[:,-1] - Phi[:,1]) / 2
acc[ix][:,:,0] = (Phi[:,:,-1] - Phi[:,:,1]) / 2
acc[iz][Ng - 1] = (Phi[Ng-2] - Phi[-1]) / 2
acc[iy][:,Ng - 1] = (Phi[:,Ng-2] - Phi[:,-1]) / 2
acc[ix][:,:,Ng-1] = (Phi[:,:,Ng-2] - Phi[:,:,-1]) / 2
return acc
def Force(pos, acc):
'''
Description:
Calculate specific force for a particle.
Input:
- pos:
position of the particle
- acc:
acceleration field
Return:
F: 3D force
'''
F = np.array([0., 0., 0.])
q, p, r = int(np.floor(pos[iz] - 1/2)), int(np.floor(pos[iy] - 1/2)), int(np.floor(pos[ix] - 1/2))
qs, ps, rs = pos[iz] - 1/2 - q, pos[iy] - 1/2 - p, pos[ix] - 1/2 - r
F += acc[:, q % Ng, p % Ng, (r + 1) % Ng] * (1 - ps) * (1 - qs) * rs
F += acc[:, q % Ng, (p + 1) % Ng, (r + 1) % Ng] * ps * (1 - qs) * rs
F += acc[:, (q + 1) % Ng, p % Ng, (r + 1) % Ng] * (1 - ps) * qs * rs
F += acc[:, (q + 1) % Ng, (p + 1) % Ng, (r + 1) % Ng] * ps * qs * rs
F += acc[:, q % Ng, p % Ng, r % Ng] * (1 - ps) * (1 - qs) * (1 - rs)
F += acc[:, q % Ng, (p + 1) % Ng, r % Ng] * ps * (1 - qs) * (1 - rs)
F += acc[:, (q + 1) % Ng, p % Ng, r % Ng] * (1 - ps) * qs * (1 - rs)
F += acc[:, (q + 1) % Ng, (p + 1) % Ng, r % Ng] * ps * qs * (1 - rs)
return F