From 3a22513186ad90f4a3da114fe4b7141c2add0f95 Mon Sep 17 00:00:00 2001
From: Jan Caron <j.caron@fz-juelich.de>
Date: Wed, 12 Jun 2013 07:25:14 +0200
Subject: [PATCH] Implementation of second angle for the creation of magnetic
 distributions magcreator: Added the second angle for the magnetization
 reconstructor: Changed method name all others: Changed "beta" to "phi" to
 reflect the new angle in magcreator

---
 pyramid/magcreator.py                   | 33 ++++++++++++++++---------
 pyramid/reconstructor.py                |  3 ++-
 scripts/compare_method_errors.py        | 24 +++++++++---------
 scripts/compare_methods.py              | 14 +++++------
 scripts/compare_pixel_fields.py         |  8 +++---
 scripts/create_alternating_filaments.py |  8 +++---
 scripts/create_logo.py                  |  4 +--
 scripts/create_random_pixels.py         |  6 ++---
 scripts/create_random_slabs.py          |  6 ++---
 scripts/create_sample.py                |  4 +--
 scripts/get_jacobi.py                   |  4 +--
 scripts/gui_create_logo.py              |  4 +--
 scripts/reconstruct_random_pixels.py    |  8 +++---
 13 files changed, 68 insertions(+), 58 deletions(-)

diff --git a/pyramid/magcreator.py b/pyramid/magcreator.py
index 1a513a0..658e2ce 100644
--- a/pyramid/magcreator.py
+++ b/pyramid/magcreator.py
@@ -3,6 +3,7 @@
 
 
 import numpy as np
+from numpy import pi
 
 
 class Shapes:
@@ -158,11 +159,13 @@ class Shapes:
         return mag_shape
 
 
-def create_mag_dist(mag_shape, beta, magnitude=1):
+def create_mag_dist(mag_shape, phi, theta=pi/2, magnitude=1):
     '''Create a 3-dimensional magnetic distribution of a homogeneous magnetized object.
     Arguments:
         mag_shape - the magnetic shapes (numpy arrays, see Shapes.* for examples)
-        beta      - the angle, describing the direction of the magnetized object
+        phi       - the azimuthal angle, describing the direction of the magnetized object
+        theta     - the polar angle, describing the direction of the magnetized object
+                    (optional, is set to pi/2 if not specified -> z-component is zero)
         magnitude - the relative magnitudes for the magnetic shape (optional, one if not specified)
     Returns:
         the 3D magnetic distribution as a MagData object (see pyramid.magdata for reference)
@@ -170,17 +173,20 @@ def create_mag_dist(mag_shape, beta, magnitude=1):
     '''
     dim = np.shape(mag_shape)
     assert len(dim) == 3, 'Magnetic shapes must describe 3-dimensional distributions!'
-    z_mag = np.zeros(dim)  # TODO: Implement another angle!
-    y_mag = np.ones(dim) * np.sin(beta) * mag_shape * magnitude
-    x_mag = np.ones(dim) * np.cos(beta) * mag_shape * magnitude
+    z_mag = np.ones(dim) * np.cos(theta) * mag_shape * magnitude
+    y_mag = np.ones(dim) * np.sin(theta) * np.sin(phi) * mag_shape * magnitude
+    x_mag = np.ones(dim) * np.sin(theta) * np.cos(phi) * mag_shape * magnitude
     return z_mag, y_mag, x_mag
 
 
-def create_mag_dist_comb(mag_shape_list, beta_list, magnitude_list=None):
+def create_mag_dist_comb(mag_shape_list, phi_list, theta_list=None, magnitude_list=None):
     '''Create a 3-dimensional magnetic distribution from a list of homogeneous magnetized objects.
     Arguments:
         mag_shape_list - a list of magnetic shapes (numpy arrays, see Shapes.* for examples)
-        beta_list      - a list of angles, describing the direction of the magnetized objects
+        phi_list       - a list of azimuthal angles, describing the direction of the
+                         magnetized object
+        theta_list     - a list of polar angles, describing the direction of the magnetized object
+                         (optional, is set to pi/2 if not specified -> z-component is zero)
         magnitude_list - a list of relative magnitudes for the magnetic shapes
                          (optional, if not specified, every relative magnitude is set to one)
     Returns:
@@ -189,21 +195,24 @@ def create_mag_dist_comb(mag_shape_list, beta_list, magnitude_list=None):
     '''
     # If no relative magnitude is specified, 1 is assumed for every homog. object:
     if magnitude_list is None:
-        magnitude_list = np.ones(len(beta_list))
+        magnitude_list = np.ones(len(phi_list))
+    # If no relative magnitude is specified, 1 is assumed for every homog. object:
+    if theta_list is None:
+        theta_list = np.ones(len(phi_list)) * pi/2
     # For every shape of a homogeneous object a relative magnitude and angle have to be set:
-    assert np.shape(mag_shape_list)[0] == len(beta_list) == len(magnitude_list), \
+    assert np.shape(mag_shape_list)[0] == len(phi_list) == len(theta_list) == len(magnitude_list),\
         'Lists have not the same length!'
     dim = np.shape(mag_shape_list[0])  # Has to be the shape of ALL mag_shapes!
     assert len(dim) == 3, 'Magnetic shapes must describe 3-dimensional distributions!'
-    assert np.array([mag_shape_list[i].shape == dim for i in range(len(mag_shape_list))]).all(), \
+    assert np.array([mag_shape_list[i].shape == dim for i in range(len(mag_shape_list))]).all(),\
         'Magnetic shapes must describe distributions with the same size!'
     # Start with a zero distribution:
     x_mag = np.zeros(dim)
     y_mag = np.zeros(dim)
     z_mag = np.zeros(dim)
     # Add every specified homogeneous object:
-    for i in range(np.size(beta_list)):
-        mag_object = create_mag_dist(mag_shape_list[i], beta_list[i], magnitude_list[i])
+    for i in range(np.size(phi_list)):
+        mag_object = create_mag_dist(mag_shape_list[i], phi_list[i], magnitude_list[i])
         z_mag += mag_object[0]
         y_mag += mag_object[1]
         x_mag += mag_object[2]
diff --git a/pyramid/reconstructor.py b/pyramid/reconstructor.py
index 105804f..b16da97 100644
--- a/pyramid/reconstructor.py
+++ b/pyramid/reconstructor.py
@@ -9,7 +9,7 @@ from pyramid.magdata import MagData
 from scipy.optimize import leastsq
 
 
-def reconstruct_simple_lsqu(phase_map, mask, b_0):
+def reconstruct_simple_leastsq(phase_map, mask, b_0):
     '''Reconstruct a magnetic distribution where the positions of the magnetized voxels are known
     from a single phase_map using the least square method (only works for slice thickness = 1)
     Arguments:
@@ -41,6 +41,7 @@ def reconstruct_simple_lsqu(phase_map, mask, b_0):
         term1 = (y_i - y_m)
         term2 = lam * x_i
         return np.concatenate([term1, term2])
+
     # Reconstruct the magnetization components:
     x_rec, _ = leastsq(J, np.zeros(3*count))
     mag_data_rec.set_vector(mask, x_rec)
diff --git a/scripts/compare_method_errors.py b/scripts/compare_method_errors.py
index 6df4859..ce967cd 100644
--- a/scripts/compare_method_errors.py
+++ b/scripts/compare_method_errors.py
@@ -34,7 +34,7 @@ def phase_from_mag():
     b_0 =  1    # in T
     res = 10.0  # in nm
     dim = (1, 128, 128)    
-    beta = -pi/4
+    phi = -pi/4
     padding_list = [0, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4,5, 6,7, 8,9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
     geometry = 'disc'
     # Create magnetic shape:
@@ -42,15 +42,15 @@ def phase_from_mag():
         center = (0, dim[1]/2-0.5, dim[2]/2-0.5)  # in px (z, y, x) index starts with 0!
         width  = (1, dim[1]/2, dim[2]/2)  # in px (z, y, x)
         mag_shape = mc.Shapes.slab(dim, center, width)
-        phase_ana = an.phase_mag_slab(dim, res, beta, center, width, b_0)
+        phase_ana = an.phase_mag_slab(dim, res, phi, center, width, b_0)
     elif geometry == 'disc':
         center = (0, dim[1]/2-0.5, dim[2]/2-0.5)  # in px (z, y, x) index starts with 0!
         radius = dim[1]/4  # in px 
         height =  1  # in px
         mag_shape = mc.Shapes.disc(dim, center, radius, height)
-        phase_ana = an.phase_mag_disc(dim, res, beta, center, radius, b_0)
+        phase_ana = an.phase_mag_disc(dim, res, phi, center, radius, b_0)
     # Project the magnetization data:    
-    mag_data = MagData(res, mc.create_mag_dist(mag_shape, beta))    
+    mag_data = MagData(res, mc.create_mag_dist(mag_shape, phi))    
     projection = pj.simple_axis_projection(mag_data)
     # Create data:
     data = np.zeros((3, len(padding_list)))
@@ -60,7 +60,7 @@ def phase_from_mag():
         # Key:
         key = ', '.join(['Padding->RMS|duration', 'Fourier', 'padding={}'.format(padding_list[i]), 
                         'B0={}'.format(b_0), 'res={}'.format(res), 'dim={}'.format(dim),
-                        'beta={}'.format(beta), 'geometry={}'.format(geometry)])
+                        'phi={}'.format(phi), 'geometry={}'.format(geometry)])
         if data_shelve.has_key(key):
             data[:, i] = data_shelve[key]
         else:
@@ -107,21 +107,21 @@ def phase_from_mag():
 #    plt.show()
 
 #    '''REAL SLAB '''
-#    beta = np.linspace(0, 2*pi, endpoint=False, num=72)
+#    phi = np.linspace(0, 2*pi, endpoint=False, num=72)
 #    
-#    RMS = np.zeros(len(beta))
-#    for i in range(len(beta)):
-#        print 'beta =', round(360*beta[i]/(2*pi))
-#        mag_data = MagData(res, mc.create_mag_dist(mag_shape, beta[i]))
+#    RMS = np.zeros(len(phi))
+#    for i in range(len(phi)):
+#        print 'phi =', round(360*phi[i]/(2*pi))
+#        mag_data = MagData(res, mc.create_mag_dist(mag_shape, phi[i]))
 #        projection = pj.simple_axis_projection(mag_data)
 #        phase_num  = pm.phase_mag_real(res, projection, 'slab', b_0)
-#        phase_ana  = an.phase_mag_slab(dim, res, beta[i], center, width, b_0)
+#        phase_ana  = an.phase_mag_slab(dim, res, phi[i], center, width, b_0)
 #        phase_diff = phase_ana - phase_num
 #        RMS[i]  = np.std(phase_diff)
 #    
 #    fig = plt.figure()
 #    fig.add_subplot(111)    
-#    plt.plot(np.round(360*beta/(2*pi)), RMS)
+#    plt.plot(np.round(360*phi/(2*pi)), RMS)
     
       
 #    phase_map_slab = PhaseMap(res, pm.phase_mag_real(res, projection, 'slab', b_0))
diff --git a/scripts/compare_methods.py b/scripts/compare_methods.py
index fc2faf8..27262a4 100644
--- a/scripts/compare_methods.py
+++ b/scripts/compare_methods.py
@@ -25,9 +25,9 @@ def compare_methods():
     
     '''
     # Input parameters:
-    b_0     = 1    # in T
-    res     = 10.0  # in nm
-    beta    = pi/4
+    b_0 = 1    # in T
+    res = 10.0  # in nm
+    phi = pi/4
     padding = 20
     density = 10
     dim = (1, 128, 128)  # in px (z, y, x)    
@@ -37,20 +37,20 @@ def compare_methods():
         center = (0, dim[1]/2.-0.5, dim[2]/2.-0.5)  # in px (z, y, x) index starts with 0!
         width  = (1, dim[1]/2., dim[2]/2.)  # in px (z, y, x)
         mag_shape = mc.Shapes.slab(dim, center, width)
-        phase_ana = an.phase_mag_slab(dim, res, beta, center, width, b_0)
+        phase_ana = an.phase_mag_slab(dim, res, phi, center, width, b_0)
     elif geometry == 'disc':
         center = (0, dim[1]/2.-0.5, dim[2]/2.-0.5)  # in px (z, y, x) index starts with 0!
         radius = dim[1]/4  # in px 
         height = 1  # in px
         mag_shape = mc.Shapes.disc(dim, center, radius, height)
-        phase_ana = an.phase_mag_disc(dim, res, beta, center, radius, height, b_0)
+        phase_ana = an.phase_mag_disc(dim, res, phi, center, radius, height, b_0)
     elif geometry == 'sphere':
         center = (50, 50, 50)  # in px (z, y, x) index starts with 0!
         radius = 25  # in px 
         mag_shape = mc.Shapes.sphere(dim, center, radius)
-        phase_ana = an.phase_mag_sphere(dim, res, beta, center, radius, b_0)
+        phase_ana = an.phase_mag_sphere(dim, res, phi, center, radius, b_0)
     # Project the magnetization data:    
-    mag_data = MagData(res, mc.create_mag_dist(mag_shape, beta))
+    mag_data = MagData(res, mc.create_mag_dist(mag_shape, phi))
     mag_data.quiver_plot(ax_slice=int(center[0]))
     projection = pj.simple_axis_projection(mag_data)
     # Construct phase maps:
diff --git a/scripts/compare_pixel_fields.py b/scripts/compare_pixel_fields.py
index 2ad1698..1217595 100644
--- a/scripts/compare_pixel_fields.py
+++ b/scripts/compare_pixel_fields.py
@@ -21,12 +21,12 @@ def compare_pixel_fields():
     
     '''
     # Input parameters:    
-    res   = 10.0  # in nm
-    beta  = pi/2  # in rad
-    dim   = (1, 11, 11)
+    res = 10.0  # in nm
+    phi = pi/2  # in rad
+    dim = (1, 11, 11)
     pixel = (0,  5,  5) 
     # Create magnetic data, project it, get the phase map and display the holography image:    
-    mag_data   = MagData(res, mc.create_mag_dist(mc.Shapes.pixel(dim, pixel), beta)) 
+    mag_data   = MagData(res, mc.create_mag_dist(mc.Shapes.pixel(dim, pixel), phi)) 
     projection = pj.simple_axis_projection(mag_data)
     phase_map_slab = PhaseMap(res, pm.phase_mag_real(res, projection, 'slab'))    
     phase_map_slab.display('Phase of one Pixel (Slab)')
diff --git a/scripts/create_alternating_filaments.py b/scripts/create_alternating_filaments.py
index 2724d6f..684f58e 100644
--- a/scripts/create_alternating_filaments.py
+++ b/scripts/create_alternating_filaments.py
@@ -22,18 +22,18 @@ def create_alternating_filaments():
     filename = '../output/mag_dist_alt_filaments.txt'    
     dim = (1, 21, 21)  # in px (z, y, x)
     res = 10.0  # in nm
-    beta = pi/2       
+    phi = pi/2       
     spacing = 5    
     # Create lists for magnetic objects:
     count = int((dim[1]-1) / spacing) + 1
     mag_shape_list = np.zeros((count,) + dim)
-    beta_list      = np.zeros(count)
+    phi_list      = np.zeros(count)
     for i in range(count):  
         pos = i * spacing
         mag_shape_list[i] = mc.Shapes.filament(dim, (0, pos))
-        beta_list[i] = (1-2*(int(pos/spacing)%2)) * beta
+        phi_list[i] = (1-2*(int(pos/spacing)%2)) * phi
     # Create magnetic distribution
-    magnitude = mc.create_mag_dist_comb(mag_shape_list, beta_list) 
+    magnitude = mc.create_mag_dist_comb(mag_shape_list, phi_list) 
     mag_data = MagData(res, magnitude)
     mag_data.quiver_plot()
     mag_data.save_to_llg(filename)    
diff --git a/scripts/create_logo.py b/scripts/create_logo.py
index 4b4847b..070a48d 100644
--- a/scripts/create_logo.py
+++ b/scripts/create_logo.py
@@ -24,7 +24,7 @@ def create_logo():
     '''
     # Input parameters:    
     res = 10.0  # in nm
-    beta = pi/2  # in rad
+    phi = -pi/2  # in rad
     density = 10
     dim = (1, 128, 128)
     # Create magnetic shape:
@@ -37,7 +37,7 @@ def create_logo():
     right  = np.fliplr(left)
     mag_shape[0,...] = np.logical_and(np.logical_and(left, right), bottom)
     # Create magnetic data, project it, get the phase map and display the holography image:    
-    mag_data   = MagData(res, mc.create_mag_dist(mag_shape, beta)) 
+    mag_data   = MagData(res, mc.create_mag_dist(mag_shape, phi)) 
     projection = pj.simple_axis_projection(mag_data)
     phase_map  = PhaseMap(res, pm.phase_mag_real(res, projection, 'slab'))    
     hi.display(hi.holo_image(phase_map, density), 'PYRAMID - LOGO')
diff --git a/scripts/create_random_pixels.py b/scripts/create_random_pixels.py
index 5d826dd..8a9224d 100644
--- a/scripts/create_random_pixels.py
+++ b/scripts/create_random_pixels.py
@@ -29,15 +29,15 @@ def create_random_pixels():
     rnd.seed(12)
     # Create lists for magnetic objects:
     mag_shape_list = np.zeros((count,) + dim)
-    beta_list      = np.zeros(count) 
+    phi_list = np.zeros(count) 
     magnitude_list = np.zeros(count)
     for i in range(count):
         pixel = (rnd.randrange(dim[0]), rnd.randrange(dim[1]), rnd.randrange(dim[2]))
         mag_shape_list[i,...] = mc.Shapes.pixel(dim, pixel)
-        beta_list[i] = 2*pi*rnd.random()
+        phi_list[i] = 2*pi*rnd.random()
         magnitude_list[i] = 1#rnd.random()
     # Create magnetic distribution:
-    magnitude = mc.create_mag_dist_comb(mag_shape_list, beta_list, magnitude_list) 
+    magnitude = mc.create_mag_dist_comb(mag_shape_list, phi_list, magnitude_list) 
     mag_data = MagData(res, magnitude)
     mag_data.quiver_plot()
     mag_data.save_to_llg('../output/mag_dist_random_pixels.txt')
diff --git a/scripts/create_random_slabs.py b/scripts/create_random_slabs.py
index 4e885e7..d36e49e 100644
--- a/scripts/create_random_slabs.py
+++ b/scripts/create_random_slabs.py
@@ -30,7 +30,7 @@ def create_random_slabs():
     w_max = 10
     # Create lists for magnetic objects:
     mag_shape_list = np.zeros((count,) + dim)
-    beta_list      = np.zeros(count) 
+    phi_list = np.zeros(count) 
     magnitude_list = np.zeros(count)
     for i in range(count):
         width  = (1, rnd.randint(1, w_max), rnd.randint(1, w_max))
@@ -38,10 +38,10 @@ def create_random_slabs():
                   rnd.randrange(int(width[1]/2), dim[1]-int(width[1]/2)),
                   rnd.randrange(int(width[2]/2), dim[2]-int(width[2]/2)))
         mag_shape_list[i,...] = mc.Shapes.slab(dim, center, width)
-        beta_list[i] = 2*pi*rnd.random()
+        phi_list[i] = 2*pi*rnd.random()
         magnitude_list[i] = 1#rnd.random()
     # Create magnetic distribution:
-    magnitude = mc.create_mag_dist_comb(mag_shape_list, beta_list, magnitude_list) 
+    magnitude = mc.create_mag_dist_comb(mag_shape_list, phi_list, magnitude_list) 
     mag_data = MagData(res, magnitude)
     mag_data.quiver_plot()
     mag_data.save_to_llg('../output/mag_dist_random_slabs.txt')
diff --git a/scripts/create_sample.py b/scripts/create_sample.py
index f4d203b..5a567c0 100644
--- a/scripts/create_sample.py
+++ b/scripts/create_sample.py
@@ -22,7 +22,7 @@ def create_sample():
     filename = '../output/mag_dist_' + key + '.txt'    
     dim = (1, 128, 128)  # in px (z, y, x)
     res = 10.0  # in nm
-    beta = pi/4   
+    phi = pi/4   
     # Geometry parameters:    
     center = (0, 64, 64)  # in px (z, y, x), index starts with 0!
     width  = (1, 50, 50)  # in px (z, y, x)
@@ -42,7 +42,7 @@ def create_sample():
     elif key == 'pixel':
         mag_shape = mc.Shapes.pixel(dim, pixel)
     # Create magnetic distribution
-    magnitude = mc.create_mag_dist(mag_shape, beta) 
+    magnitude = mc.create_mag_dist(mag_shape, phi) 
     mag_data = MagData(res, magnitude)
     mag_data.quiver_plot()
     mag_data.save_to_llg(filename)
diff --git a/scripts/get_jacobi.py b/scripts/get_jacobi.py
index 8df0613..66ece51 100644
--- a/scripts/get_jacobi.py
+++ b/scripts/get_jacobi.py
@@ -28,12 +28,12 @@ def get_jacobi():
     b_0 = 1.0  # in T    
     dim = (1, 3, 3)  # in px (y,x)
     res = 10.0  # in nm
-    beta = pi/4    
+    phi = pi/4    
     
     center = (0, 1, 1)  # in px (y,x) index starts with 0!
     width  = (0, 1, 1)  # in px (y,x)
 
-    mag_data = MagData(res, mc.create_mag_dist(mc.Shapes.slab(dim, center, width), beta))
+    mag_data = MagData(res, mc.create_mag_dist(mc.Shapes.slab(dim, center, width), phi))
     projection = pj.simple_axis_projection(mag_data)
     
     '''NUMERICAL SOLUTION'''
diff --git a/scripts/gui_create_logo.py b/scripts/gui_create_logo.py
index 917c337..4e684ae 100644
--- a/scripts/gui_create_logo.py
+++ b/scripts/gui_create_logo.py
@@ -109,7 +109,7 @@ def create_logo(dim, axis):
     '''
     # Input parameters:    
     res = 10.0  # in nm
-    beta = pi/2  # in rad
+    phi = -pi/2  # in rad
     density = 10
     # Create magnetic shape:
     mag_shape = np.zeros(dim)
@@ -121,7 +121,7 @@ def create_logo(dim, axis):
     right  = np.fliplr(left)
     mag_shape[0,...] = np.logical_and(np.logical_and(left, right), bottom)
     # Create magnetic data, project it, get the phase map and display the holography image:    
-    mag_data   = MagData(res, mc.create_mag_dist(mag_shape, beta)) 
+    mag_data   = MagData(res, mc.create_mag_dist(mag_shape, phi)) 
     projection = pj.simple_axis_projection(mag_data)
     phase_map  = PhaseMap(res, pm.phase_mag_real(res, projection, 'slab'))    
     hi.display(hi.holo_image(phase_map, density), 'PYRAMID - LOGO', axis)
diff --git a/scripts/reconstruct_random_pixels.py b/scripts/reconstruct_random_pixels.py
index 15a20d4..a705273 100644
--- a/scripts/reconstruct_random_pixels.py
+++ b/scripts/reconstruct_random_pixels.py
@@ -33,15 +33,15 @@ def reconstruct_random_distribution():
 
     # Create lists for magnetic objects:
     mag_shape_list = np.zeros((n_pixel,) + dim)
-    beta_list      = np.zeros(n_pixel)
+    phi_list      = np.zeros(n_pixel)
     magnitude_list = np.zeros(n_pixel)
     for i in range(n_pixel):
         pixel = (rnd.randrange(dim[0]), rnd.randrange(dim[1]), rnd.randrange(dim[2]))
         mag_shape_list[i,...] = mc.Shapes.pixel(dim, pixel)
-        beta_list[i] = 2*pi*rnd.random()
+        phi_list[i] = 2*pi*rnd.random()
         magnitude_list[i] = rnd.random()
     # Create magnetic distribution:
-    magnitude = mc.create_mag_dist_comb(mag_shape_list, beta_list, magnitude_list)
+    magnitude = mc.create_mag_dist_comb(mag_shape_list, phi_list, magnitude_list)
     mag_data = MagData(res, magnitude)
     # Display phase map and holography image:
     projection = pj.simple_axis_projection(mag_data)
@@ -55,7 +55,7 @@ def reconstruct_random_distribution():
     mask = np.logical_or(np.logical_or(x_mask, y_mask), z_mask)
     
     # Reconstruct the magnetic distribution:
-    mag_data_rec = rc.reconstruct_simple_lsqu(phase_map, mask, b_0)
+    mag_data_rec = rc.reconstruct_simple_leastsq(phase_map, mask, b_0)
 
     # Display the reconstructed phase map and holography image:
     projection_rec = pj.simple_axis_projection(mag_data_rec)
-- 
GitLab