ubermag / help

Repository for raising issues and requesting help on Ubermag
BSD 2-Clause "Simplified" License
11 stars 3 forks source link

Reading an Image file as the initial magnetization profile #279

Open DebanjanPolley opened 7 months ago

DebanjanPolley commented 7 months ago

Hello, I am trying to simulate a 3-dimensional magnetic disk (dot and or anti-dot type structure) lattice structure using Ubermag. I am mainly interested in stray field distribution. If it is a single magnetic disk, I can easily use:

def particle(point):
    x, y, z = point
    x1, y1, z1 = 0, 0, 0  # particle 1 centre point
    r1 = 100e-9  # particle 1 radius

    if (x - x1)**2 + (y - y1)**2 <= r1**2:
        return Ms  # inside particle 1  
    #elif (x - x2)**2 + (y - y2)**2 <= r2**2:
        #return Ms  # inside particle 2 
    else:
        return 0

region = df.Region(p1=(-100e-9, -100e-9, 0), p2=(100e-9, 100e-9, 4e-9))
mesh = df.Mesh(region=region, cell=(4e-9, 4e-9, 2e-9))
system = mm.System(name='Test')
system.energy = mm.Exchange(A=A) + mm.Zeeman(H=H) + mm.UniaxialAnisotropy(K=K1, u=(0,0,1)) + mm.Demag() #+ mm.DMI(D=2.0e-3, crystalclass='Cnv_z')
system.m = df.Field(mesh, nvdim=3, value=(0.02, 0.0, 0.98), norm=particle)

However, this process gets cumbersome if I have multiple such disks or if I try to simulate a real SEM image of such an artificial lattice structure. Is it possible to write a code in such a way that Ubermag takes the initial magnetization configuration after reading an image file (.png,. jpeg or .bmp)?

I have attached a sample image here. The black/white part should be read as 'zero/Ms' magnetization, where Ms is the predefined magnetization value.

Thanks, Debanjan

Dot 1

lang-m commented 7 months ago

Hi @DebanjanPolley , Ubermag (i.e. discretisedfield) does not have any functionality to directly create an initial state from an image. Two suggestions:

  1. To simplify the particle function for multiple particles you could probably introduce a for-loop over all (x0, y0, r) tuples to avoid some code duplication.
  2. When the configuration becomes more complex and you need the image you could use one of the image libaries available for Python (e.g. pillow) to read the image and convert it into a numpy array, which you can subsequently use (probably after a few small transformations such as adjusting the shape etc) to initialise value/norm as needed.
DebanjanPolley commented 3 months ago

Hello, I tried a solution as provided in some other thread. I have attached a simplified image and the corresponding data file (including the position and magnetization vector ). However, I fail to achieve the required spin configuration using a similar code. The idea is that the white portion of the image should have a large magnetization, and the black portion should have zero magnetization.

Screenshot 1

example.txt

My code looks like this:

import numpy as np
import discretisedfield as df
import micromagneticmodel as mm
import oommfc as oc
import matplotlib.pyplot as plt
from PIL import Image

system = mm.System(name='testpos1')

Data   = np.genfromtxt("example.txt", delimiter='\t', skip_header = 2, skip_footer=1) 

coords = Data[:, :3]
mag    = Data[:, 3:]

cell   = (5, 5, 1)
p1     = (0,0,0)                     # np.min(coords, axis=0) - np.divide(cell, 2)
p2     = (300, 270, 1)               # np.max(coords, axis=0) + np.divide(cell, 2)

mesh   = df.Mesh(p1=p1, p2=p2, cell=cell)

def val_fun(pos):
    arr = np.all(np.isclose(coords, pos), axis=1)
    if np.any(arr):
        return mag[arr].squeeze()
    else:
        return (0, 0, 0)

system.m = df.Field(mesh=mesh, nvdim=3, value=val_fun)
mesh.mpl()
system.m.sel('z').mpl()