fortran95

Scoping rules for variable and functions in contained subroutines

纵然是瞬间 提交于 2019-11-29 16:41:17
I have a problem understanding why a variable ( i ) declared in a subroutine is seen in a contained subroutine, but that this is not true for a function ( fie ) which results in a compilation error. I searched for an answer and also tried to see if I could find something in the Fortran 95 standard but in vain. I wrote a small example program: program pgm call a end subroutine a implicit none integer :: i double precision :: fie i = 7 call b !write(*,*) fie(9) contains subroutine b double precision :: x !double precision :: fie x = i x = x + fie(i) write(*,*) x end subroutine end subroutine

How to make some generic programming in fortran 90/95 working with intrinsic types

感情迁移 提交于 2019-11-29 11:18:29
I would like to program some procedure that will work with different types. I am planning to use the "include" method used in flibs described here and here . I give here a simple exemple. ! -------------------------------------------------------------- ! module data_type type ivalue integer :: v end type type rvalue real(8) :: v end type end module data_type ! -------------------------------------------------------------- ! module imod use data_type, only: T => ivalue include "template.f90" end module imod ! -------------------------------------------------------------- ! module rmod use data

FORTRAN functions

不打扰是莪最后的温柔 提交于 2019-11-29 10:56:55
I'm working on a project that needs to implement few numerical methods in FORTRAN. For this, I need to write some recursive functions. Here is my code. ! ! File: main.F95 ! RECURSIVE FUNCTION integrate(n) RESULT(rv) IMPLICIT NONE DOUBLE PRECISION :: rv INTEGER, INTENT(IN) :: n DOUBLE PRECISION, PARAMETER :: minusone = -1.0 IF (n == 1) THEN rv = 10 !exp(minusone) RETURN ELSE rv = 1 - (n * integrate(n - 1)) RETURN END IF END FUNCTION integrate RECURSIVE FUNCTION factorial(n) RESULT(res) INTEGER res, n IF (n .EQ. 0) THEN res = 1 ELSE res = n * factorial(n - 1) END IF END PROGRAM main DOUBLE

Proper use of the PURE keyword Fortran

一笑奈何 提交于 2019-11-29 09:13:04
I'm currently delving into Fortran and I've come across the pure keyword specifying functions/subroutines that have no side effects. I have a book, Fortran 90/95 by S Chapman which introduces the pure keyword but strangely provides no "good coding practice" uses. I'm wondering how liberally one should use this keyword in their procedures. Just by looking around it's obvious to me that most procedures without side effects don't find it necessary to include the pure keyword. So where is it best used? Only in procedures one wants to completely guarantee have no side effects? Or perhaps in

Scoping rules for variable and functions in contained subroutines

左心房为你撑大大i 提交于 2019-11-28 11:33:49
问题 I have a problem understanding why a variable ( i ) declared in a subroutine is seen in a contained subroutine, but that this is not true for a function ( fie ) which results in a compilation error. I searched for an answer and also tried to see if I could find something in the Fortran 95 standard but in vain. I wrote a small example program: program pgm call a end subroutine a implicit none integer :: i double precision :: fie i = 7 call b !write(*,*) fie(9) contains subroutine b double

What is the purpose of result variables in Fortran?

怎甘沉沦 提交于 2019-11-28 11:28:50
In Fortran, there are two standard ways to return a result from a function. The first one is by assigning the return value of the function to the function name. function foo() integer :: foo foo = 10 end function foo The second form, standardized in Fortran 90 is through a "result" variable. function foo result(res) integer :: res res = 10 end function foo Calling either form of the function returns the value 10. My question is, what was the rationale of the Fortran 90 committee for introducing result variables? Were they standardizing a common practice? Or were they allowing programs to be

getting free unit number in fortran

三世轮回 提交于 2019-11-28 11:04:28
I need to develop a library that opens a file and parses the stuff. The unit number, due to fortran IO style, must be decided by me, but I can't know what other units are open in the client code. Is there a standard function like give_me_any_unit_number_that_is_free() ? Jonathan Dursi In fortran 2008, there's a newunit clause to open that you can use integer :: myunit .. open(newunit=myunit,file='file.dat') ... close(myunit) but that's new enough that not all compilers support it yet. If yours doesn't yet, you can mock one up yourself; there's a good example on the fortran wiki . john You can

Proper use of the PURE keyword Fortran

一笑奈何 提交于 2019-11-28 02:36:31
问题 I'm currently delving into Fortran and I've come across the pure keyword specifying functions/subroutines that have no side effects. I have a book, Fortran 90/95 by S Chapman which introduces the pure keyword but strangely provides no "good coding practice" uses. I'm wondering how liberally one should use this keyword in their procedures. Just by looking around it's obvious to me that most procedures without side effects don't find it necessary to include the pure keyword. So where is it best

Is there a standard way to check for Infinite and NaN in Fortran 90/95?

元气小坏坏 提交于 2019-11-28 00:41:21
I've been trying to find a standards-compliant way to check for Infinite and NaN values in Fortran 90/95 but it proved harder than I thought. I tried to manually create Inf and NaN variables using the binary representation described in IEEE 754, but I found no such functionality. I am aware of the intrinsic ieee_arithmetic module in Fortran 2003 with the ieee_is_nan() and ieee_is_finite() intrinsic functions. However it's not supported by all the compilers ( notably gfortran as of version 4.9). Defining infinity and NaN at the beginning like pinf = 1. / 0 and nan = 0. / 0 seems hackish to me

Fortran forall restrictions

末鹿安然 提交于 2019-11-27 18:19:05
问题 I tried to use forall to allocate dynamic arrays, but gfortran didn't like that. I also found out that write statements are forbidden in a forall block ,and I suspect read statements are too. What other functions/operations are not permitted in a forall block? Exactly what is this construct for, besides sometimes replacing do loops when order doesn't matter? I thought it would make coding more legible and elegant, especially showing when the order of operations are not important, but it seems