Posts

Showing posts with the label FORTRAN

Coloured terminal output from Fortran

I was just curious to print some of the error messages from fortran code in different color. So I ended up with this suggestion after doing little bit google search - print *, 'Test color print -> '//achar(27)//'[31m Red'//achar(27)//'[0m.' Color codes are: [90m=dark grey [30m=black [91m=peach [31m=red [92m=light green [32m=green [93m=light yellow [33m=yellow [94m=light blue [34m=blue [95m=pink [35m=purple [96m=light aqua [36m=aqua [97m=pearl white

Usefull tips for FORTRAN

Few tips for FORTRAN: Get arguments from command line character (len = 20) :: arg1 call getarg (1, arg1) if (len_trim(arg1) == 0) then     print*,'No Arg1 provided' stop endif read (arg1, *) nsteps In this example FORTRAN takes the first arguments and read as number of steps. Always use the warning if loop for multiple argument, sometimes one might miss to give inputs of one argument and it becomes difficult to debug the code. Use bash shell command in FORTRAN character (len = 1024) :: command command = 'cp file1 folder1/' call system ( trim(command)) In this example file1 is copied to folder1/ Print integer padded by zeros Let say we would like to print 4 digit integer as 0001 0002 .... 9999 only thing we have use a formatting string I4.4 and we are done. Get number of lines in a data file open(unit,file='filename') nline = 0 do     read (unit, *, end = 10)     nlines = nlines + 1 end do 10 close(unit)

Fortran code for sorting a data array

This program can be used to sort a set of data array either in ascending or descending order. program sort_data implicit none integer, parameter :: array_length = 100 integer :: i, count double precision, dimension(1:array_length) :: data double precision :: temp logical :: not_sorted open(unit=1, file='data.input', action='read', status='old') read(1,*) (data(i), i=1, array_length) not_sorted = .TRUE. do while (not_sorted)      count = 0      do i = 1, state - 1           temp = data(i) ! if one needs to get ascending order need to change here .LT. to .GT.          if (data(i) .LT. data(i + 1)) then             data(i) = data(i+1)             data(i+1) = temp             count = count          else             count = count + 1         ...

Handle large statically allocated data in fortran (> 2GB)

Typical error occurs in this case fortranfile.f:someline numbe: relocation truncated to fit: R_X86_64_PC32 against `.bss' fortranfile.f:someline numbe: additional relocation overflows omitted from the output Solution: Compile the fortran file with the following option -mcmodel medium -shared-intel Source Link: Intel Forum