Difference between revisions of "Fortran"
From MohidWiki
(→Hello world in a module) |
(→Subroutine) |
||
Line 52: | Line 52: | ||
===Subroutine=== | ===Subroutine=== | ||
+ | <htm> | ||
+ | <pre name="code"class="fortran"> | ||
+ | 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 | ||
+ | </pre> | ||
+ | </htm> | ||
===Function=== | ===Function=== |
Revision as of 02:30, 21 May 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.
Contents
MOHID templates
Module
Program
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
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
Variables
Types
Interface procedure
Arrays
If
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