{"id":1667,"date":"2016-08-30T10:33:11","date_gmt":"2016-08-30T17:33:11","guid":{"rendered":"https:\/\/live-optics-wp.pantheonsite.io\/milster\/?page_id=1667"},"modified":"2016-09-19T10:00:17","modified_gmt":"2016-09-19T17:00:17","slug":"custom-mathematical-operations","status":"publish","type":"page","link":"https:\/\/wp.optics.arizona.edu\/milster\/resources\/optiscan-simulation-program\/optiscan-help-desk\/index\/custom-mathematical-operations\/","title":{"rendered":"Custom Mathematical Operations"},"content":{"rendered":"<p>Allows the user to augment the functionality of OPTISCAN through use of custom mathematical electric field calculations.<\/p>\n<h3>Overview:<\/h3>\n<p>In order to use a Custom Mathematical Operation object, you need to create an m-file which uses the following function declaration formation:<\/p>\n<pre><strong>function<\/strong> [mop, curlink, simdata, errmsg] = <em>function_name<\/em>(action, mop curlink, simdata);<\/pre>\n<p>if the function call is successful, the the function should set the output variable as follows:<\/p>\n<p><em>errmsg<\/em> = &#8221;;<\/p>\n<p>if the function call is not successful, and therefore the simulation should be stopped, then\u00a0<em>errmsg<\/em> should be set equal to a description of the error that occurred:<\/p>\n<p><em>errmsg<\/em> = &#8216;The Input Field Cannot be Zero&#8217;;<\/p>\n<p>Sometimes it is important for the mathematical operation to know what kind of input link is connected to it. This can be done by querying curlink for its linktype :<\/p>\n<pre>if <em>istransmitted<\/em>(curlink)\r\n %the input linktype is transmitted\r\n elseif <em>isreflected<\/em>(curlink)\r\n %the input linktype is reflected\r\n else\r\n %an electric field is not explicitly being transmitted to\r\n %the MOP object.\r\n end<\/pre>\n<p>For the following configuration:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1429\" src=\"http:\/\/wp.optics.arizona.edu\/milster\/wp-content\/uploads\/sites\/48\/2016\/08\/source-to-MOP.gif\" alt=\"source-to-MOP\" width=\"166\" height=\"41\" data-id=\"1429\" \/><\/p>\n<p>the<em> istransmitted<\/em>(curlink) function would return &#8220;true&#8221; since the input link is propagating transmitted electric fields to the MOP object. The electric fields can be retrieved through the simdata variable:<\/p>\n<p><strong>transmitted fields <\/strong><\/p>\n<p>Ext = simdata.Ext<br \/>\nEyt = simdata.Eyt<br \/>\nEzt = simdata.Ezt<\/p>\n<p><strong>reflected fields <\/strong><\/p>\n<p>Exr = simdata.Exr<br \/>\nEyt = simdata.Eyr<br \/>\nEzt = simdata.Ezr<\/p>\n<p>The ordinate and abcissa vectors can be similarly queried using:<\/p>\n<p>sysxvec = simdata.sysxvec; %x-positions of each column<br \/>\nsysyvec = simdata.sysyvec; %y-positions of each row<\/p>\n<p>After the function is finished modifying the fields and\/or the ordinate and abcissa vectors, it needs to update the simdata variable using:<\/p>\n<p>simdate.Ext = Ext;<br \/>\nsimdata.Eyt = Eyt;<br \/>\netc&#8230;.<\/p>\n<hr \/>\n<p>The MOP function may wish to save some information between invocations. The simdata variable allows the function to retrieve the custom &#8220;user data&#8221; that is associated with a specific MOP object by using:<\/p>\n<p>userdata = simdata.userdata<\/p>\n<pre>if isempty(userdata)\r\n %need to initialize the userdata\r\n userdata.sinvector = sin( linspace(0,2*pi,100);\r\n userdata.cosvector = cos( linspace(0,2*pi,100);\r\n end<\/pre>\n<p>After the custom &#8220;user data&#8221; is initialized and\/or updated, it can be stored by using:<\/p>\n<p>simdata.userdata = userdata;<\/p>\n<hr \/>\n<p>The following is a list of other simdata properties that may be useful:<\/p>\n<ul>\n<li>simdata.NHighNA &#8211; number of samples to use for High Numerical Aperature optics calculations.<\/li>\n<li>simdata.NLowNa &#8211; number of samples to use for Low Numerical Aperature optics calculations.<\/li>\n<li>simdata.LAMBDA &#8211; the current value for LAMBDA<\/li>\n<li>simdata.SourceType &#8211; 1 for coherent, 2 for incoherent.<\/li>\n<li>simdata.SourceTypePoints &#8211; number of source points for an incoherent source.<\/li>\n<li>simdata.PointCount &#8211; current source point number for an incoherent source.<\/li>\n<li>simdata.VisitCount &#8211; number of times that the current object has been calculated.<\/li>\n<\/ul>\n<p>The project directory may be determined using:<\/p>\n<p>project_path = fullfile(mop,&#8221;,&#8221;);<\/p>\n<p>Example &#8211; this simple <a href=\"http:\/\/wp.optics.arizona.edu\/milster\/wp-content\/uploads\/sites\/48\/2016\/08\/mymop.m\" target=\"_blank\">MOP function<\/a> calculates the Total Aerial Irradiance of the input fields and stores it in Ext:<\/p>\n<pre>function [mop, curlink, simdata, errmsg] = mymop(action, mop, curlink, simdata)\r\n\r\nerrmsg = '';\r\n\r\n%retreive the desired electric fields:\r\n\r\nif istransmitted(curlink)\r\nex = simdata.Ext;\r\ney = simdata.Eyt;\r\nez = simdata.Ezt;\r\nelseif istransmitted(curlink)\r\nex = simdata.Exr;\r\ney = simdata.Eyr;\r\nez = simdata.Ezr;\r\nelse\r\nerrmsg = 'Need an Input Link!'; return;\r\nend;\r\n\r\n%store the calculated \"Total Aerial Irradiance\" in ex:\r\n\r\nex = 3*10^8*0.5*8.85e-12*(abs(ex).^2+abs(ey).^2+abs(ez).^2);\r\n\r\n%\r\n\r\nset the other fields to 0\r\n\r\ney = 0;\r\nez = 0;\r\n\r\n%update the source fieds\r\nsimdata.Ext = ex;\r\nsimdata.Eyt = ey;\r\nsimdata.Ezt = ez;\r\n\r\nsimdata.Exr = 0;\r\nsimdata.Eyr = 0;\r\nsimdata.Ezr = 0;<\/pre>\n<p>An example system where this script might be used is calculating the Total Aerial Irradiance of a source, even though the Source Editor can do this for you without any programming at all:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1421\" src=\"http:\/\/wp.optics.arizona.edu\/milster\/wp-content\/uploads\/sites\/48\/2016\/08\/mopsystem.gif\" alt=\"mopsystem\" width=\"319\" height=\"48\" data-id=\"1421\" \/><\/p>\n<p>Configurable Variables &#8211; A MOP function can be used in conjunction with a Delta Object. For more information about this advanced feature, see the information on Configurable Variables.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1420\" src=\"http:\/\/wp.optics.arizona.edu\/milster\/wp-content\/uploads\/sites\/48\/2016\/08\/mopsystem.2.gif\" alt=\"mopsystem.2\" width=\"280\" height=\"98\" data-id=\"1420\" \/> &#8211; Delta Object used with a MOP object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Allows the user to augment the functionality of OPTISCAN through use of custom mathematical electric field calculations. Overview: In order to use a Custom Mathematical Operation object, you need to create an m-file which uses the following function declaration formation: function [mop, curlink, simdata, errmsg] = function_name(action, mop curlink, simdata); if the function call is successful, the the function should<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":894,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1667","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/wp.optics.arizona.edu\/milster\/wp-json\/wp\/v2\/pages\/1667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.optics.arizona.edu\/milster\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/wp.optics.arizona.edu\/milster\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/wp.optics.arizona.edu\/milster\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.optics.arizona.edu\/milster\/wp-json\/wp\/v2\/comments?post=1667"}],"version-history":[{"count":8,"href":"https:\/\/wp.optics.arizona.edu\/milster\/wp-json\/wp\/v2\/pages\/1667\/revisions"}],"predecessor-version":[{"id":1906,"href":"https:\/\/wp.optics.arizona.edu\/milster\/wp-json\/wp\/v2\/pages\/1667\/revisions\/1906"}],"up":[{"embeddable":true,"href":"https:\/\/wp.optics.arizona.edu\/milster\/wp-json\/wp\/v2\/pages\/894"}],"wp:attachment":[{"href":"https:\/\/wp.optics.arizona.edu\/milster\/wp-json\/wp\/v2\/media?parent=1667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}