Skip to content

Commit

Permalink
Included tests
Browse files Browse the repository at this point in the history
  • Loading branch information
firestarx35 committed Aug 20, 2023
1 parent 5b4331d commit c9d661d
Show file tree
Hide file tree
Showing 20 changed files with 399 additions and 167 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
# McPy - A Micromagnetic Monte Carlo Simulation package

McPy is a package developed for monte carlo simulations in micromagnetics to study magnetic pseudoparticles. This has been developed as part of my Master's thesis "Parallelisation and Optimisation of Monte Carlo Simulation in Nanomagnetism" to develop an extension of Ubermag(https://ubermag.github.io/index.html), a micromagnetic simulations package. Ubermag is a collection of several independent Python packages that can be used independently as well as in combination to be used for other physics simulations such as Fluid Dynamics.
McPy is a package developed for monte carlo simulations in micromagnetics to study magnetic pseudoparticles. This has been developed as part of my Master's thesis "Parallelisation and Optimisation of Monte Carlo Simulation in Nanomagnetism" to develop an extension of [Ubermag](https://ubermag.github.io/index.html), a micromagnetic simulations package. Ubermag is a collection of several independent Python packages that can be used independently as well as in combination to be used for other physics simulations such as Fluid Dynamics.

Ubermag already contains well maintained energy minimisation solvers but the goal of this package is to extend the capabilities of Ubermag by adding a non-pertubative approach to energy minimisation that has not been implemented till now.

#### Why Monte Carlo in micromagnetics?

Here are several papers validating the effectiveness of monte carlo in this field
-
-
-
- [Simulating anti-skyrmions on a lattice](https://www.nature.com/articles/s41598-022-22043-0)
- [The skyrmion lattice phase in three dimensional chiral magnets from Monte Carlo simulations](https://arxiv.org/abs/1304.6580)


## Installation:
- ### Requirements

### Requirements
- Python==3.10.11
- Ubermag with default oommfc driver : Follow the installation guide at (https://ubermag.github.io/installation.html)
- Ubermag with default oommfc driver : Follow the installation guide at [Ubermag Installation](https://ubermag.github.io/installation.html)
- NumPy==1.24.3
- Numba==0.57.0
- cupy-cuda12x==12.1.0

Installation of abovepackages can be achieved by running the below command
Installation of above packages can be achieved by running the following command

```bash

```
conda env create -f environment.yml

```
Expand All @@ -30,8 +32,10 @@ conda env create -f environment.yml
## Tests:
To run the automated tests

```
python -m unittest montecarlo/test
```bash

python -m unittest tests.py

```

## Guide:
Expand All @@ -53,7 +57,7 @@ python -m unittest montecarlo/test
- `bloch_point.ipynb`:
- `Instructios.ipynb`:

![Project Structure](images/project%20structure.png)
<!-- ![Project Structure](images/project%20structure.png) -->

## Documentation
You can find the documentation at `docs/html/index.html`.
Expand Down
3 changes: 3 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Disc exchang energy in numpy only. And correct it in enery_validation.ipynb
Testing new spin.\
Check if host PC has Numba installed if not then revert back to numpy
5 changes: 2 additions & 3 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ channels:
- conda-forge
- defaults
dependencies:
- python=3.8
- python=3.10
- numpy
- numba
- ubermag
# - pip
# - -r requirements.txt

Binary file added mcpy/__pycache__/Driver.cpython-38.pyc
Binary file not shown.
Binary file added mcpy/__pycache__/System.cpython-38.pyc
Binary file not shown.
Binary file added mcpy/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added mcpy/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added mcpy/__pycache__/driver.cpython-310.pyc
Binary file not shown.
Binary file added mcpy/__pycache__/system.cpython-310.pyc
Binary file not shown.
58 changes: 34 additions & 24 deletions mcpy/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from numba import njit, prange
from tqdm import tqdm
from mcpy.energies.numpy_energies import numpy_delta

# from mcpy.system import Grid

"""This module contains the main functions for implementing the Metropolis-Hastings Monte Carlo simulations.
By changing the spin each iteartion by random angle with uniform distribution."""
Expand All @@ -13,14 +13,14 @@
K_B = 1.38064852e-23 # Boltzmann constant


def driver_numpy(N, grid, zeeman_H, temperature):
def driver_numpy(N: int, grid, zeeman_H: np.ndarray, temperature: np.float64) -> np.ndarray:
""" Monte Carlo driver function for Numpy implementation
Args:
Args:
N (int): Number of Monte Carlo steps
grid (Grid): mcpy.Grid object
grid (mcpy.system.Grid): Grid object
zeeman_H (np.ndarray): Zeeman field
temperature (float): Temperature in Kelvin
temperature (np.float64): Temperature in Kelvin
Returns:
grid (np.ndarray): Relaxed system
Expand Down Expand Up @@ -97,24 +97,26 @@ def driver_numpy(N, grid, zeeman_H, temperature):


@njit(fastmath=True)
def driver_numba(N, grid, energy_func, zeeman_H, anisotropy_K, anisotropy_u, exchange_A, dmi_D, Dtype, Ms, dx, dy, dz, temperature):
def driver_numba(N: int, grid: np.ndarray, energy_func, zeeman_H: np.ndarray, anisotropy_K: np.float64,
anisotropy_u: np.ndarray, exchange_A: np.float64, dmi_D: np.ndarray, Dtype: str, Ms: np.float64,
dx: np.float64, dy: np.float64, dz: np.float64, temperature: np.float64) -> np.ndarray:
""" Monte Carlo driver function for Numba implementation
Args:
Args:
N (int): Number of Monte Carlo steps
grid (np.ndarray): 3D array of spins
energy_func (function): Energy function to be used
grid (np.ndarray): 3D array of spins(vector field)
energy_func (function): Energy function
zeeman_H (np.ndarray): Zeeman field
anisotropy_K (np.ndarray): Anisotropy constant
anisotropy_K (np.float64): Anisotropy constant
anisotropy_u (np.ndarray): Anisotropy axis
exchange_A (np.ndarray): Exchange constant
dmi_D (np.ndarray): Dzyaloshinskii-Moriya constant
Dtype (np.dtype): DMI type or Crystal class
Ms (float): Saturation magnetisation
dx (float): Grid spacing in x direction
dy (float): Grid spacing in y direction
dz (float): Grid spacing in z direction
temperature (float): Temperature in Kelvin
exchange_A (np.float64): Exchange constant
dmi_D (np.ndarray): DMI constant
Dtype (str): DMI crystal class
Ms (np.float64): Saturation magnetisation
dx (np.float64): Grid spacing in x direction
dy (np.float64): Grid spacing in y direction
dz (np.float64): Grid spacing in z direction
temperature (np.float64): Temperature in Kelvin
Returns:
grid (np.ndarray): Relaxed system
Expand Down Expand Up @@ -191,21 +193,29 @@ def driver_numba(N, grid, energy_func, zeeman_H, anisotropy_K, anisotropy_u, exc


@njit(fastmath=True)
def random_spin_uniform(v, alpha):
# Sample the cosine of the polar angle uniformly
def random_spin_uniform(v: np.ndarray, alpha: np.float64) -> np.ndarray:
"""Generates a random spin vector with uniform distribution
Args:
v (np.ndarray): Vector
alpha (np.float64): Angle
Returns:
v_proposal (np.ndarray): Random spin vector
"""

# cos of the polar angle
cos_del0 = np.random.uniform(np.cos(alpha), 1)
del_phi = np.random.uniform(0, 2 * np.pi)

# Derive the polar angle from its cosine value
del0 = np.arccos(cos_del0)
del0 = np.arccos(cos_del0) # polar angle
dx = np.sin(del0) * np.cos(del_phi)
dy = np.sin(del0) * np.sin(del_phi)
dz = np.cos(del0)

# Combine the unit vector with the original vector
v_proposal = v + np.array([dx, dy, dz])

# Normalising
# normalise
v_proposal = v_proposal / np.sqrt(np.sum(v_proposal ** 2))

return v_proposal
Binary file added mcpy/energies/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added mcpy/energies/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit c9d661d

Please sign in to comment.