3 import os, sys, subprocess, time
 
    4 from scipy.interpolate 
import interp1d
 
    9    p = subprocess.Popen(cmdlist, stdout=subprocess.PIPE,\
 
   10                                  stderr=subprocess.PIPE)
 
   11    out, err = p.communicate()
 
   17     compare text data from two files 
   18     - ignore all strings which start with '#' 
   19     - use relative tolerance 'tol' 
   20     - compare test file 'stest' against trial file 'strial' 
   28    for i1 
in range(len(strial)):
 
   29       s1 = strial[i1].strip()
 
   31       if (len(s1)==0) 
or (s1[0]==
'#'):
 
   36       for i2 
in range(i2min,len(stest)):
 
   37          s2 = stest[i2].strip()
 
   38          if (len(s2)==0) 
or (s2[0]==
'#'):
 
   49       if len(fields1)!=len(fields2):
 
   52          for i 
in range(len(fields1)):
 
   54                f1 = float(fields1[i])
 
   55                f2 = float(fields2[i])
 
   58                if ((abs(f1)<=l_limit) 
and (abs(f2)<=l_limit)):
 
   61                if abs(f2-f1)/(abs(f1)+1e-20)>tol:
 
   65                if fields1[i]!=fields2[i]:
 
   74       flog.write(
"FAILED, files differ significantly in %d lines\n" % errcode)
 
   76       flog.write(
"FAILED, files have different number of lines\n")
 
   86     compare x- and y- data of two lists from two files via linear interpolation 
   87     - ignore all strings which start with '#' 
   88     - use relative tolerance 'tol' 
   89     - compare test file 'stest' against trial file 'strial' 
   90     - x_col is the index of the x-column, this column should be monotonic increasing 
   91     - y_col is the index of the y-column 
   97    if isinstance(y_col,int):
 
   99    if isinstance(tol,int):
 
  103    amount_ycols = len(y_col)
 
  106    y_trial = [[] 
for i 
in range(amount_ycols)]
 
  109    for i 
in range(len(strial)):
 
  110       s1 = strial[i].strip()
 
  112       if (len(s1)==0) 
or (s1[0]==
'#'):
 
  117           x_trial.append(float(s1.split()[x_col]))
 
  119           for j 
in range(amount_ycols):
 
  120               y_trial[j].append(float(s1.split()[y_col[j]]))
 
  126    y_test = [[] 
for i 
in range(amount_ycols)]
 
  128    for i 
in range(len(stest)):
 
  129       s1 = stest[i].strip()
 
  131       if (len(s1)==0) 
or (s1[0]==
'#'):
 
  136           x_test.append(float(s1.split()[x_col]))
 
  138           for j 
in range(amount_ycols):
 
  139               y_test[j].append(float(s1.split()[y_col[j]]))
 
  145    start_x = max(x_trial[0],x_test[0])
 
  146    end_x   = max(start_x,min(x_trial[-1],x_test[-1]))
 
  148    for i 
in range(len(x_trial)):
 
  149        if (x_trial[i] >= start_x) 
and (x_trial[i] <= end_x):
 
  153    comp_ind_start = min(comp_ind)
 
  154    comp_ind_end   = max(comp_ind)
 
  157    for i 
in range(amount_ycols):
 
  161           test_function  = 
interp1d(x_test,y_test[i]  ,kind=
'linear')
 
  162           trial = np.array(y_trial[i][comp_ind_start:comp_ind_end])
 
  163           test  = np.array(test_function(x_trial[comp_ind_start:comp_ind_end]))
 
  164           diff_list = list(map(
lambda x,y: abs(1.-abs(x)/(abs(y)+1e-20)),trial,test))
 
  166           diff_list =np.array(diff_list)
 
  167           diff_list[(test<l_limit) & (trial<l_limit)] = 0
 
  173           max_diff  = max(diff_list)
 
  180       if max_diff > tol[i]:
 
  185           flog.write(
"FAILED, error in reading columns\n")
 
  187           flog.write(
"FAILED, maximum difference %4.2e" % max_diff + 
" was above the tolerance of %4.2e"  % tol[i]+
" in column "+str(y_col[i])+
"\n")
 
  189           flog.write(
"FAILED, could not find a maximum difference"+
"\n")
 
  191           flog.write(
"FAILED, interpolation failed"+
"\n")
 
  202     compare x- and y- data of two lists from two files via linear interpolation 
  203     - ignore all strings which start with '#' 
  204     - use relative tolerance 'tol' 
  205     - compare test file 'stest' against an analytic solution 
  206     - x_col is the index of the x-column 
  207     - y_col is the index of the y-column 
  208     - function is a list of functions, representing the behavior of the specific columns 
  214    if isinstance(y_col,int):
 
  216    if isinstance(tol,int):
 
  220    amount_ycols = len(y_col)
 
  223    y_trial = [[] 
for i 
in range(amount_ycols)]
 
  226    for i 
in range(len(strial)):
 
  227       s1 = strial[i].strip()
 
  229       if (len(s1)==0) 
or (s1[0]==
'#'):
 
  234           x_trial.append(float(s1.split()[x_col]))
 
  236           for j 
in range(amount_ycols):
 
  237               y_trial[j].append(float(s1.split()[y_col[j]]))
 
  243    y_test = [[] 
for i 
in range(amount_ycols)]
 
  245    for i 
in range(len(stest)):
 
  246       s1 = stest[i].strip()
 
  248       if (len(s1)==0) 
or (s1[0]==
'#'):
 
  253           x_test.append(float(s1.split()[x_col]))
 
  255           for j 
in range(amount_ycols):
 
  256               y_test[j].append(float(s1.split()[y_col[j]]))
 
  263    for i 
in range(amount_ycols):
 
  266       current_f = function[i]
 
  269           analytic_y = current_f(np.array(x_test))
 
  275       diff_list = list(map(
lambda x,y: abs(1.-abs(x)/(abs(y)+1e-20)),y_test[i],analytic_y))
 
  278           max_diff  = max(diff_list)
 
  283       if max_diff > tol[i]:
 
  288           flog.write(
"FAILED, error in reading columns\n")
 
  290           flog.write(
"FAILED, could not calculate analytic solution for column "+str(y_col[i])+
"\n")
 
  292           flog.write(
"FAILED, could not find a maximum difference"+
"\n")
 
  294           flog.write(
"FAILED, maximum difference %4.2e" % max_diff + 
" was above the tolerance of %4.2e"  % tol[i]+
" in column "+str(y_col[i])+
"\n")
 
  304    """ Class for winnet testcases 
  307        - testname: the name of the testcase 
  308        - basedir:  winnet base directory (where the Makefile is located) 
  309        - program:  path to the program that is being tested 
  310        - testdir:  test directory (normally $WINNET/test/<TESTNAME> 
  311        - parfile:  name of the parameter file that is used 
  312        - logfile:  where the test log will be written 
  328       if not hasattr(self,
'testdir'):
 
  330       if os.path.exists(self.
testdir): 
 
  331          subprocess.call([
"rm",
"-rf", self.
testdir])
 
  332       if not hasattr(self,
'logfile'):
 
  334       if os.path.exists(self.
logfile):
 
  335          subprocess.call([
"rm",
"-rf",self.
logfile])
 
  336       if not hasattr(self,
'arcfile'):
 
  338       if not os.path.exists(self.
arcfile):
 
  339          print(
"ERROR: archive file " + self.
arcfile + 
" is missing")
 
  342       os.chdir(self.
basedir + 
"/test")
 
  343       subprocess.call([
"tar",
"axf",self.
arcfile])
 
  344       if not os.path.isdir(self.
testdir):
 
  345          print(
"ERROR: archive "+self.
arcfile+
" is corrupt. Exiting...")
 
  349       if not hasattr(self,
'parfile'):
 
  353          out = os.listdir(
'./')
 
  357             if fname.endswith(
'.par'):
 
  359                parfile_count = parfile_count + 1
 
  360          if parfile_count != 1:
 
  361             print(
"ERROR: cannot determine parameter file for the test")
 
  364          if not os.path.exists(self.
parfile):
 
  365             print(
"ERROR: specified paramter file "+self.
parfile+
" is missing")
 
  371       if os.path.exists(
"testrun"):
 
  372          subprocess.call([
"rm",
"-rf",
"testrun"])
 
  375       command = 
"sed 's%@WINNET@%" +self.
basedir+
"%g;" \
 
  376               +      
"s%@TESTDIR@%"+self.
testdir+
"%g' "\
 
  379       subprocess.call(command,shell=
True)
 
  386       if not os.path.exists(self.
program):
 
  388          subprocess.call([
"make",
"clean"])
 
  389          subprocess.call(
"make")
 
  400       environment_command = 
'export OMP_NUM_THREADS=1 && ' 
  401       os.chdir(self.
testdir+
"/testrun")
 
  403       subprocess.call(
"ulimit -s unlimited\n"  + command + \
 
  405       if [ $? -ne 0 ]; then 
  406         echo "Test simulation with PID #`cat PID` probably failed to launch." 
  407         echo "Check """+self.
testdir+
"""/testrun/ERR for errors" 
  415       os.chdir(self.
testdir+
"/testrun")
 
  416       if not os.path.exists(
"../OUT"):
 
  417          print(
"\nWARNING: test suite doesn't have example OUT file!")
 
  421       expected_lines = int(out.split()[0])
 
  426       PID = int(out.split()[0])
 
  433             still_running = (len(out.split())>0)
 
  436             current_lines = int(out.split()[0])
 
  438          n1 = self.len_spaces * current_lines / expected_lines
 
  439          n1 = int(min(n1, self.len_spaces))
 
  440          n2 = self.len_spaces - n1
 
  441          q  = 100.0 * current_lines / expected_lines
 
  444          sys.stdout.write(
"\r [" + self.whichtest + 
"] " + self.
testname \
 
  445                                  + 
": .."+
"."*n1  + 
"-/|\\"[i%4] +
" "*n2   \
 
  446                                  + (
" [%5.1f" % q) + 
"%]")
 
  453       os.chdir(self.
testdir+
"/testrun")
 
  456       if not hasattr(self, 
'checklist'):
 
  457          flog.write(
"WARNING: no files defined for checking\n")
 
  459       flog.write(
"Comparing files:\n")
 
  460       for outfile 
in list(self.checklist.keys()):
 
  461          flog.write(
" - %-20s: " % outfile)
 
  465          meth     = self.checklist[outfile][
'method']
 
  467          if os.path.exists(
'../'+outfile) 
and (meth != 
'exists'):
 
  468             f_trial = open(
'../'+outfile)
 
  469             s_trial = f_trial.read().split(
'\n')
 
  471          elif (meth == 
'exists'):
 
  475             flog.write(
"FAILED, missing trial file in the test archive\n")
 
  479          if os.path.exists(outfile):
 
  480             if (meth != 
'exists'):
 
  481                 f_test = open(outfile)
 
  482                 s_test = f_test.read().split(
'\n')
 
  486             flog.write(
"FAILED, missing file\n")
 
  490          if (meth != 
'exists'):
 
  491              tol      = self.checklist[outfile][
'tolerance']
 
  493          if 'lowerlimit' in self.checklist[outfile]:
 
  494              l_limit  = self.checklist[outfile][
'lowerlimit']
 
  498              if (meth == 
'default'):
 
  499                 errc = compare_default (s_test, s_trial, tol,l_limit, flog)
 
  501              elif(meth == 
'listcompare'):
 
  502                 x_column  = self.checklist[outfile][
'x_column']
 
  503                 y_column  = self.checklist[outfile][
'y_column']
 
  504                 errc      = compare_lists (s_test, s_trial, x_column, y_column, tol, l_limit, flog)
 
  506              elif(meth == 
'analyticcompare'):
 
  507                 x_column  = self.checklist[outfile][
'x_column']
 
  508                 y_column  = self.checklist[outfile][
'y_column']
 
  509                 functions = self.checklist[outfile][
'function']
 
  510                 errc      = compare_analytic (s_test, s_trial, x_column, y_column, functions, tol, flog)
 
  512              elif(meth == 
'exists'):
 
  516                 flog.write(
"FAILED, unknown file comparison method '%s'\n" % meth)
 
  521              flog.write(
"FAILED, test method failed.\n")
 
  530       if os.path.exists(self.
logfile):
 
  531          subprocess.call([
"rm",
"-rf",self.
logfile])
 
  532       if os.path.exists(self.
testdir):
 
  533          subprocess.call([
"rm",
"-rf",self.
testdir])