Fortran IO and Subroutines Dr Theodore Cleveland University
Fortran I/O and Sub-routines Dr. Theodore Cleveland University of Houston CIVE 1331 – Computing for Engineers Lecture 014 - 015
How to write Programs in the ECC Environment Overview n Open IDE w Create Project File w Create Source File w Edit Source File (Write Code) w Build Source (Make. exe) w Execute Program (Run. exe)
Opening IDE From The Start Menu Locate “Microsoft Visual C++ 6. 0” group Click on the “Microsoft Visual C++ 6. 0” Icon in the group This should launch the Microsoft C++/Fortran IDE
Creating a Project From the “File” menu Select “New” The dialog to the right will pop up. Select the Projects Tab Select “Fortran Console Application” Enter a Project name (e. g. Sample 1) Change the Location if necessary (the default is to your home (“h: ”) drive, specifically “H: Fortran[Project_Name] Click “OK”
Creating a Project (2) Next a Project Template screen will popup Select “An Empty Project” (the default) Click the “Finish” button
Creating a Project (3) Finally you will see a confirmation screen Click the “OK” button
Adding a Source File From the “File” menu, select “New” The dialog to the right will pop-up. Under the Files tab, select “Fortran Fixed Format Source File” Type in a name under “File name” textbox (e. g. sample 1)* the extension “. for” will automatically be added. Click the “OK” button
Editing the Source File A window will now appear with the name of the source file on the title bar. A green box will indicate the 6 th column position. Use the [Tab] key to position the cursor on the 7 th column position.
Finally, Write Some Code! Begin writing your program Again notice that the green bar is in the 6 th column position
Build Your Program The “Build” Menu allows you to compile, link, and/or execute your program. Recall that the two steps needed to create an executable file are compiling and linking, together they are called “Building” To check to see if your program has Syntax errors, it’s quicker to just “Compile” it.
Executing your Program To Run your program, Select the “Execute [Project_Name]. exe” option. It will prompt you to build your project if there have been changes to your source since your last build. Recall that Compiling creates an Object file (. obj), and Linking Creates the Executable(. exe).
Standard Input and Output Your program will run in a “Console” window. This is similar to an MS-DOS screen WRITE statements to the Standard Output device (*) will appear in the window as text READ statements using the Standard Input (*) can be read from data entered in the console with your keyboard *You must “send” your data with the [Enter] key before Fortran can process it.
Where Are all these Files? The following files are all created by the Microsoft IDE in our Sample 1 Project Most of the Files are used by the IDE to keep track of Project Settings. You will only be asked to turn in the source file (. for)– DO NOT turn in project files Note that if the executable (. exe) file, or object (. obj) is created, it will be stored in the “Debug” folder.
When I Double-Click on My Program Nothing Happens!? In many cases a program finishes running so fast that you don’t see it or it looks like a flash. To view the program: n Open a console window. Select “Run” form the “Start Menu” and type “cmd” and [Enter] n n Navigate to the file (using DOS commands) Type the name of the file
More on Input and Output A Short Review: Assuming we have the following files set up OPEN (UNIT=10, FILE=‘input. ext’, STATUS=‘old’) OPEN (UNIT=11, FILE=‘output. ext’, STATUS=‘new’) We could store 10 values from an input file into an Array (Area) the following code: DO 5, i=1, 10 READ(10, *) Area(i) 5 CONTINUE However, note that this will only “Read” the first value on each line of the input file
Input/Output with Arrays Recall, Alternatively we could read 10 data elements with the following statement READ(10, *) (Area(i), i=1, 10) This code can be interpreted as READ(10, *) Area(1), Area(2), Area(3), Area(4), Area(5) + Area(6), Area(7), Area(8), Area(9), Area(10) This method will read up to 10 values from the first line. Additionally we can adjust the number of values we want to read per line by using a variable for the upper limit of the loop. e. g. READ(10, *) (Area(i), i=1, Num. In. Row)
Input/Output with Arrays Using the aforementioned method of reading and writing data is very handy when dealing with Arrays. Consider the following Code: READ(10, *) NROWS, NCOLS DO 5, I=1, NROWS READ(10, *) (WIDTH(I, J), J=1, NCOLS) 5 CONTINUE
Subroutines The programs that we have dealt with so far have been relatively simple programs. As programs begin to become longer and more complex, they become more difficult to follow and “read”. In addition, in many situations the problems which the program is defined to solve may involve repetitious operations of a set of executable statements. In these situations it will become more efficient to construct a series of expressions with a “subprogram” within the main program. Subprograms are usually referred to as SUBROUTINEs. The use of subroutines not only improves the efficiency of a program, but also make the program much easier to “follow” or read.
Consider the Following C C Note, we rely on integer divide to determine the number of whole Minutes DO 15, I=1, NROWS DO 5, I=1, NCOLS A 2(I, J) = A(I, J) / 60 5 CONTINUE Write(10, *) ‘Minutes ’ 15 CONTINUE DO 25, I=1, NROWS write(10, *) (A 2(I, J), + J=1, NCOLS) 25 CONTINUE DO 35, I=1, NROWS DO 45, I=1, NCOLS A 2(I, J) = A 2(I, J) * 60 45 CONTINUE 35 CONTINUE Write(10, *) ‘Seconds in Whole Minutes’ DO 55, I=1, NROWS write(10, *) (A(I, J), J=1, NCOLS) 55 CONTINUE DO 65, I=1, NROWS DO 75, I=1, NCOLS REM(I, J) = A(I, J)-A 2(I, J) 75 CONTINUE 65 CONTINUE Write(10, *) ‘Remainder Seconds’ DO 55, I=1, NROWS write(10, *) (A(I, J), J=1, NCOLS) 55 CONTINUE CALL MATDIV(A, 60, A 2, NRows, NCols) WRITE(10, *) ‘Minutes’ CALL SHOWMAT(A 2, NRows, NCOls) CALL MATMULT(A, 60, A 2, NRows, NCols) WRITE(10, *) ‘Seconds in Whole Minutes’ CALL SHOWMAT(A, NRows, NCOls) CALL MATDIFF(REM, A, A 2, NRows, NCols) Write(10, *) ‘Remainder Seconds’ CALL SHOWMAT(REM, NRows, NCOls) Note: We choose meaningful names for Subroutine Names. The Names do not correspond with data types as with Variables!
Subroutines Creating Subroutines: 1. 2. 3. 4. 5. 6. Declare the subroutine with: SUBROUTINE subroutine_name (argument_list) Declare or Dimension the Variable Types of arguments_list AND Variables used for SUBROUTINE Write body of Subroutine Ensure that a RETURN Statement is encountered in the code, (usually at the end of subroutine) Close subroutine with an END Statement Executing Subroutines Execute Subroutines with: CALL subroutine_name (argument_list)
Example SUBROUTINE MATMULT(INMAT, OUTMAT, C, ROWS, COLS) Dimension INMAT(ROWS, COLS), OUTMAT(ROWS, COLS) DO 5, I=1, NROWS DO 15, J=1, NCOLS OUTMAT(I, J) = INMAT(I, J) * C 5 CONTINUE 15 CONTINUE RETURN END … CALL MATMULT(A, A 2, 3, NROWS, NCOLS)
Subroutine Arguments in a Fortran Subroutine are used to transfer data to and from a subroutine. A subroutine can “return” values by changing values in the argument list. Arguments are the ONLY way to transfer data between the main program and a subroutine! The arguments which are used in the CALL statement are the actual arguments (Sometimes called Formal Parameters) The arguments in the SUBROUTINE statement are the dummy arguments. The arguments in the CALL statement must match in type, number and order those used in the subroutine definition. They are matched by the order they are entered i. e. the first value in the CALL Statement is passed to the first parameter in the Subroutine’s argument list.
Subroutine Be very careful with the dimension statements for multidimensional arrays in subroutines. You should always pass the dimensional size [DIMENSION A(100, 100)], and the size which actually used [NROWS, NCOLS] as arguments For example: Say that we have an array A in the main program which has been dimensioned: instead of DIMENSION A(10, 10) DIMENSION A(100, 100) …
Subroutines Recall that Arrays are groups of variables with sequential blocks of memory addresses. FORTRAN Determines what address to look at based on an Arrays Dimension and the indices of the Array Variable A(3, 4) If the Array is Dimensioned as A(100, 100) A(3, 4) refers to the 304 th address in the array If the Array is Dimensioned as A(10, 10) A(3, 4) refers to the 34 th address in the array!!!
Final Notes: Subroutines require a RETURN statement to return control to the main program or another subroutine which calls it. An END statement is also required. A subroutine may reference other subroutines, however it cannot reference itself.
Next Time Intro to Matlab
- Slides: 26