Newer
Older
"""Class for creating objects to store magnetizatin data."""
import matplotlib.pyplot as plt
'''An object storing magnetization data.'''
def __init__(self, res, magnitude): # TODO: electrostatic component!
'''Constructor for a MagData object for storing magnetization data.
res - the resolution of the grid (grid spacing) in nm
magnitude - the z-, y- and x-component of the magnetization vector for every
3D-gridpoint as a tuple
'''
dim = np.shape(magnitude[0])
assert len(dim) == 3, 'Magnitude has to be defined for a 3-dimensional grid!'
assert np.shape(magnitude[1]) == np.shape(magnitude[2]) == dim, \
'Dimensions of the magnitude components do not match!'
self.res = res
self.dim = dim
self.magnitude = magnitude
@classmethod
def load_from_llg(cls, filename):
'''Construct DataMag object from LLG-file (classmethod).
Arguments:
filename - the name of the LLG-file from which to load the data
Returns.
MagData object
'''
scale = 1.0E-9 / 1.0E-2 # From cm to nm
data = np.genfromtxt(filename, skip_header=2)
x_dim, y_dim, z_dim = np.genfromtxt(filename, dtype=int, skip_header=1,
skip_footer=len(data[:, 0]))
res = (data[1, 0] - data[0, 0]) / scale
# Reshape in Python and Igor is different, Python fills rows first, Igor columns:
x_mag, y_mag, z_mag = [data[:, i].reshape(z_dim, y_dim, x_dim)
for i in range(3,6)]
def save_to_llg(self, filename='magdata_output.txt'):
'''Save magnetization data in a file with LLG-format.
filename - the name of the LLG-file in which to store the magnetization data
(default: 'magdata_output.txt')
dim = self.dim
res = self.res * 1.0E-9 / 1.0E-2 # from nm to cm
# Create 3D meshgrid and reshape it and the magnetization into a list where x varies first:
zz, yy, xx = np.mgrid[res/2 : (dim[0]*res-res/2) : dim[0]*1j,
res/2 : (dim[1]*res-res/2) : dim[1]*1j,
res/2 : (dim[2]*res-res/2) : dim[2]*1j]
xx = np.reshape(xx,(-1))
yy = np.reshape(yy,(-1))
zz = np.reshape(zz,(-1))
x_mag = np.reshape(self.magnitude[2], (-1))
y_mag = np.reshape(self.magnitude[1], (-1))
z_mag = np.reshape(self.magnitude[0], (-1))
data = np.array([xx, yy, zz, x_mag, y_mag, z_mag]).T
with open(filename,'w') as mag_file:
mag_file.write('LLGFileCreator: %s\n' % filename.replace('.txt', ''))
mag_file.write(' %d %d %d\n' % (dim[2], dim[1], dim[0]))
mag_file.writelines('\n'.join(' '.join('{:7.6e}'.format(cell)
for cell in row) for row in data) )
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
@classmethod
def load_from_netcdf(cls, filename):
'''Construct MagData object from a NetCDF-file (classmethod).
Arguments:
filename - name of the file from which to load the data
Returns:
PhaseMap object
'''
f = nc.NetCDFFile(filename, 'r')
res = getattr(f, 'res')
z_mag = f.variables['z_mag'].getValue()
y_mag = f.variables['y_mag'].getValue()
x_mag = f.variables['x_mag'].getValue()
f.close()
return MagData(res, (z_mag, y_mag, x_mag))
def save_to_netcdf(self, filename='..\output\magdata_output.nc'):
'''Save magnetization data in a file with NetCDF-format.
Arguments:
filename - the name of the file in which to store the phase map data
(default: 'phasemap_output.txt')
Returns:
None
'''
f = nc.NetCDFFile(filename, 'w')
setattr(f, 'res', self.res)
f.createDimension('z_dim', self.dim[0])
f.createDimension('y_dim', self.dim[1])
f.createDimension('x_dim', self.dim[2])
z_mag = f.createVariable('z_mag', 'f', ('z_dim', 'y_dim', 'x_dim'))
y_mag = f.createVariable('y_mag', 'f', ('z_dim', 'y_dim', 'x_dim'))
x_mag = f.createVariable('x_mag', 'f', ('z_dim', 'y_dim', 'x_dim'))
z_mag[:] = self.magnitude[0]
y_mag[:] = self.magnitude[1]
x_mag[:] = self.magnitude[2]
print f
f.close()
def quiver_plot(self, axis='z', ax_slice=0):
'''Plot a slice of the magnetization as a quiver plot.
Arguments:
axis - the axis from which a slice is plotted ('x', 'y' or 'z'), default = 'z'
ax_slice - the slice-index of the specified axis
Returns:
None
'''
assert axis == 'z' or axis == 'y' or axis == 'x', 'Axis has to be x, y or z (as string).'
if axis == 'z': # Slice of the xy-plane with z = ax_slice
mag_slice_1 = self.magnitude[2][ax_slice, ...]
mag_slice_2 = self.magnitude[1][ax_slice, ...]
elif axis == 'y': # Slice of the xz-plane with y = ax_slice
mag_slice_1 = self.magnitude[2][:, ax_slice, :]
mag_slice_2 = self.magnitude[0][:, ax_slice, :]
elif axis == 'x': # Slice of the yz-plane with x = ax_slice
mag_slice_1 = self.magnitude[1][..., ax_slice]
mag_slice_2 = self.magnitude[0][..., ax_slice]
# Plot the magnetization vectors:
fig = plt.figure()
fig.add_subplot(111, aspect='equal')
plt.quiver(mag_slice_1, mag_slice_2, pivot='middle', angles='xy', scale_units='xy',
scale=1, headwidth=6, headlength=7)
def quiver_plot3d(self): # XXX: Still buggy, use only for small distributions!
'''3D-Quiver-Plot of the magnetization as vectors.
Arguments:
None
Returns:
None
# Create points and vector components as lists:
zz, yy, xx = np.mgrid[res/2 : (dim[0]*res-res/2) : dim[0]*1j,
res/2 : (dim[1]*res-res/2) : dim[1]*1j,
res/2 : (dim[2]*res-res/2) : dim[2]*1j]
xx = np.reshape(xx, (-1))
yy = np.reshape(yy, (-1))
zz = np.reshape(zz, (-1))
x_mag = np.reshape(self.magnitude[2], (-1))
y_mag = np.reshape(self.magnitude[1], (-1))
z_mag = np.reshape(self.magnitude[0], (-1))