Ejemplo n.º 1
0
def fdelx(
    vpara, v, vi, B, e, Egg
):  # time the electron moves (De/v), angular vel (v/R), speed of el (v,vi) , mag field B, charge e (+/- 1)
    # w2,tau2=w,tau
    if gv.STODE == 1:
        w2 = gv.drawMFPe(Egg, gv.zE(gv.dS)) / v
    else:
        w2 = gv.De(gv.zE(gv.dS), gv.Ee(Egg, gv.zE(gv.dS))) / v
    tau2 = v * 1.0 / gv.R(gv.Ee(Egg, gv.zE(gv.dS)), gv.B0, gv.zE(gv.dS))
    return v * (vpara * (tau2 - np.sin(w2 * tau2) / w2) * B +
                vi * np.sin(w2 * tau2) / w2 + e *
                (np.cos(w2 * tau2) - 1.0) * np.cross(B, vi) / w2)
Ejemplo n.º 2
0
 def F(angles):
     x, y, z = getxyzB(dg, dE, angles)
     fB = B(x, y, z)
     bnorm = np.sqrt(fB[0] * fB[0] + fB[1] * fB[1] + fB[2] * fB[2] + 1e-20)
     if RnB:
         w = bnorm / (RnB * 1e-14)
     else:
         w = 1.0 / gv.R(gv.Ee(Egg, gv.zE(gv.dS)), bnorm, gv.zE(gv.dS))
     wtau = w * tau
     cons1 = fLoS(angles[0], angles[2], dg, dE)
     cons2 = fvcBdphi(
         fBz(fB, angles[0], angles[1]),
         fBrho(fB, angles[0], angles[1]),
         angles[2],
         angles[0],
     )
     cons3 = fdeltaEqn(angles[2],
                       fvpara(fB / bnorm, angles[0], angles[1], angles[2]),
                       Egg, wtau)
     return (cons1, cons2, cons3)
Ejemplo n.º 3
0
def findmorp(steps, ds, xtol=1e-10):
    solang = []
    tcospsol = [[], []]
    tsinpsol = [[], []]
    tG, pG, deltaG = gv.tG, gv.pG, gv.deltaG
    for i in range(steps):
        Eexp = np.log10(Emin) + (np.log10(Emax) - np.log10(Emin)) * np.power(
            1 - i * 1.0 / (steps - 1), 2.0)
        Egg = np.power(10.0, Eexp)
        sol, ier, charge = co.fsolvecsq(gv.MFPg(gv.zE(ds), Egg),
                                        ds,
                                        Egg,
                                        tG,
                                        pG,
                                        deltaG,
                                        1,
                                        Morp=True,
                                        xtol=xtol)
        tG, pG, deltaG = sol[0], sol[1], sol[2]
        solang.append(sol)
        tcospsol[0].append(sol[0] * 1.0 / gv.degrad * np.cos(sol[1]))
        tsinpsol[0].append(sol[0] * 1.0 / gv.degrad * np.sin(sol[1]))

    tG, pG, deltaG = -gv.tG, gv.pG, -gv.deltaG
    for i in range(steps):
        Eexp = np.log10(Emin) + (np.log10(Emax) - np.log10(Emin)) * np.power(
            1 - i * 1.0 / (steps - 1), 2.0)
        Egg = np.power(10.0, Eexp)
        sol, ier, charge = co.fsolvecsq(gv.MFPg(gv.zE(ds), Egg),
                                        ds,
                                        Egg,
                                        tG,
                                        pG,
                                        deltaG,
                                        0,
                                        Morp=True)
        tG, pG, deltaG = sol[0], sol[1], sol[2]
        solang.append(sol)
        tcospsol[1].append(sol[0] * 1.0 / gv.degrad * np.cos(sol[1]))
        tsinpsol[1].append(sol[0] * 1.0 / gv.degrad * np.sin(sol[1]))
    return tcospsol, tsinpsol
Ejemplo n.º 4
0
def drawens(N):
    countphoton = 0
    draw = []
    while countphoton < N:
        EggT = gv.drawEnergy()
        Eelini = gv.Ee(EggT, gv.zE(gv.dS)) / 1000.0  # Ee initial in TeV
        while EggT < gv.Emin or Eelini * 2 > gv.Esourcecutoff:
            EggT = gv.drawEnergy()
            Eelini = gv.Ee(EggT, gv.zE(gv.dS)) / 1000.0  # Ee initial in TeV
            # print(Egg,np.sqrt(Egg/77.0)*10)
        dg = gv.drawMFPg(EggT, gv.zE(gv.dS))
        Egg = EggT
        tau0 = 0
        if gv.STODE == 1:
            ind = len(edists[edists < Eelini]) - 1
            tau0, Egg = pd.drawLeptonICevent(dist[ind], pdf_E[ind])
            if tau0 == 0:
                print(edists[ind], Egg, pdf_E[ind], "kek", countphoton, ind)
        draw.append([Eelini, Egg, tau0, dg])
        countphoton += 1
    return np.array(draw)
Ejemplo n.º 5
0
import time, sys, os
import numpy as np

if not sys.version_info[0] < 3:
    from importlib import reload
import globalvars as gv

import matplotlib.pyplot as plt


def Eg(E, z=0.24):
    # return 77.0*4*E*E/100.0*1.24/(1+z)
    return E * E / (gv.me * gv.me) * 1e6 * gv.ECMB(z) * 4.0 / 3.0


Ecutoff = gv.Ee(gv.Emin, gv.zE(gv.dS)) / 1000
Eemax = (gv.Esourcecutoff + 0.5) / 2.0
Eini = 5
Nsim = 10000
nbins = 100

fourpialphasquare = 4 * np.pi / (137.036**2)
ncmb = 3.71 * 10**8
mtokpc = 3.24e-20
TeVtokpc = 8.065e17 / mtokpc
meTeV = 0.511e-6
n0 = 3.71e8 / (mtokpc**3)

NUMde = TeVtokpc**2 / (n0 * fourpialphasquare)

Ejemplo n.º 6
0
def SimEvent(ds, NumPhotons, cn=1, rmf_switch=False, B0=gv.B0):
    if rmf_switch:
        kmode = 0.05
        Nmodes = 10
        helicity = 0
        np.save("Btest", rmf.createB_one_K(kmode, Nmodes, hel=helicity))
        rmf.setB = np.load("Btest.npy")
        co.B = rmf.Banalytic
    countphoton = 0
    tol = 1e-12
    events = []
    evsangle = []
    tG, pG, deltaG = gv.tG, gv.pG, gv.deltaG
    while countphoton < NumPhotons:
        EggT = gv.drawEnergy()
        SignTheta = 0  # determining if the solution has theta positive or negative.
        tG, pG, deltaG = -gv.tG, gv.pG, -gv.deltaG
        # while(Egg<gv.Emin or Egg>gv.Emax):
        Eelini = gv.Ee(EggT, gv.zE(ds)) / 1000.0  # Ee initial in TeV
        while EggT < gv.Emin or Eelini * 2 > gv.Esourcecutoff:
            EggT = gv.drawEnergy()
            Eelini = gv.Ee(EggT, gv.zE(ds)) / 1000.0  # Ee initial in TeV
            # print(Egg,np.sqrt(Egg/77.0)*10)

        dg = gv.drawMFPg(EggT, gv.zE(ds))

        if gv.STODE == 1:
            ind = len(edists[edists < Eelini]) - 1
            tau0, Egg = pd.drawLeptonICevent(dist[ind], pdf_E[ind])
            # tau0=gv.drawMFPe(Egg,0.24)/10
            # if(ind!=len(edists)-1):
            # print(ind,Eelini,gv.Emin,EggT)
            # tau0,Egg=pd.dlice(dist[ind],pdf_E[ind],dist[ind+1],pdf_E[ind+1],Eelini,edists[ind],edists[ind+1])
            if len(RnoB[ind][RnoB[ind][:, 1] < Egg]) > 0:
                avgR = RnoB[ind][RnoB[ind][:, 1] < Egg][0, 0]
            else:
                avgR = RnoB[ind][-1, 0]
        else:
            Egg = EggT
            tau0 = gv.De(gv.zE(ds), gv.Ee(Egg, gv.zE(ds)))
            avgR = 0

        if np.random.uniform() > 0.5:
            SignTheta = 1
            tG, pG, deltaG = gv.tG, gv.pG, gv.deltaG
        # start=time.clock()
        sol, ier, charge = co.fsolvecsq(
            dg, ds, Egg, tG, pG, deltaG, SignTheta, xtol=tol, tau=tau0, RnB=avgR
        )
        # print(time.clock()-start)
        if (
            ier == 1 and np.absolute(sol[0]) < np.pi / 2.0
        ):  # ier=1 if a solution is found by fsolvecsq
            solcart = [
                dg * np.sin(sol[2] - sol[0]) * np.cos(sol[1]),
                dg * np.sin(sol[2] - sol[0]) * np.sin(sol[1]),
                -dg * np.cos(sol[2] - sol[0]),
            ]
            soleves = [sol[2] - sol[0], sol[1], Egg, sol[0]]
            events.append(solcart)
            evsangle.append(soleves)
            countphoton = countphoton + 1

    path = "sim_data/3devents/case" + str(cn) + "/"
    if not os.path.exists(path):
        os.makedirs(path)
    else:
        shutil.rmtree(path)
        os.makedirs(path)
    np.savetxt("sim_data/3devents/case" + str(cn) + "/3devents", events)
    np.savetxt("sim_data/3devents/case" + str(cn) + "/3deventsangle", evsangle)

    return events, evsangle
Ejemplo n.º 7
0
def stomorp(ds,
            NumPhotons,
            alpha=0,
            phi=0,
            omega=np.pi,
            LeptonId=False,
            xtol=1e-10):
    B0 = gv.B0
    if LeptonId:
        gv.chargeanalyze = 1
    countphoton = 0
    events = []
    tcospsol = []
    tsinpsol = []
    tG, pG, deltaG = gv.tG, gv.pG, gv.deltaG
    solcount = 0
    while countphoton < NumPhotons:
        EggT = gv.drawEnergy()
        SignTheta = 0  # determining if the solution has theta positive or negative.
        tG, pG, deltaG = -gv.tG, gv.pG, -gv.deltaG
        # while(Egg<gv.Emin or Egg>gv.Emax):
        Eelini = gv.Ee(EggT, gv.zE(ds)) / 1000.0  # Ee initial in TeV
        while EggT < gv.Emin or Eelini * 2 > gv.Esourcecutoff:
            EggT = gv.drawEnergy()
            Eelini = gv.Ee(EggT, gv.zE(ds)) / 1000.0  # Ee initial in TeV
            # print(Egg,np.sqrt(Egg/77.0)*10)

        dg = gv.drawMFPg(EggT, gv.zE(ds))
        if gv.STODE == 1:
            ind = len(edists[edists < Eelini]) - 1
            tau0, Egg = pd.drawLeptonICevent(dist[ind], pdf_E[ind])
            # tau0=gv.drawMFPe(Egg,0.24)/10
            # if(ind!=len(edists)-1):
            # print(ind,Eelini,gv.Emin,EggT)
            # tau0,Egg=pd.dlice(dist[ind],pdf_E[ind],dist[ind+1],pdf_E[ind+1],Eelini,edists[ind],edists[ind+1])
            if len(RnoB[ind][RnoB[ind][:, 1] < Egg]) > 0:
                avgR = RnoB[ind][RnoB[ind][:, 1] < Egg][0, 0]
            else:
                avgR = RnoB[ind][-1, 0]
        else:
            Egg = EggT
            tau0 = gv.De(gv.zE(ds), gv.Ee(Egg, gv.zE(ds)))

        if np.random.uniform() > 0.5:
            SignTheta = 1
            tG, pG, deltaG = gv.tG, gv.pG, gv.deltaG
        # start=time.clock()
        sol, ier, charge = co.fsolvecsq(dg,
                                        ds,
                                        Egg,
                                        tG,
                                        pG,
                                        deltaG,
                                        SignTheta,
                                        xtol=tol,
                                        tau=tau0,
                                        RnB=avgR)
        # print(time.clock()-start)
        # if(ier!=1): print(solcount,countphoton)
        solcount = solcount + 1

        injet = True
        photonangle = sol[2] - sol[0]
        cosang = np.absolute(
            gv.angledotprod([photonangle, sol[1]], [alpha, phi]))
        if (gv.jetactivated == 1 and np.cos(omega) > cosang
            ):  # the second checks that the solution is part of the jet
            injet = False
            countphoton = countphoton + 1
        if (ier == 1 and np.absolute(sol[0]) < np.pi / 2.0
                and injet):  # ier=1 if a solution is found by fsolvecsq
            events.append([sol[0], sol[1]] + [Egg, charge])
            tcospsol.append(sol[0] * 1.0 / gv.degrad * np.cos(sol[1]))
            tsinpsol.append(sol[0] * 1.0 / gv.degrad * np.sin(sol[1]))
            countphoton = countphoton + 1
    return tcospsol, tsinpsol, events
Ejemplo n.º 8
0
def fsolvecsq_stable(
    dg,
    dE,
    Egg,
    tG,
    pG,
    deltaG,
    ranget,
    Morp=False,
    xtol=1e-10,
    tau=False,
    RnB=False
):  # Solves the Constraints - quite stable, but can jump to other branches
    anglesG = np.array([tG, pG, deltaG])

    x, y, z = getxyzB(dg, dE, anglesG)

    if not tau:
        if gv.STODE == 1 and not Morp:
            tau = gv.drawMFPe(Egg, gv.zE(gv.dS))
        else:
            tau = gv.De(gv.zE(gv.dS), gv.Ee(Egg, gv.zE(gv.dS)))
    w = 1.0 / gv.R(gv.Ee(Egg, gv.zE(gv.dS)), gv.B0, gv.zE(gv.dS))
    wtau = w * tau

    def F(angles):
        x, y, z = getxyzB(dg, dE, angles)
        fB = B(x, y, z)
        bnorm = np.sqrt(fB[0] * fB[0] + fB[1] * fB[1] + fB[2] * fB[2] + 1e-20)
        if RnB:
            w = bnorm / (RnB * 1e-14)
        else:
            w = 1.0 / gv.R(gv.Ee(Egg, gv.zE(gv.dS)), bnorm, gv.zE(gv.dS))
        wtau = w * tau
        cons1 = fLoS(angles[0], angles[2], dg, dE)
        cons2 = fvcBdphi(
            fBz(fB, angles[0], angles[1]),
            fBrho(fB, angles[0], angles[1]),
            angles[2],
            angles[0],
        )
        cons3 = fdeltaEqn(angles[2],
                          fvpara(fB / bnorm, angles[0], angles[1], angles[2]),
                          Egg, wtau)
        return (cons1, cons2, cons3)

    sol, o1, ier, o2 = opt.fsolve(F, anglesG, xtol=xtol, full_output=1)

    charge = 1  # initialize the charge variable, now look what charge this has to be.
    if gv.chargeanalyze == 1:
        xvec = getxyz(dg, dE, sol)
        Bvec = B(xvec[0], xvec[1], xvec[2] + dE)
        orientation = np.dot(xvec, np.cross(fvi(sol[0], sol[1], sol[2]), Bvec))
        temp = np.dot(Bvec, fvi(sol[0], sol[1], sol[2])) / np.linalg.norm(Bvec)
        r = int(np.floor(wtau / (np.pi * np.power(1 - temp * temp, 0.5)))) % 2
        if orientation >= 0:  # the electron initially moves towards us
            if r == 0:  # electron must be the lepton
                charge = -1
            else:  # positron must be the lepton
                charge = 1
        if orientation < 0:  # the electron initially moves away from us
            if r == 0:  # positron must be the lepton
                charge = 1
            else:  # electron must be the lepton
                charge = -1
    return sol, ier, charge
Ejemplo n.º 9
0
def calcqV2(e1vec, e2vec, e3vec, iE, jE, AngCutoff):
    Rmin = gv.MFPg(gv.zE(gv.dS), gv.EBins[iE + 1]) / gv.MFPg(gv.zE(gv.dS), gv.EBins[jE])
    Rmax = gv.MFPg(gv.zE(gv.dS), gv.EBins[iE]) / gv.MFPg(gv.zE(gv.dS), gv.EBins[jE + 1])
    if len(e1vec) * len(e2vec) * len(e3vec) == 0:
        return [np.zeros(len(gv.region)), np.zeros(len(gv.region))]
    # matrix with all the scalar products of e3 and e1 or e2
    dote1e3 = np.dot(e3vec, e1vec.transpose())
    dote2e3 = np.dot(e3vec, e2vec.transpose())
    qval = np.zeros(len(gv.region))
    qerr = np.zeros(len(gv.region))
    for l in range(len(gv.region)):  # start from larger gv.region is faster
        rsize = np.cos(gv.region[len(gv.region) - 1 - l] * gv.degrad)
        # mask pairs that are too far away

        e2inregion = dote2e3 > rsize
        eta1 = np.zeros((e3vec.shape[0], 3))  # N_e3 values of eta1
        eta2 = np.zeros((e3vec.shape[0], 3))  # N_e3 values of eta2
        eta1c2 = np.zeros((e3vec.shape[0], 3))
        for t in range(len(eta1)):
            e2select = e2vec[e2inregion[t]]
            if e2select.shape[0] == 0:
                eta1[t] = np.zeros(3)
                eta2[t] = np.zeros(3)
            else:
                for k in range(len(e2select)):
                    davg = np.sqrt(2 * (1 - np.dot(e2select[k], e3vec[t])))
                    rsizemax = max(np.cos(Rmax * davg), np.cos(gv.degrad * gv.maxangle))
                    rsizemin = np.cos(Rmin * davg)
                    e1inregion = (dote1e3[t] - rsizemax) * (rsizemin - dote1e3[t]) > 0
                    e1select = e1vec[e1inregion]

                    select = []
                    for p in range(len(e1select)):
                        norme1de3 = np.sqrt(2 * (1 + np.dot(e1select[p], e3vec[t])))
                        test = (
                            np.dot(e2select[k] - e3vec[t], e1select[p] - e3vec[t])
                            - davg * np.sqrt(2 * (1 + np.dot(e2select[k], e3vec[t])))
                            - norme1de3 * davg * np.cos(AngCutoff)
                        )
                        if test > 0:
                            select.append(True)
                        else:
                            select.append(False)
                    if e1select[select].shape[0] == 0:
                        eta1[t] = np.zeros(3)
                    else:
                        eta1[t] = np.average(e1select[select], axis=0)
                        # eta1[t]=np.average(e1vec[e1inregion],axis=0)
                    eta1c2[t] = eta1c2[t] + np.cross(eta1[t], e2select[k])
            # calculate cross product

            if e2select.shape[0] == 0:
                eta1c2[t] = np.zeros(3)
            else:
                eta1c2[t] = eta1c2[t] / (1.0 * len(e2select))
                # eta1c2=np.cross(eta1, eta2) # this is an array of N_e3 vectors

            # dot with eta3 and sum, then divide by n3. store in qval array
        eta1c2d3 = np.diag(np.dot(e3vec, eta1c2.transpose()))
        qval[len(gv.region) - 1 - l] = eta1c2d3.mean()
        qerr[len(gv.region) - 1 - l] = eta1c2d3.std()

    # Divide the std in qerr by sqrt(N_e3)
    qerr /= np.sqrt(e3vec.shape[0])
    # Multiply everything by 10^6
    qval *= 1.0e6
    qerr *= 1.0e6
    return [qval, qerr]