-
Notifications
You must be signed in to change notification settings - Fork 2
/
LJ_multi_4.py
65 lines (51 loc) · 1.93 KB
/
LJ_multi_4.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
#!/bin/env python3
from lennard_jones_potential import *
from multiprocessing import Pool
import numpy as np
import time
def generate_pairs(n):
"generate unique point pairs (i<j)"
pairs = []
for i in range(n):
for j in range(n):
if j>i:
pairs.append((i,j))
return pairs
def calc_pot(args):
"calculate the distance for a pair i j in coords and return LJ potential"
i, j, coords = args # unpack arguments tupel
r = np.sqrt(np.sum(np.square(np.subtract(coords[i],coords[j]))))
v = v_LJ(r)
return v
def main_4():
"Calculate distances in parallel as well."
n_points = 2000
print("Generating coordinates...", end='', flush=True)
start = time.time()
coords = generate_coodinates(n_points, 3, upper=100, seed=5)
stop = time.time()
print("done!\t\tRuntime: {:6.3f} seconds".format(stop-start))
print("Generating pairs...", end='', flush=True)
start = time.time()
pairs = generate_pairs(len(coords))
stop = time.time()
print("done!\t\tRuntime: {:6.3f} seconds".format(stop-start))
print(20*"-"+"\n")
for n_CPUs in [1, 2, 4]:
print("Parallel LJ-Potential on {} points using {} processes:".format(n_points, n_CPUs))
pool = Pool(n_CPUs)
print("Calculating LJ on {} CPUs ...".format(n_CPUs), end='', flush=True)
start = time.time()
tasks = [] # prepare tasks
for p in pairs:
args = (p[0], p[1], coords) # pack arguments tupel
tasks.append(args)
results = pool.map(calc_pot, tasks) # <-- in parallel
pool.close()
stop = time.time()
print("done!\tRuntime: {:6.3f} seconds".format(stop-start))
v_total = np.sum(results) # sum up total
print("v_Total: {:.2f}".format(v_total)) # print result
print(20*"-"+"\n")
if __name__ == '__main__':
main_4()