def test_configuration(self): M = self.M conf = M.Configuration(self.cube_universe, IN.array(NR.normal(size=(40,3))), IN.array(10., N.float64)) self.assertTrue(is_valid(conf)) with TestHDF5Store('w') as store: self.assertRaises(IOError, lambda : store.store("configuration", conf)) store.store("universe", self.cube_universe) store.store("configuration", conf) self.assertTrue(store.retrieve("configuration") is conf) with TestHDF5Store('r') as store: conf_r = store.retrieve("configuration") self.assertFalse(conf_r is conf) self.assertTrue(conf_r.is_equivalent(conf)) self.assertTrue(is_valid(conf_r)) conf = M.Configuration(self.infinite_universe, IN.array(NR.normal(size=(40,3))), None) self.assertTrue(is_valid(conf)) with TestHDF5Store('w') as store: self.assertRaises(IOError, lambda : store.store("configuration", conf)) store.store("universe", self.infinite_universe) store.store("configuration", conf) self.assertTrue(store.retrieve("configuration") is conf) with TestHDF5Store('r') as store: conf_r = store.retrieve("configuration") self.assertFalse(conf_r is conf) self.assertTrue(conf_r.is_equivalent(conf)) self.assertTrue(is_valid(conf_r))
def test_property(self): M = self.M universe = self.universe for dtype in (N.float32, N.float64): for p in [M.AtomProperty(universe, "mass", "amu", IN.array(NR.normal(size=(universe.number_of_atoms,)))), M.SiteProperty(universe, "foo", "", IN.array(NR.normal(size=(universe.number_of_sites, 3)))), M.TemplateAtomProperty(universe, "mass", "amu", IN.array(NR.normal(size=(universe.number_of_template_atoms, 1)))), M.TemplateSiteProperty(universe, "foo", "bar", IN.array(NR.normal(size=(universe.number_of_template_sites, 2, 2)))), ]: self.assertTrue(is_valid(p)) file = StringIO() with XMLWriter(file) as writer: writer.store("universe", self.universe) writer.store("property", p) xml_string = file.getvalue() file = StringIO(xml_string) with XMLReader(file) as reader: data = {xml_id: obj for xml_id, obj in reader} p_r = data['property'] self.assertTrue(p_r.is_equivalent(p)) self.assertTrue(is_valid(p_r))
def test_property(self): M = self.M for universe in[self.cube_universe, self.infinite_universe]: for p in [M.AtomProperty(universe, "mass", "amu", IN.array(NR.normal(size=(universe.number_of_atoms,)))), M.SiteProperty(universe, "foo", "", IN.array(NR.normal(size=(universe.number_of_sites, 3)))), M.TemplateAtomProperty(universe, "mass", "amu", IN.array(NR.normal(size=(universe.number_of_template_atoms, 1)))), M.TemplateSiteProperty(universe, "foo", "bar", IN.array(NR.normal(size=(universe.number_of_template_sites, 2, 2)))), ]: self.assertTrue(is_valid(p)) with TestHDF5Store('w') as store: self.assertRaises(IOError, lambda: store.store("property", p)) store.store("universe", universe) store.store("property", p) self.assertTrue(store.retrieve("property") is p) with TestHDF5Store('r') as store: p_r = store.retrieve("property") self.assertFalse(p_r is p) self.assertTrue(p_r.is_equivalent(p)) self.assertTrue(is_valid(p_r))
def test_lattice_vectors(self): conf = M.Configuration(self.infinite, IN.zeros((0, 3), N.float32), None) self.assertEqual(conf.lattice_vectors(), ()) self.assertEqual(conf.cell_volume(), None) conf = M.Configuration(self.cube, IN.zeros((0, 3), N.float32), IN.array(1., N.float32)) lv = conf.lattice_vectors() self.assertTrue((lv[0] == N.array([1., 0., 0.], N.float32)).all()) self.assertTrue((lv[1] == N.array([0., 1., 0.], N.float32)).all()) self.assertTrue((lv[2] == N.array([0., 0., 1.], N.float32)).all()) self.assertEqual(conf.cell_volume(), 1.) conf = M.Configuration(self.cuboid, IN.zeros((0, 3), N.float32), IN.array([1., 2., 4.], N.float32)) lv = conf.lattice_vectors() self.assertTrue((lv[0] == N.array([1., 0., 0.], N.float32)).all()) self.assertTrue((lv[1] == N.array([0., 2., 0.], N.float32)).all()) self.assertTrue((lv[2] == N.array([0., 0., 4.], N.float32)).all()) self.assertEqual(conf.cell_volume(), 8.) conf = M.Configuration(self.parallelepiped, IN.zeros((0, 3), N.float32), IN.array([[1., 2., 4.], [8., 4., 2.], [16., 4., 8.]], N.float32)) lv = conf.lattice_vectors() self.assertTrue((lv[0] == N.array([1., 2., 4.], N.float32)).all()) self.assertTrue((lv[1] == N.array([8., 4., 2.], N.float32)).all()) self.assertTrue((lv[2] == N.array([16., 4., 8.], N.float32)).all()) self.assertAlmostEqual(conf.cell_volume(), 168.)
def test_properties(self): masses = M.TemplateAtomProperty(self.universe, "masses", "amu", IN.array([1., 1., 16.], N.float32)) self.assertTrue(is_valid(masses)) self.assertEqual(masses.type, 'template_atom') self.assertTrue(masses.universe == self.universe) self.assertEqual(masses.element_shape, ()) self.assertEqual(masses.data.shape, (3,)) bead_masses = M.TemplateSiteProperty(self.universe, "mass", "amu", IN.array([1., 1., 1., 1., 8., 8.], N.float32)) self.assertTrue(is_valid(bead_masses)) self.assertEqual(bead_masses.type, 'template_site') self.assertTrue(bead_masses.universe is self.universe) self.assertEqual(bead_masses.element_shape, ()) self.assertEqual(bead_masses.data.shape, (6,)) velocities = M.SiteProperty(self.universe, "velocity", "nm ps-1", IN.zeros((60, 3), dtype=N.float64)) self.assertTrue(is_valid(velocities)) self.assertEqual(velocities.type, 'site') self.assertTrue(velocities.universe is self.universe) self.assertEqual(velocities.data.shape, (60, 3)) self.assertEqual(velocities.element_shape, (3,)) foo = M.AtomProperty(self.universe, "foo", "", IN.zeros((30, 2, 2), dtype=N.int16)) self.assertTrue(is_valid(foo)) self.assertEqual(foo.type, 'atom') self.assertTrue(foo.universe is self.universe) self.assertEqual(foo.data.shape, (30, 2, 2)) self.assertEqual(foo.element_shape, (2, 2))
def test_parallelepiped(self): universe = M.universe('parallelepiped', [(water, 'water', 100)]) nsites = universe.number_of_sites conf1 = M.Configuration(universe, IN.array(NR.normal(size=(nsites, 3))), IN.array(NR.normal(size=(3, 3)))) conf2 = M.Configuration(universe, IN.array(NR.normal(size=(nsites, 3))), IN.array(NR.normal(size=(3, 3)))) self._write_and_read_back(universe, conf1, conf2)
def test_cubic(self): universe = M.universe('cube', [(water, 'water', 10)]) nsites = universe.number_of_sites conf1 = M.Configuration(universe, IN.array(NR.normal(size=(nsites, 3))), IN.array(10., N.float64)) conf2 = M.Configuration(universe, IN.array(NR.normal(size=(nsites, 3))), IN.array(11., N.float64)) self._write_and_read_back(universe, conf1, conf2)
def test_infinite(self): universe = M.universe('infinite', [(water, 'water', 5)]) nsites = universe.number_of_sites conf1 = M.Configuration(universe, IN.array(NR.normal(size=(nsites, 3))), None) conf2 = M.Configuration(universe, IN.array(NR.normal(size=(nsites, 3))), None) self._write_and_read_back(universe, conf1, conf2)
def test_selections(self): s = M.AtomSelection(self.universe, IN.array([0, 2], N.uint8)) self.assertEqual(s.number_of_atoms, 2) self.assertEqual(s.number_of_sites, 4) s = M.TemplateAtomSelection(self.universe, IN.array([1], N.uint8)) self.assertEqual(s.number_of_atoms, 10) self.assertEqual(s.number_of_sites, 20) s = M.SiteSelection(self.universe, [2]) self.assertEqual(s.number_of_sites, 1) s = M.TemplateSiteSelection(self.universe, [0]) self.assertEqual(s.number_of_sites, 10)
def _property(self, el_shape, dtype, text): data = IN.array([dtype(s) for s in text.split()]) if isstring(el_shape): el_shape = tuple(int(s) for s in el_shape.split()) n_els = N.product(el_shape) assert len(data) % n_els == 0 n_entries = len(data) // n_els return data.reshape((n_entries,)+el_shape)
def test_configuration(self): M = self.M for dtype in (N.float32, N.float64): conf = M.Configuration(self.universe, IN.array(NR.normal(size=(40,3)), dtype=dtype), IN.array(10., dtype)) self.assertTrue(is_valid(conf)) file = StringIO() with XMLWriter(file) as writer: writer.store("universe", self.universe) writer.store("configuration", conf) xml_string = file.getvalue() file = StringIO(xml_string) with XMLReader(file) as reader: data = {xml_id: obj for xml_id, obj in reader} conf_r = data['configuration'] self.assertTrue(conf_r.is_equivalent(conf)) self.assertTrue(is_valid(conf_r))
def test_selection(self): M = self.M universe = self.universe for s in [M.AtomSelection(universe, IN.array([0], N.uint8)), M.SiteSelection(universe, IN.array([1, 2], N.uint8)), M.TemplateAtomSelection(universe, IN.array([0, 2], N.uint8)), M.TemplateSiteSelection(universe, IN.array([0, 3], N.uint8))]: self.assertTrue(is_valid(s)) file = StringIO() with XMLWriter(file) as writer: writer.store("universe", self.universe) writer.store("selection", s) xml_string = file.getvalue() file = StringIO(xml_string) with XMLReader(file) as reader: data = {xml_id: obj for xml_id, obj in reader} s_r = data['selection'] self.assertTrue(s_r.is_equivalent(s)) self.assertTrue(is_valid(s_r))
def _read_selection(self, el): universe = self._read_universe(el.find('universe')) ptype = el.tag[:-10] # strip off '_selection' klass = {"atom": im.AtomSelection, "site": im.SiteSelection, "template_atom": im.TemplateAtomSelection, "template_site": im.TemplateSiteSelection}[ptype] el_d = el.find('indices') indices = [int(s) for s in el_d.text.split()] indices = IN.array(indices, uint_for_max_value(max(indices))) return klass(universe, indices)
def test_selection(self): mol = make_water_fragment(nsites=2) universe = M.universe('cube', [(mol, 'water', 5)]) # Index occurs twice self.assertRaises(ValueError, lambda: M.AtomSelection(universe, IN.zeros((2,), N.uint16))) # Atom index too large self.assertRaises(ValueError, lambda: M.AtomSelection(universe, IN.array([20], N.uint16))) # Template atom index too large self.assertRaises(ValueError, lambda: M.TemplateAtomSelection(universe, IN.array([3], N.uint8))) # Site index too large self.assertRaises(ValueError, lambda: M.SiteSelection(universe, IN.array([40], N.uint16))) # Template site index too large self.assertRaises(ValueError, lambda: M.TemplateSiteSelection(universe, IN.array([8], N.uint8)))
def _array(self, shape, dtype, text): if isstring(shape): shape = tuple(int(s) for s in shape.split()) return IN.array([dtype(s) for s in text.split()]).reshape(shape)
def __init__(self, universe, indices): api.validate_type(universe, Universe, "universe") self._universe = universe indices = IN.array(indices, uint_for_max_value(max(indices))) api.validate_indices(indices, self._max_index(), "indices") self._indices = indices