Posts

Showing posts with the label fortran

Calling Fortran intrinsic functions from Visual Basic

Image
There is a great way to call the Fortran intrinsic libraries from Visual Basic, without wrapping them to dynamic libraries. I have the Digital Fortran installed on my computer, so I found out one of the running DLLs which is the dforrt.dll and that contains the Fortran intrinsic libraries (which are used by the Digital compiler). I simply opened the DLL with the Depends program and I found out the name of the functions that are declared in the DLL file. Knowing that the tangent function is declared as DTAN in Fortran, I found out some functions and declared them in a Visual Basic module file like this: Public Declare Function DTAN# Lib "dforrt.dll" Alias "_FXDTAN" (x#) Public Declare Function DTAN5# Lib "dforrt.dll" Alias "_FXIDTAN" (x#) And here is a sample call: Const PI As Double = 3.14159265358979 'the tangent of PI/4 will return 1.0 Debug.Print DTAN(PI / 4) Of course the tangent function is an intrinsic one in Visual Basic. H...

Variable format in Fortran 90

Image
In Fortran there are nowadays many ways to write formatted output. In the following example, the same output may be given in 5 ways. The last two examples include a version of format that may include a variable: program Console1 implicit none integer(4)::i,num character(50)::f i=4;num=2; !#1 (old) print 10,i,i 10 format(2I3) !#2 print '(2I3)',i,i !#3 f='(2I3)' print f,i,i !#4 !convert from number to character and write to f write(f,'(I4)') num f='(' // trim(f) // 'I3)' print f,i,i !#5 (Intel compiler specific) print '(<num>I3)', i,i end program Console1

Characters in Fortran

Image
In Fortran, the alphanumeric (character) variables must have a specific length when declared. For example, let's see the following declarations: character(len=10) :: name, surname character :: firstLetter character(16) :: nickname The above declarations define two 10-character variables (name and surname), one 1-character variable named firstLetter and a 16-character variable named nickname. The older FORTRAN 77 syntax for the 'nickname' variable would be: character*16 nickname This form of type declaration statement has been declared OBSOLESCENT in Fortran 95 and should never be used in any new programs. Character literals Character literals are either between single or double quotes. If a character string is between simple quotes and includes an apostrophe then the apostrophe may be represented by two single quotes. The two following examples are equivalent: phrase = 'Man''s best try' phrase = "Man's best try" Depending on the case, single or...

Arrays in Fortran

Image
There are many type of declarations in Fortran referring to arrays that we should be aware of, before attempting to use any of them. Explicit-shape Arrays with constant bounds Explicit-shape arrays with constant bounds are non-dummy arrays whose shape is explicitly specified. They may be declared in the main program or inside the procedure body. They do not appear in the argument list of a procedure and they may be initialized in their type declaration statements. Here are two examples: integer, parameter :: n = 20 !non-initialized two dimensional array real, dimension(n,n) :: arr !initialized with lower bound -3 real, dimension(-3:5) :: arr2 = 8.0 Dummy Arrays Dummy arrays are arrays that appear in the argument list of procedures. No memory is allocated, because they should be allocated externally prior to be used. There are three types: Explicit-shape dummy arrays ' dimensions are explicitly declared by other arguments in the procedure's argument list. All the advanced featur...

A safer Read call in Fortran 95

Image
In Fortran, the validation process is a more painful procedure in comparison with other programming environments. In the following code, a much more safer version of the Read subroutine is called, named SafeReadInteger. The syntax and the definition of the function is shown in the following code segment: program TestSafeRead implicit none integer(4) :: iValue !call SafeReadInteger("Enter the number of cylinders", iValue, "Wrong stuff") call SafeReadInteger("Enter the number of cylinders", iValue) print '(A, I4)', "The entered number of cylinders is: ", iValue contains subroutine SafeReadInteger(promptMessage, outVariable, errorMessage) character(*), intent(in) :: promptMessage, errorMessage !the errorMessage is optional optional :: errorMessage integer(4), intent(out) :: outVariable !local variable to receive the integer value integer(4) :: iostatus do !print the promptMessage on the screen and !retain the cursor p...