fortran90

NaN issue in fortran 90

大憨熊 提交于 2019-12-10 20:45:15
问题 I realize that if you write Real (Kind(0.d0))::x,y x = sqrt(-1.d0) y = sqrt(-1.d0) if (x == y) then write(*,*)'yep, they are equals', x endif It compiles ok using ifort. But nothing is written, the conditional is always false , did you notice that? why is this so? 回答1: NaN signifies not a number, and since there are many, different, reasons a calculation could give that result, they generally do not compare as equal against themselves. If you want to do nan-testing, fortran compilers that

Random number generator in PGI Fortran not so random

可紊 提交于 2019-12-10 20:36:05
问题 The following code just generates a simple triple of random numbers: program testrand integer, parameter :: nz = 160, nf = 160, nlt = 90 real :: tmpidx(3) integer :: idxarr(3), idx1, idx2, idx3, seed_size, ticks integer, allocatable :: seed(:) call random_seed(size=seed_size) allocate(seed(seed_size)) call system_clock(count=ticks) seed = ticks+37*(/(i-1, i=1,seed_size)/) call random_seed(put=seed) deallocate(seed) call random_number(tmpidx) idxarr = tmpidx * (/nz, nf, nlt/) idx1 = max(1

Random number generator (RNG/PRNG) that returns updated value of seed

本秂侑毒 提交于 2019-12-10 19:48:15
问题 I'm trying to write an RNG that also returns the value of the updated seed. The perhaps obvious reason for this is so that new random variables can be added to the program later, without changing the values of the existing RNGs. For a python/numpy version of this issue see for example: Difference between RandomState and seed in numpy Here's a example of usage with a (tentative) proposed solution: program main ! note that I am assuming size=12 for the random ! seed but this is implementation

How to pass character array into string

蹲街弑〆低调 提交于 2019-12-10 18:47:51
问题 I am wondering how could I go from a character array to several character strings. Indeed, I have a character array containing 17 file path. Lets say : character, dimension(29,17) :: FILE_SIM_all character, length(29) :: FILE_SIM ! Declarations end FILE_SIM_all(1:29,1) = "/Users/toto/Documents/toto.nc" FILE_SIM_all(1:29,2) = etc... I would like to convert recursively (inside a for loop with sim=1,17) the "sim" row of FILE_SIM_all to a character string. Lets say something like do sim=1,17 FILE

How does automatic typecasting (type conversion) work in Fortran?

倖福魔咒の 提交于 2019-12-10 18:35:05
问题 I am using gfortran compiler. Also tell me if gfortran uses something other than the Fortran standard while performing automatic typecasting (type conversion). 回答1: Assignment is defined by Fortran 2008 Section 7.2. Of note is Cl. 7.2.1.3 paragraph 8: For an intrinsic assignment statement where the variable is of numeric type, the expr may have a different numeric type or kind type parameter, in which case the value of expr is converted to the type and kind type parameter of the variable

Concise notation for inheriting size from other array?

泪湿孤枕 提交于 2019-12-10 18:12:26
问题 In my code, I have a subroutine that takes a 5th-rank array as argument and uses a local variable, which is a 4-th rank array sharing the first 4 indices. I'm trying to find a more concise way to express the size declaration in subroutine mysub(momentum) complex, intent(in) :: momentum(:,:,:,:,:) complex :: prefactor( & & size(momentum,1), size(momentum,2), size(momentum,4) & & size(momentum,5) ) ... end subroutine mysub The verbosity of the size declaration harms readability, especially when

variable length array in derived type

血红的双手。 提交于 2019-12-10 17:35:50
问题 I am mostly doing scientific programming in Python and do not have a whole lot of Fortran (90/95) experience. For one of my projects I want to define a derived type and overload a bunch of operators for that type. Critically, I'd like one of the variables of the derived type to an array of variable length; at least, I need two different lengths in different parts of the code. How can I best achieve this efficiently and avoiding code duplication? My first approach was to use an allocatable

Can we indeed avoid goto in all cases?

≯℡__Kan透↙ 提交于 2019-12-10 16:29:39
问题 Fortran 90 and later strongly recommend not to use goto statement. However, I still feel forced to use it in either of the two cases: Case 1 -- Instruct to re-enter the input value, e.g. program reenter 10 print*,'Enter a positive number' read*, n if (n < 0) then print*,'The number is negative!' goto 10 end if print*,'Root of the given number',sqrt(float(n)) stop end program reenter Case 2 -- To comment a large continuous part of a program (an equivalent to /* ... */ in C). Eg. print*,'This

difference between POINTER and ALLOCATABLE

北慕城南 提交于 2019-12-10 14:54:07
问题 what is the difference between these two codes type Foo real, allocatable :: bar(:) end type and type Foo real, pointer :: bar(:) end type in particular when it comes to the following code: type(Foo) :: myfoo allocate(myfoo%bar(10)) 回答1: I do not see any principal difference in that scenario. In general, ALLOCATABLE arrays are more efficient. But in Fortran 90/95 POINTER arrays were more flexible. For example, it was not possible to use ALLOCATABLE arrays as components of derived types.

Fortran 90 Differences in declaring allocatable array

喜欢而已 提交于 2019-12-10 14:47:44
问题 Is there a difference between integer, intent(in) :: n integer, dimension(:), allocatable :: a allocate(a(n)) and integer, intent(in) :: n integer, dimension(n) :: a In which situation would we use the first version? Perhaps I misunderstood allocatable array, is the second version even an allocatable array? 回答1: The second case indeed doesn't have a allocatable. It is, however, an automatic object. Ignoring the practical differences in memory use at the implementation level, there is a big