A case study of computational science in Jupyter notebooks...

45
A case study of computational science in Jupyter notebooks: JOOMMF Hans Fangohr * and Marijan Beg Faculty of Engineering and the Environment, University of Southampton, UK * email: [email protected], @ProfCompMod Edinburgh, United Kingdom (20 January 2017)

Transcript of A case study of computational science in Jupyter notebooks...

Page 1: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

A case study of computational science in Jupyter notebooks: JOOMMF

Hans Fangohr* and Marijan BegFaculty of Engineering and the Environment, University of Southampton, UK

* email: [email protected], @ProfCompMod

Edinburgh, United Kingdom (20 January 2017)

Page 2: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

2

Python – AN IntroduCtionHans Fangohr, [email protected], BioSimGrid Meeting 25/11/2004

Overview

• Why Python• How to get started• Interactive Python (IPython)• Installing extra modules• Lists• For-loops• if-then• modules and name spaces• while• string handling• file-input, output• functions• Numerical computation• some other features

◃ long numbers◃ exceptions◃ dictionaries

Python – an introduction 1

Why Python?

→ Chapter 1, p3 in LP

• All sorts of reasons ;-)

◃ Object-oriented scripting language◃ power of high-level language◃ portable, powerful, free◃ mixable (glue together with C/C++, Fortran,

. . . )◃ easy to use (save time developing code)◃ easy to learn◃ (in-built complex numbers)

• Today:

◃ easy to learn◃ some interesting features of the language◃ use as tool for small sysadmin/data

processing/collecting tasks

Python – an introduction 3

Page 3: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Overview1. Micromagnetics

Magnetic moment

Energies

Dynamics

OOMMF

3

2. OOMMF workflow

Standard problem 3

Example

3. JOOMMF

micromagneticmodel

standard problem 3

4. Some details & outlook

Page 4: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Micromagnetics

4

Page 5: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Why magnetic nanostructures?

1. Interesting complex system with tuneable parameters and experiments

2. Real applications, including • magnetic data storage (hard disk) • cancer therapy • low energy magnetic logic (spintronics)

E. Dobisz et. al., Proceedings of IEEE 96, 1836 (2008) Curtis & Fangohr (2011)

Page 6: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Magnetic moment

6

N

S

m

Page 7: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Magnetisation dynamics

7

@m

@t= �⇤m⇥He↵ + ↵m⇥ @m

@t

• Landau-Lifshitz-Gilbert (LLG) equation

precession

damping

He↵ He↵ He↵

He↵ He↵ He↵

+

+=

=

Page 8: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Zeeman energy

8

mH

m

H

wz = �µ0H ·mAlign the magnetic moment to an external field.

1

2

local

Page 9: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Anisotropy energy

9

Align the magnetic moment to an easy axis.

1

2

m

e

m

e

m

e

local

wa = �K(m · e)2

Page 10: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Exchange energy

10

Align all magnetic moments parallel.

1

2

we = �Jmi ·mi+1

mi+1

mi

x

mi mi+1

x

short range

Page 11: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Dzyaloshinskii-Moriya energy (DMI)

11

Align neighbouring moments perpendicular.

1

2

mi+1

mi

x

we = �D · (mi ⇥mi+1)

mi

Dx

mi+1

short range

Page 12: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Exchange + Zeeman energies

12

1

2

mi+1

mi

x

mi mi+1

x

mH

m

H

+

Page 13: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Exchange + DMI energies

13

1

2

mi+1

mi

x mi

Dx

mi+1

mi mi+1

x

Page 14: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

More complicated case

14

• All four energies included:• Exchange• Zeeman• Anisotropy• Dzyaloshinskii-Moriya energy (DMI)

• Two-dimensional sample.• Particular set of material parameters J, D, and K.• Non zero applied field.

mi mi+1

x

mH

mi

Dx

mi+1

m

e

m

e

Page 15: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

15

Page 16: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

@m

@t= f(m)

• Magnetisation in sample V is described by a continuous vector field m(r):

• We have an equation of motion

• f is complicated, involves PDEs

Micromagnetic model

m : V 7! R3 V ⇢ R3

Page 17: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Coarse graining

17

m

atom

magneticmoment

atomistic model

continuous (=micromagnetic) model

m

mesh point

magneticmoment

m =?

unknown:magnetisation

vector field

Page 18: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

interaction expressionsatomisticmodel

continuousmodel effective field

Zeemanenergy

Anisotropyenergy

Exchangeenergy

DMI energy

H

�K(m · e)2 �K(m · e)22K

µ0Ms(m · e)e

�Jmi ·mi+1 A(rm)22A

µ0Msr2m

�µ0Msm ·H�µ0Msm ·H

Dm · (r⇥m)D · (mi ⇥mi+1) � 2D

µ0Ms(r⇥m)

Page 19: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

PDEs for micromagnetics

He↵(m) =2A

µ0Msr2m� 2D

µ0Ms(r⇥m) +H+Hd

precession

damping

norm correction

exchange

DMI

Zeeman

demagnetisation

• LLG equation

• Effective field

volume term

surface term• Magnetic scalar potential and demagnetisation field

�(r0) =Ms

4⇡

✓�Z

r ·m(r0)

||r� r0|| dV +

Z

@⌦

m(r0) · n||r� r0|| dS

Hd(r) = �r�(r)

@m

@t= �⇤m⇥He↵ + ↵m⇥ @m

@t+ u(|m|� 1)

m

|m|

Page 20: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Micromagnetic computational problem

20

• semi-discretised: PDE in every timestep, coupled set of ODEs to integrate time

• Dynamics of magnetisation is defined by a stiff set of equations (different timescales)

• Different length scales (exchange vs demagnetisation)

• “More difficult than CFD”

Page 21: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Micromagnetics

21

• The number of problems that can be solved analytically is very limited.

• Experimental techniques do not provide enough insight.

• Simulations allow to study the micromagnetic systems in much more detail, vary parameters, measure different observables, etc.

Parkin, Science, 320, 190 (2008)

Bit-patterned media (Seagate)

Page 22: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Object Oriented MicroMagnetic Framework (OOMMF)

22

• Probably the most widely used simulation tool

• Developed at NIST, USA

• Cited over 2200 times in scientific publications

• Written in C++, some Tcl glue / interface

GUI

Tcl config file

Page 23: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

OOMMF workflow

Page 24: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Research question

?

flower

vortex

For what cube edge length have vortex and flower states the

same energy?

Page 25: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Step 1: write simulation configuration

Page 26: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

step 1: write simulation configuration

Page 27: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Step 2: run simulation

Page 28: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Step 3: read data

Page 29: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

L flower vortex8.0 ? 3.23 x 10-16

8.1 ? ?8.2 ? ?8.3 ? ?8.4 ? ?8.5 ? ?8.6 ? ?8.7 ? ?8.8 ? ?8.9 ? ?9.0 ? ?

We have to repeat steps 1, 2, and 3 to obtain other 21 points

repeat simulations…

Page 30: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

postprocessing• We plot the data we obtained by running separate plotting scripts or by

using some Graphical User Interfaces (Python, MATLAB, Excel, Origin…)

• Find crossing

Page 31: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Workflow

Write script

Run scripts

Read results

Postprocessing

Page 32: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Issues with (OOMMF) workflow

• Time consuming

• Keeping log of all steps that were run and in what order

• Separate post processing scripts

• Reproducibility?

Page 33: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

JOOMMF

Page 34: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

JOOMMF

• Micromagnetic Virtual Research Environment

• Enable running OOMMF simulations in Jupyter notebook

• Jupyter + OOMMF = JOOMMF

• Communicate to OOMMF via Python interface (T3.8): Domain specific language embedded in general purpose language [1]

[1] Marijan Beg, Ryan A. Pepper, Hans Fangohr: User interfaces for computational science: a domain

specific language for OOMMF embedded in Python, AIP Advances accepted, arxiv:1608.04876 (2017)

Page 35: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Road map JOOMMFPython

interface

Embed in Jupyter

DisseminationEvaluation

Data analysis

MatplotlibLatex representation

PandasHoloviews

interactive 3d vis

OOMMFC

T7.4 - evaluationT2.8 - workshops

T3.8 - Python

T4.11- Jupyter

T4.14 - cloud T4.8 - 3d vis

WidgetsT4.13 - interactive doc

Page 36: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

JOOMMF example - in Jupyter -

[Live demo in Notebook]

Page 37: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

OOMMF-Python communication• The core OOMMF computational routines are implemented in

C++, higher level functionality implemented in Tcl

• Possible solutions for interfacing Tcl and C++ with Python include swig, boost, cython

• We decided communicating with OOMMF via MIF (configuration) files:

• This provides us robustness since we do not need to adapt the interface depending on the platform.

• Re-use of all functionality in Tc

• Easier to debug, even for end users

Page 38: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Tables

Page 39: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Benefits

• Documentation, computation, visualisation in the same notebook

• The entire workflow is contained in a single document

• Self documenting, reproducible, NBVAL

• Easy to share, publish, and collaborate

• Executable tutorial for OOMMF Software

Page 40: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Micromagnetic model example - in jupyter -

[Live demo in Notebook]

Page 41: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

JOOMMF structure

micromagneticmodel

discretisedfield

oommfodt

joommfutil

oommfc

magnetisationdatatable

Page 42: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Road map JOOMMFPython

interface

Embed in Jupyter

DisseminationEvaluation

Data analysis

MatplotlibLatex representation

PandasHoloviews

interactive 3d vis

OOMMFC

T7.4 - evaluationT2.8 - workshops

T3.8 - Python

T4.11- Jupyter

T4.14 - cloud T4.8 - 3d vis

WidgetsT4.13 - interactive doc

Page 43: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Outlook

Page 44: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Summary• Virtual Research Environment allows us to have

documentation (text and equations), code, code outputs (text, figures, tables) in a single file

• URL: joommf.github.io

• Domain specific language embedded in Python: benefit of using already existing language and libraries for data analysis and visualisation:Python interface and embedded Domain Specific Language: Beg et al. arXiv 1609.07432 (2016)

• email: [email protected]

Page 45: A case study of computational science in Jupyter notebooks ...joommf.github.io/assets/2017-01-20-Edinburgh-Jupyter-workshop-joommf.pdf · 1/20/2017  · A case study of computational

Acknowledgements• Contributors: Marijan Beg, Ryan Pepper, Thomas Kluyver, Hans Fangohr

• Financial support from

• OpenDreamKit Horizon 2020, European Research Infrastructures project (#676541), http://opendreamkit.org

• EPSRC's Centre for Doctoral Training in Next Generation Computational Modelling, http://ngcm.soton.ac.uk (#EP/L015382/1) and

• The Gordon and Betty Moore Foundation through Grant GBMF #4856, by the Alfred P. Sloan Foundation and by the Helmsley Trust.