RMF
benchmark_rmf.py
1 ## \example benchmark/benchmark_rmf.py
2 # \brief Benchmark operations on the RMF from python to see how much overhead
3 # that adds
4 
5 from __future__ import print_function
6 import RMF
7 import datetime
8 
9 scale = 5
10 
11 
12 def create_residue(nh, af, pf):
13  for i in range(0, 2 * scale):
14  child = nh.add_child("CA", RMF.REPRESENTATION)
15  pf.get(child).set_mass(1)
16  pf.get(child).set_radius(1.0 + i / 18.77)
17  af.get(child).set_element(7)
18 
19 
20 def create_chain(nh, rf, af, pf):
21  for i in range(0, 60 * scale):
22  child = nh.add_child(str(i), RMF.REPRESENTATION)
23  rf.get(child).set_residue_type("cys")
24  rf.get(child).set_residue_index(i)
25  create_residue(child, af, pf)
26 
27 
28 def create_hierarchy(file):
29  cf = RMF.ChainFactory(file)
30  af = RMF.AtomFactory(file)
31  rf = RMF.ResidueFactory(file)
32  pf = RMF.ParticleFactory(file)
33  n = file.get_root_node()
34  for i in range(0, 3 * scale):
35  child = n.add_child(str(i), RMF.REPRESENTATION)
36  cf.get(child).set_chain_id(str(i))
37  create_chain(child, rf, af, pf)
38 
39 
40 def create_frame(fh, ipf, atoms, frame):
41  ret = [0.0, 0.0, 0.0]
42  for n in atoms:
43  v = RMF.Vector3((1.0 * n.get_index() + 0 + frame) / 17.0,
44  (1.0 * n.get_index() + 1 + frame) / 19.0,
45  (1.0 * n.get_index() + 2 + frame) / 23.0)
46  ret[0] += v[0]
47  ret[1] += v[1]
48  ret[2] += v[2]
49  ipf.get(fh.get_node(n)).set_coordinates(v)
50  return ret[0] + ret[1] + ret[2]
51 
52 
53 def create(file):
54  create_hierarchy(file)
55  atoms = []
56  for n in file.get_node_ids():
57  if len(file.get_node(n).get_children()) == 0:
58  atoms.append(n)
59  ipf = RMF.IntermediateParticleFactory(file)
60  ret = 0.0
61  for i in range(0, 20):
62  file.add_frame("frame", RMF.FRAME)
63  ret += create_frame(file, ipf, atoms, i)
64  return ret
65 
66 
67 def traverse(file):
68  ret = 0.0
69  queue = [file.get_root_node()]
70  ipcf = RMF.IntermediateParticleConstFactory(file)
71  while (len(queue) > 0):
72  cur = queue[-1]
73  queue = queue[:-1]
74 
75  if ipcf.get_is(cur):
76  ret += ipcf.get(cur).get_radius()
77  queue += cur.get_children()
78 
79  return ret
80 
81 
82 def load(file, nodes):
83  ipcf = RMF.IntermediateParticleConstFactory(file)
84  v = [0.0, 0.0, 0.0]
85  for fr in file.get_frames():
86  file.set_current_frame(fr)
87  for n in nodes:
88  cur = ipcf.get(file.get_node(n)).get_coordinates()
89  v[0] += cur[0]
90  v[1] += cur[1]
91  v[2] += cur[2]
92  return v[0] + v[1] + v[2]
93 
94 
95 def benchmark_create(file, type):
96  start = datetime.datetime.now()
97  dist = create(file)
98  print(type, "create,", datetime.datetime.now() - start, ",", dist)
99 
100 
101 def benchmark_traverse(file, type):
102  start = datetime.datetime.now()
103  file.set_current_frame(RMF.FrameID(0))
104  t = traverse(file)
105  print(type, "traverse,", datetime.datetime.now() - start, ",", t)
106 
107 
108 def benchmark_load(file, type):
109  nodes = []
110  ipcf = RMF.IntermediateParticleConstFactory(file)
111  for n in file.get_node_ids():
112  if ipcf.get_is(file.get_node(n)):
113  nodes.append(n)
114  start = datetime.datetime.now()
115  dist = load(file, nodes)
116  print(type, "load,", datetime.datetime.now() - start, ",", dist)
117 
118 name = "benchmark_python.rmf"
119 fh = RMF.create_rmf_file(name)
120 benchmark_create(fh, " python rmf")
121 del fh
122 
124 benchmark_traverse(fh, "python rmf")
125 benchmark_load(fh, "python rmf")
FileConstHandle open_rmf_file_read_only(std::string path)
FileHandle create_rmf_file(std::string path)
Create an RMF from a file system path.