Fix potential problem in Messenger related to MPI window
[hoomd-blue.git] / test / hoomd_script / test_data_access.py
blob50bab613811be37bb450b84bbcebc3d4e52330f4
1 # -*- coding: iso-8859-1 -*-
2 # Maintainer: joaander
4 from hoomd_script import *
5 import unittest
6 import os
8 # tests for data access
9 class particle_data_access_tests (unittest.TestCase):
10 def setUp(self):
11 print
12 self.s = init.create_random(N=100, phi_p=0.05);
14 sorter.set_params(grid=8)
16 # tests reading/setting of the box
17 def test_box(self):
18 self.s.box = data.boxdim(Lx=15, Ly=20, Lz=30, xy=1.0, xz=0.5, yz=2.0);
19 b = self.s.box;
20 self.assertAlmostEqual(15, b.Lx, 5)
21 self.assertAlmostEqual(20, b.Ly, 5)
22 self.assertAlmostEqual(30, b.Lz, 5)
23 self.assertAlmostEqual(1.0, b.xy, 5)
24 self.assertAlmostEqual(0.5, b.xz, 5)
25 self.assertAlmostEqual(2.0, b.yz, 5)
26 l = [b.get_lattice_vector(0), b.get_lattice_vector(1), b.get_lattice_vector(2)]
27 self.assertAlmostEqual(l[0][0], 15, 5)
28 self.assertAlmostEqual(l[0][1], 0, 5)
29 self.assertAlmostEqual(l[0][2], 0, 5)
30 self.assertAlmostEqual(l[1][0], 20*1.0, 5)
31 self.assertAlmostEqual(l[1][1], 20, 5)
32 self.assertAlmostEqual(l[1][2], 0)
33 self.assertAlmostEqual(l[1][2], 0)
34 self.assertAlmostEqual(l[2][0], 30*0.5, 5)
35 self.assertAlmostEqual(l[2][1], 30*2.0)
36 self.assertAlmostEqual(l[2][2], 30)
37 v = (1+l[0][0],2+l[0][1],3+l[0][2])
38 v = b.wrap(v)
39 self.assertAlmostEqual(v[0],1)
40 self.assertAlmostEqual(v[1],2)
41 self.assertAlmostEqual(v[2],3)
43 # test particles
44 def test_particles(self):
45 self.assertEqual(100, len(self.s.particles));
46 for p in self.s.particles:
47 # just access values to check that they can be read
48 t = p.tag;
49 t = p.acceleration;
50 t = p.typeid;
51 t = p.position;
52 t = p.image;
53 t = p.velocity;
54 t = p.charge;
55 t = p.mass;
56 t = p.diameter;
57 t = p.type;
58 t = p.net_force;
59 t = p.net_energy;
60 t = p.orientation;
61 t = p.net_torque;
63 # test setting properties for just one particle
64 self.s.particles[0].position = (1,2,3);
65 t = self.s.particles[0].position;
66 self.assertAlmostEqual(1, t[0], 5)
67 self.assertAlmostEqual(2, t[1], 5)
68 self.assertAlmostEqual(3, t[2], 5)
70 self.s.particles[0].velocity = (4,5,6);
71 t = self.s.particles[0].velocity;
72 self.assertAlmostEqual(4, t[0], 5)
73 self.assertAlmostEqual(5, t[1], 5)
74 self.assertAlmostEqual(6, t[2], 5)
76 self.s.particles[0].image = (7,8,9)
77 t = self.s.particles[0].image;
78 self.assertAlmostEqual(7, t[0], 5)
79 self.assertAlmostEqual(8, t[1], 5)
80 self.assertAlmostEqual(9, t[2], 5)
82 self.s.particles[0].charge = 5.6;
83 self.assertAlmostEqual(5.6, self.s.particles[0].charge, 5)
85 self.s.particles[0].mass = 7.9;
86 self.assertAlmostEqual(7.9, self.s.particles[0].mass, 5)
88 self.s.particles[0].diameter= 8.7;
89 self.assertAlmostEqual(8.7, self.s.particles[0].diameter, 5)
91 self.s.particles[0].orientation = (1,2,3,5);
92 t = self.s.particles[0].orientation;
93 self.assertAlmostEqual(1, t[0], 5)
94 self.assertAlmostEqual(2, t[1], 5)
95 self.assertAlmostEqual(3, t[2], 5)
96 self.assertAlmostEqual(5, t[3], 5)
98 def tearDown(self):
99 del self.s
100 init.reset();
102 # tests for bond, angle, dihedral, and improper data access
103 class bond_data_access_tests (unittest.TestCase):
104 def setUp(self):
105 print
106 self.s = init.create_empty(N=100, box=data.boxdim(L=10),
107 particle_types=['A'],
108 bond_types=['bondA', 'bondB'],
109 angle_types=['angleA', 'angleB'],
110 dihedral_types=['dihedralA', 'dihedralB'],
111 improper_types=['improperA', 'improperB']);
113 sorter.set_params(grid=8)
115 # tests bonds
116 def test_bonds(self):
117 self.assertEqual(0, len(self.s.bonds));
119 # add some bonds
120 b0 = self.s.bonds.add('bondA', 0, 1);
121 self.assertEqual(1, len(self.s.bonds));
122 b1 = self.s.bonds.add('bondA', 10, 11);
123 self.assertEqual(2, len(self.s.bonds));
124 b2 = self.s.bonds.add('bondB', 50, 20);
125 self.assertEqual(3, len(self.s.bonds));
127 # check that we can get all bond parameters
128 for b in self.s.bonds:
129 b.typeid
130 b.type
134 ####################################
135 ## The bond deletion feature is currently disabled
136 # test deletion by tag
137 self.s.bonds.remove(b1);
138 self.assertEqual(2, len(self.s.bonds));
140 # test deletion by index (check bond a value to delete the bond with a=0)
141 if self.s.bonds[0].tag == b0:
142 del self.s.bonds[0];
143 else:
144 del self.s.bonds[1];
146 self.assertEqual(b2, self.s.bonds[0].tag);
147 self.assertEqual(50, self.s.bonds[0].a);
148 self.assertEqual(20, self.s.bonds[0].b);
149 self.assertEqual('bondB', self.s.bonds[0].type);
151 # tests angles
152 def test_angles(self):
153 self.assertEqual(0, len(self.s.angles));
155 # add some angles
156 b0 = self.s.angles.add('angleA', 0, 1, 2);
157 self.assertEqual(1, len(self.s.angles));
158 b1 = self.s.angles.add('angleA', 10, 11, 12);
159 self.assertEqual(2, len(self.s.angles));
160 b2 = self.s.angles.add('angleB', 50, 20, 10);
161 self.assertEqual(3, len(self.s.angles));
163 # check that we can get all bond parameters
164 for b in self.s.angles:
165 b.tag
166 b.typeid
167 b.type
172 # test deletion by tag
173 self.s.angles.remove(b1);
174 self.assertEqual(2, len(self.s.angles));
176 # test deletion by index (check bond a value to delete the bond with a=0)
177 if self.s.angles[0].tag == b0:
178 del self.s.angles[0];
179 else:
180 del self.s.angles[1];
182 self.assertEqual(b2, self.s.angles[0].tag);
183 self.assertEqual(50, self.s.angles[0].a);
184 self.assertEqual(20, self.s.angles[0].b);
185 self.assertEqual(10, self.s.angles[0].c);
186 self.assertEqual('angleB', self.s.angles[0].type);
188 # tests dihedrals
189 def test_dihedrals(self):
190 self.assertEqual(0, len(self.s.dihedrals));
192 # add some dihedrals
193 b0 = self.s.dihedrals.add('dihedralA', 0, 1, 2, 3);
194 self.assertEqual(1, len(self.s.dihedrals));
195 b1 = self.s.dihedrals.add('dihedralA', 10, 11, 12, 13);
196 self.assertEqual(2, len(self.s.dihedrals));
197 b2 = self.s.dihedrals.add('dihedralB', 50, 20, 10, 1);
198 self.assertEqual(3, len(self.s.dihedrals));
200 # check that we can get all bond parameters
201 for b in self.s.dihedrals:
202 b.tag
203 b.typeid
204 b.type
210 # test deletion by tag
211 self.s.dihedrals.remove(b1);
212 self.assertEqual(2, len(self.s.dihedrals));
214 # test deletion by index (check bond a value to delete the bond with a=0)
215 if self.s.dihedrals[0].tag == b0:
216 del self.s.dihedrals[0];
217 else:
218 del self.s.dihedrals[1];
220 self.assertEqual(b2, self.s.dihedrals[0].tag);
221 self.assertEqual(50, self.s.dihedrals[0].a);
222 self.assertEqual(20, self.s.dihedrals[0].b);
223 self.assertEqual(10, self.s.dihedrals[0].c);
224 self.assertEqual(1, self.s.dihedrals[0].d);
225 self.assertEqual('dihedralB', self.s.dihedrals[0].type);
227 # tests impropers
228 def test_impropers(self):
229 self.assertEqual(0, len(self.s.impropers));
231 # add some impropers
232 b0 = self.s.impropers.add('improperA', 0, 1, 2, 3);
233 self.assertEqual(1, len(self.s.impropers));
234 b1 = self.s.impropers.add('improperA', 10, 11, 12, 13);
235 self.assertEqual(2, len(self.s.impropers));
236 b2 = self.s.impropers.add('improperB', 50, 20, 10, 1);
237 self.assertEqual(3, len(self.s.impropers));
239 # check that we can get all bond parameters
240 for b in self.s.impropers:
241 b.tag
242 b.typeid
243 b.type
249 # test deletion by tag
250 self.s.impropers.remove(b1);
251 self.assertEqual(2, len(self.s.impropers));
253 # test deletion by index (check bond a value to delete the bond with a=0)
254 if self.s.impropers[0].tag == b0:
255 del self.s.impropers[0];
256 else:
257 del self.s.impropers[1];
259 self.assertEqual(b2, self.s.impropers[0].tag);
260 self.assertEqual(50, self.s.impropers[0].a);
261 self.assertEqual(20, self.s.impropers[0].b);
262 self.assertEqual(10, self.s.impropers[0].c);
263 self.assertEqual(1, self.s.impropers[0].d);
264 self.assertEqual('improperB', self.s.impropers[0].type);
266 def tearDown(self):
267 del self.s
268 init.reset();
270 # pair.lj
271 class pair_access_tests (unittest.TestCase):
272 def setUp(self):
273 print
274 init.create_random(N=100, phi_p=0.05);
276 sorter.set_params(grid=8)
278 # basic test of creation
279 def test(self):
280 lj = pair.lj(r_cut=3.0);
281 lj.pair_coeff.set('A', 'A', epsilon=1.0, sigma=1.0, alpha=1.0, r_cut=2.5, r_on=2.0);
282 lj.update_coeffs();
283 for p in lj.forces:
284 f = p.force;
285 f = p.energy;
286 f = p.virial;
287 f = p.torque;
289 def tearDown(self):
290 init.reset();
293 # tests for body data access
294 class body_data_access_tests (unittest.TestCase):
295 def setUp(self):
296 # only for single rank execution
297 if comm.get_num_ranks() == 1:
298 print
299 self.s = init.create_random(N=100, phi_p=0.05);
301 sorter.set_params(grid=8)
303 for p in self.s.particles:
304 p.body = p.tag % 50
306 self.s.sysdef.getRigidData().initializeData()
308 # test bodies
309 def test_bodies(self):
310 if comm.get_num_ranks() == 1:
311 self.assertEqual(50, len(self.s.bodies));
313 for b in self.s.bodies:
314 # just access values to check that they can be read
315 t = b.COM
316 t = b.orientation
317 t = b.velocity
318 t = b.mass
319 t = b.angular_momentum
320 t = b.moment_inertia
321 t = b.particle_tags
322 t = b.particle_disp
323 t = b.net_force
324 t = b.net_torque
326 self.s.bodies[0].mass = 7.9;
327 self.assertAlmostEqual(7.9, self.s.bodies[0].mass, 5)
329 self.s.bodies[0].moment_inertia = (4,5,6);
330 t = self.s.bodies[0].moment_inertia;
331 self.assertAlmostEqual(4, t[0], 5)
332 self.assertAlmostEqual(5, t[1], 5)
333 self.assertAlmostEqual(6, t[2], 5)
335 self.s.bodies[0].orientation = (0,0,0,1);
336 t = self.s.bodies[0].orientation;
337 self.assertAlmostEqual(0, t[0], 5)
338 self.assertAlmostEqual(0, t[1], 5)
339 self.assertAlmostEqual(0, t[2], 5)
340 self.assertAlmostEqual(1, t[3], 5)
342 self.s.bodies[0].particle_disp = [(0,0,0),(1,0,0)];
343 t = self.s.bodies[0].particle_disp;
344 self.assertAlmostEqual(0, t[0][0], 5)
345 self.assertAlmostEqual(0, t[0][1], 5)
346 self.assertAlmostEqual(0, t[0][2], 5)
347 self.assertAlmostEqual(1, t[1][0], 5)
348 self.assertAlmostEqual(0, t[1][1], 5)
349 self.assertAlmostEqual(0, t[1][2], 5)
351 self.s.bodies[0].velocity = (1,2,3);
352 t = self.s.bodies[0].velocity;
353 self.assertAlmostEqual(1, t[0], 5)
354 self.assertAlmostEqual(2, t[1], 5)
355 self.assertAlmostEqual(3, t[2], 5)
357 self.s.bodies[0].angular_momentum = (3,2,1);
358 t = self.s.bodies[0].angular_momentum;
359 self.assertAlmostEqual(3, t[0], 5)
360 self.assertAlmostEqual(2, t[1], 5)
361 self.assertAlmostEqual(1, t[2], 5)
363 def tearDown(self):
364 if comm.get_num_ranks() == 1:
365 del self.s
366 init.reset();
368 if __name__ == '__main__':
369 unittest.main(argv = ['test.py', '-v'])