Difference between revisions of "Fortran"
From MohidWiki
(→Do) |
|||
Line 157: | Line 157: | ||
===Variables=== | ===Variables=== | ||
+ | The variable declaration is as such | ||
+ | |||
+ | <htm> | ||
+ | <pre name="code"class="fortran"> | ||
+ | real :: x, y | ||
+ | real, parameter :: z | ||
+ | integer :: m, n | ||
+ | real(8) :: w | ||
+ | character(len=30) :: frase | ||
+ | </pre> | ||
+ | </htm> | ||
+ | |||
+ | ===Arrays=== | ||
+ | <htm> | ||
+ | <pre name="code"class="fortran"> | ||
+ | real, dimension(3) :: vector | ||
+ | real, dimension(3,2) :: matriz3x2 | ||
+ | integer, dimension(:) :: vector-n | ||
+ | integer, dimension(:,:) :: matriz-mxn | ||
+ | |||
+ | !alocação do espaço em memória | ||
+ | allocate(vector-n(21)) | ||
+ | allocate(matriz-mxn(4,5)) | ||
+ | ... | ||
+ | !Limpar a casa no final do programa | ||
+ | deallocate(vector-n) | ||
+ | deallocate(matriz-mxn) | ||
+ | </pre> | ||
+ | </htm> | ||
===Types=== | ===Types=== | ||
+ | <htm> | ||
+ | <pre name="code"class="fortran"> | ||
+ | public :: T_Pais | ||
+ | type T_Pais | ||
+ | character(len=128) :: nome | ||
+ | character(len=128) :: capital | ||
+ | integer :: pop | ||
+ | end type T_Pais | ||
+ | </pre> | ||
+ | </htm> | ||
===Interface procedure=== | ===Interface procedure=== | ||
− | |||
− | |||
===If=== | ===If=== |
Revision as of 14:24, 21 June 2010
Fortran95 is the language of choice to program in MOHID. Although a primitive form of language it proves to be the most efficient and performant in the area of scientific computing. Still widely used in numerical computation. Here's a crash course. Here's a free manual.
Contents
Examples
Hello World program
program helloworld implicit none write (*,*) "Hello world!" end program helloworld
Hello world in a module
module modulehelloworld contains subroutine showhelloworld() write(*,*) "Hello world!" end subroutine showhelloworld end module modulehelloworld
program helloworld use modulehelloworld, only: showhelloworld implicit none call showhelloworld end program helloworld
Subroutine
program subfunc implicit none real :: a,b,c,root1,root2 logical :: realroots write (*,*) "Hello world!" write (*,10) read (*,*) a,b,c call solvit(a,b,c,root1,root2,realroots) write (*,20) root1,root2 if (realroots) then write(*,*) 'Sorry, there are no real roots' end if 10 format('Enter 3 coefficients') 20 format('The roots are ', 2f12.6) end program subfunc
subroutine solvit(a,b,c,root1,root2,realroots) !Arguments real ::a,b,c,root1,root2 logical :: realroots !Locals real :: test test = b**2-4*a*c if(test>=0.0) then root1 = (-b + sqrt(test))/(2.0*a) root2 = (-b - sqrt(test))/(2.0*a) realroots = .true. else realroots = .false. end if return end subroutine solvit
Function
program subfunc implicit none real :: a,b,c,root1,root2,bigroot logical :: realroots write (*,*) "Hello world!" write (*,10) read (*,*) a,b,c !call solvit(a,b,c,root1,root2,realroots) !write (*,20) root1,root2 write(*,20) bigroot(a,b,c) if (realroots) then write(*,*) 'Sorry, there are no real roots' end if 10 format('Enter 3 coefficients') !20 format('The roots are ', 2f12.6) 20 format('The biggest root is ', f12.6) end program subfunc
function bigroot(a,b,c) !Arguments real :: a,b,c,bigroot !Local real :: root1, root2, test if(test>=0.0) then root1 = (-b + sqrt(test))/(2.0*a) root2 = (-b - sqrt(test))/(2.0*a) if (root1 .gt. root2) then bigroot = root1 else bigroot = root2 end if else bigroot = -9.0e35 end if return end function bigroot
Variables
The variable declaration is as such
real :: x, y real, parameter :: z integer :: m, n real(8) :: w character(len=30) :: frase
Arrays
real, dimension(3) :: vector real, dimension(3,2) :: matriz3x2 integer, dimension(:) :: vector-n integer, dimension(:,:) :: matriz-mxn !alocação do espaço em memória allocate(vector-n(21)) allocate(matriz-mxn(4,5)) ... !Limpar a casa no final do programa deallocate(vector-n) deallocate(matriz-mxn)
Types
public :: T_Pais type T_Pais character(len=128) :: nome character(len=128) :: capital integer :: pop end type T_Pais
Interface procedure
If
Do
Loop that increments the counter.
do i=1,10 write(*,*) 'Couting ',i end do
Loop that increments the counter by two.
do i=1,21,2 write(*,*) 'Counting pair ',i-1 end do
Loop that decrements the counter.
do i=20,1,-1 write(*,*) 'Reverse counting of even ',i*2-1 end do
===Case===
==Sample==
program average ! Read in some numbers and take the average ! As written, if there are no data points, an average of zero is returned ! While this may not be desired behavior, it keeps this example simple implicit none real, dimension(:), allocatable :: points integer :: number_of_points real :: average_points=0., positive_average=0., negative_average=0. write (*,*) "Input number of points to average:" read (*,*) number_of_points allocate (points(number_of_points)) write (*,*) "Enter the points to average:" read (*,*) points ! Take the average by summing points and dividing by number_of_points if (number_of_points > 0) average_points = sum(points) / number_of_points ! Now form average over positive and negative points only if (count(points > 0.) > 0) then positive_average = sum(points, points > 0.) / count(points > 0.) end if if (count(points < 0.) > 0) then negative_average = sum(points, points < 0.) / count(points < 0.) end if deallocate (points) ! Print result to terminal write (*,'(a,g12.4)') 'Average = ', average_points write (*,'(a,g12.4)') 'Average of positive points = ', positive_average write (*,'(a,g12.4)') 'Average of negative points = ', negative_average end program average