1 # -*- coding: iso-8859-1 -*-
4 from hoomd_script
import *
8 # tests for data access
9 class particle_data_access_tests (unittest
.TestCase
):
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
18 self
.s
.box
= data
.boxdim(Lx
=15, Ly
=20, Lz
=30, xy
=1.0, xz
=0.5, yz
=2.0);
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])
39 self
.assertAlmostEqual(v
[0],1)
40 self
.assertAlmostEqual(v
[1],2)
41 self
.assertAlmostEqual(v
[2],3)
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
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)
102 # tests for bond, angle, dihedral, and improper data access
103 class bond_data_access_tests (unittest
.TestCase
):
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)
116 def test_bonds(self
):
117 self
.assertEqual(0, len(self
.s
.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
:
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
:
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);
152 def test_angles(self
):
153 self
.assertEqual(0, len(self
.s
.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
:
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];
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);
189 def test_dihedrals(self
):
190 self
.assertEqual(0, len(self
.s
.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
:
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];
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);
228 def test_impropers(self
):
229 self
.assertEqual(0, len(self
.s
.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
:
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];
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);
271 class pair_access_tests (unittest
.TestCase
):
274 init
.create_random(N
=100, phi_p
=0.05);
276 sorter
.set_params(grid
=8)
278 # basic test of creation
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);
293 # tests for body data access
294 class body_data_access_tests (unittest
.TestCase
):
296 # only for single rank execution
297 if comm
.get_num_ranks() == 1:
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
:
306 self
.s
.sysdef
.getRigidData().initializeData()
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
319 t
= b
.angular_momentum
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)
364 if comm
.get_num_ranks() == 1:
368 if __name__
== '__main__':
369 unittest
.main(argv
= ['test.py', '-v'])