How to use the gsd.hoomd.Snapshot function in gsd

To help you get started, we’ve selected a few gsd examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github glotzerlab / gsd / tests / test_hoomd.py View on Github external
def test_defaults(tmp_path, open_mode):
    """Test that the property defaults are properly set."""
    snap = gsd.hoomd.Snapshot()
    snap.particles.N = 2
    snap.bonds.N = 3
    snap.angles.N = 4
    snap.dihedrals.N = 5
    snap.impropers.N = 6
    snap.constraints.N = 4
    snap.pairs.N = 7

    with gsd.hoomd.open(name=tmp_path / "test_defaults.gsd",
                        mode=open_mode.write) as hf:
        hf.append(snap)

    with gsd.hoomd.open(name=tmp_path / "test_defaults.gsd",
                        mode=open_mode.read) as hf:
        s = hf.read_frame(0)
github glotzerlab / gsd / tests / test_hoomd.py View on Github external
def test_state(tmp_path, open_mode):
    """Test the state chunks."""
    snap0 = gsd.hoomd.Snapshot()

    snap0.state['hpmc/sphere/radius'] = [2.0]
    snap0.state['hpmc/sphere/orientable'] = [1]

    snap1 = gsd.hoomd.Snapshot()

    snap1.state['hpmc/convex_polyhedron/N'] = [3]
    snap1.state['hpmc/convex_polyhedron/vertices'] = [[-1, -1, -1], [0, 1, 1],
                                                      [1, 0, 0]]

    with gsd.hoomd.open(name=tmp_path / "test_state.gsd",
                        mode=open_mode.write) as hf:
        hf.extend([snap0, snap1])

    with gsd.hoomd.open(name=tmp_path / "test_state.gsd",
                        mode=open_mode.read) as hf:
github glotzerlab / gsd / tests / test_hoomd.py View on Github external
def test_log(tmp_path, open_mode):
    """Test the log chunks."""
    snap0 = gsd.hoomd.Snapshot()

    snap0.log['particles/net_force'] = [[1, 2, 3], [4, 5, 6]]
    snap0.log['particles/pair_lj_energy'] = [0, -5, -8, -3]
    snap0.log['value/potential_energy'] = [10]
    snap0.log['value/pressure'] = [-3]

    snap1 = gsd.hoomd.Snapshot()

    snap1.log['particles/pair_lj_energy'] = [1, 2, -4, -10]
    snap1.log['value/pressure'] = [5]

    with gsd.hoomd.open(name=tmp_path / "test_log.gsd",
                        mode=open_mode.write) as hf:
        hf.extend([snap0, snap1])

    with gsd.hoomd.open(name=tmp_path / "test_log.gsd",
                        mode=open_mode.read) as hf:
        assert len(hf) == 2
        s = hf.read_frame(0)

        numpy.testing.assert_array_equal(s.log['particles/net_force'],
                                         snap0.log['particles/net_force'])
        numpy.testing.assert_array_equal(s.log['particles/pair_lj_energy'],
github glotzerlab / gsd / tests / test_hoomd.py View on Github external
def test_fallback2(tmp_path, open_mode):
    """Test additional fallback behaviors."""
    snap0 = gsd.hoomd.Snapshot()
    snap0.configuration.step = 1
    snap0.configuration.dimensions = 3
    snap0.particles.N = 2
    snap0.particles.mass = [2, 3]

    snap1 = gsd.hoomd.Snapshot()
    snap1.configuration.step = 2
    snap1.particles.N = 2
    snap1.particles.position = [[1, 2, 3], [4, 5, 6]]

    with gsd.hoomd.open(name=tmp_path / "test_fallback2.gsd",
                        mode=open_mode.write) as hf:
        hf.extend([snap0, snap1])

    with gsd.hoomd.open(name=tmp_path / "test_fallback2.gsd",
                        mode=open_mode.read) as hf:
        assert len(hf) == 2

        s = hf.read_frame(1)
        numpy.testing.assert_array_equal(s.particles.mass, snap0.particles.mass)
github glotzerlab / gsd / tests / test_hoomd.py View on Github external
snap0.impropers.typeid = [4]
    snap0.impropers.types = ['improperA', 'improperB']
    snap0.impropers.group = [[1, 0, 0, 1]]

    snap0.constraints.N = 1
    snap0.constraints.value = [1.1]
    snap0.constraints.group = [[0, 1]]

    snap0.pairs.N = 1
    snap0.pairs.types = ['pairA', 'pairB']
    snap0.pairs.typeid = [1]
    snap0.pairs.group = [[0, 3]]

    snap0.log['value'] = [1, 2, 4, 10, 12, 18, 22]

    snap1 = gsd.hoomd.Snapshot()
    snap1.particles.N = 2
    snap1.particles.position = [[-2, -1, 0], [1, 3.0, 0.5]]

    snap2 = gsd.hoomd.Snapshot()
    snap2.particles.N = 3
    snap2.particles.types = ['q', 's']
    snap2.particles.type_shapes = \
        [{}, {"type": "Ellipsoid", "a": 7.0, "b": 5.0, "c": 3.0}]
    snap2.bonds.N = 3
    snap2.angles.N = 4
    snap2.dihedrals.N = 5
    snap2.impropers.N = 6
    snap2.constraints.N = 4
    snap2.pairs.N = 7

    with gsd.hoomd.open(name=tmp_path / "test_fallback.gsd",
github glotzerlab / gsd / gsd / hoomd.py View on Github external
`Snapshot` with the frame data

        Replace any data chunks not present in the given frame with either data
        from frame 0, or initialize from default values if not in frame 0. Cache
        frame 0 data to avoid file read overhead. Return any default data as
        non-writable numpy arrays.
        """
        if idx >= len(self):
            raise IndexError

        logger.debug('reading frame ' + str(idx) + ' from: ' + str(self.file))

        if self._initial_frame is None and idx != 0:
            self.read_frame(0)

        snap = Snapshot()
        # read configuration first
        if self.file.chunk_exists(frame=idx, name='configuration/step'):
            step_arr = self.file.read_chunk(frame=idx,
                                            name='configuration/step')
            snap.configuration.step = step_arr[0]
        else:
            if self._initial_frame is not None:
                snap.configuration.step = self._initial_frame.configuration.step
            else:
                snap.configuration.step = \
                    snap.configuration._default_value['step']

        if self.file.chunk_exists(frame=idx, name='configuration/dimensions'):
            dimensions_arr = self.file.read_chunk(
                frame=idx, name='configuration/dimensions')
            snap.configuration.dimensions = dimensions_arr[0]