Trisurf Monte Carlo simulator
Samo Penic
2016-03-07 0706adbb9bee43e3e5349c3d9fa5f001257b7510
commit | author | age
bc14fb 1 #!/usr/bin/python3
SP 2
3 import configobj
bd6993 4 import xml.etree.ElementTree as ET
SP 5 import base64
6 import zlib
7 import io
c14bd6 8 import os
SP 9 from itertools import islice
10 import mmap
bc14fb 11 '''
SP 12 This is a trisurf instance manager written in python
13
14
15 Invoke with:
16 tsmgr [-t tape | -r snapshot.vtu] [-s subdirectory]
17
18 If tape is specified, the trisurf wilt start from tape with initial distribution, if snapshot is specified the trisurf will be restored from given snapshot file and simulation will continue.
19
20 '''
21
22
23
24 class Tape:
25     '''Has all the info on the tape'''
26
27     def __init__(self):
28         return
29
30     def readTape(self, tape='tape'):
31         try:
32             self.config=configobj.ConfigObj(tape)
33         except:
34             print("Error reading or parsing tape file!\n")
35             exit(1)
36
37     def setTape(self, string):
bd6993 38         self.config=configobj.ConfigObj(io.StringIO(string))
bc14fb 39         return
SP 40
41     def getValue(self,key):
42         return self.config[key]
43
c14bd6 44     def __str__(self):
SP 45         retval=""
46         for key,val in self.config.iteritems():
47             retval=retval+str(key)+" = "+str(val)+"\n"
48         return retval
bc14fb 49
c14bd6 50
SP 51
52 class Directory:
53     def __init__(self, maindir=".", simdir=""):
54         self.maindir=maindir
55         self.simdir=simdir
56         return
57
58     def fullpath(self):
59         return os.path.join(self.maindir,self.simdir)
60
61     def exists(self):
62         path=self.fullpath()
63         if(os.path.exists(path)):
64             return 1
65         else:
66             return 0
67
68     def make(self):
69         try:
70             os.makedirs(self.fullpath())
71         except:
72             print("Cannot make directory "+self.fullpath()+"\n")
73             exit(1)
74         return
75
76     def makeifnotexist(self):
77         if(self.exists()==0):
78             self.make()
79         return
80
81     def remove(self):
82         if(self.exists()):
83             try:
84                 os.rmdir(self.fullpath())
85             except:
86                 print("Cannot remove directory "+self.fullpath()+ "\n")
87                 exit(1)
88         return
89
90     def goto(self):
91         try:
92             os.chdir(self.fullpath())
93         except:
94             print("Cannot go to directory "+self.fullpath()+"\n")
95         return
96
97
98 class Statistics:
99     def __init__(self,path,filename="statistics.csv"):
100         self.path=path
101         self.filename=filename
102         self.fullname=os.path.join(path,filename)
103         self.read()
104         return
105
106     def __str__(self):
107         return(str(self.fullname))
108
109     def exists(self):
110         if(os.path.isfile(self.fullname)):
111             return True
112         else:
113             return False
114
115     def mapcount(self):
116         f = open(self.fullname, "r+")
117         buf = mmap.mmap(f.fileno(), 0)
118         lines = 0
119         readline = buf.readline
120         while readline():
121             lines += 1
122         return lines
123
124     def read(self):
125         if(self.exists()):
126             nlines=self.mapcount()
127             try:
128                 with open(self.fullname, "r+") as fin:
129                     i=0;
130                     for line in fin:
131                         if(i==1):
132                             print (line)
133                         if(i==nlines-1):
134                             print (line)
135                         i=i+1
136             except:
137                 print("Cannot read statistics file in "+self.fullname+"\n")
138                 exit(1)
139         else:
140             print("File "+self.fullname+" does not exists.\n")
141             exit(1)
bc14fb 142
SP 143 class Runner:
144     '''
145     Class Runner consists of a single running or terminated instance of the trisurf
146     '''
c14bd6 147     def __init__(self, subdir='run0', tape='', snapshot=''):
SP 148         self.subdir=subdir
149         if(tape!=''):
150             self.initFromTape(tape)
151         if(snapshot!=''):
152             self.initFromSnapshot(snapshot)
153         return
154
155
a99f2b 156     def initFromTape(self, tape):
bc14fb 157         self.tape=Tape()
SP 158         self.tape.readTape(tape)
159
bd6993 160     def initFromSnapshot(self, snapshotfile):
SP 161         try:
162             tree = ET.parse(snapshotfile)
163         except:
164             print("Error reading snapshot file")
165             exit(1)
166
167         root = tree.getroot()
168         tapetxt=root.find('tape')
169         version=root.find('trisurfversion')
170         self.tape=Tape()
171         self.tape.setTape(tapetxt.text)
bc14fb 172
SP 173     def getStatus(self):
c14bd6 174         return 0
bc14fb 175
SP 176     def start(self):
c14bd6 177         if(self.getStatus()==0):
SP 178             self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
179             self.Dir.makeifnotexist()
180             self.Dir.goto()
181             print("Starting trisurf-ng executable at "+self.Dir.fullpath()+"\n")
182         else:
183             print("Process already running. Not starting\n")
184         return
bc14fb 185
SP 186     def stop(self):
187         pass
188
c14bd6 189     def setMaindir(self,prefix,variables):
SP 190         maindir="./"
191         for p,v in zip(prefix,variables):
192             if(v=="xk0"):
193                 tv=str(round(float(self.tape.config[v])))
194             else:
195                 tv=self.tape.config[v]
196             maindir=maindir+p+tv
197         self.maindir=maindir
198         return
199
200     def setSubdir(self, subdir="run0"):
201         self.subdir=subdir
202         return
203
204     def getStatistics(self, statfile="statistics.csv"):
205         self.statistics=Statistics("", statfile) # we are already in the running directory, so local path is needed!
206         return
207
bc14fb 208     def __str__(self):
c14bd6 209         if(self.getStatus()==0):
SP 210             str=" not running."
211         else:
212             str=" running."
213         return(self.Dir.fullpath()+str)
214
bc14fb 215