Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Usage example
  • Loading branch information
elafmusa authored Nov 21, 2023
1 parent 837995a commit cdb6cab
Showing 1 changed file with 355 additions and 0 deletions.
355 changes: 355 additions & 0 deletions pySC/example_hmba.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,355 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" INFO | len(ring)=131 | ipykernel_14552.2914558345\n",
" INFO | Circumference error applied. | core.simulated_commissioning\n",
" INFO | Calculating model response matrix | lattice_properties.response_model\n",
" INFO | Calculating model dispersion | lattice_properties.response_model\n",
"Calculating Measure response matrix\n",
"Calculating Measure response matrix\n"
]
}
],
"source": [
"import at\n",
"from pySC.core.simulated_commissioning import SimulatedCommissioning\n",
"from pySC.utils.sc_tools import SCgetOrds\n",
"from pySC.plotting.plot_lattice import plot_lattice\n",
"from pySC.utils import logging_tools\n",
"from pySC.correction.loco_modules import *\n",
"from matplotlib import pyplot as plt\n",
"from pySC.lattice_properties.response_measurement import response_matrix\n",
"import copy\n",
"from at import Lattice\n",
"LOGGER = logging_tools.get_logger(__name__)\n",
"def create_at_lattice() -> Lattice:\n",
" def _marker(name):\n",
" return at.Marker(name, PassMethod='IdentityPass')\n",
" new_ring = at.load_mat('hmba.mat')\n",
" bpm_indexes = at.get_refpts(new_ring,at.elements.Monitor)\n",
" for i in reversed(bpm_indexes):\n",
" Cor = at.elements.Corrector('CXY'+str(i), length = 0, kick_angle= [0, 0], PolynomA=[0,0] , PolynomB=[0,0])\n",
" new_ring.insert(i+1, Cor)\n",
" new_ring.enable_6d()\n",
" at.set_cavity_phase(new_ring)\n",
" at.set_rf_frequency(new_ring)\n",
" new_ring.tapering(niter=3, quadrupole=True, sextupole=True)\n",
"\n",
" return new_ring\n",
"\n",
"if __name__ == \"__main__\":\n",
" ring = at.Lattice(create_at_lattice())\n",
" LOGGER.info(f\"{len(ring)=}\")\n",
" SC = SimulatedCommissioning(ring)\n",
" ords = SCgetOrds(SC.RING, 'BPM')\n",
" SC.register_bpms(ords,\n",
" Roll=0.0, CalError=1E-2 * np.ones(2))\n",
" ords = SCgetOrds(SC.RING, 'QF')\n",
" SC.register_magnets(ords, # [rad]\n",
" CalErrorB=np.array([0, 1E-2])) # relative\n",
" ords = SCgetOrds(SC.RING, 'QD')\n",
" SC.register_magnets(ords, # [rad]\n",
" CalErrorB=np.array([0, 1E-2]))#\n",
"\n",
" ords = SCgetOrds(SC.RING, 'CXY')\n",
" SC.register_magnets(ords, CalErrorA=np.array([1E-200, 0]),\n",
" CalErrorB=np.array([1E-200, 0]))\n",
"\n",
" ords = SCgetOrds(SC.RING, 'BEND')\n",
" SC.register_magnets(ords)\n",
" ords = SCgetOrds(SC.RING, 'SF|SD')\n",
" SC.register_magnets(ords) # [1/m]\n",
" ords = SCgetOrds(SC.RING, 'RFC')\n",
" SC.register_cavities(ords)\n",
" SC.apply_errors()\n",
"\n",
"CorOrds = SCgetOrds(SC.RING, 'CXY')\n",
"\n",
"used_correctors1 = select_equally_spaced_elements(CorOrds, 10)\n",
"used_correctors2 = select_equally_spaced_elements(CorOrds, 10)\n",
"CorOrds = [used_correctors1,used_correctors2]\n",
"used_bpm1 = select_equally_spaced_elements(SC.ORD.BPM, 10)\n",
"used_bpm2 = select_equally_spaced_elements(SC.ORD.BPM, 10)\n",
"used_bpms = [used_bpm1,used_bpm2]\n",
"\n",
"CAVords = SCgetOrds(SC.RING, 'RFC')\n",
"quadsOrds = [SCgetOrds(SC.RING, 'QF'), SCgetOrds(SC.RING, 'QD')]\n",
"CAVords = SCgetOrds(SC.RING, 'RFCav')\n",
"sextOrds = SCgetOrds(SC.RING, 'SF|SD')\n",
"skewOrds = SC.ORD.SkewQuad\n",
"CMstep = np.array([1.e-4]) #correctors change [rad]\n",
"dk = 1.e-4 #quads change\n",
"RFstep = 1e3\n",
"\n",
"_, _, twiss = at.get_optics(SC.IDEALRING, SC.ORD.BPM)\n",
"orbit_response_matrix_model = SCgetModelRM(SC, SC.ORD.BPM, CorOrds, trackMode='ORB', useIdealRing=True, dkick= CMstep)\n",
"ModelDispersion = SCgetModelDispersion(SC, SC.ORD.BPM, CAVords, trackMode='ORB', Z0=np.zeros(6), nTurns=1, rfStep=RFstep, useIdealRing=True)\n",
"errq =[]\n",
"for i in np.concatenate(quadsOrds):\n",
" errq.append(SC.RING[i].K- SC.IDEALRING[i].K)\n",
"\n",
"err_cor=[]\n",
"for i in CorOrds[0]:\n",
" err_cor.append(SC.RING[i].CalErrorB[0])\n",
"for i in CorOrds[1]:\n",
" err_cor.append(SC.RING[i].CalErrorA[0])\n",
"orbit_response_matrix_measured = SCgetMeasurRM(SC, SC.ORD.BPM, CorOrds, CMstep)\n",
"_, _, twiss_err = at.get_optics(SC.RING, SC.ORD.BPM)\n",
"\n",
"Jn = generatingJacobian(SC, orbit_response_matrix_model, CMstep, CorOrds, SC.ORD.BPM, np.concatenate(quadsOrds), dk, debug=True, trackMode='ORB', useIdealRing=False,skewness = False, order=1, method='add', includeDispersion=False, rf_step=RFstep, cav_ords=CAVords )\n",
"\n",
"err_cor=[]\n",
"for i in CorOrds[0]:\n",
" err_cor.append(SC.RING[i].CalErrorB[0])\n",
"for i in CorOrds[1]:\n",
" err_cor.append(SC.RING[i].CalErrorA[0])\n",
"orbit_response_matrix_measured = SCgetMeasurRM(SC, SC.ORD.BPM, CorOrds, CMstep)\n",
"\n",
"\n",
"err_bpms=[]\n",
"for i in SC.ORD.BPM:\n",
" err_bpms.append(SC.RING[i].CalError[0])\n",
"for i in SC.ORD.BPM:\n",
" err_bpms.append(SC.RING[i].CalError[1])"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-21T10:56:29.568980900Z",
"start_time": "2023-11-21T10:56:23.114673600Z"
}
}
},
{
"cell_type": "code",
"execution_count": 19,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LOCO iteration 0\n",
"Calculating Measure response matrix\n",
"`ftol` termination condition is satisfied.\n",
"Function evaluations 582, initial cost 1.8871e+00, final cost 5.4577e-02, first-order optimality 5.16e-06.\n",
"SVD\n",
"Before LOCO correction:\n",
"RMS horizontal beta beating: 6.59% RMS vertical beta beating: 5.13%\n",
"After LOCO corrections\n",
"RMS horizontal beta beating: 1.08% RMS vertical beta beating: 0.61%\n",
"beta_x correction reduction: 83.65%\n",
"beta_y correction reduction: 88.16%\n",
" \n"
]
}
],
"source": [
"numberOfIteration = 1\n",
"sCut = 16\n",
"W = 1\n",
"for x in range(numberOfIteration): # optics correction using QF and QD\n",
" print('LOCO iteration ', x)\n",
"\n",
" C_measure = SCgetMeasurRM(SC, SC.ORD.BPM, CorOrds, CMstep)\n",
" bx_rms_err, by_rms_err = getBetaBeat(SC.RING, twiss, SC.ORD.BPM, makeplot=False)\n",
" Jn = np.transpose(Jn, (0, 2, 1))\n",
" Jt = getInverse(Jn, sCut, W)\n",
"\n",
" quads = len(np.concatenate(quadsOrds))\n",
" cor = len(np.concatenate(CorOrds))\n",
" bpm = len(SC.ORD.BPM) *2\n",
"\n",
" total_length = bpm + cor +quads\n",
" lengths = [quads ,cor, bpm]\n",
" including_fit_parameters = ['quads', 'cor', 'bpm']\n",
" initial_guess = np.zeros(total_length)\n",
" initial_guess[:lengths[0]]= 1e-6\n",
" initial_guess[lengths[0]:lengths[0] + lengths[1] ]= 1e-6\n",
" initial_guess[lengths[0] + lengths[1] :] = 1e-6\n",
"\n",
"\n",
" fit_parameters = loco_correction(lambda delta_params: objective(delta_params, np.transpose(orbit_response_matrix_model), np.transpose(orbit_response_matrix_measured), Jn, lengths, including_fit_parameters, W),initial_guess,np.transpose(orbit_response_matrix_model), np.transpose(orbit_response_matrix_measured), Jn,Jt, lengths,including_fit_parameters\n",
" , verbose=2, max_iterations= 100, eps=1e-6, method='lm',W= W)\n",
"\n",
" dg = fit_parameters[:lengths[0]]\n",
" dx = fit_parameters[lengths[0]:lengths[0] + lengths[1] ]\n",
" dy = fit_parameters[lengths[0] + lengths[1] :]\n",
" print('SVD')\n",
" SC = setCorrection_(SC,dg, np.concatenate(quadsOrds))\n",
" _, _, twiss_corr = at.get_optics(SC.RING, SC.ORD.BPM)\n",
" bx_rms_cor, by_rms_cor = getBetaBeat(SC.RING, twiss, SC.ORD.BPM, makeplot=False)\n",
" print(\n",
" \"Before LOCO correction:\\n\"\n",
" f\"RMS horizontal beta beating: {bx_rms_err*100:.2f}% RMS vertical beta beating: {by_rms_err*100:.2f}%\\n\"\n",
"\n",
" f\"After LOCO corrections\\n\"\n",
" f\"RMS horizontal beta beating: {bx_rms_cor*100:.2f}% RMS vertical beta beating: {by_rms_cor*100:.2f}%\\n\"\n",
" f\"beta_x correction reduction: {(1 - bx_rms_cor / bx_rms_err) * 100:.2f}%\\n\"\n",
" f\"beta_y correction reduction: {(1 - by_rms_cor / by_rms_err) * 100:.2f}%\\n \"\n",
"\n",
" )"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-21T10:56:29.982388100Z",
"start_time": "2023-11-21T10:56:29.584448100Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-21T10:54:24.879871200Z",
"start_time": "2023-11-21T10:54:24.865992400Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-21T10:54:24.904834400Z",
"start_time": "2023-11-21T10:54:24.882145100Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-21T10:54:24.916087100Z",
"start_time": "2023-11-21T10:54:24.898576900Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-21T10:54:24.929662500Z",
"start_time": "2023-11-21T10:54:24.915925Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-21T10:54:24.953167200Z",
"start_time": "2023-11-21T10:54:24.932026500Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-21T10:54:24.964403900Z",
"start_time": "2023-11-21T10:54:24.948149800Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-21T10:54:24.983789900Z",
"start_time": "2023-11-21T10:54:24.966242800Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-21T10:54:24.996629600Z",
"start_time": "2023-11-21T10:54:24.982744200Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-21T10:54:25.017351700Z",
"start_time": "2023-11-21T10:54:24.997627800Z"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit cdb6cab

Please sign in to comment.