1 / 22

Scientific Programming

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

verlee
Télécharger la présentation

Scientific Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scientific Programming More Basics

  2. 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

  3. 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>

  4. Applications / Folders • Notepad • Write Original Program • Save in FortranSources w/ .f90 • FortranSources • Edit Existing Programs • Intel Fortran Compiler Prompt • Compile, Link, Execute

  5. Fortran Language Elements • Elements: • Alphabetic: • A,B,C,D…………Z • a,b,c,d….………z • Numeric: • 1,2,3,4…..,0 • Symbols: • _ + - * / ** ().=,’$:!”%&;<>?

  6. Data Types • Integer (aka Fixed Point) • No decimal point • Real (aka Floating Point) • w/ decimal point • Character (aka Comments) • Also Complex & Logical

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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”

  13. Arithmetic • + Addition • - Subtraction • * Multiplication • / Division • ** Exponentiation • The hierarchy of operations reverses the above list

  14. 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

  15. 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)

  16. Mixed Mode Arithmetic • Avoid mixed mode arithmetic except for exponentiation (and it won’t hurt if you also avoid it there!!!)

  17. Intrinsic Functions • Intrinsic Functions are embedded in the Fortran Language • As opposed to external or internal functions which must be generated by the programmer

  18. Intrinsic Functions • Examples • SQRT(x) ABS(x) • ATAN(x) LOG10(x) • SIN(x) COS(x) • TAN(x) LOG(x) • Etc - Refer to Internet Resources

  19. 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

  20. 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.

  21. 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.

  22. 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

More Related