CSI 801

Lecture #6

Software Engineering

October 3, 1996

John Wallin

On the plate we are all individuals. In the field, we are all part of the same team.
Al Capone, The Untouchables


CSI 801

Lecture #6

Software Engineering I


Simulation Plan

1) Mathematical Model

geometry

equations

2) Discrete Algebratic Approximation

3) Simulation Algorithm

4) Technical Plan

5) Code Validation

6) Timing and memory tests

7) Simulation

8) Interpretation and visualization

9) Analysis and Conclusions


Validation and Testing

symmetry

does geometry affect the results

convergence

do the algorithms actually converge

conservation

are conserved quantities conserved?

consistency

is it consistent with previously published results or analytic solutions


Markov Chains

Chains of discrete events.

The ultimate success or failure of a particular state is determined by a random number generator and the probability from the previous state.


Radioactive Decay

1. Atoms decay from their original state to a final state.

2. The decay of a given atom is independent of the other atoms in the material.

3. The chances of a specific atom decaying in any given period of time is always the same.

4. All atoms have the same chance of decay.


Radioactive Markov Chains

1. Set the probability of atoms surviving in a given time period.

2. Generate an atom in the first time bin.

3. Determine if the atom survives in this time bin.

4. If it survives, move to the next time bin and repeat 3 until you have reached the final time bin.

5. If it does not survive, increment the number of decays in that particular time bin.

6. Repeat steps 2-5 until done.


Least Squares

fitting data to analytic models

fundamentally different than interpolation and polynomial fitting


Linear Least Squares

Given a set of data

(xi, yi) where i = 1, n

find the best fit to the equation

y = m x + b

The residual of the fit will be

abs( yi - (m xi + b))

This is measure of how closely our model fits a particular point.

We need to minimize the residiual in order to get the best fit.


Linear Chi^2

We need to minimize the following equation:

chi^2 = sum ( yi - (m xi + b))^2 / Ei^2

i=1,n

where Ei is the error on each term.

We must take the partial deriviative-

d (chi^2) (yi - (m xi + b)) (-xi)

------- = 2 sum -----------------

d (m) i=1,n Ei^2

d (chi^2) (yi - (m xi + b))

------- = 2 sum -----------------

d (b) i=1,n Ei^2

and then set each of these to zero


Definitions

S = sum 1 / Ei^2

Sx = sum xi / Ei^2

Sy = sum yi / Ei^2

Sxx = sum xi*xi / Ei^2

Sxy = sum xi*yi / Ei^2

The equations then become:

b*S + m *Sx = Sy

b*Sx + m*Sxx = Sxy


The Solution

delta = S*Sxx - Sx*Sx

b = (Sxx * Sy - Sx * Sxy) / delta

m = (S * Sxy - Sx * Sy) / delta

The same procedure can be used to find the best fit for any analytic model.


How good is the fit?

Propagation of error

Ef = sum ( Ei^2 * (df/dyi)^2)

where d = partial derivative and

f is any function of the data

db Sxx - Sx * xi

--- = -------------

dyi Ei^2 * delta

dm S* xi - Sx

--- = -------------

dyi Ei^2 * delta

sigma(b)^2 = Sxx / delta

sigma(m)^2 = S / delta


Covariance

correlation in variance between b and m

Cov(b, m) = - Sx/ delta

We can do a lot more with error analysis.

For example, what is the probabilty of getting this "good" of a fit from random data?

This involves incomplete gamma functions, and is beyond the scope of this discussion.


Using Least Squares

How do you use linear least squares for our problem.

Count rate = k exp( -t/tau)

ln(count rate) = ln(k) + t *( -1/tau)

substitute:

y = ln(count rate)

b = ln(k)

m = (-1/tau)

x = t


The Carpenter, the Architect, and the Home Owner

the Carpenter BUILDS

the Architect DESIGNS

the Home Owner USES


The Programmer, the Software Engineer and the User

the Programmer BUILDS

the Software Engineer DESIGNS

the User USES


What Makes a Good Program?

When is the last time you read someone's code?


Code Example #1

private void sort(int lo0, int hi0) {

int a[] = con;

int lo = lo0;

int hi = hi0;

if (lo >= hi)

return;

int mid = a[(lo + hi) / 2];

while (lo < hi) {

while (lo < hi && a[lo] < mid) {

lo++;

}

while (lo < hi && a[hi] >= mid) {

hi--;

}

if (lo < hi) {

int T = a[lo];

a[lo] = a[hi];

a[hi] = T;

}

}

if (hi < lo) {

int T = hi;

hi = lo;

lo = T;

}

sort(lo0, lo);

sort(lo == lo0 ? lo + 1 : lo, hi0);

}

ThreeD.java Copyright SunMicrosystems


Code Example #2

// Find the best x,y point for a dialog, by finding which screen

// the status window is on and offsetting from there. Used by

// dialog routines like SFGetFile and DIBadMount.

void GetBestDialogPos(Point * wherep)

{

RectsfRect;

GDHandlethGDev;

// find rect of screen that status window is on..

GetGlobalWindowRect((WindowPtr)gp2wWindow, &sfRect);

thGDev = GetClosestGDevice(&sfRect);

sfRect = (**thGDev).gdRect;

// ..and offset SF Dialog on that screen

wherep->h = sfRect.left + 40;

wherep->v = sfRect.top + 80;

} // GetBestDialogPos

// ---------------------------------------------------------------------

// Dispose and recreate an empty file list queue

static void HaltFileQ()

{

// halt any multi-doc processes

gDoingBatchODOCs = false;

FileQ_d();// Tear down ODOC File Queue

FileQ_c(); // re-create it, empty

} // HaltFileQ

POV PowerMac


Code Example #3

:[font = input; preserveAspect]

tetrapict =

Show[Graphics3D[ Polygon /@

Flatten[ HollowTriangle /@ poly , 1]],

ViewVertical -> {.52, .38, .85},

Boxed -> False

]

:[font = text; inactive; preserveAspect]

If you evaluate the next three input cells you can see how the intersecting tetrahedra fit inside the original dodecahedron.

:[font = input; preserveAspect]

WireFrame[shape_] :=

shape /. Polygon[x_] :> Line[

Append[x, First[x]]

]

:[font = input; preserveAspect]

dodec =

Show[WireFrame[Polyhedron[Dodecahedron]],

Boxed -> False]

:[font = input; preserveAspect; endGroup]

Show[ tetrapict, dodec ]

:[font = section; inactive; Cclosed; preserveAspect; startGroup]

Credits

:[font = text; inactive; preserveAspect]

By Scott Kim

:[font = smalltext; inactive; preserveAspect; fontSize = 9; endGroup; endGroup]

Copyright 1991-93 Wolfram Research, Inc.

^*)

Mathematica(tm) Notebook File


Specifications

"any program that works is better than any program which doesn't"

Does the program do what it is suppose to do?


Schedule

"what is the cost of not having the program?"

How close to the original schedule is the project?

What is the penalty for late delivery?

Who dictates when things MUST be done?

How consistent is the schedule?


How well can you set a project schedule?

How many lines of error free code can you write in a given day?

How good are you at estimating the time it will take you to complete CSI 801 assignments?

This is CRITICAL for you to learn for your dissertation and for your research.


Adapatability

How easily can this code or parts of this code be adapted for other uses?

Can this code operate with other existing codes?

How difficult will it be to use this code on other computer systems?

The Parable of Apollo


Fisher's Fundimental

Theorem

"The better adapted a system is to a particular environment, the less adaptable it is to a new environment."

This is applicable to organisms and to software.

Making software which "takes advantage" of your system will make it better and will make it less adaptable to a new system.


Platform Independence

Platform Independence does NOT happen by accident.

You must intentionally avoid system dependent functions and features.

Some day, the operating system and computer you are using will be obsolete.

Think about that when writing your code!


Separation of System Dependent and System Independent Routines

Some use of system dependent features is almost always necessary.

Plan to separate these functions to a separate file in your code project.

Keep system independent and system dependent calls in separate files!!!!


Libraries

A set of reusable code

Libraries take different forms on different systems. However the idea of a library is universal.

Good libraries are extremely useful.

Entire careers can be built from useful libraries.

Examples:

pgplot

IMSL

X-windows

Numerical Recipes


Efficiency

how is efficiency defined for this project?

Is CPU speed the primary issue?

How important is a good user interface?

Is the system limited by the amount of data it can access?


Programming Teams

"On the plate, we are all individuals. In the field, we are all members of the same team." - Al Capone

"All animals are equal. Some are just more equal than others."

-Orwell, Animal Farm

"I must catch up with them, for I am their leader."

-unknown


Essential Programmers

Programmers often specialize.

Some programmers write code which no one else can understand.

Programmers can become "essential" by the virtue of their specialization.

How do you deal with programmers who have become essential?


Measuring Progress

"Half the time, baseball is 90% mental." - Yogi B.

Determining your progress should be closely related to your schedule.

How much of the project has been completed and how much of it REALLY remains?

Honestly assessing your progress is difficult, but ESSENTIAL.


Professional vs Amateur Programming

Professional programs are usually written for someone.

These codes typically have error trapping and are designed to be extendable to other uses. These codes will be used more than once.

amateur programs are usually written for the programmer's own use

These codes will be built for one specific use. They are built as cheaply as possible, and do not usually solve a general problem.


User Interfaces

A good user interface is essential in most professional programs.

The complexity of the interface depends on the targeted user.

Simple interfaces could be text messages simply prompting the user for input.

Try to avoid :

?

In most programs you write.


GUI's and You-eys

The current "state-of-the-art" interface is the Graphical User Interface (GUI).

Menus

Buttons

Mice

Icons

Selections

Boxes


Event Driven Programming

(of Mice and Menus)

Not fundimentally more difficult than scientific programming.

Involves a structure which is something like:

1) start program

2) wait for event

3) process event

4) goto 2

Events are tied to specific functions or to methods within objects.


Usability Testing

For commerical programs, you need to conduct tests about how your audience will use your system.

A few simple changes in interface design can make a huge difference in performance.


Documentation

the nearly lost art of describing your code to both users and to programmers

this is ABSOLUTELY ESSENTIAL in all programming you do

someday, you will want to figure out what the program actually does

your only chance lies in documention


Minimal Documentation

in-line comments - what does this line do?

function comments - what is this function do? What are the input and outputs?

variable documentation - what does each variable do? Does the name make sense?


Full Documentation

testing comments - has this routine been completely tested?

user documentation - how do I use this darn code?

validation procedure - how was this code validated?

structural comments - how is the code put together? How does control flow? What are the functional components?


Programming Standards

why do we need them?

Essential for projects involving multiple programmers.

Essential for projects involving more than 500 lines.


The Ten (%) Commandments of CSI 728

1. Thou shalt use g++, gcc, or f77. Let no other compilers come before thee.

2. Remember thine internal documentation.

3. Thou shalt use UNIX systems to test thine work. For thine professor hath a UNIX system and his wrath is mighty.

4. Thou shalt use Makefiles for all thine assignments. And lo, thine makefiles shall be titled "Makefile".


5. Remember thine ANSI standards and thine function prototypes, and keep them holy (or at least wholy standard).

6. Let no system dependent functions and libraries come before thee.

7. Thou shalt write documentation on compiling thine codes.

8. Thou shalt write discussion about the assignment and its organization.

9. Thou shalt use subroutines and include files.


10. Thou shalt tar thine files thusly:

a. Create a subdirectory called assignment#

b. Move all the relavent files to the directory

c. Remove all the irrelavent files (*.o, ~, core...)

d. From above the directory type:

tar cvf username#.tar assignment#

11. Thou shalt turn thine assignments in on time (and under budget).

12. Thou shalt make thine assignments neat and readable and verily make use of white space and indenting.


13. Thou shalt check thine code and report on its failings.

14. Thou shalt produce documentation using plain tex, tex, latex, or postscript. Shun thine false word processors.

15. Thou shalt check the numerical accuracy of thine commandments.

16. Thou shalt never use goto.


Why is this code being written?

Who will be using the code?

What are the goals of its use?

What limits are there on time and schedule?

How many times will it be used?

How reliable must it be?


What elements will determine the success or failure of this project?

Is reliability more important than cost?

How important is efficiency?

Will this be reused or used with other software?

What are the conditions which determine the successfullness of the code?


What are the major components of the code?

Which numerical methods being used?

Is a simple user interface needed?

What is the overall structure of the code?

What are the major data structures which will be used in the code?

What functions, subroutines and loops will be needed?


What is the schedule for creating the project?

How much start-up time do you have?

What are the dates of the deliverables?

What are the consequences of being late?

How much freedom to you have to set the schedule?

How general and extensible must your code to be?


Who will work on each component?

Who is responsible for constructing each module?

What programming standards have you established for your team?

Who is responsible for integration and testing each module?

How do you assure components are delivered on time?

Is there a reward structure for good programming practices?


Unix This Week

Learn about shell scripts

sed awk sort if case

sed - editing file streams

awk - editing file streams

sort - sorting file streams

shell script

removing old files with *.o extensions


Shell Scripts

the first line in every shell

script should be:

#!/bin/csh

if you are executing commands in the C-shell.

You must use the "chmod" command to allow make the shell script executable.


Commands in Shell Scripts

every Unix command you have learned can be included in a shell script

there are a FEW other things you should know about writing shell scripts-

Variables

loops

decisions


Variables


x = 'date'


will assign the value of "date" to the


value x


x = `date` 

will assign the output of the
command `date` to the value of x


x = "date" will take "date" literally,
but it will interpret variables and
characters like \x

variables are accessed using the

$x

syntax. For example

echo $x

will print the value of x.


More variables

$1 ... $9 are the input values passed to the shell script


if "yyy" is a shell script, if you have
the line 
echo $1

the command line

yyy test

will print the word "test"


yyy = `pwd; echo -n "/dog"`
will assign the variable yyy

with the current directory folllowed by the filename "dog"

cat $yyy

will display the output of this file


Loops

list = `ls -l`

for i in list

echo $i

end


will set the value of $i to every word
in `ls -l` and then print each word on
a separate line.



i = 1

while ($i < 10) 

  $i = $i + 1

 echo $i
end

will count from 2 to 9


File IO

use the

> creates new file

>> appends lines to a file

<< input from a file

Decisions

if ( )

else

endif



Copyright John Wallin 1996. All rights reserved.
Last Modified : Fri October 4 11:01:00 EST 1996 <jwallin@gmu.edu>