Tutorial: Own field generators

In this quick tutorial we demonstrate how to implement a user-defined fundamental field. We will do this at the example of two focused beams with different focal spot. All other parameters will be equal for both beams (spotsize, amplitude, phase …)

We first load some modules

In [1]:
import numpy as np
import matplotlib.pyplot as plt

from pyGDM2 import fields
from pyGDM2 import tools
from pyGDM2 import visu

Implement the field generator

We implement the own field generator function calle “double_focused”. Internally, it will call focused_planewave twice with different focal positions.

In [2]:
## ---------- Setup incident field
def double_focused(struct, wavelength, theta,
                   xSpot1, ySpot1, xSpot2, ySpot2,
                   spotsize=-1, kSign=-1):
    """Two focused beams, based on `fields.focused_planewave`
    from pyGDM2 import fields

    ## --- evaluate focused beam twice using the two focal positions
    Efield1 = fields.focused_planewave(struct, wavelength, theta,
                                       xSpot1, ySpot1,
                                       spotsize=spotsize, kSign=kSign)
    Efield2 = fields.focused_planewave(struct, wavelength, theta,
                                       xSpot2, ySpot2,
                                       spotsize=spotsize, kSign=kSign)

    ## --- get the complex x/y/z field components as individual arrays for summation
    Ex1, Ey1, Ez1 = Efield1.T
    Ex2, Ey2, Ez2 = Efield2.T

    ## --- add both beams
    Ex = Ex1 + Ex2
    Ey = Ey1 + Ey2
    Ez = Ez1 + Ez2

    ## --- return as fortran array to avoid copying of arrays in memory
    return np.asfortranarray(np.transpose([Ex, Ey, Ez]))

Evaulate the new field generator in a test frame

Now we setup a test-frame in which we evaluate the new double-spot field as well as a single focused plane wave for comparison. We will plot the X-polarized field in the XZ plane.

In [3]:
## --- single focused beam
field_generator_1beam = fields.focused_planewave
kwargs1 = dict(theta=0, spotsize=200,
               xSpot=0, ySpot=0)

## --- double beam
field_generator_2beams = double_focused
kwargs2 = dict(theta=0, spotsize=200,
               xSpot1=-500, ySpot1=0,
               xSpot2=500, ySpot2=0)

## --- test-setting for field-evaluation
n3 = 1.0
n2 = 1.0
n1 = 1.0
spacing = 5000.0

## --- 2D evaluation volume (plane)
projection = 'XZ'
r_probe = tools.generate_NF_map(-1000,1000,50, -500,1000,50,0, projection=projection)

## --- evaluate the field-generators on the test XZ-map
wavelength = 800
NF_1beam = tools.evaluate_incident_field(field_generator_1beam, wavelength, kwargs1, r_probe,
                                   n1=n1,n2=n2,n3=n3, spacing=spacing)
NF_2beams = tools.evaluate_incident_field(field_generator_2beams, wavelength, kwargs2, r_probe,
                                   n1=n1,n2=n2,n3=n3, spacing=spacing)

Plot the result of the field-generator

In [4]:
plt.subplot(121, aspect='equal')
v = visu.vectorfield(NF_1beam, complex_part='real', projection=projection,
                     tit=projection+' real part, 1 beam', show=0)

plt.subplot(122, aspect='equal')
v = visu.vectorfield(NF_2beams, complex_part='real', projection=projection,
                     tit=projection+' real part, 2 beams', show=0)