Skip to content

API for developers

DataTypes

MicroMagnetic.NumberOrArrayOrFunction Type
julia
NumberOrArrayOrFunction

In Micromagnetics, typical parameters such as saturation magnetization Ms and exchange stiffness constant A are constant. However, there are many cases that a spatial Ms is needed. For instance, if the simulated system is a circular disk the Ms in the corners should be set to zero. If the simulated system contains mutiple materials, the exchange constant A should be spatial as well. The Union NumberOrArrayOrFunction is designed to deal with such situations. As indicated from its name, it means that the input parameter could be a number or an array or a function:

  • Number: should be Real.

  • Array: the length of the array should be N where N is the total spin number of the system.

  • Function: the parameter of the function should be (i,j,k,dx,dy,dz) where i,j,k is the cell index and dx,dy,dz is the cellsize. The return value of the function should be a real number. For example,

    julia
    function circular_Ms(i,j,k,dx,dy,dz)
        if (i-50.5)^2 + (j-50.5)^2 <= 50^2
            return 8.6e5
        end
        return 0.0
    end
source
MicroMagnetic.NumberOrTupleOrArrayOrFunction Type
julia
NumberOrTupleOrArrayOrFunction

In micromagnetics, there are also cases where the input parameters can be either scalars or vectors and vary with space. For example, the parameters for the DMI could be a const for bulk DMI or interfacial DMI. In some materials, the DMI const may differ in different directions and thus a tuple with three numbers is required. In MicroMagnetic, the union NumberOrTupleOrArrayOrFunction is designed to deal with such situations. Similar to NumberOrArrayOrFunction, NumberOrTupleOrArrayOrFunction means that the input parameter could be a number, a tuple, an array or a function:

  • Number: should be Real.

  • Tuple: should be Real with length 3. For example, (1,2e-5,0).

  • Array: the length of the array should be N or 3N where N is the total spin number of the system.

  • Function: the parameter of the function should be (i,j,k,dx,dy,dz) and the return value should be a tuple with length 3. For example,

    julia
    function uniform_m0(i,j,k,dx,dy,dz)
        return (0,0,1)
    end
source
MicroMagnetic.NumberOrArray Type
julia
NumberOrArray

Similar to Union NumberOrArrayOrFunction, the Union NumberOrArray is designed to deal with cases that a number or an array is needed.

source
MicroMagnetic.TupleOrArrayOrFunction Type
julia
TupleOrArrayOrFunction

Similar to NumberOrArrayOrFunction, TupleOrArrayOrFunction means that the input parameter could be a tuple or an array or a function:

  • Tuple: should be Real with length 3. For example, (0,0,1e5).

  • Array: the length of the array should be 3N where N is the total spin number of the system.

  • Function: the parameter of the function should be (i,j,k,dx,dy,dz) and the return value should be a tuple with length 3. For example,

    julia
    function uniform_m0(i,j,k,dx,dy,dz)
        return (0,0,1)
    end
source

Kernels

MicroMagnetic.atomistic_exchange_kernel! Function

compute the exchange interaction h_ex, which is defined as

Hex,i=1μsi,jJi,jmj.

Jij is represented by Js, which is an array with its length equals to n_ngbs

Note that the return value is h * alpha + h_ex

source
MicroMagnetic.atomistic_dmi_kernel! Function

compute the dmi interaction h_dmi, which is defined as

Hdmi=i,jDij(mi×mj)

Jij is a 2d array with size (3, n_ngbs)

source

Server (server.jl)

Overview

server.jl provides WebSocket server functionality for real-time communication with the frontend GUI.

Main Functions

start_server(;port, host, async, lan)

Start the WebSocket server.

Parameters:

  • port::Int: Server port (default: 10056)

  • host: Server address (default: auto-detect)

  • async::Bool: Whether to start asynchronously (default: true)

  • lan::Bool: Whether to allow LAN access (default: true)

gui(;port, host, lan)

Start MicroMagnetic GUI (high-level interface).

Parameters:

  • port::Int: Server port (default: 10056)

  • host::String: Server address (default: "127.0.0.1")

  • lan::Bool: Whether to allow LAN access (default: false)

Architecture

Message Handling

The server processes client requests through message handlers:

Message TypeDescription
run_codeExecute Julia code
get_visualization_dataRequest visualization data (mesh, spin, Ms)
stop_serverStop the server

Data Formats

Mesh Data (FD Mesh)

julia
Dict(
    "type" => "fd",  # or "fe" for FEMesh
    "nx" => mesh.nx,
    "ny" => mesh.ny,
    "nz" => mesh.nz,
    "dx" => mesh.dx * 1e9,  # Convert to nm
    "dy" => mesh.dy * 1e9,
    "dz" => mesh.dz * 1e9
)

Mesh Data (FE Mesh)

julia
Dict(
    "type" => "fe",
    "number_nodes" => mesh.number_nodes,
    "number_cells" => mesh.number_cells,
    "coordinates" => [[x1, y1, z1], [x2, y2, z2], ...],
    "cell_verts" => [[v1, v2, v3, v4], ...],  # 0-based indices
    "region_ids" => [region_id, ...]
)

Spin Data

julia
# Data format: [mx, my, mz, mx, my, mz, ...] 3 components per position
Array(spin)

Ms Data

julia
# Saturation magnetization array
Array(Ms)  # or Array(mu_s) for atomistic simulations

Client (client.js)

Overview

client.js is a WebSocket client for communication with the Julia server.

Main Class: WebSocketClient

Constructor

javascript
const client = new WebSocketClient({
    serverUrl: 'ws://localhost:10056',  // Optional, auto-detect
    sessionId: 'session_xxx'            // Optional, auto-generate
});

Common Methods

MethodDescription
connect()Connect to the server
sendMessage(type, data)Send a message
sendCommand(type, data)Send a command (with ID)
on(event, callback)Add event listener
off(event, callback)Remove event listener
disconnect()Disconnect from server

Event Types

  • connect: Connection established

  • disconnect: Connection closed

  • error: Error occurred

  • visualization_update: Visualization data received

  • run_code_response: Code execution result

  • sim_state_update: Simulation state update

Flowchart

Example

javascript
const client = new WebSocketClient();

// Listen for visualization data
client.on('visualization_update', (data) => {
    console.log('Mesh:', data.mesh);
    console.log('Spin:', data.spin);
    console.log('Ms:', data.Ms);
});

// Listen for code execution results
client.on('run_code_response', (data) => {
    console.log('Success:', data.success);
    console.log('Output:', data.stdout);
});

// Send code for execution
client.sendMessage('run_code', { code: 'sim.mesh' });

GUI Visualization

Overview

The GUI uses Three.js for 3D visualization with multiple display modes.

Component Architecture

Main Components

FDMeshVisualization (FDMeshVisualization.js)

Displays Finite Difference (FD) mesh with visualization of cell boundaries.

FEMeshVisualization (FEMeshVisualization.js)

Displays Finite Element (FE) mesh with tetrahedral elements.

ArrowVisualization (ArrowVisualization.js)

Displays magnetization vector arrows at sampled positions.

Sampling Modes:

  • cartesian: Cartesian coordinate sampling

  • cylindrical: Cylindrical coordinate sampling

Color Modes:

  • mx: X-component coloring

  • my: Y-component coloring

  • mz: Z-component coloring

  • inplane-angle: In-plane angle coloring (default)

Symmetric Sampling Algorithm:

SurfaceVisualization (SurfaceVisualization.js)

Displays surfaces and isosurfaces using Marching Cubes algorithm.

Display Types:

  • surface: 2D slice display

  • isosurface: Isosurface with vertex coloring

Isosurface Coloring Pipeline:

Configuration Parameters

Arrow Configuration

javascript
{
    sampling: 'cartesian',    // 'cartesian' | 'cylindrical'
    sampleNx: 10,             // X-direction sample count
    sampleNy: 10,             // Y-direction sample count
    sampleNz: 10,             // Z-direction sample count
    arrowSize: 1.0,          // Arrow size multiplier
    component: 'mx',          // 'mx' | 'my' | 'mz'
    colormap: 'viridis'       // Colormap name
}

Surface Configuration

javascript
{
    type: 'surface',          // 'surface' | 'isosurface'
    component: 'mx',          // 'mx' | 'my' | 'mz'
    isoValue: 0.5,            // Isosurface value
    position: 1,              // Slice position (1-based)
    direction: 'z',           // 'x' | 'y' | 'z'
    colormap: 'viridis',
    colorMode: 'inplane-angle' // 'mx' | 'my' | 'mz' | 'inplane-angle'
}

Data Flow

Coordinate System

Key Points:

  • Grid Index: 0-based (JavaScript internal)

  • Display Index: 1-based (user interface)

  • Coordinate System: Right-hand system (Three.js)

    • X-axis: Points right

    • Y-axis: Points up

    • Z-axis: Points out of screen

Colormaps

Multiple colormaps are supported: viridis, plasma, inferno, magma, coolwarm, etc.

Data Processing Pipeline