def testInterpolate(self): f = open('testfile', 'w') f.write('* \n') f.write('A\tB\tC\tD\tE\tF\tG\tH\tI\tJ\n') for i in range(10): for j in range(10): f.write('%d\t' % (i * 10 + j)) f.write('\n') f.close() dataobj = iof.ProcFile('testfile') locs = np.zeros(3) datavec = np.zeros(4) dataobj.interpolate(['C', 'D', 'J'], locs, datavec, interpval=63) np.testing.assert_array_almost_equal(datavec, [64, 65, 66, 72]) self.assertRaises(RuntimeError, dataobj.interpolate, ['C', 'D', 'J'], locs, datavec, interpval=124) self.assertRaises(IOError, dataobj.interpolate, ['C', 'D', 'J'], locs, datavec, interpmethod='linexar') self.assertRaises(IOError, dataobj.interpolate, ['C', 'Q', 'J'], locs, datavec, interpmethod='linear') os.remove('testfile')
def testGetTitles(self): f = open('testfile', 'w') f.write('* \n') strings = [ 'dojkfr', 'jkn vsnkl', 'afkjaf', 'ajknnjv nsv', '99"|} {{{}}#^&$' ] for i in strings: f.write(i) f.write('\t') f.close() dataobj = iof.ProcFile('testfile') titles = dataobj.gettitles() os.remove('testfile') np.testing.assert_array_equal(strings, titles)
def findC(datafiles, testspecies, bestC, options): # Interpolate each datafile, generate a matrix from interpolated data nofiles = len(datafiles) nocols = len(testspecies) + 1 locs = np.zeros(nocols - 1) interpdata = np.zeros((nofiles, nocols)) filesmatrix = np.zeros((nofiles, 3)) for ii in range(nofiles): # interpolate for each file dataobj = iof.ProcFile(datafiles[ii]) if ii == 0: titles = dataobj.gettitles() else: titles1 = dataobj.gettitles() np.testing.assert_array_equal( titles1, titles ) # Verify that all data files have the same column headers dataobj.interpolate(testspecies, locs, interpdata[ii, :], interpval=float(options["StoichMassFrac"][0]), interpmethod="".join(options["InterpMethod"])) filesmatrix[:, 0] = interpdata[:, 0] filesmatrix[:, 2] = interpdata[:, 0] # filesmatrix stores (row1) stoich prog var (row2) file indices (row3) stoich Temps filesmatrix[:, 1] = range(nofiles) filesmatC = matrix.Matrix(nofiles, 3) for i in range(nofiles): for j in range(3): filesmatC.SetVal(i, j, filesmatrix[i, j]) # Generate combinations matrix - skip this step if user input says to skip = options["SkipProgVar"][0] assert ( (skip == 'yes') | (skip == 'no') ), "must select input <yes> or <no> for <skip progress variable optimization:>" if skip == 'no': combosmatrix = np.zeros((nocols, cs.totnumcoms(nocols - 1) + 1)) cs.combination_mat(combosmatrix[1:, 1:]) else: print "Skipping progress variable optimization, using user input" options["PlotAllC"][0] = 'no' combosmatrix = np.zeros((nocols, 2)) for i in range(nocols - 1): combosmatrix[i + 1, 1] = 1 combosmatrix[0, 0] = 1 # Calculate progress variables progvars = np.dot(interpdata, combosmatrix) length = progvars.shape[1] # Generate progress variable matrix progVar = matrix.Matrix(nofiles, length) for i in range(nofiles): for j in range(length): progVar.SetVal(i, j, progvars[i, j]) # Sort PROGVARS and FILESMATRIX by temperature sortmethod = options["sort method"][0] print "Sorting PROGVARS by temperature using %s sort" % sortmethod for i in [progVar, filesmatC]: if sortmethod == 'bubble': sorter = sorting.BubbleSort(i) elif sortmethod == 'standard': sorter = sorting.StandardSort(i) elif sortmethod == 'brute': sorter = sorting.BruteSort(i) else: raise IOError( "invalid sorting method (%s) specified, instead use <bubble> sort" % sortmethod) sorter.SetRefColNum(0) sorter.sort_data() print "Sorting FILESMATRIX by temperature" # Test monotonicity of PROGVARS print "Testing monotonicity \n" monoAry = np.zeros(length, dtype=np.int32) checker = monocheck.MonoCheck(progVar) # Create MonoCheck object assert checker.CheckStrictMonoticity( monoAry, 0) == 0, "CheckStrictMonoticity ran unsuccessfully.\n" # ^ Check which columns of progVar are strictly increasing or strictly decreasing and store result in monoAry # Test for maximum slope if multiple monotonic progress variables are returned checksum = np.sum(monoAry) if checksum % 3 != 0: raise RuntimeError( "Incorrect values in monoAry vector, check monotonicity function.\n" ) if checksum > 3: maxslopetest = options["MaxSlopeTest"][0] print "Testing max slope using %s" % maxslopetest if maxslopetest == 'linear regression': maxchecker = maxslope.LinRegression(progVar) elif maxslopetest == 'end point slope': maxchecker = maxslope.EndPointSlope(progVar) else: raise IOError( "invalid maximum slope test (%s) specified, instead use <linear regression>" % maxslopetest) assert maxchecker.MostMonotonic( monoAry, 0) == 0, "MostMonotonic ran unsuccessfully.\n" # ^ Distinguish the best monotonic progress variables elif checksum == 0: # Least non-monotonic tests to be implemented in beta version lnmoption = options["lnmcheck"][0] print "Finding least non-monotonic progress variable using %s nonmono check" % lnmoption if lnmoption == 'simple': lnm_check = leastnonmono.SimpleLNM(progVar) elif lnmoption == 'advanced': lnm_check = leastnonmono.AdvancedLNM(progVar) else: raise IOError( "invalid lnm test (%s) specified, instead use <simple> or <advanced>" % maxslopetest) assert lnm_check.LeastNonMonotonic( monoAry, 0) == 0, "LeastNonMonotonic ran unsuccessfully.\n" # Print results monoAryflag = 0 for i in range(length): if monoAry[ i] == 3.0: # Print best monotonic progress variable if it exists if monoAryflag != 0: raise RuntimeError( "Error in contents of monoAry vector: multiple best selected.\n" ) monoAryflag = 2 bestC[:] = iof.get_progvar(combosmatrix[1:, i], testspecies, locs, i) print 'The chosen progress variable is C = %s' % bestC[1][0], for j in bestC[1][1:]: print "+ %s" % j, print '\nThe column numbers of these species are ', bestC[ 0], ', respectively.\n' elif monoAry[ i] == 1.0: # Otherwise print least non-monotonic progress variable if monoAryflag != 0: raise RuntimeError("Error in contents of monoAry vector.\n") monoAryflag = 1 bestC[:] = iof.get_progvar(combosmatrix[1:, i], testspecies, locs, i) print 'WARNING: no monotonic progress variables found, but proceeding with best alternative.\n' print 'The least non-monotonic progress variable is C = %s' % bestC[ 1][0], for j in bestC[1][1:]: print "+ %s" % j, print '\nThe column numbers of these species are', bestC[ 0], ', respectively.\n' for i in range( length): # Print/identify other monotonic progress variables if monoAry[i] == 2.0: if monoAryflag < 2: raise RuntimeError("Error in contents of monoAry vector.\n") elif monoAryflag == 2: print "Other candidate monotonic progress variables are:" otherC = iof.get_progvar(combosmatrix[1:, i], testspecies, locs, i) print 'C = %s' % otherC[1][0], for j in otherC[1][1:]: print "+ %s" % j, print "\n", monoAryflag = 3 if monoAryflag < 1: # Give error if no best progress variable is found raise RuntimeError("Error: no best progress variable selected.") # Write results for i in range(nofiles): filesmatC.SetVal(i, 0, progVar.GetVal(i, bestC[2])) # Plot results Cst = np.zeros(nofiles) Tst = np.zeros(nofiles) plt.figure() for ii in range(nofiles): Tst[ii] = progVar.GetVal(ii, 0) if options["PlotAllC"][0] == 'yes': for jj in range(length - 1): for ii in range(nofiles): Cst[ii] = progVar.GetVal(ii, jj + 1) Cst = Cst / Cst.max() otherplt, = plt.plot(Tst, Cst, color='r') for ii in range(nofiles): Cst[ii] = filesmatC.GetVal(ii, 0) Cst = Cst / Cst.max() bestplt, = plt.plot(Tst, Cst, color='k', marker='o', markerfacecolor='none') if options["PlotAllC"][0] == 'yes': plt.legend( [bestplt, otherplt], ['Best progress variable', 'Other candidate progress variables'], loc='lower right') plt.xlabel("T (K)") plt.ylabel("Normalized Progress Variable (C/Cmax)") plt.title("Best Progress Variable") if skip == 'yes': plt.title("User-selected progress variable") plt.savefig("output/%s.pdf" % 'CvsTemp') plt.clf() return filesmatC
def findC(datafiles, testspecies, bestC): # Interpolate each datafile, generate a matrix from interpolated data nofiles = len(datafiles) nocols = len(testspecies) + 1 locs = np.zeros(nocols - 1) interpdata = np.zeros((nofiles, nocols)) filesmatrix = np.zeros((nofiles, 2)) for ii in range(nofiles): # interpolate for each file dataobj = iof.ProcFile(datafiles[ii]) if ii == 0: titles = dataobj.gettitles() else: titles1 = dataobj.gettitles() np.testing.assert_array_equal( titles1, titles ) # Verify that all data files have the same column headers dataobj.interpolate(testspecies, locs, interpdata[ii, :]) filesmatrix[:, 0] = interpdata[:, 0] # filesmatrix stores file indices in order and interpolated Temps filesmatrix[:, 1] = range(nofiles) filesmatC = matrix.Matrix(nofiles, 2) for i in range(nofiles): for j in range(2): filesmatC.SetVal(i, j, filesmatrix[i, j]) # Generate combinations matrix combosmatrix = np.zeros((nocols, cs.totnumcoms(nocols - 1) + 1)) combosmatrix[0, 0] = 1 cs.combination_mat(combosmatrix[1:, 1:]) # Calculate progress variables progvars = np.dot(interpdata, combosmatrix) # Generate progress variable matrix progVar = matrix.Matrix(nofiles, cs.totnumcoms(nocols - 1) + 1) for i in range(nofiles): for j in range(cs.totnumcoms(nocols - 1) + 1): progVar.SetVal(i, j, progvars[i, j]) # Sort PROGVARS and FILESMATRIX by temperature print "Sorting PROGVARS by temperature" sortmethod = 'bubble' if "".join(sortmethod) == 'bubble': sorter = bubble_sort.bubble_sort(progVar) sorter.SetRefColNum(0) sorter.SetSortEndIndex(nofiles) sorter.SetSortStartIndex(0) sorter.generateIndexArray() sorter.extractRefCol() sorter.sort_data() print "Sorting FILESMATRIX by temperature" sortmethod = 'bubble' if "".join(sortmethod) == 'bubble': sorter = bubble_sort.bubble_sort(filesmatC) sorter.SetRefColNum(0) sorter.SetSortEndIndex(nofiles) sorter.SetSortStartIndex(0) sorter.generateIndexArray() sorter.extractRefCol() sorter.sort_data() # Test monotonicity of PROGVARS print "Testing monotonicity \n" length = progvars.shape[1] monoAryPy = np.zeros(length) monoAry = vector.Vector(length) helper.copy_py_to_vector(monoAryPy, monoAry) checker = monocheck.MonoCheck(progVar) # Create MonoCheck object assert checker.CheckStrictMonoticity( 0, monoAry) == 0, "CheckStrictMonoticity ran unsuccessfully.\n" # ^ Check which columns of progVar are strictly increasing or strictly decreasing and store result in monoAry # Test for maximum slope if multiple monotonic progress variables are returned checksum = 0 for i in range(length): checksum += monoAry.GetVal(i) if checksum % 3 != 0: raise RuntimeError( "Incorrect values in monoAry vector, check monotonicity function.\n" ) if checksum > 3: print "Testing max slope:" maxchecker = linregression.LinRegression(progVar) #maxchecker = endpointslope.EndPointSlope(progVar) assert maxchecker.MostMonotonic( 0, monoAry) == 0, "MostMonotonic ran unsuccessfully.\n" # ^ Distinguish the best monotonic progress variables elif checksum == 0: # Least non-monotonic tests to be implemented in beta version print "Finding least non-monotonic:" monoAry[0] = 1 # Print results monoAryflag = 0 for i in range(length): if monoAry.GetVal( i ) == 3.0: # Print best monotonic progress variable if it exists if monoAryflag != 0: raise RuntimeError( "Error in contents of monoAry vector: multiple best selected.\n" ) monoAryflag = 2 bestC[:] = iof.get_progvar(combosmatrix[1:, i], testspecies, locs, i) print 'The best strictly monotonic progress variable is C = %s' % bestC[ 1][0], for j in bestC[1][1:]: print "+ %s" % j, print '\nThe column numbers of these species are ', bestC[ 0], ', respectively.\n' elif monoAry.GetVal( i ) == 1.0: # Otherwise print least non-monotonic progress variable if monoAryflag != 0: raise RuntimeError("Error in contents of monoAry vector.\n") monoAryflag = 1 bestC[:] = iof.get_progvar(combosmatrix[1:, i], testspecies, locs, i) print 'WARNING: no monotonic progress variables found, but proceeding with best alternative.\n' print 'The least non-monotonic progress variable is C = %s' % bestC[ 1][0], for j in bestC[1][1:]: print "+ %s" % j, print '\nThe column numbers of these species are', bestC[ 0], ', respectively.\n' for i in range( length): # Print/identify other monotonic progress variables if monoAry.GetVal(i) == 2.0: if monoAryflag < 2: raise RuntimeError("Error in contents of monoAry vector.\n") elif monoAryflag == 2: print "Other candidate monotonic progress variables are:" otherC = iof.get_progvar(combosmatrix[1:, i], testspecies, locs, i) print 'C = %s' % otherC[1][0], for j in otherC[1][1:]: print "+ %s" % j, print "\n", monoAryflag = 3 if monoAryflag < 1: # Give error if no best progress variable is found raise RuntimeError("Error: no best progress variable selected.") # Plot results iof.plotCvT(progvars[:, 0], progvars[:, bestC[2]]) # Write results for i in range(nofiles): filesmatC.SetVal(i, 0, progvars[i, bestC[2]]) return filesmatC
def findC(datafiles, testspecies, bestC, filesmatrix): # interpolate each datafile, generate a matrix from interpolated data nofiles = len(datafiles) nocols = len(testspecies)+1 locs = np.zeros(nocols-1) interpdata = np.zeros((nofiles,nocols)) for ii in range(nofiles): dataobj = iof.ProcFile(datafiles[ii]) if ii == 0: titles = dataobj.gettitles() else: titles1 = dataobj.gettitles() np.testing.assert_array_equal(titles1,titles) # verify that all data files have the same column headers dataobj.interpolate(testspecies, locs, interpdata[ii,:]) filesmatrix[:,0] = interpdata[:,0] filesmatrix[:,1] = range(nofiles) # generate combinations matrix combosmatrix = np.zeros((nocols,cs.totnumcoms(nocols-1)+1)) combosmatrix[0,0] = 1 cs.combination_mat(combosmatrix[1:,1:]) # calculate progress variables progvars = np.dot(interpdata,combosmatrix) # sort PROGVARS and FILESMATRIX by temperature # will add connectivity to C++ sort later print "sorting PROGVARS by temperature" print "sorting FILESMATRIX by temperature" # test monotonicity of PROGVARS # will add connectivity to C++ monotonicity check later print "testing monotonicity" length = progvars.shape[1] monocheck = np.zeros(length) monocheck[3] = 3 ### monocheck[4] = 3 ### checksum = monocheck.sum() if checksum % 3 != 0: raise RuntimeError("incorrect values in monocheck vector, check monotonicity function") if checksum > 3: # max slop tests in C++ to be connected print "testing max slope" monocheck[3] = 2 ### elif checksum == 0: # non monotonicity check in C++ to be connected print "finding least non-monotonic" monocheck[-1] = 1 ### # print results monocheckflag = 0 for i in range(length): if monocheck[i] == 3: # Find best monotonic progress variable if it exists if monocheckflag != 0: raise RuntimeError("error in contents of monocheck vector: multiple best selected") monocheckflag = 2 bestC[:] = iof.get_progvar(combosmatrix[1:,i], testspecies, locs, i) print '\nThe best monotonically increasing progress variable is C = %s' % bestC[1][0], for j in bestC[1][1:]: print "+ %s" % j print '\nThe column numbers of these species are', bestC[0],', respectively' elif monocheck[i] == 1: # otherwise find least non-monotonic progress variable if monocheckflag != 0: raise RuntimeError("error in contents of monocheck vector") monocheckflag = 1 bestC[:] = iof.get_progvar(combosmatrix[1:,i], testspecies, locs, i) print '\nWARNING: no monotonic progress variables found, but proceeding with best alternative' print 'The least non-monotonic progress variable is C = %s' % bestC[1][0], for j in bestC[1][1:]: print "+ %s" % j print '\nThe column numbers of these species are', bestC[0],', respectively' for i in range(length): # identify other monotonic progress variables if monocheck[i] == 2: if monocheckflag < 2: raise RuntimeError("error in contents of monocheck vector") else: print "\nOther candidate monotonic progress variables are:" otherC = iof.get_progvar(combosmatrix[1:,i], testspecies, locs, i) print 'C = %s' % otherC[1][0], for j in otherC[1][1:]: print "+ %s" % j print "\n" monocheckflag = 3 if monocheckflag < 1: # give error if no best progress variable is found raise RuntimeError("error: no best progress variable selected") # plot results iof.plotCvT(progvars[:,0],progvars[:,bestC[2]]) # write results filesmatrix[:,0] = progvars[:,bestC[2]]
print "Generating PDF matrix with", Zpdf[0], "PDF" pdfValM = matrix3d.Matrix3D(ZvarPoints, ZmeanPoints, ZPoints) for i in range(ZvarPoints): for j in range(ZmeanPoints): for k in range(ZPoints): pdfValM.SetVal(i, j, k, 0) if Zpdf[0] == "delta": import deltaPDF d = deltaPDF.DeltaPDF(Z, ZPoints) elif Zpdf[0] == "beta": # beta PDF support still in progress import betaPDF d = betaPDF.BetaPDF(Z, ZPoints) pdfValReturn = d.pdfVal(Zvar, Zmean, pdfValM) # Find locations of columns of reaction rate data for species in the best progress variable dataobj = iof.ProcFile(datafiles[0]) titles = dataobj.gettitles() rxn_rate_locs = range(len(bestC[0])) for ii in range(len(rxn_rate_locs)): locflag = 0 species = list(bestC[1][ii]) speciesprodrate = species[2:] speciesprodrate = "".join(speciesprodrate) speciesprodrate = "".join(["ProdRate", speciesprodrate, " [kg/m^3s]"]) for jj in range(len(titles)): if titles[jj] == speciesprodrate: rxn_rate_locs[ii] = jj locflag = 1 if locflag == 0: raise IOError("Production rate data for %s is missing" % speciesprodrate)