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, ratio_cmb_surface=1, scale_b=1, iplot=True, field='B', r=1, precision='Float64', lCut=None, quiet=False): """ :param tag: if you specify a pattern, it tries to read the corresponding files :type tag: str :param ratio_cmb_surface: ratio of surface ratio to CMB radius (default is 1) :type ratio_cmb_surface: float :param scale_b: magnetic field unit (default is 1) :type scale_b: float :param iplot: a logical to toggle the plot (default is True) :type iplot: bool :param field: 'B', 'V' or 'T' (magnetic field, velocity field or temperature) :type field: str :param r: an integer to characterise which file we want to plot :type r: int :param precision: single or double precision :type precision: str :param lCut: reduce the spherical harmonic truncation to l <= lCut :type lCut: int :param quiet: verbose when toggled to True (default is True) :type quiet: bool """ logFiles = scanDir('log.*') if len(logFiles) != 0: MagicSetup.__init__(self, quiet=True, nml=logFiles[-1]) else: str1 = 'Aspect ratio ?\n' self.radratio = float(input(str1)) self.rcmb = 1. / (1. - self.radratio) ricb = self.radratio / (1. - self.radratio) files = scanDir('%s_coeff_r%i.%s' % (field, r, tag)) # Read the B_coeff files (by stacking the different tags) data = [] for k, file in enumerate(files): if not quiet: print('Reading %s' % file) f = npfile(file, endian='B') out = f.fort_read('3i4,%s' % precision)[0] self.l_max_r, self.minc, n_data = out[0] self.m_max_r = int((self.l_max_r / self.minc) * self.minc) self.radius = out[1] while 1: try: data.append(f.fort_read(precision)) except TypeError: break self.lm_max_r = self.m_max_r*(self.l_max_r+1)//self.minc - \ self.m_max_r*(self.m_max_r-self.minc)//(2*self.minc) + \ self.l_max_r-self.m_max_r+1 # Get indices location self.idx = np.zeros((self.l_max_r + 1, self.m_max_r + 1), 'i') self.ell = np.zeros(self.lm_max_r, 'i') self.ms = np.zeros(self.lm_max_r, 'i') self.idx[0:self.l_max_r + 2, 0] = np.arange(self.l_max_r + 1) self.ell[0:self.l_max_r + 2] = np.arange(self.l_max_r + 2) k = self.l_max_r + 1 for m in range(self.minc, self.l_max_r + 1, self.minc): for l in range(m, self.l_max_r + 1): self.idx[l, m] = k self.ell[self.idx[l, m]] = l self.ms[self.idx[l, m]] = m k += 1 # Rearange data data = np.array(data, dtype=precision) self.nstep = data.shape[0] self.wlm = np.zeros((self.nstep, self.lm_max_r), 'Complex64') self.dwlm = np.zeros((self.nstep, self.lm_max_r), 'Complex64') self.zlm = np.zeros((self.nstep, self.lm_max_r), 'Complex64') # Get time self.time = np.zeros(self.nstep, dtype=precision) self.time = data[:, 0] # wlm self.wlm[:, 1:self.l_max_r + 1] = data[:, 1:self.l_max_r + 1] k = self.l_max_r + 1 for m in range(self.minc, self.l_max_r + 1, self.minc): for l in range(m, self.l_max_r + 1): self.wlm[:, self.idx[l, m]] = data[:, k] + 1j * data[:, k + 1] k += 2 # dwlm self.dwlm[:, 1:self.l_max_r + 1] = data[:, k:k + self.l_max_r] k += self.l_max_r for m in range(self.minc, self.l_max_r + 1, self.minc): for l in range(m, self.l_max_r + 1): self.dwlm[:, self.idx[l, m]] = data[:, k] + 1j * data[:, k + 1] k += 2 # zlm self.zlm[:, 1:self.l_max_r + 1] = data[:, k:k + self.l_max_r] k += self.l_max_r for m in range(self.minc, self.l_max_r + 1, self.minc): for l in range(m, self.l_max_r + 1): self.zlm[:, self.idx[l, m]] = data[:, k] + 1j * data[:, k + 1] k += 2 # ddw in case B is stored if field == 'B': self.ddwlm = np.zeros((self.nstep, self.lm_max_r), 'Complex64') self.ddwlm[:, 1:self.l_max_r + 1] = data[:, k:k + self.l_max_r] k += self.l_max_r for m in range(self.minc, self.l_max_r + 1, self.minc): for l in range(m, self.l_max_r + 1): self.ddwlm[:, self.idx[l, m]] = data[:, k] + 1j * data[:, k + 1] k += 2 # Truncate! if lCut is not None: if lCut < self.l_max_r: self.truncate(lCut, field=field) self.e_pol_axi_l = np.zeros((self.nstep, self.l_max_r + 1), precision) self.e_tor_axi_l = np.zeros((self.nstep, self.l_max_r + 1), precision) self.e_pol_l = np.zeros((self.nstep, self.l_max_r + 1), precision) self.e_tor_l = np.zeros((self.nstep, self.l_max_r + 1), precision) for l in range(1, self.l_max_r + 1): self.e_pol_l[:, l] = 0. self.e_tor_l[:, l] = 0. self.e_pol_axi_l[:, l] = 0. self.e_tor_axi_l[:, l] = 0. for m in range(0, l + 1, self.minc): lm = self.idx[l, m] if m == 0: epol = 0.5*self.ell[lm]*(self.ell[lm]+1)*( \ self.ell[lm]*(self.ell[lm]+1)/self.radius**2* \ abs(self.wlm[:,lm])**2+ abs(self.dwlm[:,lm])**2 ) etor = 0.5 * self.ell[lm] * (self.ell[lm] + 1) * abs( self.zlm[:, lm])**2 self.e_pol_axi_l[:, l] += epol self.e_tor_axi_l[:, l] += etor else: epol = self.ell[lm]*(self.ell[lm]+1)*( \ self.ell[lm]*(self.ell[lm]+1)/self.radius**2* \ abs(self.wlm[:,lm])**2+ abs(self.dwlm[:,lm])**2 ) etor = self.ell[lm] * (self.ell[lm] + 1) * abs( self.zlm[:, lm])**2 self.e_pol_l[:, l] += epol self.e_tor_l[:, l] += etor # Time-averaged energy facT = 1. / (self.time[-1] - self.time[0]) self.e_pol_lM = facT * np.trapz(self.e_pol_l, self.time, axis=0) self.e_tor_lM = facT * np.trapz(self.e_tor_l, self.time, axis=0) self.e_pol_axi_lM = facT * np.trapz( self.e_pol_axi_l, self.time, axis=0) self.e_tor_axi_lM = facT * np.trapz( self.e_tor_axi_l, self.time, axis=0)
def __init__(self, tag=None, datadir='.', ratio_cmb_surface=1, scale_b=1, iplot=True, lCut=None, precision='Float64', ave=False, sv=False, quiet=False): """ A class to read the B_coeff_cmb files :param tag: if you specify a pattern, it tries to read the corresponding files :type tag: str :param ratio_cmb_surface: ratio of surface ratio to CMB radius (default is 1) :type ratio_cmb_surface: float :param scale_b: magnetic field unit (default is 1) :type scale_b: float :param iplot: a logical to toggle the plot (default is True) :type iplot: int :param precision: single or double precision :type precision: char :param ave: load a time-averaged CMB file when set to True :type ave: bool :param sv: load a dt_b CMB file when set to True :type sv: bool :param quiet: verbose when toggled to True (default is True) :type quiet: bool :param lCut: reduce the spherical harmonic truncation to l <= lCut :type lCut: int :param datadir: working directory :type datadir: str """ pattern = os.path.join(datadir, 'log.*') logFiles = scanDir(pattern) if ave: self.name = 'B_coeff_cmb_ave' elif sv: self.name = 'B_coeff_dt_cmb' else: self.name = 'B_coeff_cmb' if tag is not None: pattern = os.path.join(datadir, '%s.%s' % (self.name, tag)) files = scanDir(pattern) # Either the log.tag directly exists and the setup is easy to obtain if os.path.exists(os.path.join(datadir, 'log.%s' % tag)): MagicSetup.__init__(self, datadir=datadir, quiet=True, nml='log.%s' % tag) # Or the tag is a bit more complicated and we need to find # the corresponding log file else: pattern = os.path.join(datadir, '%s' % self.name) mask = re.compile(r'%s\.(.*)' % pattern) if mask.match(files[-1]): ending = mask.search(files[-1]).groups(0)[0] pattern = os.path.join(datadir, 'log.%s' % ending) if logFiles.__contains__(pattern): MagicSetup.__init__(self, datadir=datadir, quiet=True, nml='log.%s' % ending) else: pattern = os.path.join(datadir, '%s.*' % self.name) files = scanDir(pattern) filename = files[-1] # Determine the setup mask = re.compile(r'%s\.(.*)' % self.name) ending = mask.search(files[-1]).groups(0)[0] if os.path.exists('log.%s' % ending): try: MagicSetup.__init__(self, datadir=datadir, quiet=True, nml='log.%s' % ending) except AttributeError: pass self.rcmb = 1. / (1. - self.radratio) ricb = self.radratio / (1. - self.radratio) # Read the B_coeff files (by stacking the different tags) data = [] for k, file in enumerate(files): if not quiet: print('Reading %s' % file) f = npfile(file, endian='B') self.l_max_cmb, self.minc, n_data = f.fort_read('i') self.m_max_cmb = int((self.l_max_cmb / self.minc) * self.minc) while 1: try: data.append(f.fort_read(precision)) except TypeError: break self.lm_max_cmb = self.m_max_cmb*(self.l_max_cmb+1)//self.minc - \ self.m_max_cmb*(self.m_max_cmb-self.minc)//(2*self.minc) + \ self.l_max_cmb-self.m_max_cmb+1 # Get indices location self.idx = np.zeros((self.l_max_cmb + 1, self.m_max_cmb + 1), 'i') self.ell = np.zeros(self.lm_max_cmb, 'i') self.ms = np.zeros(self.lm_max_cmb, 'i') self.idx[0:self.l_max_cmb + 2, 0] = np.arange(self.l_max_cmb + 1) self.ell[0:self.l_max_cmb + 2] = np.arange(self.l_max_cmb + 2) k = self.l_max_cmb + 1 for m in range(self.minc, self.l_max_cmb + 1, self.minc): for l in range(m, self.l_max_cmb + 1): self.idx[l, m] = k self.ell[self.idx[l, m]] = l self.ms[self.idx[l, m]] = m k += 1 # Rearange data data = np.array(data, dtype=precision) self.nstep = data.shape[0] self.blm = np.zeros((self.nstep, self.lm_max_cmb), 'Complex64') self.blm[:, 1:self.l_max_cmb + 1] = data[:, 1:self.l_max_cmb + 1] self.blm[:, self.l_max_cmb+1:] = data[:, self.l_max_cmb+1::2]+\ 1j*data[:, self.l_max_cmb+2::2] # Truncate! if lCut is not None: if lCut < self.l_max_cmb: self.truncate(lCut) # Get time self.time = np.zeros(self.nstep, precision) self.time = data[:, 0] # Get Gauss coefficients self.glm = np.zeros((self.nstep, self.lm_max_cmb), precision) self.hlm = np.zeros((self.nstep, self.lm_max_cmb), precision) self.glm, self.hlm = getGauss(self.blm.real, self.blm.imag, self.ell, self.ms, scale_b, ratio_cmb_surface, self.rcmb) # Time-averaged Gauss coefficient if not ave: facT = 1. / (self.time[-1] - self.time[0]) self.glmM = facT * np.trapz(self.glm, self.time, axis=0) self.hlmM = facT * np.trapz(self.hlm, self.time, axis=0) if len(self.time) > 3: self.dglmdt = deriv(self.time, self.glm.T, axis=1) self.dhlmdt = deriv(self.time, self.hlm.T, axis=1) self.dglmdt = self.dglmdt.T self.dhlmdt = self.dhlmdt.T else: self.dglmdt = np.zeros_like(self.glm) self.dhlmdt = np.zeros_like(self.hlm) # Magnetic energy (Lowes) self.El = np.zeros((self.nstep, self.l_max_cmb + 1), precision) self.Em = np.zeros((self.nstep, self.m_max_cmb + 1), precision) self.ESVl = np.zeros((self.nstep, self.l_max_cmb + 1), precision) E = 0. for l in range(1, self.l_max_cmb + 1): self.El[:, l] = 0. self.ESVl[:, l] = 0. for m in range(0, l + 1, self.minc): lm = self.idx[l, m] self.El[:, l] += (self.ell[lm]+1)*\ (self.glm[:,lm]**2+self.hlm[:,lm]**2) self.Em[:, m] += (self.ell[lm]+1)*\ (self.glm[:,lm]**2+self.hlm[:,lm]**2) if not ave: self.ESVl[:, l] += (self.ell[lm]+1)*\ (self.dglmdt[:, lm]**2+self.dhlmdt[:, lm]**2) if not ave: # Time-averaged energy self.ElM = facT * np.trapz(self.El, self.time, axis=0) self.EmM = facT * np.trapz(self.Em, self.time, axis=0) # Secular variation self.ESVlM = facT * np.trapz(self.ESVl, self.time, axis=0) self.taul = np.sqrt(self.ElM[1:] / self.ESVlM[1:]) if iplot: self.plot()
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, tag, ratio_cmb_surface=1, scale_b=1, iplot=True, precision='Float64', ave=False, sv=False): """ A class to read the B_coeff_cmb files :param tag: if you specify a pattern, it tries to read the corresponding files :type tag: str :param ratio_cmb_surface: ratio of surface ratio to CMB radius (default is 1) :type ratio_cmb_surface: float :param scale_b: magnetic field unit (default is 1) :type scale_b: float :param iplot: a logical to toggle the plot (default is True) :type iplot: int :param precision: single or double precision :type precision: char :param ave: load a time-averaged CMB file when set to True :type ave: bool :param sv: load a dt_b CMB file when set to True :type sv: bool """ logFiles = scanDir('log.*') if len(logFiles) != 0: MagicSetup.__init__(self, quiet=True, nml=logFiles[-1]) else: str1 = 'Aspect ratio ?\n' self.radratio = float(input(str1)) rcmb = 1./(1.-self.radratio) ricb = self.radratio/(1.-self.radratio) if ave: files = scanDir('B_coeff_cmb_ave.%s' % tag) elif sv: files = scanDir('B_coeff_dt_cmb.%s' % tag) else: files = scanDir('B_coeff_cmb.%s' % tag) # Read the B_coeff files (by stacking the different tags) data = [] for k, file in enumerate(files): print('Reading %s' % file) f = npfile(file, endian='B') self.l_max_cmb, self.minc, n_data = f.fort_read('i') self.m_max_cmb = int((self.l_max_cmb/self.minc)*self.minc) while 1: try: data.append(f.fort_read(precision)) except TypeError: break data = N.array(data, dtype=precision) self.ell = N.arange(self.l_max_cmb+1) self.nstep = data.shape[0] # Get time self.time = N.zeros(self.nstep, precision) self.time = data[:, 0] # Rearange and get Gauss coefficients self.alm = N.zeros((self.nstep, self.l_max_cmb+1, self.m_max_cmb+1), precision) self.blm = N.zeros((self.nstep, self.l_max_cmb+1, self.m_max_cmb+1), precision) self.glm = N.zeros((self.nstep, self.l_max_cmb+1, self.m_max_cmb+1), precision) self.hlm = N.zeros((self.nstep, self.l_max_cmb+1, self.m_max_cmb+1), precision) # Axisymmetric coefficients (m=0) self.alm[:, 1:, 0] = data[:, 1:self.l_max_cmb+1] self.glm[:, 1:, 0], self.hlm[:, 1:, 0] = getGauss(self.alm[:, 1:, 0], self.blm[:, 1:, 0], self.ell[1:], 0, scale_b, ratio_cmb_surface, rcmb) # Other coefficients (m!=0) k = self.l_max_cmb+1 for m in range(self.minc, self.l_max_cmb+1, self.minc): for l in range(m, self.l_max_cmb+1): self.alm[:, l, m] = data[:, k] self.blm[:, l, m] = data[:, k+1] self.glm[:, l, m], self.hlm[:, l, m] = getGauss(self.alm[:, l, m], self.blm[:, l, m], l, m, scale_b, ratio_cmb_surface, rcmb) k += 2 # Time-averaged Gauss coefficient facT = 1./(self.time[-1]-self.time[0]) self.glmM = facT * N.trapz(self.glm, self.time, axis=0) self.hlmM = facT * N.trapz(self.hlm, self.time, axis=0) if len(self.time) > 3: self.dglmdt = deriv(self.time, self.glm.T, axis=2) self.dhlmdt = deriv(self.time, self.hlm.T, axis=2) self.dglmdt = self.dglmdt.T self.dhlmdt = self.dhlmdt.T else: self.dglmdt = N.zeros_like(self.glm) self.dhlmdt = N.zeros_like(self.hlm) # Magnetic energy (Lowes) self.El = (self.ell+1)*(self.glm**2+self.hlm**2).sum(axis=2) self.Em = N.zeros((self.nstep, self.m_max_cmb+1), precision) # For m, we need to unfold the loop in case of minc != 1 for m in range(0, self.m_max_cmb+1, self.minc): self.Em[:,m] = ((self.ell+1)*(self.glm[:, :, m]**2+self.hlm[:, :, m]**2)).sum(axis=1) #self.Em = ((self.ell+1)*(self.glm**2+self.hlm**2)).sum(axis=1) # Time-averaged energy self.ElM = facT * N.trapz(self.El, self.time, axis=0) self.EmM = facT * N.trapz(self.Em, self.time, axis=0) # Secular variation self.ESVl = (self.ell+1)*(self.dglmdt**2+self.dhlmdt**2).sum(axis=2) self.ESVlM = facT * N.trapz(self.ESVl, self.time, axis=0) self.taul = N.sqrt(self.ElM[1:]/self.ESVlM[1:]) if iplot: self.plot()
def __init__(self, tag, ratio_cmb_surface=1, scale_b=1, iplot=True, field='B', r=1, precision='Float64'): """ :param tag: if you specify a pattern, it tries to read the corresponding files :type tag: str :param ratio_cmb_surface: ratio of surface ratio to CMB radius (default is 1) :type ratio_cmb_surface: float :param scale_b: magnetic field unit (default is 1) :type scale_b: float :param iplot: a logical to toggle the plot (default is True) :type iplot: bool :param field: 'B', 'V' or 'T' (magnetic field, velocity field or temperature) :type field: str :param r: an integer to characterise which file we want to plot :type r: int :param precision: single or double precision :type precision: str """ logFiles = scanDir('log.*') if len(logFiles) != 0: MagicSetup.__init__(self, quiet=True, nml=logFiles[-1]) else: str1 = 'Aspect ratio ?\n' self.radratio = float(input(str1)) rcmb = 1./(1.-self.radratio) ricb = self.radratio/(1.-self.radratio) files = scanDir('%s_coeff_r%i.%s' % (field,r,tag)) # Read the B_coeff files (by stacking the different tags) data = [] for k, file in enumerate(files): print('Reading %s' % file) f = npfile(file, endian='B') out = f.fort_read('3i4,%s' % precision)[0] self.l_max_cmb, self.minc, n_data = out[0] self.m_max_cmb = int((self.l_max_cmb/self.minc)*self.minc) self.radius = out[1] while 1: try: data.append(f.fort_read(precision)) except TypeError: break data = N.array(data, dtype=precision) self.ell = N.arange(self.l_max_cmb+1) self.nstep = data.shape[0] # Get time self.time = N.zeros(self.nstep, dtype=precision) self.time = data[:, 0] # Rearange and get Gauss coefficients self.wlm = N.zeros((self.nstep, self.l_max_cmb+1, self.m_max_cmb+1), 'Complex64') self.dwlm = N.zeros((self.nstep, self.l_max_cmb+1, self.m_max_cmb+1), 'Complex64') self.zlm = N.zeros((self.nstep, self.l_max_cmb+1, self.m_max_cmb+1), 'Complex64') # wlm # Axisymmetric coefficients (m=0) self.wlm[:, 1:, 0] = data[:, 1:self.l_max_cmb+1] # Other coefficients (m!=0) k = self.l_max_cmb+1 for m in range(self.minc, self.l_max_cmb+1, self.minc): for l in range(m, self.l_max_cmb+1): self.wlm[:, l, m] = data[:, k]+1j*data[:, k+1] k += 2 # dwlm self.dwlm[:, 1:, 0] = data[:, k:k+self.l_max_cmb] k += self.l_max_cmb for m in range(self.minc, self.l_max_cmb+1, self.minc): for l in range(m, self.l_max_cmb+1): self.dwlm[:, l, m] = data[:, k]+1j*data[:, k+1] k += 2 # zlm self.zlm[:, 1:, 0] = data[:, k:k+self.l_max_cmb] k += self.l_max_cmb for m in range(self.minc, self.l_max_cmb+1, self.minc): for l in range(m, self.l_max_cmb+1): self.zlm[:, l, m] = data[:, k]+1j*data[:, k+1] k += 2 # ddw in case B is stored if field == 'B': self.ddwlm = N.zeros((self.nstep, self.l_max_cmb+1, self.m_max_cmb+1), 'Complex64') self.ddwlm[:, 1:, 0] = data[:, k:k+self.l_max_cmb] k += self.l_max_cmb for m in range(self.minc, self.l_max_cmb+1, self.minc): for l in range(m, self.l_max_cmb+1): self.ddwlm[:, l, m] = data[:, k]+1j*data[:, k+1] k += 2 #self.epolLM = 0.5*self.ell*(self.ell+1)* (self.ell*(self.ell+1)* \ # abs(self.wlm)**2+abs(self.dwlm)**2) self.epolAxiL = 0.5*self.ell*(self.ell+1)*(self.ell*(self.ell+1)* \ abs(self.wlm[:,:,0])**2+abs(self.dwlm[:,:,0])**2) #self.etorLM = 0.5*self.ell*(self.ell+1)*abs(self.zlm)**2 self.etorAxiL = 0.5*self.ell*(self.ell+1)*abs(self.zlm[:,:,0])**2 #epolTot = self.epolLM.sum(axis=1).sum(axis=1) #etorTot = self.etorLM.sum(axis=1).sum(axis=1) etorAxiTot = self.etorAxiL.sum(axis=1) epolAxiTot = self.epolAxiL.sum(axis=1) if iplot: P.plot(self.time, epolTot) P.plot(self.time, etorTot) P.plot(self.time, epolAxiTot) P.plot(self.time, etorAxiTot) """
def __init__(self, tag, ratio_cmb_surface=1, scale_b=1, iplot=True, precision='Float64', ave=False, sv=False, quiet=False): """ A class to read the B_coeff_cmb files :param tag: if you specify a pattern, it tries to read the corresponding files :type tag: str :param ratio_cmb_surface: ratio of surface ratio to CMB radius (default is 1) :type ratio_cmb_surface: float :param scale_b: magnetic field unit (default is 1) :type scale_b: float :param iplot: a logical to toggle the plot (default is True) :type iplot: int :param precision: single or double precision :type precision: char :param ave: load a time-averaged CMB file when set to True :type ave: bool :param sv: load a dt_b CMB file when set to True :type sv: bool :param quiet: verbose when toggled to True (default is True) :type quiet: bool """ logFiles = scanDir('log.*') if len(logFiles) != 0: MagicSetup.__init__(self, quiet=True, nml=logFiles[-1]) else: str1 = 'Aspect ratio ?\n' self.radratio = float(input(str1)) self.rcmb = 1./(1.-self.radratio) ricb = self.radratio/(1.-self.radratio) if ave: files = scanDir('B_coeff_cmb_ave.%s' % tag) elif sv: files = scanDir('B_coeff_dt_cmb.%s' % tag) else: files = scanDir('B_coeff_cmb.%s' % tag) # Read the B_coeff files (by stacking the different tags) data = [] for k, file in enumerate(files): if not quiet: print('Reading %s' % file) f = npfile(file, endian='B') self.l_max_cmb, self.minc, n_data = f.fort_read('i') self.m_max_cmb = int((self.l_max_cmb/self.minc)*self.minc) while 1: try: data.append(f.fort_read(precision)) except TypeError: break self.lm_max_cmb = self.m_max_cmb*(self.l_max_cmb+1)//self.minc - \ self.m_max_cmb*(self.m_max_cmb-self.minc)//(2*self.minc) + \ self.l_max_cmb-self.m_max_cmb+1 # Get indices location self.idx = np.zeros((self.l_max_cmb+1, self.m_max_cmb+1), 'i') self.ell = np.zeros(self.lm_max_cmb, 'i') self.ms = np.zeros(self.lm_max_cmb, 'i') self.idx[0:self.l_max_cmb+2, 0] = np.arange(self.l_max_cmb+1) self.ell[0:self.l_max_cmb+2] = np.arange(self.l_max_cmb+2) k = self.l_max_cmb+1 for m in range(self.minc, self.l_max_cmb+1, self.minc): for l in range(m, self.l_max_cmb+1): self.idx[l, m] = k self.ell[self.idx[l,m]] = l self.ms[self.idx[l,m]] = m k +=1 # Rearange data data = np.array(data, dtype=precision) self.nstep = data.shape[0] self.blm = np.zeros((self.nstep, self.lm_max_cmb), 'Complex64') self.blm[:, 1:self.l_max_cmb+1] = data[:, 1:self.l_max_cmb+1] self.blm[:, self.l_max_cmb+1:] = data[:, self.l_max_cmb+1::2]+\ 1j*data[:, self.l_max_cmb+2::2] # Get time self.time = np.zeros(self.nstep, precision) self.time = data[:, 0] # Get Gauss coefficients self.glm = np.zeros((self.nstep, self.lm_max_cmb), precision) self.hlm = np.zeros((self.nstep, self.lm_max_cmb), precision) self.glm, self.hlm = getGauss(self.blm.real, self.blm.imag, self.ell, self.ms, scale_b, ratio_cmb_surface, self.rcmb) # Time-averaged Gauss coefficient if not ave: facT = 1./(self.time[-1]-self.time[0]) self.glmM = facT * np.trapz(self.glm, self.time, axis=0) self.hlmM = facT * np.trapz(self.hlm, self.time, axis=0) if len(self.time) > 3: self.dglmdt = deriv(self.time, self.glm.T, axis=1) self.dhlmdt = deriv(self.time, self.hlm.T, axis=1) self.dglmdt = self.dglmdt.T self.dhlmdt = self.dhlmdt.T else: self.dglmdt = np.zeros_like(self.glm) self.dhlmdt = np.zeros_like(self.hlm) # Magnetic energy (Lowes) self.El = np.zeros((self.nstep, self.l_max_cmb+1), precision) self.Em = np.zeros((self.nstep, self.m_max_cmb+1), precision) self.ESVl = np.zeros((self.nstep, self.l_max_cmb+1), precision) E = 0. for l in range(1, self.l_max_cmb+1): self.El[:, l] = 0. self.ESVl[:, l] = 0. for m in range(0, l+1, self.minc): lm = self.idx[l, m] self.El[:, l] += (self.ell[lm]+1)*\ (self.glm[:,lm]**2+self.hlm[:,lm]**2) self.Em[:, m] += (self.ell[lm]+1)*\ (self.glm[:,lm]**2+self.hlm[:,lm]**2) if not ave: self.ESVl[:, l] += (self.ell[lm]+1)*\ (self.dglmdt[:, lm]**2+self.dhlmdt[:, lm]**2) if not ave: # Time-averaged energy self.ElM = facT * np.trapz(self.El, self.time, axis=0) self.EmM = facT * np.trapz(self.Em, self.time, axis=0) # Secular variation self.ESVlM = facT * np.trapz(self.ESVl, self.time, axis=0) self.taul = np.sqrt(self.ElM[1:]/self.ESVlM[1:]) if iplot: self.plot()
def __init__(self, tag, ratio_cmb_surface=1, scale_b=1, iplot=True, field='B', r=1, precision='Float64', lCut=None, quiet=False): """ :param tag: if you specify a pattern, it tries to read the corresponding files :type tag: str :param ratio_cmb_surface: ratio of surface ratio to CMB radius (default is 1) :type ratio_cmb_surface: float :param scale_b: magnetic field unit (default is 1) :type scale_b: float :param iplot: a logical to toggle the plot (default is True) :type iplot: bool :param field: 'B', 'V' or 'T' (magnetic field, velocity field or temperature) :type field: str :param r: an integer to characterise which file we want to plot :type r: int :param precision: single or double precision :type precision: str :param lCut: reduce the spherical harmonic truncation to l <= lCut :type lCut: int :param quiet: verbose when toggled to True (default is True) :type quiet: bool """ logFiles = scanDir('log.*') if len(logFiles) != 0: MagicSetup.__init__(self, quiet=True, nml=logFiles[-1]) else: str1 = 'Aspect ratio ?\n' self.radratio = float(input(str1)) self.rcmb = 1./(1.-self.radratio) ricb = self.radratio/(1.-self.radratio) files = scanDir('%s_coeff_r%i.%s' % (field,r,tag)) # Read the B_coeff files (by stacking the different tags) data = [] for k, file in enumerate(files): if not quiet: print('Reading %s' % file) f = npfile(file, endian='B') out = f.fort_read('3i4,%s' % precision)[0] self.l_max_r, self.minc, n_data = out[0] self.m_max_r = int((self.l_max_r/self.minc)*self.minc) self.radius = out[1] while 1: try: data.append(f.fort_read(precision)) except TypeError: break self.lm_max_r = self.m_max_r*(self.l_max_r+1)//self.minc - \ self.m_max_r*(self.m_max_r-self.minc)//(2*self.minc) + \ self.l_max_r-self.m_max_r+1 # Get indices location self.idx = np.zeros((self.l_max_r+1, self.m_max_r+1), 'i') self.ell = np.zeros(self.lm_max_r, 'i') self.ms = np.zeros(self.lm_max_r, 'i') self.idx[0:self.l_max_r+2, 0] = np.arange(self.l_max_r+1) self.ell[0:self.l_max_r+2] = np.arange(self.l_max_r+2) k = self.l_max_r+1 for m in range(self.minc, self.l_max_r+1, self.minc): for l in range(m, self.l_max_r+1): self.idx[l, m] = k self.ell[self.idx[l,m]] = l self.ms[self.idx[l,m]] = m k +=1 # Rearange data data = np.array(data, dtype=precision) self.nstep = data.shape[0] self.wlm = np.zeros((self.nstep, self.lm_max_r), 'Complex64') self.dwlm = np.zeros((self.nstep, self.lm_max_r), 'Complex64') self.zlm = np.zeros((self.nstep, self.lm_max_r), 'Complex64') # Get time self.time = np.zeros(self.nstep, dtype=precision) self.time = data[:, 0] # wlm self.wlm[:, 1:self.l_max_r+1] = data[:, 1:self.l_max_r+1] k = self.l_max_r+1 for m in range(self.minc, self.l_max_r+1, self.minc): for l in range(m, self.l_max_r+1): self.wlm[:, self.idx[l, m]] = data[:, k]+1j*data[:, k+1] k += 2 # dwlm self.dwlm[:, 1:self.l_max_r+1] = data[:, k:k+self.l_max_r] k += self.l_max_r for m in range(self.minc, self.l_max_r+1, self.minc): for l in range(m, self.l_max_r+1): self.dwlm[:, self.idx[l, m]] = data[:, k]+1j*data[:, k+1] k += 2 # zlm self.zlm[:, 1:self.l_max_r+1] = data[:, k:k+self.l_max_r] k += self.l_max_r for m in range(self.minc, self.l_max_r+1, self.minc): for l in range(m, self.l_max_r+1): self.zlm[:, self.idx[l, m]] = data[:, k]+1j*data[:, k+1] k += 2 # ddw in case B is stored if field == 'B': self.ddwlm = np.zeros((self.nstep, self.lm_max_r), 'Complex64') self.ddwlm[:, 1:self.l_max_r+1] = data[:, k:k+self.l_max_r] k += self.l_max_r for m in range(self.minc, self.l_max_r+1, self.minc): for l in range(m, self.l_max_r+1): self.ddwlm[:, self.idx[l, m]] = data[:, k]+1j*data[:, k+1] k += 2 # Truncate! if lCut is not None: if lCut < self.l_max_r: self.truncate(lCut, field=field) self.e_pol_axi_l = np.zeros((self.nstep, self.l_max_r+1), precision) self.e_tor_axi_l = np.zeros((self.nstep, self.l_max_r+1), precision) self.e_pol_l = np.zeros((self.nstep, self.l_max_r+1), precision) self.e_tor_l = np.zeros((self.nstep, self.l_max_r+1), precision) for l in range(1, self.l_max_r+1): self.e_pol_l[:, l] = 0. self.e_tor_l[:, l] = 0. self.e_pol_axi_l[:, l] = 0. self.e_tor_axi_l[:, l] = 0. for m in range(0, l+1, self.minc): lm = self.idx[l, m] if m == 0: epol = 0.5*self.ell[lm]*(self.ell[lm]+1)*( \ self.ell[lm]*(self.ell[lm]+1)/self.radius**2* \ abs(self.wlm[:,lm])**2+ abs(self.dwlm[:,lm])**2 ) etor = 0.5*self.ell[lm]*(self.ell[lm]+1)*abs(self.zlm[:, lm])**2 self.e_pol_axi_l[:, l] += epol self.e_tor_axi_l[:, l] += etor else: epol = self.ell[lm]*(self.ell[lm]+1)*( \ self.ell[lm]*(self.ell[lm]+1)/self.radius**2* \ abs(self.wlm[:,lm])**2+ abs(self.dwlm[:,lm])**2 ) etor = self.ell[lm]*(self.ell[lm]+1)*abs(self.zlm[:, lm])**2 self.e_pol_l[:, l] += epol self.e_tor_l[:, l] += etor # Time-averaged energy facT = 1./(self.time[-1]-self.time[0]) self.e_pol_lM = facT * np.trapz(self.e_pol_l, self.time, axis=0) self.e_tor_lM = facT * np.trapz(self.e_tor_l, self.time, axis=0) self.e_pol_axi_lM = facT * np.trapz(self.e_pol_axi_l, self.time, axis=0) self.e_tor_axi_lM = facT * np.trapz(self.e_tor_axi_l, self.time, axis=0)