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