MATLAB_Webinar

Embed Size (px)

Citation preview

  • 8/9/2019 MATLAB_Webinar

    1/30

    Click to edit Master title style

    MATLAB Integration with Zemax

    PresenterRhys Poolman

  • 8/9/2019 MATLAB_Webinar

    2/30

    Introduction- How MATLAB and Zemax communicate.

    What is a DDE.

    Linking MATLAB and Zemax- Three important MATLAB commands.

    Zemax Data Items.

    Testing Connection Examining MATLAB code.

    How to use code to link with Zemax.

    Overview

  • 8/9/2019 MATLAB_Webinar

    3/30

    Extracting Zemax Data

    Loading a lens file (.ZMX).

    Extracting data for analysis in MATLAB.

    Modifying Zemax Data

    Making Changes to a lens file.

    Updating Zemax instance.

    Accessing Zemax Functions

    Tracing rays

    Overview

  • 8/9/2019 MATLAB_Webinar

    4/30

    MATLAB: Powerful numerical computing

    package

    Useful for post-processing Zemax simulations

    Larger range of mathematics and visualisation

    commands

    MATLAB and Zemax talk with DynamicData Exchange (DDE)

    Introduction- MATLAB and Zemax

  • 8/9/2019 MATLAB_Webinar

    5/30

    DDE is a method for applications to passinformation to each other.

    Client sends data and processing instructions.

    Server returns processed data. Zemax is server.

    DDE is equivalent to a conversion

    Client (MATLAB) starts conversion by askingserver (Zemax) to do something.

    Server replies

    Introduction- DDE

  • 8/9/2019 MATLAB_Webinar

    6/30

    DDE is an obsolete method of

    interprocess communication.

    Superseded by COM and .NET.

    Zemax still uses it because its so easy!

    IMPORTANT: MATLAB no longer supports

    or develops DDE methods.

    DDE issues with Zemax and 64-bit MATLAB.

    32-bit works fine!

    Important Comment on DDE

  • 8/9/2019 MATLAB_Webinar

    7/30

    Only three MATLAB commands are

    required:

    ddeint

    Opens a channel between MATLAB and Zemax.

    ddereq

    Sends requests to Zemax (server).

    ddeterm Closes the channel between the two applications.

    The DDE Link

  • 8/9/2019 MATLAB_Webinar

    8/30

    The syntax for the DDE initialisation

    command:channel = ddeinit(service',topic');

    The DDE Link- ddeint

    The application

    to act as the

    server, in our

    case Zemax.

    A specific file, Zemax

    only opens one file

    per instance so leave

    blank.

    A reference number

    to pass the channel

    to the other three

    Commands.

    Our DDE initialisation command is

    channel = ddeinit(ZEMAX',');

  • 8/9/2019 MATLAB_Webinar

    9/30

    The syntax for the DDE request command:result = ddereq(channel,item, format, timeout);

    The DDE Link- ddereq

    Optional: Format of

    input and output in 2

    element array; 1

    string, 0 numeric,

    default [1, 0];

    Optional: Wait time,

    default 3000 ms.

    Zemax Data Item

    Passed as a string.

    A full list of Data

    Items in manualin chapter 26

  • 8/9/2019 MATLAB_Webinar

    10/30

    The syntax for the DDE termination

    command:

    close = ddeterm(channel);

    closecontains a 0 indicating failure and 1

    indicating success.

    The DDE Link- ddeterm

  • 8/9/2019 MATLAB_Webinar

    11/30

  • 8/9/2019 MATLAB_Webinar

    12/30

    Last argument is format specifier.

    Two integer elements [input, output] indicate

    whether string or numeral is expected.

    GetName outputs string so [1,1] is used.

    Numerical output [1,0] is default.

    General data item string format:DataItem, argument_1, argument2, , argument_n

    Important: MATLAB wont pick up on this

    comma if its missing!

    Zemax Data Items

  • 8/9/2019 MATLAB_Webinar

    13/30

    This is a simple example to get a lens title

    % Template MATLAB-ZEMAX DDE program

    clear all% removes all variables

    % initialize DDE connection

    channel = ddeinit('ZEMAX','');

    % Main body of script. All commands are passed to ZEMAX using data items in

    % chapter 26 using the ddereq MATLAB command.

    name = ddereq(channel,'GetName', [1,1]);

    disp(name);

    % terminate DDE connetion

    close = ddeterm(channel);

    First Example- Test Connection

    First command opens

    DDE channel for use

    with ddereq andddeinit commands.

    Second command gets

    the lens title displayedin the Title/Notes tab

    of the General dialogue

    box.Final command close

    DDE channel.

  • 8/9/2019 MATLAB_Webinar

    14/30

    Open the 32-bit version of MATLAB and

    load a lens file with a lens with a Lens Title,

    i.e. led_model.zmx.

    Run code in MATLAB by pressing F5 in the

    Editor.

    Name should be filed with Lens Title and

    displayed in Command window, in this case

    LED Polar Data.

    First Example- Test Connection

  • 8/9/2019 MATLAB_Webinar

    15/30

    Loading a .ZMX file

    Need file path (GetPath)

    Load lens (LoadFile)

    Second Example - Data Extraction

    % zemax data item to get path

    paths = ddereq(channel, 'GetPath', [1,1]);

    % MATLAB data processing

    modify = strrep(paths, ',', ' '); % replaces comma with space

    [data_path lens_path] = strtok(modify); % splits paths into two variables

    % zemax data item to load lens file

    item = strcat('LoadFile,', lens_path, ...

    '\Non-sequential\Sources\led_model.zmx' );

    load = ddereq(channel, item);

    Returns contents of Data box

    and Lens box in Folders tab of

    preferences dialogue, both

    of which strings so [1,1] is needed.

    Sends data item constructed by strcat

    MATLAB function to load lens file into

    Zemax instance memory NOT into LDE.

  • 8/9/2019 MATLAB_Webinar

    16/30

    What if we move the source around inside thedetector? Need to know the detector size (GetNSCParameter).

    Need to know the detector position (GetNSCPostion).

    Second Example - Data Extraction

    % Zemax data item to get detector

    radius

    surf = num2str(1);

    object = num2str(4);

    parameter = num2str(2);

    item = strcat('GetNSCParameter, ',...

    surf, ', ', object, ', ', parameter);

    detector_radius = ...

    ddereq(channel,item);

    % Zemax data item to get detector

    position

    surf = num2str(1);

    object = num2str(1);

    item = strcat('GetNSCPosition,',...

    surf, ', ', object);

    detector_position = ...

    ddereq(channel, item, [1,1]);

    Polar Detector Radius Source Position

  • 8/9/2019 MATLAB_Webinar

    17/30

    Second Example - Data Extraction

    Data processing using MATLABcommands:

    % MATLAB data processing

    modify = strrep(detector_position, ',', ' ');position_data = textscan(modify, '%f %f %f %f %f %f %s');

    x = position_data{1};

    y = position_data{2};

    z = position_data{3};

    x_tilt = position_data{4};

    y_tilt = position_data{5};z_tilt = position_data{6};

    material = position_data{7};

  • 8/9/2019 MATLAB_Webinar

    18/30

    Return processing is messy and makes

    reading code difficult.

    Can use MATLAB functions to execute

    specific ddereq calls e.g.

    Executes both GetNSCPosition ddereqand puts data into individual variables.

    Zemax DDE Toolbox for MATAB

    Aside- MATLAB Functions

    [ x, y, z, x_tilt, y_tilt, z_tilt, material ] =

    GetNSCPosition( channel, surf, object )

  • 8/9/2019 MATLAB_Webinar

    19/30

    We know:

    range of movement for source (polar detector

    radius).

    starting position (object 1 position).

    Loop over a range of positions using

    SetNSCPosition

    Update non-sequential component editor.

    Return source to starting point.

    Third Example- Modifying Lens

  • 8/9/2019 MATLAB_Webinar

    20/30

    To change lens position use

    SetNSCPosition in for loop

    Source doesnt move in Non-sequentialcomponent editor, why?

    Third Example- Moving Source

    object = num2str(1);

    code = num2str(3); % alters z position

    forii = z-detector_radius/2:detector_radius/10:z+detector_radius/2;% Zemax data item to alter the source assembly position

    data = num2str(ii);

    item = ['SetNSCPosition, ', surf, ', ', object, ', ', code, ', ', data];

    result = ddereq(channel, item);

    end

  • 8/9/2019 MATLAB_Webinar

    21/30

    PushLens updates Zemax editors with

    contents of sever memory.

    Need to tell Zemax this is OK:

    Third Example- Modify Lens File

  • 8/9/2019 MATLAB_Webinar

    22/30

    Use the PushLens data item inside the for

    loop

    Changes the source position in the Non-sequential component editor.

    Third Example- PushLens

    % Zemax data item to update the non-sequential component editor

    error = ddereq(channel, 'PushLens, 1');iferror

    disp(['PushLens failed to update editor for z = ',...

    num2str(ii), 'mm']);

    end

  • 8/9/2019 MATLAB_Webinar

    23/30

    Were altering the design from MATLAB.

    Lets see what effect that has Use TraceRays

    Use NSCDetectorData

    Order is Clear Detectors -> Trace Rays ->

    Recover Detector Data

    Forth Example- Trace Rays

  • 8/9/2019 MATLAB_Webinar

    24/30

    Add NSCDetectorData to for loop after

    push lens:

    Also need to recover data after ray trace:

    Forth Example- Detector Data

    % Zemax data item to clear detector

    item = 'NSCDetectorData, 1, 0, 0, 0';

    data = ddereq(channel, item); This zero indicates to Zemaxto clear all detectors, just as

    NSDD optimisation operand.

    % Zemax data item to get RMS angular width

    item = ['NSCDetectorData, ', surf, ', 4, 0, 0'];total_power(jj) = ddereq(channel, item);

    jj = jj + 1; 4- Detector object number

    0- Total flux in

    0- position space.

  • 8/9/2019 MATLAB_Webinar

    25/30

  • 8/9/2019 MATLAB_Webinar

    26/30

    Two data Items need to set source back to

    original position.

    SetNSCPosition with data set to original z

    value.

    PushLens to change non-sequential

    component editor.

    Use MATLAB plot command to plot totalpower as a function of source to detector

    distance.

    Forth Example- Return Source

  • 8/9/2019 MATLAB_Webinar

    27/30

    Results from MATLAB

  • 8/9/2019 MATLAB_Webinar

    28/30

    Flashy Results from MATLAB!

  • 8/9/2019 MATLAB_Webinar

    29/30

    You need three MATLAB commands:

    ddeinit to initiate link with MATLAB

    ddereq to tell Zemax what to do from MATLAB

    ddeterm to terminate the link

    Can use link to analyse Zemax results.

    Take advantage of MATLAB mathematicaland plotting libraries.

    Conclusion

  • 8/9/2019 MATLAB_Webinar

    30/30

    Please direct questions to:

    [email protected]

    [email protected]

    Ill be answering question for the next 30

    minutes.

    Contact Details

    mailto:[email protected]:[email protected]:[email protected]:[email protected]