Summing ARNs Using Detector Entities

This knowledge base article discusses how to iterate raytraces and sum the results calculated by Detector Entities via a script. A related article also demonstrates how to do this when using Analysis Surfaces.

This method traces in batches and sums the results after each step, saving time when tracing a large number of rays. This is particularly helpful in illumination models where a very large number of rays are required to obtain statistically good data.

Detector Entities can be configured to automatically produce an ARN at the end of the raytrace with the CalcTiming option set to "At Trace End", so all the script needs to do is create a composite ARN from those collected in the Analysis Results folder and save it as a .fgd file in the current directory.

The script is structured as follows:

    numloops    = 10
    numRays     = 100000        'per source, per loop
    analysisID  = FindFullName( "Analysis Surface(s).Analysis 1" )<br>

The number of loops, the number of rays (per run, per source), and the Analysis Surface at which the data is to be calculated are defined by the user before the script is run.

Next, the script loops over all entities in the Tree and all sources are reconfigured to use the number of rays defined in the User Settings. Each source is power normalized according to the number of loops so that the total power contributing to the final data set at the end of the iteration loop is consistent with the initial source power settings. Note that it is assumed that each source has Ray Position Type = Random Plane.

    For i = 0 To GetEntityCount () - 1
        If IsSource(i) Then

            GetSourcePosGridRandom ( i, semiWidthX, semiWidthY, num, isEllipse )
            num = numRays
            SetSourcePosGridRandom ( i, semiWidthX, semiWidthY, num, isEllipse )

            SetSourcePower i, GetSourcePower( i )/numloops

        End If
    Next

For each iteration of the loop, the raytrace is performed and the ARN is automatically created.

    For i = 1 To numloops
        EnableTextPrinting False

        TraceCreate

        EnableTextPrinting True
        Print i & Chr(9) & i * numRays
    Next i

After the series of raytraces is completed, each ARN is accumulated into a newly created node to produce the composite result. 

    compArn = ARNCreateCopy( 0, "Composite Result" )

    'Start looping over the other ARN starting at index 1
    For i = 1 To ARNGetMaxNodeNum() - 1
        ARNLinearCombination 1, compArn, 1, i
    Next

    'Remove all other ARNs
    For i = 0 To ARNGetMaxNodeNum() - 1
        ARNDelete i
    Next i

The accumulated result is saved to the current directory using the ARNWriteToFile subroutine. The file is saved with the name “[numRays] rays - irradiance”.

    fullfilename    = GetDocDir() & "\" & filename & "-irradiance.fgd"
    ARNWriteToFile compArn, fullfilename, False, False

The script creates a new ARN in the Analysis Results folder on the FRED Tree and the fgd file in the current directory. The fgd file can be loaded into the Document at any time by right-clicking the Analysis Results folder on the Tree and selecting " Create New ARN From a File..."

Associated FRED Script: SummingARNs---DE.frs

Associated FRED Document: SummingARNsTestFile---DE.frd

Still need help? Contact Us Contact Us