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)
tstart=count_0/count_rate
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

ESw.dEe

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”.

How to embed fonts when using pdflatex and gnuplot

IEEE require that PDF documents are created with embedded fonts. To do this, run

$ updmap --setoption pdftexDownloadBase14 true

And then run pdflatex again. To check whether it is embedded, run pdffonts on to the newly created pdf. You will see something like this.

$ pdffonts draft_isic2009_1.pdf
name                                 type              emb sub uni object ID
------------------------------------ ----------------- --- --- --- ---------
OONIXM+NimbusRomNo9L-Regu            Type 1            yes yes no       6  0
LREKMX+NimbusRomNo9L-MediItal        Type 1            yes yes no       7  0
GPJANF+NimbusRomNo9L-Medi            Type 1            yes yes no       8  0
SOCFYY+CMMI10                        Type 1            yes yes no       9  0
XUABDJ+CMR10                         Type 1            yes yes no      18  0
[none]                               Type 3            yes no  no      19  0
[none]                               Type 3            yes no  no      20  0
NYFSWW+CMR7                          Type 1            yes yes no      21  0
[none]                               Type 3            yes no  no      22  0
Symbol                               Type 1            no  no  no      25  0
MJDZZV+Arial                         TrueType          yes yes no      26  0
NEOHJY+Arial                         TrueType          yes yes no      33  0
RGPVWY+CMMI7                         Type 1            yes yes no      42  0
QFGUOO+Arial                         TrueType          yes yes no      45  0
WHIAUZ+NimbusRomNo9L-ReguItal        Type 1            yes yes no      54  0
Symbol                               Type 1            no  no  no      57  0
KLCILH+Arial                         TrueType          yes yes no      58  0
SHWEVG+Arial                         TrueType          yes yes no      65  0
PWUUWC+Arial                         TrueType          yes yes no      71  0

Now you notice that Symbol font is not embedded. It turns out to be from the figures that I have. I created my figures using Gnuplot, and it gives the eps file. The eps file is then converted to pdf using epstopdf command. If you type pdffonts to this figures, you will notice that the symbol font is not embedded. To ensure that the symbol font is embedded, edit the following file:

/usr/share/ghostscript/8.61/lib/gs_pdfwr.ps

And comment the lines as shown below:

/.standardfonts [
% /Courier /Courier-Bold /Courier-Oblique /Courier-BoldOblique
% /Helvetica /Helvetica-Bold /Helvetica-Oblique /Helvetica-BoldOblique
% /Times-Roman /Times-Bold /Times-Italic /Times-BoldItalic
% /Symbol /ZapfDingbats
] readonly def

After this, run epstopdf again, and check using pdffonts. I ran my pdflatex to my tex file again, and check to find that all fonts have been embedded:

$ pdffonts draft_isic2009_1.pdf
name                                 type              emb sub uni object ID
------------------------------------ ----------------- --- --- --- ---------
OONIXM+NimbusRomNo9L-Regu            Type 1            yes yes no       6  0
LREKMX+NimbusRomNo9L-MediItal        Type 1            yes yes no       7  0
GPJANF+NimbusRomNo9L-Medi            Type 1            yes yes no       8  0
SOCFYY+CMMI10                        Type 1            yes yes no       9  0
XUABDJ+CMR10                         Type 1            yes yes no      18  0
[none]                               Type 3            yes no  no      19  0
[none]                               Type 3            yes no  no      20  0
NYFSWW+CMR7                          Type 1            yes yes no      21  0
[none]                               Type 3            yes no  no      22  0
CNBJKM+Symbol                        Type 1C           yes yes no      25  0
MJDZZV+Arial                         TrueType          yes yes no      26  0
NEOHJY+Arial                         TrueType          yes yes no      35  0
RGPVWY+CMMI7                         Type 1            yes yes no      44  0
QFGUOO+Arial                         TrueType          yes yes no      47  0
WHIAUZ+NimbusRomNo9L-ReguItal        Type 1            yes yes no      56  0
CNBJKM+Symbol                        Type 1C           yes yes no      59  0
KLCILH+Arial                         TrueType          yes yes no      60  0
SHWEVG+Arial                         TrueType          yes yes no      69  0
PWUUWC+Arial                         TrueType          yes yes no      75  0

Installing Octave 3.2.3 in Kubuntu Hardy

Octave has released a major version 3.2 and now the latest bug fix release is 3.2.3. I decided to install it. What I did was to download the tarball from www.octave.org and then unzip it. After that, I run the configure:

./configure --prefix=$HOME/Programs/Octave-3.2.3

where the prefix defines the location where to install the new version of octave. I got some warning on the graphics library since Octave 3.2 uses a different graphics library.

configure: WARNING: qrupdate not found. The QR & Cholesky updating functions will be slow.
configure: WARNING: GraphicsMagick++ config script not found. Assuming GraphicsMagic++ library and header files are missing, so imread will not be fully functional
configure: WARNING: "FTGL headers not found. Native renderer will not have on-screen text"
configure: WARNING: "FLTK config script not found. Native graphics will be disabled."
configure: WARNING:
configure: WARNING: I didn't find the necessary libraries to compile native
configure: WARNING: graphics. It isn't necessary to have native graphics
configure: WARNING: but you will have to use gnuplot or you won't be able
configure: WARNING: to use any of Octave's plotting commands
configure: WARNING:

To solve this problem, I run Adept package manager and install the following:

libgraphicsmagick++1-dev
ftgl-dev
libfltk1.1-dev

And I also download qrupdate library from this page. Hardy does not provide this package, but it can be found in Karmic. To install this, edit the Makeconf file according to your system. In my case I just edit the PREFIX part to where I want to install it. After that type "make lib" for creating static library, and "make solib" for dynamic library. To install, type "make install". For more info, you can type "make help"

To make sure Octave can find the qrupdate library, you might have to add LDFLAGS into the ./configure command:

./configure --prefix=$HOME/Programs/Octave-3.2.3 LDFLAGS='-L/home/kurniawano/local/lib'

After everything seems ok, type "make" and "make install"

Debugger for Fortran in Linux

I have been trying to search for a good debugger for Fortran code in Linux. But it is very hard to find. I know that GDB is supposed to be able to do it. But when I debug Fortran 95 code compiled using GFortran, I simply don’t know how to display the dynamic size array. I tried to google, and found that other people also find the same issue with GDB and fortran:
comp.lang.fortran

this is what I get when I tried to print an array which was allocatable, and allocated afterward:

(gdb) p myarray
{ 0x80614c8, -1, 537, ({ 1, 1, 6})}

and when I get the type:

(gdb) pt myarray
type = Type array_descriptor1
PTR TO -> ( void ) :: data
int4 :: offset
int4 :: dtype
Type descriptor_dimension
int4 :: stride
int4 :: lbound
int4 :: ubound
End Type descriptor_dimension (0:0) :: dim
End Type array_descriptor1

so for me, the best solution currently is to use Intel compiler and debugger in Linux. The debugger is called “idb”. You can download freely from intel website.

So what I did was that I recompiled my code using “ifort” which is the intel fortran compiler, and use the “idb” to debug. To run it type:

$ /opt/intel/Compiler/11.1/038/bin/ia32/idb

the location will depend on where you install the compiler and the debugger.

To start debug, click “File->Load executable”, and choose the executable which has been compiled using Ifort. To view the source as well, click “View->Source Files”.

You can set a break point, etc, in the code, and to view an array simply type:

(idb) p myarray

or you can also access the element

(idb) p myarray(3)

Below is the snapshot of the debugger. I used Kubuntu 8.04.

idb

2D plot of filled contour with Plplot libraries using plshades

In the previous post, we show how to plot 2D data with plcont. It plots the line contour. Now, how about if we want something more colorful? we can use a filled contour plot with the command plshade. The syntax is similar. But first, let’s look at the complete program.

program simple
use plplot
implicit none
real(plflt),dimension(6) :: x,y
real(plflt),dimension(6,6)::z,x2d,y2d,z2
real(plflt)::xmin,xmax,ymin,ymax,zmin,zmax
real(plflt),dimension(10)::clevel
real::step
integer::nlevel
integer::i,j
character*1 defined
real(plflt),dimension(11)::shedge,shedge2
integer::fill_width
integer::cont_color
integer::cont_width
x=(/1,2,3,4,5,6/)
y=x
do i=1,6
  do j=1,6
    z(i,j)=x(i)*2*y(j)
    z2(i,j)=x(i)**2*y(j)
    x2d(i,j)=x(i)
    y2d(i,j)=y(j)
  enddo
enddo
call plinit()
xmin=1.0
xmax=6.0
ymin=1.0
ymax=6.0
nlevel=10
fill_width=2
cont_color=0
cont_width=0
zmin=minval(z)
zmax=maxval(z)

do i=1,nlevel+1
   shedge(i)=zmin+(zmax-zmin)*dble(i-1)/dble(nlevel)
   shedge2(i)=minval(z2)+(maxval(z2)-minval(z2))*dble(i-1)/dble(nlevel)
enddo

call plenv(xmin,xmax,ymin,ymax,0,0)
call pllab('X','Y','Test 2D plot')
call plshades(z,defined,1._plflt,6._plflt,1._plflt,6._plflt,&
     shedge,fill_width,cont_color,cont_width,x2d,y2d)
call sleep(2)
call plshades(z2,defined,1._plflt,6._plflt,1._plflt,6._plflt,&
     shedge2,fill_width,cont_color,cont_width,x2d,y2d)
call plend()

end program simple

In this program we plot 2 functions “z” and “z2″. the main command is:

call plshades(z,defined,1._plflt,6._plflt,1._plflt,6._plflt,&
shedge,fill_width,cont_color,cont_width,x2d,y2d)

The examples describes the parameter this way (note that it is different with the manual, sigh):

plshades(a, defined, xmin, xmax, ymin, ymax, clevel, fill_width, cont_color, cont_width,x2d,y2d)

where “a” is the 2D array of the data. The “defined” accepts x and y coordinate where we want to exclude the data, it must gives 0 for excluded region and 1 otherwise. The xmin, xmax, ymin, and ymax has the usual meaning which gives the range of the data to be plotted. The “clevel” is the a kind of contour level in plcont, but in our case it is the boundary for the filled contour regions. So for 10 level, we have to define 11 “clevel”. The values for this “color” level is computed from:

shedge(i)=zmin+(zmax-zmin)*dble(i-1)/dble(nlevel)

when i=1, shedge(1)=zmin. then for i=2, shedge(2)=zmin+width, where width=(zmax-zmin)/nlevel is the width between one contour level to another contour level.

Now, to be honest, I am not sure about what “fill_width” , “cont_color”, and “cont_width” does. I tried to change the values but can’t see any changes. All I know is that they take integer value as its input. But let me show you what the manual says, maybe you can understand it.

fill_width (PLINT, input): Defines width used by the fill pattern.
cont_color (PLINT, input): Defines pen color used for contours defining edges of shaded regions. The pen color is only temporary set for the contour drawing. Set this value to zero or less if no shade edge contours are wanted.
cont_width (PLINT, input): Defines pen width used for contours defining edges of shaded regions. This value may not be honored by all drivers. The pen width is only temporary set for the contour drawing. Set this value to zero or less if no shade edge contours are wanted.

The last two parameter is obvious, they are the 2D array for the x and y coordinates.

The plot can be seen in the image below:
shade2d

2D Contour plot using plplot function “plcont”

In the previous post, we describe how to do a simple line plot. In this post, I will describe how to plot contour using Plplot libraries, particularly using the “plcont” function. Let’s look at the code first.

program simple
use plplot
implicit none
real(plflt),dimension(6) :: x,y
real(plflt),dimension(6,6)::z,x2d,y2d
real(plflt)::xmin,xmax,ymin,ymax,zmin,zmax
real(plflt),dimension(10)::clevel
real::step
integer::nlevel
integer::i,j
x=(/1,2,3,4,5,6/)
y=x
do i=1,6
  do j=1,6
    z(i,j)=x(i)*2*y(j)
    x2d(i,j)=x(i)
    y2d(i,j)=y(j)
  enddo
enddo
call plinit()
xmin=1.0
xmax=6.0
ymin=1.0
ymax=6.0
nlevel=10
zmin=minval(z)
zmax=maxval(z)
step=(zmax-zmin+1)/nlevel
do i=1,nlevel
  clevel(i)=zmin+i*step
enddo
call plenv(xmin,xmax,ymin,ymax,0,0)
call pllab('X','Y','Test 2D plot')
call plcont(z,1,6,1,6,clevel,x2d,y2d)
call plend()

end program simple

As usual, we need to declare the variables needed in the beginning part of the code. The important part starts with

call plinit()

which is used to initialize the Plplot library.


do i=1,nlevel
clevel(i)=zmin+i*step
enddo

This code is used to compute the contour level. We first specified how many level we want in “nlevel”, and then the contour level is cimputed using the function shown above. The distance between the contour level is uniform, which is given in “step”.


call plenv(xmin,xmax,ymin,ymax,0,0)
call pllab('X','Y','Test 2D plot')

The first statement is to set up a standard window and draw box with min and max of the data values. The last two parameter are the “just” and “axis”. Just parameter determine how the axis will be scaled. The value 0 for “just” means that the x and y axes are scaled independently to use as much of the screen as possible. The “axis” parameter controls the drawing of the box around the plot. The value 0 means that it will draw box, ticks, and numeric tick labels.


call plcont(z,1,6,1,6,clevel,x2d,y2d)
call plend()

The first statement plot the contour. The fortran 95 function gives this options to call plcont:

plcont(z, kx, lx, ky, ly, clevel, tr?) or plcont(z, kx, lx, ky, ly, clevel, xgrid, ygrid)

We use the second option, where kx, lx are the range of x indices, and ky,ly are the range of y indixes. Since we are plotting all data, it starts with 1 and the last data is the number of grid point in x and y, which is 6.
clevel is the contour level which we have computed previously, while xgrid and ygrid are the 2D axis data. The latter means that xgrid and ygrid is a 2D array that gives the x and y position for the 2D array data.

The plot is shown in this image:
simple2d

Wiki for Corporate

my presentation during presentation skills workshop with the topic: Wiki for Corporate

Using Plplot from fortran in Kubuntu 8.04

I installed libplplot from Kubuntu Adept package manager, and chose “libplplot-fortran9″ package.

To test the code, I created the following fortran program:

program testplplot2d
use plplot
implicit none
real(plflt),dimension(6) :: x,y
real(plflt)::xmin,xmax,ymin,ymax
x=(/1,2,3,4,5,6/)
y=x**2
write(*,*) y
call plinit()
xmin=1.0
xmax=6.0
ymin=1.0
ymax=40.0
call plcol0(1)
call plenv(xmin,xmax,ymin,ymax,0,0)
call pllab('X','Y','Test 1D plot')
call plpoin(x,y,9)
call plline(x,y)
y=x**3
call plpoin(x,y,9)
call plline(x,y)
call plend()

end program testplplot2d

and then compiled it using this command:

gfortran -o simple simple.f90 `PKG_CONFIG_PATH=/usr/lib/pkgconfig pkg-config --cflags --libs plplotd-f95`

When I run ./simple, it asks me to choose the output device, and I chose “1″ which is the XWin display, and it plots as shown in the image below:
simple