<jupyter_start><jupyter_text>tutorial-mayavi-mlab-visualising-magnetisation  Author: <NAME>  Last Modified: 28/03/2014, tested on Mark's So'ton Desktop and his SP35P2Pro This notebook is aimed at users of finmag who are not familiar Mayavi's scripting capabilities and who want to streamline the process of creating pretty graphics. In this notebook we will create some sample data and use Mayavi to display, render and save the image, all without touching your mouse (hopefully!). We will be producing plots with glyphs in this tutorial, but the procedure is very similar for surfaces and other such objects. Much of this information has been obtained from the well-written Mayavi documentation[1].<jupyter_code>import dolfin as df
import finmag
from mayavi import __version__ as mayavi_version
from mayavi import mlab
import IPython.display
import numpy as np
import os

title = "tutorial-visualising-magnetisation-using-mayavi-mlab"
# IPYTHON_TEST_IGNORE_OUTPUT
print "Mayavi Version: " + mayavi_version<jupyter_output>Mayavi Version: 4.1.0<jupyter_text>Firstly, lets create some simulation data. Pretty standard stuff.<jupyter_code># Create simulation object with single skyrmion magnetisation.
mesh = df.RectangleMesh(-100, -100, 100, 100, 20, 20)
sim = finmag.Simulation(mesh, 1e5, unit_length=1e-9)
sim.initialise_skyrmions(70)
    
# Obtain locations for sampling. "x", "y" and "z" are locations of vectors
# in 3D space. We set "z" to zero everywhere to show an example in 2D space
# for simplicity.
c = mesh.coordinates().transpose()
x, y = c[:]
z = np.zeros_like(x)

# Obtain vector directions. "u", "v" and "w" define the directions of the
# vectors given by the "x", "y" and "z" co-ordinates.
u = np.zeros_like(x)
v = np.zeros_like(x)
w = np.zeros_like(x)

for zI in xrange(len(x)):
    u[zI], v[zI], w[zI] = sim.llg._m_field.f(x[zI], y[zI])<jupyter_output>[2014-09-12 15:59:31] INFO: Finmag logging output will be written to file: '/home/mb4e10/finmag/doc/ipython_notebooks_src/unnamed.log' (any old content will be overwritten).
[2014-09-12 15:59:31] DEBUG: Creating DataWriter for file 'unnamed.ndt'
[2014-09-12 15:59:31] INFO: Creating Sim object 'unnamed' (rank=0/1).
[2014-09-12 15:59:31] INFO: <Mesh of topological dimension 2 (triangles) with 441 vertices and 800 cells, ordered>
[2014-09-12 15:59:31] DEBUG: Creating LLG object.<jupyter_text>So now we have some data that precisely describes our field of vectors. Now for the interesting part! We are going to draw some vectors and render them to the "scene" (an object that Mayavi uses "under the hood" of mlab). Whenever data is modified using mlab, the scene is update automatically for simplicity. So, let's draw some vectors:<jupyter_code># Set offscreen rendering. This will still open a window, but nothing will be shown there.
mlab.options.offscreen = True

# Plot 3D vector field, and assign a reference scale to each vector equal to
# its out-of-plane component. Also use a colourmap that isn't hideous[2], though
# you can use your own custom colormap via the 'color' kwarg.
vectors = mlab.quiver3d(x, y, z, u, v, w, scalars=w, colormap="PuOr")

# We temporarily disable rendering to speed things up. This will be enabled again below.
# See http://docs.enthought.com/mayavi/mayavi/tips.html#accelerating-a-mayavi-script
vectors.scene.disable_render = True

# Colour vectors by the reference scale we included.
vectors.glyph.color_mode = 'color_by_scalar'

# Change the vectors to cones, because cones are awesome. NB <!>: I'm sure that
# there is a more elegant way of doing this, but I haven't found it yet.
cone_source = vectors.glyph.glyph_source.glyph_dict["cone_source"]
vectors.glyph.glyph_source.glyph_source = cone_source

# Resize the cones.
vectors.glyph.glyph_source.glyph_source.height = 0.4
vectors.glyph.glyph_source.glyph_source.radius = 0.2

# Reposition the cones relative to one-another (so that the result is flat). 
# Strange that this isn't the default setting, but there you go.
vectors.glyph.glyph_source.glyph_source.center = np.zeros(3)

# Set the resolution of objects.
vectors.glyph.glyph_source.glyph_source.resolution *= 6

# Re-enable rendering
vectors.scene.disable_render = False

# We also disable anti-aliasing for speed. This should not be done for high-quality plots.
# See http://docs.enthought.com/mayavi/mayavi/tips.html#accelerating-a-mayavi-script
vectors.scene.anti_aliasing_frames = 0<jupyter_output><empty_output><jupyter_text>Now the cones are in the scene, we must look at them from a nice angle. Once there, we can save our image and enter interactive mode.<jupyter_code># Choose a useful camera angle. Angles given in degrees. Lots of options for
# this function, would recommend taking a look at the documentation especially
# if you want to create "rotating camera" animations.
mlab.view(azimuth=90., elevation=0., distance=350.)

# Save the image. The size, output format (deduced by the filename) and
# magnification can be customised here.
mlab.savefig(title + "-output.png")

# Enable interactivity.
#mlab.show()<jupyter_output><empty_output><jupyter_text>This is what the generated image looks like (it would be slightly nicer with anti-aliasing enabled, but it already looks quite good).<jupyter_code>IPython.display.Image(title + "-output.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09126834776063229, 'fraction_numerical': 0.025780955965374483, 'mean_word_length': 5.0397727272727275, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '202747', 'license': 'BSL-1.0', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1639, 'n_words': 632, 'path': 'doc/ipython_notebooks_src/tutorial-mayavi-mlab-visualising-magnetisation.ipynb', 'repo_name': 'davidcortesortuno/finmag', 'size': '5320'}