Exemple #1
0
    def __init__(self, run=None, master=None, lines=100, foamserver=None):
        """
        :param run: The thread that controls the run
        :param master: The Runner-Object that controls everything
	:param lines: the number of lines the server should remember
        """
        self._run = run
        self._master = master
        self._foamserver = foamserver
        self._lines = RingBuffer(nr=lines)
        self._lastTime = time()
        self._linesLock = Lock()
        self._maxOutputTime = config().getfloat("IsAlive", "maxTimeStart")
    def __init__(self,run=None,master=None,lines=100,foamserver=None):
        """
        @param run: The thread that controls the run
        @param master: The Runner-Object that controls everything
	@param lines: the number of lines the server should remember
        """
        self._run=run
        self._master=master
        self._foamserver=foamserver
        self._lines=RingBuffer(nr=lines)
        self._lastTime=time()
        self._linesLock=Lock()
        self._maxOutputTime=config().getfloat("IsAlive","maxTimeStart")
class FoamAnswerer(object):
    """The class that handles the actual requests (only needed to hide the
    Thread-methods from the world
    """
    def __init__(self,run=None,master=None,lines=100,foamserver=None):
        """
        @param run: The thread that controls the run
        @param master: The Runner-Object that controls everything
	@param lines: the number of lines the server should remember
        """
        self._run=run
        self._master=master
        self._foamserver=foamserver
        self._lines=RingBuffer(nr=lines)
        self._lastTime=time()
        self._linesLock=Lock()
        self._maxOutputTime=config().getfloat("IsAlive","maxTimeStart")

    def _insertLine(self,line):
        """Inserts a new line, not to be called via XMLRPC"""
        self._linesLock.acquire()
        self._lines.insert(line)
        tmp=time()
        if (tmp-self._lastTime)>self._maxOutputTime:
            self._maxOutputTime=tmp-self._lastTime
        self._lastTime=tmp
        self._linesLock.release()

    def isFoamServer(self):
        """This is a Foam-Server (True by default)"""
        return True

    def isLiving(self):
        """The calculation still generates output and therefor seems to be living"""
        return self.elapsedTime()<self._maxOutputTime

    def _kill(self):
        """Interrupts the FOAM-process"""
        if self._run:
            foamLogger().warning("Killed by request")
            self._run.interrupt()
            return True
        else:
            return False

    def stop(self):
        """Stops the run gracefully (after writing the last time-step to disk)"""
        self._master.stopGracefully()
        return True

    def write(self):
        """Makes the program write the next time-step to disk and the continue"""
        self._master.writeResults()
        return True

    def argv(self):
        """Argument vector with which the runner was called"""
        if self._master:
            return self._master.origArgv
        else:
            return []

    def usedArgv(self):
        """Argument vector with which the runner started the run"""
        if self._master:
            return self._master.argv
        else:
            return []

    def isParallel(self):
        """Is it a parallel run?"""
        if self._master:
            return self._master.lam!=None
        else:
            return False

    def procNr(self):
        """How many processors are used?"""
        if self._master:
            if self._master.lam!=None:
                return self._master.lam.cpuNr()
            else:
                return 1
        else:
            return 0

    def nrWarnings(self):
        """Number of warnings the executable emitted"""
        if self._master:
            return self._master.warnings
        else:
            return 0

    def commandLine(self):
        """The command line"""
        if self._master:
            return string.join(self._master.origArgv)
        else:
            return ""

    def actualCommandLine(self):
        """The actual command line used"""
        if self._master:
            return self._master.cmd
        else:
            return ""

    def scriptName(self):
        """Name of the Python-Script that runs the show"""
        return sys.argv[0]

    def runnerData(self):
        """@return: the data the runner collected so far"""
        return self._master.data

    def lastLogLineSeen(self):
        """@return: the time at which the last log-line was seen"""
        return self._master.lastLogLineSeen

    def lastTimeStepSeen(self):
        """@return: the time at which the last log-line was seen"""
        return self._master.lastTimeStepSeen

    def lastLine(self):
        """@return: the last line that was output by the running FOAM-process"""
        self._linesLock.acquire()
        result=self._lines.last()
        self._linesLock.release()
        if not result:
            return ""
            return result

    def tail(self):
        """@return: the current last lines as a string"""
        self._linesLock.acquire()
        tmp=self._lines.dump()
        self._linesLock.release()
        result=""
        for l in tmp:
            result+=l

        return result

    def elapsedTime(self):
        """@return: time in seconds since the last line was output"""
        self._linesLock.acquire()
        result=time()-self._lastTime
        self._linesLock.release()

        return result

    def getEnviron(self,name):
        """@param name: name of an environment variable
        @return: value of the variable, empty string if non-existing"""
        result=""
        if name in environ:
            result=environ[name]
        return result

    def mpi(self):
        """@return: name of the MPI-implementation"""
        return foamMPI()

    def foamVersion(self):
        """Version number of the Foam-Version"""
        return self.getEnviron("WM_PROJECT_VERSION")

    def pyFoamVersion(self):
        """@return: Version number of the PyFoam"""
        return versionString()

    def uname(self):
        """@return: the complete uname-information"""
        return uname()

    def ip(self):
        """@return: the ip of this machine"""
        try:
            address = socket.gethostbyname(socket.gethostname())
            # This gives 127.0.0.1 if specified so in the /etc/hosts ...
        except:
            address = ''
        if not address or address.startswith('127.'):
            # ...the hard way.
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            try:
                s.connect(('4.2.2.1', 0))
                address = s.getsockname()[0]
            except:
                # Got no internet connection
                address="127.0.0.1"
        return address

    def hostname(self):
        """@return: The name of the computer"""
        return uname()[1]

    def configuration(self):
        """@return: all the configured parameters"""
        return config().dump()

    def cwd(self):
        """@return: the current working directory"""
        return path.abspath(path.curdir)

    def pid(self):
        """@return: the PID of the script"""
        return getpid()

    def loadAvg(self):
        """@return: a tuple with the average loads of the last 1, 5 and 15 minutes"""
        return getloadavg()

    def user(self):
        """@return: the user that runs this script"""
        return userName()

    def id(self):
        """@return: an ID for this run: IP:port:startTimestamp """
        return "%s:%d:%f" % (self.ip(),self._foamserver._port,self.startTimestamp())

    def startTimestamp(self):
        """@return: the unix-timestamp of the process start"""
        return self._master.startTimestamp

    def time(self):
        """@return: the current time in the simulation"""
        if self._master.nowTime:
            return self._master.nowTime
        else:
            return 0

    def createTime(self):
        """@return: the time in the simulation for which the mesh was created"""
        if self._master.nowTime:
            return self._master.createTime
        else:
            return 0

    def _readParameter(self,name):
        """Reads a parametr from the controlDict
        @param name: the parameter
        @return: The value"""
        control=ParameterFile(self._master.getSolutionDirectory().controlDict())
        return control.readParameter(name)

    def startTime(self):
        """@return: parameter startTime from the controlDict"""
        return float(self._readParameter("startTime"))

    def endTime(self):
        """@return: parameter endTime from the controlDict"""
        return float(self._readParameter("endTime"))

    def deltaT(self):
        """@return: parameter startTime from the controlDict"""
        return float(self._readParameter("deltaT"))

    def pathToSolution(self):
        """@return: the path to the solution directory"""
        return self._master.getSolutionDirectory().name

    def writtenTimesteps(self):
        """@return: list of the timesteps on disc"""
        return self._master.getSolutionDirectory().getTimes()

    def solutionFiles(self,time):
        """@param time: name of the timestep
        @return: list of the solution files at that timestep"""
        return self._master.getSolutionDirectory()[time].getFiles()

    def listFiles(self,directory):
        """@param directory: Sub-directory of the case
        @return: List of the filenames (not directories) in that case"""
        return self._master.getSolutionDirectory().listFiles(directory)

    def getDictionaryText(self,directory,name):
        """@param directory: Sub-directory of the case
        @param name: name of the dictionary file
        @return: the contents of the file as a big string"""
        return self._master.getSolutionDirectory().getDictionaryText(directory,name)

    def getDictionaryContents(self,directory,name):
        """@param directory: Sub-directory of the case
        @param name: name of the dictionary file
        @return: the contents of the file as a python data-structure"""
        return self._master.getSolutionDirectory().getDictionaryContents(directory,name)

    def writeDictionaryText(self,directory,name,text):
        """Writes the contents of a dictionary
        @param directory: Sub-directory of the case
        @param name: name of the dictionary file
        @param text: String with the dictionary contents"""

        self._master.getSolutionDirectory().writeDictionaryText(directory,name,text)

        return True

    def writeDictionaryContents(self,directory,name,contents):
        """Writes the contents of a dictionary
        @param directory: Sub-directory of the case
        @param name: name of the dictionary file
        @param contents: Python-dictionary with the dictionary contents"""

        self._master.getSolutionDirectory().writeDictionaryContents(directory,name,contents)
        return True

    def getPlots(self):
        """Get all the information about the plots"""
        return allPlots().prepareForTransfer()

    def getPlotData(self):
        """Get all the data for the plots"""
        return allLines().prepareForTransfer()

    def controlDictUnmodified(self):
        """Checks whether there is a pending change to the controlDict"""
        return self._master.controlDict == None

    def getRemark(self):
        """Get the user-defined remark for this job"""
        if self._master.remark:
            return self._master.remark
        else:
            return ""

    def setRemark(self,remark):
        """Overwrite the user-defined remark
        @return: True if the remark was set previously"""
        oldRemark=self._master.remark
        self._master.remark=remark
        return oldRemark!=None

    def jobId(self):
        """Return the job-ID of the queuing-system. Empty if unset"""
        if self._master.jobId:
            return self._master.jobId
        else:
            return ""
Exemple #4
0
class FoamAnswerer(object):
    """The class that handles the actual requests (only needed to hide the
    Thread-methods from the world
    """
    def __init__(self, run=None, master=None, lines=100, foamserver=None):
        """
        :param run: The thread that controls the run
        :param master: The Runner-Object that controls everything
	:param lines: the number of lines the server should remember
        """
        self._run = run
        self._master = master
        self._foamserver = foamserver
        self._lines = RingBuffer(nr=lines)
        self._lastTime = time()
        self._linesLock = Lock()
        self._maxOutputTime = config().getfloat("IsAlive", "maxTimeStart")

    def _insertLine(self, line):
        """Inserts a new line, not to be called via XMLRPC"""
        self._linesLock.acquire()
        self._lines.insert(line)
        tmp = time()
        if (tmp - self._lastTime) > self._maxOutputTime:
            self._maxOutputTime = tmp - self._lastTime
        self._lastTime = tmp
        self._linesLock.release()

    def isFoamServer(self):
        """This is a Foam-Server (True by default)"""
        return True

    def isLiving(self):
        """The calculation still generates output and therefor seems to be living"""
        return self.elapsedTime() < self._maxOutputTime

    def _kill(self):
        """Interrupts the FOAM-process"""
        if self._run:
            foamLogger().warning("Killed by request")
            self._run.interrupt()
            return True
        else:
            return False

    def stop(self):
        """Stops the run gracefully (after writing the last time-step to disk)"""
        self._master.stopGracefully()
        return True

    def write(self):
        """Makes the program write the next time-step to disk and the continue"""
        self._master.writeResults()
        return True

    def argv(self):
        """Argument vector with which the runner was called"""
        if self._master:
            return self._master.origArgv
        else:
            return []

    def usedArgv(self):
        """Argument vector with which the runner started the run"""
        if self._master:
            return self._master.argv
        else:
            return []

    def isParallel(self):
        """Is it a parallel run?"""
        if self._master:
            return self._master.lam != None
        else:
            return False

    def procNr(self):
        """How many processors are used?"""
        if self._master:
            if self._master.lam != None:
                return self._master.lam.cpuNr()
            else:
                return 1
        else:
            return 0

    def nrWarnings(self):
        """Number of warnings the executable emitted"""
        if self._master:
            return self._master.warnings
        else:
            return 0

    def commandLine(self):
        """The command line"""
        if self._master:
            return " ".join(self._master.origArgv)
        else:
            return ""

    def actualCommandLine(self):
        """The actual command line used"""
        if self._master:
            return self._master.cmd
        else:
            return ""

    def scriptName(self):
        """Name of the Python-Script that runs the show"""
        return sys.argv[0]

    def runnerData(self):
        """:return: the data the runner collected so far"""
        return self._master.data

    def lastLogLineSeen(self):
        """:return: the time at which the last log-line was seen"""
        return self._master.lastLogLineSeen

    def lastTimeStepSeen(self):
        """:return: the time at which the last log-line was seen"""
        return self._master.lastTimeStepSeen

    def lastLine(self):
        """:return: the last line that was output by the running FOAM-process"""
        self._linesLock.acquire()
        result = self._lines.last()
        self._linesLock.release()
        if not result:
            return ""
            return result

    def tail(self):
        """:return: the current last lines as a string"""
        self._linesLock.acquire()
        tmp = self._lines.dump()
        self._linesLock.release()
        result = ""
        for l in tmp:
            result += l

        return result

    def elapsedTime(self):
        """:return: time in seconds since the last line was output"""
        self._linesLock.acquire()
        result = time() - self._lastTime
        self._linesLock.release()

        return result

    def getEnviron(self, name):
        """:param name: name of an environment variable
        :return: value of the variable, empty string if non-existing"""
        result = ""
        if name in environ:
            result = environ[name]
        return result

    def mpi(self):
        """:return: name of the MPI-implementation"""
        return foamMPI()

    def foamVersion(self):
        """Version number of the Foam-Version"""
        return self.getEnviron("WM_PROJECT_VERSION")

    def pyFoamVersion(self):
        """:return: Version number of the PyFoam"""
        return versionString()

    def uname(self):
        """:return: the complete uname-information"""
        return uname()

    def ip(self):
        """:return: the ip of this machine"""
        try:
            address = socket.gethostbyname(socket.gethostname())
            # This gives 127.0.0.1 if specified so in the /etc/hosts ...
        except:
            address = ''
        if not address or address.startswith('127.'):
            # ...the hard way.
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            try:
                s.connect(('4.2.2.1', 0))
                address = s.getsockname()[0]
            except:
                # Got no internet connection
                address = "127.0.0.1"
        return address

    def hostname(self):
        """:return: The name of the computer"""
        return uname()[1]

    def configuration(self):
        """:return: all the configured parameters"""
        return config().dump()

    def cwd(self):
        """:return: the current working directory"""
        return path.abspath(path.curdir)

    def pid(self):
        """:return: the PID of the script"""
        return getpid()

    def loadAvg(self):
        """:return: a tuple with the average loads of the last 1, 5 and 15 minutes"""
        return getloadavg()

    def user(self):
        """:return: the user that runs this script"""
        return userName()

    def id(self):
        """:return: an ID for this run: IP:port:startTimestamp """
        return "%s:%d:%f" % (self.ip(), self._foamserver._port,
                             self.startTimestamp())

    def startTimestamp(self):
        """:return: the unix-timestamp of the process start"""
        return self._master.startTimestamp

    def time(self):
        """:return: the current time in the simulation"""
        if self._master.nowTime:
            return self._master.nowTime
        else:
            return 0

    def createTime(self):
        """:return: the time in the simulation for which the mesh was created"""
        if self._master.nowTime:
            return self._master.createTime
        else:
            return 0

    def _readParameter(self, name):
        """Reads a parametr from the controlDict
        :param name: the parameter
        :return: The value"""
        control = ParameterFile(
            self._master.getSolutionDirectory().controlDict())
        return control.readParameter(name)

    def startTime(self):
        """:return: parameter startTime from the controlDict"""
        return float(self._readParameter("startTime"))

    def endTime(self):
        """:return: parameter endTime from the controlDict"""
        return float(self._readParameter("endTime"))

    def deltaT(self):
        """:return: parameter startTime from the controlDict"""
        return float(self._readParameter("deltaT"))

    def pathToSolution(self):
        """:return: the path to the solution directory"""
        return self._master.getSolutionDirectory().name

    def writtenTimesteps(self):
        """:return: list of the timesteps on disc"""
        return self._master.getSolutionDirectory().getTimes()

    def solutionFiles(self, time):
        """:param time: name of the timestep
        :return: list of the solution files at that timestep"""
        return self._master.getSolutionDirectory()[time].getFiles()

    def listFiles(self, directory):
        """:param directory: Sub-directory of the case
        :return: List of the filenames (not directories) in that case"""
        return self._master.getSolutionDirectory().listFiles(directory)

    def getDictionaryText(self, directory, name):
        """:param directory: Sub-directory of the case
        :param name: name of the dictionary file
        :return: the contents of the file as a big string"""
        return self._master.getSolutionDirectory().getDictionaryText(
            directory, name)

    def getDictionaryContents(self, directory, name):
        """:param directory: Sub-directory of the case
        :param name: name of the dictionary file
        :return: the contents of the file as a python data-structure"""
        return self._master.getSolutionDirectory().getDictionaryContents(
            directory, name)

    def writeDictionaryText(self, directory, name, text):
        """Writes the contents of a dictionary
        :param directory: Sub-directory of the case
        :param name: name of the dictionary file
        :param text: String with the dictionary contents"""

        self._master.getSolutionDirectory().writeDictionaryText(
            directory, name, text)

        return True

    def writeDictionaryContents(self, directory, name, contents):
        """Writes the contents of a dictionary
        :param directory: Sub-directory of the case
        :param name: name of the dictionary file
        :param contents: Python-dictionary with the dictionary contents"""

        self._master.getSolutionDirectory().writeDictionaryContents(
            directory, name, contents)
        return True

    def getPlots(self):
        """Get all the information about the plots"""
        return allPlots().prepareForTransfer()

    def getPlotData(self):
        """Get all the data for the plots"""
        return allLines().prepareForTransfer()

    def controlDictUnmodified(self):
        """Checks whether there is a pending change to the controlDict"""
        return self._master.controlDict == None

    def getRemark(self):
        """Get the user-defined remark for this job"""
        if self._master.remark:
            return self._master.remark
        else:
            return ""

    def setRemark(self, remark):
        """Overwrite the user-defined remark
        :return: True if the remark was set previously"""
        oldRemark = self._master.remark
        self._master.remark = remark
        return oldRemark != None

    def jobId(self):
        """Return the job-ID of the queuing-system. Empty if unset"""
        if self._master.jobId:
            return self._master.jobId
        else:
            return ""