fortran95

Passing a string inline to a subroutine call, where the parameter has defined length, gives unexpected results

天涯浪子 提交于 2019-12-20 01:35:46
问题 I found this code to behave unexpectedly module testmodule integer, parameter :: LCHARS = 50 contains subroutine init() call foobar("foobar") end subroutine subroutine foobar(s) character(len=*), intent(in) :: s call bar(s) end subroutine subroutine bar(str) character(len=LCHARS), intent(in) :: str print *, str end subroutine end module program foo use testmodule call init() end program This code prints garbage which is compiler dependent. I see the problem being the fact that I am jumping

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

a 夏天 提交于 2019-12-18 06:55:04
问题 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"

Numerical Precision in Fortran 95:

别说谁变了你拦得住时间么 提交于 2019-12-17 14:54:08
问题 I have the following Fortran code: Program Strange Real(Kind=8)::Pi1=3.1415926535897932384626433832795028841971693993751058209; Real(Kind=8)::Pi2=3.1415926535897932384626433832795028841971693993751058209_8; Print*, "Pi1=", Pi1; Print*, "Pi2=", Pi2; End Program Strange I compile with gfortran, and the output is: Pi1= 3.1415927410125732 Pi2= 3.1415926535897931 Of course the second is correct, but should this be the case? It seems like Pi1 is being input to memory as a single precision number,

Numerical Precision in Fortran 95:

﹥>﹥吖頭↗ 提交于 2019-12-17 14:53:22
问题 I have the following Fortran code: Program Strange Real(Kind=8)::Pi1=3.1415926535897932384626433832795028841971693993751058209; Real(Kind=8)::Pi2=3.1415926535897932384626433832795028841971693993751058209_8; Print*, "Pi1=", Pi1; Print*, "Pi2=", Pi2; End Program Strange I compile with gfortran, and the output is: Pi1= 3.1415927410125732 Pi2= 3.1415926535897931 Of course the second is correct, but should this be the case? It seems like Pi1 is being input to memory as a single precision number,

Writing multiple output files in Fortran

牧云@^-^@ 提交于 2019-12-17 14:21:11
问题 Dear All, I am writing a code that writes the out put in multiple files named as 1.dat, 2.dat, ..... Here is my code but it gives some unusual output. May you tell me what is wrong in my code please? Basically I could not get the correct syntax to open multiple files, write on them and close before the next file is opened. Thank you. My Code : implicit double precision (a-h,o-z),integer(i-n) dimension b(3300,78805),bb(78805) character*70,fn character*80,fnw nf = 3600 ! NUMBER OF FILES nj =

Writing multiple output files in Fortran

痞子三分冷 提交于 2019-12-17 14:20:13
问题 Dear All, I am writing a code that writes the out put in multiple files named as 1.dat, 2.dat, ..... Here is my code but it gives some unusual output. May you tell me what is wrong in my code please? Basically I could not get the correct syntax to open multiple files, write on them and close before the next file is opened. Thank you. My Code : implicit double precision (a-h,o-z),integer(i-n) dimension b(3300,78805),bb(78805) character*70,fn character*80,fnw nf = 3600 ! NUMBER OF FILES nj =

FORTRAN 95: is it possible to share a module without sharing the source code?

无人久伴 提交于 2019-12-13 15:11:47
问题 I would like to be able to share a FORTRAN 95 module without sharing its source code. Is it possible to do so (maybe by sharing the .MOD file)? In case this is relevant, I use Silverfrost FTN95 compiler on Plato. So far, I only manage to make this work by using the source code of the external module. Here is an example: file: module_test.f95 module TEST contains subroutine 1 code... end module TEST file: main_program.f95 include "module_test.f95" program MAIN_PROGRAM use TEST implicit none

fortran class declaration of dummy argument

醉酒当歌 提交于 2019-12-13 15:02:28
问题 I would like to have a derived type, a , which is empty. From this derived type I would like to define further types which extend a. Suppose all of these type extensions contain some generic procedure name, value , i.e value => valuea1 , value => valuea2 , etc. If I then want to pass variables of class a to some other procedure, I need to declare the relevant dummy argument of that procedure with class(a) . If I do this, however, then referencing the value of the dummy argument leads to

Data type mismatch in fortran

半城伤御伤魂 提交于 2019-12-13 02:55:25
问题 I've written a rudimentary algorithm in Fortran 95 to calculate the gradient of a function (an example of which is prescribed in the code) using central differences augmented with a procedure known as Richardson extrapolation. function f(n,x) ! The scalar multivariable function to be differentiated integer :: n real(kind = kind(1d0)) :: x(n), f f = x(1)**5.d0 + cos(x(2)) + log(x(3)) - sqrt(x(4)) end function f !=====! !=====! !=====! program gradient !=========================================

How to assign a pointer to a diagonal of a matrix?

只愿长相守 提交于 2019-12-12 23:21:04
问题 I have a matrix say REAL*8 MATRIX(100,100) , I want to create a pointer out of just the main diagonal or the upper triangle. How to do that? The purpose is to easily access those elements in a clean "named" way. 回答1: For the main diagonal, with care, you can do something like: PROGRAM diagonal IMPLICIT NONE REAL, TARGET :: array(4,4) REAL, POINTER :: ptr(:) INTEGER :: i array = RESHAPE([(i,i=1,SIZE(array))], SHAPE(array)) CALL get_diagonal_pointer(array, SIZE(array, 1), ptr) PRINT "(*(G0,:,',