Fortran programming language - description, basic commands and functions

Table of contents:

Fortran programming language - description, basic commands and functions
Fortran programming language - description, basic commands and functions
Anonim

The Fortran programming language is used primarily for scientific computing. Invented in 1954, it is the oldest high-level programming language, followed by Lisp (1958), Algol (1958) and COBOL (1959). The number of scientific libraries written in Fortran and the creation of special translator-compilers make it possible to use the language today. In addition, multiple calculators have been created for vectorization, coprocessors, parallelism, which interspersed this language for use in the industrial production of the modern world.

Fortran milestones

John Backus, an IBM radio engineer, published articles in 1954 under the headings "Preliminary Report", "Specifications for the IBM Matmal Transmula TRANslating System", which gave rise to the term FORTRAN. It then took another two years of effort by a whole team he led to write the first Fortran programming language compiler (25,000 linesfor IBM 704).

Fortran milestones
Fortran milestones

The name of the language was originally capitalized FORTRAN and was used to denote language versions up to Fortran 77, as opposed to free syntax versions since Fortran 90. In the Fortran 77 standard, lowercase letters are not part of the language, but most compilers support them, in addition to the standard.

Today, the Fortran programming language is the dominant programming language used in engineering applications. Therefore, it is important that graduate engineers be able to read and modify Fortran code. From time to time, so-called experts predict that the language will lose its popularity and will soon cease to be used at all.

These predictions have always failed. Fortran is the most stable computer programming language in history. One of the main reasons the Fortran programming language has survived and will continue to survive is the inertia of the software. After a company has spent many resources and possibly millions of dollars on a software product, it is unlikely that they will translate the software into another language.

The main advantage of Fortran is that it is standardized by the international bodies ANSI and ISO. Therefore, if a program is written in ANSI, then it will run on any computer with a Fortran 77 compiler. This is important information. Thus, programs of the object-oriented programming language Fortran exist on different programming devices.

Language differenceversions

Difference in language versions
Difference in language versions

Steps in creating a language platform:

  1. In 1954-1957 the first compiler was developed from scratch. In those days there were no "high-level languages" (=HLL), most operating systems were simple, and memory was small, something like 16 Kb. The first compiler ran on the IBM 704. This HLL language was much more efficient than assembly programming and very popular in its day.
  2. Fortran II was published in 1958. FORTRAN III was developed the same year but never released to the general public.
  3. In 1961, FORTRAN IV was created. It contained improvements such as the implementation of the COMMON and EQUIVALENCE operators.
  4. In 1962, the ASA committee began developing a standard for the object-oriented programming language Fortran. This allowed the seller to use it in every new computer. And this fact made it even more popular HLL, the language became available on Apple systems and TRS80.
  5. In 1967, FORTRAN 66 was released, the world's first HLL standard. The publication of the standard meant that the language became more widely implemented than any other. By the mid-1970s, virtually every computer, mini or mainframe was equipped with the standard FORTRAN 66 language. The language used if statements, goto-statements, and spagethi programs. This structured programming became popular in the 60s and 70s.
  6. "Fortran" existed on punched cards in particular with the FMS system, optimizing the location of its sources until Fortran 90 introduced"free" syntax. In it, the Fortran array code starts from the 7th column and should not exceed 72 thousand characters.

It should also be noted that prior to Fortran 90, spaces didn't matter between the 7th and 72nd columns. So the cycle "DO I=1.5" can also be written "DOI=1.5". On the other hand, "DO I=1.5" is equivalent to "DOI=1.5".

Numerous industrial codes have been written in Nastran, NAG and IMSL-Fortran libraries. Compatibility of new versions with previous ones is important. For this reason, Fortran 90 is fully compatible with Fortran 77. However, later versions of the standard introduced incompatibilities.

Perfect Fortran 90 languages
Perfect Fortran 90 languages

The more advanced languages Fortran 90 and Fortran 95 soon followed, updated to the current Fortran-2003 standard. While modern compilers work indefinitely in all current versions of Windows and even support 64-bit processors. Meanwhile, manufacturers have recognized the trend of the times and are offering compilers for Linux in the form of the object-oriented programming language Actor Fortran.

Prerequisites for using a programming language

You need to understand that Fortran is still a widely used programming language and is mainly used in the field of discovery. Classic applications, such as physics or engineering, where extensive and complex mathematical calculations are performed. They are very useful extensive mathematical libraries that exist for different compilers. Summing up, it can be argued that today the Fortran languagestill used for a number of reasons:

  • Having numerous functional libraries developed over the years.
  • Having software in "Fortran" that requires very important development resources when switching to another language is considered too costly.
  • Having powerful compilers with built-in Fortran functions that produce very fast executables.
  • The language is more accessible to an inventor who has not had a specialized computer course.

Many scientific programs are now written in C and C++, compilers available on most machines. Other compiled languages are sometimes used for scientific computing, and especially for programs like Scilab or Matlab. The latter also include the BLAS and LAPACK libraries developed in Fortran programming. Matlab was originally a Fortran program distributed to universities and research centers.

Modern compilers

Modern compilers
Modern compilers

Although Tom Lahey is now the "only" general compiler, Lahey Computer Systems continues to be used by many programmers. Lahey has been working with Fujitsu for several years now, Lahey is concentrating on the Fortran parser and Fujitsu on the code generator. The current Compiler Suite for Windows is called Lahey Fujitsu Fortran 95 (LF95) and is available in various versions, some of which also integrate with Visual Studio. NET 2003.

There is alsoan inexpensive version of the LF95 Express without its own IDE. The current version is 7.1. on Linux is called by the Lahey/Fujitsu Fortran 95 v6.2 compiler for Linux and is available in two different versions. For example, the Pro version includes OpenMP v2.0 compatibility, a simple Winteracter Starter Kit graphics engine, a math library, and a scientific subroutine library from Fujitsu 2.

The other manufacturer is Absoft. Compilers and C++ exist not only for Windows and Linux, but also for OS X on the Macintosh. These compilers are of interest to developers who need or want to support all three platforms. Unfortunately, Absoft distinguishes between 32-bit and 64-bit versions under Linux, currently version 10.0 Fortran 95 is used for 64-bit Linux.

Relatively new to the market is the EKOPath Compiler Suite. This consists of the C++ compilers and the Fortran development environment for Linux, which are also available separately and are mainly aimed at 64-bit AMDusers. It also works on Intel EM64T. Microsoft also once tried to find a "cheap market" for Fortran and brought Microsoft Powerstation to the market.

Maybe the market was too small for the software giant, but Digital took over some of the code in 1997 and used its experience with Digital's Unix and OpenVMS compilers. This was the birth of the still very successful Digital Visual Fortran. At some point, Digital then moved to Compaq, the compiler was finalized to the current version of Compaq Visual Fortran (CVF) v6.6.

In addition to the "normal" 32-bit platforms, there are various 64-bit compilers thatfor example, for Intel Itanium and Intel EM64T. Although they are not "urgent" for the scope of delivery, they are available for free download through the Intel Premier web support system.

After a one-time, somewhat cumbersome registration, you can use it for a year, with new updates every few weeks. Even older versions will remain available.

Organizational structures of programs

Organizational structures of programs
Organizational structures of programs

A Fortran program is a sequence of lines of text. The text must follow a certain syntax. For example: circle of radius r, area c.

This program reads the actual radius and determines the area of a circle with radius r:

write (,);

'Radius r:'read (,) r;

area=3.14159rr;

write (,) 'Area=';

stop;

end.

Lines starting with "C" are comments and have no purpose other than to make the program more human readable. Initially, all Fortran programs were written in capital letters. Most programmers now write lower case as it is more legible.

A Fortran program usually consists of a main program or driver and a number of subroutines, procedures or subroutines. Main program structure:

  • program name;
  • declarations;
  • statements;
  • stop;
  • end.

Italics should not be taken as literal text, but rather as a general description. Operatorstop is optional and may seem redundant since the program will stop when it reaches the end anyway, but it is recommended that you always terminate the program with a stop statement to emphasize that the flow of execution is terminated.

Column position rules

Fortran 77 is not a free format language, but has a very strict set of rules for formatting source code. The most important rules are the column layout rules:

  • Col. 1: Blank or "c" or "" for comments.
  • Col. 2-5: operator label.
  • Col. 6: continuation of the previous line.
  • Col. 7-72: approval.
  • Col. 73- 80: Sequence number.

A Fortran line starting with a "c" or an asterisk in the first column is a comment. Comments can appear anywhere in the program. Well written, they are critical to the readability of a program. Commercial Fortran codes often have around 50% comments. You may also encounter programs that use an exclamation mark (!). This is very non-standard in Fortran 77 but allowed in Fortran 90.

The exclamation point can appear anywhere in a line. Sometimes the statement does not fit on one line, in which case it is possible to break the statement into two or more lines and use the continuation sign in position.

Example:

  1. C23456789 - this shows the position of the column.
  2. "C" - the next operator passes two areas of physical lines.
  3. Area=3.14159265358979+rr.

Empty spaces are ignored starting with "Fortran 77". Therefore, if all spaces are removed in Fortran 77, the program is still syntactically correct, although it is almost unreadable for operators.

Variables, types and declarations

Variables, types and declarations
Variables, types and declarations

Variable names in Fortran consist of 1-6 characters, chosen from the letters a-z and the numbers 0-9. The first character must be a letter. Fortran 90 allows variable names of arbitrary length. Fortran 77 does not distinguish between upper and lower case, in fact it assumes that all input is in upper case. However, almost all F77 compilers will accept lowercase letters. Every variable must be defined in a declaration. This sets the type of the variable. The most common variable lists are:

  • integer;
  • real;
  • double precision;
  • complex;
  • logical;
  • character.

The list of variables must consist of names separated by commas. Each variable must be declared exactly once. If the variable is not declared, F 77 uses a set of implicit rules to determine the type. This means that all variables beginning with the letters "in" are integers, and all others are real. Many older F 77 programs use these implicit rules, but programmers should not do this, as the chances of program errors increase dramatically if they declare variables inconsistently.

Fortran 77 has only one type for integersvariables. Integers are usually stored as 32-bit (4 bytes) variables. Therefore, all integer variables must take values in the range [-m, m], where m is approximately 210 9.

F 77 has two different types for floating point variables called real double precision. Some numerical calculations require very high precision and double precision should be used. Usually real is a 4-byte variable and double is 8 bytes, but it depends on the machine.

Non-standard versions of Fortran use the real8 syntax to denote 8-byte floating point variables. Some constants appear many times in a program. Therefore, it is desirable to define them only once, at the beginning of the program. For this, the parameter operator is used. It also makes programs more readable. For example, the program for the area of a circle would be written like this.

Circle area program
Circle area program

Parameter operator syntax (name=constant, …, name=constant). Rules for the parameter operator:

  1. The "variable" defined in the parameter statement is not a variable, but a constant whose value can never change.
  2. "Variable" can display at most one parameter statement.
  3. The operator parameter must come before the first executable operator

Some good reasons to use a parameter - helps reduce typos, easy to change a constant that appears many times in a program.

Booleanexpressions

Boolean expressions
Boolean expressions

Boolean expressions can only have the value. TRUE. or. FALSE. and can be formed by comparing arithmetic expressions using relational operators.

You cannot use characters such as "<" or "=" for comparison in F 77, but you can use the correct two-letter abbreviation enclosed by dots. However, such characters are allowed in Fortran 90.

Logical expressions can be combined with the logical operators "AND", "OR", "NOT", which have an obvious meaning. Truth values can be stored in boolean variables. Assignment is similar to arithmetic assignment.

Example: logical a, ba=. TRUE.b=a. AND. 3. LT. 5/2

The order of priority is very important. The rule is that arithmetic expressions are evaluated first, then relational operators, and finally logical operators. Therefore, b will be assigned. FALSE. In the example above, boolean variables are rarely used in Fortran, but they are often used in conditional statements such as the "if" statement.

Constant and assignment

The simplest form of an expression is a constant. There are 6 types of constants corresponding to 6 data types. Here are some integer constants: 10-10032767+15

Real constants:1, 0-0, 252.0E63.333E-1.

E-notation means to multiply the constant by 10 raised to the power following the "E". Therefore, 2.0E6 is two million and 3.333E-1 is roughly one third forconstants that are larger than the largest real allowable, or that require high precision, double precision should be used. The notation is the same as for real constants, except that "E" is replaced by "D".

Example:2.0D-11D99.

Here 2.0D-1 represents double precision with one fifth while 1D99 is one followed by 99 zeros.

The next type is complex constants. They are denoted by a pair of constants (integer or real), separated by a comma and enclosed in parentheses.

Examples are:(2, -3)(1, 9, 9E-1). The first number is the real part and the second is the imaginary part.

The fifth type is logical constants. They can only have one of two meanings:

. TRUE.

. FALSE.

Note that dots containing letters are mandatory.

The last type is character constants. They are most commonly used as an array of characters called a string. They consist of an arbitrary sequence of characters enclosed in apostrophes (single quotes):

'ABC'

'Anything goes!'

'It is a nice day'

String and character constants are case sensitive. The problem arises if you want to have a real apostrophe in the string itself. In this case, you need to double the apostrophe: 'It''s a nice day', which means 'What a wonderful day'

If conditional statements

Conditional "if" statements
Conditional "if" statements

The essential components of any programming language are conditional statements. Mosta common such statement in Fortran is the "if" statement, which actually has several forms.

The simplest is the logical expression "if" in the Fortran description: if (logical expression) executable statement.

This should be written on one line, for example when defining the absolute value of x:

if (x. LT. 0) x=-x

If more than one statement is to be executed in an "if", then the following syntax should be used: if (logical expression) thenstatementsendif.

Flow of execution from top to bottom. Conditional expressions are evaluated sequentially until a true value is found. The appropriate code is then executed and the control moves to the next statement after end "if".

If statements can be nested multiple levels. To ensure readability, it is important to use proper indentation. Here is an example:

if (x. GT. 0) thenif (x. GE. y) thenwrite(,) 'x is positive and x >=y'elsewrite(,) 'x is positive but x< y 'endifelseif (x. LT. 0) thenwrite(,) 'x is negative'elsewrite(,) 'x is zero'endif

Programmers should avoid nesting many levels of "if" statements as it will be difficult to follow.

Usage in Unix OS

Usage in Unix OS
Usage in Unix OS

You can use any Unix workstation with F 77 compiler. Experienced programmers recommend using either Sun or Dec.

A Fortran program consists of plain text that follows certain syntax rules. It is calledsource code. Programmers use an editor to write source code. The most common editors on Unix are emacs and vi, but they can be a bit difficult for beginners. You can use a simpler editor like xedit which works under X windows.

After a Fortran program is written, it is saved in a file with the extension ".f" or ".for" and the program is translated into a machine-readable form. This is done using a special program called a compiler. The Fortran 77 compiler is commonly referred to as f77. The compilation result is given the somewhat cryptic name "a.out" by default, but you can choose a different name if you like. To run the program, simply enter the name of the executable, such as "a.out". The compiler translates the source code into object code, and the linker or loader translates it into an executable file. As you can see, this procedure is not at all complicated and is available to any user.

Production simulation methods

Production modeling methods
Production modeling methods

Simulation is one of the most commonly used production methods and other systems available in today's factories. Most simulation models are built using the Actor Fortran object-oriented programming language, or a simulation software package written in a traditional language. These tools have their limitations. Object-oriented technology has seen increasing adoption in many areas and promises a more flexible and efficient approach tobusiness systems modeling.

The object-oriented programming languages Simula Fortran are compared to the conventional scientific programming language called FORTRAN. A typical military simulation model is programmed in both SIMULA and FORTRAN. The SIMULA program was 24% shorter than the FORTRAN version.

The SIMULA version is also simpler and gives a better picture of the simulated model. On the other hand, run times for production runs are 64% faster with the object-oriented programming language Simula Fortran. Weighing the pros and cons shows that SIMULA will be increasingly profitable software, with higher staff costs and lower computer costs.

CUDA shows how high performance application developers can harness the power of GPUs using Fortran, the familiar language for scientific computing and supercomputer performance testing. The authors assume no prior parallel computing experience and only cover the basics and best practices. GPU compute efficiency using CUDA Fortran is provided by the target GPU architecture.

CUDA Fortran for scientists and engineers will identify computationally intensive parts of the code and reshape the code for data management, parallelism and performance optimization. All this is done in Fortran, without the need to rewrite the program into another language. Each concept is illustrated with actual examples so you canimmediately evaluate the performance of the code.

Perhaps someday a global corporation will “finally globalize” and decide that Fortran is no longer needed, but not now. Thanks to the current capabilities of modern Fortran, many programmers and scientists see the future behind it. In addition, there are enough manufacturers in the world who live off the development of modern compilers and make good money in this process.

Popular topic

Editor's choice

  • IPv6 protocol: setup on Windows systems
    IPv6 protocol: setup on Windows systems

    Probably, many users of computer systems, delving into the network settings, noticed that in the list of protocols, in addition to the well-known IPv4, there is also the sixth version (IPv6)

  • View history on computer
    View history on computer

    Today you can learn about almost all the actions taken on the computer. It's about browsing history

  • Computer technology hardware: definition, description and types
    Computer technology hardware: definition, description and types

    Modern computers to ensure maximum performance and correct operation use hardware and software that are very interconnected and clearly interact in different directions. Now let's touch on the consideration of hardware, since initially it is they who occupy a dominant position in ensuring the operability of any computer or even mobile system

  • Keyboard shortcuts and the evolution of operating systems
    Keyboard shortcuts and the evolution of operating systems

    It takes about a minute to copy or move a file from one location to another using the menu system. The developers introduced a keyboard shortcut that replaced all these actions and made it possible to reduce the time of this common operation by 3 or more times. True, for this it was necessary to slightly modernize the keyboard: new keys were added - "Ctrl", and later - "Windows"

  • If the torrent does not download
    If the torrent does not download

    An article for those who consider the ability to download files from torrent trackers a convenience, not an echo of the past. Not only the unavailability of "high-speed" tariff plans is the reason for the choice, but also personal preferences