# MIF 2.1
# MIF Example File: manyspheres.mif
# Description: Simulation of an array of spherical particles.
#
set pi [expr {4*atan(1.0)}]
set mu0 [expr {4*$pi*1e-7}]

RandomSeed 1

Parameter ball_radius     20e-9 ;# meters

Parameter ball_x_offset   25e-9 ;# meters
Parameter ball_y_offset   25e-9
Parameter ball_z_offset   25e-9

Parameter ball_x_spacing  50e-9 ;# meters
Parameter ball_y_spacing  50e-9
Parameter ball_z_spacing  50e-9

Parameter Ball_Ms          8e5  ;# A/m
Parameter Medium_Ms          0

Parameter cellsize         5e-9 ;# meters

proc SphereGrid { x y z } {
    global ball_radius
    global ball_x_offset   ball_y_offset   ball_z_offset
    global ball_x_spacing  ball_y_spacing  ball_z_spacing
    global Ball_Ms Medium_Ms

    # Determine position relative to grid
    set x [expr {fmod(abs($x-$ball_x_offset),$ball_x_spacing)}]
    if {2*$x>$ball_x_spacing} {set x [expr {$ball_x_spacing-$x}]}
    set y [expr {fmod(abs($y-$ball_y_offset),$ball_y_spacing)}]
    if {2*$y>$ball_y_spacing} {set y [expr {$ball_y_spacing-$y}]}
    set z [expr {fmod(abs($z-$ball_z_offset),$ball_z_spacing)}]
    if {2*$z>$ball_z_spacing} {set z [expr {$ball_z_spacing-$z}]}

    set radsq [expr {$x*$x+$y*$y+$z*$z}]
    if {$radsq>$ball_radius*$ball_radius} { return $Medium_Ms }
    return $Ball_Ms
}

Specify Oxs_BoxAtlas:atlas {
  xrange {0 500e-9}
  yrange {0 250e-9}
  zrange {0 100e-9}
}

Specify Oxs_RectangularMesh:mesh [subst {
  cellsize {$cellsize $cellsize $cellsize}
  atlas :atlas
}]

Specify Oxs_UniformExchange {
  A  13e-12
}

Specify Oxs_UZeeman [subst {
  multiplier [expr {0.001/$mu0}]
  Hrange {
      { 0  0  0   50  0  0   5 }
  }
}]

Specify Oxs_Demag {}

Specify Oxs_CGEvolve {}

Specify Oxs_MinDriver {
 basename manyspheres
 evolver Oxs_CGEvolve
 stopping_mxHxm 0.1
 mesh :mesh
 Ms { Oxs_ScriptScalarField {
     atlas :atlas
     script SphereGrid
     script_args rawpt
 }}
 m0 {1 0 0}
}
