def create_density_maps():
    time_start = time.time()
    # Get command line arguments
    args = {}
    args["simdir"] = sys.argv[1]
    args["lcdir"] = sys.argv[2]
    args["ncells"] = int(sys.argv[3])
    args["sml"] = int(sys.argv[4])
    args["walltime"] = int(sys.argv[5])
    args["outbase"] = sys.argv[6]
    label = args["simdir"].split('/')[-2].split('_')[2]
    lclabel = args["lcdir"].split('/')[-1][-4]
    # Characteristics
    hflabel = whichhalofinder(args["lcdir"])

    # Load LightCone Contents
    lchdf = h5py.File(args["lcdir"], 'r')
    dfhalo = pd.DataFrame({
        'HF_ID': lchdf['HF_ID'].value,
        'LC_ID': lchdf['LC_ID'].value,
        'Halo_z': lchdf['Halo_z'].value,
        'snapnum': lchdf['snapnum'].value,
        'Vrms': lchdf['VelDisp'].value,
        'fov_Mpc': lchdf['FOV'][:][1],
        ('HaloPosBox', 'X'): lchdf['HaloPosBox'][:, 0],
        ('HaloPosBox', 'Y'): lchdf['HaloPosBox'][:, 1],
        ('HaloPosBox', 'Z'): lchdf['HaloPosBox'][:, 2]
    })

    if len(dfhalo.index.values) > 2000:
        # Limit number of halos, to keep comp. cost down
        dfhalo = dfhalo.sample(n=2000)
    print('There are %d galaxies in this lightcone' % len(dfhalo.index.values))

    nhalo_per_snapshot = dfhalo.groupby('snapnum').count()['HF_ID']
    print('devided over lightcone as:')
    print(nhalo_per_snapshot)
    snapshots = dfhalo.groupby('snapnum').count().index.values
    dfhalo = dfhalo.sort_values(by=['snapnum'])

    sigma_tot = []
    out_hfid = []
    out_lcid = []
    out_redshift = []
    out_snapnum = []
    out_vrms = []
    out_fov = []

    ## Run over Snapshots
    for ss in range(len(nhalo_per_snapshot)):
        print('Snapshot %d of %d' % (ss, len(nhalo_per_snapshot)))
        dfhalosnap = dfhalo.loc[dfhalo['snapnum'] == snapshots[ss]]

        # Load simulation
        s = read_hdf5.snapshot(snapshots[ss], args["simdir"])
        s.read(["Coordinates", "Masses", "GFM_StellarFormationTime"],
               parttype=[0, 1, 4, 5])
        scale = 1e-3 * s.header.hubble
        print(': Redshift: %f' % s.header.redshift)

        DM, Gas, Star, BH = particle_data(s.data, h, 'kpc')
        ## Run over Sub-&Halos
        for ll in range(len(dfhalosnap.index)):
            print('Lens %d of %d' % (ll, len(dfhalosnap.index)))
            #TODO: for z=0 sh_dist=0!!!

            # Define Cosmology
            cosmo = LambdaCDM(H0=s.header.hubble * 100,
                              Om0=s.header.omega_m,
                              Ode0=s.header.omega_l)
            cosmosim = {
                'omega_M_0': s.header.omega_m,
                'omega_lambda_0': s.header.omega_l,
                'omega_k_0': 0.0,
                'h': s.header.hubble
            }

            smlpixel = args["sml"]  # maximum smoothing pixel length
            shpos = dfhalosnap.filter(regex='HaloPosBox').iloc[ll].values
            #time_start = time.time()
            ## BH
            pos, indx = dmaps.select_particles(
                BH['Pos'],
                shpos,  #*a/h,
                dfhalosnap['fov_Mpc'].values[ll],
                'box')
            bh_sigma = dmaps.projected_density_pmesh(
                pos, BH['Mass'][indx], dfhalosnap['fov_Mpc'].values[ll],
                args["ncells"])
            ## Star
            pos, indx = dmaps.select_particles(
                Gas['Pos'],
                shpos,  #*a/h,
                dfhalosnap['fov_Mpc'].values[ll],
                'box')
            gas_sigma = dmaps.projected_density_pmesh_adaptive(
                pos,
                Gas['Mass'][indx],
                dfhalosnap['fov_Mpc'].values[ll],
                args["ncells"],
                hmax=smlpixel)
            ## Gas
            pos, indx = dmaps.select_particles(
                Star['Pos'],
                shpos,  #*a/h
                dfhalosnap['fov_Mpc'].values[ll],
                'box')
            star_sigma = dmaps.projected_density_pmesh_adaptive(
                pos,
                Star['Mass'][indx],
                dfhalosnap['fov_Mpc'].values[ll],
                args["ncells"],
                hmax=smlpixel)
            ## DM
            pos, indx = dmaps.select_particles(
                DM['Pos'],
                shpos,  #*a/h
                dfhalosnap['fov_Mpc'].values[ll],
                'box')
            dm_sigma = dmaps.projected_density_pmesh_adaptive(
                pos,
                DM['Mass'][indx],
                dfhalosnap['fov_Mpc'].values[ll],  #[Mpc]
                args["ncells"],
                hmax=smlpixel)
            sigmatotal = dm_sigma + gas_sigma + star_sigma + bh_sigma

            # Make sure that density-map if filled
            extention = 0
            while 0.0 in sigmatotal and (extention < 60):
                extention += 5
                dm_sigma = dmaps.projected_density_pmesh_adaptive(
                    pos,
                    DM['Mass'][indx],
                    dfhalosnap['fov_Mpc'].values[ll],  #[Mpc]
                    args["ncells"],
                    hmax=smlpixel + extention)
                sigmatotal = dm_sigma + gas_sigma + star_sigma + bh_sigma

            sigma_tot.append(sigmatotal)
            out_hfid.append(dfhalosnap['HF_ID'].values[ll])
            out_lcid.append(dfhalosnap['LC_ID'].values[ll])
            out_fov.append(dfhalosnap['fov_Mpc'].values[ll])
            if args["walltime"] - (time_start - time.time()) / (60 *
                                                                60) < 0.25:
                fname = args["outbase"] + 'DM_' + label + '_lc' + str(
                    lclabel) + '.h5'
                hf = h5py.File(fname, 'w')
                hf.create_dataset('density_map', data=sigma_tot)
                hf.create_dataset('HF_ID', data=np.asarray(out_hfid))
                hf.create_dataset('LC_ID', data=np.asarray(out_lcid))
                hf.create_dataset('fov_Mpc', data=np.asarray(out_fov))
                hf.close()

    fname = args["outbase"] + 'DM_' + label + '_lc_' + str(lclabel) + '.h5'
    hf = h5py.File(fname, 'w')
    hf.create_dataset('density_map', data=sigma_tot)
    hf.create_dataset('HF_ID', data=np.asarray(out_hfid))
    hf.create_dataset('LC_ID', data=out_lcid)
    hf.create_dataset('fov_Mpc', data=np.asarray(out_fov))
    #RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
    hf.close()
def create_density_maps():
    # Get command line arguments
    args = {}
    if comm_rank == 0:
        print(':Registered %d processes' % comm_size)
        args["simdir"] = sys.argv[1]
        args["hfdir"] = sys.argv[2]
        args["snapnum"] = int(sys.argv[3])
        args["zs"] = float(sys.argv[4]) / 10
    args = comm.bcast(args)
    label = args["simdir"].split('/')[-2].split('_')[2]

    # Organize devision of Sub-&Halos over Processes on Proc. 0
    if comm_rank == 0:
        # Load simulation
        s = read_hdf5.snapshot(args["snapnum"], args["simdir"])
        s.read(["Coordinates", "Masses", "GFM_StellarFormationTime"],
               parttype=[0, 1, 4])  #[0,1,4]
        scale = 1e-3 * s.header.hubble

        # Define Cosmology
        cosmo = LambdaCDM(H0=s.header.hubble * 100,
                          Om0=s.header.omega_m,
                          Ode0=s.header.omega_l)
        cosmosim = {
            'omega_M_0': s.header.omega_m,
            'omega_lambda_0': s.header.omega_l,
            'omega_k_0': 0.0,
            'h': s.header.hubble
        }
        redshift = s.header.redshift
        print(': Redshift: %f' % redshift)

        # Sort Sub-&Halos over Processes
        df = pd.read_csv(args["hfdir"] + 'halos_%d.dat' % args["snapnum"],
                         sep='\s+',
                         skiprows=16,
                         usecols=[0, 2, 4, 9, 10, 11],
                         names=['ID', 'Mvir', 'Vrms', 'X', 'Y', 'Z'])
        df = df[df['Mvir'] > 5e11]
        sh_id = df['ID'].values.astype('float64')
        sh_vrms = df['Vrms'].values.astype('float64')
        sh_x = df['X'].values.astype('float64')
        sh_y = df['Y'].values.astype('float64')
        sh_z = df['Z'].values.astype('float64')
        del df
        hist_edges = procdiv.histedges_equalN(sh_x, comm_size)
        SH = procdiv.cluster_subhalos(sh_id, sh_vrms, sh_x, sh_y, sh_z,
                                      hist_edges, comm_size)

        # Calculate overlap for particle cuboids
        c = (const.c).to_value('km/s')
        fov_rad = 4 * np.pi * (np.percentile(SH['Vrms'], 90) / c)**2
        sh_dist = (cosmo.comoving_distance(redshift)).to_value('Mpc')
        alpha = 6  # multiplied by 4 because of Oguri&Marshall
        overlap = 0.5 * alpha * fov_rad * sh_dist  #[Mpc] half of field-of-view
        print('Cuboids overlap is: %f [Mpc]' % overlap)

        # Sort Particles over Processes
        ## Dark Matter
        DM = {
            'Mass': (s.data['Masses']['dm']).astype('float64'),
            'Pos': (s.data['Coordinates']['dm'] * scale).astype('float64')
        }
        DM = procdiv.cluster_particles(DM, hist_edges, comm_size)
        ## Gas
        Gas = {
            'Mass': (s.data['Masses']['gas']).astype('float64'),
            'Pos': (s.data['Coordinates']['gas'] * scale).astype('float64')
        }
        Gas = procdiv.cluster_particles(Gas, hist_edges, comm_size)
        ## Stars
        age = (s.data['GFM_StellarFormationTime']['stars']).astype('float64')
        Star = {
            'Mass': (s.data['Masses']['stars'][age >= 0]).astype('float64'),
            'Pos': (s.data['Coordinates']['stars'][age >= 0, :] *
                    scale).astype('float64')
        }
        del age
        Star = procdiv.cluster_particles(Star, hist_edges, comm_size)

    else:
        c = None
        alpha = None
        overlap = None
        cosmosim = None
        cosmo = None
        redshift = None
        hist_edges = None
        SH = {
            'ID': None,
            'Vrms': None,
            'X': None,
            'Y': None,
            'Z': None,
            'split_size_1d': None,
            'split_disp_1d': None
        }
        DM = {
            'Mass': None,
            'X': None,
            'Y': None,
            'Z': None,
            'split_size_1d': None,
            'split_disp_1d': None
        }
        Gas = {
            'Mass': None,
            'X': None,
            'Y': None,
            'Z': None,
            'split_size_1d': None,
            'split_disp_1d': None
        }
        Star = {
            'Mass': None,
            'X': None,
            'Y': None,
            'Z': None,
            'split_size_1d': None,
            'split_disp_1d': None
        }

    # Broadcast variables over all processors
    sh_split_size_1d = comm.bcast(SH['split_size_1d'], root=0)
    dm_split_size_1d = comm.bcast(DM['split_size_1d'], root=0)
    gas_split_size_1d = comm.bcast(Gas['split_size_1d'], root=0)
    star_split_size_1d = comm.bcast(Star['split_size_1d'], root=0)
    c = comm.bcast(c, root=0)
    alpha = comm.bcast(alpha, root=0)
    overlap = comm.bcast(overlap, root=0)
    cosmo = comm.bcast(cosmo, root=0)
    redshift = comm.bcast(redshift, root=0)
    hist_edges = comm.bcast(hist_edges, root=0)

    SH = procdiv.scatter_subhalos(SH,
                                  sh_split_size_1d,
                                  comm_rank,
                                  comm,
                                  root_proc=0)
    DM = procdiv.scatter_particles(DM,
                                   dm_split_size_1d,
                                   comm_rank,
                                   comm,
                                   root_proc=0)
    Gas = procdiv.scatter_particles(Gas,
                                    gas_split_size_1d,
                                    comm_rank,
                                    comm,
                                    root_proc=0)
    Star = procdiv.scatter_particles(Star,
                                     star_split_size_1d,
                                     comm_rank,
                                     comm,
                                     root_proc=0)
    print(
        ': Proc. %d got: \n\t %d Sub-&Halos \n\t %d dark matter \n\t %d gas \n\t %d stars \n'
        % (comm_rank, int(sh_split_size_1d[comm_rank]),
           int(dm_split_size_1d[comm_rank]), int(gas_split_size_1d[comm_rank]),
           int(star_split_size_1d[comm_rank])))

    ## Run over Sub-&Halos
    zl = redshift
    zs = args["zs"]
    ncells = [512, 256, 128]
    nparts = [1, 2, 4, 8]
    M200 = np.ones(len(SH['ID']))
    ID = np.ones(len(SH['ID']))
    Rein = np.ones((len(SH['ID']), len(ncells), len(nparts)))
    for ll in range(len(SH['ID'])):
        # Define field-of-view
        fov_rad = 4 * np.pi * (SH['Vrms'][ll] / c)**2
        sh_dist = (cosmo.comoving_distance(redshift)).to_value('Mpc')
        fov_Mpc = alpha * fov_rad * sh_dist  #[Mpc] is it the diameter?
        fov_arc = (fov_Mpc / cf.Da(zl, cosmo) * u.rad).to_value('arcsec')
        sigma_cr = sigma_crit(zl, zs, cosmo).to_value('Msun Mpc-2')

        # Check cuboid boundary condition,
        # that all surface densities are filled with particles
        if ((SH['Pos'][ll,0]-hist_edges[comm_rank] < overlap) or
                (hist_edges[comm_rank+1]-overlap < \
                 SH['Pos'][ll,0]-hist_edges[comm_rank])):
            if fov_Mpc * 0.45 > overlap:
                print("FOV is bigger than cuboids overlap: %f > %f" % \
                        (fov_Mpc*0.45, overlap))
                continue

        ## Run over different Ncells
        for cc in range(len(ncells)):
            dsx_arc = fov_arc / ncells[cc]  #[arcsec] pixel size

            ## Run over particle reductions
            for mm in range(len(nparts)):
                smlpixel = 20  # maximum smoothing pixel length
                pos, indx = dmap.select_particles(
                    Gas['Pos'],
                    SH['Pos'][ll],  #*a/h,
                    fov_Mpc,
                    'box')
                gas_sigma = dmap.projected_density_pmesh_adaptive(
                    pos[::nparts[mm], :],
                    Gas['Mass'][indx][::nparts[mm]],
                    fov_Mpc,
                    ncells[cc],
                    hmax=smlpixel)
                pos, indx = dmap.select_particles(
                    Star['Pos'],
                    SH['Pos'][ll],  #*a/h,
                    fov_Mpc,
                    'box')
                star_sigma = dmap.projected_density_pmesh_adaptive(
                    pos[::nparts[mm], :],
                    Star['Mass'][indx][::nparts[mm]],
                    fov_Mpc,
                    ncells[cc],
                    hmax=smlpixel)
                pos, indx = dmap.select_particles(
                    DM['Pos'],
                    SH['Pos'][ll],  #*a/h,
                    fov_Mpc,
                    'box')
                dm_sigma = dmap.projected_density_pmesh_adaptive(
                    pos[::nparts[mm], :],
                    DM['Mass'][indx][::nparts[mm]],
                    fov_Mpc,  #[Mpc]
                    ncells[cc],
                    hmax=smlpixel)
                tot_sigma = dm_sigma + gas_sigma + star_sigma

                # Make sure that density-map is filled
                while 0.0 in tot_sigma:
                    smlpixel += 5
                    dm_sigma = dmap.projected_density_pmesh_adaptive(
                        pos[::nparts[mm], :],
                        DM['Mass'][indx][::nparts[mm]],
                        fov_Mpc,  #[Mpc]
                        ncells[cc],
                        hmax=smlpixel)
                    tot_sigma = dm_sigma + gas_sigma + star_sigma
                #tmap.plotting(tot_sigma, ncells[cc], fov_Mpc, zl)

                # initialize the coordinates of grids (light rays on lens plan)
                lpv = np.linspace(-(fov_arc - dsx_arc) / 2,
                                  (fov_arc - dsx_arc) / 2, ncells[cc])
                lp1, lp2 = np.meshgrid(lpv, lpv)  #[arcsec]

                fig = plt.figure()
                ax = fig.add_subplot(111)
                # Calculate convergence map
                kappa = tot_sigma / sigma_cr

                # Calculate Deflection Maps
                alpha1, alpha2, mu_map, phi, detA, lambda_t = cal_lensing_signals(
                    kappa, fov_arc, ncells[cc])
                # Calculate Einstein Radii
                Rein[ll, cc, mm] = einstein_radii(lp1, lp2, detA, lambda_t, zl,
                                                  cosmo, ax, 'med', ll)
                #print('Rein = %f' % Rein[ll, cc, mm])
                ID[ll] = SH['ID'][ll]
                #plt.close(fig)
    output = {}
    for cc in range(len(ncells)):
        for mm in range(len(nparts)):
            output[(str(ncells[cc]), str(nparts[mm]))] = Rein[:, cc, mm]
    df = pd.DataFrame.from_dict(output)
    df['ID'] = ID
    #self.df = pd.concat([self.df, dfp], axis=1)
    fname = 'DMConvTest_' + label + '_' + str(comm_rank) + '_zs150.h5'
    df.to_hdf(fname, key='Rein', mode='w')
    plt.close(fig)
def create_density_maps():
    time_start = time.time()
    # Get command line arguments
    args = {}
    if comm_rank == 0:
        args["simdir"] = sys.argv[1]
        args["hfdir"] = sys.argv[2]
        args["lcdir"] = sys.argv[3]
        args["ncells"] = int(sys.argv[4])
        args["walltime"] = int(sys.argv[5])
        args["outbase"] = sys.argv[6]
    args = comm.bcast(args, root=0)
    label = args["simdir"].split('/')[-2].split('_')[2]
    hflabel = whichhalofinder(args["lcdir"])

    # Load LightCone Contents
    if comm_rank == 0:
        lchdf = h5py.File(args["lcdir"], 'r')
        dfhalo = pd.DataFrame({
            'HF_ID': lchdf['Halo_Rockstar_ID'].value,
            'ID': lchdf['Halo_ID'].value,
            'Halo_z': lchdf['Halo_z'].value,
            'snapnum': lchdf['snapnum'].value,
            'Vrms': lchdf['VelDisp'].value,
            'fov_Mpc': lchdf['FOV'][:][1],
            ('HaloPosBox', 'X'): lchdf['HaloPosBox'][:, 0],
            ('HaloPosBox', 'Y'): lchdf['HaloPosBox'][:, 1],
            ('HaloPosBox', 'Z'): lchdf['HaloPosBox'][:, 2]
        })
        nhalo_per_snapshot = dfhalo.groupby('snapnum').count()['HF_ID']
        snapshots = dfhalo.groupby('snapnum').count().index.values
        dfhalo = dfhalo.sort_values(by=['snapnum'])
    else:
        nhalo_per_snapshot = None
    nhalo_per_snapshot = comm.bcast(nhalo_per_snapshot, root=0)

    sigma_tot = []
    out_hfid = []
    out_lcid = []
    out_redshift = []
    out_snapshot = []
    out_vrms = []
    out_fov = []
    ## Run over Snapshots
    for ss in range(len(nhalo_per_snapshot))[-2:]:
        print('Snapshot %d of %d' % (ss, len(nhalo_per_snapshot)))

        if comm_rank == 0:
            dfhalosnap = dfhalo.loc[dfhalo['snapnum'] == snapshots[ss]]
            # Load simulation
            s = read_hdf5.snapshot(snapshots[ss], args["simdir"])
            s.read(["Coordinates", "Masses", "GFM_StellarFormationTime"],
                   parttype=[0, 1, 4, 5])
            scale = 1e-3 * s.header.hubble
            cosmo = LambdaCDM(H0=s.header.hubble * 100,
                              Om0=s.header.omega_m,
                              Ode0=s.header.omega_l)
            print(': Redshift: %f' % s.header.redshift)

            sh_hfid = dfhalosnap['HF_ID'].values
            sh_id = dfhalosnap['ID'].values
            sh_red = dfhalosnap['Halo_z'].values
            sh_snap = dfhalosnap['snapnum'].values
            sh_vrms = dfhalosnap['Vrms'].values
            sh_fov = dfhalosnap['fov_Mpc'].values
            sh_x = dfhalosnap[('HaloPosBox', 'X')].values
            sh_y = dfhalosnap[('HaloPosBox', 'Y')].values
            sh_z = dfhalosnap[('HaloPosBox', 'Z')].values
            hist_edges = procdiv.histedges_equalN(sh_x, comm_size)
            SH = procdiv.cluster_subhalos_lc(sh_hfid, sh_id, sh_red, sh_snap,
                                             sh_vrms, sh_fov, sh_x, sh_y, sh_z,
                                             hist_edges, comm_size)

            ## Dark Matter
            DM = {
                'Mass': (s.data['Masses']['dm']).astype('float64'),
                'Pos': (s.data['Coordinates']['dm'] * scale).astype('float64')
            }
            ## Gas
            Gas = {
                'Mass': (s.data['Masses']['gas']).astype('float64'),
                'Pos': (s.data['Coordinates']['gas'] * scale).astype('float64')
            }
            ## Stars
            age = (
                s.data['GFM_StellarFormationTime']['stars']).astype('float64')
            Star = {
                'Mass':
                (s.data['Masses']['stars'][age >= 0]).astype('float64'),
                'Pos': (s.data['Coordinates']['stars'][age >= 0, :] *
                        scale).astype('float64')
            }
            ## BH
            BH = {
                'Mass': (s.data['Masses']['bh']).astype('float64'),
                'Pos': (s.data['Coordinates']['bh'] * scale).astype('float64')
            }

            # Calculate overlap for particle cuboids
            c = (const.c).to_value('km/s')
            fov_rad = 4 * np.pi * (np.percentile(SH['Vrms'], 90) / c)**2
            sh_dist = (cosmo.comoving_distance(
                s.header.redshift)).to_value('Mpc')
            alpha = 6  # multiplied by 4 because of Oguri&Marshall
            overlap = 0.5 * alpha * fov_rad * sh_dist  #[Mpc] half of field-of-view
            print('Cuboids overlap is: %f [Mpc]' % overlap)

            DM = procdiv.cluster_particles(DM, hist_edges, comm_size)
            Gas = procdiv.cluster_particles(Gas, hist_edges, comm_size)
            Star = procdiv.cluster_particles(Star, hist_edges, comm_size)
            BH = procdiv.cluster_particles(BH, hist_edges, comm_size)
        else:
            overlap = None
            hist_edges = None
            SH = {
                'HF_ID': None,
                'ID': None,
                'redshift': None,
                'snapshot': None,
                'Vrms': None,
                'fov_Mpc': None,
                'X': None,
                'Y': None,
                'Z': None,
                'split_size_1d': None,
                'split_disp_1d': None
            }
            DM = {
                'Mass': None,
                'X': None,
                'Y': None,
                'Z': None,
                'split_size_1d': None,
                'split_disp_1d': None
            }
            Gas = {
                'Mass': None,
                'X': None,
                'Y': None,
                'Z': None,
                'split_size_1d': None,
                'split_disp_1d': None
            }
            Star = {
                'Mass': None,
                'X': None,
                'Y': None,
                'Z': None,
                'split_size_1d': None,
                'split_disp_1d': None
            }
            BH = {
                'Mass': None,
                'X': None,
                'Y': None,
                'Z': None,
                'split_size_1d': None,
                'split_disp_1d': None
            }
        # Broadcast variables over all processors
        overlap = comm.bcast(overlap, root=0)
        hist_edges = comm.bcast(hist_edges, root=0)
        sh_split_size_1d = comm.bcast(SH['split_size_1d'], root=0)
        dm_split_size_1d = comm.bcast(DM['split_size_1d'], root=0)
        gas_split_size_1d = comm.bcast(Gas['split_size_1d'], root=0)
        star_split_size_1d = comm.bcast(Star['split_size_1d'], root=0)
        bh_split_size_1d = comm.bcast(BH['split_size_1d'], root=0)

        SH = procdiv.scatter_subhalos_lc(SH,
                                         sh_split_size_1d,
                                         comm_rank,
                                         comm,
                                         root_proc=0)
        DM = procdiv.scatter_particles(DM,
                                       dm_split_size_1d,
                                       comm_rank,
                                       comm,
                                       root_proc=0)
        Gas = procdiv.scatter_particles(Gas,
                                        gas_split_size_1d,
                                        comm_rank,
                                        comm,
                                        root_proc=0)
        Star = procdiv.scatter_particles(Star,
                                         star_split_size_1d,
                                         comm_rank,
                                         comm,
                                         root_proc=0)
        BH = procdiv.scatter_particles(BH,
                                       bh_split_size_1d,
                                       comm_rank,
                                       comm,
                                       root_proc=0)

        print(
            ': Proc. %d got: \n\t %d Sub-&Halos \n\t %d dark matter \n\t %d gas \n\t %d stars \n'
            % (comm_rank, int(
                sh_split_size_1d[comm_rank]), int(dm_split_size_1d[comm_rank]),
               int(gas_split_size_1d[comm_rank]),
               int(star_split_size_1d[comm_rank])))

        ## Run over Sub-&Halos
        for ll in range(len(SH['ID'])):
            print('Lens %d' % (ll))
            #TODO: for z=0 sh_dist=0!!!

            smlpixel = 20  # maximum smoothing pixel length
            ## BH
            pos, indx = dmaps.select_particles(
                BH['Pos'],
                SH['Pos'][ll],  #*a/h,
                SH['fov_Mpc'][ll],
                'box')
            bh_sigma = dmaps.projected_density_pmesh_adaptive(
                pos,
                BH['Mass'][indx],
                SH['fov_Mpc'][ll],
                args["ncells"],
                hmax=smlpixel)
            ## Star
            pos, indx = dmaps.select_particles(
                Star['Pos'],
                SH['Pos'][ll],  #*a/h,
                SH['fov_Mpc'][ll],
                'box')
            star_sigma = dmaps.projected_density_pmesh_adaptive(
                pos,
                Star['Mass'][indx],
                SH['fov_Mpc'][ll],
                args["ncells"],
                hmax=smlpixel)
            ## Gas
            pos, indx = dmaps.select_particles(
                Gas['Pos'],
                SH['Pos'][ll],  #*a/h
                SH['fov_Mpc'][ll],
                'box')
            gas_sigma = dmaps.projected_density_pmesh_adaptive(
                pos,
                Gas['Mass'][indx],
                SH['fov_Mpc'][ll],
                args["ncells"],
                hmax=smlpixel)
            ## DM
            pos, indx = dmaps.select_particles(
                DM['Pos'],
                SH['Pos'][ll],  #*a/h
                SH['fov_Mpc'][ll],
                'box')
            dm_sigma = dmaps.projected_density_pmesh_adaptive(
                pos,
                DM['Mass'][indx],
                SH['fov_Mpc'][ll],  #[Mpc]
                args["ncells"],
                hmax=smlpixel)
            sigmatotal = dm_sigma + gas_sigma + star_sigma + bh_sigma

            # Make sure that density-map if filled
            while 0.0 in sigmatotal:
                smlpixel += 5
                dm_sigma = dmaps.projected_density_pmesh_adaptive(
                    pos,
                    DM['Mass'][indx],
                    SH['fov_Mpc'][ll],  #[Mpc]
                    args["ncells"],
                    hmax=smlpixel)
                sigmatotal = dm_sigma + gas_sigma + star_sigma + bh_sigma

            #tmap.plotting(sigmatotal, args["ncells"],
            #              SH['fov_Mpc'][ll], SH['redshift'][ll])
            sigma_tot.append(sigmatotal)
            out_hfid.append(SH['HF_ID'][ll])
            out_lcid.append(SH['ID'][ll])
            out_redshift.append(SH['redshift'][ll])
            out_snapshot.append(SH['snapshot'][ll])
            out_vrms.append(SH['Vrms'][ll])
            out_fov.append(SH['fov_Mpc'][ll])
            if args["walltime"] - (time_start - time.time()) / (60 *
                                                                60) < 0.25:
                fname = args["outbase"] + 'DM_' + label + '_lc.h5'
                hf = h5py.File(fname, 'w')
                hf.create_dataset('density_map', data=sigma_tot)
                hf.create_dataset('HF_ID', data=np.asarray(out_hfid))
                hf.create_dataset('LC_ID', data=np.asarray(out_lcid))
                hf.create_dataset('redshift', data=np.asarray(out_redshift))
                hf.create_dataset('snapshot', data=np.asarray(out_snapshot))
                hf.create_dataset('Vrms', data=np.asarray(out_vrms))
                hf.create_dataset('fov_Mpc', data=np.asarray(out_fov))
                hf.close()

    # Gather the Results
    #comm.Barrier()
    #comm.Gather(out_hfid, [rootout_hfid,split_sizes,displacements,MPI.DOUBLE], root=0)

    fname = args["outbase"] + 'DM_' + label + '_lc.h5'
    hf = h5py.File(fname, 'w')
    hf.create_dataset('density_map', data=sigma_tot)
    hf.create_dataset('HF_ID', data=np.asarray(out_hfid))
    hf.create_dataset('LC_ID', data=np.asarray(out_lcid))
    hf.create_dataset('redshift', data=np.asarray(out_redshift))
    hf.create_dataset('snapshot', data=np.asarray(out_snapshot))
    hf.create_dataset('Vrms', data=np.asarray(out_vrms))
    hf.create_dataset('fov_Mpc', data=np.asarray(out_fov))
    #RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
    hf.close()
Exemple #4
0
def create_density_maps():
    # Get command line arguments
    args = {}
    if comm_rank == 0:
        print(':Registered %d processes' % comm_size)
        args["simdir"] = sys.argv[1]
        args["hfname"] = sys.argv[2]
        args["hfdir"] = sys.argv[3]
        args["snapnum"] = int(sys.argv[4])
        args["ncells"] = int(sys.argv[5])
        args["smlpixel"] = int(sys.argv[6])
        args["outbase"] = sys.argv[7]
    args = comm.bcast(args)
    label = args["simdir"].split('/')[-2].split('_')[2]

    # Organize devision of Sub-&Halos over Processes on Proc. 0
    if comm_rank == 0:
        # Load simulation
        s = read_hdf5.snapshot(args["snapnum"], args["simdir"])
        s.read(["Coordinates", "Masses", "GFM_StellarFormationTime"],
               parttype=[0, 1, 4, 5])

        unitlength = dmaps.define_unit(s.header.unitlength)
        # Define Cosmology
        cosmo = LambdaCDM(H0=s.header.hubble * 100,
                          Om0=s.header.omega_m,
                          Ode0=s.header.omega_l)
        redshift = s.header.redshift
        print(': Redshift: %f' % redshift)

        # Sort Sub-&Halos over Processes
        SH = subhalo_data(args["hfdir"], args["hfname"], args["snapnum"],
                          s.header.hubble, s.header.unitlength)
        hist_edges = procdiv.histedges_equalN(SH['X'], comm_size)
        SH = procdiv.cluster_subhalos_box(SH, hist_edges, comm_size)

        # Calculate overlap for particle cuboids
        c = (const.c).to_value('km/s')
        fov_rad = 4 * np.pi * (np.percentile(SH['Vrms'], 90) / c)**2
        sh_dist = (cosmo.comoving_distance(redshift)).to_value(unitlength)
        alpha = 2  # multiplied by 4 because of Oguri&Marshall
        overlap = 0.5 * alpha * fov_rad * sh_dist  # half of field-of-view
        print('Cuboids overlap is: %f [%s]' % (overlap, unitlength))

        # Sort Particles over Processes
        DM, Gas, Star, BH = particle_data(s.data, s.header.hubble, unitlength)
        DM = procdiv.cluster_particles(DM, hist_edges, comm_size)
        Gas = procdiv.cluster_particles(Gas, hist_edges, comm_size)
        Star = procdiv.cluster_particles(Star, hist_edges, comm_size)
        BH = procdiv.cluster_particles(BH, hist_edges, comm_size)

    else:
        c = None
        alpha = None
        overlap = None
        unitlength = None
        cosmo = None
        redshift = None
        hist_edges = None
        SH = {
            'ID': None,
            'Vrms': None,
            'X': None,
            'Y': None,
            'Z': None,
            'split_size_1d': None,
            'split_disp_1d': None
        }
        DM = {
            'Mass': None,
            'X': None,
            'Y': None,
            'Z': None,
            'split_size_1d': None,
            'split_disp_1d': None
        }
        Gas = {
            'Mass': None,
            'X': None,
            'Y': None,
            'Z': None,
            'split_size_1d': None,
            'split_disp_1d': None
        }
        Star = {
            'Mass': None,
            'X': None,
            'Y': None,
            'Z': None,
            'split_size_1d': None,
            'split_disp_1d': None
        }
        BH = {
            'Mass': None,
            'X': None,
            'Y': None,
            'Z': None,
            'split_size_1d': None,
            'split_disp_1d': None
        }

    # Broadcast variables over all processors
    sh_split_size_1d = comm.bcast(SH['split_size_1d'], root=0)
    sh_split_disp_1d = comm.bcast(SH['split_disp_1d'], root=0)
    dm_split_size_1d = comm.bcast(DM['split_size_1d'], root=0)
    dm_split_disp_1d = comm.bcast(DM['split_disp_1d'], root=0)
    gas_split_size_1d = comm.bcast(Gas['split_size_1d'], root=0)
    gas_split_disp_1d = comm.bcast(Gas['split_disp_1d'], root=0)
    star_split_size_1d = comm.bcast(Star['split_size_1d'], root=0)
    star_split_disp_1d = comm.bcast(Star['split_disp_1d'], root=0)
    bh_split_size_1d = comm.bcast(BH['split_size_1d'], root=0)
    bh_split_disp_1d = comm.bcast(BH['split_disp_1d'], root=0)
    c = comm.bcast(c, root=0)
    unitlength = comm.bcast(unitlength, root=0)
    alpha = comm.bcast(alpha, root=0)
    overlap = comm.bcast(overlap, root=0)
    cosmo = comm.bcast(cosmo, root=0)
    redshift = comm.bcast(redshift, root=0)
    hist_edges = comm.bcast(hist_edges, root=0)

    SH = procdiv.scatter_subhalos(SH,
                                  sh_split_size_1d,
                                  comm_rank,
                                  comm,
                                  root_proc=0)
    DM = procdiv.scatter_particles(DM,
                                   dm_split_size_1d,
                                   comm_rank,
                                   comm,
                                   root_proc=0)
    Gas = procdiv.scatter_particles(Gas,
                                    gas_split_size_1d,
                                    comm_rank,
                                    comm,
                                    root_proc=0)
    Star = procdiv.scatter_particles(Star,
                                     star_split_size_1d,
                                     comm_rank,
                                     comm,
                                     root_proc=0)
    BH = procdiv.scatter_particles(BH,
                                   star_split_size_1d,
                                   comm_rank,
                                   comm,
                                   root_proc=0)

    print(
        ': Proc. %d got: \n\t %d Sub-&Halos \n\t %d dark matter \n\t %d gas \n\t %d stars \n'
        % (comm_rank, int(sh_split_size_1d[comm_rank]),
           int(dm_split_size_1d[comm_rank]), int(gas_split_size_1d[comm_rank]),
           int(star_split_size_1d[comm_rank])))

    sigma_tot = []
    subhalo_id = []
    FOV = []
    ## Run over Sub-&Halos
    for ll in range(len(SH['ID'])):
        # Define field-of-view edge-length
        fov_rad = 4 * np.pi * (SH['Vrms'][ll] / c)**2
        #TODO: for z=0 sh_dist=0!!!
        sh_dist = (cosmo.comoving_distance(redshift)).to_value(unitlength)
        alpha = 1.4
        fov = alpha * fov_rad * sh_dist  #[kpc] edge-length of box

        # Check cuboid boundary condition,
        # that all surface densities are filled with particles
        if ((SH['Pos'][ll][0]-hist_edges[comm_rank] < overlap) or
                (hist_edges[comm_rank+1]-overlap < \
                 SH['Pos'][ll][0]-hist_edges[comm_rank])):
            if fov * 0.45 > overlap:
                print("FOV is bigger than cuboids overlap: %f > %f" % \
                        (fov*0.45, overlap))
                continue

        ## BH
        pos, indx = dmaps.select_particles(
            BH['Pos'],
            SH['Pos'][ll],  #*a/h,
            fov,
            'box')
        bh_sigma = dmaps.projected_density_pmesh(pos, BH['Mass'][indx], fov,
                                                 args["ncells"])

        ## Gas
        pos, indx = dmaps.select_particles(
            Gas['Pos'],
            SH['Pos'][ll],  #*a/h,
            fov,
            'box')
        gas_sigma = dmaps.projected_density_pmesh_adaptive(
            pos, Gas['Mass'][indx], fov, args["ncells"], hmax=args["smlpixel"])
        ## Star
        pos, indx = dmaps.select_particles(
            Star['Pos'],
            SH['Pos'][ll],  #*a/h,
            fov,
            'box')
        star_sigma = dmaps.projected_density_pmesh_adaptive(
            pos,
            Star['Mass'][indx],
            fov,
            args["ncells"],
            hmax=args["smlpixel"])
        ## DM
        pos, indx = dmaps.select_particles(
            DM['Pos'],
            SH['Pos'][ll],  #*a/h,
            fov,
            'box')
        dm_sigma = dmaps.projected_density_pmesh_adaptive(
            pos, DM['Mass'][indx], fov, args["ncells"], hmax=args["smlpixel"])
        sigmatotal = dm_sigma + gas_sigma + star_sigma

        # Make sure that density-map if filled
        extention = 0
        while (0.0 in sigmatotal) and (extention < 60):
            extention += 5
            dm_sigma = dmaps.projected_density_pmesh_adaptive(
                pos,
                DM['Mass'][indx],
                fov,
                args["ncells"],
                hmax=args["smlpixel"] + extention)
            sigmatotal = dm_sigma + gas_sigma + star_sigma

        #tmap.plotting(sigmatotal, args["ncells"], fov, 0.57)
        sigma_tot.append(sigmatotal)
        subhalo_id.append(int(SH['ID'][ll]))
        FOV.append(fov)

    fname = args["outbase"] + 'z_' + str(
        args["snapnum"]) + '/' + 'DM_' + label + '_' + str(comm_rank) + '.h5'
    hf = h5py.File(fname, 'w')
    hf.create_dataset('DMAP',
                      data=sigma_tot)  # density map in unit of simulation
    hf.create_dataset('HFID',
                      data=np.asarray(subhalo_id))  # Rockstar sub-&halo id
    hf.create_dataset(
        'FOV', data=np.asarray(FOV))  # field-of-view in units #[kpc, Mpc]
    #RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
    hf.close()
    ## Run over Sub-&Halos
    for ll in range(len(SH['ID'])):
        #TODO: for z=0 sh_dist=0!!!
        
        # Check cuboid boundary condition,
        # that all surface densities are filled with particles
        if ((SH['Pos'][ll][0]-hist_edges[comm_rank] < overlap) or
                (hist_edges[comm_rank+1]-overlap < \
                 SH['Pos'][ll][0]-hist_edges[comm_rank])):
            if fov_Mpc*0.45 > overlap:
                print("FOV is bigger than cuboids overlap: %f > %f" % \
                        (fov_Mpc*0.45, overlap))
                continue

        smlpixel = 20  # maximum smoothing pixel length
        indx = dmaps.select_particles(Gas['Pos'], SH['Pos'][ll], fov_Mpc, 'box')
        gas_sigma = dmaps.projected_density_pmesh_adaptive(
                Gas['Pos'][indx,:], Gas['Mass'][indx],
                SH['Pos'][ll], #*a/h,
                fov_Mpc,
                args["ncells"],
                hmax=smlpixel,
                particle_type=0)
        indx = dmaps.select_particles(Star['Pos'], SH['Pos'][ll], fov_Mpc, 'box')
        star_sigma = dmaps.projected_density_pmesh_adaptive(
                Star['Pos'][indx,:],Star['Mass'][indx],
                SH['Pos'][ll], #*a/h,
                fov_Mpc,
                args["ncells"],
                hmax=smlpixel,
                particle_type=4)