Resampling¶
(Available in Nmag-0.1.0-dev)
You can load an h5 file like this
import ocaml
from nmag.h5probe import Fields
handler = Fields("infile.h5")
field = handler.set_field_data("m", "Py", 0)
And probe one of its fields like this
position = [0, 1, 2] # In mesh units (typically is nanometres) value = ocaml.probe_field(field, "m_Py", position)[0][1]
This way you can create two arrays: rs containing an array of points and vs containing the corresponding values.
You can then use pyvtk to generate a VTK file from these:
import pyvtk
grid = pyvtk.UnstructuredGrid(rs)
data = pyvtk.PointData(pyvtk.Vectors(vs))
v = pyvtk.VtkData(grid, data)
v.tofile("outfile.vtk")
Here is a full example, which probes the magnetisation in the outer skin of a cylinder, in sections which are not equally spaced.
Notice the usage of the function float_set to specify where the sampling should be denser (originally, here is where a domain wall was).
The script should be used as nsim probe.py infile.h5 outfile.vtk
import math
import sys
import pyvtk
import ocaml
from nmag.h5probe import Fields
from nmag import float_set
# First we probe the field in the required points
handler = Fields(sys.argv[1])
field = handler.set_field_data("m", "Py", 0)
xs = float_set([-150.0, -145.0, [], -15.0, -12.5, [], 15.0, 20.0, [], 50.0])
angles = float_set([0, [20], 2*math.pi])
R, R2 = (4.9, 5.1)
rs = []
vs = []
for x in xs:
for angle in angles:
r = [x, R*math.cos(angle), R*math.sin(angle)]
rs.append([x, R2*math.cos(angle), R2*math.sin(angle)])
vs.append(ocaml.probe_field(field, "m_Py", r)[0][1])
# Now we output the values to a VTK file
grid = pyvtk.UnstructuredGrid(rs)
data = pyvtk.PointData(pyvtk.Vectors(vs))
v = pyvtk.VtkData(grid, data)
v.tofile(sys.argv[2])