Commit d2b871c1 authored by Zolnierczuk, Piotr's avatar Zolnierczuk, Piotr
Browse files

more or less working flippers

need to get the absolute scale right
parent 9a627140
Loading
Loading
Loading
Loading
+35 −19
Original line number Diff line number Diff line
@@ -8,23 +8,26 @@ import scipy.optimize as opt
from numpy import sqrt, cos

try:
    from ..constants import (PI, MU_0, HMN, GAMMA_N)
    from ..constants import (PI, MU_0, HMN, OMEGA_N)
except ImportError:
    from scipy import constants as _const
    PI      = np.pi
    MU_0    = 4e-7*PI
    HMN     = _const.Planck/_const.neutron_mass
    GAMMA_N = _const.physical_constants['neutron gyromag. ratio'][0]
    OMEGA_N = _const.physical_constants['neutron gyromag. ratio'][0]/HMN



_pisqrt2  = PI/sqrt(2)
PISQRT2 = PI/sqrt(2)

def rxx13(omega_z, omega_x):
    """omega.z**2 cos(omega.x**2+omega.z**2) + omega.x**2"""
    omega_x, omega_z = omega_x**2, omega_z**2 # compute the squares
    return omega_z*cos(sqrt(omega_x+omega_z)) + omega_x

def current_sheet(turns_density):
    "magnetic field of an infinite current sheet per unit current"
    return MU_0/2*turns_density


class BaseFlipper:
    "Base flipper class"
    def __init__(self, name, position=0, thickness=0.01, turns_density=1):
@@ -38,9 +41,14 @@ class BaseFlipper:
        "the flipper"
        return f"{self.name:8s} (pos={self.position:g}m d={self.thickness:g}m n={self.turns_density:g})"

    def current(self, lam):
        "flipper current"
        return HMN/GAMMA_N * PI/(MU_0*self.turns_density*self.thickness*lam)
    def current(self, arg, rotation=None):
        """calculate flipper current
            arg      - wavelength or magnetic field (if rotation is None)
            rotation - rotation angle in radians (if given arg is wavelength)
            returns current in Amperes
        """
        bfield = arg if rotation is None else rotation/(OMEGA_N*self.thickness*arg)
        return bfield/current_sheet(self.turns_density)/2 # factor of 2 for two sheets per flipper


class FlipperPi(BaseFlipper):
@@ -57,26 +65,30 @@ class FlipperPi(BaseFlipper):

        (bx_ext, _ , bz_ext) = (0, 0, 0) if b_ext is None else b_ext

        sigma = HMN * PI/(self.thickness*GAMMA_N)
        sigma = PI/OMEGA_N/self.thickness
        f0 = sqrt((sigma/lam0)**2 - bx_ext**2) - bz_ext
        fl = sqrt((sigma/lam )**2 - bx_ext**2) - bz_ext

        return fl/f0

    def current(self, arg, rotation=PI):
        "flipper current"
        return super().current(arg, rotation=rotation)

class FlipperPi2(BaseFlipper):
    """Simple pi/2-flipper class """

    def current(self, lam):
    def current(self, arg, rotation=PISQRT2):
        "flipper current"
        return super().current(lam)/sqrt(2)
        return super().current(arg, rotation=rotation)

    @staticmethod
    def curve(lam, lam0, **_kwargs):
        """pi/2-flipper ramp curve
        lam  - array of neutron wavelengths
        lam0 - reference wavelength (lambda_max at SNS-NSE)"""
        lam     = np.asarray(lam)
        omega_x = _pisqrt2*lam/lam0
        lam     = np.atleast_1d(lam)
        omega_x = PISQRT2*lam/lam0
        omega_z = np.asarray([opt.newton(rxx13,PI/2,args=(x,)) for x in omega_x])
        return omega_z/omega_x

@@ -107,17 +119,21 @@ def tof_curve(flipper, lmax, dlam, points, sample_to_source, **kwargs):
    lam     = (lam[1:]+lam[:-1])/2
    tofmax  = dhmn * lmax
    tofmax  = tofmax - int(tofmax*freq)/freq
    zeroidx = int(np.round(points*tofmax*freq))-1
    zeroidx = int(np.round(points*tofmax*freq))

    # the "curve"
    curve = flipper.curve(lam, lmax, b_ext=b_ext)
    cmin, cmax = min(curve), max(curve)

    # ramp currents
    ramp  = curve[-1] + np.arange(nrewind+1)/(nrewind+1)*(curve[0]-curve[-1])
    # keep at max
    hold  = np.ones((ngap-nrewind-1,))*curve[0]
    ramp  = np.arange(1,nrewind+1)/(nrewind+1)*(cmax-cmin) + cmin

    # keep at max
    hold  = np.ones((ngap-nrewind+1,))*cmax
    #
    # build and roll the curve
    curve = np.hstack((ramp, hold, curve))
    curve = np.roll(curve, zeroidx)
    curve = np.hstack((ramp, hold, curve[1:])) # drop the first point
    curve = np.roll(curve, zeroidx)/cmin # normalize to cmin
    #
    return curve
# End of file
+163 −0
Original line number Diff line number Diff line
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.644367
	    0.642464
	    0.640571
	    0.638690
	    0.636819
	    0.634959
	    0.633109
	    0.631271
	    0.629442
	    0.627624
	    0.625817
	    0.624019
	    0.622232
	    0.620455
	    0.618688
	    0.616930
	    0.615183
	    0.613445
	    0.611716
	    0.609998
	    0.608288
	    0.606588
	    0.604898
	    0.603216
	    0.601544
	    0.599881
	    0.598227
	    0.596581
	    0.594945
	    0.593317
	    0.591698
	    0.590088
	    0.588486
	    0.586893
	    0.585308
	    0.583732
	    0.582164
	    0.580604
	    0.579052
	    0.577509
	    0.575973
	    0.574445
	    0.572926
	    0.571414
	    0.569910
	    0.568413
	    0.566924
	    0.565443
	    0.563970
	    0.562504
	    0.561045
	    0.559594
	    0.558150
	    0.556713
	    0.555283
	    0.553861
	    0.552446
	    0.551038
	    0.549636
	    0.548242
	    0.546855
	    0.545474
	    0.544100
	    0.542733
	    0.541373
	    0.540019
	    0.538671
	    0.537331
	    0.535997
	    0.534669
	    0.533347
	    0.532032
	    0.530724
	    0.529421
	    0.528125
	    0.526835
	    0.525550
	    0.524272
	    0.523001
	    0.521735
	    0.520474
	    0.519220
	    0.517972
	    0.516730
	    0.515493
	    0.514262
	    0.513037
	    0.511817
	    0.510603
	    0.509394
	    0.508191
	    0.506994
	    0.505802
	    0.504615
	    0.503434
	    0.502258
	    0.501088
	    0.499923
	    0.498763
	    0.497608
	    0.496458
	    0.495313
	    0.494174
	    0.493040
	    0.491910
	    0.490786
	    0.489666
	    0.488552
	    0.487442
	    0.486337
	    0.485237
	    0.484142
	    0.483052
	    0.481966
	    0.480885
	    0.479808
	    0.478737
	    0.477670
	    0.476607
	    0.475549
	    0.474496
	    0.473447
	    0.472402
	    0.471362
	    0.470326
	    0.469295
	    0.468268
	    0.467245
	    0.466227
	    0.465212
	    0.464202
	    0.494549
	    0.524896
	    0.555242
	    0.585589
	    0.615935
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
	    0.646282
+163 −0
Original line number Diff line number Diff line
	    0.449480
	    0.448565
	    0.447653
	    0.446746
	    0.445841
	    0.444941
	    0.444044
	    0.443150
	    0.442260
	    0.441373
	    0.440490
	    0.439610
	    0.438734
	    0.437861
	    0.436991
	    0.436125
	    0.435262
	    0.434402
	    0.433546
	    0.432693
	    0.431843
	    0.430996
	    0.430153
	    0.429312
	    0.428475
	    0.427641
	    0.426810
	    0.425983
	    0.425158
	    0.424336
	    0.423518
	    0.422702
	    0.421890
	    0.421080
	    0.420274
	    0.419471
	    0.418670
	    0.417872
	    0.417078
	    0.416286
	    0.415497
	    0.414711
	    0.413928
	    0.413148
	    0.412370
	    0.411596
	    0.410824
	    0.410055
	    0.409288
	    0.408525
	    0.407764
	    0.407006
	    0.406250
	    0.405497
	    0.404747
	    0.404000
	    0.403255
	    0.402513
	    0.401773
	    0.401036
	    0.400302
	    0.399570
	    0.398841
	    0.398114
	    0.397390
	    0.396669
	    0.395949
	    0.395233
	    0.394519
	    0.393807
	    0.393098
	    0.392391
	    0.391686
	    0.390984
	    0.390285
	    0.389587
	    0.388893
	    0.388200
	    0.387510
	    0.386822
	    0.386137
	    0.385453
	    0.384773
	    0.384094
	    0.383418
	    0.382744
	    0.382072
	    0.381402
	    0.380735
	    0.380070
	    0.379407
	    0.378746
	    0.378087
	    0.377431
	    0.376777
	    0.376125
	    0.375475
	    0.374827
	    0.374181
	    0.373538
	    0.372896
	    0.372257
	    0.371619
	    0.370984
	    0.370351
	    0.369719
	    0.387202
	    0.404684
	    0.422166
	    0.439648
	    0.457130
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.474612
	    0.473595
	    0.472582
	    0.471573
	    0.470569
	    0.469568
	    0.468572
	    0.467580
	    0.466592
	    0.465608
	    0.464628
	    0.463653
	    0.462681
	    0.461713
	    0.460749
	    0.459789
	    0.458833
	    0.457880
	    0.456932
	    0.455987
	    0.455046
	    0.454109
	    0.453176
	    0.452246
	    0.451320
	    0.450398
+163 −0
Original line number Diff line number Diff line
	    1.311114
	    1.303057
	    1.295097
	    1.287231
	    1.279460
	    1.271780
	    1.264190
	    1.256689
	    1.249275
	    1.241947
	    1.234703
	    1.227542
	    1.220462
	    1.213461
	    1.206540
	    1.199695
	    1.192927
	    1.186233
	    1.179613
	    1.173064
	    1.166587
	    1.160180
	    1.153842
	    1.147571
	    1.141367
	    1.135228
	    1.129154
	    1.123143
	    1.117195
	    1.111308
	    1.105482
	    1.099715
	    1.094008
	    1.088358
	    1.082764
	    1.077227
	    1.071746
	    1.066318
	    1.060945
	    1.055624
	    1.050355
	    1.045137
	    1.039970
	    1.034853
	    1.029784
	    1.024764
	    1.019792
	    1.014867
	    1.009988
	    1.005155
	    1.277824
	    1.550494
	    1.823164
	    2.095833
	    2.368503
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.641173
	    2.609089
	    2.577771
	    2.547193
	    2.517327
	    2.488149
	    2.459637
	    2.431767
	    2.404517
	    2.377868
	    2.351800
	    2.326293
	    2.301330
	    2.276894
	    2.252969
	    2.229537
	    2.206585
	    2.184097
	    2.162060
	    2.140460
	    2.119285
	    2.098521
	    2.078157
	    2.058182
	    2.038584
	    2.019354
	    2.000480
	    1.981952
	    1.963762
	    1.945901
	    1.928359
	    1.911127
	    1.894198
	    1.877565
	    1.861218
	    1.845151
	    1.829356
	    1.813828
	    1.798558
	    1.783541
	    1.768771
	    1.754241
	    1.739945
	    1.725878
	    1.712035
	    1.698410
	    1.684998
	    1.671794
	    1.658793
	    1.645991
	    1.633383
	    1.620965
	    1.608732
	    1.596680
	    1.584806
	    1.573105
	    1.561574
	    1.550208
	    1.539005
	    1.527961
	    1.517073
	    1.506337
	    1.495750
	    1.485309
	    1.475011
	    1.464853
	    1.454832
	    1.444946
	    1.435191
	    1.425566
	    1.416067
	    1.406693
	    1.397440
	    1.388307
	    1.379290
	    1.370388
	    1.361599
	    1.352921
	    1.344351
	    1.335887
	    1.327528
	    1.319271
+163 −0
Original line number Diff line number Diff line
	    0.657540
	    0.655507
	    0.653486
	    0.651477
	    0.649480
	    0.647495
	    0.645522
	    0.643560
	    0.641610
	    0.639671
	    0.637744
	    0.635828
	    0.633923
	    0.632029
	    0.698715
	    0.765400
	    0.832086
	    0.898771
	    0.965456
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.032142
	    1.027216
	    1.022337
	    1.017504
	    1.012715
	    1.007971
	    1.003271
	    0.998614
	    0.993999
	    0.989427
	    0.984896
	    0.980406
	    0.975956
	    0.971546
	    0.967175
	    0.962843
	    0.958549
	    0.954293
	    0.950074
	    0.945892
	    0.941746
	    0.937635
	    0.933560
	    0.929520
	    0.925514
	    0.921542
	    0.917604
	    0.913699
	    0.909826
	    0.905986
	    0.902178
	    0.898401
	    0.894655
	    0.890940
	    0.887255
	    0.883600
	    0.879975
	    0.876379
	    0.872811
	    0.869273
	    0.865762
	    0.862279
	    0.858824
	    0.855396
	    0.851995
	    0.848620
	    0.845272
	    0.841949
	    0.838652
	    0.835381
	    0.832134
	    0.828913
	    0.825715
	    0.822542
	    0.819393
	    0.816267
	    0.813165
	    0.810086
	    0.807029
	    0.803996
	    0.800984
	    0.797995
	    0.795028
	    0.792082
	    0.789157
	    0.786254
	    0.783371
	    0.780510
	    0.777668
	    0.774847
	    0.772046
	    0.769265
	    0.766503
	    0.763761
	    0.761038
	    0.758333
	    0.755648
	    0.752981
	    0.750333
	    0.747703
	    0.745090
	    0.742496
	    0.739919
	    0.737360
	    0.734818
	    0.732293
	    0.729785
	    0.727294
	    0.724819
	    0.722361
	    0.719919
	    0.717493
	    0.715083
	    0.712689
	    0.710311
	    0.707948
	    0.705600
	    0.703267
	    0.700950
	    0.698647
	    0.696359
	    0.694086
	    0.691827
	    0.689582
	    0.687352
	    0.685135
	    0.682933
	    0.680744
	    0.678569
	    0.676407
	    0.674259
	    0.672124
	    0.670002
	    0.667893
	    0.665798
	    0.663714
	    0.661644
	    0.659586
Loading