4 from __future__
import print_function
8 number_of_particles = 10
15 tfn = RMF._get_temporary_file_path(
"motion.rmf3")
21 tf = RMF.TypedFactory(fh)
22 pf = RMF.ParticleFactory(fh)
23 bf = RMF.BondFactory(fh)
24 sf = RMF.SegmentFactory(fh)
25 rf = RMF.RepresentationFactory(fh)
26 scf = RMF.ScoreFactory(fh)
33 def get_distance(v0, r0, v1, r1):
35 ((v0[0] - v1[0]) ** 2 + (v0[1] - v1[1])
36 ** 2 + (v0[1] - v1[1]) ** 2) ** .5 - r0 - r1
40 def intersects(c, r, cs, rs):
41 for oc, orad
in zip(cs, rs):
42 if get_distance(c, r, oc, orad) < 0:
48 def too_far(c, r, oc, orad):
49 d = get_distance(c, r, oc, orad)
55 def random_coordinates():
57 random.uniform(0, box_size),
58 random.uniform(0, box_size))
60 for p
in range(0, number_of_particles):
61 cur = fh.get_root_node().add_child(str(p), RMF.REPRESENTATION)
62 tf.get(cur).set_type_name(str(p % 3))
64 pd.set_static_radius(random.uniform(minimum_radius, maximum_radius))
65 pd.set_static_mass(pd.get_radius())
66 curcoord = random_coordinates()
67 while intersects(curcoord, pd.get_radius(), coords, radii):
68 curcoord = random_coordinates()
69 radii.append(pd.get_radius())
70 coords.append(curcoord)
73 bn = fh.get_root_node().add_child(
"bonds", RMF.ORGANIZATIONAL)
76 print(
"creating bonds")
78 bd0 = random.choice(particles)
79 bd1 = random.choice(particles)
82 bp = (particles.index(bd0), particles.index(bd1))
84 while too_far(coords[bp[0]], radii[bp[0]],
85 coords[bp[1]], radii[bp[1]])\
86 or intersects(coords[bp[0]], radii[bp[0]],
87 coords[:bp[0]] + coords[bp[0] + 1:],
88 radii[:bp[0]] + radii[bp[0] + 1:]):
89 coords[bp[0]] = random_coordinates()
94 b = bn.add_child(
"bond", RMF.BOND)
96 bd.set_static_bonded_0(bd0.get_id().get_index())
97 bd.set_static_bonded_1(bd1.get_id().get_index())
100 coords[bonds[-1][0]] = coords[bonds[-1][1]]
102 print(
"created", len(bonds),
"bonds")
103 fn = fh.get_root_node().add_child(
"restraints", RMF.ORGANIZATIONAL)
106 print(
"creating features")
108 f0 = random.choice(particles)
109 f1 = random.choice(particles)
112 f = fn.add_child(
"r", RMF.FEATURE)
114 rd.set_representation([f0, f1])
115 features.append((particles.index(f0), particles.index(f1)))
116 feature_nodes.append(f)
119 def get_particle_distance(p0, p1):
122 v0 = pd0.get_coordinates()
123 v1 = pd1.get_coordinates()
124 r0 = pd0.get_radius()
125 r1 = pd1.get_radius()
127 ((v0[0] - v1[0]) ** 2 + (v0[1] - v1[1])
128 ** 2 + (v0[1] - v1[1]) ** 2) ** .5 - r0 - r1
131 bn = fh.get_root_node().add_child(
"box", RMF.ORGANIZATIONAL)
147 en = bn.add_child(
"edge", RMF.GEOMETRY)
149 sd.set_static_coordinates_list(e)
152 def write_frame(name):
153 fh.add_frame(name, RMF.FRAME)
154 for c, p
in zip(coords, particles):
156 pd.set_frame_coordinates(c)
157 for f, p
in zip(feature_nodes, features):
158 s = get_particle_distance(particles[p[0]], particles[p[1]])
160 sd.set_frame_score(s)
162 write_frame(
"initial")
164 for i
in range(0, number_of_frames):
165 for i
in range(len(coords)):
168 c[1] + random.uniform(-.2, .2),
169 c[2] + random.uniform(-.2, .2))
170 if intersects(cn, radii[i], coords[:i] + coords[i + 1:], radii[:i] + radii[i + 1:]):
173 for b
in [b
for b
in bonds
if b[0] == i
or b[1] == i]:
176 if too_far(cn, radii[i], coords[b[1]], radii[b[1]]):