<filename>scripts/Laguerre_Gauss_3d/plot_3d_mayavi.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
file:   plot_3d_mayavi.py
brief:  Python script to visualise (intensity) isosurfaces of scattered vortex beams.
        The program uses as input HDF5 files generated by the FDTD solver Meep.
author: <NAME>
date:   19.01.2018
"""
from mayavi import mlab

import numpy as np
import h5py


def cuboid(ext_grid, rot=0, color=(1, 0, 0), opacity=1.0):
    """Return a cuboid.

    The cuboid is defined on the grid 'ext_grid' with an angle of rotation
    'rot' about the z-axis given in degrees. This cuboid is ment to represent
    the optically denser material.
    """
    xl, xr, yl, yr, zl, zr = ext_grid

    triangles = [(0, 1, 2), (1, 2, 3), (0, 1, 4), (1, 4, 5), (4, 5, 6),
                 (6, 7, 5), (2, 3, 6), (3, 6, 7), (0, 2, 6), (0, 6, 4),
                 (3, 1, 7), (1, 7, 5)]

    x = np.tile(np.array([xr, xr, xl, xl]), 2)   # repeat array twice
    y = np.tile(np.array([yl, yr, yl, yr]), 2)   # repeat array twice
    z = np.concatenate((np.array([zl, zl, zl, zl]),
                        np.array([zr, zr, zr, zr])))

    rot = np.deg2rad(rot)

    xr = np.cos(rot)*x - np.sin(rot)*y
    yr = np.sin(rot)*x + np.cos(rot)*y

    return mlab.triangular_mesh(xr, yr, z, triangles, color=color,
                                opacity=opacity)


def free_memory(*objects):
    """Delete objects.

    Delete and free memory if object exists and no other references than the
    given variable name point to it.

    objects: comma separated variables nams given as strings
    Example: free_memory("x", "y", "z")
    """
    for obj in objects:
        if obj in globals():
            del globals()[obj]


# ------------------------------------------------------------------------------
# set parameters
# ------------------------------------------------------------------------------
# relative index of refraction
n = 1.54 / 1.0
# angle of incidence in degrees
chi_deg = 45.0
# inclination of the interface with respect to the x-axis
inc_deg = 90 - chi_deg
# cut off borders of data (remove PML layer up to and including line source
# placement)
cutoff = 30


# ------------------------------------------------------------------------------
# import data from HDF file(s)
# ------------------------------------------------------------------------------
path = "simulations/meep-C/LaguerreGauss3d_C-out/"

filename_real = path + "e_real2_s-000001540.h5"
filename_imag = path + "e_imag2_s-000001540.h5"

with h5py.File(filename_real, 'r') as hf:
    # print("keys: %s" % hf.keys())
    data_real = hf[list(hf.keys())[0]][:]

with h5py.File(filename_imag, 'r') as hf:
    # print("keys: %s" % hf.keys())
    data_imag = hf[list(hf.keys())[0]][:]

# choose whether to use the electric field energy density (proportional to
# 'data_real') or the complex modulus of the complex electric field
# (proportional to 'data_real + data_imag') as data basis:
#data = data_real
data = data_real + data_imag

# free memory early
free_memory("data_real", "data_imag")

orig_shape = np.shape(data)

print("file size in MB: ", data.nbytes / 1024 / 1024)
print("data (max, min): ", (data.max(), data.min()))
print("original shape:  ", orig_shape)

data = data[cutoff:-cutoff, cutoff:-cutoff, cutoff:-cutoff] / data.max()
new_shape = np.shape(data)


# ------------------------------------------------------------------------------
# visualising an iso-contour surface of the vortex beam
# ------------------------------------------------------------------------------
fig = mlab.figure(1, bgcolor=(0, 0, 0), size=(400, 400))
# TODO: fix the following line
#fig.scene.render_window.aa_frames = 8               # antialiasing

sx, sy, sz = np.array([5, 5, 3.5]) * (np.asarray(new_shape)
                                      / np.asarray(orig_shape))
SX, SY, SZ = np.mgrid[-sx/2.0:sx/2.0:eval('{}j'.format(data.shape[0])),
                      -sy/2.0:sy/2.0:eval('{}j'.format(data.shape[1])),
                      -sz/2.0:sz/2.0:eval('{}j'.format(data.shape[2]))]

src = mlab.pipeline.scalar_field(SX, SY, SZ, data)  # Mayavi source

# free memory early
free_memory("data", "SX", "SY", "SZ")

# volume of interest
voi = mlab.pipeline.extract_grid(src)

# halving volume at plane of incidence (comment out to visualise entire beam)
voi.set(z_max=np.ceil(new_shape[2]/2))

VMAX = 0.02
iso = mlab.pipeline.iso_surface(voi, colormap="hot", contours=[VMAX])
iso.module_manager.scalar_lut_manager.use_default_range = False
iso.module_manager.scalar_lut_manager.data_range = [0.0, VMAX]

# add two cut planes to show the transverse intensity distributions
pipe_inc = mlab.pipeline.image_plane_widget(src, plane_orientation='x_axes',
                                            slice_index=0, colormap='hot',
                                            vmin=0.0, vmax=VMAX)
pipe_ref = mlab.pipeline.image_plane_widget(src, plane_orientation='y_axes',
                                            slice_index=new_shape[1],
                                            colormap='hot',
                                            vmin=0.0, vmax=VMAX)


# ------------------------------------------------------------------------------
# visualising k-vectors of central plane waves accor. to classical geom. optics
# ------------------------------------------------------------------------------
# angle of refraction in radians
eta_rad = np.arcsin((1.0/n) * np.sin(np.deg2rad(chi_deg)))

# properties of the k-vectors
vec_length = 3.5
vec_radius = 0.008
vec_color = (1, 0, 0)

# components of the k-vectors
inc = (-vec_length, 0, 0)
ref = (vec_length * np.sin(np.deg2rad(chi_deg - inc_deg)),
       vec_length * np.cos(np.deg2rad(chi_deg - inc_deg)), 0)
tra = (vec_length * np.sin(eta_rad + np.deg2rad(inc_deg)),
       -vec_length * np.cos(eta_rad + np.deg2rad(inc_deg)), 0)

components = [inc, ref, tra]

# visualise incident central k-vector
vector = mlab.quiver3d(0, 0, 0, *components[0], color=vec_color,
                       scale_factor=1, mode='cylinder', resolution=25)
vector.glyph.glyph_source.glyph_source.radius = vec_radius

# visualise secondary central k-vectors
for i in [1, 2]:
    vectors = mlab.quiver3d(0, 0, 0, *components[i], color=vec_color,
                            scale_factor=1, mode='arrow', resolution=25)
    vectors.glyph.glyph_source.glyph_source.shaft_radius = vec_radius
    vectors.glyph.glyph_source.glyph_source.tip_radius = 0.025
    vectors.glyph.glyph_source.glyph_source.tip_length = 0.13

# visualise optically denser material
color_overlay = (0.227, 0.188, 0.188)
cuboid(ext_grid=(-sx/2.0, sx/2.0, -sy/1.5, 0, -sz/2.0, sz/2.0), rot=inc_deg,
       color=color_overlay, opacity=0.5)


# mlab.orientation_axes()
mlab.view(azimuth=-inc_deg, elevation=0, distance=12, focalpoint='auto')
fig.scene.camera.azimuth(20)
fig.scene.camera.elevation(30)

# mlab.savefig('visualize_field.png')
mlab.show()

free_memory("src", "vol", "voi", "pipe_inc", "pipe_ref", "iso", "SX", "SY",
            "SZ")

<filename>scripts/Laguerre_Gauss_3d/plot_3d_mayavi.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
file:   plot_3d_mayavi.py
brief:  Python script to visualise (intensity) isosurfaces of scattered vortex beams.
        The program uses as input HDF5 files generated by the FDTD solver Meep.
author: <NAME>
date:   19.01.2018
"""
from mayavi import mlab

import numpy as np
import h5py


def cuboid(ext_grid, rot=0, color=(1, 0, 0), opacity=1.0):
    """Return a cuboid.

    The cuboid is defined on the grid 'ext_grid' with an angle of rotation
    'rot' about the z-axis given in degrees. This cuboid is ment to represent
    the optically denser material.
    """
    xl, xr, yl, yr, zl, zr = ext_grid

    triangles = [(0, 1, 2), (1, 2, 3), (0, 1, 4), (1, 4, 5), (4, 5, 6),
                 (6, 7, 5), (2, 3, 6), (3, 6, 7), (0, 2, 6), (0, 6, 4),
                 (3, 1, 7), (1, 7, 5)]

    x = np.tile(np.array([xr, xr, xl, xl]), 2)   # repeat array twice
    y = np.tile(np.array([yl, yr, yl, yr]), 2)   # repeat array twice
    z = np.concatenate((np.array([zl, zl, zl, zl]),
                        np.array([zr, zr, zr, zr])))

    rot = np.deg2rad(rot)

    xr = np.cos(rot)*x - np.sin(rot)*y
    yr = np.sin(rot)*x + np.cos(rot)*y

    return mlab.triangular_mesh(xr, yr, z, triangles, color=color,
                                opacity=opacity)


def free_memory(*objects):
    """Delete objects.

    Delete and free memory if object exists and no other references than the
    given variable name point to it.

    objects: comma separated variables nams given as strings
    Example: free_memory("x", "y", "z")
    """
    for obj in objects:
        if obj in globals():
            del globals()[obj]


# ------------------------------------------------------------------------------
# set parameters
# ------------------------------------------------------------------------------
# relative index of refraction
n = 1.54 / 1.0
# angle of incidence in degrees
chi_deg = 45.0
# inclination of the interface with respect to the x-axis
inc_deg = 90 - chi_deg
# cut off borders of data (remove PML layer up to and including line source
# placement)
cutoff = 30


# ------------------------------------------------------------------------------
# import data from HDF file(s)
# ------------------------------------------------------------------------------
path = "simulations/meep-C/LaguerreGauss3d_C-out/"

filename_real = path + "e_real2_s-000001540.h5"
filename_imag = path + "e_imag2_s-000001540.h5"

with h5py.File(filename_real, 'r') as hf:
    # print("keys: %s" % hf.keys())
    data_real = hf[list(hf.keys())[0]][:]

with h5py.File(filename_imag, 'r') as hf:
    # print("keys: %s" % hf.keys())
    data_imag = hf[list(hf.keys())[0]][:]

# choose whether to use the electric field energy density (proportional to
# 'data_real') or the complex modulus of the complex electric field
# (proportional to 'data_real + data_imag') as data basis:
#data = data_real
data = data_real + data_imag

# free memory early
free_memory("data_real", "data_imag")

orig_shape = np.shape(data)

print("file size in MB: ", data.nbytes / 1024 / 1024)
print("data (max, min): ", (data.max(), data.min()))
print("original shape:  ", orig_shape)

data = data[cutoff:-cutoff, cutoff:-cutoff, cutoff:-cutoff] / data.max()
new_shape = np.shape(data)


# ------------------------------------------------------------------------------
# visualising an iso-contour surface of the vortex beam
# ------------------------------------------------------------------------------
fig = mlab.figure(1, bgcolor=(0, 0, 0), size=(400, 400))
# TODO: fix the following line
#fig.scene.render_window.aa_frames = 8               # antialiasing

sx, sy, sz = np.array([5, 5, 3.5]) * (np.asarray(new_shape)
                                      / np.asarray(orig_shape))
SX, SY, SZ = np.mgrid[-sx/2.0:sx/2.0:eval('{}j'.format(data.shape[0])),
                      -sy/2.0:sy/2.0:eval('{}j'.format(data.shape[1])),
                      -sz/2.0:sz/2.0:eval('{}j'.format(data.shape[2]))]

src = mlab.pipeline.scalar_field(SX, SY, SZ, data)  # Mayavi source

# free memory early
free_memory("data", "SX", "SY", "SZ")

# volume of interest
voi = mlab.pipeline.extract_grid(src)

# halving volume at plane of incidence (comment out to visualise entire beam)
voi.set(z_max=np.ceil(new_shape[2]/2))

VMAX = 0.02
iso = mlab.pipeline.iso_surface(voi, colormap="hot", contours=[VMAX])
iso.module_manager.scalar_lut_manager.use_default_range = False
iso.module_manager.scalar_lut_manager.data_range = [0.0, VMAX]

# add two cut planes to show the transverse intensity distributions
pipe_inc = mlab.pipeline.image_plane_widget(src, plane_orientation='x_axes',
                                            slice_index=0, colormap='hot',
                                            vmin=0.0, vmax=VMAX)
pipe_ref = mlab.pipeline.image_plane_widget(src, plane_orientation='y_axes',
                                            slice_index=new_shape[1],
                                            colormap='hot',
                                            vmin=0.0, vmax=VMAX)


# ------------------------------------------------------------------------------
# visualising k-vectors of central plane waves accor. to classical geom. optics
# ------------------------------------------------------------------------------
# angle of refraction in radians
eta_rad = np.arcsin((1.0/n) * np.sin(np.deg2rad(chi_deg)))

# properties of the k-vectors
vec_length = 3.5
vec_radius = 0.008
vec_color = (1, 0, 0)

# components of the k-vectors
inc = (-vec_length, 0, 0)
ref = (vec_length * np.sin(np.deg2rad(chi_deg - inc_deg)),
       vec_length * np.cos(np.deg2rad(chi_deg - inc_deg)), 0)
tra = (vec_length * np.sin(eta_rad + np.deg2rad(inc_deg)),
       -vec_length * np.cos(eta_rad + np.deg2rad(inc_deg)), 0)

components = [inc, ref, tra]

# visualise incident central k-vector
vector = mlab.quiver3d(0, 0, 0, *components[0], color=vec_color,
                       scale_factor=1, mode='cylinder', resolution=25)
vector.glyph.glyph_source.glyph_source.radius = vec_radius

# visualise secondary central k-vectors
for i in [1, 2]:
    vectors = mlab.quiver3d(0, 0, 0, *components[i], color=vec_color,
                            scale_factor=1, mode='arrow', resolution=25)
    vectors.glyph.glyph_source.glyph_source.shaft_radius = vec_radius
    vectors.glyph.glyph_source.glyph_source.tip_radius = 0.025
    vectors.glyph.glyph_source.glyph_source.tip_length = 0.13

# visualise optically denser material
color_overlay = (0.227, 0.188, 0.188)
cuboid(ext_grid=(-sx/2.0, sx/2.0, -sy/1.5, 0, -sz/2.0, sz/2.0), rot=inc_deg,
       color=color_overlay, opacity=0.5)


# mlab.orientation_axes()
mlab.view(azimuth=-inc_deg, elevation=0, distance=12, focalpoint='auto')
fig.scene.camera.azimuth(20)
fig.scene.camera.elevation(30)

# mlab.savefig('visualize_field.png')
mlab.show()

free_memory("src", "vol", "voi", "pipe_inc", "pipe_ref", "iso", "SX", "SY",
            "SZ")

#!/usr/bin/env python3 # -*- coding: utf-8 -*- file:   plot_3d_mayavi.py brief:  Python script to visualise (intensity) isosurfaces of scattered vortex beams.         The program uses as input HDF5 files generated by the FDTD solver Meep. author: <NAME> date:   19.01.2018 Return a cuboid.      The cuboid is defined on the grid 'ext_grid' with an angle of rotation     'rot' about the z-axis given in degrees. This cuboid is ment to represent     the optically denser material. # repeat array twice # repeat array twice Delete objects.      Delete and free memory if object exists and no other references than the     given variable name point to it.      objects: comma separated variables nams given as strings     Example: free_memory("x", "y", "z") # ------------------------------------------------------------------------------ # set parameters # ------------------------------------------------------------------------------ # relative index of refraction # angle of incidence in degrees # inclination of the interface with respect to the x-axis # cut off borders of data (remove PML layer up to and including line source # placement) # ------------------------------------------------------------------------------ # import data from HDF file(s) # ------------------------------------------------------------------------------ # print("keys: %s" % hf.keys()) # print("keys: %s" % hf.keys()) # choose whether to use the electric field energy density (proportional to # 'data_real') or the complex modulus of the complex electric field # (proportional to 'data_real + data_imag') as data basis: #data = data_real # free memory early # ------------------------------------------------------------------------------ # visualising an iso-contour surface of the vortex beam # ------------------------------------------------------------------------------ # TODO: fix the following line #fig.scene.render_window.aa_frames = 8               # antialiasing # Mayavi source # free memory early # volume of interest # halving volume at plane of incidence (comment out to visualise entire beam) # add two cut planes to show the transverse intensity distributions # ------------------------------------------------------------------------------ # visualising k-vectors of central plane waves accor. to classical geom. optics # ------------------------------------------------------------------------------ # angle of refraction in radians # properties of the k-vectors # components of the k-vectors # visualise incident central k-vector # visualise secondary central k-vectors # visualise optically denser material # mlab.orientation_axes() # mlab.savefig('visualize_field.png')