220 likes | 340 Views
Scientific Programming. More Basics. Homework1 – Source Code. PROGRAM homework1 !part 1: a = 0. n = 1000 DO i = 1,n,2 b = i a = a + b END DO WRITE (*,*) '1+3+5+...+',i-2,'=',a !part 2: a = 0. DO i = 2,n,2 b = i a = a + b
E N D
Scientific Programming More Basics
Homework1 – Source Code PROGRAM homework1 !part 1: a = 0. n = 1000 DO i = 1,n,2 b = i a = a + b END DO WRITE (*,*) '1+3+5+...+',i-2,'=',a !part 2: a = 0. DO i = 2,n,2 b = i a = a + b End DO WRITE (*,*) '2+4+6+...+',i-2,'=',a !part 3: a = 0. DO i = 1,n b = i b = b**.5 a = a + b END DO WRITE (*,*) 'sqrt(1)+sqrt(2)+sqrt(3)+...+sqrt(',i-1,')=',a END
Homework1 Output c:\FortranSources>homework1 1+3+5+...+…….+ 999 = 250000.0 2+4+6+...+…….+1000 = 250500.0 sqrt(1)+sqrt(2)+sqrt(3)+...+sqrt(1000) = 21097.46 c:\FortranSources>
Applications / Folders • Notepad • Write Original Program • Save in FortranSources w/ .f90 • FortranSources • Edit Existing Programs • Intel Fortran Compiler Prompt • Compile, Link, Execute
Fortran Language Elements • Elements: • Alphabetic: • A,B,C,D…………Z • a,b,c,d….………z • Numeric: • 1,2,3,4…..,0 • Symbols: • _ + - * / ** ().=,’$:!”%&;<>?
Data Types • Integer (aka Fixed Point) • No decimal point • Real (aka Floating Point) • w/ decimal point • Character (aka Comments) • Also Complex & Logical
Integer • Implicit (Default) • Any constant w/o a decimal point • Variables w/ names starting with i,j,k,l,m,n • Explicit (Declared) • INTEGER :: aa, bnx, ipox
Real • Implicit (Default) • Any constant w/ a decimal point • Variables w/ names starting with anything other than i,j,k,l,m,n • Explicit (Declared) • REAL :: aa, bnx, ipox
Character • ‘place the comment here’ • used for embedding comments in the output • CHARACTER , PARAMETER :: error = ‘I give up’ • used for embedding comments in the output
Good Practice • PROGRAM type_example • IMPLICIT NONE • INTEGER :: bbb, ztop, kbxx • REAL :: aa, bnx, ipox • CHARACTER(len=12) :: cc, t, izt • The value, 12, is the field width
Good Practice • Name your constants for use in the program • REAL, PARAMETER :: PI = 3.14, NOX = 1.732, etc • INTEGER, PARAMETER :: IZ = 12, MCS = 52, etc
Assignment Statement • a & b are variable names & each is assigned an address where the contents are stored • a = a + b means: • “Take the current contents of a & b, add them, and store the results in a, thereby eliminating the current contents of a”
Arithmetic • + Addition • - Subtraction • * Multiplication • / Division • ** Exponentiation • The hierarchy of operations reverses the above list
Integer Arithmetic • + Addition - OK • - Subtraction - OK • * Multiplication - OK • / Division - Problematic • ½ = 0, 3/8 = 0, 6/4 = 1, 10/3 = 3 • ** Exponentiation - OK • Avoid integer arithmetic except for counting, indexing, exponentiation
Real Arithmetic • Hierarchy of Operations • Parenthesis (Inner to outer) • Exponentiation (R to L) • Multiplication & Division (L to R) • Addition & Subtraction (L to R) • Examples: • a+b/c**d-e*f • (a+(b/c))**d-e*f • (a+(b/c))**(d-e)*f • (a+(b/c))**((d-e)*f)
Mixed Mode Arithmetic • Avoid mixed mode arithmetic except for exponentiation (and it won’t hurt if you also avoid it there!!!)
Intrinsic Functions • Intrinsic Functions are embedded in the Fortran Language • As opposed to external or internal functions which must be generated by the programmer
Intrinsic Functions • Examples • SQRT(x) ABS(x) • ATAN(x) LOG10(x) • SIN(x) COS(x) • TAN(x) LOG(x) • Etc - Refer to Internet Resources
List Directed Input & Output • The type of the item in the list determines the form • Sloppy Input & Output • Read(*,*) a, bb, k, char • Write(*,*) charout, zz, vel, ibot • Formatted Input & Output - Later
Good Practice Summary • Use meaningful variable names whenever possible. Use names that can be understood at a glance, like day, month, and year. • Always use the IMPLICIT NONE statement to catch typographical errors in your program at compilation time. • Create a data dictionary in each program you write. The data dictionary should explicitly declare and define each variable in the program. Be sure to include the physical units associated with each variable, if applicable. • Use a consistent number of significant digits in constants. For example, do not use 3.14 for PI in one part of your program and 3.141593 in another part of the program. To ensure consistency, a constant may be named, and the constant may be referenced by name wherever it is needed. • Be sure to specify all constants with as much precision as your computer will support. For example, specify PI as 3.141593, not 3.14.
Good Practice Summary • Do not use integer arithmetic to calculate continuously varying real-world quantities such as distance or time. Use integer arithmetic only for things that are intrinsically integer, such as indexes and counters. • Avoid mixed-mode arithmetic except for exponentiation. • Use extra parentheses whenever necessary to improve the readability of your expressions. • Always echo any variables you enter into a program from a keyboard to make sure that they were typed and processed correctly. • Initialize all variables in a program before using them. The variables may be initialized with assignment statements, with READ statements, or directly in type declaration statements. • Always print the physical units associated with any value being written out.
Assignment • In class - assign arbitrary values to a – f and write a Fortran program to evaluate & print out: • a+b/c**d-e*f • (a+(b/c))**d-e*f • (a+(b/c))**(d-e)*f • (a+(b/c))**((d-e)*f) • Self-generate practice problems to program • Read (and Re-Read) these notes until you begin to dream in Fortran