<jupyter_start><jupyter_text>Reading and writing fieldsThere are two main file formats to which a `discretisedfield.Field` object can be saved:- [VTK](https://vtk.org/) for visualisation using e.g., [ParaView](https://www.paraview.org/) or [Mayavi](https://docs.enthought.com/mayavi/mayavi/)- OOMMF [Vector Field File Format (OVF)](https://math.nist.gov/oommf/doc/userguide12a5/userguide/Vector_Field_File_Format_OV.html) for exchanging fields with micromagnetic simulators.Let us say we have a nanosphere sample:$$x^2 + y^2 + z^2 <= r^2$$with $r=5\,\text{nm}$. The space is discretised into cells with dimensions $(0.5\,\text{nm}, 0.5\,\text{nm}, 0.5\,\text{nm})$. The value of the field at $(x, y, z)$ point is $(-cy, cx, cz)$, with $c=10^{9}$. The norm of the field inside the cylinder is $10^{6}$.Let us first build that field.<jupyter_code>import discretisedfield as df

r = 5e-9
cell = (0.5e-9, 0.5e-9, 0.5e-9)

mesh =  df.Mesh(p1=(-r, -r, -r), p2=(r, r, r), cell=cell)

def norm_fun(pos):
    x, y, z = pos
    if x**2 + y**2 + z**2 <= r**2:
        return 1e6
    else:
        return 0
    
def value_fun(pos):
    x, y, z = pos
    c = 1e9
    return (-c*y, c*x, c*z)

field = df.Field(mesh, dim=3, value=value_fun, norm=norm_fun)<jupyter_output><empty_output><jupyter_text>Let us have a quick view of the field we created<jupyter_code># NBVAL_IGNORE_OUTPUT
field.plane('z').k3d.vector(color_field=field.z)<jupyter_output><empty_output><jupyter_text>Writing the field to a fileThe main method used for saving field in different files is `discretisedfield.Field.write()`. It takes `filename` as an argument, which is a string with one of the following extensions:- `'.vtk'` for saving in the VTK format- `'.ovf'`, `'.omf'`, `'.ohf'` for saving in the OVF formatLet us firstly save the field in the VTK file.<jupyter_code>vtkfilename = 'my_vtk_file.vtk'
field.write(vtkfilename)<jupyter_output><empty_output><jupyter_text>We can check if the file was saved in the current directory.<jupyter_code>import os
os.path.isfile(f'./{vtkfilename}')<jupyter_output><empty_output><jupyter_text>Now, we can delete the file:<jupyter_code>os.remove(f'./{vtkfilename}')<jupyter_output><empty_output><jupyter_text>Next, we can save the field in the OVF format and check whether it was created in the current directory.<jupyter_code>omffilename = 'my_omf_file.omf'
field.write(omffilename)
os.path.isfile(f'./{omffilename}')<jupyter_output><empty_output><jupyter_text>There are three different possible representations of an OVF file: one ASCII (`txt`) and two binary (`bin4` or `bin8`). ASCII `txt` representation is a default representation when `discretisedfield.Field.write()` is called. If any different representation is required, it can be passed via `representation` argument.<jupyter_code>field.write(omffilename, representation='bin8')
os.path.isfile(f'./{omffilename}')<jupyter_output><empty_output><jupyter_text>Reading the OVF fileThe method for reading OVF files is a class method `discretisedfield.Field.fromfile()`. By passing a `filename` argument, it reads the file and creates a `discretisedfield.Field` object. It is not required to pass the representation of the OVF file to the `discretisedfield.Field.fromfile()` method, because it can retrieve it from the content of the file.<jupyter_code>read_field = df.Field.fromfile(omffilename)<jupyter_output><empty_output><jupyter_text>Like previouly, we can quickly visualise the field<jupyter_code># NBVAL_IGNORE_OUTPUT
read_field.plane('z').k3d.vector(color_field=read_field.z)<jupyter_output><empty_output><jupyter_text>Finally, we can delete the OVF file we created.<jupyter_code>os.remove(f'./{omffilename}')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13023507160226966, 'fraction_numerical': 0.012969467711429343, 'mean_word_length': 5.682310469314079, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 41, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '321', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1281, 'n_words': 367, 'path': 'docs/field-read-write.ipynb', 'repo_name': 'ubermag/discretisedfield', 'size': '3701'}