Ejemplo n.º 1
0
def read_gsd(filename, restart=None, frame=0, time_step=None):
    R""" Read initial system state from an GSD file.

    Args:
        filename (str): File to read.
        restart (str): If it exists, read the file *restart* instead of *filename*.
        frame (int): Index of the frame to read from the GSD file.
        time_step (int): (if specified) Time step number to initialize instead of the one stored in the GSD file.

    All particles, bonds, angles, dihedrals, impropers, constraints, and box information
    are read from the given GSD file at the given frame index. To read and write GSD files
    outside of hoomd, see http://gsd.readthedocs.io/. :py:class:`hoomd.dump.gsd` writes GSD files.

    For restartable jobs, specify the initial condition in *filename* and the restart file in *restart*.
    :py:func:`hoomd.init.read_gsd` will read the restart file if it exists, otherwise it will read *filename*.

    If *time_step* is specified, its value will be used as the initial time
    step of the simulation instead of the one read from the GSD file.

    The result of :py:func:`hoomd.init.read_gsd` can be saved in a variable and later used to read and/or
    change particle properties later in the script. See :py:mod:`hoomd.data` for more information.

    See Also:
        :py:class:`hoomd.dump.gsd`
    """
    hoomd.util.print_status_line()

    hoomd.context._verify_init()

    # check if initialization has already occured
    if is_initialized():
        hoomd.context.msg.error("Cannot initialize more than once\n")
        raise RuntimeError("Error initializing")

    if restart is not None and os.path.exists(restart):
        reader = _hoomd.GSDReader(hoomd.context.exec_conf, restart, frame)
    else:
        reader = _hoomd.GSDReader(hoomd.context.exec_conf, filename, frame)
    snapshot = reader.getSnapshot()
    if time_step is None:
        time_step = reader.getTimeStep()

    # broadcast snapshot metadata so that all ranks have _global_box (the user may have set box only on rank 0)
    snapshot._broadcast(hoomd.context.exec_conf)
    my_domain_decomposition = _create_domain_decomposition(
        snapshot._global_box)

    if my_domain_decomposition is not None:
        hoomd.context.current.system_definition = _hoomd.SystemDefinition(
            snapshot, hoomd.context.exec_conf, my_domain_decomposition)
    else:
        hoomd.context.current.system_definition = _hoomd.SystemDefinition(
            snapshot, hoomd.context.exec_conf)

    # initialize the system
    hoomd.context.current.system = _hoomd.System(
        hoomd.context.current.system_definition, time_step)

    _perform_common_init_tasks()
    return hoomd.data.system_data(hoomd.context.current.system_definition)
Ejemplo n.º 2
0
    def create_state_from_gsd(self, filename, frame=-1):
        """Create the simulation state from a GSD file.

        Args:
            filename (str): GSD file to read

            frame (int): Index of the frame to read from the file. Negative
                values index back from the last frame in the file.
        """
        if self.state is not None:
            raise RuntimeError("Cannot initialize more than once\n")
        filename = _hoomd.mpi_bcast_str(filename, self.device._cpp_exec_conf)
        # Grab snapshot and timestep
        reader = _hoomd.GSDReader(self.device._cpp_exec_conf, filename,
                                  abs(frame), frame < 0)
        snapshot = Snapshot._from_cpp_snapshot(reader.getSnapshot(),
                                               self.device.communicator)

        step = reader.getTimeStep() if self.timestep is None else self.timestep
        self._state = State(self, snapshot)

        reader.clearSnapshot()
        # Store System and Reader for Operations
        self._cpp_sys = _hoomd.System(self.state._cpp_sys_def, step)
        self._init_communicator()
        self.operations._store_reader(reader)
Ejemplo n.º 3
0
 def setup_system(self, cls, shape_params, expected_shapespec, filename,
                  dim):
     if dim == 2:
         system = hoomd.init.create_lattice(
             unitcell=hoomd.lattice.sq(a=5.50), n=5)
     elif dim == 3:
         system = hoomd.init.create_lattice(
             unitcell=hoomd.lattice.sc(a=5.50), n=5)
     snapshot = system.take_snapshot(all=True)
     bindex = np.random.choice(range(5**dim),
                               int(0.5 * 5**dim),
                               replace=False)
     if comm.get_rank() == 0:
         snapshot.particles.types = ['A', 'B']
         snapshot.particles.typeid[bindex] = 1
     hoomd.context.initialize()
     system = hoomd.init.read_snapshot(snapshot)
     md.integrate.mode_standard(dt=0.001)
     obj = cls(nlist=md.nlist.cell(), radius=.5)
     obj.setParams('A', **shape_params["A"])
     obj.setParams('B', **shape_params["B"])
     md.integrate.nvt(group=group.all(), kT=1.0, tau=0.5)
     dumper = dump.gsd(filename=filename,
                       group=group.all(),
                       period=1,
                       overwrite=True)
     dumper.dump_shape(obj)
     steps = 5
     hoomd.run(steps)
     reader = _hoomd.GSDReader(hoomd.context.exec_conf, filename, 0, False)
     if comm.get_rank() == 0:
         for i in range(steps):
             shape_spec = parse_shape_spec(reader.readTypeShapesPy(i))
             self.assertEqual(shape_spec[0], expected_shapespec[0])
             self.assertEqual(shape_spec[1], expected_shapespec[1])
Ejemplo n.º 4
0
 def setup_system(self, cls, shape_params, expected_shapespec, filename,
                  dim):
     if dim == 2:
         system = hoomd.init.create_lattice(
             unitcell=hoomd.lattice.sq(a=5.50), n=5)
     elif dim == 3:
         system = hoomd.init.create_lattice(
             unitcell=hoomd.lattice.sc(a=5.50), n=5)
     snapshot = system.take_snapshot(all=True)
     bindex = np.random.choice(range(5**dim),
                               int(0.5 * 5**dim),
                               replace=False)
     if hoomd.context.current.device.comm.rank == 0:
         snapshot.particles.types = ['A', 'B']
         snapshot.particles.typeid[bindex] = 1
     hoomd.context.initialize()
     system = hoomd.init.read_snapshot(snapshot)
     obj = cls(seed=123)
     obj.shape_param.set('A', **shape_params["A"])
     obj.shape_param.set('B', **shape_params["B"])
     dumper = dump.gsd(filename=filename,
                       group=group.all(),
                       period=1,
                       overwrite=True)
     dumper.dump_shape(obj)
     steps = 5
     hoomd.run(steps)
     reader = _hoomd.GSDReader(hoomd.context.current.device.cpp_exec_conf,
                               filename, 0, False)
     if hoomd.context.current.device.comm.rank == 0:
         for i in range(steps):
             shape_spec = parse_shape_spec(reader.readTypeShapesPy(i))
             self.assertEqual(shape_spec[0], expected_shapespec[0])
             self.assertEqual(shape_spec[1], expected_shapespec[1])
Ejemplo n.º 5
0
    def create_state_from_gsd(self,
                              filename,
                              frame=-1,
                              domain_decomposition=(None, None, None)):
        """Create the simulation state from a GSD file.

        Args:
            filename (str): GSD file to read

            frame (int): Index of the frame to read from the file. Negative
                values index back from the last frame in the file.

            domain_decomposition (tuple): Choose how to distribute the state
                across MPI ranks with domain decomposition. Provide a tuple
                of 3 integers indicating the number of evenly spaced domains in
                the x, y, and z directions (e.g. ``(8,4,2)``). Provide a tuple
                of 3 lists of floats to set the fraction of the simulation box
                to include in each domain. The sum of each list of floats must
                be 1.0 (e.g. ``([0.25, 0.75], [0.2, 0.8], [1.0])``).

        When `timestep` is `None` before calling, `create_state_from_gsd`
        sets `timestep` to the value in the selected GSD frame in the file.

        Note:
            Set any or all of the ``domain_decomposition`` tuple elements to
            `None` and `create_state_from_gsd` will select a value that
            minimizes the surface area between the domains (e.g.
            ``(2,None,None)``). The domains are spaced evenly along each
            automatically selected direction. The default value of ``(None,
            None, None)`` will automatically select the number of domains in all
            directions.
        """
        if self._state is not None:
            raise RuntimeError("Cannot initialize more than once\n")
        filename = _hoomd.mpi_bcast_str(filename, self.device._cpp_exec_conf)
        # Grab snapshot and timestep
        reader = _hoomd.GSDReader(self.device._cpp_exec_conf, filename,
                                  abs(frame), frame < 0)
        snapshot = Snapshot._from_cpp_snapshot(reader.getSnapshot(),
                                               self.device.communicator)

        step = reader.getTimeStep() if self.timestep is None else self.timestep
        self._state = State(self, snapshot, domain_decomposition)

        reader.clearSnapshot()

        self._init_system(step)