Most commented posts
- Start to Fortran — 1 comment
- Phonons: shake those atoms — 3 comments
Permanent link to this article: https://dannyvanpoucke.be/paper2015_xrd_crystendcomm-en/
Permanent link to this article: https://dannyvanpoucke.be/iap-meeting-poster-2015-en/
Aug 29 2015
In the previous fortran tutorials, we learned the initial aspects of object oriented programming (OOP) in fortran 2003. And even though our agent-based opinion-dynamics-code is rather simple, it can quickly take several minutes for a single run of the program to finish. Two tools which quickly become of interest for codes that need more than a few minutes to run are: (1) a progress bar, to track the advance of the “slow” part of the code and prevent you from killing the program 5 seconds before it is to finish, and (2) a timer, allowing you to calculate the time needed to complete certain sections of code, and possibly make predictions of the expected total time of execution.
In this tutorial, we will focus on the progress bar. Since our (hypothetical) code is intended to run on High-Performance Computing (HPC) systems and is written in the fortran language, there generally is no (or no easy) access to GUI’s. So we need our progress bar class to run in a command line user interface. Furthermore, because it is such a widely useful tool we want to build it into a (shared) library (or dll in windows).
What do we want out of our progress bar? It needs to be easy to use, flexible and smart enough to work nicely even for a lazy user. The output it should provide is formatted as follows: <string> <% progress> <text progress bar>, where the string is a custom character string provided by the user, while ‘%progress’ and ‘text progress bar’ both show the progress. The first shows the progress as an updating number (fine grained), while the second shows it visually as a growing bar (coarse grained).
[codesyntax lang=”fortran” lines=”normal” title=”TProgressBar Class” bookmarkname=”PBarClass” blockstate=”expanded” doclinks=”0″]
type, public :: TProgressBar
private
logical :: init
logical :: running
logical :: done
character(len=255) :: message
character(len=30) :: progressString
character(len=20) :: bar
real :: progress
contains
private
procedure,pass(this),public :: initialize
procedure,pass(this),public :: reset
procedure,pass(this),public :: run
procedure,pass(this),private:: printbar
procedure,pass(this),private:: updateBar
end type TProgressBar
[/codesyntax]
All properties of the class are private (data hiding), and only 3 procedures are available to the user: initialize, run and reset. The procedures, printbar and updatebar are private, because we intend the class to be smart enough to decide if a new print and/or update is required. The reset procedure is intended to reset all properties of the class. Although one might consider to make this procedure private as well, it may be useful to allow the user to reset a progress bar in mid progress.(The same goes for the initialize procedure.)
[codesyntax lang=”fortran” lines=”normal” title=”Run procedure of the TProgressBar class” blockstate=”expanded” bookmarkname=”RunProcedure” ]
subroutine run(this,pct,Ix,msg)
class(TProgressBar) :: this
real::pct
integer, intent(in), optional :: Ix
character(len=*),intent(in),optional :: msg
if (.not. this%init) call this%initialize(msg)
if (.not. this%done) then
this%running=.true.
this%progress=pct
call this%updateBar(Ix)
call this%printbar()
if (abs(pct-100.0)<1.0E-6) then
this%done=.true.
write(*,'(A6)') "] done"
end if
end if
end subroutine run
[/codesyntax]
In practice, the run procedure is the heart of the class, and the only procedure needed in most applications. It takes 3 parameters: The progress (pct), the number of digits to print of pct (Ix),and the <string> message (msg). The later two parameters are even optional, since msg may already have been provided if the initialize procedure was called by the user. If the class was not yet initialized it will be done at the start of the procedure. And while the progress bar has not yet reached 100% (within 1 millionth of a %) updates and prints of the bar are performed. Using a set of Boolean properties (init, running, done), the class keeps track of its status. The update and print procedures just do this: update the progress bar data and print the progress bar. To print the progress bar time and time again on the same line, we need to make use of the carriage return character (character 13 of the ASCII table):
write(*,trim(fm), advance='NO') achar(13), trim(this%message),trim(adjustl(this%progressString)),'%','[',trim(adjustl(this%bar))
The advance=’NO‘ option prevents the write statement to move to the next line. This can sometimes have the unwanted side-effect that the write statement above does not appear on the screen. To force this, we can use the fortran 2003 statement flush(OUTPUT_UNIT), where “OUTPUT_UNIT” is a constant defined in the intrinsic fortran 2003 module iso_fortran_env. For older versions of fortran, several compilers provided a (non standard) flush subroutine that could be called to perform the same action. As such, we now have our class ready to be used. The only thing left to do is to turn it into a dll or shared library.
There are two types of libraries: static and dynamic.
Static libraries are used to provide access to functions/subroutines at compile time to the library user. These functions/subroutines are then included in the executable that is being build. In linux environments these will have the extension “.a”, with the .a referring to archive. In a windows environment the extension is “.lib”, for library.
Dynamic libraries are used to provide access to functions/subroutines at run time. In contrast to static libraries, the functions are not included in the executable, making it smaller in size. In linux environments these will have the extension “.so”, with the .so referring to shared object. In a windows environment the extension is “.dll”, for dynamically linked library.
In contrast to C/C++, there is relatively little information to be found on the implementation and use of libraries in fortran. This may be the reason why many available fortran-“libraries” are not really libraries, in the sense meant here. Instead they are just one or more files of fortran code shared by their author(s), and there is nothing wrong with that. These files can then be compiled and used as any other module file.
So how do we create a library from our Progressbar class? Standard examples start from a set of procedures one wants to put in a library. These procedures are put into a .f or .f90 file. Although they are not put into a module (probably due to the idea of having compatibility with fortran 77) which is required for our class, this is not really an issue. The same goes for the .f03 or .f2003 extension for our file containing a fortran 2003 class. To have access to our class and its procedures in our test program, we just need to add the use progressbarsmodule clause. This is because our procedures and class are incorporated in a module (in contrast to the standard examples). Some of the examples I found online also include compiler dependent pragmas to export and import procedures from a dll. Since I am using gfortran+CB for development, and ifort for creating production code, I prefer to avoid such approaches since it hampers workflow and introduces another possible source of bugs.
The compiler setups I present below should not be considered perfect, exhaustive or fool-proof, they are just the ones that work fine for me. I am, however, always very interested in hearing other approaches and fixes in the comments.
The windows approach is very easy. We let Code::Blocks do all the hard work.
Creating the dll : Start a new project, and select the option “Fortran DLL“. Follow the instructions, which are similar to the setup of a standard fortran executable. Modify/replace/add the fortran source you wish to include into your library and build your code (you can not run it since it is a library).
Creating a user program : The program in which you will be using the dll is setup in the usual way. And to get the compilation running smoothly the following steps are required:
The entire setup is the same as for the shared library. This time, however, choose the “Fortran Library” option instead of Fortran dll. As the static library is included in the executable, there is no need to ship it with the executable, as is the case for the dll.
For the unix approach we will be working on the command line, using the intel compiler, since this compiler is often installed at HPC infrastructures.
After having created the appropriate fortran files you wish to include in your library (in our example this is always a single file: PBar.f03, but for multiple files you just need to replace PBar.f03 with the list of files of interest.)
ifort -fpic -c -free -Tf Pbar.f03
Where -fpic tells the compiler to generate position independent code, typical for use in a shared object/library, while -c tells the compiler to create an object file. The -free and -Tf compiler options are there to convince the compiler that the f03 file is actual fortran code to compile and that it is free format.
ar rc PBarlib.a PBar.oifort TestProgram.f90 PBarlib.a -o TestProgram.exe
Note that also here the .mod file of our Progressbarsmodule needs to be present for the compilation to be successful.
For the shared library the approach does not differ that much.
ifort -fpic -c -free -Tf Pbar.f03
In this case the fpic option is not optional in contrast to the static library above. The other options are the same as above.
ifort -shared PBar.o -o libPBar.so
The compiler option -shared creates a shared library, while the -o option allows us to set the name of the library.
ifort TestProgram.f90 libPBar.so -o TestProgram.exe
Note that also here the .mod file of our Progressbarsmodule needs to be present for the compilation to be successful. To run the program you also need to add the location of the library file libPBar.so to the environment variable LD_LIBRARY_PATH
HPC systems may perform extensive buffering of data before output, to increase the efficiency of the machine (disk-writes are the slowest memory access option)…and as a result this can sometimes overrule our flush command. The progressbar in turn will not show much progress until it is actually finished, at which point the entire bar will be shown at once. There are options to force the infrastructure not to use this buffering (and the system administrators in general will not appreciate this), for example by setting the compiler flag -assume nobuffered_stdout. So the best solution for HPC applications will be the construction of a slightly modified progress bar, where the carriage return is not used.

Special thanks also to the people of stack-exchange for clarifying some of the issues with the modules.
Source files for the class and test-program can be downloaded here.
Permanent link to this article: https://dannyvanpoucke.be/fortran-dlls-en/
Aug 10 2015
What happens if two physicists take a holiday? They end up stargazing and hunting for the International Space Station (ISS). During the last week and a half we tried to capture the ISS flybys on camera nearly every night (with varying success). Each night we learned new things:

(a) Flyby of the ISS on August 4, 22h38 with a 20 second exposure, and ISO speed of 160. (b) Midnight starscape on august 5th (awaiting the ISS flyby) with a 60 second exposure at ISO-160. (c) and (d) zoomed in sections of (b) showing the color of the stars and arcs due to the earth rotation. (c) shows the a section of sky quite close to the north(small arcs). The arcs shown in (d) are about 15 arc-minute in size. (pictures by Sylvia Wenmackers)
Permanent link to this article: https://dannyvanpoucke.be/iss-gazing-en/
Aug 02 2015
Most kids love dinosaurs, or at some point in life have been intrigued by the idea of the large monsters among them. My inner child still does, so twenty-two years after having seen the original Jurassic Park movie, I watched the first three movies again with my girlfriend, as preparation for its most recent incarnation:Jurassic World.(JP4)
The movie starts from the same premise as the original one (building a theme-park with dinosaurs), but unlike the original, this theme-park is already up and running, and welcoming over 20.000 visitors a day. However, as the mathematician Dr. Ian Malcolm noted in Lost World:
Oh, yeah. Oooh, ahhh, that’s how it always starts. Then later there’s running and screaming.
and so it is in Jurassic world. Everything is peachy, until the genetically engineered Indominus Rex (or the King that can not be mastered/tamed) escapes and runs rampage on Isla Nublar. The representation of a commercialized dinosaur theme-park is scarily realistic: With a type of kids-farm like section, where little children can hug small dinosaurs and ride baby Triceratops’s, shops selling merchandising, rides through the park and animal (feeding-)shows. Also the modern trend to use abstractions (as euphemisms) to describe negative experiences and events, turns out rather painfully realistic with the automated warning messages to inform the tourists of a “containment anomaly“, when in fact the aviary is breached and the escaped pterodactyls are about to start pecking the above tourists to death.
Permanent link to this article: https://dannyvanpoucke.be/jurassic-world-en/
Permanent link to this article: https://dannyvanpoucke.be/sylvia-twitter256-en/
Jul 06 2015

With the inclusion of the phonon-module and some minor fixes and extensions to existing code, the hive 3.x program now counts over 40.000 lines, spread over 60 files. 8% of all lines are blank lines, 71% of all lines contain code, and 21% contain comments, an indication of the extent of the documentation of the code.
The code now provides access to 33 command line options of varying complexity. The simplest options (extracting a geometry, or creating a cif-file) are nearly instantaneous, while more complex options (such as Hirshfeld-I calculations) can take up to several hours.
Also from this point onward, HIVE will require to be linked with a lapack-library during compilation, to allow for the efficient solution of eigenvalue-problems.
Time for a little celebration.
Permanent link to this article: https://dannyvanpoucke.be/hive-40k-en/
Jun 28 2015
June 28th or 6/28 the first three digits of 2 aka
. In response to the creation of
-day (March 14th), June 28th was suggested as
-day to celebrate the number representing the ratio between the circumference of a circle and its radius. (And as with most opinions these days there needs to be a lot of controversy and discussion 😎 ) Having no religious preferences for either, I suggest to celebrate both, one with a single pie, and the other with two.
Permanent link to this article: https://dannyvanpoucke.be/happy-tau-day-2015-en/
Jun 19 2015
In physics, a phonon is a collective excitation in a periodic, elastic arrangement of atoms or molecules in condensed matter, like solids and some liquids. Often designated a quasi-particle, it represents an excited state in the quantum mechanical quantization of the modes of vibrations of elastic structures of interacting particles.
— source: wikipedia
Or for simplicity: sound waves; the ordered shaking of atoms or molecules. When you hit a metal bell with a (small) hammer, or make a wineglass sing by rubbing the edges, you experience the vibrations of the object as sound. All objects in nature (going from atoms to stars) can be made to vibrate, and they do this at one or more specific frequencies : their eigenfrequencies or normal frequencies.
Also single molecules, if they are hit (for example by another molecule bumping into them) or receive extra energy in another way, start to vibrate. These vibrations can take many forms (elongating and shortening of bonds, rotating of parts of the molecule with respect to other parts, flip-flopping of loose ends, and so forth) and give a unique signature to the molecule since each of these vibrations (so-called eigen-modes) corresponds with a certain energy given to the molecule. As a result, if you know all the eigen-modes of a molecule, you also know which frequencies of infrared light they should absorb, which is very useful, since in experiment we do not “see” molecules (if we see them at all) as nice ball-and-stick objects.
From the computational point of view, this is not the only reason why in molecular modeling the vibrational frequencies of a system (i.e. the above eigen-modes) are calculated. In addition, they also tell if a system is in its ground state (which is what one is looking for most of the time) or not. Although this tool has wide-spread usage in molecular modeling, it is seldom used in ab initio solid state physics because of the associated computational cost. In addition, because of the finite size of the unit cell, the reciprocal space in which phonons live also has a finite size, in contrast to the single point for a molecule…making life complex. 😎
Permanent link to this article: https://dannyvanpoucke.be/phonons-shake-those-atoms-en/
May 22 2015
| additional resources | |
| agent paper: | Sobkowicz |
| source-code: | AgentTutorials |
| Arxiv: | Full Tutorial |
In the previous tutorial, we saw how to tackle an opinion dynamics problem using agents as a class in an Object Oriented Programming (OOP) approach. In many topics of interest in (socio-)physics and chemistry, we deal with a large number of particles, be it electrons, atoms, agents, stars, … These are contained in a superstructure (electrons⇒atom, atoms⇒molecule/solid, agents⇒population, stars⇒galaxy,…) which is generally represented in the code as an array. As we noted in the previous tutorial, there were several variables which were global to the agents, but we implemented them as properties of the agents anyhow. As a result, a significant amount of additional memory needed to be allocated for storing in essence the same data. This was done to prevent the need of having to provide this information at every function call.
Returning to our problem of interest, we now consider two object classes: The TAgent-class and the TPopulation-class. This leads to several possible ways this problem can be implemented.
Although the true OOP-programmer may only consider the second option the way to go, we will consider the third option in this tutorial.
Permanent link to this article: https://dannyvanpoucke.be/oop-fortran-tut3-en/