Example #1
0
    def snapshot(self):
        r"""hoomd.Snapshot: All data of a simulation's current microstate.

        `State.snapshot` should be used when all of a simulation's state
        information is desired in a single object. When accessed, data across
        all MPI ranks and from GPUs is gathered on the root MPI rank's memory.
        When accessing data in MPI simulations, it is recommended to use a
        ``if snapshot.exists:`` conditional to prevent attempting to access data
        on a non-root rank.

        This property can be set to replace the system state with the given
        `hoomd.Snapshot` object.  Example use cases in which a simulation's
        state may be reset from a snapshot include Monte Carlo schemes
        implemented at the Python script level, where the current snapshot is
        passed to the Monte Carlo simulation before being passed back after
        running some Monte Carlo steps.

        Warning:
            Using `State.snapshot` multiple times will gather data across MPI
            ranks and GPUs every time. If the snapshot is needed for more than
            one use, it is recommended to store it in a variable.

        Note:
            Setting or getting a snapshot is an order :math:`O(N_{particles}
            + N_{bonds} + \ldots)` operation.
        """
        cpp_snapshot = self._cpp_sys_def.takeSnapshot_double()
        return Snapshot._from_cpp_snapshot(cpp_snapshot,
                                           self._simulation.device.communicator)
Example #2
0
    def get_snapshot(self):
        """Make a copy of the simulation current state.

        `State.get_snapshot` makes a copy of the simulation state and
        makes it available in a single object. `State.set_snapshot` resets
        the internal state to that in the given snapshot. Use these methods
        to implement techniques like hybrid MD/MC or umbrella sampling where
        entire system configurations need to be reset to a previous one after a
        rejected move.

        Note:
            Data across all MPI ranks and from GPUs is gathered on the root MPI
            rank's memory. When accessing data in MPI simulations, use a ``if
            snapshot.communicator.rank == 0:`` conditional to access data arrays
            only on the root rank.

        Note:
            `State.get_snapshot` is an order :math:`O(N_{particles} + N_{bonds}
            + \\ldots)` operation.

        See Also:
            `set_snapshot`

        Returns:
            hoomd.Snapshot: The current simulation state
        """
        cpp_snapshot = self._cpp_sys_def.takeSnapshot_double()
        return Snapshot._from_cpp_snapshot(
            cpp_snapshot, self._simulation.device.communicator)
Example #3
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)
Example #4
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)