PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Post on 13-Jan-2016

226 views 0 download

Transcript of PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

PH24010Data Handling and Statistics

Photon Scattering program written in MathCAD

The Golden Rule of Programming

• Applies to all programming

• K.I.S.S. principle– Keep– It– Simple– Stupid

MathCAD ProgramsStructures

• if, otherwise, for, while– Indentation & vertical bars– Watch selection rectangle– <Space> to increase (more lines)– <Insert> to swap sides

• No GOTO– Considered harmful

MathCAD programsif statement

• Better than if() function for complicated cases.

• otherwise statement to catch unhandled cases.

Programmed if statement

fThrust t( ) 0 t 0if

100 0 t 5if

80 5 t 20if

0 otherwise

• Note: – Comparisons– use of otherwise to

catch all cases

MathCAD programs – The for loop

fOnes N( )

Resulti 1

i 0 N 1for

Result

• Loop extend shown by indent• ‘Result’ array built up• Note syntax of ‘for’ line• Use when you know in advance how many

iterations

The while loop• Execute statements while a condition is

true• Used when you don’t know in advance

how many times loop will be executed.• Loop while you are searching• Loop while error is too big• Loop while system is stable

A while loop example

• Find first member of vector ‘Vec’ greater than threshold, ‘t’

• Written as function• j is index• while loop• return index & value as

vector

Thresh t Vec( ) j 0

j j 1

Vecj twhile

j

Vecj

Longer Loops• Use ‘Add Line’ in body of loop to extend

scope of loop.

• Lines added at vertical bar

• <Insert> key swaps sides of selection bar

Longer Loops

fCatInHat x( )

UpUpUpWithAFish

MotherIsOut x( )while

fCatInHat2 x( )

Thing1

Thing2

MotherIsOut x( )while

Program ExamplePhoton Scattering #1

1. Photon enters box

2. Travels random distance

3. Scatter through random angle

4. Repeat from step 2 until photon leaves box

5. Record walk for posterity

Photon ScatteringProgram to create Walk

OnePath InitX InitY Init i 0

x InitX

y InitY

Resulti 0 x

Resulti 1 y

P PathLength0( )

ScatterAngle Init

x x P cos

y y P sin

i i 1

InBox x y( )while

Resulti 0 x

Resulti 1 y

Result

Program ExamplePhoton Scattering #2

• Store x-y co-ordinates and i (loop count)• Write functions for

– Pathlength()– ScatterAngle()– InBox(x,y)

• Test these functions !!!

Photon ScatteringPathLength function

• path is related to ln(2)/mean path• x placeholder is dummy• rexp(1, path) function returns vector of

1 number from distribution• indexing to extract element 0 from

vector

PathLengthx( ) rexp 1 path 0

Photon ScatteringScatterAngle()

• Isotropic scatter - uniform• Give 1 angle randomly between – • Similar use of built-in random numbers to

earlier.• Deal with anisotropy later

ScatterAngle runif 1 0

Photon ScatteringInBox(V) function

• Takes x,y as arguments• 2 way logical expression

– LoLimit < x < HiLimit

• Uses multiplication to form AND• Returns 1 if in box, 0 otherwise

MinXBoxSize2

MaxXBoxSize

2 MinY

BoxSize2

MaxYBoxSize

2

InBox x y( ) MinX x MaxX( ) MinY y MaxY( )

Photon ScatteringProgram to create Walk

OnePath InitX InitY Init i 0

x InitX

y InitY

Resulti 0 x

Resulti 1 y

P PathLength0( )

ScatterAngle Init

x x P cos

y y P sin

i i 1

InBox x y( )while

Resulti 0 x

Resulti 1 y

Result

Photon ScatteringUsing the programHaving created the data in MyFirstWalk, we can now plot the path taken by our photon on an

X-Y Graph

MyFirstWalk OnePath 0 0 0( ) rows MyFirstWalk( ) 509

400 200 0 200 400500

0

500

MyFirstWalk1

MyFirstWalk0

Photon ScatteringConclusions

• 14 line program + functions

• Records entire walk

• Extract info from result vector

• Easy to extend– 3D scatter– Anisotropy

• Change ScatterAngle()

Combining Many Walks

• Use stack() to join results together

ManyWalks InitX InitY Init NWalk Result OnePath InitX InitY Init

Result stack Result OnePath InitX InitY Init

i 1 NWalk 1for

Result

Simple anisotropy

• Assume ‘normal’ distribution about angle

0.2

ScatterAngle rnorm 1 0

Many walks with anisotropy

400 200 0 200 400500

0

500MaxY

MinY

ManyPaths 1

MaxXMinX ManyPaths 0