Пример #1
0
	def analyzeData(self,gd):
		""" 
		Analysis of the BasicGraphData() instance with data.
		Returns the error value.
		"""
		if(gd.getNumbOfPoints() == 0): return 0.
		y_max = gd.getMaxY()
		y_min = gd.getMinY()
		amp0 = (y_max - y_min)/2.0
		amp_step = 0.05*amp0 
		phase_step = 3.0
		self.param_arr[0] = (y_max + y_min)/2.0
		for i_h in range(self.n_harm+1):
			param_local_arr = self.param_arr[0:2*i_h+1]
			#print "debug i_h=",i_h," =====start ==== param_arr=",param_local_arr 
			variables = ArrayList()	
			delta_hint = InitialDelta()
			var = Variable("base",param_local_arr[0], - Double.MAX_VALUE, Double.MAX_VALUE)
			variables.add(var)
			delta_hint.addInitialDelta(var,amp_step)
			for i_fit in range(i_h):
				var = Variable("amp"+str(i_fit),param_local_arr[2*i_fit+1], - Double.MAX_VALUE, Double.MAX_VALUE)
				variables.add(var)
				delta_hint.addInitialDelta(var,amp_step)
				var = Variable("phase"+str(i_fit),param_local_arr[2*i_fit+2], - Double.MAX_VALUE, Double.MAX_VALUE)
				variables.add(var)
				delta_hint.addInitialDelta(var,phase_step)
			scorer = HarmScorer(gd,variables)
			self.harm_func = scorer.getHarmFunc()
			if(i_h == 0):
				scorer.copyFromExternalParams(self.param_arr)
				(amp,shift) = scorer.getNextHarm_Amp_Shift()
				#print "debug zero harm (amp,shift)=",(amp,shift)
				self.param_arr[2*i_h+1] = amp
				self.param_arr[2*i_h+2] = shift
				#print "debug i_h=",i_h," =====final ==== param_arr=", self.param_arr		
				continue
			maxSolutionStopper = SolveStopperFactory.maxEvaluationsStopper((i_h+1)*self.n_iter) 
			solver = Solver(SimplexSearchAlgorithm(),maxSolutionStopper)
			problem = ProblemFactory.getInverseSquareMinimizerProblem(variables,scorer,0.0001)
			problem.addHint(delta_hint)
			solver.solve(problem)
			#------- get results
			trial = solver.getScoreBoard().getBestSolution()
			err2 = scorer.score(trial,variables)				
			scorer.copyToExternalParams(self.param_arr)
			#print "debug i_h=",i_h," =====final err2=",math.sqrt(err2),"==== param_arr=", self.param_arr				
			if(i_h != self.n_harm):
				(amp,shift) = scorer.getNextHarm_Amp_Shift()
				#print "debug last step (amp,shift)=",(amp,shift)
				self.param_arr[2*i_h+1] = amp
				self.param_arr[2*i_h+2] = shift	
				#print "debug i_h=",i_h," ===== last step==== param_arr=", self.param_arr	
		return math.sqrt(err2)
Пример #2
0
	def analyzeData(self,gd):
		""" 
		Analysis of the BasicGraphData() instance with data.
		Returns the error value.
		"""
		if(gd.getNumbOfPoints() == 0): return 0.
		y_max = gd.getMaxY()
		y_min = gd.getMinY()
		amp0 = (y_max - y_min)/2.0
		amp_step = 0.05*amp0 
		phase_step = 3.0
		self.param_arr[0] = (y_max + y_min)/2.0
		for i_h in range(self.n_harm+1):
			param_local_arr = self.param_arr[0:2*i_h+1]
			#print "debug i_h=",i_h," =====start ==== param_arr=",param_local_arr 
			variables = ArrayList()	
			delta_hint = InitialDelta()
			var = Variable("base",param_local_arr[0], - Double.MAX_VALUE, Double.MAX_VALUE)
			variables.add(var)
			delta_hint.addInitialDelta(var,amp_step)
			for i_fit in range(i_h):
				var = Variable("amp"+str(i_fit),param_local_arr[2*i_fit+1], - Double.MAX_VALUE, Double.MAX_VALUE)
				variables.add(var)
				delta_hint.addInitialDelta(var,amp_step)
				var = Variable("phase"+str(i_fit),param_local_arr[2*i_fit+2], - Double.MAX_VALUE, Double.MAX_VALUE)
				variables.add(var)
				delta_hint.addInitialDelta(var,phase_step)
			scorer = HarmScorer(gd,variables)
			self.harm_func = scorer.getHarmFunc()
			if(i_h == 0):
				scorer.copyFromExternalParams(self.param_arr)
				(amp,shift) = scorer.getNextHarm_Amp_Shift()
				#print "debug zero harm (amp,shift)=",(amp,shift)
				self.param_arr[2*i_h+1] = amp
				self.param_arr[2*i_h+2] = shift
				#print "debug i_h=",i_h," =====final ==== param_arr=", self.param_arr		
				continue
			maxSolutionStopper = SolveStopperFactory.maxEvaluationsStopper((i_h+1)*self.n_iter) 
			solver = Solver(SimplexSearchAlgorithm(),maxSolutionStopper)
			problem = ProblemFactory.getInverseSquareMinimizerProblem(variables,scorer,0.0001)
			problem.addHint(delta_hint)
			solver.solve(problem)
			#------- get results
			trial = solver.getScoreBoard().getBestSolution()
			err2 = scorer.score(trial,variables)				
			scorer.copyToExternalParams(self.param_arr)
			#print "debug i_h=",i_h," =====final err2=",math.sqrt(err2),"==== param_arr=", self.param_arr				
			if(i_h != self.n_harm):
				(amp,shift) = scorer.getNextHarm_Amp_Shift()
				#print "debug last step (amp,shift)=",(amp,shift)
				self.param_arr[2*i_h+1] = amp
				self.param_arr[2*i_h+2] = shift	
				#print "debug i_h=",i_h," ===== last step==== param_arr=", self.param_arr	
		return math.sqrt(err2)
	def fit(self):
		if(self.active_cav_wrapper == null): return
		variables = ArrayList()
		delta_hint = InitialDelta()
		#----- variable eKin_in
		var = Variable("eKin_in",self.eKin_in, - Double.MAX_VALUE, Double.MAX_VALUE)
		variables.add(var)
		delta_hint.addInitialDelta(var,0.3)
		#----- variable cavity amplitude
		var = Variable("cav_amp",self.cav_amp, - Double.MAX_VALUE, Double.MAX_VALUE)
		variables.add(var)
		delta_hint.addInitialDelta(var,self.cav_amp*0.01)
		#----- variable cavity phase offset
		var = Variable("phase_offset",self.cav_phase_shift, - Double.MAX_VALUE, Double.MAX_VALUE)
		variables.add(var)
		delta_hint.addInitialDelta(var,1.0)
		#-------- solve the fitting problem
		scorer = CavAmpPhaseScorer(self,variables)
		maxSolutionStopper = SolveStopperFactory.maxEvaluationsStopper(120) 
		self.solver = Solver(SimplexSearchAlgorithm(),maxSolutionStopper)
		problem = ProblemFactory.getInverseSquareMinimizerProblem(variables,scorer,0.0001)
		problem.addHint(delta_hint)
		self.solver.solve(problem)
		#------- get results
		trial = self.solver.getScoreBoard().getBestSolution()
		err2 = scorer.score(trial,variables)	
		[self.eKin_in,self.cav_amp,self.cav_phase_shift] = scorer	.getTrialParams(trial)	
		self.active_cav_wrapper.eKin_in = self.eKin_in
		self.active_cav_wrapper.designPhase = makePhaseNear(self.active_cav_wrapper.livePhase - self.cav_phase_shift,0.)
		self.active_cav_wrapper.eKin_err = math.sqrt(err2)
		cav_phase = self.active_cav_wrapper.livePhase
		self.active_cav_wrapper.eKin_out = self.getModelEnergyOut(self.eKin_in,self.cav_amp,cav_phase,self.cav_phase_shift)
		#print "debug cav=",self.active_cav_wrapper.alias," shift=",self.cav_phase_shift," amp=",self.cav_amp," err2=",	math.sqrt(err2)," ekinOut=",	self.active_cav_wrapper.eKin_out		
		#----- this defenition of the avg. gap phase will be replaced by another with self.model_eKin_in
		self.active_cav_wrapper.avg_gap_phase = self.getAvgGapPhase()
		self.active_cav_wrapper.designAmp = self.cav_amp
		self.solver = null
		#----make theory graph plot
		x_arr = []
		y_arr = []
		for i in range(self.scan_gd.getNumbOfPoints()):
			phase = self.scan_gd.getX(i)
			y = self.scan_gd.getY(i)
			x_arr.append(phase)
			y_arr.append(y)
		self.active_cav_wrapper.eKinOutPlotTh.addPoint(x_arr,y_arr)			
field_max = 0.012
field_min = -0.012

field_step = (field_max - field_min) / 30

for dch_ind in range(len(dchs)):
    dch = dchs[dch_ind]
    field = dch.getField()
    var = Variable(dch.getId(), field, field_min, field_max)
    variables.add(var)
    delta_hint.addInitialDelta(var, field_step)

scorer = OrbitScorer(bpms, dchs, variables)

n_iterations = 200
maxSolutionStopper = SolveStopperFactory.maxEvaluationsStopper(n_iterations)
#solver = Solver(SimplexSearchAlgorithm(),maxSolutionStopper)
solver = Solver(RandomShrinkSearch(), maxSolutionStopper)
problem = ProblemFactory.getInverseSquareMinimizerProblem(
    variables, scorer, 0.00000001)
problem.addHint(delta_hint)
solver.solve(problem)

#------- get optimization results
trial = solver.getScoreBoard().getBestSolution()
dch_field_arr = scorer.getDCH_Field_Arr_for_Trial(trial)

print "========== put new dch fields into VA ======"
#---- send all results to EPICS
for dch_ind in range(len(dchs)):
    dch = dchs[dch_ind]
Пример #5
0
	def run(self):
		tr_twiss_analysis_controller = self.linac_wizard_document.tr_twiss_analysis_controller
		transverse_twiss_fitting_controller = tr_twiss_analysis_controller.transverse_twiss_fitting_controller
		init_and_fit_params_controller = transverse_twiss_fitting_controller.init_and_fit_params_controller
		initial_twiss_params_holder = transverse_twiss_fitting_controller.initial_twiss_params_holder
		final_twiss_params_holder = init_and_fit_params_controller.final_twiss_params_holder
		nIterations = int(init_and_fit_params_controller.fit_iter_text.getValue())	
		#print "debug Twiss_Fitter start to run! Iter=",nIterations
		(alphaX, betaX, emittX) = initial_twiss_params_holder.getParams(0)
		(alphaY, betaY, emittY) = initial_twiss_params_holder.getParams(1)
		(alphaZ, betaZ, emittZ) = initial_twiss_params_holder.getParams(2)
		twiss_arr = []
		twiss_arr.append(Twiss(alphaX, betaX, emittX))
		twiss_arr.append(Twiss(alphaY, betaY, emittY))
		twiss_arr.append(Twiss(alphaZ, betaZ, emittZ))
		(alphaXStep, betaXStep, emittXStep) = initial_twiss_params_holder.getParamsStep(0)
		(alphaYStep, betaYStep, emittYStep) = initial_twiss_params_holder.getParamsStep(1)
		(alphaZStep, betaZStep, emittZStep) = initial_twiss_params_holder.getParamsStep(2)	
		variables = ArrayList()
		variables.add(Variable("alphaX", alphaX, - Double.MAX_VALUE, Double.MAX_VALUE))
		variables.add(Variable("betaX",   betaX, - Double.MAX_VALUE, Double.MAX_VALUE))
		variables.add(Variable("emittX", emittX, - Double.MAX_VALUE, Double.MAX_VALUE))
		variables.add(Variable("alphaY", alphaY, - Double.MAX_VALUE, Double.MAX_VALUE))
		variables.add(Variable("betaY",   betaY, - Double.MAX_VALUE, Double.MAX_VALUE))
		variables.add(Variable("emittY", emittY, - Double.MAX_VALUE, Double.MAX_VALUE))
		variables.add(Variable("alphaZ", alphaZ, - Double.MAX_VALUE, Double.MAX_VALUE))
		variables.add(Variable("betaZ",   betaZ, - Double.MAX_VALUE, Double.MAX_VALUE))
		variables.add(Variable("emittZ", emittZ, - Double.MAX_VALUE, Double.MAX_VALUE))
		delta_hint = InitialDelta()
		variables_fit = ArrayList()
		#---------- X
		if(alphaXStep != 0.): 
			variables_fit.add(variables.get(0))
			delta_hint.addInitialDelta(variables.get(0), alphaXStep)
		if(betaXStep != 0.): 
			variables_fit.add(variables.get(1))
			delta_hint.addInitialDelta(variables.get(1), betaXStep)
		if(emittXStep != 0.): 
			variables_fit.add(variables.get(2))
			delta_hint.addInitialDelta(variables.get(2), emittXStep)
		#---------- Y
		if(alphaYStep != 0.): 
			variables_fit.add(variables.get(3))
			delta_hint.addInitialDelta(variables.get(3), alphaYStep)
		if(betaYStep != 0.): 
			variables_fit.add(variables.get(4))
			delta_hint.addInitialDelta(variables.get(4), betaYStep)
		if(emittYStep != 0.): 
			variables_fit.add(variables.get(5))
			delta_hint.addInitialDelta(variables.get(5), emittYStep)			
		#---------- Z
		if(alphaZStep != 0.): 
			variables_fit.add(variables.get(6))
			delta_hint.addInitialDelta(variables.get(6), alphaZStep)
		if(betaZStep != 0.): 
			variables_fit.add(variables.get(7))
			delta_hint.addInitialDelta(variables.get(7), betaZStep)
		if(emittZStep != 0.): 
			variables_fit.add(variables.get(8))
			delta_hint.addInitialDelta(variables.get(8), emittZStep)	
		#------- fitting process with solver
		if(variables_fit.isEmpty()):
			return
		scorer = AccScoreCalculator(self.linac_wizard_document,variables,twiss_arr)
		maxSolutionStopper = SolveStopperFactory.maxEvaluationsStopper(nIterations) 
		solver = Solver(SimplexSearchAlgorithm(),maxSolutionStopper)
		self.solver = solver
		problem = ProblemFactory.getInverseSquareMinimizerProblem(variables_fit,scorer,0.001)
		problem.addHint(delta_hint)
		solver.solve(problem)
		#------- get results
		trial = solver.getScoreBoard().getBestSolution()
		scorer.trialToTwiss(trial)
		twiss_arr = scorer.getTwissArr()
		(alphaX, betaX, emittX) = (twiss_arr[0].getAlpha(),twiss_arr[0].getBeta(),twiss_arr[0].getEmittance())
		(alphaY, betaY, emittY) = (twiss_arr[1].getAlpha(),twiss_arr[1].getBeta(),twiss_arr[1].getEmittance())
		(alphaZ, betaZ, emittZ) = (twiss_arr[2].getAlpha(),twiss_arr[2].getBeta(),twiss_arr[2].getEmittance())			
		final_twiss_params_holder.setParams(0,alphaX, betaX, emittX)
		final_twiss_params_holder.setParams(1,alphaY, betaY, emittY)
		final_twiss_params_holder.setParams(2,alphaZ, betaZ, emittZ)
		init_and_fit_params_controller.finalTwiss_table.getModel().fireTableDataChanged()
		init_and_fit_params_controller.fit_iter_left_text.setValue(0.)
		scorer.setUpLastNode(false)
		scorer.calculateDiff2()