From c14bd6e408bec0634a7cac3aa87e177562430afe Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@fe.uni-lj.si>
Date: Mon, 07 Mar 2016 14:04:32 +0000
Subject: [PATCH] Working on script for executing trisurf. Added directory generation, started working on start code. Started reading statistics CSV file for generating report on running simulation

---
 python/trisurf/trisurf.py                         |  160 +++++++++++++++++++++++++++++++++++----
 python/trisurf/__pycache__/trisurf.cpython-34.pyc |    0 
 python/tsmgr                                      |    9 +
 aclocal.m4                                        |   65 +---------------
 4 files changed, 155 insertions(+), 79 deletions(-)

diff --git a/aclocal.m4 b/aclocal.m4
index 7657505..da47901 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -180,61 +180,6 @@
 fi[]dnl
 ])# PKG_CHECK_MODULES
 
-
-# PKG_INSTALLDIR(DIRECTORY)
-# -------------------------
-# Substitutes the variable pkgconfigdir as the location where a module
-# should install pkg-config .pc files. By default the directory is
-# $libdir/pkgconfig, but the default can be changed by passing
-# DIRECTORY. The user can override through the --with-pkgconfigdir
-# parameter.
-AC_DEFUN([PKG_INSTALLDIR],
-[m4_pushdef([pkg_default], [m4_default([$1], ['${libdir}/pkgconfig'])])
-m4_pushdef([pkg_description],
-    [pkg-config installation directory @<:@]pkg_default[@:>@])
-AC_ARG_WITH([pkgconfigdir],
-    [AS_HELP_STRING([--with-pkgconfigdir], pkg_description)],,
-    [with_pkgconfigdir=]pkg_default)
-AC_SUBST([pkgconfigdir], [$with_pkgconfigdir])
-m4_popdef([pkg_default])
-m4_popdef([pkg_description])
-]) dnl PKG_INSTALLDIR
-
-
-# PKG_NOARCH_INSTALLDIR(DIRECTORY)
-# -------------------------
-# Substitutes the variable noarch_pkgconfigdir as the location where a
-# module should install arch-independent pkg-config .pc files. By
-# default the directory is $datadir/pkgconfig, but the default can be
-# changed by passing DIRECTORY. The user can override through the
-# --with-noarch-pkgconfigdir parameter.
-AC_DEFUN([PKG_NOARCH_INSTALLDIR],
-[m4_pushdef([pkg_default], [m4_default([$1], ['${datadir}/pkgconfig'])])
-m4_pushdef([pkg_description],
-    [pkg-config arch-independent installation directory @<:@]pkg_default[@:>@])
-AC_ARG_WITH([noarch-pkgconfigdir],
-    [AS_HELP_STRING([--with-noarch-pkgconfigdir], pkg_description)],,
-    [with_noarch_pkgconfigdir=]pkg_default)
-AC_SUBST([noarch_pkgconfigdir], [$with_noarch_pkgconfigdir])
-m4_popdef([pkg_default])
-m4_popdef([pkg_description])
-]) dnl PKG_NOARCH_INSTALLDIR
-
-
-# PKG_CHECK_VAR(VARIABLE, MODULE, CONFIG-VARIABLE,
-# [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
-# -------------------------------------------
-# Retrieves the value of the pkg-config variable for the given module.
-AC_DEFUN([PKG_CHECK_VAR],
-[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
-AC_ARG_VAR([$1], [value of $3 for $2, overriding pkg-config])dnl
-
-_PKG_CONFIG([$1], [variable="][$3]["], [$2])
-AS_VAR_COPY([$1], [pkg_cv_][$1])
-
-AS_VAR_IF([$1], [""], [$5], [$4])dnl
-])# PKG_CHECK_VAR
-
 # Copyright (C) 2002-2013 Free Software Foundation, Inc.
 #
 # This file is free software; the Free Software Foundation
@@ -318,9 +263,10 @@
 # configured tree to be moved without reconfiguration.
 
 AC_DEFUN([AM_AUX_DIR_EXPAND],
-[AC_REQUIRE([AC_CONFIG_AUX_DIR_DEFAULT])dnl
-# Expand $ac_aux_dir to an absolute path.
-am_aux_dir=`cd "$ac_aux_dir" && pwd`
+[dnl Rely on autoconf to set up CDPATH properly.
+AC_PREREQ([2.50])dnl
+# expand $ac_aux_dir to an absolute path
+am_aux_dir=`cd $ac_aux_dir && pwd`
 ])
 
 # AM_CONDITIONAL                                            -*- Autoconf -*-
@@ -787,8 +733,7 @@
 END
     AC_MSG_ERROR([Your 'rm' program is bad, sorry.])
   fi
-fi
-])
+fi])
 
 dnl Hook into '_AC_COMPILER_EXEEXT' early to learn its expansion.  Do not
 dnl add the conditional right here, as _AC_COMPILER_EXEEXT may be further
diff --git a/python/trisurf/__pycache__/trisurf.cpython-34.pyc b/python/trisurf/__pycache__/trisurf.cpython-34.pyc
index 310179a..456b1ab 100644
--- a/python/trisurf/__pycache__/trisurf.cpython-34.pyc
+++ b/python/trisurf/__pycache__/trisurf.cpython-34.pyc
Binary files differ
diff --git a/python/trisurf/trisurf.py b/python/trisurf/trisurf.py
index d777610..67a0813 100644
--- a/python/trisurf/trisurf.py
+++ b/python/trisurf/trisurf.py
@@ -5,8 +5,9 @@
 import base64
 import zlib
 import io
-
-
+import os
+from itertools import islice
+import mmap
 '''
 This is a trisurf instance manager written in python
 
@@ -32,7 +33,6 @@
 		except:
 			print("Error reading or parsing tape file!\n")
 			exit(1)
-	
 
 	def setTape(self, string):
 		self.config=configobj.ConfigObj(io.StringIO(string))
@@ -41,12 +41,118 @@
 	def getValue(self,key):
 		return self.config[key]
 
+	def __str__(self):
+		retval=""
+		for key,val in self.config.iteritems():
+			retval=retval+str(key)+" = "+str(val)+"\n"
+		return retval
 
+
+
+class Directory:
+	def __init__(self, maindir=".", simdir=""):
+		self.maindir=maindir
+		self.simdir=simdir
+		return
+
+	def fullpath(self):
+		return os.path.join(self.maindir,self.simdir)
+
+	def exists(self):
+		path=self.fullpath()
+		if(os.path.exists(path)):
+			return 1
+		else:
+			return 0
+
+	def make(self):
+		try:
+			os.makedirs(self.fullpath())
+		except:
+			print("Cannot make directory "+self.fullpath()+"\n")
+			exit(1)
+		return
+
+	def makeifnotexist(self):
+		if(self.exists()==0):
+			self.make()
+		return
+
+	def remove(self):
+		if(self.exists()):
+			try:
+				os.rmdir(self.fullpath())
+			except:
+				print("Cannot remove directory "+self.fullpath()+ "\n")
+				exit(1)
+		return
+
+	def goto(self):
+		try:
+			os.chdir(self.fullpath())
+		except:
+			print("Cannot go to directory "+self.fullpath()+"\n")
+		return
+
+
+class Statistics:
+	def __init__(self,path,filename="statistics.csv"):
+		self.path=path
+		self.filename=filename
+		self.fullname=os.path.join(path,filename)
+		self.read()
+		return
+
+	def __str__(self):
+		return(str(self.fullname))
+
+	def exists(self):
+		if(os.path.isfile(self.fullname)):
+			return True
+		else:
+			return False
+
+	def mapcount(self):
+		f = open(self.fullname, "r+")
+		buf = mmap.mmap(f.fileno(), 0)
+		lines = 0
+		readline = buf.readline
+		while readline():
+			lines += 1
+		return lines
+
+	def read(self):
+		if(self.exists()):
+			nlines=self.mapcount()
+			try:
+				with open(self.fullname, "r+") as fin:
+					i=0;
+					for line in fin:
+						if(i==1):
+							print (line)
+						if(i==nlines-1):
+							print (line)
+						i=i+1
+			except:
+				print("Cannot read statistics file in "+self.fullname+"\n")
+				exit(1)
+		else:
+			print("File "+self.fullname+" does not exists.\n")
+			exit(1)
 
 class Runner:
 	'''
 	Class Runner consists of a single running or terminated instance of the trisurf
 	'''
+	def __init__(self, subdir='run0', tape='', snapshot=''):
+		self.subdir=subdir
+		if(tape!=''):
+			self.initFromTape(tape)
+		if(snapshot!=''):
+			self.initFromSnapshot(snapshot)
+		return
+
+
 	def initFromTape(self, tape):
 		self.tape=Tape()
 		self.tape.readTape(tape)
@@ -61,29 +167,49 @@
 		root = tree.getroot()
 		tapetxt=root.find('tape')
 		version=root.find('trisurfversion')
-		#print("Reading snapshot made from: "+version.text)
 		self.tape=Tape()
-		#print(tapetxt.text)
 		self.tape.setTape(tapetxt.text)
 
-	def __init__(self, subdir='run0', tape='', snapshot=''):
-		self.subdir=subdir
-		if(tape!=''):
-			self.initFromTape(tape)
-		if(snapshot!=''):
-			self.initFromSnapshot(snapshot)
-
-		return
-
 	def getStatus(self):
-		pass
+		return 0
 
 	def start(self):
-		pass
+		if(self.getStatus()==0):
+			self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
+			self.Dir.makeifnotexist()
+			self.Dir.goto()
+			print("Starting trisurf-ng executable at "+self.Dir.fullpath()+"\n")
+		else:
+			print("Process already running. Not starting\n")
+		return
 
 	def stop(self):
 		pass
 
+	def setMaindir(self,prefix,variables):
+		maindir="./"
+		for p,v in zip(prefix,variables):
+			if(v=="xk0"):
+				tv=str(round(float(self.tape.config[v])))
+			else:
+				tv=self.tape.config[v]
+			maindir=maindir+p+tv
+		self.maindir=maindir
+		return
+
+	def setSubdir(self, subdir="run0"):
+		self.subdir=subdir
+		return
+
+	def getStatistics(self, statfile="statistics.csv"):
+		self.statistics=Statistics("", statfile) # we are already in the running directory, so local path is needed!
+		return
+
 	def __str__(self):
-		return("Running instance")
+		if(self.getStatus()==0):
+			str=" not running."
+		else:
+			str=" running."
+		return(self.Dir.fullpath()+str)
+
 
diff --git a/python/tsmgr b/python/tsmgr
index d69312c..34c3644 100755
--- a/python/tsmgr
+++ b/python/tsmgr
@@ -5,6 +5,11 @@
 
 
 run1=trisurf.Runner(snapshot='snapshot.vtu')
+run1.setMaindir(("N","k","V","Np","Nm"),("nshell","xk0","constvolswitch","npoly","nmono"))
+run1.start()
+#run
 print(run1)
-#print(run1.tape.config)
-print(run1.tape.getValue('nshell'))
+run1.getStatistics()
+print(run1.statistics)
+#print(run1.tape)
+#print(run1.tape.getValue('nshell'))

--
Gitblit v1.9.3