From 4476a0ca0e22d6a35bb07da87956d21ec7c49074 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Sat, 22 Jul 2017 23:53:20 +0800 Subject: Use all uppercase to identify the global CONFIGS and COSMO Update to use the global CONFIGS and COSMO Signed-off-by: Aaron LI --- bin/fg21sim | 20 ++++++++++---------- fg21sim/configs/__init__.py | 4 ++-- fg21sim/extragalactic/clusters/emission.py | 6 +++--- fg21sim/extragalactic/clusters/formation.py | 14 +++++++------- fg21sim/extragalactic/clusters/halo.py | 26 +++++++++++--------------- fg21sim/extragalactic/clusters/helper.py | 16 ++++++++-------- fg21sim/extragalactic/clusters/main.py | 6 +++--- fg21sim/utils/__init__.py | 2 +- fg21sim/utils/cosmology.py | 10 +++++----- 9 files changed, 50 insertions(+), 54 deletions(-) diff --git a/bin/fg21sim b/bin/fg21sim index c25aef3..c80e5ca 100755 --- a/bin/fg21sim +++ b/bin/fg21sim @@ -14,8 +14,8 @@ import argparse import logging import time -from fg21sim.configs import configs -from fg21sim.utils import setup_logging, cosmo +from fg21sim.configs import CONFIGS +from fg21sim.utils import setup_logging, COSMO def main(): @@ -36,14 +36,14 @@ def main(): t1_start = time.perf_counter() t2_start = time.process_time() - configs.read_userconfig(args.config) + CONFIGS.read_userconfig(args.config) if os.environ.get("DEBUG_FG21SIM"): - print("DEBUG: Current configurations:", configs._config, + print("DEBUG: Current configurations:", CONFIGS._config, sep="\n", file=sys.stderr) - configs.check_all() + CONFIGS.check_all() log_stream = "" if args.quiet else None - setup_logging(dict_config=configs.logging, + setup_logging(dict_config=CONFIGS.logging, level=args.loglevel, stream=log_stream, logfile=args.logfile) @@ -52,17 +52,17 @@ def main(): logger.info("COMMAND: {0}".format(" ".join(sys.argv))) # Save/backup current configurations - configfile_dump = os.path.splitext(configs.userconfig)[0] + "_dump.conf" - configs.save(configfile_dump, clobber=True, backup=True) + configfile_dump = os.path.splitext(CONFIGS.userconfig)[0] + "_dump.conf" + CONFIGS.save(configfile_dump, clobber=True, backup=True) logger.info("Saved current configurations to file: %s" % configfile_dump) # Setup cosmology model logger.info("Setup cosmology model with parameters from configs ...") - cosmo.setup(**configs.cosmology) + COSMO.setup(**CONFIGS.cosmology) logger.info("Importing modules + Numba JIT, waiting ...") from fg21sim.foregrounds import Foregrounds - fg = Foregrounds(configs) + fg = Foregrounds(CONFIGS) fg.preprocess() fg.simulate() fg.postprocess() diff --git a/fg21sim/configs/__init__.py b/fg21sim/configs/__init__.py index 306b0d5..7670c90 100644 --- a/fg21sim/configs/__init__.py +++ b/fg21sim/configs/__init__.py @@ -1,7 +1,7 @@ -# Copyright (c) 2016 Weitian LI +# Copyright (c) 2016-2017 Weitian LI # MIT license from .manager import ConfigManager from .checkers import check_configs -configs = ConfigManager() +CONFIGS = ConfigManager() diff --git a/fg21sim/extragalactic/clusters/emission.py b/fg21sim/extragalactic/clusters/emission.py index 4366025..5c9e0a7 100644 --- a/fg21sim/extragalactic/clusters/emission.py +++ b/fg21sim/extragalactic/clusters/emission.py @@ -18,7 +18,7 @@ import numpy as np import scipy.integrate import scipy.special -from ...utils import cosmo +from ...utils import COSMO from ...utils.units import (Units as AU, UnitConversions as AUC, Constants as AC) @@ -164,7 +164,7 @@ class SynchrotronEmission: Synchrotron flux at frequency ``nu``. Unit: [Jy] = 1e-23 [erg/s/cm^2/Hz] """ - DL = cosmo.DL(self.z) * AUC.Mpc2cm # [cm] + DL = COSMO.DL(self.z) * AUC.Mpc2cm # [cm] P_nu = self.power(nu) F_nu = 1e23 * P_nu / (4*np.pi * DL*DL) # [Jy] return F_nu @@ -191,7 +191,7 @@ class SynchrotronEmission: Synchrotron surface brightness at frequency ``nu``. Unit: [K] <-> [Jy/pixel] """ - DA = cosmo.DL(self.z) * AUC.Mpc2cm # [cm] + DA = COSMO.DL(self.z) * AUC.Mpc2cm # [cm] radius = self.radius * AUC.kpc2cm # [cm] omega = (np.pi * radius**2 / DA**2) * AUC.rad2deg**2 # [deg^2] pixelarea = (pixelsize * AUC.arcsec2deg) ** 2 # [deg^2] diff --git a/fg21sim/extragalactic/clusters/formation.py b/fg21sim/extragalactic/clusters/formation.py index a036069..02b26f4 100644 --- a/fg21sim/extragalactic/clusters/formation.py +++ b/fg21sim/extragalactic/clusters/formation.py @@ -21,7 +21,7 @@ import scipy.special import scipy.optimize from .mergertree import MergerTree -from ...utils import cosmo +from ...utils import COSMO logger = logging.getLogger(__name__) @@ -98,7 +98,7 @@ class ClusterFormation: References: Ref.[1],Eq.(2) """ alpha = self.sigma_index - sigma = cosmo.sigma8 * (mass / cosmo.M8) ** (-alpha) + sigma = COSMO.sigma8 * (mass / COSMO.M8) ** (-alpha) return sigma def f_delta_c(self, z): @@ -110,7 +110,7 @@ class ClusterFormation: References: Ref.[1],App.A,Eq.(A1) """ - return cosmo.overdensity_crit(z) + return COSMO.overdensity_crit(z) def f_dw_max(self, mass): """ @@ -145,7 +145,7 @@ class ClusterFormation: References: Ref.[1],Sec.(3) """ alpha = self.sigma_index - mass = cosmo.M8 * (S / cosmo.sigma8**2)**(-1/(2*alpha)) + mass = COSMO.M8 * (S / COSMO.sigma8**2)**(-1/(2*alpha)) return mass @staticmethod @@ -251,7 +251,7 @@ class ClusterFormation: Mc = self.M0 mtree_root = MergerTree(data={"mass": Mc, "z": zc, - "age": cosmo.age(zc)}) + "age": COSMO.age(zc)}) logger.debug("[main] z=%.4f : mass=%g [Msun]" % (zc, Mc)) mtree = mtree_root @@ -272,7 +272,7 @@ class ClusterFormation: dS = self.gen_dS(dw) # Progenitor properties z1 = self.calc_z(w2 + dw) - age1 = cosmo.age(z1) + age1 = COSMO.age(z1) S1 = S2 + dS M1 = self.calc_mass(S1) dM = Mc - M1 @@ -310,7 +310,7 @@ class ClusterFormation: merger tree. """ z = 0.0 if _z is None else _z - node_data = {"mass": M, "z": z, "age": cosmo.age(z)} + node_data = {"mass": M, "z": z, "age": COSMO.age(z)} # Whether to stop the trace if self.zmax is not None and z > self.zmax: diff --git a/fg21sim/extragalactic/clusters/halo.py b/fg21sim/extragalactic/clusters/halo.py index 7887ace..8fdc78c 100644 --- a/fg21sim/extragalactic/clusters/halo.py +++ b/fg21sim/extragalactic/clusters/halo.py @@ -44,8 +44,8 @@ import numpy as np from . import helper from .solver import FokkerPlanckSolver -from ...configs import configs -from ...utils import cosmo +from ...configs import CONFIGS +from ...utils import COSMO from ...utils.units import (Units as AU, UnitConversions as AUC, Constants as AC) @@ -88,10 +88,6 @@ class RadioHalo: Unit: [Msun] z_merger : float The redshift when the (major) merger begins. - tau_merger : float - The timescale of the merger-induced disturbance. - Unit: [Gyr] - ... Attributes ---------- @@ -102,7 +98,7 @@ class RadioHalo: Unit: [kpc] """ def __init__(self, M_obs, z_obs, M_main, M_sub, z_merger, - configs=configs): + configs=CONFIGS): self.M_obs = M_obs self.z_obs = z_obs self.M_main = M_main @@ -144,11 +140,11 @@ class RadioHalo: @property def age_obs(self): - return cosmo.age(self.z_obs) + return COSMO.age(self.z_obs) @property def age_merger(self): - return cosmo.age(self.z_merger) + return COSMO.age(self.z_merger) @property def time_crossing(self): @@ -191,13 +187,13 @@ class RadioHalo: Unit: [cm^-3] """ if zbegin is None: - tstart = cosmo.age(self.z_merger) + tstart = COSMO.age(self.z_merger) else: - tstart = cosmo.age(zbegin) + tstart = COSMO.age(zbegin) if zend is None: - tstop = cosmo.age(self.z_obs) + tstop = COSMO.age(self.z_obs) else: - tstop = cosmo.age(zend) + tstop = COSMO.age(zend) if n0_e is None: # Accumulated constantly injected electrons until ``tstart``. n_inj = np.array([self.fp_injection(gm) @@ -401,7 +397,7 @@ class RadioHalo: ---------- Ref.[sarazin1999],Eq.(9) """ - z = cosmo.redshift(t) + z = COSMO.redshift(t) mass = self._mass(t) n_th = helper.density_number_thermal(mass, z) coef = -1.20e-12 * AUC.Gyr2s # [Gyr^-1] @@ -417,7 +413,7 @@ class RadioHalo: ---------- Ref.[sarazin1999],Eq.(6,7) """ - z = cosmo.redshift(t) + z = COSMO.redshift(t) mass = self._mass(t) B = helper.magnetic_field(mass) coef = -1.37e-20 * AUC.Gyr2s # [Gyr^-1] diff --git a/fg21sim/extragalactic/clusters/helper.py b/fg21sim/extragalactic/clusters/helper.py index f20af5f..5963bab 100644 --- a/fg21sim/extragalactic/clusters/helper.py +++ b/fg21sim/extragalactic/clusters/helper.py @@ -26,8 +26,8 @@ References import numpy as np -from ...configs import configs -from ...utils import cosmo +from ...configs import CONFIGS +from ...utils import COSMO from ...utils.units import (Constants as AC, UnitConversions as AUC) @@ -51,8 +51,8 @@ def radius_virial(mass, z=0.0): Virial radius of the cluster Unit: [kpc] """ - Dc = cosmo.overdensity_virial(z) - rho = cosmo.rho_crit(z) # [g/cm^3] + Dc = COSMO.overdensity_virial(z) + rho = COSMO.rho_crit(z) # [g/cm^3] R_vir = (3*mass*AUC.Msun2g / (4*np.pi * Dc * rho))**(1/3) # [cm] R_vir *= AUC.cm2kpc # [kpc] return R_vir @@ -123,7 +123,7 @@ def mass_to_kT(mass, z=0.0): """ A = 5.34 + np.random.normal(scale=0.22) alpha = 1.72 + np.random.normal(scale=0.10) - Ez = cosmo.E(z) + Ez = COSMO.E(z) kT = 5.0 * (mass * Ez / A) ** (1/alpha) return kT @@ -151,7 +151,7 @@ def density_number_thermal(mass, z=0.0): Number density of the ICM thermal plasma Unit: [cm^-3] """ - N = mass * AUC.Msun2g * cosmo.baryon_fraction / (AC.mu * AC.u) + N = mass * AUC.Msun2g * COSMO.baryon_fraction / (AC.mu * AC.u) R_vir = radius_virial(mass, z) * AUC.kpc2cm # [cm] volume = (4*np.pi / 3) * R_vir**3 # [cm^3] n_th = N / volume # [cm^-3] @@ -258,8 +258,8 @@ def magnetic_field(mass): Ref.[cassano2012],Eq.(1) """ comp = "extragalactic/clusters" - b_mean = configs.getn(comp+"/b_mean") - b_index = configs.getn(comp+"/b_index") + b_mean = CONFIGS.getn(comp+"/b_mean") + b_index = CONFIGS.getn(comp+"/b_index") M_mean = 1.6e15 # [Msun] B = b_mean * (mass/M_mean) ** b_index diff --git a/fg21sim/extragalactic/clusters/main.py b/fg21sim/extragalactic/clusters/main.py index 7d8d494..daa1c83 100644 --- a/fg21sim/extragalactic/clusters/main.py +++ b/fg21sim/extragalactic/clusters/main.py @@ -58,7 +58,7 @@ class GalaxyClusters: # Component name name = "galaxy clusters (halos)" - def __init__(self, configs=configs): + def __init__(self, configs=CONFIGS): self.configs = configs self.sky = get_sky(configs) self._set_configs() @@ -196,8 +196,8 @@ class GalaxyClusters: if ii % 50 == 0: logger.info("[%d/%d] %.1f%% ..." % (ii, num, 100*ii/num)) z0, M0 = row.z, row.mass - age0 = cosmo.age(z0) - zmax = cosmo.redshift(age0 - self.tau_merger) + age0 = COSMO.age(z0) + zmax = COSMO.redshift(age0 - self.tau_merger) clform = ClusterFormation(M0=M0, z0=z0, zmax=zmax, ratio_major=self.ratio_major, merger_mass_min=self.merger_mass_min) diff --git a/fg21sim/utils/__init__.py b/fg21sim/utils/__init__.py index 194b356..5b61027 100644 --- a/fg21sim/utils/__init__.py +++ b/fg21sim/utils/__init__.py @@ -4,4 +4,4 @@ from .logging import setup_logging from .cosmology import Cosmology -cosmo = Cosmology() +COSMO = Cosmology() diff --git a/fg21sim/utils/cosmology.py b/fg21sim/utils/cosmology.py index e4979b1..e4ac4d7 100644 --- a/fg21sim/utils/cosmology.py +++ b/fg21sim/utils/cosmology.py @@ -11,7 +11,7 @@ import numpy as np from scipy import integrate from astropy.cosmology import FlatLambdaCDM -from ..configs import configs +from ..configs import CONFIGS from .units import (UnitConversions as AUC, Constants as AC) @@ -55,10 +55,10 @@ class Cosmology: _growth_factor0 = None def __init__(self, - H0=configs.cosmology["H0"], - Om0=configs.cosmology["Om0"], - Ob0=configs.cosmology["Ob0"], - sigma8=configs.cosmology["sigma8"]): + H0=CONFIGS.cosmology["H0"], + Om0=CONFIGS.cosmology["Om0"], + Ob0=CONFIGS.cosmology["Ob0"], + sigma8=CONFIGS.cosmology["sigma8"]): self.setup(H0=H0, Om0=Om0, Ob0=Ob0, sigma8=sigma8) def setup(self, **kwargs): -- cgit v1.2.2