def __init__(self, tag, field='Br', precision='Float32', avg=False): """ :param tag: if you specify a pattern, it tries to read the corresponding files and stack them. :type tag: str :param field: nature of the radial spectra. Possible choices are 'Bt' or 'Bp' :type field: str :param precision: single or double precision (default single, i.e. 'Float32') :type precision: str :param avg: when set to True, display time averaged quantities :type avg: bool """ logFiles = scanDir('log.*') if len(logFiles) != 0: MagicSetup.__init__(self, quiet=True, nml=logFiles[-1]) self.n_r_max = int(self.n_r_max) self.n_r_ic_max = int(self.n_r_ic_max) else: n_r_max = 'n_r_max ?\n' self.n_r_max = int(input(str1)) n_r_ic_max = 'n_r_ic_max ?\n' self.n_r_ic_max = int(input(str1)) str1 = 'Aspect ratio ?\n' self.radratio = float(input(str1)) self.n_r_tot = self.n_r_max+self.n_r_ic_max self.ricb = self.radratio/(1.-self.radratio) self.rcmb = 1./(1.-self.radratio) outerCoreGrid = chebgrid(self.n_r_max-1, self.rcmb, self.ricb) n_r_ic_tot = 2*self.n_r_ic_max-1 innerCoreGrid = chebgrid(n_r_ic_tot-1, self.ricb, -self.ricb) self.radius = np.zeros((self.n_r_tot-1), dtype=precision) self.radius[:self.n_r_max] = outerCoreGrid self.radius[self.n_r_max-1:] = innerCoreGrid[:self.n_r_ic_max] pattern = 'r%s' % field +'Spec' files = scanDir('%s.%s' % (pattern, tag)) # Read the rB[rp]Spec.TAG files (stack them) data = [] for k, file in enumerate(files): print('Reading %s' % file) f = npfile(file, endian='B') while 1: try: data.append(f.fort_read(precision)) except TypeError: break data = np.array(data, dtype=precision) # Time (every two lines only) self.time = data[::2, 0] # Poloidal/Toroidal energy for all radii for the 6 first spherical harmonic # degrees self.e_pol = np.zeros((len(self.time), self.n_r_tot-1, 6), dtype=precision) self.e_pol_axi = np.zeros_like(self.e_pol) self.e_pol[:, :, 0] = data[::2, 1:self.n_r_tot] self.e_pol[:, :, 1] = data[::2, self.n_r_tot-1:2*(self.n_r_tot-1)] self.e_pol[:, :, 2] = data[::2, 2*(self.n_r_tot-1):3*(self.n_r_tot-1)] self.e_pol[:, :, 3] = data[::2, 3*(self.n_r_tot-1):4*(self.n_r_tot-1)] self.e_pol[:, :, 4] = data[::2, 4*(self.n_r_tot-1):5*(self.n_r_tot-1)] self.e_pol[:, :, 5] = data[::2, 5*(self.n_r_tot-1):6*(self.n_r_tot-1)] self.e_pol_axi[:, :, 0] = data[1::2, 1:self.n_r_tot] self.e_pol_axi[:, :, 1] = data[1::2, self.n_r_tot-1:2*(self.n_r_tot-1)] self.e_pol_axi[:, :, 2] = data[1::2, 2*(self.n_r_tot-1):3*(self.n_r_tot-1)] self.e_pol_axi[:, :, 3] = data[1::2, 3*(self.n_r_tot-1):4*(self.n_r_tot-1)] self.e_pol_axi[:, :, 4] = data[1::2, 4*(self.n_r_tot-1):5*(self.n_r_tot-1)] self.e_pol_axi[:, :, 5] = data[1::2, 5*(self.n_r_tot-1):6*(self.n_r_tot-1)] if avg: self.plotAvg()
def __init__(self, tag, field='Br', precision=np.float32, avg=False): """ :param tag: if you specify a pattern, it tries to read the corresponding files and stack them. :type tag: str :param field: nature of the radial spectra. Possible choices are 'Bt' or 'Bp' :type field: str :param precision: single or double precision (default single, i.e. np.float32) :type precision: str :param avg: when set to True, display time averaged quantities :type avg: bool """ logFiles = scanDir('log.*') if len(logFiles) != 0: MagicSetup.__init__(self, quiet=True, nml=logFiles[-1]) self.n_r_max = int(self.n_r_max) self.n_r_ic_max = int(self.n_r_ic_max) else: n_r_max = 'n_r_max ?\n' self.n_r_max = int(input(str1)) n_r_ic_max = 'n_r_ic_max ?\n' self.n_r_ic_max = int(input(str1)) str1 = 'Aspect ratio ?\n' self.radratio = float(input(str1)) self.n_r_tot = self.n_r_max + self.n_r_ic_max self.ricb = self.radratio / (1. - self.radratio) self.rcmb = 1. / (1. - self.radratio) outerCoreGrid = chebgrid(self.n_r_max - 1, self.rcmb, self.ricb) n_r_ic_tot = 2 * self.n_r_ic_max - 1 innerCoreGrid = chebgrid(n_r_ic_tot - 1, self.ricb, -self.ricb) self.radius = np.zeros((self.n_r_tot - 1), dtype=precision) self.radius[:self.n_r_max] = outerCoreGrid self.radius[self.n_r_max - 1:] = innerCoreGrid[:self.n_r_ic_max] pattern = 'r{}'.format(field) + 'Spec' files = scanDir('{}.{}'.format(pattern, tag)) # Read the rB[rp]Spec.TAG files (stack them) data = [] for k, file in enumerate(files): print('Reading {}'.format(file)) f = npfile(file, endian='B') while 1: try: data.append(f.fort_read(precision)) except TypeError: break data = np.array(data, dtype=precision) # Time (every two lines only) self.time = data[::2, 0] # Poloidal/Toroidal energy for all radii for the 6 first spherical harmonic # degrees self.e_pol = np.zeros((len(self.time), self.n_r_tot - 1, 6), dtype=precision) self.e_pol_axi = np.zeros_like(self.e_pol) self.e_pol[:, :, 0] = data[::2, 1:self.n_r_tot] self.e_pol[:, :, 1] = data[::2, self.n_r_tot - 1:2 * (self.n_r_tot - 1)] self.e_pol[:, :, 2] = data[::2, 2 * (self.n_r_tot - 1):3 * (self.n_r_tot - 1)] self.e_pol[:, :, 3] = data[::2, 3 * (self.n_r_tot - 1):4 * (self.n_r_tot - 1)] self.e_pol[:, :, 4] = data[::2, 4 * (self.n_r_tot - 1):5 * (self.n_r_tot - 1)] self.e_pol[:, :, 5] = data[::2, 5 * (self.n_r_tot - 1):6 * (self.n_r_tot - 1)] self.e_pol_axi[:, :, 0] = data[1::2, 1:self.n_r_tot] self.e_pol_axi[:, :, 1] = data[1::2, self.n_r_tot - 1:2 * (self.n_r_tot - 1)] self.e_pol_axi[:, :, 2] = data[1::2, 2 * (self.n_r_tot - 1):3 * (self.n_r_tot - 1)] self.e_pol_axi[:, :, 3] = data[1::2, 3 * (self.n_r_tot - 1):4 * (self.n_r_tot - 1)] self.e_pol_axi[:, :, 4] = data[1::2, 4 * (self.n_r_tot - 1):5 * (self.n_r_tot - 1)] self.e_pol_axi[:, :, 5] = data[1::2, 5 * (self.n_r_tot - 1):6 * (self.n_r_tot - 1)] if avg: self.plotAvg()
def __init__(self, iplot=False, angle=10, pickleName='thHeat.pickle'): """ :param iplot: a boolean to toggle the plots on/off :type iplot: bool :param angle: the integration angle in degrees :type angle: float :pickleName: calculations a """ angle = angle * np.pi / 180 if os.path.exists('tInitAvg'): file = open('tInitAvg', 'r') tstart = float(file.readline()) file.close() logFiles = scanDir('log.*') tags = [] for lg in logFiles: nml = MagicSetup(quiet=True, nml=lg) if nml.start_time > tstart: if os.path.exists('bLayersR.%s' % nml.tag): tags.append(nml.tag) if len(tags) == 0: tags = [nml.tag] print('Only 1 tag: %s' % tags) MagicSetup.__init__(self, quiet=True, nml=logFiles[-1]) a = AvgField() self.nuss = a.nuss else: logFiles = scanDir('log.*') MagicSetup.__init__(self, quiet=True, nml=logFiles[-1]) if not os.path.exists(pickleName): # reading ATmov k = 0 for tag in tags: file = 'ATmov.%s' % tag if os.path.exists(file): if k == 0: m = Movie(file=file, iplot=False) print(file) else: m += Movie(file=file, iplot=False) print(file) k += 1 # reading AHF_mov kk = 0 for tag in tags: file = 'AHF_mov.%s' % tag if os.path.exists(file): if kk == 0: m1 = Movie(file=file, iplot=False) print(file) else: m1 += Movie(file=file, iplot=False) print(file) kk += 1 self.tempmean = m.data[0, ...].mean(axis=0) self.tempstd = m.data[0, ...].std(axis=0) self.colat = m.theta if kk > 0: # i.e. at least one AHF_mov file has been found self.fluxmean = m1.data[0, ...].mean(axis=0) self.fluxstd = m1.data[0, ...].std(axis=0) else: self.fluxmean = rderavg(self.tempmean, eta=self.radratio, exclude=False, spectral=False) self.fluxstd = rderavg(self.tempstd, eta=self.radratio, exclude=False, spectral=False) # Pickle saving file = open(pickleName, 'wb') pickle.dump([self.colat, self.tempmean, self.tempstd,\ self.fluxmean, self.fluxstd], file) file.close() else: file = open(pickleName, 'r') dat = pickle.load(file) if len(dat) == 5: self.colat, self.tempmean, self.tempstd, \ self.fluxmean, self.fluxstd = dat else: self.colat, self.tempmean, self.fluxmean = dat self.fluxstd = np.zeros_like(self.fluxmean) self.tempstd = np.zeros_like(self.fluxmean) file.close() self.ri = self.radratio/(1.-self.radratio) self.ro = 1./(1.-self.radratio) self.ntheta, self.nr = self.tempmean.shape self.radius = chebgrid(self.nr-1, self.ro, self.ri) th2D = np.zeros((self.ntheta, self.nr), dtype=self.radius.dtype) #self.colat = np.linspace(0., np.pi, self.ntheta) for i in range(self.ntheta): th2D[i, :] = self.colat[i] self.temprmmean = 0.5*simps(self.tempmean*np.sin(th2D), th2D, axis=0) self.temprmstd = 0.5*simps(self.tempstd*np.sin(th2D), th2D, axis=0) sinTh = np.sin(self.colat) d1 = matder(self.nr-1, self.ro, self.ri) # Conducting temperature profile (Boussinesq only!) self.tcond = self.ri*self.ro/self.radius-self.ri+self.temprmmean[0] self.fcond = -self.ri*self.ro/self.radius**2 self.nusstopmean = self.fluxmean[:, 0] / self.fcond[0] self.nussbotmean = self.fluxmean[:, -1] / self.fcond[-1] self.nusstopstd = self.fluxstd[:, 0] / self.fcond[0] self.nussbotstd = self.fluxstd[:, -1] / self.fcond[-1] # Close to the equator mask2D = (th2D>=np.pi/2.-angle/2.)*(th2D<=np.pi/2+angle/2.) mask = (self.colat>=np.pi/2.-angle/2.)*(self.colat<=np.pi/2+angle/2.) fac = 1./simps(sinTh[mask], self.colat[mask]) self.nussBotEq = fac*simps(self.nussbotmean[mask]*sinTh[mask], self.colat[mask]) self.nussTopEq = fac*simps(self.nusstopmean[mask]*sinTh[mask], self.colat[mask]) sinC = sinTh.copy() sinC[~mask] = 0. fac = 1./simps(sinC, self.colat) tempC = self.tempmean.copy() tempC[~mask2D] = 0. self.tempEqmean = fac*simps(tempC*np.sin(th2D), th2D, axis=0) tempC = self.tempstd.copy() tempC[~mask2D] = 0. self.tempEqstd = fac*simps(tempC*np.sin(th2D), th2D, axis=0) dtempEq = np.dot(d1, self.tempEqmean) self.betaEq = dtempEq[self.nr/2] # 45\deg inclination mask2D = (th2D>=np.pi/4.-angle/2.)*(th2D<=np.pi/4+angle/2.) mask = (self.colat>=np.pi/4.-angle/2.)*(self.colat<=np.pi/4+angle/2.) fac = 1./simps(np.sin(self.colat[mask]), self.colat[mask]) nussBot45NH = fac*simps(self.nussbotmean[mask]*sinTh[mask], self.colat[mask]) nussTop45NH = fac*simps(self.nusstopmean[mask]*sinTh[mask], self.colat[mask]) sinC = sinTh.copy() sinC[~mask] = 0. fac = 1./simps(sinC, self.colat) tempC = self.tempmean.copy() tempC[~mask2D] = 0. temp45NH = fac*simps(tempC*np.sin(th2D), th2D, axis=0) mask2D = (th2D>=3.*np.pi/4.-angle/2.)*(th2D<=3.*np.pi/4+angle/2.) mask = (self.colat>=3.*np.pi/4.-angle/2.)*(self.colat<=3.*np.pi/4+angle/2.) fac = 1./simps(np.sin(self.colat[mask]), self.colat[mask]) nussBot45SH = fac*simps(self.nussbotmean[mask]*sinTh[mask], self.colat[mask]) nussTop45SH = fac*simps(self.nusstopmean[mask]*sinTh[mask], self.colat[mask]) sinC = sinTh.copy() sinC[~mask] = 0. fac = 1./simps(sinC, self.colat) tempC = self.tempmean.copy() tempC[~mask2D] = 0. temp45SH = fac*simps(tempC*np.sin(th2D), th2D, axis=0) self.nussTop45 = 0.5*(nussTop45NH+nussTop45SH) self.nussBot45 = 0.5*(nussBot45NH+nussBot45SH) self.temp45 = 0.5*(temp45NH+temp45SH) dtemp45 = np.dot(d1, self.temp45) self.beta45 = dtemp45[self.nr/2] # Polar regions mask2D = (th2D<=angle/2.) mask = (self.colat<=angle/2.) fac = 1./simps(np.sin(self.colat[mask]), self.colat[mask]) nussBotPoNH = fac*simps(self.nussbotmean[mask]*sinTh[mask], self.colat[mask]) nussTopPoNH = fac*simps(self.nusstopmean[mask]*sinTh[mask], self.colat[mask]) sinC = sinTh.copy() sinC[~mask] = 0. fac = 1./simps(sinC, self.colat) tempC = self.tempmean.copy() tempC[~mask2D] = 0. tempPolNHmean = fac*simps(tempC*np.sin(th2D), th2D, axis=0) tempC = self.tempstd.copy() tempC[~mask2D] = 0. tempPolNHstd = fac*simps(tempC*np.sin(th2D), th2D, axis=0) mask2D = (th2D>=np.pi-angle/2.) mask = (self.colat>=np.pi-angle/2.) fac = 1./simps(np.sin(self.colat[mask]), self.colat[mask]) nussBotPoSH = fac*simps(self.nussbotmean[mask]*sinTh[mask], self.colat[mask]) nussTopPoSH = fac*simps(self.nusstopmean[mask]*sinTh[mask], self.colat[mask]) sinC = sinTh.copy() sinC[~mask] = 0. fac = 1./simps(sinC, self.colat) tempC = self.tempmean.copy() tempC[~mask2D] = 0. tempPolSHmean = fac*simps(tempC*np.sin(th2D), th2D, axis=0) tempC = self.tempstd.copy() tempC[~mask2D] = 0. tempPolSHstd = fac*simps(tempC*np.sin(th2D), th2D, axis=0) self.nussBotPo = 0.5*(nussBotPoNH+nussBotPoSH) self.nussTopPo = 0.5*(nussTopPoNH+nussTopPoSH) self.tempPolmean = 0.5*(tempPolNHmean+tempPolSHmean) self.tempPolstd= 0.5*(tempPolNHstd+tempPolSHstd) dtempPol = np.dot(d1, self.tempPolmean) self.betaPol = dtempPol[self.nr/2] # Inside and outside TC angleTC = np.arcsin(self.ri/self.ro) mask2D = (th2D<=angleTC) mask = (self.colat<=angleTC) fac = 1./simps(np.sin(self.colat[mask]), self.colat[mask]) nussITC_NH = fac*simps(self.nusstopmean[mask]*sinTh[mask], self.colat[mask]) mask2D = (th2D>=np.pi-angleTC) mask = (self.colat>=np.pi-angleTC) fac = 1./simps(np.sin(self.colat[mask]), self.colat[mask]) nussITC_SH = fac*simps(self.nusstopmean[mask]*sinTh[mask], self.colat[mask]) self.nussITC = 0.5*(nussITC_NH+nussITC_SH) mask2D = (th2D>=angleTC)*(th2D<=np.pi-angleTC) mask = (self.colat>=angleTC)*(self.colat<=np.pi-angleTC) fac = 1./simps(sinTh[mask], self.colat[mask]) self.nussOTC = fac*simps(self.nusstopmean[mask]*sinTh[mask], self.colat[mask]) if iplot: self.plot() print(self)
def __init__(self, iplot=False, angle=10, pickleName='thHeat.pickle'): """ :param iplot: a boolean to toggle the plots on/off :type iplot: bool :param angle: the integration angle in degrees :type angle: float :pickleName: calculations a """ angle = angle * np.pi / 180 if os.path.exists('tInitAvg'): file = open('tInitAvg', 'r') tstart = float(file.readline()) file.close() logFiles = scanDir('log.*') tags = [] for lg in logFiles: nml = MagicSetup(quiet=True, nml=lg) if nml.start_time > tstart: if os.path.exists('bLayersR.%s' % nml.tag): tags.append(nml.tag) if len(tags) == 0: tags = [nml.tag] print('Only 1 tag: %s' % tags) MagicSetup.__init__(self, quiet=True, nml=logFiles[-1]) a = AvgField() self.nuss = a.nuss else: logFiles = scanDir('log.*') MagicSetup.__init__(self, quiet=True, nml=logFiles[-1]) if not os.path.exists(pickleName): # reading ATmov k = 0 for tag in tags: file = 'ATmov.%s' % tag if os.path.exists(file): if k == 0: m = Movie(file=file, iplot=False) print(file) else: m += Movie(file=file, iplot=False) print(file) k += 1 # reading AHF_mov kk = 0 for tag in tags: file = 'AHF_mov.%s' % tag if os.path.exists(file): if kk == 0: m1 = Movie(file=file, iplot=False) print(file) else: m1 += Movie(file=file, iplot=False) print(file) kk += 1 self.tempmean = m.data.mean(axis=0) self.colat = m.theta if kk > 0: # i.e. at least one AHF_mov file has been found self.flux = m1.data.mean(axis=0) else: self.flux = rderavg(self.tempmean, eta=self.radratio, exclude=False, spectral=False) # Pickle saving file = open(pickleName, 'wb') pickle.dump([self.colat, self.tempmean, self.flux], file) file.close() else: file = open(pickleName, 'r') self.colat, self.tempmean, self.flux = pickle.load(file) file.close() self.ri = self.radratio/(1.-self.radratio) self.ro = 1./(1.-self.radratio) self.ntheta, self.nr = self.tempmean.shape self.radius = chebgrid(self.nr-1, self.ro, self.ri) th2D = np.zeros((self.ntheta, self.nr), dtype=self.radius.dtype) #self.colat = np.linspace(0., np.pi, self.ntheta) for i in range(self.ntheta): th2D[i, :] = self.colat[i] self.temprm = 0.5*simps(self.tempmean*np.sin(th2D), th2D, axis=0) sinTh = np.sin(self.colat) d1 = matder(self.nr-1, self.ro, self.ri) # Conducting temperature profile (Boussinesq only!) self.tcond = self.ri*self.ro/self.radius-self.ri+self.temprm[0] self.fcond = -self.ri*self.ro/self.radius**2 self.nusstop = self.flux[:, 0] / self.fcond[0] self.nussbot = self.flux[:, -1] / self.fcond[-1] # Close to the equator mask2D = (th2D>=np.pi/2.-angle/2.)*(th2D<=np.pi/2+angle/2.) mask = (self.colat>=np.pi/2.-angle/2.)*(self.colat<=np.pi/2+angle/2.) fac = 1./simps(sinTh[mask], self.colat[mask]) self.nussBotEq = fac*simps(self.nussbot[mask]*sinTh[mask], self.colat[mask]) self.nussTopEq = fac*simps(self.nusstop[mask]*sinTh[mask], self.colat[mask]) sinC = sinTh.copy() sinC[~mask] = 0. fac = 1./simps(sinC, self.colat) tempC = self.tempmean.copy() tempC[~mask2D] = 0. self.tempEq = fac*simps(tempC*np.sin(th2D), th2D, axis=0) dtempEq = np.dot(d1, self.tempEq) self.betaEq = dtempEq[self.nr/2] # 45\deg inclination mask2D = (th2D>=np.pi/4.-angle/2.)*(th2D<=np.pi/4+angle/2.) mask = (self.colat>=np.pi/4.-angle/2.)*(self.colat<=np.pi/4+angle/2.) fac = 1./simps(np.sin(self.colat[mask]), self.colat[mask]) nussBot45NH = fac*simps(self.nussbot[mask]*sinTh[mask], self.colat[mask]) nussTop45NH = fac*simps(self.nusstop[mask]*sinTh[mask], self.colat[mask]) sinC = sinTh.copy() sinC[~mask] = 0. fac = 1./simps(sinC, self.colat) tempC = self.tempmean.copy() tempC[~mask2D] = 0. temp45NH = fac*simps(tempC*np.sin(th2D), th2D, axis=0) mask2D = (th2D>=3.*np.pi/4.-angle/2.)*(th2D<=3.*np.pi/4+angle/2.) mask = (self.colat>=3.*np.pi/4.-angle/2.)*(self.colat<=3.*np.pi/4+angle/2.) fac = 1./simps(np.sin(self.colat[mask]), self.colat[mask]) nussBot45SH = fac*simps(self.nussbot[mask]*sinTh[mask], self.colat[mask]) nussTop45SH = fac*simps(self.nusstop[mask]*sinTh[mask], self.colat[mask]) sinC = sinTh.copy() sinC[~mask] = 0. fac = 1./simps(sinC, self.colat) tempC = self.tempmean.copy() tempC[~mask2D] = 0. temp45SH = fac*simps(tempC*np.sin(th2D), th2D, axis=0) self.nussTop45 = 0.5*(nussTop45NH+nussTop45SH) self.nussBot45 = 0.5*(nussBot45NH+nussBot45SH) self.temp45 = 0.5*(temp45NH+temp45SH) dtemp45 = np.dot(d1, self.temp45) self.beta45 = dtemp45[self.nr/2] # Polar regions mask2D = (th2D<=angle/2.) mask = (self.colat<=angle/2.) fac = 1./simps(np.sin(self.colat[mask]), self.colat[mask]) nussBotPoNH = fac*simps(self.nussbot[mask]*sinTh[mask], self.colat[mask]) nussTopPoNH = fac*simps(self.nusstop[mask]*sinTh[mask], self.colat[mask]) sinC = sinTh.copy() sinC[~mask] = 0. fac = 1./simps(sinC, self.colat) tempC = self.tempmean.copy() tempC[~mask2D] = 0. tempPolNH = fac*simps(tempC*np.sin(th2D), th2D, axis=0) mask2D = (th2D>=np.pi-angle/2.) mask = (self.colat>=np.pi-angle/2.) fac = 1./simps(np.sin(self.colat[mask]), self.colat[mask]) nussBotPoSH = fac*simps(self.nussbot[mask]*sinTh[mask], self.colat[mask]) nussTopPoSH = fac*simps(self.nusstop[mask]*sinTh[mask], self.colat[mask]) sinC = sinTh.copy() sinC[~mask] = 0. fac = 1./simps(sinC, self.colat) tempC = self.tempmean.copy() tempC[~mask2D] = 0. tempPolSH = fac*simps(tempC*np.sin(th2D), th2D, axis=0) self.nussBotPo = 0.5*(nussBotPoNH+nussBotPoSH) self.nussTopPo = 0.5*(nussTopPoNH+nussTopPoSH) self.tempPol = 0.5*(tempPolNH+tempPolSH) dtempPol = np.dot(d1, self.tempPol) self.betaPol = dtempPol[self.nr/2] if iplot: self.plot() print(self)