From ca792216f16c38b4affaa5d4ee516d67e51655d7 Mon Sep 17 00:00:00 2001
From: Yngve Levinsen <yngve.levinsen@esss.se>
Date: Wed, 15 Aug 2018 11:33:11 +0200
Subject: [PATCH] for relativity, using scipy.constants when applicable moving
 examples out of sources

---
 ess/SP_Relativity.py         | 23 ++++++++++++-----------
 ess/TTF.py                   | 19 +------------------
 examples/fieldmap/example.py | 35 +++++++++++++++++++++++++++++++++++
 examples/ttf/example.py      | 13 +++++++++++++
 4 files changed, 61 insertions(+), 29 deletions(-)
 create mode 100644 examples/fieldmap/example.py
 create mode 100644 examples/ttf/example.py

diff --git a/ess/SP_Relativity.py b/ess/SP_Relativity.py
index cbdc916..446c0e4 100644
--- a/ess/SP_Relativity.py
+++ b/ess/SP_Relativity.py
@@ -2,7 +2,14 @@
 renamed variables not to have a variable with the same name as the fuction the variable is used within
 -- ME. 2018-07-23
 '''
-def beta(energy: float, mass=938.2720813):
+from scipy import constants
+
+# Proton mass in MeV/c
+proton_mass_in_MeV=constants.physical_constants['proton mass energy equivalent in MeV'][0]
+# Speed of light in m/s
+c=constants.c
+
+def beta(energy: float, mass=proton_mass_in_MeV):
     '''
     Parameters
     ----------
@@ -18,7 +25,7 @@ def beta(energy: float, mass=938.2720813):
     b=(1-gamma(energy, mass)**-2)**0.5
     return b   
 
-def beta_c(energy, mass=938.2720813):
+def beta_c(energy, mass=proton_mass_in_MeV):
     '''
     Parameters
     ----------
@@ -31,7 +38,7 @@ def beta_c(energy, mass=938.2720813):
     -------
     relaivistic c*beta of the particle
     '''    
-    bc=c()*(1-gamma(energy, mass)**-2)**0.5
+    bc=c*(1-gamma(energy, mass)**-2)**0.5
     return bc   
 
 def beta_from_gamma(gamma):
@@ -56,7 +63,7 @@ def beta_from_gamma(gamma):
         beta=(1-gamma**-2)**0.5
     return beta  
 
-def gamma(energy, mass=938.2720813):
+def gamma(energy, mass=proton_mass_in_MeV):
     '''
     Parameters
     ----------
@@ -91,7 +98,7 @@ def gamma_from_beta(beta):
         gamma = (1-beta**2)**-0.5
     return gamma  
 
-def energy(gamma_beta, mass=938.2720813):
+def energy(gamma_beta, mass=proton_mass_in_MeV):
     '''
     Parameters
     ----------
@@ -120,9 +127,3 @@ def energy(gamma_beta, mass=938.2720813):
         e = mass * (-1 + 1 / (1 - gamma_beta ** 2) ** 0.5)
     return e
 
-def c():
-    '''
-    speed of light, 299792458 m/s
-    '''
-    return 299792458
-
diff --git a/ess/TTF.py b/ess/TTF.py
index 862c2fa..26e0a01 100644
--- a/ess/TTF.py
+++ b/ess/TTF.py
@@ -59,7 +59,7 @@ def Field_TTF(field_1d, step_dz, freq, E_in, mass, RetMaxPhase):
                 #print(dE)
                 if dE<0:
                     print('Please adjust the field level, particle decelrated to zero energy!')
-                t += dz/(myrel.c()*myrel.beta(dE, mass))
+                t += dz/(myrel.c*myrel.beta(dE, mass))
             if dE > Emax:
                 Emax = dE
                 #print('Emax', Emax)
@@ -112,20 +112,3 @@ def TTF_beta(field_1d, step_dz, freq, mass):
     return TTFb
     
     
-# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
-#                                                                                     #
-#                                    E X A M P L E S                                  #
-#                                                                                     #
-# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
-
-Header, Field = bccb.read_binary_fieldmap('Data/FM/HB_W_coupler.edz', 3)
-fz = field_on_axis(Field, 3, Header[0], Header[2], Header[5])
-z = np.arange(0,int(Header[0]+1))
-dz = Header[1]/Header[0]
-z_points = dz*z
-
-print('Int_E:', Field_integral(fz, dz), '[TTF, Phase_Max_Energy]: ', Field_TTF(fz, dz, 704.42, 700, 938, True))
-TTFb = TTF_beta(fz, dz, 704.42, 938)
-
-#plot(z_points, fz);
-#plot(TTFb[0,:], 50*TTFb[1,:]);  
\ No newline at end of file
diff --git a/examples/fieldmap/example.py b/examples/fieldmap/example.py
new file mode 100644
index 0000000..8fe860d
--- /dev/null
+++ b/examples/fieldmap/example.py
@@ -0,0 +1,35 @@
+from ess import fieldmap
+
+'''
+Example
+
+The parameters in the following six rows define the path to your fieldmap, no name is needed, this script 
+scans the folder and converts all files, assumes all are binary fieldmaps, to ASCII fieldmaps.
+The dimension of the field is also needed.
+If you need to cut the field, the n_enter and n_exit should be adjusted.
+'''
+mypath = 'Data/FM/'          # where the fieldmaps are stored
+fieldmap_dim = 3
+#fieldmap_dim = 2
+#fieldmap_dim = 1
+n_enter = 0                 #Number of lines to be removed from entrance of fieldmap
+n_exit = 0                  #Number of lines to be removed from exit of fieldmap
+
+onlyfiles = [f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))]
+
+for f in onlyfiles:
+    if f[0]!=".":
+
+        if (n_enter+n_exit>0):
+            Header, Field = fieldmap.cut_fieldmap_length(f, fieldmap_dim, n_enter, n_exit, 'b')
+        else:
+            Header, Field = fieldmap.read_binary_fieldmap(mypath+f, fieldmap_dim)
+
+        save_to_ascii(Header, Field, fieldmap.fieldmap_dim, 'Data/FM/ASCII/', f)
+
+test = False
+#%pylab inline
+if test == True:
+    #field = field_on_axis_from_file('Data/FM/MB_W_coupler.edz', 3, 'b')
+    field = field_on_axis_from_file('Data/FM/ASCII/Spoke_W_coupler.edz', 3, 'a')
+    plot(field[0,:], field[1,:]) 
diff --git a/examples/ttf/example.py b/examples/ttf/example.py
new file mode 100644
index 0000000..32382f9
--- /dev/null
+++ b/examples/ttf/example.py
@@ -0,0 +1,13 @@
+from ess import TTF
+
+Header, Field = TTF.bccb.read_binary_fieldmap('Data/FM/HB_W_coupler.edz', 3)
+fz = TTF.field_on_axis(Field, 3, Header[0], Header[2], Header[5])
+z = np.arange(0,int(Header[0]+1))
+dz = Header[1]/Header[0]
+z_points = dz*z
+
+print('Int_E:', TTF.Field_integral(fz, dz), '[TTF, Phase_Max_Energy]: ', TTF.Field_TTF(fz, dz, 704.42, 700, 938, True))
+TTFb = TTF_beta(fz, dz, 704.42, 938)
+
+#plot(z_points, fz);
+#plot(TTFb[0,:], 50*TTFb[1,:]);  
-- 
GitLab