Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading an Image file as the initial magnetization profile #279

Open
DebanjanPolley opened this issue Apr 20, 2024 · 2 comments
Open

Reading an Image file as the initial magnetization profile #279

DebanjanPolley opened this issue Apr 20, 2024 · 2 comments
Labels
question General question

Comments

@DebanjanPolley
Copy link

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 lang-m added the question General question label Apr 22, 2024
@lang-m
Copy link
Member

lang-m commented Apr 22, 2024

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.

@lang-m lang-m closed this as completed Apr 22, 2024
@DebanjanPolley
Copy link
Author

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()

@lang-m lang-m reopened this Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question General question
Projects
None yet
Development

No branches or pull requests

2 participants