2011-11-17 19:35:39 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
import os, sys
|
|
|
|
import subprocess,shutil
|
|
|
|
|
2011-11-24 00:02:22 +05:30
|
|
|
import damask_tools
|
|
|
|
import msc_tools
|
2011-11-17 19:35:39 +05:30
|
|
|
|
|
|
|
damask_tools.DAMASK_TOOLS().check_env()
|
|
|
|
|
|
|
|
class DAMASK_TEST():
|
2011-11-24 00:02:22 +05:30
|
|
|
'''
|
|
|
|
General class for testing.
|
|
|
|
Is sub-classed by the individual tests.
|
|
|
|
'''
|
2011-12-09 20:43:27 +05:30
|
|
|
#define those according to your test
|
2011-11-17 19:35:39 +05:30
|
|
|
modelname=None
|
|
|
|
jobname=None
|
2011-12-09 20:43:27 +05:30
|
|
|
test_dir=None
|
2011-11-17 19:35:39 +05:30
|
|
|
spectral_options=None
|
2011-12-09 20:43:27 +05:30
|
|
|
compile=False = None
|
|
|
|
post_txt = None
|
|
|
|
tol = 0.0
|
2011-11-24 00:02:22 +05:30
|
|
|
|
|
|
|
has_variants=False # False ==> A single test case is run
|
|
|
|
#has_variants=True # True ==> Need to define the test_variants generator
|
2011-11-17 19:35:39 +05:30
|
|
|
|
2011-11-24 00:02:22 +05:30
|
|
|
def test_variants(self):
|
|
|
|
'''
|
2011-12-09 20:43:27 +05:30
|
|
|
If has_subtests == True this method defines the generator for test variants
|
2011-11-24 00:02:22 +05:30
|
|
|
This generator must be defined in each test,
|
|
|
|
depending on what to change: orientations, parameters,....
|
|
|
|
Below is an EXAMPLE.
|
|
|
|
'''
|
|
|
|
subtest_orientations=[[0.0,90.0,0.0],[0.0,0.0,90.0]]
|
|
|
|
for i,o in enumerate(subtest_orientations):
|
|
|
|
from damask_tools import MATERIAL_CONFIG
|
|
|
|
mat=MATERIAL_CONFIG()
|
|
|
|
mat.read('material.config_base')
|
|
|
|
mat.add_texture(label='Grain001',
|
|
|
|
type ='(gauss)',
|
|
|
|
eulers = o)
|
|
|
|
mat.write(overwrite=True)
|
|
|
|
print(mat.data['texture']['Grain001'])
|
|
|
|
testlabel='orientation_%03i'%i
|
|
|
|
yield(testlabel)
|
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
res=[]
|
|
|
|
if self.has_variants:
|
2011-12-09 20:43:27 +05:30
|
|
|
for t in self.test_variants():
|
|
|
|
print '###############################################'
|
|
|
|
print '###############################################'
|
2011-11-24 00:02:22 +05:30
|
|
|
print(t)
|
2011-12-09 20:43:27 +05:30
|
|
|
print '###############################################'
|
|
|
|
val=self.run_single_test(t)
|
2011-11-24 00:02:22 +05:30
|
|
|
res.append(val==True)
|
|
|
|
else:
|
|
|
|
val=self.run_single_test()
|
|
|
|
res.append(val==True)
|
|
|
|
if all(res) is True:
|
|
|
|
return True
|
|
|
|
print(res)
|
|
|
|
return False
|
|
|
|
|
2011-12-09 20:43:27 +05:30
|
|
|
def run_single_test(self,variant):
|
2011-11-17 19:35:39 +05:30
|
|
|
self.clean_current_results()
|
2011-12-09 20:43:27 +05:30
|
|
|
if self.calc_current_results(variant) is False:
|
2011-11-17 19:35:39 +05:30
|
|
|
return False
|
|
|
|
print('simulation finished')
|
|
|
|
self.postprocess()
|
|
|
|
if self.compare_to_reference() is False:
|
2011-12-09 20:43:27 +05:30
|
|
|
print '++++++++ Test not OK +++++++++'
|
2011-11-17 19:35:39 +05:30
|
|
|
return False
|
|
|
|
print 'Test OK'
|
|
|
|
return True
|
|
|
|
|
|
|
|
def clean_current_results(self):
|
2011-12-09 20:43:27 +05:30
|
|
|
os.chdir(self.test_dir)
|
2011-11-17 19:35:39 +05:30
|
|
|
try:
|
|
|
|
shutil.rmtree('current_results')
|
|
|
|
except:
|
|
|
|
print('Could not delete current_results')
|
|
|
|
os.mkdir('current_results')
|
2011-11-24 00:02:22 +05:30
|
|
|
|
|
|
|
def calc_current_results(self):
|
|
|
|
'''
|
|
|
|
Should be defined in the individual tests
|
|
|
|
'''
|
|
|
|
pass
|
2011-11-17 19:35:39 +05:30
|
|
|
|
2011-11-24 00:02:22 +05:30
|
|
|
def calc_marc(self,compile=None):
|
|
|
|
'''
|
|
|
|
Calculates current results for MSC.Marc
|
|
|
|
'''
|
2011-11-17 19:35:39 +05:30
|
|
|
if compile is None: compile=self.compile
|
|
|
|
self.copy_from_ref=[self.modelname+'_'+self.jobname+'.dat',
|
|
|
|
self.modelname+'.mfd', # for dev
|
|
|
|
'material.config'
|
|
|
|
]
|
2011-11-24 00:02:22 +05:30
|
|
|
self.copy_files_from_reference_results()
|
|
|
|
os.chdir('./current_results')
|
2011-11-17 19:35:39 +05:30
|
|
|
m=msc_tools.MSC_TOOLS()
|
|
|
|
m.submit_job(compile=compile, compiled_dir='../../../code/')
|
|
|
|
print('simulation submitted')
|
|
|
|
self.exit_number=m.exit_number_from_outFile(outFile=self.modelname+'_'+self.jobname+'.out')
|
|
|
|
|
|
|
|
if not self.exit_number==3004:
|
|
|
|
print('Job did not exit with No. 3004')
|
|
|
|
return False
|
2011-11-24 00:02:22 +05:30
|
|
|
return True
|
|
|
|
|
|
|
|
def calc_spectral(self, compile=None):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def copy_files_from_reference_results(self):
|
|
|
|
for file in self.copy_from_ref:
|
|
|
|
shutil.copy2('./reference_results/%s'%file,'./current_results/%s'%file)
|
|
|
|
# Note: possibly symlinking? No, because copy is OS independent.
|
2011-11-17 19:35:39 +05:30
|
|
|
|
2011-12-09 20:43:27 +05:30
|
|
|
def read_val_from_file(self,fname=None):
|
|
|
|
file = open(fname,'r')
|
|
|
|
headerlength = int(file.readline().split()[0]) + 1
|
|
|
|
file.close
|
|
|
|
import numpy as N
|
|
|
|
val = N.loadtxt(fname,skiprows=headerlength)
|
|
|
|
return val
|
2011-11-18 02:49:08 +05:30
|
|
|
|
2011-12-09 20:43:27 +05:30
|
|
|
def compare_to_reference(self):
|
2011-11-17 19:35:39 +05:30
|
|
|
import string
|
2011-12-09 20:43:27 +05:30
|
|
|
print 'comparing results against reference_results...'
|
|
|
|
os.chdir(os.path.join(self.test_dir,'current_results'))
|
|
|
|
cur=self.read_val_from_file(fname='postProc/'+self.post_txt)
|
|
|
|
ref=self.read_val_from_file(fname='../reference_results/postProc/'+self.post_txt)
|
2011-11-17 19:35:39 +05:30
|
|
|
|
|
|
|
err=abs((ref/cur)-1.) # relative tolerance
|
|
|
|
#err=abs(ref-cur) # absolute tolerance
|
2011-12-09 20:43:27 +05:30
|
|
|
print 'tol', self.tol
|
|
|
|
if err.any()>self.tol:
|
|
|
|
return False
|
2011-11-17 19:35:39 +05:30
|
|
|
return True
|
|
|
|
|
2011-12-09 20:43:27 +05:30
|
|
|
def postprocess(self):
|
|
|
|
print 'postprocessing results ...'
|
|
|
|
os.chdir(self.test_dir)
|
|
|
|
file=open('./postprocessing.cmd','r')
|
|
|
|
postproc=file.readlines()
|
|
|
|
file.close()
|
|
|
|
os.chdir(os.path.join(self.test_dir,'current_results'))
|
|
|
|
for cmd in postproc: # PHILIP: suggestion to just execute the script "postprocessing" directly within a shell, i.e. os.system('../postprocessing')
|
|
|
|
print(cmd)
|
|
|
|
os.system(cmd) # PHILIP: reason is that for loops and the like get broken with line by line execution from here...
|
|
|
|
|
2011-11-17 19:35:39 +05:30
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test=DAMASK_TESTER()
|
|
|
|
test.run_test()
|
2011-11-18 02:49:08 +05:30
|
|
|
|