def getSpectraAndDisp(k0, nK, nW, root, comp=0): files = glob(root) numFiles = len(files) d = pg.GData(files[0]) dg = pg.GInterpNodal(d, 2, 'ns') x, val = dg.interpolate(0) evolution = np.zeros((numFiles, len(x[0]))) spectrum = np.zeros((numFiles, nK)) t = np.zeros(numFiles) for i, f in enumerate(files): d = pg.GData(f) t[i] = d.time dg = pg.GInterpNodal(d, 2, 'ns') x, val = dg.interpolate(comp) evolution[i, :] = val valf = fftpack.fft(val) spectrum[i, :] = np.abs(valf[1:(nK + 1)])**2 k = fftpack.fftfreq(len(val), x[0][1] - x[0][0]) k = 2 * np.pi * k[1:(nK + 1)] / k0 x = x[0] * k0 / 2 / np.pi disp = np.zeros((nW, nK)) for i in range(nK): ft = fftpack.fft(spectrum[:, i]) disp[:, i] = np.abs(ft[1:(nW + 1)])**2 omega = fftpack.fftfreq(numFiles, t[1] - t[0]) omega = 2 * np.pi * omega[1:(nW + 1)] return x, k, t, omega, evolution, spectrum, disp
def getMoments(polyOrder, pre, numInterp): d = postgkyl.GData("%s-sod-shock_neut_M0_1.bp" % pre) dg = postgkyl.GInterpModal(d, polyOrder, "ms", numInterp=numInterp) X, m0 = dg.interpolate() d = postgkyl.GData("%s-sod-shock_neut_M1i_1.bp" % pre) dg = postgkyl.GInterpModal(d, polyOrder, "ms", numInterp=numInterp) X, m1i = dg.interpolate() d = postgkyl.GData("%s-sod-shock_neut_M2_1.bp" % pre) dg = postgkyl.GInterpModal(d, polyOrder, "ms", numInterp=numInterp) X, m2 = dg.interpolate() d = postgkyl.GData("%s-sod-shock_neut_M3i_1.bp" % pre) dg = postgkyl.GInterpModal(d, polyOrder, "ms", numInterp=numInterp) X, m3i = dg.interpolate() u = m1i / m0 # Velocity. nvt2 = m2 - m0 * u**2 # Pressure (density*temperature). q = m3i - (3 * u * m2 - 3 * u**2 * m1i + u**3 * m0 ) # Plasma-frame heat-flux. Xn = X[0] dx = Xn[1] - Xn[0] # Cell-center coordinates. Xc = linspace(Xn[0] + 0.5 * dx, Xn[-1] - 0.5 * dx, Xn.shape[0] - 1) return Xc, m0, u, nvt2, q
def calcEnergyError(pre, p): # Inputs: # pre - prefix for file name. # p - polynomial order (used for assisting in distinguishing between different prefixes). # Outputs: # t - array of time data. # err - normalized error ( abs(err - err[0])/err[0] ) in the total energy. # Read in electron data. d = postgkyl.GData("%s-conservation-test-%s_elc_intM2Thermal.bp" % (pre, p)) elcM2Thermal = 0.5 * elcMass * d.getValues() d = postgkyl.GData("%s-conservation-test-%s_elc_intM2Flow.bp" % (pre, p)) elcM2Flow = 0.5 * elcMass * d.getValues() # Read in proton data. d = postgkyl.GData("%s-conservation-test-%s_ion_intM2Thermal.bp" % (pre, p)) ionM2Thermal = 0.5 * ionMass * d.getValues() d = postgkyl.GData("%s-conservation-test-%s_ion_intM2Flow.bp" % (pre, p)) ionM2Flow = 0.5 * ionMass * d.getValues() # Read in electromagnetic field data. d = postgkyl.GData("%s-conservation-test-%s_fieldEnergy.bp" % (pre, p)) fieldEnergy = 0.5 * epsilon0 * d.getValues() t = d.getGrid()[0] err = calcNormError(elcM2Flow[:, 0] + elcM2Thermal[:, 0] + ionM2Flow[:, 0] + ionM2Thermal[:, 0] + fieldEnergy[:, 0]) return t, err
def getMoments(polyOrder, pre, numInterp): d = postgkyl.GData("%s-sod-shock_neut_M0_1.bp" % pre) dg = postgkyl.GInterpModal(d, polyOrder, "ms", numInterp=numInterp) X, m0 = dg.interpolate() d = postgkyl.GData("%s-sod-shock_neut_M1i_1.bp" % pre) dg = postgkyl.GInterpModal(d, polyOrder, "ms", numInterp=numInterp) X, m1i = dg.interpolate() d = postgkyl.GData("%s-sod-shock_neut_M2_1.bp" % pre) dg = postgkyl.GInterpModal(d, polyOrder, "ms", numInterp=numInterp) X, m2 = dg.interpolate() d = postgkyl.GData("%s-sod-shock_neut_M3i_1.bp" % pre) dg = postgkyl.GInterpModal(d, polyOrder, "ms", numInterp=numInterp) X, m3i = dg.interpolate() u = m1i/m0 # velocity nvt2 = m2 - m0*u**2 # ptcl internal energy q = m3i - (3*u*m2 - 3*u**2*m1i + u**3*m0) # heat-flux Xn = X[0]; dx = Xn[1]-Xn[0] # cell-center coordinates Xc = linspace(Xn[0]+0.5*dx, Xn[-1]-0.5*dx, Xn.shape[0]-1) return Xc, m0, u, nvt2, q
def calcEnergyError(pre): d = postgkyl.GData("%s-relax_neut_intM2Thermal.bp" % pre) m2Thermal = d.getValues() d = postgkyl.GData("%s-relax_neut_intM2Flow.bp" % pre) m2Flow = d.getValues() t = d.getGrid()[0] err = calcNormError(m2Flow + m2Thermal) return t, err
def calcMeanSpect(fn, lo, up): d = postgkyl.GData("%s_00%d.bp" % (fn, lo)) s = d.getValues() g = d.getGrid() dx = g[0][1] - g[0][0] gc = linspace(g[0][0] + 0.5 * dx, g[0][-1] - 0.5 * dx, g[0].shape[0] - 1) for i in range(lo + 1, up + 1): d = postgkyl.GData("%s_00%d.bp" % (fn, i)) s = s + d.getValues() return gc, sqrt(s / (up - lo + 1))
def calc_ke_dke(root_file_name, initFrame, finalFrame, dim, Vol): #function to calculate all the total kinetic energy and the rate of dissipation of KE #root_file_name is the name of the file before the numbers start #initFrame is the first frame and finalFrame is the final frame #dim gives the dimension of the simulation (2 = 2D, 3 = 3D) #Vol = the volume of the grid #returns the kinetic energy and dissipation of KE #calculate integrated kinetic energy ke = np.zeros((1, (finalFrame - initFrame + 1))) dEk = ke f = postgkyl.GData(root_file_name + str(initFrame) + '.bp') grid = f.getGrid() dx = grid[0][1] - grid[0][0] dy = grid[1][1] - grid[1][0] dt = (finalTime - initTime + 1) / (finalFrame - initFrame + 1) r = 0 if dim == 3: dz = grid[2][1] - grid[2][0] elif dim == 2: dz = 1 for c in range(initFrame, finalFrame + 1): frame = postgkyl.GData(root_file_name + "%d.bp" % c) data = frame.getValues() if dim == 2: rho = data[:, :, 0] px = data[:, :, 1] py = data[:, :, 2] pz = data[:, :, 3] elif dim == 3: rho = data[:, :, :, 0] px = data[:, :, :, 1] py = data[:, :, :, 2] pz = data[:, :, :, 3] u = px / (rho * V0) v = py / (rho * V0) w = pz / (rho * V0) e = rho * (u**2 + v**2 + w**2) ke[0, r] = np.sum(e, axis=(0, 1, 2)) * dx * dy * dz * Vol r += 1 r = 0 for i in range(initFrame, finalFrame - 1): dEk[0, r] = -(ke[0, i + 1] - ke[0, i]) / dt r += 1 return ke, dEk
def calcEnergyError(pre): d = postgkyl.GData("%s-sonic-sod-shock_neut_intM2Thermal.bp" % pre) m2Thermal = d.getValues() d = postgkyl.GData("%s-sonic-sod-shock_neut_intM2Flow.bp" % pre) m2Flow = d.getValues() t = d.getGrid()[0] eErr = calcNormError(m2Flow + m2Thermal) d = postgkyl.GData("%s-sonic-sod-shock_neut_intM1i.bp" % pre) m1i = d.getValues() mErr = calcNormError(m1i) return t, eErr, mErr
def getJ(fr): data = pg.GData("%s_elc_M1i_%d.bp" % (baseNm, fr)) dg = pg.data.GInterpModal(data, 2, "ms") XX, m1e = dg.interpolate() data = pg.GData("%s_ion_M1i_%d.bp" % (baseNm, fr)) dg = pg.data.GInterpModal(data, 2, "ms") XX, m1i = dg.interpolate() Jg = 0.159*ones(Ji.shape, float) Xn = XX[0]; dx = Xn[1]-Xn[0] Xc = linspace(Xn[0]+0.5*dx, Xn[-1]-0.5*dx, Xn.shape[0]-1) return Xc, m1i, -m1e+Jg
def getRawGrid(dataFile, **opKey): pgData = pg.GData(dataFile) #.Read data with pgkyl. dimOut = pgData.getNumDims() xNodal = pgData.getGrid() #.If desired, output cell center values of grid coordinates instead of nodal coordinates. if 'location' in opKey: if opKey['location'] == 'center': xOut = [[] for i in range(dimOut)] for i in range(dimOut): nNodes = np.shape(xNodal[i])[0] xOut[i] = np.zeros(nNodes - 1) xOut[i] = np.multiply( 0.5, xNodal[i][0:nNodes - 1] + xNodal[i][1:nNodes]) else: xOut = xNodal else: xOut = xNodal nxOut = np.zeros(dimOut, dtype='int') lxOut = np.zeros(dimOut, dtype='double') dxOut = np.zeros(dimOut, dtype='double') for i in range(dimOut): nxOut[i] = np.size(xOut[i]) lxOut[i] = xOut[i][-1] - xOut[i][0] dxOut[i] = xOut[i][1] - xOut[i][0] return xOut, dimOut, nxOut, lxOut, dxOut
def test_LoadBpFrame(self): data = pg.GData('data/frame_0.bp') nd = data.getNumDims() assert (isinstance(nd, int)) self.assertEqual(nd, 2) nc = data.getNumComps() assert (isinstance(nc, int)) self.assertEqual(nc, 8) lo, up = data.getBounds() assert (isinstance(lo, np.ndarray)) assert (isinstance(up, np.ndarray)) self.assertEqual(len(lo), 2) self.assertEqual(len(up), 2) nc = data.getNumCells() assert (isinstance(nc, np.ndarray)) assert (isinstance(nc[0], np.int64)) assert (isinstance(nc[1], np.int64)) self.assertEqual(nc[0], 64) self.assertEqual(nc[1], 32) grid = data.peakGrid() assert (isinstance(grid, list)) self.assertEqual(len(grid), 2) assert (isinstance(grid[0], np.ndarray)) assert (isinstance(grid[1], np.ndarray)) self.assertEqual(grid[0].shape, (64, )) self.assertEqual(grid[1].shape, (32, )) values = data.peakValues() assert (isinstance(values, np.ndarray)) self.assertEqual(values.shape, (64, 32, 8))
def test_LoadBpHistory(self): data = pg.GData('data/hist_') nd = data.getNumDims() assert (isinstance(nd, int)) self.assertEqual(nd, 1) nc = data.getNumComps() assert (isinstance(nc, int)) self.assertEqual(nc, 8) lo, up = data.getBounds() assert (isinstance(lo, np.ndarray)) assert (isinstance(up, np.ndarray)) self.assertEqual(len(lo), 1) self.assertEqual(len(up), 1) nc = data.getNumCells() assert (isinstance(nc, np.ndarray)) assert (isinstance(nc[0], np.int64)) self.assertEqual(nc[0], 7641) grid = data.peakGrid() assert (isinstance(grid, list)) self.assertEqual(len(grid), 1) assert (isinstance(grid[0], np.ndarray)) self.assertEqual(grid[0].shape, (7641, )) values = data.peakValues() assert (isinstance(values, np.ndarray)) self.assertEqual(values.shape, (7641, 8))
def getGrid(dataFile, p, basisType, **opKey): pgData = pg.GData(dataFile) #.Read data with pgkyl. pgInterp = pg.GInterpModal(pgData, p, basisType) #.Interpolate data. xNodal, dataInterp = pgInterp.interpolate() dimOut = np.shape(xNodal)[0] #.Number of dimensions in data. #.If desired, output cell center values of grid coordinates instead of nodal coordinates. if 'location' in opKey: if opKey['location'] == 'center': xOut = [[] for i in range(dimOut)] for i in range(dimOut): nNodes = np.shape(xNodal[i])[0] xOut[i] = np.zeros(nNodes - 1) xOut[i] = np.multiply( 0.5, xNodal[i][0:nNodes - 1] + xNodal[i][1:nNodes]) else: xOut = xNodal else: xOut = xNodal nxOut = np.zeros(dimOut, dtype='int') lxOut = np.zeros(dimOut, dtype='double') dxOut = np.zeros(dimOut, dtype='double') for i in range(dimOut): nxOut[i] = np.size(xOut[i]) lxOut[i] = xOut[i][-1] - xOut[i][0] dxOut[i] = xOut[i][1] - xOut[i][0] return xOut, dimOut, nxOut, lxOut, dxOut
def getDist(pre, fr): d = postgkyl.GData("%s-bi-maxwellian-relax_neut_%d.bp" % (pre, fr)) dg = postgkyl.GInterpModal(d, 2, "ms") XX, fv = dg.interpolate() X, V = meshgrid(XX[1], XX[2]) return X, V, fv
def plotFig(nr,nc,i,fr, showX=False): print("Working on %d ..." % i) data = pg.GData("s3-oscc-E_ions_%d.bp" % fr) dg = pg.data.GInterpModal(data, 2, "ms") XX, q = dg.interpolate() tm = data.time wx, wy = wR(tm) qEx = fexact(wx, wy) f = subplot(nr,nc,i) pcolormesh(XX[1], XX[2], transpose(q[3,:,:,0])) plot(WX, WY, 'w', linewidth=0.5) if showX: xlabel("$v_x$") ylabel('$v_y$') grid() axis('image') f = subplot(nr,nc,i+1) pcolormesh(VX, VY, qEx) plot(WX, WY, 'w', linewidth=0.5) if showX: xlabel("$v_x$") grid() axis('image')
def getInterpData(dataFile, p, basisType, **opKey): pgData = pg.GData(dataFile) #.Read data with pgkyl. pgInterp = pg.GInterpModal(pgData, p, basisType) #.Interpolate data. if 'comp' in opKey: xOut, dataOut = pgInterp.interpolate(opKey['comp']) else: xOut, dataOut = pgInterp.interpolate() return dataOut
def plotP(p, fName): for i in range(1, 11): print("Woroking on %s_%d.bp ... " % (fName, i)) data = pg.GData("%s_%d.bp" % (fName, i)) T = data.peakGrid() v = data.peakValues() tp, vp = mkPoincare(T[0], v) p.scatter(2 * pi * array(tp) / tper, array(vp), 0.1, marker='.')
def calcMomentumError(pre): d = postgkyl.GData("%s-bi-maxwellian-relax_neut_intM1i_" % pre) m1i = d.getValues() t = d.getGrid()[0] err1 = calcNormError(m1i[:, 0]) err2 = calcNormError(m1i[:, 1]) return t, err1, err2
def getEntropy(polyOrder, pre): svals = zeros((100, ), float) for i in range(0, 100): d = postgkyl.GData("%s-relax_neut_%d.bp" % (pre, i)) dg = postgkyl.GInterpModal(d, polyOrder, "ms") XX, fv = dg.interpolate() svals[i] = calcEntropy(XX[0], XX[1], fv) return svals
def plotFig(i, fr): print("Working on %d ..." % i) data = pg.GData("c4-oscc-E_ions_%d.bp" % fr) dg = pg.data.GInterpModal(data, 2, "ms") XX, q = dg.interpolate() nx2 = int(q.shape[0] / 2) nvx2 = int(q.shape[1] / 2) return q[nx2, nvx2, :, 0]
def calcMomentumError(pre, p): # Inputs: # pre - prefix for file name. # p - polynomial order (used for assisting in distinguishing between different prefixes). # Outputs: # t - array of time data. # err - normalized error ( abs(err - err[0])/err[0] ) in the total momentum. # Read in electron data. d = postgkyl.GData("%s-conservation-test-%s_elc_intM1i.bp" % (pre, p)) elcM1i = elcMass * d.getValues() # Read in proton data. d = postgkyl.GData("%s-conservation-test-%s_ion_intM1i.bp" % (pre, p)) ionM1i = ionMass * d.getValues() t = d.getGrid()[0] err = calcNormError(elcM1i[:, 0] + ionM1i[:, 0]) return t, err
def plotFig(i,fr): print("Working on %d ..." % i) data = pg.GData("c5-oscc-E_ions_%d.bp" % fr) dg = pg.data.GInterpModal(data, 2, "ms") XX, q = dg.interpolate() qsum = sum(q,axis=0) nvx2 = int(q.shape[1]/2) return qsum[nvx2,:].squeeze()
def plotFig(i, fr): print("Working on %d ..." % i) figure(i, figsize=(14, 8)) data_fieldEnergy = pg.GData("kink_fieldEnergy_") t = data_fieldEnergy.getGrid()[0] fieldEnergy = data_fieldEnergy.getValues() ExEnergy = 0.5 * fieldEnergy[:, 0] omegaCi = 0.0055277079839257 t = t * 0.0055277079839257 #param, R2, N = fitGrowth(t[40000:50000], ExEnergy[40000:50000]) subplot(1, 2, 1) semilogy(t, ExEnergy, "k") xlabel("$t (\Omega_{ci}^{-1})$") ylabel("$\int E_x^2$") xlim(0, 8) ylim(1e-12, 1e-2) data_field = pg.GData("kink_field_%d.bp" % fr) dg_field = pg.data.GInterpModal(data_field, 2, "ms") XX, Ex = dg_field.interpolate(0) #center the grid values for d in range(2): XX[d] = 0.5 * (XX[d][:-1] + XX[d][1:]) #normalization for electric field ionMass = 36 norm = (omegaCi * ionMass)**2 / math.sqrt(ionMass) #computing ion larmor radius vtIon = 0.0316227833333333 rhoi = vtIon / omegaCi subplot(1, 2, 2) pcolormesh(XX[0] / rhoi, XX[1] / rhoi, Ex[:, :, 0].transpose() / norm, cmap="seismic", shading="gouraud") colorbar(label="$E_x$") xlabel(r"$X(\rho_i)$") ylabel(r"$Y(\rho_i)$") title(r"$t=6 \Omega_{ci}^{-1}$") tight_layout() savefig("lhdi-ex-energy-and-ex.pdf")
def plotFig1D(i, lbl): print("Working on %d ..." % i) data = pg.GData("c6-oscc-E_ions_%d.bp" % i) dg = pg.data.GInterpModal(data, 2, "ms") XX, q = dg.interpolate() numCells = q.shape qSum = sum(q, axis=0)*(2.0*pi)/numCells[0] plot(XX[1], qSum[:,int(numCells[2]/2),0], label=lbl) grid()
def getExpRecon(pre, fr): d = postgkyl.GData("%s-relax_neut_%d.bp" % (pre, fr)) q = d.getValues() f0 = q[0, :, 0] f1 = q[0, :, 2] gcoeff = zeros((f0.shape[0], 2), float) for i in range(f0.shape[0]): gcoeff[i][0], gcoeff[i][1] = fitExp(f0[i], f1[i]) return gcoeff
def calcL2Error(pre, p): # Inputs: # pre - prefix for file name. # p - polynomial order (used for assisting in distinguishing between different prefixes). # Outputs: # t - array of time data. # err_elcL2 - normalized error ( abs(err - err[0])/err[0] ) in the electron L^2 norm (f_e^2). # ion_elcL2 - normalized error ( abs(err - err[0])/err[0] ) in the proton L^2 norm (f_p^2). # Read in electron data. d = postgkyl.GData("%s-conservation-test-%s_elc_intL2.bp" % (pre, p)) elcL2 = d.getValues() # Read in proton data. d = postgkyl.GData("%s-conservation-test-%s_ion_intL2.bp" % (pre, p)) ionL2 = d.getValues() t = d.getGrid()[0] err_elcL2 = calcNormError(elcL2[:, 0]) err_ionL2 = calcNormError(ionL2[:, 0]) return t, err_elcL2, err_ionL2
def getPhi(fr): data = pg.GData("n1-es-buneman_field_%d.bp" % fr) dg = pg.data.GInterpModal(data, 2, "ms") XX, E = dg.interpolate() X1 = XX[0] dx = X1[1] - X1[0] phi = numpy.zeros((E.shape[0] + 1, 1), numpy.float) for i in range(1, phi.shape[0]): phi[i] = phi[i - 1] - dx * E[i - 1] return phi - 0 * min(phi)
def plotFig(nr,nc,i,fr): print("Working on %d ..." % i) data = pg.GData("c6-oscc-E_ions_%d.bp" % fr) dg = pg.data.GInterpModal(data, 2, "ms") XX, q = dg.interpolate() qSum = sum(q, axis=0) subplot(nr,nc,i) pcolormesh(XX[1], XX[2], transpose(qSum[:,:,0])) grid() axis('image')
def getData(i): #print("Working on %d ..." % i) data = pg.GData("s4-oscc-E_ions_%d.bp" % i) dg = pg.data.GInterpModal(data, 2, "ms") XX, q = dg.interpolate() nx, nvx, nvy = q.shape[0], q.shape[1], q.shape[2] qMid = q[int(nx/2),:,int(nvy/2)] tm = data.time tm = tm - tper*floor(tm/tper) return tm, qMid
def plotFig(i): print("Working on %d ..." % i) data = pg.GData("c6-oscc-E_ions_M0_%d.bp" % i) dg = pg.data.GInterpModal(data, 2, "ms") XX, q0 = dg.interpolate() data = pg.GData("c6-oscc-E_ions_M1i_%d.bp" % i) dg = pg.data.GInterpModal(data, 2, "ms") XX, nux = dg.interpolate(0) XX, nuy = dg.interpolate(1) ux = nux/q0 uy = nuy/q0 data = pg.GData("c6-oscc-E_ions_M2_%d.bp" % i) dg = pg.data.GInterpModal(data, 2, "ms") XX, q2 = dg.interpolate() dx = 1/q2.shape[0] vt = (q2-q0*(ux**2+uy**2))*0.5 return dx*vt.sum()