Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/lanl/FEHM
Browse files Browse the repository at this point in the history
  • Loading branch information
ehinrichs committed Jun 4, 2024
2 parents 5bfc4b4 + 11a3d04 commit d62425f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 32 deletions.
56 changes: 41 additions & 15 deletions fehmpytests/fehmpytests.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,16 +616,28 @@ def test_case(self, name, parameters={}):

#Search for fehmn control files and extract subcases.
filenames = glob.glob(os.path.join('input','control','*.files'))
#print('filenames: ', filenames)
subcases = []
for filename in filenames:
subcase = re.sub(os.path.join('input','control',''), '', filename)
subcase = re.sub('.files', '', subcase)
#print('\nfilename: ', filename)

path = os.path.join('input', 'control', ' ')
if os.name == 'nt': # For Windows
subcase = re.sub(r'input[\\/]', '', filename)
subcase = re.sub(r'control[\\/]', '', subcase)
subcase = re.sub(r'\.files$', '', subcase)
else: # For Unix-based systems
subcase = re.sub(os.path.join('input','control',''), '', filename)
subcase = re.sub('.files', '', subcase)
#print('subcase: ', subcase)

#File named 'fehmn.files' to be used for tests with single case.
if subcase != 'fehmn':
subcases.append(subcase)
#print('append subases')
else:
subcases = ['']
#print('broken subcases')
break

try:
Expand All @@ -639,25 +651,34 @@ def test_case(self, name, parameters={}):
os.chdir( output_dir )
filetypes = ['*.avs','*.csv','*.his','*.out','*.trc','*.ptrk']
test_flag = False

for filetype in filetypes:
parameters['filetype'] = filetype
#Check to make sure there are files of this type.
if len(glob.glob(os.path.join('..','compare','')+'*'+subcase+filetype)) > 0:
test_method = \
self._test_template(filetype, subcase, parameters)
compare_pattern = (os.path.join('..', 'compare', '*' )+ subcase + filetype)
found_files = glob.glob(compare_pattern)

#print('Checking for files with pattern: ', compare_pattern)
#print('Found files: ', found_files)
#print('Number of found files: ', len(found_files))

if len(found_files) > 0:
test_method = self._test_template(filetype, subcase, parameters)
test_method()
#print('Test method executed for filetype: ', filetype)
test_flag = True
os.chdir( '..' )
else:
#print('Test method NOT executed for filetype: ', filetype)
pass

os.chdir('..')
if not test_flag:
#Write to fail log if switch is on.
if self.log:
line = '\nFailed at subcase:'+subcase
line = line+' filetype:'+filetype
line = f'\nFailed at subcase: {subcase} filetype: {filetype}'
self.fail_log.write(line)
self.fail("Missing any valid comparison files, no test performed")

finally:
#Allows other tests to be performed after exception.
# Allows other tests to be performed after exception.
os.chdir(self.maindir)

def _test_template(self, filetype, subcase, parameters={}):
Expand Down Expand Up @@ -698,7 +719,7 @@ def contour_case():
f_dif = fpost.fdiff(f_new, f_old)

msg = 'Incorrect %s at time %s.'

#If no pre-specified times, grab them from f_dif.
if len(values['times']) == 0:
times = f_dif.times
Expand All @@ -714,6 +735,7 @@ def contour_case():
#Check the variables at each time for any significant differences.
test_flag = False
for t in times:
#print(subcase)
#Its possible some times do not have all variables in f_dif.
for v in np.intersect1d(variables, list(f_dif[t].keys())):
#Measure the difference into a single quantity.
Expand All @@ -729,6 +751,7 @@ def contour_case():
float(len(f_dif[t][v]))
}[test_measure]
try:
# print('true? ', difference, '<', mxerr, '=', difference<mxerr)
self.assertTrue(difference<mxerr, msg%(v, t))
test_flag = True
except AssertionError as e:
Expand All @@ -745,7 +768,7 @@ def contour_case():
self.fail("Missing common nodes in compare and output contour files, no test performed")
def history_case():
#Find the difference between the old and new
f_old = fpost.fhistory(os.path.join('..','compare','*')+subcase+filetype)
f_old = fpost.fhistory(os.path.join('..','compare','*')+subcase+filetype)
f_new = fpost.fhistory('*'+subcase+filetype)
f_dif = fpost.fdiff(f_new, f_old)

Expand Down Expand Up @@ -971,11 +994,13 @@ def _run_fehm(self, subcase):

#Find the control file for the test-case or for the subcase.
if subcase == '':
#print('subcase empty assume fehm at: \n', os.path.join('..','input','control','fehmn.files'))
filesfile = os.path.join('..','input','control','fehmn.files')
else:
filesfile = os.path.join('..','input','control',subcase+'.files')

evalstr = exe+' '+filesfile
#print('evalstr: ', evalstr)

with open(os.devnull, "w") as f:
call(evalstr, shell=True, stdout=f)
Expand Down Expand Up @@ -1008,7 +1033,8 @@ def _run_fehm(self, subcase):
errstr = open( errfile, 'r' ).read()
else:
errstr = ''
# Change to maindir in case assertTrue fails
# Change to maindir in case assertTrue fails
#print('CWD: ', os.getcwd() )
curdir = os.getcwd()
os.chdir(self.maindir)

Expand Down
75 changes: 58 additions & 17 deletions fehmpytests/fpost.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,13 +1026,38 @@ def read(self,filename): # read contents of file
'''
from glob import glob
import re
#print('\n*********************************************\nFilename as imported from fehmpytests fhistory: ', filename)
glob_pattern = re.sub(r'\[','[[]',filename)
glob_pattern = re.sub(r'(?<!\[)\]','[]]', glob_pattern)
files=glob(glob_pattern)
configured=False
for i,fname in enumerate(files):
#print('Names of files: \n', files)
#if self._verbose:
# print(fname)
if '..' in fname:
#print('fname is in compare: ', fname)
if os.name == 'nt': # For Windows
tmp=fname.split('\\')[-1]
else:
tmp = fname.split('/')[-1]
#print('tmp: ', tmp)
if os.path.exists(tmp):
#print('valid comparison of: ', fname, ' and ', tmp)
pass
else:
print('\n **WARNING** The file ', tmp , ' is in compare, but not in output. Skipping file...')
continue
elif '..' not in fname:
#print('fname is in output: ', fname)
path=os.path.join('..', 'compare', '')+fname
#print('PATH', path)
if os.path.exists(path):
#print('valid comparison of: ', fname, ' and ', path)
pass
else:
#print('\n **WARNING** The file ', fname , 'is in output, but doesn''t have a valid compare file. Skipping file...')
continue

with open(fname, 'r') as self._file:
header=self._file.readline()
if header.strip()=='': continue # empty file
Expand All @@ -1050,15 +1075,21 @@ def read(self,filename): # read contents of file
elif self._format=='surf':
self._setup_headers_surf(header)
elif self._format=='default':
#print('headers1', header)
header=self._file.readline()
#print('headers2', header)
header=self._file.readline()
#print('headers3', header)
if header.strip()=='': continue # empty file
i = 0; sum_file = False
while not header.startswith('Time '):
while 'Time' not in header:
header=self._file.readline()
#print('iteratin through i = ',i , header)
i = i+1
if i==10: sum_file=True; break
if sum_file: continue
if sum_file:
#print('final interation' , header)
continue
self._setup_headers_default(header)
else: print('Unrecognised format');return
if not configured:
Expand All @@ -1069,7 +1100,13 @@ def read(self,filename): # read contents of file
elif self._format=='surf':
self._read_data_surf(fname.split('_')[-2])
elif self._format=='default':
self._read_data_default(fname.split('_')[-1].split('.')[0])
if 'temp' in fname:
#print('fname with temp:', fname.split('_')[1].split('.')[0])
self._read_data_default(fname.split('_')[1].split('.')[0])
else:
#print('fname without temp:', fname.split('_')[-1].split('.')[0])
self._read_data_default(fname.split('_')[-1].split('.')[0])
#self._get_information()
#self._file.close()
def _detect_format(self,header):
if header.startswith('TITLE'):
Expand Down Expand Up @@ -1113,12 +1150,15 @@ def _read_data_surf(self,var_key):
self._data[hist_var_names[var_key]] = dict([(node,data[:,icol+1]) for icol,node in enumerate(self.nodes)])
def _read_data_default(self,var_key):
self._variables.append(hist_var_names[var_key])
#print('These are the variables: ', self._variables)
lns = self._file.readlines()
#print('lns: ', lns)
data = []
for ln in lns: data.append([float(d) for d in ln.strip().split()])
data = np.array(data)
if data[-1,0]<data[-2,0]: data = data[:-1,:]
self._times = np.array(data[:,0])
#print(' TIMES ', self._times)
self._data[hist_var_names[var_key]] = dict([(node,data[:,icol+1]) for icol,node in enumerate(self.nodes)])
def _get_variables(self): return self._variables
variables = property(_get_variables)#: (*lst[str]*) List of variables for which output data are available.
Expand All @@ -1133,19 +1173,19 @@ def _get_times(self): return np.sort(self._times)
times = property(_get_times) #: (*lst[fl64]*) List of times (in seconds) for which output data are available.
def _get_nodes(self): return self._nodes
nodes = property(_get_nodes) #: (*lst[fl64]*) List of node indices for which output data are available.
#def _get_information(self):
# print('FEHM history output - format '+self._format)
# print(' call format: fhistory[variable][node][time_index]')
# prntStr = ' nodes: '
# for nd in self.nodes: prntStr += str(nd)+', '
# print(prntStr)
# prntStr = ' times ('+str(len(self.times))+'): '
# for time in self.times: prntStr += str(time)+', '
# print(prntStr[:-2]+' days')
# prntStr = ' variables: '
# for var in self.variables: prntStr += str(var)+', '
# print(prntStr)
#what = property(_get_information) #:(*str*) Print out information about the fhistory object.
def _get_information(self):
print('FEHM history output - format '+self._format)
print('call format: fhistory[variable][node][time_index]')
prntStr = ' nodes: '
for nd in self.nodes: prntStr += str(nd)+', '
print(prntStr)
prntStr = 'times \n('+str(len(self.times))+'): '
for time in self.times: prntStr += str(time)+', '
print(prntStr[:-2]+' days')
prntStr = 'variables: '
for var in self.variables: prntStr += str(var)+', '
print(prntStr)
what = property(_get_information) #:(*str*) Print out information about the fhistory object.
class fzoneflux(fhistory): # Derived class of fhistory, for zoneflux output
'''Zone flux history output information object.
'''
Expand Down Expand Up @@ -1409,6 +1449,7 @@ def fdiff( in1, in2, format='diff', times=[], variables=[], components=[], nodes
return
if isinstance(in1, fcontour) or isinstance(in1, fhistory) or 'foutput' in str(in1.__class__):
# Find common timesclear
#print('from diff', in1.times , in2.times)
t = np.intersect1d(in1.times,in2.times)
if len(t) == 0:
print("ERROR: fpost object times do not have any matching values")
Expand Down

0 comments on commit d62425f

Please sign in to comment.