Sie sind auf Seite 1von 30

Click to edit Master title style

MATLAB Integration with Zemax


Presenter Rhys Poolman

Overview
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
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

Introduction- MATLAB and Zemax


MATLAB: Powerful numerical computing
package
Useful for post-processing Zemax simulations
Larger range of mathematics and visualisation
commands

MATLAB and Zemax talk with Dynamic


Data Exchange (DDE)

Introduction- DDE
DDE is a method for applications to pass
information 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 asking
server (Zemax) to do something.
Server replies

Important Comment on DDE


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!

The DDE Link


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- ddeint


The syntax for the DDE initialisation
command:
channel = ddeinit(service',topic');

A reference number
to pass the channel
to the other three
Commands.

The application
to act as the
server, in our
case Zemax.

A specific file, Zemax


only opens one file
per instance so leave
blank.

Our DDE initialisation command is


channel = ddeinit(ZEMAX',');

The DDE Link- ddereq


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

Zemax Data Item


Passed as a string.

A full list of Data


Items in manual
in chapter 26

Optional: Wait time,


default 3000 ms.

Optional: Format of
input and output in 2
element array; 1
string, 0 numeric,
default [1, 0];

The DDE Link- ddeterm


The syntax for the DDE termination
command:
close = ddeterm(channel);

close contains a 0 indicating failure and 1


indicating success.

Zemax Data Items


The slide on the ddereq command mention
Data Items:
result = ddereq(channel,item, format, timeout);

These are Zemax commands passed as


strings from MATLAB to Zemax GetName is
a simple example:
result = ddereq(channel,GetName, [1,1]);

Zemax Data Items


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!

First Example- Test Connection


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','');

First command opens


DDE channel for use
with ddereq and
ddeinit commands.

% Main body of script. All commands are passed to ZEMAX using data items in
% chapter 26 using the ddereq MATLAB command.
Second command gets
name = ddereq(channel,'GetName', [1,1]);
the lens title displayed
disp(name);
% terminate DDE connetion
close = ddeterm(channel);

in the Title/Notes tab


of the General dialogue
Final command close box.
DDE channel.

First Example- Test Connection


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.

Second Example - Data Extraction


Loading a .ZMX file
Need file path (GetPath)
Load lens (LoadFile)

Returns contents of Data box


and Lens box in Folders tab of
preferences dialogue, both
of which strings so [1,1] is needed.

% 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);

Sends data item constructed by strcat


MATLAB function to load lens file into
Zemax instance memory NOT into LDE.

Second Example - Data Extraction


What if we move the source around inside the
detector?
Need to know the detector size (GetNSCParameter).
Need to know the detector position (GetNSCPostion).
Polar Detector Radius

Source Position

% 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]);

Second Example - Data Extraction


Data processing using MATLAB
commands:
% 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};

Aside- MATLAB Functions


Return processing is messy and makes
reading code difficult.
Can use MATLAB functions to execute
specific ddereq calls e.g.
[ x, y, z, x_tilt, y_tilt, z_tilt, material ] =
GetNSCPosition( channel, surf, object )

Executes both GetNSCPosition ddereq


and puts data into individual variables.
Zemax DDE Toolbox for MATAB

Third Example- Modifying Lens


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- Moving Source


To change lens position use
SetNSCPosition in for loop
object = num2str(1);
code = num2str(3); % alters z position
for ii = 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

Source doesnt move in Non-sequential


component editor, why?

Third Example- Modify Lens File


PushLens updates Zemax editors with
contents of sever memory.
Need to tell Zemax this is OK:

Third Example- PushLens


Use the PushLens data item inside the for
loop
% Zemax data item to update the non-sequential component editor
error = ddereq(channel, 'PushLens, 1');
if error
disp(['PushLens failed to update editor for z = ',...
num2str(ii), 'mm']);
end

Changes the source position in the Nonsequential component editor.

Forth Example- Trace Rays


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- Detector Data


Add NSCDetectorData to for loop after
push lens:
% Zemax data item to clear detector
item = 'NSCDetectorData, 1, 0, 0, 0'; This zero indicates to Zemax
data = ddereq(channel, item);
to clear all detectors, just as
NSDD optimisation operand.

Also need to recover data after ray trace:


% 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.

Forth Example- Trace Rays


Between the two NSCDetectorData data
items a TraceRays operand is required.
% Zemax data item to trace rays through
item = ['NSCTrace, ', surf, ', ', source, ', 0, 0, 0, 1, 1'];
error = ddereq(channel, item);
if error
disp('ray trace errors');
end
Translation:
, splitting off, scattering off, polarization off, ignore errors on, random ray trace on

Forth Example- Return Source


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 total


power as a function of source to detector
distance.

Results from MATLAB

Flashy Results from MATLAB!

Conclusion
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 mathematical
and plotting libraries.

Contact Details
Please direct questions to:
zemaxsupport@radiantzemax.com
eusupport@radiantzemax.com

Ill be answering question for the next 30


minutes.

Das könnte Ihnen auch gefallen