12.07.2015 Views

Click to edit Master title style - Zemax

Click to edit Master title style - Zemax

Click to edit Master title style - Zemax

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Click</strong> <strong>to</strong> <strong>edit</strong> <strong>Master</strong> <strong>title</strong> <strong>style</strong>MATLAB Integration with <strong>Zemax</strong>Presenter – Rhys Poolman


Overview• Extracting <strong>Zemax</strong> Data– Loading a lens file (.ZMX).– Extracting data for analysis in MATLAB.• Modifying <strong>Zemax</strong> Data– Making Changes <strong>to</strong> a lens file.– Updating <strong>Zemax</strong> instance.• Accessing <strong>Zemax</strong> Functions– Tracing rays


Introduction- MATLAB and <strong>Zemax</strong>• MATLAB: Powerful numerical computingpackage– Useful for post-processing <strong>Zemax</strong> simulations– Larger range of mathematics and visualisationcommands• MATLAB and <strong>Zemax</strong> talk with DynamicData Exchange (DDE)


Introduction- DDE• DDE is a method for applications <strong>to</strong> passinformation <strong>to</strong> each other.– Client sends data and processing instructions.– Server returns processed data.– <strong>Zemax</strong> is server.• DDE is equivalent <strong>to</strong> a conversion– Client (MATLAB) starts conversion by askingserver (<strong>Zemax</strong>) <strong>to</strong> do something.– Server replies


Important Comment on DDE• DDE is an obsolete method ofinterprocess communication.– Superseded by COM and .NET.• <strong>Zemax</strong> still uses it because it’s so easy!• IMPORTANT: MATLAB no longer supportsor develops DDE methods.– DDE issues with <strong>Zemax</strong> and 64-bit MATLAB.– 32-bit works fine!


The DDE Link• Only three MATLAB commands arerequired:– ddeint• Opens a channel between MATLAB and <strong>Zemax</strong>.– ddereq• Sends requests <strong>to</strong> <strong>Zemax</strong> (server).– ddeterm• Closes the channel between the two applications.


The DDE Link- ddeint• The syntax for the DDE initialisationcommand:channel = ddeinit(‘service',‘<strong>to</strong>pic');A reference number<strong>to</strong> pass the channel<strong>to</strong> the other threeCommands.The application<strong>to</strong> act as theserver, in ourcase <strong>Zemax</strong>.A specific file, <strong>Zemax</strong>only opens one fileper instance so leaveblank.• Our DDE initialisation command ischannel = ddeinit(‘ZEMAX',‘');


The DDE Link- ddereq• The syntax for the DDE request command:result = ddereq(channel,‘item‘, format, timeout);• A full list of DataItems in manualin chapter 26<strong>Zemax</strong> Data ItemPassed as a string.Optional: Format ofinput and output in 2element array; 1string, 0 numeric,default [1, 0];Optional: Wait time,default 3000 ms.


The DDE Link- ddeterm• The syntax for the DDE terminationcommand:close = ddeterm(channel);• close contains a 0 indicating failure and 1indicating success.


<strong>Zemax</strong> Data Items• The slide on the ddereq command mention“Data Items”:result = ddereq(channel,‘item‘, format, timeout);• These are <strong>Zemax</strong> commands passed asstrings from MATLAB <strong>to</strong> <strong>Zemax</strong> GetName isa simple example:result = ddereq(channel,‘GetName‘, [1,1]);


<strong>Zemax</strong> Data Items• Last argument is format specifier.– Two integer elements [input, output] indicatewhether 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 won’t pick up on thiscomma if its missing!


First Example- Test Connection• This is a simple example <strong>to</strong> get a lens <strong>title</strong>% Template MATLAB-ZEMAX DDE programclear all % removes all variables% initialize DDE connectionchannel = ddeinit('ZEMAX','');First command opensDDE channel for usewith ddereq andddeinit commands.% Main body of script. All commands are passed <strong>to</strong> ZEMAX using data items in% chapter 26 using the ddereq MATLAB command.name = ddereq(channel,'GetName', [1,1]);disp(name);% terminate DDE connetionclose = ddeterm(channel);Final command closeDDE channel.Second command getsthe lens <strong>title</strong> displayedin the Title/Notes tabof the General dialoguebox.


Second Example - Data Extraction• Loading a .ZMX file– Need file path (GetPath)– Load lens (LoadFile)Returns contents of Data boxand Lens box in Folders tab ofpreferences dialogue, bothof which strings so [1,1] is needed.% zemax data item <strong>to</strong> get pathpaths = ddereq(channel, 'GetPath', [1,1]);% MATLAB data processingmodify = strrep(paths, ',', ' '); % replaces comma with space[data_path lens_path] = str<strong>to</strong>k(modify); % splits paths in<strong>to</strong> two variables% zemax data item <strong>to</strong> load lens fileitem = strcat('LoadFile,', lens_path, ...'\Non-sequential\Sources\led_model.zmx');load = ddereq(channel, item);Sends data item constructed by strcatMATLAB function <strong>to</strong> load lens file in<strong>to</strong><strong>Zemax</strong> instance memory NOT in<strong>to</strong> LDE.


Second Example - Data Extraction• What if we move the source around inside thedetec<strong>to</strong>r?– Need <strong>to</strong> know the detec<strong>to</strong>r size (GetNSCParameter).– Need <strong>to</strong> know the detec<strong>to</strong>r position (GetNSCPostion).Polar Detec<strong>to</strong>r Radius% <strong>Zemax</strong> data item <strong>to</strong> get detec<strong>to</strong>rradiussurf = num2str(1);object = num2str(4);parameter = num2str(2);item = strcat('GetNSCParameter, ',...surf, ', ', object, ', ', parameter);detec<strong>to</strong>r_radius = ...ddereq(channel,item);Source Position% <strong>Zemax</strong> data item <strong>to</strong> get detec<strong>to</strong>rpositionsurf = num2str(1);object = num2str(1);item = strcat('GetNSCPosition,',...surf, ', ', object);detec<strong>to</strong>r_position = ...ddereq(channel, item, [1,1]);


Second Example - Data Extraction• Data processing using MATLABcommands:% MATLAB data processingmodify = strrep(detec<strong>to</strong>r_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 makesreading code difficult.• Can use MATLAB functions <strong>to</strong> executespecific ddereq calls e.g.[ x, y, z, x_tilt, y_tilt, z_tilt, material ] =GetNSCPosition( channel, surf, object )• Executes both GetNSCPosition ddereqand puts data in<strong>to</strong> individual variables.• <strong>Zemax</strong> DDE Toolbox for MATAB


Third Example- Modifying Lens• We know:– range of movement for source (polar detec<strong>to</strong>rradius).– starting position (object 1 position).• Loop over a range of positions usingSetNSCPosition• Update non-sequential component edi<strong>to</strong>r.• Return source <strong>to</strong> starting point.


Third Example- Moving Source• To change lens position useSetNSCPosition in for loopobject = num2str(1);code = num2str(3); % alters z positionfor ii = z-detec<strong>to</strong>r_radius/2:detec<strong>to</strong>r_radius/10:z+detec<strong>to</strong>r_radius/2;% <strong>Zemax</strong> data item <strong>to</strong> alter the source assembly positiondata = num2str(ii);item = ['SetNSCPosition, ', surf, ', ', object, ', ', code, ', ', data];result = ddereq(channel, item);end• Source doesn’t move in Non-sequentialcomponent edi<strong>to</strong>r, why?


Third Example- Modify Lens File• PushLens updates <strong>Zemax</strong> edi<strong>to</strong>rs withcontents of sever memory.• Need <strong>to</strong> tell <strong>Zemax</strong> this is OK:


Third Example- PushLens• Use the PushLens data item inside the forloop% <strong>Zemax</strong> data item <strong>to</strong> update the non-sequential component edi<strong>to</strong>rerror = ddereq(channel, 'PushLens, 1');if errordisp(['PushLens failed <strong>to</strong> update edi<strong>to</strong>r for z = ',...num2str(ii), 'mm']);end• Changes the source position in the Nonsequentialcomponent edi<strong>to</strong>r.


Forth Example- Trace Rays• We’re altering the design from MATLAB.• Lets see what effect that has– Use TraceRays– Use NSCDetec<strong>to</strong>rData• Order is Clear Detec<strong>to</strong>rs -> Trace Rays ->Recover Detec<strong>to</strong>r Data


Forth Example- Detec<strong>to</strong>r Data• Add NSCDetec<strong>to</strong>rData <strong>to</strong> for loop afterpush lens:% <strong>Zemax</strong> data item <strong>to</strong> clear detec<strong>to</strong>ritem = 'NSCDetec<strong>to</strong>rData, 1, 0, 0, 0'; This zero indicates <strong>to</strong> <strong>Zemax</strong>data = ddereq(channel, item);<strong>to</strong> clear all detec<strong>to</strong>rs, just asNSDD optimisation operand.• Also need <strong>to</strong> recover data after ray trace:% <strong>Zemax</strong> data item <strong>to</strong> get RMS angular widthitem = ['NSCDetec<strong>to</strong>rData, ', surf, ', 4, 0, 0'];<strong>to</strong>tal_power(jj) = ddereq(channel, item);jj = jj + 1;4- Detec<strong>to</strong>r object number0- Total flux in…0- position space.


Forth Example- Trace Rays• Between the two NSCDetec<strong>to</strong>rData dataitems a TraceRays operand is required.% <strong>Zemax</strong> data item <strong>to</strong> trace rays throughitem = ['NSCTrace, ', surf, ', ', source, ', 0, 0, 0, 1, 1'];error = ddereq(channel, item);if errordisp('ray trace errors');endTranslation:‘, splitting off, scattering off, polarization off, ignore errors on, random ray trace on’


Forth Example- Return Source• Two data Items need <strong>to</strong> set source back <strong>to</strong>original position.– SetNSCPosition with data set <strong>to</strong> original zvalue.– PushLens <strong>to</strong> change non-sequentialcomponent edi<strong>to</strong>r.• Use MATLAB plot command <strong>to</strong> plot <strong>to</strong>talpower as a function of source <strong>to</strong> detec<strong>to</strong>rdistance.


Results from MATLAB


Flashy Results from MATLAB!


Conclusion• You need three MATLAB commands:– ddeinit <strong>to</strong> initiate link with MATLAB– ddereq <strong>to</strong> tell <strong>Zemax</strong> what <strong>to</strong> do from MATLAB– ddeterm <strong>to</strong> terminate the link• Can use link <strong>to</strong> analyse <strong>Zemax</strong> results.• Take advantage of MATLAB mathematicaland plotting libraries.


Contact Details• Please direct questions <strong>to</strong>:– zemaxsupport@radiantzemax.com– eusupport@radiantzemax.com• I’ll be answering question for the next 30minutes.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!