@@ -370,57 +370,99 @@ def printSpaceDistr(self):
370370 from qmat .lagrange import LagrangeApproximation
371371 import matplotlib .pyplot as plt
372372
373- dirName = "test_F2 "
373+ dirName = "test_M4_R1 "
374374
375375 problem = RBCProblem2D .runSimulation (
376- dirName , 100 , 1e-2 / 2 , logEvery = 20 , dtWrite = 1.0 ,
377- meshRatio = 1 , resFactor = 2 )
376+ dirName , 140 , 1e-2 / 2 , logEvery = 20 , dtWrite = 1.0 ,
377+ meshRatio = 4 , resFactor = 1 )
378378
379379 output = OutputFiles (dirName )
380380 approx = LagrangeApproximation (output .z )
381- uAll = u , w = output .vData (0 )[- 1 ]
381+
382+ uAll = output .vData (0 )[:]
383+ bAll = output .bData (0 )[:]
384+ u , w = uAll [:, 0 ], uAll [:, 1 ]
382385 nX , nZ = output .nX , output .nZ
383386
384387 # Kinetic energy
385388 mIz = approx .getIntegrationMatrix ([(0 , 1 )])
386- ke = (u - u .mean ())** 2 + (w - w .mean ())** 2
387- keMean = (mIz @ ke .T ).mean ()
388- # 2D spectrum
389- uAll = uAll - uAll .mean (axis = (- 2 ,- 1 ))[:, None , None ]
390- mPz = approx .getInterpolationMatrix (np .linspace (0 , 1 , nZ , endpoint = False ))
391- uReg = (mPz @ uAll [:, :, :, None ])[..., 0 ]
392- uHat = np .fft .fft2 (uReg )
393- reParts = [uF .ravel ().real ** 2 for uF in uHat ]
394- imParts = [uF .ravel ().imag ** 2 for uF in uHat ]
395-
396-
397- kX = np .fft .fftfreq (nX , 1 / nX )** 2
398- kZ = np .fft .fftfreq (nZ , 1 / nZ )** 2
399-
400- ell = kX [:, None ]/ (nX // 2 )** 2 + kZ [None , :]/ (nZ // 2 )** 2
401-
402- kMod = kX [:, None ] + kZ [None , :]
403- kMod **= 0.5
404-
405- idx = kMod .copy ()
406- idx *= (ell < 1 )
407- idx -= (ell >= 1 )
408- idxList = range (int (idx .max ()) + 1 )
409- flatIdx = idx .ravel ()
410-
411- spectrum = np .zeros (len (idxList ))
412- for i in idxList :
413- kIdx = np .argwhere (flatIdx == i )
414- tmp = np .empty (kIdx .shape )
415- for re , im in zip (reParts , imParts ):
416- np .copyto (tmp , re [kIdx ])
417- tmp += im [kIdx ]
418- spectrum [i ] += tmp .sum ()
419- spectrum /= 2 * (nX * nZ )** 2
420- wavenumbers = list (i + 0.5 for i in idxList )
421-
422- plt .figure ("spectrum" )
423- plt .loglog (wavenumbers , spectrum )
424-
425- plt .figure ("contour" )
426- plt .pcolormesh (output .x , output .z , ke .T )
389+ ke = (u - u .mean (axis = (- 1 , - 2 ))[:, None , None ])** 2 \
390+ + (w - w .mean (axis = (- 1 , - 2 ))[:, None , None ])** 2
391+
392+ nThrow = 20
393+ keProfile = ke [nThrow :].mean (axis = (0 , 1 ))
394+ plt .figure ("keProfile" )
395+ plt .plot (keProfile , output .z , label = dirName )
396+ plt .legend ()
397+
398+
399+ # keMean = np.sum(mIz*ke, axis=-1).mean(axis=-1)
400+ # plt.figure("ke")
401+ # plt.plot(keMean, label=dirName)
402+ # plt.legend()
403+
404+ # Removing constant component
405+ # uAll = uAll - uAll.mean(axis=(-2,-1))[:, None, None]
406+
407+ # # 2D spectrum
408+ # mPz = approx.getInterpolationMatrix(np.linspace(0, 1, nZ, endpoint=False))
409+ # uReg = (mPz @ uAll[:, :, :, None])[..., 0]
410+ # uHat = np.fft.fft2(uReg)
411+ # reParts = [uF.ravel().real**2 for uF in uHat]
412+ # imParts = [uF.ravel().imag**2 for uF in uHat]
413+
414+
415+ # kX = np.fft.fftfreq(nX, 1/nX)**2
416+ # kZ = np.fft.fftfreq(nZ, 1/nZ)**2
417+
418+ # ell = kX[:, None]/(nX//2)**2 + kZ[None, :]/(nZ//2)**2
419+
420+ # kMod = kX[:, None] + kZ[None, :]
421+ # kMod **= 0.5
422+
423+ # idx = kMod.copy()
424+ # idx *= (ell < 1)
425+ # idx -= (ell >= 1)
426+ # idxList = range(int(idx.max()) + 1)
427+ # flatIdx = idx.ravel()
428+
429+ # spectrum = np.zeros(len(idxList))
430+ # for i in idxList:
431+ # kIdx = np.argwhere(flatIdx == i)
432+ # tmp = np.empty(kIdx.shape)
433+ # for re, im in zip(reParts, imParts):
434+ # np.copyto(tmp, re[kIdx])
435+ # tmp += im[kIdx]
436+ # spectrum[i] += tmp.sum()
437+ # spectrum /= 2*(nX*nZ)**2
438+ # wavenumbers = list(i + 0.5 for i in idxList)
439+
440+ # plt.figure("spectrum-2D")
441+ # plt.loglog(wavenumbers, spectrum)
442+
443+
444+ # 1D spectrum (average)
445+ spectrum1D = []
446+ for i in range (2 ):
447+ u = uAll [:, i ] # (nT, Nx, Nz)
448+ s = np .fft .rfft (u , axis = - 2 ) # over Nx --> (nT, Kx, Nz)
449+ s *= np .conj (s ) # (nT, Kx, Nz)
450+ s = np .sum (mIz * s .real , axis = - 1 ) # integrate over Nz --> (nT, Kx)
451+ s = s [40 :].mean (axis = 0 ) # mean over nT -> (Kx,)
452+ s /= nX ** 2
453+
454+ spectrum1D .append (s )
455+ waveNum = np .fft .rfftfreq (nX , 1 / nX )+ 0.5
456+
457+ plt .figure ("spectrum-1D" )
458+ plt .loglog (waveNum , spectrum1D [0 ], label = "ux" )
459+ plt .loglog (waveNum , spectrum1D [1 ], label = "uz" )
460+ plt .loglog (waveNum , waveNum ** (- 5 / 3 ), '--k' )
461+ plt .legend ()
462+
463+ print (f"iS[ux] : { float (spectrum1D [0 ].sum ())} " )
464+ print (f"iS[uz] : { float (spectrum1D [1 ].sum ())} " )
465+
466+ plt .figure (f"contour-{ dirName } " )
467+ plt .pcolormesh (output .x , output .z , ke [- 1 ].T )
468+ plt .colorbar ()
0 commit comments