Navigation: Scripting Reference Manual > Script Examples > Examples - Sources

 

Examples - Sources

 

Contact Us: fredsupport@photonengr.com

 

These examples cover the FRED source scripting commands:

"Add" - add sources, source properties, and rays,

"Delete" - deletes wavelengths or rays,

"Get" - gets source properties,

"Is" - Boolean query of source properties,

"Read" - read source definition files,

"Set" - sets source properties.

 

The structures listed below contain parameters required for source commands:

T_ENTITY

T_SOURCEPROPSPEC

T_SOURCEPOLARIZATION

T_SOURCEVISUALIZE

T_WAVELENGTHSPEC

T_SOURCEPOSAPODLENGTH

T_SOURCEDIRAPODSINCOS

T_SOURCEUSERRAY

T_SOURCEPOSSURFACE

T_SOURCEPOSBITMAP

T_SOURCEDIRRANGE

T_SOURCEDIRPOSITIONGRID

T_SOURCEDIRASTIGGAUSBEAM

T_SOURCEPOSAPODSAMPLED

 

 

 


 

"Add" sources, rays or properties

Use the "Add" commands to create new sources, add wavelength specifications, add user-defined rays or to add angles or positions to FRED Multiple sources.

 

A default source can be added with the script lines below. This default source is a "Detailed Optical Source" with the following properties:

 

"Single Direction" pointing in the (0,0,1) direction,

"Grid Plane" with 21x21 rays, semi-aperture of 0.5, elliptical shape,

Location at 0,0,0 in the "Optical Sources" coordinate System,

Uniform position/direction apodization,

Power = 1,

Not coherent,

Not polarized,

wavelength of 0.5875618 mm (He d-line).

 

In this example, a new source is added to the FRED document with the name "MyNewSource", the parent set to "Optical Sources" and is traceable:

 

Dim sdata As T_ENTITY, newid As Long

sdata.name="MyNewSource"

sdata.parent=1            'source parent is "Optical Sources" node 1

sdata.traceable=True

newid=AddSource(sdata)

Update

 

In this example, the AddSourceWavelengthSpec command is used to add a group of evenly-spaced wavelengths to the new source created above. These wavelengths are weighted by a gaussian function with center wavelength=0.65 mm and a 1/e2 HW=0.1mm. Since the default source is originally created with one wavelength (0.5875618 mm), the DeleteSoureIthWavelengthSpec command is used to remove that default wavelength located at index=0.

 

Dim spec As T_WAVELENGTHSPEC
id=FindName("MyNewSource")

wavecen=0.65
wavewidth=0.1

For i=-1 To 1 Step 0.1
 spec.wavelen=wavecen+(i*2*wavewidth)
 spec.weight=Gaus((spec.wavelen-wavecen)/wavewidth)
 number = AddSourceWavelengthSpec ( id, spec )
Next i
 

success = DeleteSourceIthWavelengthSpec ( id, 0 )       'remove the default wavelength at index 0
Update

 

User-defined rays can also be added to a source. In the example below, a new source "MyGridSource" is added as shown in the above example. In the case of user-defined rays, the SetSourcePosUser command is required to notify FRED that this source in not of the standard type. An array of T_SOURCEUSERRAY structures must be declared to hold the individual ray position, direction, power and pathlength settings. Upon defining data for each ray, the AddSourceUserRays command loads the ray data into the source definition.

 

Dim sdata As T_ENTITY, newid As Long

sdata.name="MyGridSource"
sdata.parent=1                       'source parent is "Optical Sources" node 1
sdata.traceable=True
newid=AddSource(sdata)
SetSourcePosUser newid
Update

Dim useray(120) As T_SOURCEUSERRAY   'array of T_SOURCEUSERRAY structures

For i=-5 To 5
 For j=-5 To 5
  useray((i+5)*11+(j+5)).Xpos=i
  useray((i+5)*11+(j+5)).Ypos=j
  useray((i+5)*11+(j+5)).power=1
  useray((i+5)*11+(j+5)).Xdir=0
  useray((i+5)*11+(j+5)).Ydir=0
  useray((i+5)*11+(j+5)).Zdir=1
 Next j
Next i

numRays = AddSourceUserRays ( newid, useray )


 

"Delete" wavelengths or rays

 

(see 2nd example in "Add" section)


 

"Get" source properties

General information such as power, immersion material, phase and propagation specification can be retrieved from a specified source. For example, to find the material in which source "Blue Diode" is located,

 

sid=FindName("Blue Diode")

mat = GetSourceMaterial ( sid )

Print "The Blue Diode rays are currently in ",GetMaterialName(mat)

 

For coherent sources, other properties such as beam overlap, polarization and number of secondary beams can be accessed with "Get" commands.

 

id=FindName("Source 1")
overlap = GetSourceBeamOverlap ( id )
Print overlap

Dim spec As T_SOURCEPOLARIZATION
GetSourcePolarization id, spec
Print spec.ellipticity, spec.randomizeAngle

 

The visualization properties of a source may be of interest. They can be loaded into a T_SOURCEVISUALIZE structure for inspection or further use. In this example, the visualization properties of "Source 1" are retrieved using GetSourceVisualization. These visualization properties could be applied to other sources via the vspec structure by using a "Set" command discussed below.

 

Dim vspec As T_SOURCEVISUALIZE

ids1=FindName("Source 1")

GetSourceVisualization ids1, vspec

Print "visualize every ", vspec.everyNth, "th ray for Source 1"

 

The wavelength list and properties of a source can also be accessed:

 

sid=FindName("Red LED")

count = GetSourceWavelengthCount ( sid )         'get the number of wavelengths for Red LED

wavelength = GetSourceIthWavelength ( sid, 3 )   'get the 4th wavelength in the list of Red LED

 

Dim wavedata As T_WAVELENGTHSPEC

GetSourceIthWavelengthSpec sid, 12, wavedata     'load the data for the 13th wavelength into wavedata structure

 

The position/direction type as well as apodization type with associated data can be retrieved in the following manner:

 

Dim posType$,funcType$
Dim apodLength As T_SOURCEPOSAPODLENGTH
 

posType = GetSourcePosFuncType ( 5 )
funcType = GetSourcePosApodFuncType ( 5 )
Print posType, funcType
success = GetSourcePosApodLength ( 5, apodLength )
Print apodLength.exponent, apodLength.referenceLength

 

In the case of a surface made into a random emitting source, the data from that source can be loaded into a T_SOURCEPOSSURFACE structure for easy access using GetSourcePosSurface:

 

Dim sourcesurfdata As T_SOURCEPOSSURFACE
success = GetSourcePosSurface ( 5, sourcesurfdata )
Print "Number of rays-",sourcesurfdata.numRays , "  directional type-", sourcesurfdata.directionSpec


 

"Is" source queries

The "Is" statements return True or False to a wide range of queries. For instance, the user may want to know which entities in his/her model are random emitters and set the number of rays to 10,000. First declare a T_SOURCEPOSSURFACE structure to hold the appropriate data. Next, loop through all entities asking first if the entity is a source and then if it is a random surface emitter. If both "Is" statements return True, then make the change. Remember that the structure pos is not loaded with default values. Therefore, be sure to "Get" the current parameters for each structure element before making changes.

 

Dim pos As T_SOURCEPOSSURFACE

For i=0 To GetEntityCount ()-1
 If IsSource(i)=True Then                     'is entity a source
  If IsSourcePosSurface( i )=True Then        'if so, is source a surface emitter
   success = GetSourcePosSurface ( i, pos )   'get surface emitter properties
   pos.numRays=10000                          'new number of rays
   SetSourcePosSurface i, pos                 'set surface emitter new properties
  End If 
 End If
Next i


 

"Read" source definitions

FRED can read numerous types of ray files. These include binary files from FRED, TracePro, OSLO, Zemax, OptiCAD, ASAP and LucidShape as well as text files from FRED, TracePro, OSLO and LightTools. The example below reads a previously saved FRED ray text file after confirming that "Source 3" is of the proper type, that is "SRCPOSGeneral".

 

id=FindName("Source 3")
file="RayData.dat"
If GetSourcePosFuncType (id)="SRCPosGeneral" Then
 numRays = ReadRayFile (id, file, 1, -1, 1)
End If
Print numRays," rays loaded"


 

"Set" source properties

 

The "Set" commands convey a wide range of source properties in much the same way the "Get" commands access source information. In fact, "Get" and "Set" are often used in tandem especially when selected source parameters are to be changed while others are to remain the same.

 

In the following example, the weights of all wavelength of source "lamp" are set to 1 without changing the individual wavelengths or their RGB values:

 

Dim wavespec As T_WAVELENGTHSPEC
sid=FindName("lamp")

For i=0 To GetSourceWavelengthCount(sid)-1
 GetSourceIthWavelengthSpec sid, i, wavespec
 wavespec.weight=1
 SetSourceIthWavelengthSpec sid, i, wavespec
Next i
 

This example resets several parameters of a sine/cosine directional apodization while preserving the others:

 

Dim apod As T_SOURCEDIRAPODSINCOS
fid=FindName("filament")

success = GetSourceDirApodSinCos ( fid, apod )
apod.exponent=3
apod.dirX=1
apod.dirY=1
apod.dirZ=1
SetSourceDirApodSinCos fid, apod

 

As a final example in this section, a bitmap source is created. Note that this source creation requires 3 FRED structures; T_ENTITY, T_SOURCEPOSBITMAP, and T_SOURCEDIRRANGE. The scr structure supplies a name and makes the source traceable. The bpod structure provides dimensions, centering, rays per pixel and the bitmap filename. The dirR structure assigns a 25deg x 25deg elliptic directional property to the rays.

 

Dim src As T_ENTITY
Dim bpod As T_SOURCEPOSBITMAP
Dim dirR As T_SOURCEDIRRANGE

src.name="prism pic"
src.traceable=True
bid = AddSource ( src )

'bitmap size, center, rays per pixel and filename
bpod.semiApeX=2
bpod.semiApeY=2
bpod.centerX=0
bpod.centerY=0
bpod.centerZ=0
bpod.raysPerPixel=1
bpod.filename="prismspectra.bmp"

'source randomly radiates into a specified angular range
dirR.semiApeX=25
dirR.semiApeY=25
dirR.isElliptical=True
 

'set source parameters

SetSourcePosBitmap bid, bpod
SetSourceDirRange bid, dirR


See Also….

Advanced Raytrace

Basic Raytrace

Coatings

Curves

Elements

Entity Info

General Ray Data

Gratings

Importance Sampling

Linear Transforms

Materials

Ray Buffer Handling

Ray Data Get/Set

Ray Path

Raytrace Control

Scatter

Surfaces

 

 

 

 

 

Copyright © Photon Engineering, LLC