Project

General

Profile

2016-08-25-optics » currents_to_fields.py

Rogers, Chris, 25 August 2016 17:32

 
1
import json
2

    
3
import cdb
4
import xboa.common
5

    
6
DS = ["DS"]
7
Q123 = ["Q1", "Q2", "Q3"]
8
Q4TO9 = ["Q4", "Q5", "Q6", "Q7", "Q8", "Q9",]
9
D12 = ["D1", "D2"]
10
ALL_MAGS = sorted(Q123 + Q4TO9 + D12 + DS)
11

    
12
def d12_current(field):
13
    current = 253.91*field #+39.59*field**3-55.998*field**2
14
    return current
15

    
16
def d12_field(current):
17
    #a = 39.59
18
    #b = -55.998
19
    c = 253.91
20
    #d = -current
21

    
22
    guess = current/c
23
    func = lambda x: [d12_current(x[0])-current]
24
    root = xboa.common.nd_newton_raphson1(func,
25
                                   [1e-3],
26
                                   [guess],
27
                                   [1.], verbose = False)
28
    return root[0]
29

    
30
def q123_current(field_gradient):
31
    return 96.00*field_gradient
32

    
33
def q123_field_gradient(field_gradient):
34
    return field_gradient/96.00
35

    
36
def q4to9_current(field_gradient):
37
    return 174.09*field_gradient
38

    
39
def q4to9_field_gradient(current):
40
    return current/174.09
41

    
42
def ds_field(field):
43
    return field*5./870.
44

    
45
def ds_current(current):
46
    return 870*current/5.
47

    
48
def currents_to_fields(magnets):
49
    if sorted(magnets.keys()) != ALL_MAGS:
50
        raise KeyError("Missing keys")
51
    currents = {}
52
    for key in magnets.keys():
53
        if key in Q4TO9:
54
            currents[key] = q4to9_field_gradient(magnets[key])
55
        elif key in Q123:
56
            currents[key] = q123_field_gradient(magnets[key])
57
        elif key in D12:
58
            currents[key] = d12_field(magnets[key])
59
        elif key in DS:
60
            currents[key] = ds_field(magnets[key])
61
    return currents
62

    
63

    
64
def fields_to_currents(magnets):
65
    if sorted(magnets.keys()) != ALL_MAGS:
66
        raise KeyError("Missing keys")
67
    currents = {}
68
    for key in magnets.keys():
69
        if key in Q4TO9:
70
            currents[key] = q4to9_current(magnets[key])
71
        elif key in Q123:
72
            currents[key] = q123_current(magnets[key])
73
        elif key in D12:
74
            currents[key] = d12_current(magnets[key])
75
        elif key in DS:
76
            currents[key] = ds_current(magnets[key])
77
    return currents
78

    
79

    
80
def get_fields_for_beamline(run_number):
81
    print "Getting fields for beamline run", run_number
82
    bl = cdb.Beamline()
83
    beamline_all = bl.get_beamline_for_run(run_number)[run_number]
84
    beamline = beamline_all['magnets']
85
    print beamline.keys()
86
    currents = {}
87
    def_polarity = {"Q1":1, "Q2":-1, "Q3":1, "D1":-1, "D2":-1, "DS":1}
88
    for magnet in ALL_MAGS:
89
        print beamline[magnet]["polarity"]
90
        currents[magnet] = beamline[magnet]["set_current"]
91
        if magnet in def_polarity.keys():
92
            currents[magnet] *= beamline[magnet]["polarity"]*def_polarity[magnet]
93
        if magnet == "DS" and currents[magnet] == None:
94
            currents[magnet] = 0.
95
    print "currents:"
96
    for key in sorted(currents.keys()):
97
        print "   ", key+":", currents[key]
98
    magnets = currents_to_fields(currents)
99
    print "fields:"
100
    for key in sorted(magnets.keys()):
101
        print "   ", key+":", magnets[key]
102
    currents_2 = fields_to_currents(magnets)
103
    for key in sorted(currents_2.keys()):
104
        if currents[key] - currents_2[key] > 1e-6:
105
            raise ValueError("Urk")
106
    print "MAUS keys:"
107
    print '    # cdb derived currents for run', run_number
108
    key_sort = {"Q1":"q_1", "Q2":"q_2", "Q3":"q_3", "D1":"d_1", "D2":"d_2", "DS":"d_s"}
109
    for key in sorted(key_sort):
110
        if key in key_sort:
111
            maus_key = key_sort[key]
112
            print '    "'+maus_key+'":'+str(magnets[key])+','
113
    thickness = beamline_all["proton_absorber_thickness"]
114
    print '    "proton_absorber_thickness":'+str(thickness)+','
115
    print "Got fields for beamline run", run_number
116
        
117

    
118

    
119
def main():
120
    get_fields_for_beamline(7434)
121

    
122
if __name__ == "__main__":
123
    main()
124

    
(10-10/11)