How to Write a Fortran Jiffy (C)2005 Mark Rould University of Vermont.

Slides:



Advertisements
Similar presentations
Input and Output READ WRITE OPEN. FORMAT statement Format statements allow you to control how data are read or written. Some simple examples: Int=2; real=
Advertisements

CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
CS0007: Introduction to Computer Programming
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 2 Basic Elements of Fortan
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Computer Science 1620 Programming & Problem Solving.
Revision – A simple program How to start a program? How to end a program? How to declare variables? What are the mathematical operators? How to start a.
Chapter 5 Input / Output. 2 Control over input & output  The input and output are basically facilitates a communication between the user and the program.
Introduction to C Programming
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Introduction to FORTRAN-90 University of Liverpool course.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Python Programming Fundamentals
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 2 - Algorithms and Design
Input, Output, and Processing
Class Review. Basic Unix Commands list files in a directory: ls list files in a directory: ls remove files: rm remove files: rm rename files: mv rename.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to Programming with RAPTOR
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1 INPUT STREAMS ifstream xin; // declares an input stream.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CSE 1320 Basics Dr. Sajib Datta
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Basic concepts of C++ Presented by Prof. Satyajit De
Fortran Tutorial Fortran spaces for code qqqqqqCODE
Data Types and Expressions
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Other Kinds of Arrays Chapter 11
User input We’ve seen how to use the standard output buffer
Arithmetic operations, decisions and looping
Introduction to C++ Programming
Fundamentals of visual basic
Primitive Types and Expressions
DATA TYPES There are four basic data types associated with variables:
DATA TYPES AND OPERATIONS
Introduction to C Programming
Presentation transcript:

How to Write a Fortran Jiffy (C)2005 Mark Rould University of Vermont

Lines with a "C" or "!" in column 1 are comments (ignored) Lines which start with something in column 6 are continuations of the previous line All other lines should begin in column 7 or later Fortran has a few idiosyncrasies c This line is a comment ! This line is too. write( 6, *) 'This is a normal line (aka, a statement)' do i = 1, 3 write( 6, *) 'This statement continues ', & 'on the next line.' end do end

Programs Play with Data; Data is stored in Variables 3 types of variables will get you far in Fortran: INTEGER for holding whole numbers: 43, -9843, 0 REAL for holding numbers with decimal fractions and/or exponents: , , , E23 CHARACTER for holding anything alphanumeric: "I love Fortran!", "Nothing from nothing leaves 0" Other notables: LOGICAL variables hold values of either TRUE or FALSE COMPLEX variables are really two REALs (a real part and an imaginary part) Any of these variables can be used in arrays All of the numeric variables are also available as DOUBLE PRECISION (at least twice as many significant digits)

Do you have any variables to declare? implicit none integer Hin, Kin, Lin, & Hout, Kout, Lout, & m11, m12, m13, m21, m22, m23, m31, m32, m33, determinant, & endOfFileFlag, & nreflectionsRead, nreflectionsWritten, nsysAbsencesTossed character filename*200, headerLine*100, restOfReflectionLine*32 real detector_width, wavelength, & min_xtl_detector_dist, max_xtl_detector_dist, & xtl_detector_dist_increment, xtl_detector_dist, & twoTheta_max_radians, twoTheta_max_degrees, highest_resolution It's always a good idea to have an implicit none statement as the first line of your program -- It forces you to declare your variables at the beginning of the program, but it also helps catch a lot of typos. All of these variables are declared to be integers These are declared as character variables; the # after the * indicates how many characters long they are. These variables will hold real numbers.

Getting Data In and Out write( 6, *) 'Width of detector (in mm)?' read( 5, *) detector_width Display on the terminal Take input from the user and store it in this variable

Could you get me that file? write( 6, *) 'Input.sca file:' read( 5, '(a)') filename open( unit=1, file=filename, status='old') read( 1, '(i4,i4,i4,a32)', iostat = endOfFileFlag ) Hin, Kin, Lin, restOfReflectionLine Open an existing (old) file whose name is stored in the character variable filename Read a line from that file, take the integer number in the first 4 columns and store it in variable Hin, take the number in the next 4 columns and store it in Kin, the next 4 in Lin, and store the next 32 columns as alphanumeric data in the character variable restOfReflectionLine. If we run out of data while trying to read from this file, then the variable endOfFileFlag will be set to a non-zero value; otherwise it will be set to 0.

File This Away write( 6, *) 'Output.sca file:' read( 5, '(a)') filename open( unit=2, file=filename, status='unknown') write( 2, '(i4,i4,i4,a32)') Hout, Kout, Lout, restOfReflectionLine Open this file, regardless of whether or not it already exists. Write the values in these variables to the file in the specified format. Section of output file:

Data Games nreflectionsRead = 0 Store a 0 in this variable nreflectionsRead = nreflectionsRead + 1 Take the amount stored in this variable, add 1 to it, and store the result back in the same variable.

More Fun with Variables Hout = m11 * Hin + m12 * Kin + m13 * Lin + - * / sin( ) asin( ) cos( ) acos( ) tan( ) atan( ) mod( a, b) -- remainder of a/b highest_resolution = wavelength / ( 2. * sin( 0.5 * twoTheta_max_radians)) Use parentheses liberally to force the order of evaluation Calculate the sine of the following expression in parentheses Variables

Decisions if ( min_xtl_detector_dist.le. 0.0 ) then write( 6, *) 'This distance must be greater than 0.0 mm.' stop end if.eq. =.lt. <.gt. >.le. < or =.ge. > or =.ne. not = Only if the expression in parentheses is true will the statements in between be executed.

if ( ( Hout.eq. 0 ).and. ( Kout.eq. 0 ).and. ( mod( Lout, 2).ne. 0 ) ) then write( 6, '( " ** Tossed ** ", 3i4, a32)' ) Hout, Kout, Lout, restOfReflectionLine nsysAbsencesTossed = nsysAbsencesTossed + 1 else write( 2, '(3i4,a32)') Hout, Kout, Lout, restOfReflectionLine nreflectionsWritten = nreflectionsWritten + 1 end if.and..or..not. Only if all 3 of these conditions are met will the statements between the then and else be executed; else if not, the statements between the else and end if will be executed Tough Decisions

Loop-de-Loop do i = 10, 20, 2 write( 6, *) i end do A simple loop: "Begin by setting the variable i to 10. Execute all statements up to the end do, then go back up to the beginning of the loop, increment i by 2, and repeat until i exceeds 20."

do xtl_detector_dist = min_xtl_detector_dist, max_xtl_detector_dist, xtl_detector_dist_increment twoTheta_max_radians = atan( 0.5 * detector_width / xtl_detector_dist) twoTheta_max_degrees = twoTheta_max_radians * 180. / highest_resolution = wavelength / ( 2. * sin( 0.5 * twoTheta_max_radians)) write( 6, '(5x, f6.1, 19x, f5.2, 21x, f5.1)') xtl_detector_dist, highest_resolution, twoTheta_max_degrees end do When this loop is reached, the variable xtl_detector_dist will be set to the value in variable min_xtl_detector_dist. Do (execute) all statements up to the end of the loop, indicated by end do. At the bottom of the loop, xtl_detector_dist_increment is added to xtl_detector_dist. As long as xtl_detector_dist does not exceed max_xtl_detector_distance, the loop repeats. Loop-de-Loop, part Deux

endOfFileFlag = 0 do while ( endOfFileFlag.eq. 0 ) read( 1, '(3i4,a32)', iostat = endOfFileFlag ) Hin, Kin, Lin, restOfReflectionLine if ( endOfFileFlag.eq. 0 ) then nreflectionsRead = nreflectionsRead + 1 Hout = m11 * Hin + m12 * Kin + m13 * Lin Kout = m21 * Hin + m22 * Kin + m23 * Lin Lout = m31 * Hin + m32 * Kin + m33 * Lin. end if end do Repeat this loop as long as the condition in parentheses is true How long do I have to keep doing this? Note: The indenting is not required, but helps make the program more legible.

write( 6, '(a)') ' Xtl-Det Dist (mm) Highest Resolution (A) 2theta_max (deg)' write( 6, '(a)') ' ' ! '.....####.# ##.## ###.#' write( 6, '(5x, f6.1, 19x, f5.2, 21x, f5.1)') xtl_detector_dist, highest_resolution, twoTheta_max_degrees 5x -- skip 5 spaces f write a real value in a total of 6 spaces, with 1 decimal digit i5 -- write an integer value in 5 spaces a7 -- write 7 characters of a character variable How do you want that written? The same format specifiers can be used for reading too.

Compiling and Running your Jiffy g77 yourProgam.f -o yourProgram -ffixed-line-length-255 To compile (ie, convert your program into the computer's native tongue) To run: yourProgram