Skip to content

Commit 61e4ce7

Browse files
committed
draft of benchmark
1 parent 320f25e commit 61e4ce7

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

polychrom/benchmark.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
A simple benchmark for polychrom.
3+
4+
This benchmark is a simple simulation of a polymer chain in a cubic box with PBC,
5+
at density of 0.25.
6+
It defaults to a chain with N=100,000 monomers, but it can be changed by passing N as a first argument.
7+
8+
The simulation is run for 10 blocks of 5,000 steps each.
9+
10+
The benchmark reports initialization time in ms, as well as the average steps per second (SPS) for the simulation.
11+
12+
The benchmark can be run with the following command:
13+
python -m polychrom.benchmark [N] [GPU]
14+
"""
15+
16+
17+
from polychrom.simulation import Simulation
18+
from polychrom.forces import harmonic_bonds, polynomial_repulsive, angle_force
19+
from polychrom.forcekits import polymer_chains
20+
from polychrom.starting_conformations import grow_cubic
21+
import sys
22+
import datetime as dt
23+
24+
25+
26+
def run_behcnmark(*args):
27+
# Get the number of monomers from the command line argument
28+
# measure time
29+
N = int(args[1]) if len(args) > 1 else 100_000
30+
GPU = args[2] if len(args) > 2 else "0"
31+
box_size = N ** (1 / 3) / 0.25
32+
33+
polymer = grow_cubic(N, boxSize=int(box_size)-2)
34+
35+
time_start = dt.datetime.now()
36+
37+
38+
# Create a simulation object
39+
sim = Simulation(
40+
collision_rate = 0.02,
41+
N = N,
42+
error_tol=0.003,
43+
PBCbox = (box_size, box_size, box_size),
44+
GPU = GPU,
45+
)
46+
47+
sim.set_data(polymer, center=True) # loads a polymer, puts a center of mass at zero
48+
49+
50+
sim.add_force(
51+
polymer_chains(
52+
sim,
53+
chains=[(0, None, False)],
54+
bond_force_func=harmonic_bonds,
55+
bond_force_kwargs={
56+
"bondLength": 1.0,
57+
"bondWiggleDistance": 0.05, # Typical parameters used in a sim
58+
},
59+
angle_force_func=angle_force,
60+
angle_force_kwargs={
61+
"k": 1.5, # gentle angle force
62+
},
63+
nonbonded_force_func=polynomial_repulsive,
64+
nonbonded_force_kwargs={
65+
"trunc": 5.0, # on the border of chain crossing - good for benchmark
66+
},
67+
except_bonds=True,
68+
)
69+
)
70+
sim.do_block(1)
71+
72+
73+
# init time
74+
init_time = dt.datetime.now() - time_start
75+
76+
time_start = dt.datetime.now()
77+
78+
for _ in range(10): # Do 10 blocks
79+
sim.do_block(5000) # Of 100 timesteps each. Data is saved automatically.
80+
81+
sim_time = dt.datetime.now() - time_start
82+
sps = (10 * 5000) / sim_time.total_seconds()
83+
84+
print(f"Initialization time: {init_time.total_seconds() * 1000:.2f} ms")
85+
print(f"Average steps per second: {sps:.2f} SPS")
86+
87+
88+
if __name__ == "__main__":
89+
run_behcnmark(*sys.argv)

0 commit comments

Comments
 (0)