def getNormilizedSpline(self):
		"""
		Returns the spline  normilized by the integral of the absolute value.
		"""
		n = self.splineFiled.getSize()
		f = Function()
		for i in range(n):
			f.add(self.splineFiled.x(i),math.fabs(self.splineFiled.y(i)))
		integral = GaussLegendreIntegrator(500)
		integral.setLimits(self.splineFiled.x(0),self.splineFiled.x(n-1))
		spline = SplineCH()					
		spline.compile(f)
		res = integral.integral(spline)	
		f = Function()
		for i in range(n):
			f.add(self.splineFiled.x(i),self.splineFiled.y(i)/res)		
		spline = SplineCH()					
		spline.compile(f)
		return spline		
示例#2
0
def FF(x):
	return math.sin(x)

def FFP(x):
	return math.cos(x)

n = 40
step = 2*math.pi/n
for i in range(n):
	x = step*i+0.1*((1.0*i)/n)**2;
	y = FF(x)
	f.add(x,y)

f.dump()

spline = SplineCH()
spline.compile(f)
	
spline.dump()

print "================"
n = 100
step = 0.8*(f.getMaxX() - f.getMinX())/(n-1)
y_dev_max = 0.
yp_dev_max = 0.
for j in range(n):
	x = f.getMinX() + j*step
	y = FF(x)
	yp = FFP(x)
	ys = math.fabs(spline.getY(x) - y)
	yps = math.fabs(spline.getYP(x) - yp)
integrator = GaussLegendreIntegrator(4)
print "Number of integral points =", integrator.getnPoints()
print "The integral of sin(x) from 0. to pi/2 is 1."
#------------------------------------------
# integral of sin(x) from 0. to pi/2 is 1.
#------------------------------------------

x_min = 0.
x_max = math.pi / 2
integrator.setLimits(x_min, x_max)

f = Function()
n = 10
for i in range(n):
    x = x_min + i * (x_max - x_min) / (n - 1)
    f.add(x, math.sin(x))

res = integrator.integral(f)
print "For Function integral =", res, " error=", math.fabs(res - 1.0)

spline = SplineCH()
spline.compile(f)
res = integrator.integral(spline)
print "For SplineCH integral =", res, " error=", math.fabs(res - 1.0)
print "=============== Integration points and weights ================"
point_weight_arr = integrator.getPointsAndWeights()
for (x, w) in point_weight_arr:
    print "x = %8.5f   w = %12.5g " % (x, w)
print "Done."
示例#4
0
	def __init__(self,splineFiled, zeroIsCenter = False):
		self.splineFiled = splineFiled
		#----------------------------------------------------
		self.eps_root = 1.0e-6
		self.rf_freq = 0.0
		#self.e0_normalized_arr - normilized amplitudes of the gaps
		self.e0_normalized_arr = []
		self.e0l_normalized_arr = []
		# self.beta_arr - relativistic beta, cappa = 2*math.pi*rf_freq/(c_light*beta)
		self.beta_arr = []
		self.cappa_arr = []	
		# self.ttp_ssp_gap_arr array with [T,Tp,S,Sp] for all gaps (T,Tp,S,Sp - Functions of cappa)
		self.ttp_ssp_gap_arr = []
		# self.gap_polynoms_coef_arr array with polynomial coefficients for [T,Tp,S,Sp] for each gap
		self.gap_polynoms_coef_arr = []
		# self.gap_polynoms_arr array with Polynomial instances for [T,Tp,S,Sp] for each gap
		self.gap_polynoms_arr = []		
		# self.gap_polynoms_t_tp_s_sp_err_arr - maximal relative errors for polynomial fitting
		self.gap_polynoms_t_tp_s_sp_err_arr = []
		#-----------------------------------------------------
		#calculate the roots
		self.roots_arr = self.rootAnalysis()
		#find the roots of derivative - yp = y' - RFgap center positions
		if(zeroIsCenter):
			self.yp_roots_arr = [0.]
		else:
			self.yp_roots_arr = self.gapCentersAnalysis()
		#print "debug yp roots=",self.yp_roots_arr
		if(len(self.roots_arr) - 1 != len(self.yp_roots_arr)):
			rank = orbit_mpi.MPI_Comm_rank(mpi_comm.MPI_COMM_WORLD)
			if(rank == 0):
				print "Class RF_AxisFieldAnalysis."
				print "The structure of the input rf field spline is wrong!"
				print "roots of the filed =",self.roots_arr 
				print "extrema positions =",self.yp_roots_arr
			sys.exit(1)
		# caluclate the position of the center of the cavity 
		rf_center = 0
		for i in range(1,len(self.yp_roots_arr)-1):
			rf_center += self.yp_roots_arr[i]
		rf_center /= (len(self.yp_roots_arr)-2)
		self.rf_center = rf_center
		# make spline for each RF gap
		self.gap_slpline_arr = []
		#print "debug roots_arr=",self.roots_arr
		#---make splineGap with x in the [m] instead of [cm]
		for i in range(len(self.roots_arr)-1):			
			x_center = self.yp_roots_arr[i]
			x0 = self.roots_arr[i]
			x1 = self.roots_arr[i+1]
			f = Function()
			f.add((x0-x_center),math.fabs(splineFiled.getY(x0)))
			for ix in range(splineFiled.getSize()-1):
				x = splineFiled.x(ix)
				if(x > x0 and x < x1):
					f.add((x-x_center),math.fabs(splineFiled.y(ix)))
			f.add((x1-x_center),math.fabs(splineFiled.getY(x1)))
			splineGap = SplineCH()					
			splineGap.compile(f)
			n = splineGap.getSize()
			x_min = splineGap.x(0)
			x_max = splineGap.x(n-1)
			gap_length = x_max - x_min
			self.gap_slpline_arr.append([gap_length,(x_center - self.rf_center),splineGap])
示例#5
0
	def makeTransitTimeTables(self,beta_min,beta_max,n_table_points,rf_freq):
		"""
		It will calculate transit time factor tables for all RF gaps
		TTFs (T,S,Tp,Sp) are funcftions of the cappa variable = 2*pi*f/(c*beta)
		"""
		self.rf_freq = rf_freq
		c_light = 2.99792458e+8
		self.beta_arr = []
		self.cappa_arr = []
		for i_beta in range(n_table_points):
			beta = beta_min + i_beta*(beta_max-beta_min)/(n_table_points-1)
			cappa = 2*math.pi*rf_freq/(c_light*beta)
			self.beta_arr.append(beta)
			self.cappa_arr.append(cappa)
		self.beta_arr.reverse()
		self.cappa_arr.reverse()
		#--calculate realtive gap amplitudes
		integral = GaussLegendreIntegrator(500)
		e0l_arr = []
		e0l_sum = 0.
		for i in range(len(self.gap_slpline_arr)):
			[gap_length,x_center,splineGap] = self.gap_slpline_arr[i]
			n = splineGap.getSize()
			x_min = splineGap.x(0)
			x_max = splineGap.x(n-1)
			integral.setLimits(x_min,x_max)
			e0l = integral.integral(splineGap)
			e0l_sum += e0l
			e0l_arr.append(e0l)
		self.e0_normalized_arr = []
		self.e0l_normalized_arr = []
		e0_norm = e0l_arr[0]/self.gap_slpline_arr[0][0]
		e0l_norm = e0l_arr[0]
		for i in range(len(e0l_arr)):
			self.e0_normalized_arr.append((e0l_arr[i]/self.gap_slpline_arr[i][0])/e0_norm)
			self.e0l_normalized_arr.append((e0l_arr[i]/e0l_norm))
		#--- calculate transit time factors
		self.ttp_ssp_gap_arr = []
		for i in range(len(self.gap_slpline_arr)):
			func_T  = Function()
			func_TP = Function()
			func_S  = Function()
			func_SP = Function()
			self.ttp_ssp_gap_arr.append([func_T,func_TP,func_S,func_SP])
		for i_gap in range(len(self.gap_slpline_arr)):
			[func_T,func_TP,func_S,func_SP] = self.ttp_ssp_gap_arr[i_gap]
			[gap_length,x0,spline] = self.gap_slpline_arr[i_gap]
			x_min = spline.x(0)
			x_max = spline.x(spline.getSize()-1)
			integral.setLimits(x_min,x_max)		
			for i_beta in range(n_table_points):
				beta = self.beta_arr[i_beta]
				cappa = self.cappa_arr[i_beta]
				f_cos = Function()
				f_sin = Function()	
				for isp in range(spline.getSize()):
					x = spline.x(isp)
					y = spline.y(isp)
					phase = cappa*x
					s = math.sin(phase)
					c = math.cos(phase)
					f_cos.add(x,c*y)
					f_sin.add(x,s*y)
				f_sp_cos = SplineCH()	
				f_sp_sin = SplineCH()	
				f_sp_cos.compile(f_cos)	
				f_sp_sin.compile(f_sin)	
				T = integral.integral(f_sp_cos)	
				S = integral.integral(f_sp_sin)
				func_T.add(cappa,T/e0l_arr[i_gap])
				func_S.add(cappa,S/e0l_arr[i_gap])	
			spline_T = SplineCH()
			spline_S = SplineCH()
			spline_T.compile(func_T)
			spline_S.compile(func_S)
			for i_beta in range(spline_T.getSize()):
				cappa = spline_T.x(i_beta)
				TP = spline_T.getYP(cappa)
				SP = spline_S.getYP(cappa)
				func_TP.add(cappa,TP)
				func_SP.add(cappa,SP)
		return self.ttp_ssp_gap_arr
示例#6
0
    data_in_arr.append(
        ["SDTL", sdtl_cav, sdtl_cav_rf_gap_names, rf_freq, 0.27, 0.65])

n_poly_order = 4
n_table_points = 100
dir_name = "ttf_results_data"

for [
        cav_directory_name, cav_name, rf_gap_names_arr, rf_freq, beta_min,
        beta_max
] in data_in_arr:
    file_cav_name = cav_name + ".dat"
    for rf_gap_name in rf_gap_names_arr:
        file_gap_name = data_root_dir + cav_directory_name + "/" + rf_gap_name + ".SFO"
        func = getAxisFieldFunction(file_gap_name)
        splineGap = SplineCH()
        splineGap.compile(func)
        rf_gap_analysis = RF_AxisFieldAnalysis(splineGap, zeroIsCenter=True)
        rf_gap_analysis.makeTransitTimeTables(beta_min, beta_max,
                                              n_table_points, rf_freq)
        gap_polynoms_coef_arr = rf_gap_analysis.makePlynomialFittings(
            n_poly_order)
        gap_polynoms_t_tp_s_sp_err_arr = rf_gap_analysis.gap_polynoms_t_tp_s_sp_err_arr
        (err_t, err_tp, err_s, err_sp) = gap_polynoms_t_tp_s_sp_err_arr[0]
        print "gap=", rf_gap_name, " spline size = ", splineGap.getSize(
        ), " err = %5.6f %5.6f %5.6f %5.6f " % (err_t, err_tp, err_s, err_sp)
        rf_gap_analysis.dumpTransitTimeTables("./" + dir_name + "/" +
                                              rf_gap_name + "_t_tp_s_sp.dat")
        rf_gap_analysis.dumpTTFandFitting("./" + dir_name + "/" + rf_gap_name +
                                          "_ttf_fiitings.dat")
        #--------------------------------------------
import sys
import math

from orbit_utils import Function
from orbit_utils import SplineCH

from orbit.utils.fitting import PolynomialFit

f = Function()
for i in range(15):
	x = 0.1*i
	y = -1+1.0*x**2+2.0*x**3 + 0.01*x**4  
	f.add(x,y)
	
print "polynomial for fitting = y = -1+1.0*x**2+2.0*x**3 + 0.01*x**4"
print "================Function===================================="
poly = PolynomialFit(4)
poly.fitFunction(f)
[coef_arr,err_arr] = poly.getCoefficientsAndErr()
for i in range(len(coef_arr)):
	print "i=",i," coef = %8.5f +- %8.5f"%(coef_arr[i],err_arr[i])
	
print "================SplineCH===================================="
spline = SplineCH()
spline.compile(f)
poly.fitSpline(f)	
[coef_arr,err_arr] = poly.getCoefficientsAndErr()
for i in range(len(coef_arr)):
	print "i=",i," coef = %8.5f +- %8.5f"%(coef_arr[i],err_arr[i])