def testDeltaPDF2(self): print "\n test Delta PDF 2:" ZPoints = 6 ZmeanPoints = 1 ZvarPoints = 1 ZMin = 0 ZMax = 1 ZmeanMin = 0.25 ZmeanMax = ZmeanMin ZvarMin = 0 ZvarMax = ZvarMin # create arrays of type "double *" Z = np.linspace(ZMin, ZMax, ZPoints) Zmean = np.linspace(ZmeanMin, ZmeanMax, ZmeanPoints) Zvar = np.linspace(ZvarMin, ZvarMax, ZvarPoints) # create instances of PDF class d = pdf.DeltaPDF(Zmean) dPdfValM = matrix3d.Matrix3D(ZvarPoints, ZmeanPoints, ZPoints) # expected PDF values PDF = np.zeros(ZPoints) PDF[1] = 0.75 * (ZPoints - 1) PDF[2] = 0.25 * (ZPoints - 1) # calculate PDF test = d.pdfVal(Z, dPdfValM) PDFCalc = np.zeros(ZPoints) # test for k in range(ZPoints): PDFCalc[k] = dPdfValM.GetVal(0, 0, k) self.assertAlmostEqual(PDF[k], PDFCalc[k])
def testBetaPDF6(self): print "\n test Beta PDF 6: Skewed" ZPoints = 101 ZmeanPoints = 1 ZvarPoints = 1 ZMin = 0 ZMax = 1 ZmeanMin = 0.1 ZmeanMax = ZmeanMin ZvarMin = 0.01 ZvarMax = ZvarMin # create arrays of type "double *" Z = np.linspace(ZMin, ZMax, ZPoints) Zmean = np.linspace(ZmeanMin, ZmeanMax, ZmeanPoints) Zvar = np.linspace(ZvarMin, ZvarMax, ZvarPoints) # create instances of PDF class b = pdf.BetaPDF(Zmean, Zvar) bPdfValM = matrix3d.Matrix3D(ZvarPoints, ZmeanPoints, ZPoints) bPDF = np.zeros(ZPoints) # expected PDF values PDF = np.zeros(ZPoints) PDF[0] = 5.93 PDF[1] = 1.43 PDF[2] = 0.209 PDF[3] = 0.01557 PDF[4] = 0 PDF[5] = 0 # calculate PDF bTest = b.pdfVal(Z, bPdfValM) # test for k in range(ZPoints): bPDF[k] = bPdfValM.GetVal(0, 0, 20 * k) """ print "PDF[0] = inf, bPDF[0] = " + str(bPDF[0]) print "PDF[1] = " + str(PDF[1]) + ", bPDF[1] = " + str(bPDF[1]) print "PDF[2] = " + str(PDF[2]) + ", bPDF[2] = " + str(bPDF[2]) print "PDF[3] = " + str(PDF[3]) + ", bPDF[3] = " + str(bPDF[3]) print "PDF[4] = " + str(PDF[4]) + ", bPDF[4] = " + str(bPDF[4]) print "PDF[5] = " + str(PDF[5]) + ", bPDF[4] = " + str(bPDF[5]) print "PDF[6] = inf, bPDF[5] = " + str(bPDF[6]) """ self.assertTrue(np.abs(bPDF[1] - PDF[1]) < 0.01) self.assertTrue(np.abs(bPDF[2] - PDF[2]) < 0.01) self.assertTrue(np.abs(bPDF[3] - PDF[3]) < 0.01) self.assertTrue(np.abs(bPDF[4] - PDF[4]) < 0.01) self.assertTrue(np.abs(bPDF[5] - PDF[5]) < 0.01) self.assertEqual(bTest, 0)
def testBetaPDF1(self): print "\n test Beta PDF 1: Var = 0 --> Delta PDF" ZPoints = 6 ZmeanPoints = 1 ZvarPoints = 1 ZMin = 0 ZMax = 1 ZmeanMin = 0.25 ZmeanMax = ZmeanMin ZvarMin = 0 ZvarMax = ZvarMin # create arrays of type "double *" Z = np.linspace(ZMin, ZMax, ZPoints) Zmean = np.linspace(ZmeanMin, ZmeanMax, ZmeanPoints) Zvar = np.linspace(ZvarMin, ZvarMax, ZvarPoints) # create instances of PDF class b = pdf.BetaPDF(Zmean, Zvar) bPdfValM = matrix3d.Matrix3D(ZvarPoints, ZmeanPoints, ZPoints) bPDF = np.zeros(ZPoints) # expected PDF values d = pdf.DeltaPDF(Zmean) dPdfValM = matrix3d.Matrix3D(ZvarPoints, ZmeanPoints, ZPoints) dPDF = np.zeros(ZPoints) # calculate PDF bTest = b.pdfVal(Z, bPdfValM) dTest = d.pdfVal(Z, dPdfValM) # test for k in range(ZPoints): bPDF[k] = bPdfValM.GetVal(0, 0, k) dPDF[k] = dPdfValM.GetVal(0, 0, k) self.assertAlmostEqual(bPDF[k], dPDF[k]) self.assertEqual(bTest, 0) self.assertEqual(dTest, 0)
import fittogrid import interpolator import matrix import matrix3d import matrix4d # Set up inputs to fittogrid function dim1 = 2 # w~ and c~ dim2 = 1 # dimension of z~ dim3 = 1 # dimension of z_v dim4 = 10 # number of files lcgrid = 10 # length of cgrid datain = matrix4d.Matrix4D(dim1, dim2, dim3, dim4) cgrid = np.zeros((lcgrid)) interp = interpolator.LinInterp() dataout = matrix3d.Matrix3D(dim2, dim3, lcgrid) nthreads = 1 # Fill input data with numbers for i in range(dim4): datain.SetVal(0, 0, 0, i, i * i) datain.SetVal(1, 0, 0, i, i + 1) class FitToGrid(unittest.TestCase): def testNodes(self): # Test if node values are returned when the interpolating grid # is exactly along the nodes for i in range(lcgrid): cgrid[i] = i + 1 flag = fittogrid.fittogrid_func(datain, cgrid, interp, dataout,
def testBetaPDF1(self): print "\n Beta PDF 1: Zero Variance" ZPoints = 5 ZmeanPoints = 1 ZvarPoints = 1 ZMin = 0 ZMax = 1 ZmeanMin = 0.25 ZmeanMax = ZmeanMin ZvarMin = 0 ZvarMax = ZvarMin Nodes = 50 # create arrays of type "double *" Z = np.linspace(ZMin, ZMax, ZPoints) Zmean = np.linspace(ZmeanMin, ZmeanMax, ZmeanPoints) Zvar = np.linspace(ZvarMin, ZvarMax, ZvarPoints) # create instances of PDF class b = pdf.BetaPDF(Zmean, Zvar) bPdfValM = matrix3d.Matrix3D(ZvarPoints, ZmeanPoints, ZPoints) # expected PDF values PDF = np.zeros(ZPoints) PDF[1] = 1 * (ZPoints - 1) # calculate PDF test = b.pdfVal(Z, bPdfValM) PDFCalc = np.zeros(ZPoints) # test for k in range(ZPoints): PDFCalc[k] = bPdfValM.GetVal(0, 0, k) self.assertAlmostEqual(PDF[k], PDFCalc[k]) # create Integrators Trapz = integrator.Trapz() Quadr = integrator.GLQuad(Nodes) # create Filtered Data Matrices postTrapz = matrix.Matrix(ZvarPoints, ZmeanPoints) postQuadr = matrix.Matrix(ZvarPoints, ZmeanPoints) # create matrix for printing filterTrapz = np.zeros(ZmeanPoints) filterQuadr = np.zeros(ZmeanPoints) # create test data testData = np.ones(ZPoints) # calculate filtered reaction rates c = convolute.convVal_func(Z, testData, bPdfValM, postTrapz, Trapz) c = convolute.convVal_func(Z, testData, bPdfValM, postQuadr, Quadr) for j in range(ZmeanPoints): filterTrapz[j] = postTrapz.GetVal(0, j) filterQuadr[j] = postQuadr.GetVal(0, j) self.assertEqual(filterTrapz[j], 1) self.assertAlmostEqual(filterQuadr[j], 1, 1)
def testBetaPDF3(self): print "\n Beta PDF 3: Symmetric" Points = 6 ZPoints = 101 ZmeanPoints = 1 ZvarPoints = 1 ZMin = 0 ZMax = 1 ZmeanMin = 0.5 ZmeanMax = ZmeanMin ZvarMin = 0.05 ZvarMax = ZvarMin Nodes = 10 # create arrays of type "double *" Z = np.linspace(ZMin, ZMax, ZPoints) Zmean = np.linspace(ZmeanMin, ZmeanMax, ZmeanPoints) Zvar = np.linspace(ZvarMin, ZvarMax, ZvarPoints) # create instances of PDF class b = pdf.BetaPDF(Zmean, Zvar) bPdfValM = matrix3d.Matrix3D(ZvarPoints, ZmeanPoints, ZPoints) bPDF = np.zeros(Points) # expected PDF values PDF = np.zeros(ZPoints) PDF[0] = 0 PDF[1] = 0.96 PDF[2] = 1.44 PDF[3] = 1.44 PDF[4] = 0.96 PDF[5] = 0 # calculate PDF bTest = b.pdfVal(Z, bPdfValM) # test for k in range(Points): bPDF[k] = bPdfValM.GetVal(0, 0, 20 * k) """ print "PDF[0] = inf, bPDF[0] = " + str(bPDF[0]) print "PDF[1] = " + str(PDF[1]) + ", bPDF[1] = " + str(bPDF[1]) print "PDF[2] = " + str(PDF[2]) + ", bPDF[2] = " + str(bPDF[2]) print "PDF[3] = " + str(PDF[3]) + ", bPDF[3] = " + str(bPDF[3]) print "PDF[4] = " + str(PDF[4]) + ", bPDF[4] = " + str(bPDF[4]) print "PDF[5] = inf, bPDF[5] = " + str(bPDF[5]) """ self.assertTrue(np.abs(bPDF[1] - PDF[1]) < 0.2) self.assertTrue(np.abs(bPDF[2] - PDF[2]) < 0.2) self.assertTrue(np.abs(bPDF[3] - PDF[3]) < 0.2) self.assertTrue(np.abs(bPDF[4] - PDF[4]) < 0.2) self.assertEqual(bTest, 0) # create Integrators Trapz = integrator.Trapz() Quadr = integrator.GLQuad(Nodes) # create Filtered Data Matrices postTrapz = matrix.Matrix(ZvarPoints, ZmeanPoints) postQuadr = matrix.Matrix(ZvarPoints, ZmeanPoints) # create matrix for printing filterTrapz = np.zeros(ZmeanPoints) filterQuadr = np.zeros(ZmeanPoints) # create test data testData = np.ones(ZPoints) # calculate filtered reaction rates c = convolute.convVal_func(Z, testData, bPdfValM, postTrapz, Trapz) c = convolute.convVal_func(Z, testData, bPdfValM, postQuadr, Quadr) for j in range(ZmeanPoints): filterTrapz[j] = postTrapz.GetVal(0, j) filterQuadr[j] = postQuadr.GetVal(0, j) self.assertTrue(np.abs(filterTrapz[j] - 1) < 0.01) self.assertTrue(np.abs(filterQuadr[j] - 1) < 0.01)
ZvarPy = helper.Line() ZvarPy = np.linspace(ZvarMin, ZvarMax, ZvarPoints) Zvar = vector.Vector(ZvarPoints) helper.copy_py_to_vector(ZvarPy, Zvar) """ print 'ZvarPy' helper.print_py(ZvarPy) print 'Zvar' for i in range(0, ZvarPoints): s = 'Zvar[' + repr(i) + '] = ' + repr(Zvar.GetVal(i)) print s """ pdfValMPy = helper.Line() pdfValMPy = np.empty(ZvarPoints * ZmeanPoints) pdfValM = matrix3d.Matrix3D(ZvarPoints, ZmeanPoints, ZPoints) d = deltaPDF.DeltaPDF(Z, ZPoints) for i in range(0, ZvarPoints): for j in range(0, ZmeanPoints): for k in range(0, ZPoints): pdfValM.SetVal(i, j, k, 0) print 'after setVal pdfValM' pdfValReturn = d.pdfVal(Zvar, Zmean, pdfValM) print 'after pdfValReturn' for i in range(0, ZvarPoints): for j in range(0, ZmeanPoints): for k in range(0, ZPoints): s = 'pdfValM[var = ' + repr(Zvar.GetVal(i)) + '][mean = ' + repr( Zmean.GetVal(j)) + '][Z = ' + repr(
ZvarPy = np.linspace(0, float(Zvar_max[0]), int(Zvar_grid[0])) # Copy to C++ readable vectors ZPoints = len(ZPy) ZvarPoints = len(ZvarPy) ZmeanPoints = len(ZmeanPy) Z = vector.Vector(ZPoints) Zmean = vector.Vector(ZmeanPoints) Zvar = vector.Vector(ZvarPoints) helper.copy_py_to_vector(ZPy, Z) helper.copy_py_to_vector(ZmeanPy, Zmean) helper.copy_py_to_vector(ZvarPy, Zvar) # generate PDF matrix 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()