簡介:FRED作(zuò)為(wèi)COM組件可(kě)以實現與Excel、VB、Matlab等調用來(lái)完成龐大(dà)的計(jì)算(suàn)任務或畫(huà)圖,本文的目的是通(tōng)過運行(xíng)一個(gè)案例來(lái)實現與Matlab的相互調用,在此我們需要借助腳本來(lái)完成,此腳本為(wèi)視(shì)為(wèi)通(tōng)用型腳本。
配置:在執行(xíng)調用之前,我們需要在Matlab命令行(xíng)窗口輸入如下命令:
enableservice('AutomationServer', true)
enableservice('AutomationServer')
結果輸出為(wèi)1,這種操作(zuò)方式保證了當前的Matlab實體(tǐ)可(kě)以用于通(tōng)信。
在winwrp界面,為(wèi)增加和(hé)使用Matlab類型的目錄庫,我們需要如下步驟:
1. 在FRED腳本編輯界面找到參考.
2. 找到Matlab Automation Server Type Library
3. 将名字改為(wèi)MLAPP
在Matlab裏面有(yǒu)兩種常用的數(shù)據發送選項PutWorkspaceData 及PutFullMatrix,PutWorkspaceData适用于存儲一般的數(shù)據在工作(zuò)區(qū),并賦予其為(wèi)變量,PutFullMatrix試用于複數(shù)數(shù)據。
圖 編輯/參考
現在将腳本代碼公布如下,此腳本執行(xíng)如下幾個(gè)步驟:
1. 創建Matlab服務器(qì)。
2. 移動探測面對于前一聚焦面的位置。
3. 在探測面追迹光線
4. 在探測面計(jì)算(suàn)照度
5. 使用PutWorkspaceData發送照度數(shù)據到Matlab
6. 使用PutFullMatrix發送标量場(chǎng)數(shù)據到Matlab中
7. 用Matlab畫(huà)出照度數(shù)據
8. 在Matlab計(jì)算(suàn)照度平均值
9. 返回數(shù)據到FRED中
代碼分享:
Option Explicit
Sub Main
Dim ana As T_ANALYSIS
Dim move As T_OPERATION
Dim Matlab As MLApp.MLApp
Dim detNode As Long, detSurfNode As Long, anaSurfNode As Long
Dim raysUsed As Long, nXpx As Long, nYpx As Long
Dim irrad() As Double, imagData() As Double, reals() As Double, imags() As Double
Dim z As Double, xMin As Double, xMax As Double, yMin As Double, yMax As Double
Dim meanVal As Variant
Set Matlab = CreateObject("Matlab.Application")
ClearOutputWindow
'Find the node numbers for the entities being used.
detNode = FindFullName("Geometry.Screen")
detSurfNode = FindFullName("Geometry.Screen.Surf 1")
anaSurfNode = FindFullName("Analysis Surface(s).Analysis 1")
'Load the properties of the analysis surface being used.
LoadAnalysis anaSurfNode, ana
'Move the detector custom element to the desired z position.
z = 50
GetOperation detNode,1,move
move.Type = "Shift"
move.val3 = z
SetOperation detNode,1,move
Print "New screen position, z = " &z
'Update the model and trace rays.
EnableTextPrinting (False)
Update
DeleteRays
TraceCreateDraw
EnableTextPrinting (True)
'Calculate the irradiance for rays on the detector surface.
raysUsed = Irradiance( detSurfNode, -1, ana, irrad )
Print raysUsed & " rays were included in the irradiance calculation.
'When using real number data to send to MATLAB, it is simplest to use PutWorkspaceData.
Matlab.PutWorkspaceData("irradiance_pwd","base",irrad)
'PutFullMatrix is more useful when actually having complex data such as with
'scalar wavefield, for example. Note that the scalarfield array in MATLAB
'is a complex valued array.
raysUsed = ScalarField ( detSurfNode, -1, ana, reals, imags )
Matlab.PutFullMatrix("scalarfield","base", reals, imags )
Print raysUsed & " rays were included in the scalar field calculation."
'Calculate plot characteristics from the T_ANALYSIS structure. This information is used
'to customize the plot figure.
xMin = ana.posX+ana.AcellX*(ana.Amin-0.5)
xMax = ana.posX+ana.AcellX*(ana.Amax+0.5)
yMin = ana.posY+ana.BcellY*(ana.Bmin-0.5)
yMax = ana.posY+ana.BcellY*(ana.Bmax+0.5)
nXpx = ana.Amax-ana.Amin+1
nYpx = ana.Bmax-ana.Bmin+1
'Plot the data in Matlab with some parameters calculated from the T_ANALYSIS
'structure. Set the axes labels, title, colorbar and plot view.
Matlab.Execute( "figure; surf(linspace("&xMin &","&xMax &","&nXpx &"),linspace("& yMin &"," & yMax & "," & nYpx & "),irradiance_pwd, 'EdgeColor', 'None');" )
Matlab.Execute( "xlabel('X Position (" & GetUnits() & ")')" ) : Matlab.Execute( "ylabel('Y Position (" & GetUnits() & ")')" ) : Matlab.Execute( "zLabel( 'Irradiance' )" )
Matlab.Execute( "title('Detector Irradiance')" )
Matlab.Execute( "colorbar" )
Matlab.Execute( "view(2)" )
Print ""
Print "Matlab figure plotted..."
'Have Matlab calculate and return the mean value.
Matlab.Execute( "irrad = mean(mean(irradiance_pwd));" )
Matlab.GetWorkspaceData( "irrad", "base", meanVal )
Print "The mean irradiance value calculated by Matlab is: " & meanVal
'Release resources
Set Matlab = Nothing
End Sub
最後在Matlab畫(huà)圖如下:
并在工作(zuò)區(qū)保存了數(shù)據:
并返回平均值:
與FRED中計(jì)算(suàn)的照度圖對比:
例:
此例系統數(shù)據,可(kě)按照此數(shù)據建立模型
系統數(shù)據
光源數(shù)據:
Type: Laser Beam(Gaussian 00 mode)
Beam size: 5;
Grid size: 12;
Sample pts: 100;
相幹光;
波長0.5876微米,
距離原點沿着Z軸負方向25mm。
對于執行(xíng)代碼,如果想保存圖片,請(qǐng)在開(kāi)始之前一定要執行(xíng)如下代碼:
enableservice('AutomationServer', true)
enableservice('AutomationServer')
|