Esempio n. 1
0
def _measChromaticity(**kwargs):
    """Measure the chromaticity

    Parameters
    -----------
    dfmax - max RF frequency change (within plus/minus), 1e-6
    gamma - beam, 3.0/0.511e-3
    alphac - momentum compaction factor, 3.62619e-4
    wait - 1.5 second
    num_points - 5

    returns dp/p, nu, chrom
    dpp - dp/p energy deviation
    nu - tunes 
    chrom - result chromaticities 
    obt - orbit at each f settings (initial, every df, final).
    """
    dfmax = kwargs.get("dfmax", 1e-6)
    gamma = kwargs.get("gamma", 3.0e3 / 0.511)
    alphac = kwargs.get("alphac", 3.6261976841792413e-04)
    wait = kwargs.get("wait", 1.5)
    npt = kwargs.get("num_points", 5)
    verbose = kwargs.get("verbose", 0)

    eta = alphac - 1 / gamma / gamma

    obt = []
    f0 = getRfFrequency(handle="setpoint")
    nu0 = getTunes()
    obt.append(getOrbit(spos=True))
    _logger.info("Initial RF freq= {0}, tunes= {1}" % (f0, nu0))

    # incase RF does not allow large step change, ramp down first
    if verbose:
        print("Initial RF freq= {0}, stepping down {1} in {2} steps".format(f0, -dfmax, npt))
    for df in np.linspace(0, abs(dfmax), npt)[1:-1]:
        putRfFrequency(f0 - df)
        time.sleep(2.0 / npt)

    f = np.linspace(f0 - dfmax, f0 + dfmax, npt)
    nu = np.zeros((len(f), 2), "d")
    for i, f1 in enumerate(f):
        if verbose > 0:
            print("freq= ", f1, end=" ")
        putRfFrequency(f1)
        time.sleep(wait)
        nu[i, :] = getTunes()
        obt.append(getOrbit(spos=True))
        if verbose > 0:
            print(
                "tunes:",
                nu[i, 0],
                nu[i, 1],
                np.min(obt[-1][:, 0]),
                np.max(obt[-1][:, 0]),
                np.min(obt[-1][:, 1]),
                np.max(obt[-1][:, 1]),
            )

    for df in np.linspace(0, abs(dfmax), npt):
        putRfFrequency(f0 + abs(dfmax) - df)
        time.sleep(2.0 / npt)

    obt.append(getOrbit(spos=True))

    df = f - f0
    dnu = nu - np.array(nu0)
    p, resi, rank, sing, rcond = np.polyfit(df, dnu, deg=2, full=True)
    chrom = p[-2, :] * (-f0 * eta)
    if verbose > 0:
        print("Coef:", p)
        print("Resi:", resi)
        print("Chrom:", chrom)
    return df / (-f0 * eta), nu, chrom, obt
Esempio n. 2
0
def measChromaticity(
    dfmax=1e-6, gamma=3.0e3 / 0.511, alphac=3.626e-4, num_points=5, fMeasTunes=None, deg=2, wait=0.5, verbose=0
):
    """Measure the chromaticity

    Parameters
    -----------
    dfmax - max RF frequency change (within plus/minus), 1e-6
    gamma - beam, 3.0/0.511e-3
    alphac - momentum compaction factor, 3.62619e-4
    wait - 1.5 second
    num_points - 5
    fMeasTunes - function used to get tunes. default None, use getTunes().

    returns
    --------
    chrom : chromaticity, (chx, chy)
    info : dictionary
        freq : list of frequency setting
        freq0 : initial frequency setpoint
        tune : list of tunes,  shape (num_points, 2)
        dpp : dp/p energy deviation
        obt : orbit at each f settings (initial, every df, final).
        deg : degree of polynomial fitting.
        p : polynomial coeff, shape (deg+1, 2)
    """
    obt = []
    f0 = getRfFrequency(handle="setpoint")

    # names, x0, y0, Isum0, timestamp, offset = \
    #    measKickedTbtData(7, (0.15, 0.2), sleep=10, output=False)
    # nu0 = (calcFftTunes(x0), calcFftTunes(y0))
    nu0 = fMeasTunes() if fMeasTunes else getTunes()

    obt.append(getOrbit(spos=True))
    # _logger.info("Initial RF freq= {0}, tunes= {1}" % (f0, nu0))

    # incase RF does not allow large step change, ramp down first
    if verbose:
        print("Initial RF freq= {0}, stepping down {1} in {2} steps".format(f0, -dfmax, num_points))
    for df in np.linspace(0, abs(dfmax), num_points)[1:-1]:
        putRfFrequency(f0 - df)
        time.sleep(2.0 / num_points)

    f = np.linspace(f0 - dfmax, f0 + dfmax, num_points)
    nu = np.zeros((len(f), 2), "d")
    for i, f1 in enumerate(f):
        if verbose > 0:
            print("dfreq= {0: .2e}".format(f1 - f0), end=" ")
        putRfFrequency(f1)
        time.sleep(wait)
        nu[i, :] = fMeasTunes() if fMeasTunes else getTunes()
        obt.append(getOrbit(spos=True))
        if verbose > 0:
            print(
                "tunes: {0:.5f} {1:.5f}".format(nu[i, 0], nu[i, 1]),
                "orbit min-max: {0:.2e} {1:.2e}, {2:.2e} {3:.2e}".format(
                    np.min(obt[-1][:, 0]), np.max(obt[-1][:, 0]), np.min(obt[-1][:, 1]), np.max(obt[-1][:, 1])
                ),
            )

    for df in np.linspace(0, abs(dfmax), num_points):
        putRfFrequency(f0 + abs(dfmax) - df)
        time.sleep(2.0 / num_points)
    obt.append(getOrbit(spos=True))

    # calculate chrom, return info
    info = {"freq": f, "tune": nu, "orbit": obt, "freq0": f0, "deg": deg}
    chrom, dpp, p = calcChromaticity(f0, f, nu, deg=deg)
    info["dpp"] = dpp
    info["p"] = p

    return chrom, info
Esempio n. 3
0
def measDispersion(
    elem, dfmax=5e-7, alphac=3.6261976841792413e-04, gamma=5.870841487279844e3, num_points=5, full=False, verbose=0
):
    """measure dispersion at BPMs

    Parameters
    -----------
    elem : BPM name, list or pattern
    dfmax : float. frequency change (check the unit)
    alphac : float. momentum compaction factor.
    gamma : float. beam energy.
    num_points : int. points to fit line
    full : reserved.

    Returns
    --------
    eta : numpy.array. (nelem, 3) with columns etax, etay and s

    Examples
    ---------
    >>> eta = measDispersion('p*c0[1-4]*')

    """

    eta = alphac - 1.0 / gamma / gamma

    bpmobj = [b for b in getElements(elem) if b.family == "BPM"]
    bpmnames = [b.name for b in bpmobj]
    nbpm = len(bpmnames)

    _logger.info("measure dispersions at %d elements '%s'" % (len(bpmnames), str(elem)))

    f0 = getRfFrequency(handle="setpoint")
    dflst = np.linspace(-abs(dfmax), abs(dfmax), num_points)

    # incase RF does not allow large step change, ramp down first
    for df in np.linspace(0, abs(dfmax), num_points)[1:-1]:
        putRfFrequency(f0 - df)
        time.sleep(2.0 / num_points)

    # avoid a bug in virtac
    obt0 = getOrbit(bpmnames)

    cod = np.zeros((len(dflst), 2 * nbpm), "d")
    for i, df in enumerate(dflst):
        v0 = getOrbit()
        putRfFrequency(f0 + df)
        if verbose > 0:
            print(i, "df=", df, " f=", f0)
        waitStableOrbit(v0)

        # repeat the put/get in case simulator did not response latest results
        obt = getOrbit(bpmnames)
        # print i, obt[0,:2], obt0[0,:2], np.shape(obt), np.shape(obt0)

        cod[i, :nbpm] = obt[:, 0] - obt0[:, 0]
        cod[i, nbpm:] = obt[:, 1] - obt0[:, 1]

    # restore
    for df in np.linspace(0, abs(dfmax), num_points):
        putRfFrequency(f0 + abs(dfmax) - df)
        time.sleep(2.0 / num_points)

    # fitting
    p = np.polyfit(dflst, cod, deg=1)
    disp = -p[0, :] * f0 * eta
    s = np.array([e.sb for e in bpmobj], "d")
    ret = np.zeros((len(bpmobj), 3), "d")
    ret[:, 0] = disp[:nbpm]
    ret[:, 1] = disp[nbpm:]
    ret[:, 2] = s
    if verbose > 0:
        for i, bpm in enumerate(bpmobj):
            print(i, bpm.name, bpm.sb, ret[i, 0], ret[i, 1])
    return ret
Esempio n. 4
0
def _measChromaticity(**kwargs):
    """Measure the chromaticity

    Parameters
    -----------
    dfmax - max RF frequency change (within plus/minus), 1e-6
    gamma - beam, 3.0/0.511e-3
    alphac - momentum compaction factor, 3.62619e-4
    wait - 1.5 second
    num_points - 5

    returns dp/p, nu, chrom
    dpp - dp/p energy deviation
    nu - tunes 
    chrom - result chromaticities 
    obt - orbit at each f settings (initial, every df, final).
    """
    dfmax = kwargs.get("dfmax", 1e-6)
    gamma = kwargs.get("gamma", 3.0e3 / .511)
    alphac = kwargs.get("alphac", 3.6261976841792413e-04)
    wait = kwargs.get("wait", 1.5)
    npt = kwargs.get("num_points", 5)
    verbose = kwargs.get("verbose", 0)

    eta = alphac - 1 / gamma / gamma

    obt = []
    f0 = getRfFrequency(handle="setpoint")
    nu0 = getTunes()
    obt.append(getOrbit(spos=True))
    _logger.info("Initial RF freq= {0}, tunes= {1}" % (f0, nu0))

    # incase RF does not allow large step change, ramp down first
    if verbose:
        print("Initial RF freq= {0}, stepping down {1} in {2} steps".format(
            f0, -dfmax, npt))
    for df in np.linspace(0, abs(dfmax), npt)[1:-1]:
        putRfFrequency(f0 - df)
        time.sleep(2.0 / npt)

    f = np.linspace(f0 - dfmax, f0 + dfmax, npt)
    nu = np.zeros((len(f), 2), 'd')
    for i, f1 in enumerate(f):
        if verbose > 0:
            print("freq= ", f1, end=" ")
        putRfFrequency(f1)
        time.sleep(wait)
        nu[i, :] = getTunes()
        obt.append(getOrbit(spos=True))
        if verbose > 0:
            print("tunes:", nu[i, 0], nu[i, 1], np.min(obt[-1][:, 0]),
                  np.max(obt[-1][:, 0]), np.min(obt[-1][:, 1]),
                  np.max(obt[-1][:, 1]))

    for df in np.linspace(0, abs(dfmax), npt):
        putRfFrequency(f0 + abs(dfmax) - df)
        time.sleep(2.0 / npt)

    obt.append(getOrbit(spos=True))

    df = f - f0
    dnu = nu - np.array(nu0)
    p, resi, rank, sing, rcond = np.polyfit(df, dnu, deg=2, full=True)
    chrom = p[-2, :] * (-f0 * eta)
    if verbose > 0:
        print("Coef:", p)
        print("Resi:", resi)
        print("Chrom:", chrom)
    return df / (-f0 * eta), nu, chrom, obt
Esempio n. 5
0
def measChromaticity(dfmax=1e-6,
                     gamma=3.0e3 / 0.511,
                     alphac=3.626e-4,
                     num_points=5,
                     fMeasTunes=None,
                     deg=2,
                     wait=0.5,
                     verbose=0):
    """Measure the chromaticity

    Parameters
    -----------
    dfmax - max RF frequency change (within plus/minus), 1e-6
    gamma - beam, 3.0/0.511e-3
    alphac - momentum compaction factor, 3.62619e-4
    wait - 1.5 second
    num_points - 5
    fMeasTunes - function used to get tunes. default None, use getTunes().

    returns
    --------
    chrom : chromaticity, (chx, chy)
    info : dictionary
        freq : list of frequency setting
        freq0 : initial frequency setpoint
        tune : list of tunes,  shape (num_points, 2)
        dpp : dp/p energy deviation
        obt : orbit at each f settings (initial, every df, final).
        deg : degree of polynomial fitting.
        p : polynomial coeff, shape (deg+1, 2)
    """
    obt = []
    f0 = getRfFrequency(handle="setpoint")

    #names, x0, y0, Isum0, timestamp, offset = \
    #    measKickedTbtData(7, (0.15, 0.2), sleep=10, output=False)
    #nu0 = (calcFftTunes(x0), calcFftTunes(y0))
    nu0 = fMeasTunes() if fMeasTunes else getTunes()

    obt.append(getOrbit(spos=True))
    #_logger.info("Initial RF freq= {0}, tunes= {1}" % (f0, nu0))

    # incase RF does not allow large step change, ramp down first
    if verbose:
        print("Initial RF freq= {0}, stepping down {1} in {2} steps".format(
            f0, -dfmax, num_points))
    for df in np.linspace(0, abs(dfmax), num_points)[1:-1]:
        putRfFrequency(f0 - df)
        time.sleep(2.0 / num_points)

    f = np.linspace(f0 - dfmax, f0 + dfmax, num_points)
    nu = np.zeros((len(f), 2), 'd')
    for i, f1 in enumerate(f):
        if verbose > 0:
            print("dfreq= {0: .2e}".format(f1 - f0), end=" ")
        putRfFrequency(f1)
        time.sleep(wait)
        nu[i, :] = fMeasTunes() if fMeasTunes else getTunes()
        obt.append(getOrbit(spos=True))
        if verbose > 0:
            print(
                "tunes: {0:.5f} {1:.5f}".format(nu[i, 0], nu[i, 1]),
                "orbit min-max: {0:.2e} {1:.2e}, {2:.2e} {3:.2e}".format(
                    np.min(obt[-1][:, 0]), np.max(obt[-1][:, 0]),
                    np.min(obt[-1][:, 1]), np.max(obt[-1][:, 1])))

    for df in np.linspace(0, abs(dfmax), num_points):
        putRfFrequency(f0 + abs(dfmax) - df)
        time.sleep(2.0 / num_points)
    obt.append(getOrbit(spos=True))

    # calculate chrom, return info
    info = {"freq": f, "tune": nu, "orbit": obt, "freq0": f0, "deg": deg}
    chrom, dpp, p = calcChromaticity(f0, f, nu, deg=deg)
    info["dpp"] = dpp
    info["p"] = p

    return chrom, info
Esempio n. 6
0
def measDispersion(elem,
                   dfmax=5e-7,
                   alphac=3.6261976841792413e-04,
                   gamma=5.870841487279844e3,
                   num_points=5,
                   full=False,
                   verbose=0):
    """measure dispersion at BPMs

    Parameters
    -----------
    elem : BPM name, list or pattern
    dfmax : float. frequency change (check the unit)
    alphac : float. momentum compaction factor.
    gamma : float. beam energy.
    num_points : int. points to fit line
    full : reserved.

    Returns
    --------
    eta : numpy.array. (nelem, 3) with columns etax, etay and s

    Examples
    ---------
    >>> eta = measDispersion('p*c0[1-4]*')

    """

    eta = alphac - 1.0 / gamma / gamma

    bpmobj = [b for b in getElements(elem) if b.family == 'BPM']
    bpmnames = [b.name for b in bpmobj]
    nbpm = len(bpmnames)

    _logger.info("measure dispersions at %d elements '%s'" %
                 (len(bpmnames), str(elem)))

    f0 = getRfFrequency(handle="setpoint")
    dflst = np.linspace(-abs(dfmax), abs(dfmax), num_points)

    # incase RF does not allow large step change, ramp down first
    for df in np.linspace(0, abs(dfmax), num_points)[1:-1]:
        putRfFrequency(f0 - df)
        time.sleep(2.0 / num_points)

    # avoid a bug in virtac
    obt0 = getOrbit(bpmnames)

    cod = np.zeros((len(dflst), 2 * nbpm), 'd')
    for i, df in enumerate(dflst):
        v0 = getOrbit()
        putRfFrequency(f0 + df)
        if verbose > 0:
            print(i, "df=", df, " f=", f0)
        waitStableOrbit(v0)

        # repeat the put/get in case simulator did not response latest results
        obt = getOrbit(bpmnames)
        #print i, obt[0,:2], obt0[0,:2], np.shape(obt), np.shape(obt0)

        cod[i, :nbpm] = obt[:, 0] - obt0[:, 0]
        cod[i, nbpm:] = obt[:, 1] - obt0[:, 1]

    # restore
    for df in np.linspace(0, abs(dfmax), num_points):
        putRfFrequency(f0 + abs(dfmax) - df)
        time.sleep(2.0 / num_points)

    # fitting
    p = np.polyfit(dflst, cod, deg=1)
    disp = -p[0, :] * f0 * eta
    s = np.array([e.sb for e in bpmobj], 'd')
    ret = np.zeros((len(bpmobj), 3), 'd')
    ret[:, 0] = disp[:nbpm]
    ret[:, 1] = disp[nbpm:]
    ret[:, 2] = s
    if verbose > 0:
        for i, bpm in enumerate(bpmobj):
            print(i, bpm.name, bpm.sb, ret[i, 0], ret[i, 1])
    return ret