Пример #1
0
def createField(name, info, energy, redshift, photonDensity, outDir):
    try:
        git_hash = gh.get_git_revision_hash()
        addHash = True
    except:
        addHash = False

    with open(outDir + "/" + name + "_photonEnergy.txt", 'w') as f:
        f.write(info + '\n')
        if addHash:
            f.write("# Produced with crpropa-data version: " + git_hash + "\n")
        f.write("# photon energies in [J]\n")
        for e in energy:
            f.write("{}\n".format(e * eV))  # [J]
    if redshift is not None:
        with open(outDir + "/" + name + "_redshift.txt", 'w') as f:
            f.write(info + '\n')
            if addHash:
                f.write("# Produced with crpropa-data version: " + git_hash +
                        "\n")
            f.write("# redshift\n")
            for z in redshift:
                f.write("{}\n".format(np.round(z, 2)))
    with open(outDir + "/" + name + "_photonDensity.txt", 'w') as f:
        f.write(info + '\n')
        if addHash:
            f.write("# Produced with crpropa-data version: " + git_hash + "\n")
        f.write(
            "# Comoving photon number density in [m^-3], format: d(e1,z1), ... , d(e1,zm), d(e2,z1), ... , d(e2,zm), ... , d(en,zm)\n"
        )
        for i, densSlice in enumerate(photonDensity):
            for d in densSlice:
                f.write("{}\n".format(d * energy[i] /
                                      cm3))  # [# / m^3], comoving
    print("done: " + name)
Пример #2
0
def compute_spectrum(x, outputName):
    """
    Cumulative differential synchrotron spectrum. 
    This implementation follows:
      J.~D. Jackson, Classical Electrondynamics.
      Wiley, 3rd ed., p. 681, eq. 14.91

    Input
    . x: fraction between photon frequency and critical frequency
    . outputName: name of output file
    """
    cdf = synchrotron_spectrum(x)
    lx = np.log10(x)
    data = np.c_[lx, cdf]
    # Add git hash of crpropa-data repository to header
    try:
        git_hash = gh.get_git_revision_hash()
        header = 'Produced with crpropa-data version: ' + git_hash + '\nx\t: photon frequency to critical frequency fraction\nlog10(x)\tCDF\n'
    except:
        header = 'x\t: photon frequency to critical frequency fraction\nlog10(x)\tCDF\n'
    fmt = '%3.2f\t%7.6e'
    np.savetxt(outputName, data, fmt=fmt, header=header)
Пример #3
0
]

for field in fields:
    print(field.name)

    data = field.data  # dictionary {z : (eps, n(eps))}
    tz = np.array(list(data.keys()))
    tz.sort()
    ts = np.zeros_like(tz)

    for i, z in enumerate(tz):
        eps, n = data[z]
        ts[i] = trapz(n, eps)

    ts /= ts[0]

    # output folder
    folder = 'data/Scaling'
    if not os.path.exists(folder):
        os.makedirs(folder)

    try:
        git_hash = gh.get_git_revision_hash()
        header = 'Produced with crpropa-data version: ' + git_hash + '\nredshift\t global evolution factor'
    except:
        header = 'redshift\t global evolution factor'
    np.savetxt('data/Scaling/%s_scaling.txt' % field.name,
               np.c_[tz, ts],
               fmt='%.2f\t%.4e',
               header=header)
def process(sigma, field, name):
    # output folder
    folder = 'data/' + name
    if not os.path.exists(folder):
        os.makedirs(folder)

    # tabulated energies, limit to energies where the interaction is possible
    Emin = getEmin(sigma, field)
    E = np.logspace(9, 23, 281) * eV
    E = E[E > Emin]

    # -------------------------------------------
    # calculate interaction rates
    # -------------------------------------------
    # tabulated values of s_kin = s - mc^2
    # Note: integration method (Romberg) requires 2^n + 1 log-spaced tabulation points
    s_kin = np.logspace(4, 23, 2**18 + 1) * eV**2
    xs = getTabulatedXS(sigma, s_kin)
    rate = interactionRate.calc_rate_s(s_kin, xs, E, field)

    # save
    fname = folder + '/rate_%s.txt' % field.name
    data = np.c_[np.log10(E / eV), rate]
    fmt = '%.2f\t%8.7e'
    try:
        git_hash = gh.get_git_revision_hash()
        header = ("%s interaction rates\nphoton field: %s\n" %
                  (name, field.info) + "Produced with crpropa-data version: " +
                  git_hash + "\n" + "log10(E/eV), 1/lambda [1/Mpc]")
    except:
        header = ("%s interaction rates\nphoton field: %s\n" %
                  (name, field.info) + "log10(E/eV), 1/lambda [1/Mpc]")
    np.savetxt(fname, data, fmt=fmt, header=header)

    # -------------------------------------------
    # calculate cumulative differential interaction rates for sampling s values
    # -------------------------------------------
    # find minimum value of s_kin
    skin1 = getSmin(sigma)  # s threshold for interaction
    skin2 = 4 * field.getEmin() * E[
        0]  # minimum achievable s in collision with background photon (at any tabulated E)
    skin_min = max(skin1, skin2)

    # tabulated values of s_kin = s - mc^2, limit to relevant range
    # Note: use higher resolution and then downsample
    skin = np.logspace(4, 23, 380000 + 1) * eV**2
    skin = skin[skin > skin_min]

    xs = getTabulatedXS(sigma, skin)
    rate = interactionRate.calc_rate_s(skin, xs, E, field, cdf=True)

    # downsample
    skin_save = np.logspace(4, 23, 190 + 1) * eV**2
    skin_save = skin_save[skin_save > skin_min]
    rate_save = np.array([np.interp(skin_save, skin, r) for r in rate])

    # save
    data = np.c_[np.log10(E / eV),
                 rate_save]  # prepend log10(E/eV) as first column
    row0 = np.r_[0, np.log10(skin_save / eV**2)][np.newaxis]
    data = np.r_[row0, data]  # prepend log10(s_kin/eV^2) as first row

    fname = folder + '/cdf_%s.txt' % field.name
    fmt = '%.2f' + '\t%6.5e' * np.shape(rate_save)[1]
    try:
        git_hash = gh.get_git_revision_hash()
        header = (
            "%s cumulative differential rate\nphoton field: %s\n" %
            (name, field.info) + "Produced with crpropa-data version: " +
            git_hash + "\n" +
            "log10(E/eV), d(1/lambda)/ds_kin [1/Mpc/eV^2] for log10(s_kin/eV^2) as given in first row"
        )
    except:
        header = (
            "%s cumulative differential rate\nphoton field: %s\n" %
            (name, field.info) +
            "log10(E/eV), d(1/lambda)/ds_kin [1/Mpc/eV^2] for log10(s_kin/eV^2) as given in first row"
        )
    np.savetxt(fname, data, fmt=fmt, header=header)

    del data, rate, skin, skin_save, rate_save
Пример #5
0
        print()
    else:
        sys.stdout.flush()

    infile.close()
print()

duration = time.time() - start
log.debug("Total time to read data: {}s, {}s per file".format(duration, duration / len(args.inputfiles)))


cfg = open(os.path.join(args.lensname, 'lens.cfg'), 'w')
cfg.write('# Magnetic lens {} for usage with CRPRopa3\n'.format(args.lensname))
cfg.write('# Created on {} with {} backtracked particles\n'.format(time.ctime(), tot_cr))
try:
    cfg.write('# Produced with crpropa-data version: {}\n'.format(gh.get_git_revision_hash())
except:
    pass
cfg.write('#\n')
cfg.write('# fname log(Rmin / eV) log(Rmax / eV)\n')
cfg.write('#\n')

def rigidity_processor(filename, data):
    try:
        mldat_of = open(filename, 'wb')
        # Write header information
        mldat_of.write(struct.pack("<I", data.nnz))
        mldat_of.write(struct.pack("<I", npix))
        mldat_of.write(struct.pack("<I", npix))

        # Normalize rows to account for anisotropic particle emission