Difference between revisions of "HowTo:fortran"
(→Links and Further Reading) |
(→Overloading) |
||
Line 83: | Line 83: | ||
{| style="border-spacing: 8px;" | {| style="border-spacing: 8px;" | ||
− | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:# | + | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:#f7f7f7; border-radius:7px" | |
== High Precision == | == High Precision == |
Revision as of 20:26, 22 November 2016
Contents
Fortran (Programming Language)
FORTRAN, C, and C++ have a long history as the basic/main compiled languages for high performance computing. The key parallel computing packages, MPI and OpenMP, have been implemented in all of them from the beginning. While C and C++ have been extended for all programming purposes, FORTRAN originated from FORmular TRANslation, and developed with an emphasis on scientific computing. After the FORTRAN I-IV, 66, and 77 stages, the FORTRAN 90, 95, 2003, 2008, and 2015 versions have adopted many advanced features to become a true modern (object oriented) programming language, especially geared toward scientific computations. The following lists some of the most useful and prominent programming features of FORTRAN.
Well StructuredFORTRAN is very well structured. All routines should have a clear beginning statement, and a corresponding ending one. For example (since case-in-sensitiveness, usually written in either lower or upper case only) PROGRAM MY_VERY_USEFUL_CODE ... CALL PROBLEM_SOLVING (...) ... STOP END PROGRAM MY_VERY_USEFUL_CODE SUBROUTINE PROBLEM_SOLVING (...) ... RESULT = AVERAGE_SCORE (...) RETURN END SUBROUTINE PROBLEM_SOLVING FUNCTION AVERAGE_SCORE (...) ... RETURN END FUNCTION AVERAGE_SCORE The DO loop and IF structure are also finished with an END statement. DO I = ISTART, IEND ... END DO IF (CONDITION) ... ELSE ... END IF |
ModulesSimilar to classes in C++, modules are very important and widely-used in FORTRAN. Theoretically modules are not classes, but usually contain many objects, since in most scientific computations data structures are known and given objects. Modules can also contain specific routines operating on the objects inside, similar to the encapsulation concept of classes. Meanwhile modules are also a good method to share such objects, so that routines arguments can be reduced to necessaries only. OverloadingAs a modern language, FORTRAN also supports routine overloading. MODULE MY_KINETICS INTERFACE GENERIC_KINETIC SUBROUTINE KINETIC_ROUTINE_A(...) ... END SUBROUTINE KINETIC_ROUTINE_A SUBROUTINE KINETIC_ROUTINE_B(...) ... END SUBROUTINE KINETIC_ROUTINE_B SUBROUTINE KINETIC_ROUTINE_C(...) ... END SUBROUTINE KINETIC_ROUTINE_C ... END INTERFACE GENERIC_KINETIC END MODULE MY_KINETICS After this module is cited USE MY_KINETICS with each of the specific routines available, the call CALL GENERIC_KINETIC(...) will invoke the specific routine with the matching unique interface. In C++, overloading is a type of class polymorphism. |
High PrecisionMost FORTRAN compilers have built-in data types of very high precision, like quadruple precision REAL*16 :: VELOCITY(3,1000) COMPLEX*32 :: HAMILTON(1000, 1000) Dynamic Memory AllocationEarly versions of FORTRAN had a big drawback: they did not allow for dynamic memory allocation, forcing re-compilation array sizes were changed. Newer versions of FORTRAN (since F90) support such operations even for many-dimensional arrays. REAL*16, ALLOCATABLE :: COMPLICATED_DATA(:, :, :, :, :, :) ALLOCATE(COMPLICATED_DATA(3, 90, 80, 72, 500, 28)) in contrast to C/C++ where all arrays are allocated as one-dimensional. |
User Defined Data TypesFORTRAN also supports user defined data types: TYPE PERSON CHARACTER(LEN=10) :: NAME REAL :: AGE INTEGER :: ID END TYPE PERSON TYPE(PERSON) :: YOU, ME REAL :: DIFF YOU%ID = 12345 DIFF = YOU%AGE - ME%AGE |
Some Other Features
Links and Further Reading
|
HelpSend email to cac.help@queensu.ca. We have scientific programmers on staff who will probably be able to help you out. Of course, we can't do the coding for you but we do our best to get your code ready for parallel machines and clusters. |