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 |
7c45b1
|
11 |
import shlex |
34600b
|
12 |
import psutil |
bc14fb
|
13 |
''' |
SP |
14 |
This is a trisurf instance manager written in python |
|
15 |
|
|
16 |
|
|
17 |
Invoke with: |
|
18 |
tsmgr [-t tape | -r snapshot.vtu] [-s subdirectory] |
|
19 |
|
|
20 |
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. |
|
21 |
|
|
22 |
''' |
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
class Tape: |
|
27 |
'''Has all the info on the tape''' |
|
28 |
|
|
29 |
def __init__(self): |
|
30 |
return |
|
31 |
|
|
32 |
def readTape(self, tape='tape'): |
|
33 |
try: |
|
34 |
self.config=configobj.ConfigObj(tape) |
|
35 |
except: |
|
36 |
print("Error reading or parsing tape file!\n") |
|
37 |
exit(1) |
|
38 |
|
|
39 |
def setTape(self, string): |
bd6993
|
40 |
self.config=configobj.ConfigObj(io.StringIO(string)) |
bc14fb
|
41 |
return |
SP |
42 |
|
|
43 |
def getValue(self,key): |
|
44 |
return self.config[key] |
|
45 |
|
c14bd6
|
46 |
def __str__(self): |
SP |
47 |
retval="" |
|
48 |
for key,val in self.config.iteritems(): |
|
49 |
retval=retval+str(key)+" = "+str(val)+"\n" |
|
50 |
return retval |
bc14fb
|
51 |
|
c14bd6
|
52 |
|
SP |
53 |
|
|
54 |
class Directory: |
|
55 |
def __init__(self, maindir=".", simdir=""): |
|
56 |
self.maindir=maindir |
|
57 |
self.simdir=simdir |
|
58 |
return |
|
59 |
|
|
60 |
def fullpath(self): |
|
61 |
return os.path.join(self.maindir,self.simdir) |
|
62 |
|
|
63 |
def exists(self): |
|
64 |
path=self.fullpath() |
|
65 |
if(os.path.exists(path)): |
|
66 |
return 1 |
|
67 |
else: |
|
68 |
return 0 |
|
69 |
|
|
70 |
def make(self): |
|
71 |
try: |
|
72 |
os.makedirs(self.fullpath()) |
|
73 |
except: |
|
74 |
print("Cannot make directory "+self.fullpath()+"\n") |
|
75 |
exit(1) |
|
76 |
return |
|
77 |
|
|
78 |
def makeifnotexist(self): |
|
79 |
if(self.exists()==0): |
|
80 |
self.make() |
|
81 |
return |
|
82 |
|
|
83 |
def remove(self): |
|
84 |
if(self.exists()): |
|
85 |
try: |
|
86 |
os.rmdir(self.fullpath()) |
|
87 |
except: |
|
88 |
print("Cannot remove directory "+self.fullpath()+ "\n") |
|
89 |
exit(1) |
|
90 |
return |
|
91 |
|
|
92 |
def goto(self): |
|
93 |
try: |
|
94 |
os.chdir(self.fullpath()) |
|
95 |
except: |
|
96 |
print("Cannot go to directory "+self.fullpath()+"\n") |
|
97 |
return |
|
98 |
|
|
99 |
|
|
100 |
class Statistics: |
|
101 |
def __init__(self,path,filename="statistics.csv"): |
|
102 |
self.path=path |
|
103 |
self.filename=filename |
|
104 |
self.fullname=os.path.join(path,filename) |
|
105 |
self.read() |
|
106 |
return |
|
107 |
|
|
108 |
def __str__(self): |
|
109 |
return(str(self.fullname)) |
|
110 |
|
|
111 |
def exists(self): |
|
112 |
if(os.path.isfile(self.fullname)): |
|
113 |
return True |
|
114 |
else: |
|
115 |
return False |
|
116 |
|
|
117 |
def mapcount(self): |
|
118 |
f = open(self.fullname, "r+") |
|
119 |
buf = mmap.mmap(f.fileno(), 0) |
|
120 |
lines = 0 |
|
121 |
readline = buf.readline |
|
122 |
while readline(): |
|
123 |
lines += 1 |
|
124 |
return lines |
|
125 |
|
|
126 |
def read(self): |
|
127 |
if(self.exists()): |
|
128 |
nlines=self.mapcount() |
|
129 |
try: |
|
130 |
with open(self.fullname, "r+") as fin: |
|
131 |
i=0; |
|
132 |
for line in fin: |
|
133 |
if(i==1): |
7c45b1
|
134 |
#print (line) |
SP |
135 |
fields=shlex.split(line) |
|
136 |
epoch1=fields[0] |
|
137 |
n1=fields[1] |
c14bd6
|
138 |
if(i==nlines-1): |
7c45b1
|
139 |
fields=shlex.split(line) |
SP |
140 |
epoch2=fields[0] |
|
141 |
n2=fields[1] |
c14bd6
|
142 |
i=i+1 |
SP |
143 |
except: |
|
144 |
print("Cannot read statistics file in "+self.fullname+"\n") |
7c45b1
|
145 |
return(False) |
c14bd6
|
146 |
else: |
SP |
147 |
print("File "+self.fullname+" does not exists.\n") |
7c45b1
|
148 |
return(False) |
SP |
149 |
|
|
150 |
self.dT=(int(epoch2)-int(epoch1))/(int(n2)-int(n1)) |
|
151 |
return(True) |
|
152 |
|
bc14fb
|
153 |
|
SP |
154 |
class Runner: |
|
155 |
''' |
|
156 |
Class Runner consists of a single running or terminated instance of the trisurf |
|
157 |
''' |
c14bd6
|
158 |
def __init__(self, subdir='run0', tape='', snapshot=''): |
SP |
159 |
self.subdir=subdir |
|
160 |
if(tape!=''): |
|
161 |
self.initFromTape(tape) |
|
162 |
if(snapshot!=''): |
|
163 |
self.initFromSnapshot(snapshot) |
|
164 |
return |
|
165 |
|
|
166 |
|
a99f2b
|
167 |
def initFromTape(self, tape): |
bc14fb
|
168 |
self.tape=Tape() |
SP |
169 |
self.tape.readTape(tape) |
|
170 |
|
bd6993
|
171 |
def initFromSnapshot(self, snapshotfile): |
SP |
172 |
try: |
|
173 |
tree = ET.parse(snapshotfile) |
|
174 |
except: |
|
175 |
print("Error reading snapshot file") |
|
176 |
exit(1) |
|
177 |
|
|
178 |
root = tree.getroot() |
|
179 |
tapetxt=root.find('tape') |
|
180 |
version=root.find('trisurfversion') |
|
181 |
self.tape=Tape() |
|
182 |
self.tape.setTape(tapetxt.text) |
bc14fb
|
183 |
|
SP |
184 |
def getStatus(self): |
34600b
|
185 |
self.Dir=Directory(maindir=self.maindir,simdir=self.subdir) |
SP |
186 |
self.Dir.makeifnotexist() |
|
187 |
# self.Dir.goto() |
|
188 |
try: |
|
189 |
fp = open(os.path.join(self.Dir.fullpath(),'.lock')) |
|
190 |
except IOError as e: |
|
191 |
return 0 #file probably does not exist. e==2?? |
|
192 |
pid=fp.readline(); |
|
193 |
fp.close(); |
|
194 |
if(psutil.pid_exists(int(pid))): |
|
195 |
return 1 |
|
196 |
else: |
|
197 |
return 0 |
bc14fb
|
198 |
|
SP |
199 |
def start(self): |
c14bd6
|
200 |
if(self.getStatus()==0): |
SP |
201 |
self.Dir=Directory(maindir=self.maindir,simdir=self.subdir) |
|
202 |
self.Dir.makeifnotexist() |
34600b
|
203 |
# self.Dir.goto() |
c14bd6
|
204 |
print("Starting trisurf-ng executable at "+self.Dir.fullpath()+"\n") |
SP |
205 |
else: |
|
206 |
print("Process already running. Not starting\n") |
|
207 |
return |
bc14fb
|
208 |
|
SP |
209 |
def stop(self): |
|
210 |
pass |
|
211 |
|
c14bd6
|
212 |
def setMaindir(self,prefix,variables): |
SP |
213 |
maindir="./" |
|
214 |
for p,v in zip(prefix,variables): |
|
215 |
if(v=="xk0"): |
|
216 |
tv=str(round(float(self.tape.config[v]))) |
|
217 |
else: |
|
218 |
tv=self.tape.config[v] |
|
219 |
maindir=maindir+p+tv |
|
220 |
self.maindir=maindir |
|
221 |
return |
|
222 |
|
|
223 |
def setSubdir(self, subdir="run0"): |
|
224 |
self.subdir=subdir |
|
225 |
return |
|
226 |
|
|
227 |
def getStatistics(self, statfile="statistics.csv"): |
34600b
|
228 |
self.statistics=Statistics(self.Dir.fullpath(), statfile) |
c14bd6
|
229 |
return |
SP |
230 |
|
bc14fb
|
231 |
def __str__(self): |
c14bd6
|
232 |
if(self.getStatus()==0): |
SP |
233 |
str=" not running." |
|
234 |
else: |
|
235 |
str=" running." |
|
236 |
return(self.Dir.fullpath()+str) |
|
237 |
|
bc14fb
|
238 |
|