API for developers
DataTypes
MicroMagnetic.NumberOrArrayOrFunction Type
NumberOrArrayOrFunctionIn 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
NwhereNis the total spin number of the system.Function: the parameter of the function should be
(i,j,k,dx,dy,dz)wherei,j,kis the cell index anddx,dy,dzis the cellsize. The return value of the function should be a real number. For example,juliafunction 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
MicroMagnetic.NumberOrTupleOrArrayOrFunction Type
NumberOrTupleOrArrayOrFunctionIn 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
Nor3NwhereNis 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,juliafunction uniform_m0(i,j,k,dx,dy,dz) return (0,0,1) end
MicroMagnetic.NumberOrArray Type
NumberOrArraySimilar to Union NumberOrArrayOrFunction, the Union NumberOrArray is designed to deal with cases that a number or an array is needed.
MicroMagnetic.TupleOrArrayOrFunction Type
TupleOrArrayOrFunctionSimilar 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
3NwhereNis 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,juliafunction uniform_m0(i,j,k,dx,dy,dz) return (0,0,1) end
Kernels
MicroMagnetic.atomistic_exchange_kernel! Function
compute the exchange interaction h_ex, which is defined as
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
sourceMicroMagnetic.atomistic_dmi_kernel! Function
compute the dmi interaction h_dmi, which is defined as
Jij is a 2d array with size (3, n_ngbs)
sourceServer (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 Type | Description |
|---|---|
run_code | Execute Julia code |
get_visualization_data | Request visualization data (mesh, spin, Ms) |
stop_server | Stop the server |
Data Formats
Mesh Data (FD Mesh)
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)
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
# Data format: [mx, my, mz, mx, my, mz, ...] 3 components per position
Array(spin)Ms Data
# Saturation magnetization array
Array(Ms) # or Array(mu_s) for atomistic simulationsClient (client.js)
Overview
client.js is a WebSocket client for communication with the Julia server.
Main Class: WebSocketClient
Constructor
const client = new WebSocketClient({
serverUrl: 'ws://localhost:10056', // Optional, auto-detect
sessionId: 'session_xxx' // Optional, auto-generate
});Common Methods
| Method | Description |
|---|---|
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 establisheddisconnect: Connection closederror: Error occurredvisualization_update: Visualization data receivedrun_code_response: Code execution resultsim_state_update: Simulation state update
Flowchart
Example
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 samplingcylindrical: Cylindrical coordinate sampling
Color Modes:
mx: X-component coloringmy: Y-component coloringmz: Z-component coloringinplane-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 displayisosurface: Isosurface with vertex coloring
Isosurface Coloring Pipeline:
Configuration Parameters
Arrow Configuration
{
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
{
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.