Ejemplo n.º 1
0
 def test_default(self):
     with TemporaryDirectory() as tmp_dir:
         gsdfile = os.path.join(tmp_dir, 'testfile.gsd')
         posfile = os.path.join(tmp_dir, 'testfile.pos')
         with open(gsdfile, "wb") as f:
             f.write(base64.b64decode(garnett.samples.GSD_BASE64))
         with garnett.read(gsdfile) as traj:
             with self.assertRaises(AttributeError):
                 traj[-1].shapedef
             garnett.write(traj, posfile)
         with garnett.read(posfile) as traj:
             for frame in traj:
                 for name in frame.shapedef.keys():
                     self.assertEqual(frame.shapedef[name],
                                      DEFAULT_SHAPE_DEFINITION)
Ejemplo n.º 2
0
def make_gsd(job):
    try:
        with garnett.read(job.fn('dump.pos')) as traj:
            garnett.write(traj, job.fn('dump.gsd'))
    except garnett.errors.ParserError as e:
        print(e)
        job.doc.failed = True
Ejemplo n.º 3
0
    def test_write_pos(self):
        with self.create_tmp_and_traj() as (tmp_dir, traj):
            tmp_name = os.path.join(tmp_dir, 'test.pos')
            garnett.write(traj, tmp_name)

            # Read back the file and check if it is the same as the original
            with garnett.read(tmp_name) as traj2:
                self.assertEqual(len(traj), len(traj2))
Ejemplo n.º 4
0
    def test_read_gsd_template(self):
        with garnett.read(
                get_filename('template-missing-shape.gsd'),
                template=get_filename('template-missing-shape.pos')) as traj:
            self.assertGreater(len(traj), 0)

            # Make sure a shape definition was parsed from the POS file
            self.assertGreater(len(traj[0].shapedef), 0)
Ejemplo n.º 5
0
    def test_write_format(self):
        with self.create_tmp_and_traj() as (tmp_dir, traj):
            # No suffix is given to the temp file, so no format is detected
            tmp_name = os.path.join(tmp_dir, 'test')
            garnett.write(traj, tmp_name, fmt='pos')

            # Read back the file and check if it is the same as the original
            with garnett.read(tmp_name, fmt='pos') as traj2:
                self.assertEqual(len(traj), len(traj2))
Ejemplo n.º 6
0
    def test_write_io(self):
        with self.create_tmp_and_traj() as (tmp_dir, traj):
            tmp_name = os.path.join(tmp_dir, 'test_io.gsd')
            with open(tmp_name, 'wb') as gsdfile:
                garnett.write(traj, gsdfile)

            # Read back the file and check if it is the same as the original
            with garnett.read(tmp_name) as traj2:
                self.assertEqual(len(traj), len(traj2))
Ejemplo n.º 7
0
    def test_write_sphere_shapedef(self):
        # Write to / read from a temp file
        tmpfile = tempfile.NamedTemporaryFile(mode='wb')

        with tmpfile:
            with garnett.read(get_filename('FeSiUC.pos')) as traj:
                self.writer.write(traj, tmpfile)
                written_traj = self.reader.read(tmpfile)
                assertEqualShapedefs(written_traj[0].shapedef,
                                     traj[0].shapedef)
Ejemplo n.º 8
0
def get_structure(description):
    filename = description['href'].replace('./', '').replace('.html', '.cif')
    rcif = requests.get(
        'http://aflowlib.org/CrystalDatabase/CIF/{}'.format(filename))
    handle = io.StringIO(rcif.content.decode())
    handle.name = 'test.cif'

    short_name = get_short_name(description)
    type_map = collections.defaultdict(lambda: len(type_map))

    with garnett.read(handle, fmt='cif') as traj:
        frame = traj[-1]
        types = [type_map[t] for t in frame.types]

        return dict(name=short_name,
                    positions=frame.positions.tolist(),
                    types=types,
                    space_group=description['Space Group Number'],
                    box=frame.box.get_box_array())
Ejemplo n.º 9
0
 def test_write_defaults(self):
     tmpfile = tempfile.NamedTemporaryFile(mode='wb')
     with tmpfile:
         with garnett.read(get_filename('shapes/ellipsoid_3d.pos')) as traj:
             self.writer.write(traj, tmpfile)
             written_traj = self.reader.read(tmpfile)
             assert np.array_equal(written_traj[0].mass,
                                   np.ones(27).astype(float))
             assert np.array_equal(written_traj[0].velocity,
                                   np.zeros([27, 3]).astype(float))
             assert np.array_equal(written_traj[0].diameter,
                                   np.ones(27).astype(float))
             assert np.array_equal(written_traj[0].moment_inertia,
                                   np.zeros([27, 3]).astype(float))
             assert np.array_equal(written_traj[0].angmom,
                                   np.zeros([27, 4]).astype(float))
             assert np.array_equal(written_traj[0].charge,
                                   np.zeros([27]).astype(float))
             assert np.array_equal(written_traj[0].image,
                                   np.zeros([27, 3]).astype(np.int32))
Ejemplo n.º 10
0
def test_read(file, template=None):
    with garnett.read(file, template=template) as traj:
        for frame in traj:
            frame.load()
            print(frame)
Ejemplo n.º 11
0
def read(*args, **kwargs):
    with garnett.read(*args, **kwargs) as traj:
        yield Trajectory(traj)
Ejemplo n.º 12
0
    snapshot.particles.position[:] = [
        [2, 0, 0],
        [4, 0, 0],
        [0, 4, 0],
        [0, 0, 4],
    ]
    system = hoomd.init.read_snapshot(snapshot)
    mc = hoomd.hpmc.integrate.convex_polyhedron(seed=452784, d=0.2, a=0.4)
    mc.shape_param.set('A', vertices=cube_verts)
    gsd_dump = hoomd.dump.gsd('cube.gsd', period=10, group=hoomd.group.all())
    gsd_dump.dump_state(mc)
    gsd_dump.dump_shape(mc)
    hoomd.run(1000)

    # Restore a snapshot from saved data
    with garnett.read('cube.gsd') as traj:
        snapshot2 = system.take_snapshot()
        traj[-1].to_hoomd_snapshot(snapshot2)

with hoomd.context.SimulationContext():
    # Create a HOOMD snapshot from a garnett Trajectory frame
    with garnett.read('cube.gsd') as traj:
        snapshot = traj[-1].to_hoomd_snapshot()
        system = hoomd.init.read_snapshot(snapshot)

    mc = hoomd.hpmc.integrate.convex_polyhedron(seed=452784, d=0.2, a=0.4)
    mc.shape_param.set('A', vertices=cube_verts)
    gsd_dump = hoomd.dump.gsd('cube.gsd', period=10, group=hoomd.group.all())
    gsd_dump.dump_state(mc)
    gsd_dump.dump_shape(mc)
    hoomd.run(1000)
Ejemplo n.º 13
0
def main(args):
    with garnett.read(args.infile) as traj:
        traj_centered = garnett.trajectory.Trajectory(
            (center(frame) for frame in traj))
        garnett.write(traj_centered, args.outfile)
    return 0
Ejemplo n.º 14
0
# Copyright (c) 2020 The Regents of the University of Michigan
# All rights reserved.
# This software is licensed under the BSD 3-Clause License.

import garnett
import signac

project = signac.get_project()

for job in project:
    print(job)
    with job:
        with garnett.read('dump.gsd') as traj:
            print(traj)

        with garnett.read('dump.dcd') as traj:
            print(traj)

        with garnett.read("dump.gsd", template='dump.pos') as traj:
            print(traj)

        with garnett.read("dump.gsd", template='init.xml') as traj:
            print(traj)
Ejemplo n.º 15
0
 def test_read_xml(self):
     with garnett.read(get_filename('hoomd.xml')) as traj:
         self.assertGreater(len(traj), 0)
Ejemplo n.º 16
0
def main(args):
    with garnett.read(args.infile) as traj:
        garnett.write(traj, args.outfile)
    return 0
Ejemplo n.º 17
0
 def test_read_gsd(self):
     with garnett.read(get_filename('dump.gsd')) as traj:
         self.assertGreater(len(traj), 0)
Ejemplo n.º 18
0
 def test_read_gtar(self):
     with garnett.read(get_filename('libgetar_sample.tar')) as traj:
         self.assertGreater(len(traj), 0)
Ejemplo n.º 19
0
 def test_garnett_dcd(self):
     import garnett
     with garnett.read(LJ_DCD) as traj:
         self.run_analyses(traj)
Ejemplo n.º 20
0
 def test_read_nonexistent(self):
     with self.assertRaises(FileNotFoundError):
         with garnett.read(get_filename('does_not_exist.pos')):
             pass
Ejemplo n.º 21
0
 def test_read_unsupported_template(self):
     with self.assertRaises(ValueError):
         with garnett.read(
                 get_filename('FeSiUC.pos'),
                 template=get_filename('template-missing-shape.pos')):
             pass
Ejemplo n.º 22
0
 def test_read_cif(self):
     with garnett.read(get_filename('cI16.cif')) as traj:
         self.assertGreater(len(traj), 0)
Ejemplo n.º 23
0
 def test_read_io(self):
     with open(get_filename('dump.gsd'), 'rb') as gsdfile:
         with garnett.read(gsdfile) as traj:
             self.assertGreater(len(traj), 0)
Ejemplo n.º 24
0
 def test_read_unsupported(self):
     with self.assertRaises(NotImplementedError):
         with garnett.read(get_filename('unsupported.ext')):
             pass
Ejemplo n.º 25
0
 def test_read_pos(self):
     with garnett.read(get_filename('FeSiUC.pos')) as traj:
         self.assertGreater(len(traj), 0)
Ejemplo n.º 26
0
    def test_minkowski_structure_metrics(self):
        for structure in ["fcc", "bcc", "hcp", "sc"]:
            expected_ql = _get_structure_data(structure, "q")
            expected_avql = _get_structure_data(structure, "avq")
            expected_wl = _get_structure_data(structure, "w")
            expected_avwl = _get_structure_data(structure, "avw")

            with garnett.read(
                    os.path.join(
                        os.path.dirname(__file__),
                        "files",
                        "minkowski_structure_metrics",
                        f"{structure}.gsd",
                    )) as traj:
                frame = traj[0]
                box = frame.box
                positions = frame.position.copy()

            voro = freud.locality.Voronoi()
            voro.compute((box, positions))
            for sph_l in range(expected_ql.shape[1]):

                # These tests fail for unknown (probably numerical) reasons.
                if structure == "hcp" and sph_l in [3, 5]:
                    continue

                # Test ql
                comp = freud.order.Steinhardt(sph_l, weighted=True)
                comp.compute((box, positions), neighbors=voro.nlist)
                npt.assert_allclose(comp.order,
                                    expected_ql[:, sph_l],
                                    rtol=5e-5,
                                    atol=1e-3)

                # Test Average ql
                comp = freud.order.Steinhardt(sph_l,
                                              average=True,
                                              weighted=True)
                comp.compute((box, positions), neighbors=voro.nlist)
                npt.assert_allclose(comp.order,
                                    expected_avql[:, sph_l],
                                    rtol=5e-5,
                                    atol=1e-3)

                # These tests fail for unknown (probably numerical) reasons.
                if sph_l != 2:
                    # Test wl
                    comp = freud.order.Steinhardt(sph_l,
                                                  wl=True,
                                                  weighted=True,
                                                  wl_normalize=True)
                    comp.compute((box, positions), neighbors=voro.nlist)
                    npt.assert_allclose(comp.order,
                                        expected_wl[:, sph_l],
                                        rtol=5e-5,
                                        atol=1e-3)

                    # Test Average wl
                    comp = freud.order.Steinhardt(sph_l,
                                                  wl=True,
                                                  average=True,
                                                  weighted=True,
                                                  wl_normalize=True)
                    comp.compute((box, positions), neighbors=voro.nlist)
                    npt.assert_allclose(comp.order,
                                        expected_avwl[:, sph_l],
                                        rtol=5e-5,
                                        atol=1e-3)
Ejemplo n.º 27
0
 def create_tmp_and_traj(self):
     with TemporaryDirectory() as tmp_dir:
         with garnett.read(get_filename('dump.gsd')) as traj:
             yield (tmp_dir, traj)