Creating a linear integer array in fortran using implied DO loop

program linarr
implicit none
integer:: idx, min, max, interval
myarray=(/ (idx, idx=min,max,interval) /)
write(*,*) myarray
end program linarr


How to get double precision pi constant in Fortran


program testpi
implicit none
double precision::mypi
write(*,*) mypi
write(*,*) sin(mypi)
end program testpi


This outputs:




Note that the sin(pi) is close to the value given in Mathworks:

Obtaining clock time using Fortran

to get the current time in Fortran, we can use “system_clock” built-in function. To do this, take a look at the code below.

integer::count_0,count_rate,count_max, walltime, tstart,tend
call system_clock(count_0,count_rate,count_max)
write(*,*) 'begin (system_clock)',tstart

To find out how long your program has run, add a similar line and save the time to, for example “t_end” variable, and get the difference. It will give the duration time in seconds.

Debugging OpenMP fortran code

As the code becomes parallel, the complexity increases. One of the common problem in parallel code is data race. To debug this problem, I encounter that Intel’s Thread Checker to be a good software. You can download from the Intel’s website for evaluation. I found about this when I read this article

When I run tcheck_cl, I realized that some parts has a data race. So I need to eliminate this. I couldn’t make it private since it will give segmentation fault (refer to this bug). So I simply add the !$omp critical to the section that has the data race. And it solves the problem.

Anyway, Intel’s Thread Checker really helped to identify which section of the code has the problem.

VTK error reading ascii data! when writing using Fortran

I write a fortran code and save the data in VTK legacy format with structured points. However, for certain data somehow Paraview and Mayavi are unable to read the VTK file. It gives a “Generic Warning”, and says that
Error reading ascii data!

After days of debugging, it turns out that the cause is my precision, I used double precision for my data, and there are some data with 3 digit for the exponents, and I write to the file using ES format. This turns out cannot be read by Paraview and Mayavi. So when I changed the format to F which is just a floating point with decimal points (no exponents), then paraview and mayavi2 can read my data. But this is of course is not a good format to store. And after looking for more detail in Fortran languange, I can set my digit in the exponent to only 2. This is the format in fortran


where “e” is the digit for the exponent.

The other thing is that In VTK, I set the data type as “float”, I need to change this to “double”.

What does !DEC$ mean in fortran

I encounter this symbol !DEC$ in fortran code, and tried to figure out what it means. It’s rather difficult to search using google, but after a while, I got a good explanation. It is basically similar to preprocessor in C/C++, you may read further from here:

Regarding Inverse Fermi Dirac Integral

Regarding my previous post on obtaining inverse fermi dirac integral. I found a paper by Antia which uses rational function to solve the inverse integral.

Antia, H. M. Rational Function Approximations for Fermi-Dirac Integrals. Astrophysical Journal Supplement v.84, p.101, (1993)

Do a Google Scholar Search to get the file.

The paper provide the Fortran codes.