fortran
Computing the cross product of two vectors in Fortran 90
The best practice is to place your procedures (subroutines and functions) in a module and then “use” that module from your main program or other procedures. You don’t need to “use” the module from other procedures of the same module. This will make the interface of the procedure explicit so that the calling program or … Read more
Unexpected “padding” in a Fortran unformatted file
For unformated IO, Fortran compilers typically write the length of the record at the beginning and end of the record. Most but not all compilers use four bytes. This aids in reading records, e.g., length at the end assists with a backspace operation. You can suppress this with the new Stream IO mode of Fortran … Read more
Why are the elements of an array formatted as zeros when they are multiplied by 1/2 or 1/3?
In Fortran 1/2 is an integer division operation which will round down to, in this case, 0. Same for 1/3. If you want a real result, do a real division operation, such as 1.0/2.0. Note that assigning the result of 1/2 to a real variable will set the real variable to 0.0, that is the … Read more
What is an undefined reference/unresolved external symbol error and how do I fix it in Fortran?
A link-time error like these messages can be for many of the same reasons as for more general uses of the linker, rather than just having compiled a Fortran program. Some of these are covered in the linked question about C++ linking and in another answer here: failing to specify the library, or providing them … Read more
Tentative definitions in C and linking
See also What are extern variables in C. This is mentioned in the C standard in informative Annex J as a common extension: J.5.11 Multiple external definitions There may be more than one external definition for the identifier of an object, with or without the explicit use of the keyword extern; if the definitions disagree, … Read more
Why Segmentation fault is happening in this openmp code?
The most probable cause for this behaviour is that your stack size limit is too small (for whatever reason). Since e_in is private to each OpenMP thread, one copy per thread is allocated on the thread stack (even if you have specified -heap-arrays!). 202000 elements of REAL(KIND=8) take 1616 kB (or 1579 KiB). The stack … Read more
Convert integers to strings to create output filenames at run time
you can write to a unit, but you can also write to a string program foo character(len=1024) :: filename write (filename, “(A5,I2)”) “hello”, 10 print *, trim(filename) end program Please note (this is the second trick I was talking about) that you can also build a format string programmatically. program foo character(len=1024) :: filename character(len=1024) … Read more
Fortran: integer*4 vs integer(4) vs integer(kind=4)
In Fortran >=90, the best approach is use intrinsic functions to specify the precision you need — this guarantees both portability and that you get the precision that you need. For example, to obtain integers i and my_int that will support at least 8 decimal digits, you could use: integer, parameter :: RegInt_K = selected_int_kind … Read more
Fortran 90 kind parameter
The KIND of a variable is an integer label which tells the compiler which of its supported kinds it should use. Beware that although it is common for the KIND parameter to be the same as the number of bytes stored in a variable of that KIND, it is not required by the Fortran standard. … Read more