2011-12-15 20:23:10 +05:30
|
|
|
#!/usr/bin/env python
|
2011-12-22 16:00:25 +05:30
|
|
|
# $Id$
|
|
|
|
|
2011-12-15 20:23:10 +05:30
|
|
|
import os, sys
|
2012-02-16 02:24:14 +05:30
|
|
|
import subprocess,shutil,string
|
2011-12-15 20:23:10 +05:30
|
|
|
import damask
|
2012-02-16 02:24:14 +05:30
|
|
|
from optparse import OptionParser
|
2011-12-15 20:23:10 +05:30
|
|
|
|
|
|
|
class Test():
|
2012-01-18 15:04:49 +05:30
|
|
|
'''
|
|
|
|
General class for testing.
|
|
|
|
Is sub-classed by the individual tests.
|
|
|
|
'''
|
|
|
|
|
|
|
|
variants = []
|
|
|
|
|
2012-02-16 02:24:14 +05:30
|
|
|
def __init__(self,test_description):
|
|
|
|
print '\n'+test_description
|
2012-01-18 15:04:49 +05:30
|
|
|
self.dirBase = os.path.dirname(os.path.realpath(sys.argv[0]))
|
2012-02-16 02:24:14 +05:30
|
|
|
self.parser = OptionParser(
|
|
|
|
description = 'Using: $Id run_test.py 1285 2012-02-09 08:54:09Z MPIE\m.diehl $',
|
|
|
|
usage='run_test.py [options]')
|
2012-01-18 15:04:49 +05:30
|
|
|
|
2012-02-16 02:24:14 +05:30
|
|
|
self.parser.add_option("-u", "--update", action="store_true",\
|
|
|
|
dest="update",\
|
|
|
|
help="use current test results as new reference")
|
|
|
|
self.parser.set_defaults(update = False)
|
|
|
|
|
|
|
|
(self.options, self.args) = self.parser.parse_args()
|
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
def execute(self,variants = [],update = []):
|
2012-02-09 20:11:18 +05:30
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
'''
|
|
|
|
Run all variants and report first failure.
|
|
|
|
'''
|
|
|
|
if len(variants) == 0: variants = xrange(len(self.variants)) # iterate over all variants
|
|
|
|
self.clean()
|
|
|
|
for variant in variants:
|
2012-01-31 18:40:14 +05:30
|
|
|
try:
|
|
|
|
self.prepare(variant)
|
|
|
|
self.run(variant)
|
|
|
|
self.postprocess(variant)
|
|
|
|
if variant in update:
|
|
|
|
self.update(variant)
|
|
|
|
elif not self.compare(variant):
|
|
|
|
return variant
|
|
|
|
except:
|
2012-01-26 20:56:21 +05:30
|
|
|
return variant
|
2012-01-18 15:04:49 +05:30
|
|
|
return -1
|
2012-02-09 20:11:18 +05:30
|
|
|
# for self.current_variant_index,variant in enumerate(variants):
|
2012-01-31 18:40:14 +05:30
|
|
|
# self.prepare(variant)
|
|
|
|
# self.run(variant)
|
|
|
|
# self.postprocess(variant)
|
2012-02-09 20:11:18 +05:30
|
|
|
# if self.current_variant_index in update:
|
2012-01-31 18:40:14 +05:30
|
|
|
# self.update(variant)
|
|
|
|
# elif not self.compare(variant):
|
|
|
|
# return variant
|
|
|
|
# return -1
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
|
|
|
|
def clean(self):
|
2011-12-15 20:23:10 +05:30
|
|
|
'''
|
2012-01-18 15:04:49 +05:30
|
|
|
Delete directory tree containing current results.
|
|
|
|
'''
|
|
|
|
status = True
|
|
|
|
|
|
|
|
try:
|
|
|
|
shutil.rmtree(self.dirCurrent())
|
|
|
|
except:
|
|
|
|
print('removal of directory "%s" failed...'%(self.dirCurrent()))
|
|
|
|
status = status and False
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.mkdir(self.dirCurrent())
|
|
|
|
except:
|
|
|
|
print('creation of directory "%s" failed...'%(self.dirCurrent()))
|
|
|
|
status = status and False
|
|
|
|
|
|
|
|
return status
|
|
|
|
|
|
|
|
|
|
|
|
def prepare(self,variant):
|
|
|
|
'''
|
|
|
|
Do all necessary preparations for the run of each test variant
|
|
|
|
'''
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def run(self,variant):
|
|
|
|
'''
|
|
|
|
Execute the requested test variant.
|
|
|
|
'''
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def postprocess(self,variant):
|
|
|
|
'''
|
|
|
|
Perform post-processing of generated results for this test variant.
|
|
|
|
'''
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def compare(self,variant):
|
|
|
|
'''
|
|
|
|
Compare reference to current results.
|
|
|
|
'''
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def update(self,variant):
|
|
|
|
'''
|
|
|
|
Update reference with current results.
|
|
|
|
'''
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def dirReference(self):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
return os.path.normpath(os.path.join(self.dirBase,'reference/'))
|
|
|
|
|
|
|
|
def dirCurrent(self):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
return os.path.normpath(os.path.join(self.dirBase,'current/'))
|
|
|
|
|
|
|
|
def fileInReference(self,file):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
return os.path.join(self.dirReference(),file)
|
|
|
|
|
|
|
|
def fileInCurrent(self,file):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
return os.path.join(self.dirCurrent(),file)
|
|
|
|
|
|
|
|
def copy_Reference2Current(self,files=[]):
|
|
|
|
for file in files:
|
2012-02-23 23:14:09 +05:30
|
|
|
try:
|
|
|
|
shutil.copy2(self.fileInReference(file),self.fileInCurrent(file))
|
|
|
|
except:
|
2012-02-24 20:04:44 +05:30
|
|
|
print 'Reference2Current: Unable to copy file ', file
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
def copy_Current2Current(self,files=[]):
|
|
|
|
for file in files:
|
2012-02-23 23:14:09 +05:30
|
|
|
try:
|
|
|
|
shutil.copy2(self.fileInCurrent(file[0]),self.fileInCurrent(file[1]))
|
|
|
|
except:
|
2012-02-24 20:04:44 +05:30
|
|
|
print 'Current2Current: Unable to copy file ', file
|
2012-01-18 15:04:49 +05:30
|
|
|
|
2012-01-27 15:25:19 +05:30
|
|
|
def execute_inCurrentDir(self,cmd):
|
|
|
|
os.chdir(self.dirCurrent())
|
|
|
|
print cmd
|
|
|
|
os.system(cmd)
|
|
|
|
|
|
|
|
def compare_Array(self,ref,cur):
|
|
|
|
import numpy
|
|
|
|
refName = self.fileInReference (ref)
|
|
|
|
curName = self.fileInCurrent(cur)
|
|
|
|
|
|
|
|
refFile = open(refName)
|
|
|
|
table = damask.ASCIItable(refFile)
|
|
|
|
table.head_read()
|
|
|
|
refFile.close()
|
2012-03-09 22:00:42 +05:30
|
|
|
refArray = numpy.genfromtxt(refName,missing_values='n/a',skip_header = len(table.info)+1)
|
|
|
|
curArray = numpy.genfromtxt(curName,missing_values='n/a',skip_header = len(table.info)+1)
|
2012-01-27 15:25:19 +05:30
|
|
|
err = abs((refArray/curArray)-1.) # relative tolerance
|
2012-02-09 20:11:18 +05:30
|
|
|
refNaN=len(numpy.isnan(refArray))
|
|
|
|
curNaN=len(numpy.isnan(curArray))
|
|
|
|
if curNaN == refNaN:
|
|
|
|
err[numpy.isnan(err)]=0.0
|
2012-01-27 15:25:19 +05:30
|
|
|
max_err = numpy.max(err)
|
2012-02-23 23:14:09 +05:30
|
|
|
print ' ********\n * maximum relative error',max_err,'\n ********'
|
2012-01-27 15:25:19 +05:30
|
|
|
return max_err
|
|
|
|
|
2012-01-31 18:40:14 +05:30
|
|
|
def report_Success(self,culprit):
|
2012-01-27 15:25:19 +05:30
|
|
|
if culprit < 0:
|
|
|
|
print '%s passed.'%({False: 'The test',
|
2012-01-31 18:40:14 +05:30
|
|
|
True: 'All %i tests'%(len(self.variants))}[len(self.variants) > 1])
|
2012-01-27 15:25:19 +05:30
|
|
|
else:
|
2012-02-24 20:04:44 +05:30
|
|
|
print ' ********\n * Test %i failed...\n ********'%(culprit+1)
|