Beispiel #1
0
 def setCavitiesPositions(self, cavities_da, nodes_da):
     #cav_rfgaps_dict[cav_name] = [node_gap_da0,...]
     cav_rfgaps_dict = {}
     for node_da in nodes_da:
         if (node_da.stringValue("type") == "RFGAP"):
             params_da = node_da.childAdaptors("parameters")[0]
             cav_name = params_da.stringValue("cavity")
             if (cav_rfgaps_dict.has_key(cav_name)):
                 cav_rfgaps_dict[cav_name].append(node_da)
             else:
                 cav_rfgaps_dict[cav_name] = [
                     node_da,
                 ]
     #---- calculate cavity position as a avg position of the gaps
     cav_avg_pos_dict = {}
     for cav_name in cav_rfgaps_dict.keys():
         avg_pos = 0.
         for rfgap_da in cav_rfgaps_dict[cav_name]:
             avg_pos += rfgap_da.doubleValue("pos")
         avg_pos /= len(cav_rfgaps_dict[cav_name])
         cav_avg_pos_dict[cav_name] = avg_pos
     for cavity_da in cavities_da:
         cav_name = cavity_da.stringValue("name")
         pos = cav_avg_pos_dict[cav_name]
         cavity_da.setValue("pos", pos)
         #------- set modes for cavities with mode != 0
         rfgap_da = cav_rfgaps_dict[cav_name][0]
         params_da = rfgap_da.childAdaptors("parameters")[0]
         mode0 = params_da.intValue("mode")
         if (mode0 > 0):
             mode = 0
             for rfgap_da in cav_rfgaps_dict[cav_name]:
                 params_da = rfgap_da.childAdaptors("parameters")[0]
                 params_da.setValue("mode", mode)
                 if (mode == 0):
                     mode = 1
                 else:
                     mode = 0
     #---- redefine cavities' phases to the PyORBIT definitions
     for cavity_da in cavities_da:
         cav_name = cavity_da.stringValue("name")
         for rfgap_da in cav_rfgaps_dict[cav_name]:
             params_da = rfgap_da.childAdaptors("parameters")[0]
             phase = params_da.doubleValue("phase")
             phase = phaseNearTargetPhaseDeg(180. + phase, 0.)
             params_da.setValue("phase", phase)
Beispiel #2
0
    def analyzeBunch(self, bunch):
        """
		Here we assume that the macrosize is the same for each 
		particle
		"""
        comm = bunch.getMPIComm()
        self._cleanPhaseHist()
        self.x_avg = 0.
        self.y_avg = 0.
        self.phase_avg = 0.  # in deg
        self.phase_max = 0.  # in deg
        self.synch_pahse = 0.  # in d
        self.fourier_amp = 0.
        self.fourier_phase = 0.  # in deg
        self.amp = 0.
        nPartsGlobal = bunch.getSizeGlobal()
        if (nPartsGlobal == 0): return
        x_avg = 0.
        y_avg = 0.
        phase_avg = 0.
        beta = bunch.getSyncParticle().beta()
        z_to_phase = -360. * self.frequency / (speed_of_light * beta)
        synch_phase = 360. * bunch.getSyncParticle().time() * self.frequency
        self.synch_pahse = phaseNearTargetPhaseDeg(synch_phase, 0.)
        nParts = bunch.getSize()
        for ind in range(nParts):
            x_avg += bunch.x(ind)
            y_avg += bunch.y(ind)
            phase_avg += z_to_phase * bunch.z(ind)
        #---- for parallel case
        (x_avg, y_avg, phase_avg) = orbit_mpi.MPI_Allreduce(
            (x_avg, y_avg, phase_avg), mpi_datatype.MPI_DOUBLE, mpi_op.MPI_SUM,
            comm)
        x_avg /= nPartsGlobal
        y_avg /= nPartsGlobal
        phase_avg /= nPartsGlobal
        phase2_avg = 0.
        for ind in range(nParts):
            phase2_avg += (z_to_phase * bunch.z(ind) - phase_avg)**2
        phase2_avg /= nPartsGlobal
        self.rms_phase = math.sqrt(phase2_avg)
        self.x_avg = x_avg
        self.y_avg = y_avg
        phase_avg += synch_phase
        self.phase_avg = phaseNearTargetPhaseDeg(phase_avg, 0.)
        #------- fill out histogram
        nHistP = len(self.phase_hist_arr)
        for ind in range(nParts):
            phase_ind = int(
                (180. + phaseNearTargetPhaseDeg(z_to_phase * bunch.z(ind), 0.))
                / self.phase_step)
            phase_ind = phase_ind % nHistP
            if (phase_ind < 0): phase_ind = 0
            if (phase_ind >= nHistP): phase_ind = nHistP - 1
            self.phase_hist_arr[phase_ind] += 1.0
        phase_hist_arr = orbit_mpi.MPI_Allreduce(self.phase_hist_arr,
                                                 mpi_datatype.MPI_DOUBLE,
                                                 mpi_op.MPI_SUM, comm)
        phase_hist_arr = list(phase_hist_arr)
        #---- find the position of the max value
        total_sum = 0.
        ind_max = -1
        max_val = 0
        for ind in range(nHistP):
            val = phase_hist_arr[ind]
            if (val > max_val):
                ind_max = ind
                max_val = val
            self.phase_hist_arr[ind] = val
            total_sum += val
        self.phase_max = (ind_max + 0.5) * self.phase_step
        self.phase_max -= 180.
        self.phase_max += synch_phase
        self.phase_max = phaseNearTargetPhaseDeg(self.phase_max, 0.)
        #---- calculate Fourier amplitude
        sin_sum = 0.
        cos_sum = 0.
        grad_to_rad_coeff = math.pi / 180.
        phase_step = self.phase_step * grad_to_rad_coeff
        #---- normalization
        n_local_coeff = 1. / (total_sum * phase_step)
        for ind in range(nHistP):
            phase_hist_arr[ind] *= n_local_coeff
        for ind in range(nHistP):
            self.phase_hist_arr[ind] = phase_hist_arr[ind]
        #--- Fourier amplitude and phase
        for ind in range(nHistP):
            val = phase_hist_arr[ind]
            phase = (ind + 0.5) * self.phase_step * grad_to_rad_coeff
            sin_sum += val * math.sin(phase)
            cos_sum += val * math.cos(phase)
        sin_sum *= self.phase_step * grad_to_rad_coeff
        cos_sum *= self.phase_step * grad_to_rad_coeff
        self.fourier_amp = math.sqrt(sin_sum**2 + cos_sum**2) / math.pi
        self.amp = self.norm_coeff * self.fourier_amp
        self.fourier_phase = -math.atan2(cos_sum,
                                         sin_sum) * 180. / math.pi - 90.
        self.fourier_phase += synch_phase
        self.fourier_phase = phaseNearTargetPhaseDeg(self.fourier_phase, 0.)
Beispiel #3
0
phase_avg_old = 0.
phase_peak_old = 0.
phase_fourier_old = 0.
phase_synch_old = 0.
phase_rms_old = 0.

phase_step = 5.0
phase = 0.

print "========================================================================"
print " phase amp   phase_rms   phase_synch   phase_avg   phase_peak   phase_fourier "
for phase_ind in range(int(360/phase_step)):
	phase += phase_step
	bunch_gen.shiftPhase(bunch,phase_step)
	bpm_node.analyzeBunch(bunch)
	phase_avg = phaseNearTargetPhaseDeg(bpm_node.getAvgPhase(),phase_avg_old)
	phase_peak = phaseNearTargetPhaseDeg(bpm_node.getPeakPhase(),phase_peak_old)
	phase_fourier = phaseNearTargetPhaseDeg(bpm_node.getFourierPhase(),phase_fourier_old)
	phase_synch = phaseNearTargetPhaseDeg(bpm_node.getSynchPhase(),phase_synch_old)
	phase_rms = phaseNearTargetPhaseDeg(bpm_node.getPhaseRMS(),phase_rms_old)
	amp = bpm_node.getAmplitude()
	phase_avg_old = phase_avg
	phase_peak_old = phase_peak
	phase_fourier_old = phase_fourier
	phase_synch_old = phase_synch
	phase_rms_old = phase_rms
	st = " %6.2f "%phase
	st += "  %6.5f  "%amp
	st += "   %6.1f    %6.1f  %6.1f  %6.1f  %6.1f  "%(phase_rms, phase_synch,phase_avg,phase_peak,phase_fourier)
	print st
	
Beispiel #4
0
acs_gap_ind = 0

eKin = 3.0
fl_out = open(
    "../jparc_linac_model/optics/jparc_matched2_40mA100_trace3d_rfgaps.dat",
    "w")
fl_out.write(
    " gap_name    E0TL[MeV]    phase[deg]     pos[m]    EkinIn[MeV]    EkinOut[MeV] \n"
)

for rec in rec_arr:
    if (rec.typeId == 10):
        rfgap_name = rec.name
        if (rfgap_name == "NEW"):
            rfgap_name = new_gap_names_arr[acs_gap_ind]
            acs_gap_ind += 1
        E0TL = rec.params_arr[0]
        phase = phaseNearTargetPhaseDeg(180. + rec.params_arr[1], 0.)
        eKin_in = eKin
        eKin -= E0TL * math.cos(math.pi * phase / 180.)
        eKin_out = eKin
        pos = rec.pos_start / 1000.
        fl_out.write(rfgap_name + "  %11.9f" % E0TL + "  %+6.2f" % phase +
                     " %10.6f" % pos + "     %8.6f" % eKin_in +
                     "  %8.6f" % eKin_out + "\n")
fl_out.close()

print "total length =", rec_arr[len(rec_arr) - 1].pos_end / 1000

print "Stop."
#bunch_in = bunch_gen.getBunch(nParticles = 100000, distributorClass = KVDist3D)

print "Bunch Generation completed."

for cav_name in cav_names:
    cav_synch_phase = cav_synch_phases[cav_name]
    setSynchPhase(bunch_in, accLattice, cav_name, cav_synch_phase)

#set up design
accLattice.trackDesignBunch(bunch_in)

#----- Print out the average phases for the RF gaps in SCL RF Cavities
#----- the charge of H- is negative, so the phases are shifted by -180.
cavs = accLattice.getRF_Cavities()
for cav in cavs:
    avg_phase = phaseNearTargetPhaseDeg(cav.getAvgGapPhaseDeg() - 180., 0.)
    print "debug cav=", cav.getName(
    ), " gaps phaseAvg=", avg_phase, " cav_amp=", cav.getAmp()

print "Design tracking completed."

#track through the lattice
paramsDict = {"old_pos": -1., "count": 0, "pos_step": 0.1}
actionContainer = AccActionsContainer("Test Design Bunch Tracking")

pos_start = 0.

twiss_analysis = BunchTwissAnalysis()

file_out = open("pyorbit_sts_twiss_sizes_ekin.dat", "w")