Trisurf Monte Carlo simulator
Samo Penic
2016-03-16 9154b3cc571c2461010573f84b91fcf84830cf5a
python/trisurf/trisurf.py
@@ -10,6 +10,9 @@
import mmap
import shlex
import psutil
import time
import datetime
'''
This is a trisurf instance manager written in python
@@ -21,7 +24,29 @@
'''
class FileContent:
   def __init__(self,filename):
      self.filename=filename
      self.data=""
      self.readfile()
   def readfile(self):
      try:
         with open (self.filename, "r") as myfile:
            self.data=myfile.read().replace('\n', '')
      except:
         pass
   def writefile(self, data, mode='w'):
      with open (self.filename, mode) as myfile:
         myfile.write(data)
   def getText(self):
      return self.data
   def __str__(self):
      return self.getText()
class Tape:
   '''Has all the info on the tape'''
@@ -102,11 +127,8 @@
      self.path=path
      self.filename=filename
      self.fullname=os.path.join(path,filename)
      self.read()
      self.fileOK=self.read()
      return
   def __str__(self):
      return(str(self.fullname))
   def exists(self):
      if(os.path.isfile(self.fullname)):
@@ -141,14 +163,20 @@
                     n2=fields[1]
                  i=i+1
         except:
            print("Cannot read statistics file in "+self.fullname+"\n")
            #print("Cannot read statistics file in "+self.fullname+"\n")
            return(False)
      else:
         print("File "+self.fullname+" does not exists.\n")
         #print("File "+self.fullname+" does not exists.\n")
         return(False)
      self.dT=(int(epoch2)-int(epoch1))/(int(n2)-int(n1))
      self.last=n2
      self.startDate=epoch1
      return(True)
   def __str__(self):
      return(str(self.fullname))
class Runner:
@@ -181,18 +209,26 @@
      self.tape=Tape()
      self.tape.setTape(tapetxt.text)
   def getStatus(self):
   def getPID(self):
      self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
      self.Dir.makeifnotexist()
#      self.Dir.goto()
      try:
         fp = open(os.path.join(self.Dir.fullpath(),'.lock'))
      except IOError as e:
         return 0 #file probably does not exist. e==2??
      pid=fp.readline();
      fp.close();
      pid=fp.readline()
      fp.close()
      return pid
   def getStatus(self):
      pid=self.getPID()
      if(pid==0):
         return 0
      if(psutil.pid_exists(int(pid))):
         return 1
         if psutil.Process(int(pid)).name=="trisurf":
            return 1
         else:
            return 0
      else:
         return 0
@@ -227,7 +263,25 @@
   def getStatistics(self, statfile="statistics.csv"):
      self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
      self.statistics=Statistics(self.Dir.fullpath(), statfile)
      return
      self.Comment=FileContent(os.path.join(self.Dir.fullpath(),".comment"))
      pid=self.getPID();
      if(self.getStatus()):
         statustxt="Running"
      else:
         statustxt="Stopped"
         pid=""
      if(self.statistics.fileOK):
#         report=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(self.statistics.startDate)))+"\t"+str(datetime.timedelta(microseconds=(int(self.tape.config['iterations'])-int(self.statistics.last))*self.statistics.dT)*1000)+" ETA\t"+"STATUS"
         report=[time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(self.statistics.startDate))),str(datetime.timedelta(microseconds=(int(self.tape.config['iterations'])-int(self.statistics.last))*self.statistics.dT)*1000), statustxt, pid, str(self.Dir.fullpath()), self.Comment.getText()]
      else:
         report=["N/A","N/A\t",statustxt, pid, str(self.Dir.fullpath()), self.Comment.getText()]
      return report
   def writeComment(self, data):
      self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
      self.Comment=FileContent(os.path.join(self.Dir.fullpath(),".comment"))
      self.Comment.writefile(data,mode='w')
   def __str__(self):
      if(self.getStatus()==0):