Exemplo n.º 1
0
    def bind(self):
        """Calls the bind routines for all the objects in the simulation."""

        if self.tsteps <= self.step:
            raise ValueError(
                "Simulation has already run for total_steps, will not even start. "
                "Modify total_steps or step counter to continue.")

        for s in self.syslist:
            # binds important computation engines
            s.bind(self)

        self.outputs = []
        for o in self.outtemplate:
            if type(
                    o
            ) is eoutputs.CheckpointOutput:  # checkpoints are output per simulation
                o.bind(self)
                self.outputs.append(o)
            else:  # properties and trajectories are output per system
                isys = 0
                for s in self.syslist:  # create multiple copies
                    no = deepcopy(o)
                    if s.prefix != "":
                        no.filename = s.prefix + "_" + no.filename
                    no.bind(s)
                    self.outputs.append(no)
                    isys += 1

        self.chk = eoutputs.CheckpointOutput("RESTART", 1, True, 0)
        self.chk.bind(self)

        if self.mode == "paratemp":
            self.paratemp.bind(self.syslist, self.prng)
            softexit.register_function(self.paratemp.softexit)
Exemplo n.º 2
0
    def bind(self, system):
        """Binds output proxy to System object.

        Args:
           system: A System object to be bound.
        """

        self.system = system

        # Checks as soon as possible if some asked-for trajs are missing or mispelled
        key = getkey(self.what)
        if not key in self.system.trajs.traj_dict.keys():
            print "Computable trajectories list: ", self.system.trajs.traj_dict.keys()
            raise KeyError(key + " is not a recognized output trajectory")

        self.open_stream()
        softexit.register_function(self.softexit)
Exemplo n.º 3
0
    def run(self):
        """Spawns a new thread.

        Splits the main program into two threads, one that runs the polling loop
        which updates the client list, and one which gets the data.

        Raises:
            NameError: Raised if the polling thread already exists.
        """

        if not self._thread is None:
            raise NameError("Polling thread already started")

        self._doloop[0] = True
        self._thread = threading.Thread(target=self._poll_loop, name="poll_" + self.name)
        self._thread.daemon = True
        self._thread.start()
        softexit.register_function(self.softexit)
        softexit.register_thread(self._thread, self._doloop)
Exemplo n.º 4
0
    def bind(self, system):
        """Binds output proxy to System object.

        Args:
           system: A System object to be bound.
        """

        self.system = system

        # Checks as soon as possible if some asked-for properties are
        # missing or mispelled
        for what in self.outlist:
            key = getkey(what)
            if not key in self.system.properties.property_dict.keys():
                print "Computable properties list: ", self.system.properties.property_dict.keys()
                raise KeyError(key + " is not a recognized property")

        self.open_stream()
        softexit.register_function(self.softexit)
Exemplo n.º 5
0
    def run(self):
        """Spawns a new thread.

        Splits the main program into two threads, one that runs the polling loop
        which updates the client list, and one which gets the data.

        Raises:
            NameError: Raised if the polling thread already exists.
        """

        if not self._thread is None:
            raise NameError("Polling thread already started")

        self._doloop[0] = True
        self._thread = threading.Thread(target=self._poll_loop, name="poll_" + self.name)
        self._thread.daemon = True
        self._thread.start()
        softexit.register_function(self.softexit)
        softexit.register_thread(self._thread, self._doloop)
Exemplo n.º 6
0
    def run(self):
        """Runs the simulation.

        Does all the simulation steps, and outputs data to the appropriate files
        when necessary. Also deals with starting and cleaning up the threads used
        in the communication between the driver and the PIMD code.
        """

        # registers the softexit routine
        softexit.register_function(self.softexit)
        softexit.start(self.ttime)

#        for k, f in self.fflist.iteritems():
#            f.run()

        # prints inital configuration -- only if we are not restarting
        if self.step == 0:
            self.step = -1
            # must use multi-threading to avoid blocking in multi-system runs with WTE
            if self.threading:
                stepthreads = []
                for o in self.outputs:
                    st = threading.Thread(target=o.write, name=o.filename)
                    st.daemon = True
                    st.start()
                    stepthreads.append(st)

                for st in stepthreads:
                    while st.isAlive():
                        # This is necessary as join() without timeout prevents main from receiving signals.
                        st.join(2.0)
            else:
                for o in self.outputs:
                    o.write()  # threaded output seems to cause random hang-ups. should make things properly thread-safe

            self.step = 0

        steptime = 0.0
        simtime = time.time()

        cstep = 0
        #tptime = 0.0
        #tqtime = 0.0
        #tttime = 0.0
        ttot = 0.0
        # main MD loop
        for self.step in xrange(self.step, self.tsteps):
            # stores the state before doing a step.
            # this is a bit time-consuming but makes sure that we can honor soft
            # exit requests without screwing the trajectory

            steptime = -time.time()
            if softexit.triggered:
                break

            self.chk.store()

            if self.threading:
                stepthreads = []
                # steps through all the systems
                for s in self.syslist:
                    # creates separate threads for the different systems
                    st = threading.Thread(target=s.motion.step, name=s.prefix, kwargs={"step": self.step})
                    st.daemon = True
                    stepthreads.append(st)

                for st in stepthreads:
                    st.start()

                for st in stepthreads:
                    while st.isAlive():
                        # This is necessary as join() without timeout prevents main from receiving signals.
                        st.join(2.0)
            else:
                for s in self.syslist:
                    s.motion.step(step=self.step)

            if softexit.triggered:
                # Don't continue if we are about to exit.
                break

            # does the "super motion" step
            if self.smotion is not None:
                # TODO: We need a file where we store the exchanges
                self.smotion.step(self.step)

            if softexit.triggered:
                # Don't write if we are about to exit.
                break

            if self.threading:
                stepthreads = []
                for o in self.outputs:
                    st = threading.Thread(target=o.write, name=o.filename)
                    st.daemon = True
                    st.start()
                    stepthreads.append(st)

                for st in stepthreads:
                    while st.isAlive():
                        # This is necessary as join() without timeout prevents main from receiving signals.
                        st.join(2.0)
            else:
                for o in self.outputs:
                    o.write()

            steptime += time.time()
            ttot += steptime
            cstep += 1

            if (verbosity.high or (verbosity.medium and self.step % 100 == 0) or (verbosity.low and self.step % 1000 == 0)):
                info(" # Average timings at MD step % 7d. t/step: %10.5e" % (self.step, ttot / cstep))
                cstep = 0
                ttot = 0.0
                # info(" # MD diagnostics: V: %10.5e    Kcv: %10.5e   Ecns: %10.5e" %
                #     (self.properties["potential"], self.properties["kinetic_cv"], self.properties["conserved"] ) )

            if os.path.exists("EXIT"):
                info(" # EXIT file detected! Bye bye!", verbosity.low)
                break

            if (self.ttime > 0) and (time.time() - simtime > self.ttime):
                info(" # Wall clock time expired! Bye bye!", verbosity.low)
                break

        self.rollback = False
Exemplo n.º 7
0
    def bind(self, mode="w"):
        """Stores a reference to system and registers for exiting"""

        self.open_stream(mode)
        softexit.register_function(self.softexit)
Exemplo n.º 8
0
    def run(self):
        """Runs the simulation.

        Does all the simulation steps, and outputs data to the appropriate files
        when necessary. Also deals with starting and cleaning up the threads used
        in the communication between the driver and the PIMD code.
        """

        # registers the softexit routine
        softexit.register_function(self.softexit)
        softexit.start(self.ttime)

        # prints inital configuration -- only if we are not restarting
        if self.step == 0:
            self.step = -1
            # must use multi-threading to avoid blocking in multi-system runs with WTE
            if self.threading:
                stepthreads = []
                for o in self.outputs:
                    st = threading.Thread(target=o.write, name=o.filename)
                    st.daemon = True
                    st.start()
                    stepthreads.append(st)

                for st in stepthreads:
                    while st.is_alive():
                        # This is necessary as join() without timeout prevents main from receiving signals.
                        st.join(2.0)
            else:
                for o in self.outputs:
                    o.write(
                    )  # threaded output seems to cause random hang-ups. should make things properly thread-safe

            self.step = 0

        steptime = 0.0
        simtime = time.time()

        cstep = 0
        # tptime = 0.0
        # tqtime = 0.0
        # tttime = 0.0
        ttot = 0.0
        # main MD loop
        for self.step in range(self.step, self.tsteps):
            # stores the state before doing a step.
            # this is a bit time-consuming but makes sure that we can honor soft
            # exit requests without screwing the trajectory

            steptime = -time.time()
            if softexit.triggered:
                break

            self.chk.store()

            if self.threading:
                stepthreads = []
                # steps through all the systems
                for s in self.syslist:
                    # creates separate threads for the different systems
                    st = threading.Thread(target=s.motion.step,
                                          name=s.prefix,
                                          kwargs={"step": self.step})
                    st.daemon = True
                    stepthreads.append(st)

                for st in stepthreads:
                    st.start()

                for st in stepthreads:
                    while st.is_alive():
                        # This is necessary as join() without timeout prevents main from receiving signals.
                        st.join(2.0)
            else:
                for s in self.syslist:
                    s.motion.step(step=self.step)

            if softexit.triggered:
                # Don't continue if we are about to exit.
                break

            # does the "super motion" step
            if self.smotion is not None:
                # TODO: We need a file where we store the exchanges
                self.smotion.step(self.step)

            if softexit.triggered:
                # Don't write if we are about to exit.
                break

            if self.threading:
                stepthreads = []
                for o in self.outputs:
                    st = threading.Thread(target=o.write, name=o.filename)
                    st.daemon = True
                    st.start()
                    stepthreads.append(st)

                for st in stepthreads:
                    while st.is_alive():
                        # This is necessary as join() without timeout prevents main from receiving signals.
                        st.join(2.0)
            else:
                for o in self.outputs:
                    o.write()

            steptime += time.time()
            ttot += steptime
            cstep += 1

            if (verbosity.high or (verbosity.medium and self.step % 100 == 0)
                    or (verbosity.low and self.step % 1000 == 0)):
                info(" # Average timings at MD step % 7d. t/step: %10.5e" %
                     (self.step, ttot / cstep))
                cstep = 0
                ttot = 0.0
                # info(" # MD diagnostics: V: %10.5e    Kcv: %10.5e   Ecns: %10.5e" %
                #     (self.properties["potential"], self.properties["kinetic_cv"], self.properties["conserved"] ) )

            if os.path.exists("EXIT"):
                info(" # EXIT file detected! Bye bye!", verbosity.low)
                break

            if (self.ttime > 0) and (time.time() - simtime > self.ttime):
                info(" # Wall clock time expired! Bye bye!", verbosity.low)
                break

        self.rollback = False
Exemplo n.º 9
0
Arquivo: outputs.py Projeto: i-pi/i-pi
    def bind(self, mode="w"):
        """ Stores a reference to system and registers for exiting """

        self.open_stream(mode)
        softexit.register_function(self.softexit)
Exemplo n.º 10
0
    def run(self):
        """Runs the simulation.

        Does all the simulation steps, and outputs data to the appropriate files
        when necessary. Also deals with starting and cleaning up the threads used
        in the communication between the driver and the PIMD code.
        """

        # registers the softexit routine
        softexit.register_function(self.softexit)
        softexit.start(self.ttime)

        for k, f in self.fflist.iteritems():
            f.run()

        # prints inital configuration -- only if we are not restarting
        info("Step:", self.step)
        if self.step == 0:
            print           # me c*g
            print "Waiting for all CP2K clients to connect before starting the simulation."          # me c*g
            trials_count = 0          # me c*g
            trials_max = 2*self.syslist[0].beads.nbeads + 500          # me c*g
            while self.syslist[0].beads.nbeads != len(self.fflist["cp2k"].socket.clients):          # me c*g
                trials_count += 1          # me c*g
                print " * Currently " + str(len(self.fflist["cp2k"].socket.clients)) + " of " + str(self.syslist[0].beads.nbeads) + " CP2K clients have connected. Sleeping one second... (trial " + str(trials_count) + " of " + str(trials_max) + ")."          # me c*g
                if trials_count == trials_max:          # me c*g
                    softexit.trigger("Sufficiently many CP2K clients failed to connect within the maximum waiting time of " + str(trials_max) + " seconds.")          # me c*g
                time.sleep(1)          # me c*g
            print "All CP2K clients have connected. Continuing..."          # me c*g
            self.step = -1
            # must use multi-threading to avoid blocking in multi-system runs with WTE
            stepthreads = []
            for o in self.outputs:
                o.write()  # threaded output seems to cause random hang-ups. should make things properly thread-safe
                #st = threading.Thread(target=o.write, name=o.filename)
                #st.daemon = True
                #st.start()
                #stepthreads.append(st)

            for st in stepthreads:
                while st.isAlive():
                    # This is necessary as join() without timeout prevents main
                    # from receiving signals.
                    st.join(2.0)

            if self.mode == "paratemp":
                self.paratemp.parafile.write("%10d" % (self.step + 1))
                for i in self.paratemp.temp_index:
                    self.paratemp.parafile.write(" %5d" % i)
                self.paratemp.parafile.write("\n")
                self.paratemp.parafile.flush()
                os.fsync(self.paratemp.parafile)

            self.step = 0

        steptime = 0.0
        simtime = time.time()

        cstep = 0
        #tptime = 0.0
        #tqtime = 0.0
        #tttime = 0.0
        ttot = 0.0
        # main MD loop
        for self.step in range(self.step, self.tsteps):
            # stores the state before doing a step.
            # this is a bit time-consuming but makes sure that we can honor soft
            # exit requests without screwing the trajectory

            # Checking if no CP2K client has disconnected



            steptime = -time.time()
            if softexit.triggered:
                break

            self.chk.store()

            stepthreads = []
            # steps through all the systems
            #for s in self.syslist:
            #   s.motion.step()
            for s in self.syslist:
                # creates separate threads for the different systems
                #st = threading.Thread(target=s.motion.step, name=s.prefix, kwargs={"step":self.step})
                #st.daemon = True
                s.motion.step(step=self.step)
                #st.start()
                #stepthreads.append(st)

            for st in stepthreads:
                while st.isAlive():
                    # This is necessary as join() without timeout prevents main
                    # from receiving signals.
                    st.join(2.0)

            if softexit.triggered:
                # Don't continue if we are about to exit.
                break

            for o in self.outputs:
                o.write()

            # does parallel tempering
            if self.mode == "paratemp":

                # because of where this is in the loop, we must write out BEFORE doing the swaps.
                self.paratemp.parafile.write("%10d" % (self.step + 1))
                for i in self.paratemp.temp_index:
                    self.paratemp.parafile.write(" %5d" % i)
                self.paratemp.parafile.write("\n")
                self.paratemp.parafile.flush()
                os.fsync(self.paratemp.parafile)

                self.paratemp.swap(self.step)

            if softexit.triggered:
                # Don't write if we are about to exit.
                break

            steptime += time.time()
            ttot += steptime
            cstep += 1

            if (verbosity.high or (verbosity.medium and self.step % 100 == 0) or (verbosity.low and self.step % 1000 == 0)):
                info(" # Average timings at MD step % 7d. t/step: %10.5e" % (self.step, ttot / cstep))
                cstep = 0
                ttot = 0.0
                #info(" # MD diagnostics: V: %10.5e    Kcv: %10.5e   Ecns: %10.5e" %
                #     (self.properties["potential"], self.properties["kinetic_cv"], self.properties["conserved"] ) )

            if os.path.exists("EXIT"):
                info(" # EXIT file detected! Bye bye!", verbosity.low)
                break

            if (self.ttime > 0) and (time.time() - simtime > self.ttime):
                info(" # Wall clock time expired! Bye bye!", verbosity.low)
                break

        self.rollback = False