Beispiel #1
0
    def __init__(self,
                 latency=1.0,
                 name="",
                 pars=None,
                 dopbc=True,
                 active=np.array([-1]),
                 interface=None):
        """Initialises FFSocket.

        Args:
           latency: The number of seconds the socket will wait before updating
              the client list.
           name: The name of the forcefield.
           pars: A dictionary used to initialize the forcefield, if required.
              Of the form {'name1': value1, 'name2': value2, ... }.
           dopbc: Decides whether or not to apply the periodic boundary conditions
              before sending the positions to the client code.
           interface: The object used to create the socket used to interact
              with the client codes.
        """

        # a socket to the communication library is created or linked
        super(FFSocket, self).__init__(latency, name, pars, dopbc, active)
        if interface is None:
            self.socket = InterfaceSocket()
        else:
            self.socket = interface
        self.socket.requests = self.requests
Beispiel #2
0
    def fetch(self):
        """Creates a ForceSocket object.

        Returns:
           A ForceSocket object with the correct socket parameters.
        """

        if self.threaded.fetch() == False:
            raise ValueError("FFSockets cannot poll without threaded mode.")
        # just use threaded throughout
        return FFSocket(
            pars=self.parameters.fetch(),
            name=self.name.fetch(),
            latency=self.latency.fetch(),
            dopbc=self.pbc.fetch(),
            active=self.activelist.fetch(),
            threaded=self.threaded.fetch(),
            interface=InterfaceSocket(
                address=self.address.fetch(),
                port=self.port.fetch(),
                slots=self.slots.fetch(),
                mode=self.mode.fetch(),
                timeout=self.timeout.fetch(),
                match_mode=self.matching.fetch(),
                exit_on_disconnect=self.exit_on_disconnect.fetch()))
Beispiel #3
0
   def fetch(self):
      """Creates a ForceSocket object.

      Returns:
         A ForceSocket object with the correct socket parameters.
      """

      return FFSocket(pars = self.parameters.fetch(), name = self.name.fetch(), latency = self.latency.fetch(), dopbc = self.pbc.fetch(),
              interface=InterfaceSocket(address=self.address.fetch(), port=self.port.fetch(),
            slots=self.slots.fetch(), mode=self.mode.fetch(), timeout=self.timeout.fetch() ) )
Beispiel #4
0
   def __init__(self, pars=None, interface=None):
      """Initializes FFSocket.

      Args:
         pars: Optional dictionary, giving the parameters needed by the driver.
         interface: Optional Interface object, which contains the socket.
      """

      # a socket to the communication library is created or linked
      super(FFSocket,self).__init__()
      if interface is None:
         self.socket = InterfaceSocket()
      else:
         self.socket = interface

      if pars is None:
         self.pars = {}
      else:
         self.pars = pars
      self.request = None
Beispiel #5
0
class FFSocket(ForceField):
    """Interface between the PIMD code and a socket for a single replica.

    Deals with an individual replica of the system, obtaining the potential
    force and virial appropriate to this system. Deals with the distribution of
    jobs to the interface.

    Attributes:
        socket: The interface object which contains the socket through which
            communication between the forcefield and the driver is done.
    """
    def __init__(self,
                 latency=1.0,
                 name="",
                 pars=None,
                 dopbc=True,
                 active=np.array([-1]),
                 interface=None):
        """Initialises FFSocket.

        Args:
           latency: The number of seconds the socket will wait before updating
              the client list.
           name: The name of the forcefield.
           pars: A dictionary used to initialize the forcefield, if required.
              Of the form {'name1': value1, 'name2': value2, ... }.
           dopbc: Decides whether or not to apply the periodic boundary conditions
              before sending the positions to the client code.
           interface: The object used to create the socket used to interact
              with the client codes.
        """

        # a socket to the communication library is created or linked
        super(FFSocket, self).__init__(latency, name, pars, dopbc, active)
        if interface is None:
            self.socket = InterfaceSocket()
        else:
            self.socket = interface
        self.socket.requests = self.requests

    def poll(self):
        """Function to check the status of the client calculations."""

        self.socket.poll()

    def run(self):
        """Spawns a new thread."""

        self.socket.open()
        super(FFSocket, self).run()

    def stop(self):
        """Closes the socket and the thread."""

        super(FFSocket, self).stop()
        if self._thread is not None:
            # must wait until loop has ended before closing the socket
            self._thread.join()
        self.socket.close()
Beispiel #6
0
class FFSocket(ForceField):

    """Interface between the PIMD code and a socket for a single replica.

    Deals with an individual replica of the system, obtaining the potential
    force and virial appropriate to this system. Deals with the distribution of
    jobs to the interface.

    Attributes:
        socket: The interface object which contains the socket through which
            communication between the forcefield and the driver is done.
    """

    def __init__(self, latency=1.0, name="", pars=None, dopbc=True, active=np.array([-1]), threaded=True, interface=None, matching="auto"):
        """Initialises FFSocket.

        Args:
           latency: The number of seconds the socket will wait before updating
              the client list.
           name: The name of the forcefield.
           pars: A dictionary used to initialize the forcefield, if required.
              Of the form {'name1': value1, 'name2': value2, ... }.
           dopbc: Decides whether or not to apply the periodic boundary conditions
              before sending the positions to the client code.
           interface: The object used to create the socket used to interact
              with the client codes.
        """

        # a socket to the communication library is created or linked
        super(FFSocket, self).__init__(latency, name, pars, dopbc, active, threaded)
        if interface is None:
            self.socket = InterfaceSocket()
        else:
            self.socket = interface
        self.socket.requests = self.requests

    def poll(self):
        """Function to check the status of the client calculations."""

        self.socket.poll()

    def start(self):
        """Spawns a new thread."""

        self.socket.open()
        super(FFSocket, self).start()

    def stop(self):
        """Closes the socket and the thread."""

        super(FFSocket, self).stop()
        if self._thread is not None:
            # must wait until loop has ended before closing the socket
            self._thread.join()
        self.socket.close()
Beispiel #7
0
   def __init__(self, pars=None, interface=None):
      """Initialises FFSocket.

      Args:
         pars: Optional dictionary, giving the parameters needed by the driver.
         interface: Optional Interface object, which contains the socket.
      """

      # a socket to the communication library is created or linked
      super(FFSocket,self).__init__()
      if interface is None:
         self.socket = InterfaceSocket()
      else:
         self.socket = interface

      if pars is None:
         self.pars = {}
      else:
         self.pars = pars
      self.request = None
Beispiel #8
0
    def __init__(self, latency=1.0, name="", pars=None, dopbc=True, active=np.array([-1]), threaded=True, interface=None, matching="auto"):
        """Initialises FFSocket.

        Args:
           latency: The number of seconds the socket will wait before updating
              the client list.
           name: The name of the forcefield.
           pars: A dictionary used to initialize the forcefield, if required.
              Of the form {'name1': value1, 'name2': value2, ... }.
           dopbc: Decides whether or not to apply the periodic boundary conditions
              before sending the positions to the client code.
           interface: The object used to create the socket used to interact
              with the client codes.
        """

        # a socket to the communication library is created or linked
        super(FFSocket, self).__init__(latency, name, pars, dopbc, active, threaded)
        if interface is None:
            self.socket = InterfaceSocket()
        else:
            self.socket = interface
        self.socket.requests = self.requests
Beispiel #9
0
class FFSocket(ForceField):
   """Interface between the PIMD code and the socket for a single replica.

   Deals with an individual replica of the system, obtaining the potential
   force and virial appropriate to this system. Deals with the distribution of
   jobs to the interface.

   Attributes:
      parameters: A dictionary of the parameters used by the driver. Of the
         form {'name': value}.
      socket: The interface object which contains the socket through which
         communication between the forcefield and the driver is done.
      request: During the force calculation step this holds a dictionary
         containing the relevant data for determining the progress of the step.
         Of the form {'atoms': atoms, 'cell': cell, 'pars': parameters,
                      'status': status, 'result': result, 'id': bead id,
                      'start': starting time}.
   """

   def __init__(self, pars=None, interface=None):
      """Initialises FFSocket.

      Args:
         pars: Optional dictionary, giving the parameters needed by the driver.
         interface: Optional Interface object, which contains the socket.
      """

      # a socket to the communication library is created or linked
      super(FFSocket,self).__init__()
      if interface is None:
         self.socket = InterfaceSocket()
      else:
         self.socket = interface

      if pars is None:
         self.pars = {}
      else:
         self.pars = pars
      self.request = None

   def bind(self, atoms, cell):
      """Pass on the binding request from ForceBeads.

      Also makes sure to set the socket's softexit.

      Args:
         atoms: Atoms object from which the bead positions are taken.
         cell: Cell object from which the system box is taken.
      """

      super(FFSocket,self).bind(atoms, cell)

   def copy(self):
      """Creates a deep copy without the bound objects.

      Used in ForceBeads to create a FFSocket for each replica of the system.

      Returns:
         A FFSocket object without atoms or cell attributes.
      """

      # does not copy the bound objects
      # (i.e., the returned forcefield must be bound before use)
      return type(self)(self.pars, self.socket)

   def get_all(self):
      """Driver routine.

      When one of the force, potential or virial are called, this sends the
      atoms and cell to the driver through the interface, requesting that the
      driver does the calculation. This then waits until the driver is finished,
      and then returns the ufvx list.

      Returns:
         A list of the form [potential, force, virial, extra].
      """

      # this is converting the distribution library requests into [ u, f, v ]  lists
      if self.request is None:
         self.request = self.socket.queue(self.atoms, self.cell, pars=self.pars, reqid=-1)
      while self.request["status"] != "Done":
         if self.request["status"] == "Exit":
            break
         time.sleep(self.socket.latency)
      if self.request["status"] == "Exit":
         softexit.trigger(" @Force: Requested returned a Exit status")

      # data has been collected, so the request can be released and a slot
      #freed up for new calculations
      self.socket.release(self.request)
      result = self.request["result"]
      self.request = None

      return result

   def queue(self, reqid=-1):
      """Sends the job to the interface queue directly.

      Allows the ForceBeads object to ask for the ufvx list of each replica
      directly without going through the get_all function. This allows
      all the jobs to be sent at once, allowing them to be parallelized.

      Args:
         reqid: An optional integer that indentifies requests of the same type,
            e.g. the bead index.
      """

      if self.request is None and dget(self,"ufvx").tainted():
         self.request = self.socket.queue(self.atoms, self.cell, pars=self.pars, reqid=reqid)

   def run(self):
      """Makes the socket start looking for driver codes.

      Tells the interface code to start the thread that looks for
      connection from the driver codes in a loop. Until this point no
      jobs can be queued.
      """

      if not self.socket.started():
         self.socket.start_thread()

   def stop(self):
      """Makes the socket stop looking for driver codes.

      Tells the interface code to stop the thread that looks for
      connection from the driver codes in a loop. After this point no
      jobs can be queued.
      """

      if self.socket.started():
         self.socket.end_thread()
Beispiel #10
0
class FFSocket(ForceField):
   """Interface between the PIMD code and the socket for a single replica.

   Deals with an individual replica of the system, obtaining the potential
   force and virial appropriate to this system. Deals with the distribution of
   jobs to the interface.

   Attributes:
      parameters: A dictionary of the parameters used by the driver. Of the
         form {'name': value}.
      socket: The interface object which contains the socket through which
         communication between the forcefield and the driver is done.
      request: During the force calculation step this holds a dictionary
         containing the relevant data for determining the progress of the step.
         Of the form {'atoms': atoms, 'cell': cell, 'pars': parameters,
                      'status': status, 'result': result, 'id': bead id,
                      'start': starting time}.
   """

   def __init__(self, pars=None, interface=None):
      """Initializes FFSocket.

      Args:
         pars: Optional dictionary, giving the parameters needed by the driver.
         interface: Optional Interface object, which contains the socket.
      """

      # a socket to the communication library is created or linked
      super(FFSocket,self).__init__()
      if interface is None:
         self.socket = InterfaceSocket()
      else:
         self.socket = interface

      if pars is None:
         self.pars = {}
      else:
         self.pars = pars
      self.request = None

   def bind(self, atoms, cell):
      """Pass on the binding request from ForceBeads.

      Also makes sure to set the socket's softexit.

      Args:
         atoms: Atoms object from which the bead positions are taken.
         cell: Cell object from which the system box is taken.
      """

      super(FFSocket,self).bind(atoms, cell)

   def copy(self):
      """Creates a deep copy without the bound objects.

      Used in ForceBeads to create a FFSocket for each replica of the system.

      Returns:
         A FFSocket object without atoms or cell attributes.
      """

      # does not copy the bound objects
      # (i.e., the returned forcefield must be bound before use)
      return type(self)(self.pars, self.socket)

   def get_all(self):
      """Driver routine.

      When one of the force, potential or virial are called, this sends the
      atoms and cell to the driver through the interface, requesting that the
      driver does the calculation. This then waits until the driver is finished,
      and then returns the ufvx list.

      Returns:
         A list of the form [potential, force, virial, extra].
      """

      # this is converting the distribution library requests into [ u, f, v ]  lists
      if self.request is None:
         self.request = self.socket.queue(self.atoms, self.cell, pars=self.pars, reqid=-1)
      while self.request["status"] != "Done":
         if self.request["status"] == "Exit":
            break
         time.sleep(self.socket.latency)
      if self.request["status"] == "Exit":
         softexit.trigger(" @Force: Requested returned a Exit status")

      # data has been collected, so the request can be released and a slot
      #freed up for new calculations
      self.socket.release(self.request)
      result = self.request["result"]
      self.request = None

      return result

   def queue(self, reqid=-1):
      """Sends the job to the interface queue directly.

      Allows the ForceBeads object to ask for the ufvx list of each replica
      directly without going through the get_all function. This allows
      all the jobs to be sent at once, allowing them to be parallelized.

      Args:
         reqid: An optional integer that indentifies requests of the same type,
            e.g. the bead index.
      """

      if self.request is None and dget(self,"ufvx").tainted():
         self.request = self.socket.queue(self.atoms, self.cell, pars=self.pars, reqid=reqid)

   def run(self):
      """Makes the socket start looking for driver codes.

      Tells the interface code to start the thread that looks for
      connection from the driver codes in a loop. Until this point no
      jobs can be queued.
      """

      if not self.socket.started():
         self.socket.start_thread()

   def stop(self):
      """Makes the socket stop looking for driver codes.

      Tells the interface code to stop the thread that looks for
      connection from the driver codes in a loop. After this point no
      jobs can be queued.
      """

      if self.socket.started():
         self.socket.end_thread()
Beispiel #11
0
def test_interface():
    """InterfaceSocket: startup."""
    InterfaceSocket()