# MIF 2.1
# MIF Example File: pillar.mif
# Description: Applied field pulse to a pillar (cylinder) geometry.

set pi [expr {4*atan(1.0)}]
set mu0 [expr {4*$pi*1e-7}]

RandomSeed 1

Parameter relax 0     ;# Relax to equilibrium instead of pulse?

Parameter cellsize 2.5e-9

Parameter pamp  200   ;# Maximum amplitude of the applied field pulse, in mT
Parameter pwidth 10   ;# Pulse width at half height, in ps
Parameter pwait  0.1  ;# Location in time of pulse peak, in ns

# Convert pulse amplitude from mT to A/m
set pamp [expr {$pamp*0.001/$mu0}]

# Convert pwait to seconds
set pwait [expr {$pwait*1e-9}]

# Compute exponential factor, in 1/sec^2.  Field formula is
# pamp.exp(pfac.(t-pwait)^2), with t in seconds.
set pfac [expr {-log(2.)/(($pwidth*0.5*1e-12)*($pwidth*0.5*1e-12))}]

Specify Oxs_BoxAtlas:atlas {
  xrange {0  20e-9}
  yrange {0  20e-9}
  zrange {0 120e-9}
}

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

Specify Oxs_CubicAnisotropy {
  K1  -4.5e3
  axis1 {1 0 0}
  axis2 {0 1 0}
}

Specify Oxs_UniformExchange {
  A  9e-12
}

proc ExpPulse { total_time } {
   set amp 7.96          ;# A/m
   set damping -7.675e9  ;# 1/sec
   set Hy [expr {$amp*exp($damping*$total_time)}]
   set dHy [expr {$damping*$Hy}]
   return [list 0 $Hy 0 0 $dHy 0]
}

proc GaussianPulse { total_time } {
   global pamp pfac pwait
   set t [expr {$total_time-$pwait}]
   set Hy [expr {$pamp*exp($pfac*$t*$t)}]
   set dHy [expr {2*$pfac*$t*$Hy}]
   return [list 0 $Hy 0 0 $dHy 0]
}

# Add biasing field, 100 mT
Ignore Specify Oxs_FixedZeeman [subst {
   field { 100 0 0}
   multiplier [expr {0.001/$mu0}]
}]


Specify Oxs_Demag {}

proc Cylinder { Ms x y z } {
    set xrad [expr {2.*$x - 1.} ]
    set yrad [expr {2.*$y - 1.} ]
    set test [expr {$xrad*$xrad+$yrad*$yrad}]
    if {$test>1.0} {return 0}
    return $Ms
}

if {$relax} {
 Specify Oxs_CGEvolve:evolve {}
 Specify Oxs_MinDriver [subst {
    basename pillar-start
    evolver :evolve
    stopping_mxHxm 1e-9
    mesh :mesh
    Ms { Oxs_ScriptScalarField {
       atlas :atlas
       script {Cylinder 8e5}
    }}
    comment {Ms 8e5}
    m0 { 1 0.02 0.01 }
 }]
} else {
 Specify Oxs_ScriptUZeeman {
   script_args total_time
   script GaussianPulse
 }
 Specify Oxs_RungeKuttaEvolve:evolve {
    alpha 0.015
 }
 Specify Oxs_TimeDriver [subst {
    basename pillar
    evolver :evolve
    comment {run for 5 ns total, with a stage event triggered every 0.2 ns
       inside the first ns, and then every 0.05 ns for the next 4 ns.}
    comment {stopping_time {{0.2e-9 5} {0.05e-9 80} :expand:}}
    stopping_time {{1e-12 5000} :expand:}
    mesh :mesh
    Ms { Oxs_ScriptScalarField {
       atlas :atlas
       script {Cylinder 8e5}
    } }
    comment {Ms 8e5}
    m0 { Oxs_FileVectorField {
       atlas :atlas
       file pillar-start.omf
    }}
 }]
}
