fortran90

Reading a string with spaces in Fortran

雨燕双飞 提交于 2019-12-06 19:19:21
问题 Using read(asterisk, asterisk) in Fortran doesn't seem to work if the string to be read from the user contains spaces. Consider the following code: character(Len = 1000) :: input = ' ' read(*,*) input If the user enters the string "Hello, my name is John Doe", only "Hello," will be stored in input; everything after the space is disregarded. My assumption is that the compiler assumes that "Hello," is the first argument, and that "my" is the second, so to capture the other words, we'd have to

Interface mismatch in dummy procedure 'f' when passing a function to a subroutine

喜你入骨 提交于 2019-12-06 15:48:17
I am trying to write a subroutine (for minimisation) that has two arguments: an array x of any length a function f that takes an array of that length and returns a scalar example module: module foo contains subroutine solve(x, f) real, dimension(:), intent(inout) :: x interface real pure function f(y) import x real, dimension(size(x)), intent(in) :: y end function end interface print *, x print *, f(x) end subroutine end module and test program: use foo real, dimension(2) :: x = [1.0, 2.0] call solve(x, g) contains real pure function g(y) real, dimension(2), intent(in) :: y g = sum(y) end

How could a simple optional argument lead to data corruption?

蓝咒 提交于 2019-12-06 12:55:34
问题 I have a FORTRAN code with a routine: SUBROUTINE READ_NC_VALS(NCID, RECID, VARNAME, VARDATA) integer ncid, recid character*(*) varname real*8 vardata dimension vardata(15,45,75) etc. I want to add some flexibility to this code, and I thought I would do it by first adding an optional flag argument: SUBROUTINE READ_NC_VALS(NCID, RECID, VARNAME, VARDATA, how_to_calculate) ! everything the same and then ... logical, optional :: how_to_calculate Now, at this point, I am not even using "how_to

Using Gatherv for 2d Arrays in Fortran

烈酒焚心 提交于 2019-12-06 10:09:40
I have a number of 2d arrays of size = (2,9) on different processes, which I want to concatenate using MPI_Gatherv in a global array of size = (2*nProcs,9) on the root process. For this I'm trying to adapt this post: Sending 2D arrays in Fortran with MPI_Gather But I do really understand what they are doing and my example isn't working: program testing use mpi implicit none integer(4), allocatable :: local(:,:) integer(4), allocatable :: global(:,:), displs(:), counts(:) integer(4) :: me, nProcs, ierr, i integer(4), parameter :: root = 3 integer :: loc_size(2), glob_size(2), newtype, int_size,

Identify version of Fortran of this code for the LF compiler

南楼画角 提交于 2019-12-06 09:25:03
I'm new to Fortran. I was given a file that is supposed to be in Fortran 90, but written to be compiled with the Lahey Fujitsu compiler (the sparse documentation states that it should be compiled with lf95 filename.f -out compiled_name @imsllf95.cmd ). However, some lines are commented with c , which as I understand was the way to comment in Fortran 77. Also, matrices are declared like REAL*8, DIMENSION(23,8) :: xxx19 , which again I think is from Fortran 77. For the most part, I can compile the file with gfortran or ifort except for a section that requires the computation of a matrix inverse.

How to add new element to dynamical array in Fortran90

送分小仙女□ 提交于 2019-12-06 06:10:28
[SOLVED] by francescalus . Working code for double precision dynamical arrays is: module DynamicalArrays contains subroutine AddToList(list, element) IMPLICIT NONE integer :: i, isize double precision, intent(in) :: element double precision, dimension(:), allocatable, intent(inout) :: list double precision, dimension(:), allocatable :: clist if(allocated(list)) then isize = size(list) allocate(clist(isize+1)) do i=1,isize clist(i) = list(i) end do clist(isize+1) = element deallocate(list) call move_alloc(clist, list) else allocate(list(1)) list(1) = element end if end subroutine AddToList end

callback Python from Fortran

試著忘記壹切 提交于 2019-12-06 04:48:40
问题 Now I am using the f2py to call Python function from Fortran code. I have tried a very easy example but it didn't work. Fortran90 code: subroutine foo(fun,r) external fun integer ( kind = 4 ) i real ( kind = 8 ) r r=0.0D+00 do i= 1,5 r=r+fun(i) enddo end using the commandline: f2py -c -m callback callback.f90 Python code: import callback def f(i): return i * i print callback.foo(f) Error: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: `Required argument 'r'

Call Python function from Fortran/C

筅森魡賤 提交于 2019-12-06 02:50:42
I am writing a Fortran code and I would like to use some special functions and methods from Python libraries. This is a Python code: from mpmath import * from scipy.optimize import * def g(A,B,t): return newton(lambda x: (x - A*polylog(1.5, B*exp(-t*x))), 0.0) In fortran code I would like to pass real values A,B,t and get in return the value of g(A,B,t) (possibly complex). This can also be done in C. I would like to mention that mixing Python with other languages is something new to me. Bociek SOLUTION In case someone is interested I have solved this problem. The following was of great help:

Write data to file in columns (Fortran)

杀马特。学长 韩版系。学妹 提交于 2019-12-06 01:57:36
问题 I need to write some data to file in Fortran 90. How should I use WRITE (*,*) input to have the values grouped in columns ? WRITE always puts a new line after each call, that's the problem. code example: open (unit = 4, file = 'generated_trajectories1.dat', form='formatted') do time_nr=0, N write (4,*) dble(time_nr)*dt, initial_traj(time_nr) end do And now the point is to have it written in separate columns. 回答1: You can use implied DO loops to write values as single records. Compare the

`Relocation truncated to fit` error in Fortran with large arrays

£可爱£侵袭症+ 提交于 2019-12-06 01:52:13
问题 I have written a Fortran 90 code to extract angles from molecular simulation data. In this code I used a module with name all_parameter . In this module I defined an array such as: CH_Angles INTEGER,PARAMETER :: totalFrames = 32000 INTEGER,PARAMETER :: AAA=75 REAL,DIMENSION(45:AAA,1:256,1:totalFrames) :: CH_Angles If I use the value of AAA = 75 , I can compile this code without any error and I can get the values I wanted. But if I change the value of AAA to be AAA=105 , then I get some error