from oommffield import Field, read_oommf_file

"""
Explanation: Tutorial: Manipulating OOMMF vector field files
In this tutorial, reading and writing of OOMMF vector field files (omf and ohf) are demonstrated. As usual, we need to import the Field class, but this time also the read_oommf_file function.
End of explanation
"""


cmin = (0, 0, 0)
cmax = (100e-9, 100e-9, 5e-9)
d = (5e-9, 5e-9, 5e-9)
dim = 3

"""
Explanation: We create a three-dimansional vector field with domain that spans between:

minimum coordinate $c_\text{min} = (0, 0, 0)$ and
maximum coordinate $c_\text{max} = (100 \,\text{nm}, 100 \,\text{nm}, 5 \,\text{nm})$,

with discretisation $d = (5 \,\text{nm}, 5 \,\text{nm}, 5 \,\text{nm})$.
End of explanation
"""


def m_init(pos):
    x, y, z = pos
    
    return (x+1, x+y+2, z+2)

field = Field(cmin, cmax, d, dim=dim, value=m_init)

"""
Explanation: Now, we can create a vector field object and initialise it so that:
$$f(x, y, z) = (x+1, x+y+2, z+3)$$
End of explanation
"""


#PYTEST_VALIDATE_IGNORE_OUTPUT
%matplotlib inline
fig = field.plot_slice('z', 2.5e-9, xsize=8)

"""
Explanation: Please note, that in this case we provided a field value as a field argument, which internally calls a set method explained in other tutorials.
If we plot the field, we get:
End of explanation
"""


filename = 'vector_field.omf'
field.write_oommf_file(filename)

"""
Explanation: This vector field can now be saved in an OOMMF omf file, by using write_oommf_file method and providing a filename.
End of explanation
"""


!ls *.omf

"""
Explanation: We can now see that, the OOMMF file is saved:
End of explanation
"""


field_read = read_oommf_file('vector_field.omf')

"""
Explanation: Now when we have the OOMMF vector field file, we can read it, which will create a different Field object.
End of explanation
"""


(field.f == field_read.f).all()

"""
Explanation: As expected, two fields must have exactly the same values at all nodes:
End of explanation
"""


!rm vector_field.omf

"""
Explanation: Finally we can delete the OOMFF file used in this tutorial.
End of explanation
"""


