def _makePolynomial(self): nPoints = len(self.x_y_err_arr) if (nPoints < (self.order + 1)): self.order = nPoints - 1 #check if just one of errors is zero infoZeroErr = 1.0 for [x, y, err] in self.x_y_err_arr: infoZeroErr *= err for i in range(nPoints): [x, y, err] = self.x_y_err_arr[i] sigma = 1.0 if (infoZeroErr != 0.): sigma = 1.0 / (err * err) self.x_y_err_arr[i][2] = sigma #now make A matrix aMatr = Matrix(nPoints, self.order + 1) for i in range(nPoints): for j in range(self.order + 1): x = self.x_y_err_arr[i][0] aMatr.set(i, j, math.pow(x, j)) aTCa = Matrix(self.order + 1, self.order + 1) for i in range(self.order + 1): for j in range(self.order + 1): a = 0. for k in range(nPoints): sigma = self.x_y_err_arr[k][2] a += aMatr.get(k, i) * sigma * aMatr.get(k, j) aTCa.set(i, j, a) #now the resuting coefficients and errors aTCaI = aTCa.invert() e = aTCaI.mult(aTCa) if (aTCa == None): print "python PolynomialFit: Problem with data." for i in range(nPoints): x = self.x_y_err_arr[i][0] y = self.x_y_err_arr[i][1] err = self.x_y_err_arr[i][2] print " x,y,err = %12.5g %12.5g %12.5g " % (x, y, err) print "Stop." sys.exit(1) coef_arr = [0.] * (self.order + 1) err_arr = [0.] * (self.order + 1) for i in range(self.order + 1): err_arr[i] = math.sqrt(math.fabs(aTCaI.get(i, i))) for i in range(self.order + 1): coef_arr[i] = 0. for j in range(self.order + 1): for k in range(nPoints): sigma = self.x_y_err_arr[k][2] y = self.x_y_err_arr[k][1] coef_arr[i] += aTCaI.get(i, j) * aMatr.get(k, j) * sigma * y # polinimial coefficients are found self.polynomial.order(self.order) for i in range(len(coef_arr)): self.polynomial.coefficient(i, coef_arr[i]) # now let's calculate errors if (infoZeroErr == 0.): total_sigma = 0. for k in range(nPoints): x = self.x_y_err_arr[k][0] y = self.x_y_err_arr[k][1] total_sigma += (self.polynomial.value(x) - y)**2 total_sigma = math.sqrt(total_sigma / (nPoints - 2)) for i in range(len(err_arr)): err_arr[i] *= total_sigma # set the resulting coefficients and errors array self.coef_err_arr = [coef_arr, err_arr]
mtrxA_init.set(1, 0, 1.4 * .001) mtrxA_init.set(3, 2, 1.3 * .001) mtrxA_init.set(5, 4, 1.2 * .001) printM(mtrxA_init, "Init M ") #----------------------------------------------------------------------------- #------ put particles from b_in bunch to b_out noise_level = 0.000001 for i in range(nParticles): coord_arr = (b_in.x(i), b_in.xp(i), b_in.y(i), b_in.yp(i), b_in.z(i), b_in.dE(i)) coord_arr_res = [0., 0., 0., 0., 0., 0.] for ix in range(6): coord_arr_res[ix] = mtrxA_init.get(ix, 6) for iy in range(6): coord_arr_res[ix] += mtrxA_init.get(ix, iy) * coord_arr[iy] coord_arr_res[ix] += noise_level * random.random() [x, xp, y, yp, z, dE] = coord_arr_res b_out.addParticle(x, xp, y, yp, z, dE) part_id = b_in.partAttrValue("ParticleIdNumber", i, 0) b_out.partAttrValue("ParticleIdNumber", i, 0, part_id) if (b_in_has_ms): m_size = b_in.partAttrValue("macrosize", i, 0) b_out.partAttrValue("macrosize", i, 0, m_size) #---- let's remove 20% of the particles from the out-bunch (they are lost!) n_deleted = int(nParticles * 0.2) for ind in range(n_deleted): ind = int(nParticles * random.random())
matrixGenerator.initBunch(matrixTracker.b) node.trackBunch(matrixTracker.b) matrixGenerator.calculateMatrix(matrixTracker.b,m) #m_prt0 = m.copy() #transormToMAD(m_prt0,momentum,beta) #printM(m_prt0) res_matrix = m.mult(res_matrix) length = length + node.getLength() print "====res for TEAPOT n elements=",n_max," L=",length transormToMAD(res_matrix,momentum,beta) printM(res_matrix) m = transp_matrix det_x = m.get(0,0)*m.get(1,1) - m.get(0,1)*m.get(1,0) print "determinant det(Mx)=",det_x det_y = m.get(2,2)*m.get(3,3) - m.get(2,3)*m.get(3,2) print "determinant det(My)=",det_y cos_phi_x = (m.get(0,0)+m.get(1,1))/2.0 cos_phi_y = (m.get(2,2)+m.get(3,3))/2.0 if(math.fabs(cos_phi_x) >= 1.0 or math.fabs(cos_phi_x) >= 1.0): print "Unstable MAD lattice!" print "Stop." sys.exit(1) nux = math.acos(cos_phi_x)/(2*math.pi) nuy = math.acos(cos_phi_y)/(2*math.pi)
def _makePolynomial(self): nPoints = len(self.x_y_err_arr) if(nPoints < (self.order+1)): self.order = nPoints - 1 #check if just one of errors is zero infoZeroErr = 1.0 for [x,y,err] in self.x_y_err_arr: infoZeroErr *= err for i in range(nPoints): [x,y,err] = self.x_y_err_arr[i] sigma = 1.0 if(infoZeroErr != 0.): sigma = 1.0/(err*err) self.x_y_err_arr[i][2] = sigma #now make A matrix aMatr = Matrix(nPoints,self.order+1) for i in range(nPoints): for j in range(self.order+1): x = self.x_y_err_arr[i][0] aMatr.set(i,j,math.pow(x,j)) aTCa = Matrix(self.order+1,self.order+1) for i in range(self.order+1): for j in range(self.order+1): a = 0. for k in range(nPoints): sigma = self.x_y_err_arr[k][2] a += aMatr.get(k,i)*sigma*aMatr.get(k,j) aTCa.set(i,j,a) #now the resuting coefficients and errors aTCaI = aTCa.invert() e = aTCaI.mult(aTCa) if(aTCa == None): print "python PolynomialFit: Problem with data." for i in range(nPoints): x = self.x_y_err_arr[i][0] y = self.x_y_err_arr[i][1] err = self.x_y_err_arr[i][2] print " x,y,err = %12.5g %12.5g %12.5g "%(x,y,err) print "Stop." sys.exit(1) coef_arr = [0.]*(self.order+1) err_arr = [0.]*(self.order+1) for i in range(self.order+1): err_arr[i] = math.sqrt(math.fabs(aTCaI.get(i,i))) for i in range(self.order+1): coef_arr[i] = 0. for j in range(self.order+1): for k in range(nPoints): sigma = self.x_y_err_arr[k][2] y = self.x_y_err_arr[k][1] coef_arr[i] += aTCaI.get(i,j)*aMatr.get(k,j)*sigma*y # polinimial coefficients are found self.polynomial.order(self.order) for i in range(len(coef_arr)): self.polynomial.coefficient(i,coef_arr[i]) # now let's calculate errors if(infoZeroErr == 0.): total_sigma = 0. for k in range(nPoints): x = self.x_y_err_arr[k][0] y = self.x_y_err_arr[k][1] total_sigma += (self.polynomial.value(x)-y)**2 total_sigma = math.sqrt(total_sigma/(nPoints-2)) for i in range(len(err_arr)): err_arr[i] *= total_sigma # set the resulting coefficients and errors array self.coef_err_arr = [coef_arr,err_arr]
mtrxA_init = Matrix(7,7) mtrxA_init.zero() for ix in range(6): mtrxA_init.set(ix,6,1.0*ix) mtrxA_init.set(6,6,1.0) for ix in range(6): mtrxA_init.set(ix,ix,1.*(ix+1)) printM(mtrxA_init, "Init M ") #----------------------------------------------------------------------------- #------ put particles from b_in bunch to b_out noise_level = 0.000001 for i in range(nParticles): coord_arr = (b_in.x(i),b_in.xp(i),b_in.y(i),b_in.yp(i),b_in.z(i),b_in.dE(i)) coord_arr_res = [0.,0.,0.,0.,0.,0.] for ix in range(6): coord_arr_res[ix] = mtrxA_init.get(ix,6) for iy in range(6): coord_arr_res[ix] += mtrxA_init.get(ix,iy)*coord_arr[iy] coord_arr_res[ix] += noise_level*random.random() [x,xp,y,yp,z,dE] = coord_arr_res b_out.addParticle(x,xp,y,yp,z,dE) part_id = b_in.partAttrValue("ParticleIdNumber", i, 0) b_out.partAttrValue("ParticleIdNumber", i, 0, part_id) b_out.deleteParticleFast(int(nParticles*0.5)) b_out.deleteParticleFast(int(nParticles*0.3)) #-----get the matrix mtrxA = Matrix(7,7) n_part_analysis = bunch_utils_functions.trasportMtrx(b_in,b_out,mtrxA) print "Total N=",n_part_analysis
bunch = Bunch() bunch_in.copyBunchTo(bunch) accLattice.trackBunch(bunch, None, None, bnch02_ind) transportMtrxFromInitCoords(bunch, trMtrx, use_twiss_weight_x, use_twiss_weight_y, use_twiss_weight_z) twiss_analysis.analyzeBunch(bunch) x_rms = math.sqrt( twiss_analysis.getTwiss(0)[1] * twiss_analysis.getTwiss(0)[3]) * 1000. y_rms = math.sqrt( twiss_analysis.getTwiss(1)[1] * twiss_analysis.getTwiss(1)[3]) * 1000. z_rms = math.sqrt( twiss_analysis.getTwiss(2)[1] * twiss_analysis.getTwiss(2)[3]) * 1000. z_to_phase_coeff = bunch_gen.getZtoPhaseCoeff(bunch) z_rms_deg = z_to_phase_coeff * z_rms / 1000.0 #----transformation from [m] to [deg] and from [GeV] to [MeV] for z, phi and dE trMtrx.set(4, 4 + 1, trMtrx.get(4, 4 + 1) * z_to_phase_coeff / 1000.) trMtrx.set(4 + 1, 4, trMtrx.get(4 + 1, 4) * 1000. / z_to_phase_coeff) mz11 = trMtrx.get(4, 4) mz12 = trMtrx.get(4, 4 + 1) #---------------------------------------------------------- det_z = trMtrx.get(0 + 4, 0 + 4) * trMtrx.get(1 + 4, 1 + 4) - trMtrx.get( 1 + 4, 0 + 4) * trMtrx.get(0 + 4, 1 + 4) res_sizes_trMtrx_arr.append([E0TL, z_rms_deg, z_rms_exp, mz11, mz12]) print "E0TL[keV] = %5.1f (z_rms,z_rms_exp) = %5.3f %5.1f det(TrMtrxZ) = %5.4f " % ( E0TL * 1.0e+6, z_rms_deg, z_rms_exp, det_z) #------------------- #-------------------------------------------------------------- #----------- LSQ method for Longitudinal Twiss parameters #-------------------------------------------------------------- n_cases = len(res_sizes_trMtrx_arr)
m = Matrix(6, 6) matrixGenerator.initBunch(matrixTracker.b) node.trackBunch(matrixTracker.b) matrixGenerator.calculateMatrix(matrixTracker.b, m) #m_prt0 = m.copy() #transormToMAD(m_prt0,momentum,beta) #printM(m_prt0) res_matrix = m.mult(res_matrix) length = length + node.getLength() print "====res for TEAPOT n elements=", n_max, " L=", length transormToMAD(res_matrix, momentum, beta) printM(res_matrix) m = transp_matrix det_x = m.get(0, 0) * m.get(1, 1) - m.get(0, 1) * m.get(1, 0) print "determinant det(Mx)=", det_x det_y = m.get(2, 2) * m.get(3, 3) - m.get(2, 3) * m.get(3, 2) print "determinant det(My)=", det_y cos_phi_x = (m.get(0, 0) + m.get(1, 1)) / 2.0 cos_phi_y = (m.get(2, 2) + m.get(3, 3)) / 2.0 if (math.fabs(cos_phi_x) >= 1.0 or math.fabs(cos_phi_x) >= 1.0): print "Unstable MAD lattice!" print "Stop." sys.exit(1) nux = math.acos(cos_phi_x) / (2 * math.pi) nuy = math.acos(cos_phi_y) / (2 * math.pi)
trMtrx.set(2,3,sin_kL/kq) trMtrx.set(3,2,-kq*sin_kL) #------------------------- trMtrx.set(0,0,cosh_kL) trMtrx.set(1,1,cosh_kL) trMtrx.set(0,1,sinh_kL/kq) trMtrx.set(1,0,kq*sinh_kL) #------------------- this is 6x6 matrix for Twiss parameters for x and y axis trTwissMtrx = Matrix(6,6) #---- dir_ind = 0 for x and 1 for y-directions for dir_ind in range(2): #---- index shift ish = 3*dir_ind ind1 = 0 + 2*dir_ind ind2 = 1 + 2*dir_ind trTwissMtrx.set(0+ish,0+ish,trMtrx.get(ind1,ind1)*trMtrx.get(ind2,ind2) + trMtrx.get(ind1,ind2)*trMtrx.get(ind2,ind1)) trTwissMtrx.set(0+ish,1+ish,- trMtrx.get(ind1,ind1)*trMtrx.get(ind2,ind1)) trTwissMtrx.set(0+ish,2+ish,- trMtrx.get(ind1,ind2)*trMtrx.get(ind2,ind2)) trTwissMtrx.set(1+ish,0+ish,- 2*trMtrx.get(ind1,ind1)*trMtrx.get(ind1,ind2)) trTwissMtrx.set(1+ish,1+ish,trMtrx.get(ind1,ind1)*trMtrx.get(ind1,ind1)) trTwissMtrx.set(1+ish,2+ish,trMtrx.get(ind1,ind2)*trMtrx.get(ind1,ind2)) trTwissMtrx.set(2+ish,0+ish,- 2*trMtrx.get(ind2,ind1)*trMtrx.get(ind2,ind2)) trTwissMtrx.set(2+ish,1+ish,trMtrx.get(ind2,ind1)*trMtrx.get(ind2,ind1)) trTwissMtrx.set(2+ish,2+ish,trMtrx.get(ind2,ind2)*trMtrx.get(ind2,ind2)) matrix_arr.append([z_avg,trMtrx,trTwissMtrx]) #print "debug trMtrx ====" #printM(trMtrx) #print "debug trTwissMtrx ====" #printM(trTwissMtrx) #sys.exit()