19 July 2012

210. Compiling CPMD on Debian Wheezy

[To those coming here from cpmd.org -- I wasn't meant to post in the bug tracking section, but unfortunately I also can't delete posts]

Here's how to compile cpmd on Wheezy. CPMD is the standard implementation of ab initio Carr-Parrinello method for molecular dynamic and would thus be useful to have at hand.

It took a fair while to hammer out the method, so this post might not read very well. Right now I'm having crashes after the binary has been running for a while -- this point towards system resources more than anything else.


WARNING:
 This involves patching a file -- you never know IF that causes problems with the output. To the best of my knowledge, it should be safe.

There's no reason to think that Ubuntu and Linux mint won't work just as well (i.e. you may want to look here if you're trying to compile for Ubuntu or Mint)

Errors I encountered on the road are found at the end of the post together with solutions.




COMPILING THE BINARY

First you need to request a username and password from http://cpmd.org/. That'll take a few days.

You may have to install libopenmpi-dev libfftw3-dev libblas-dev first

Once your application is approved and you've downloaded your file (in my case cpmd-v3_15_3.tar.gz) copy it to ~/tmp, then

tar -xvf cpmd-v3_15_3.tar.gz
cd CPMD/CONFIGURE

create CONFIGURE/LINUX-x86_64-DEBIAN
     IRAT=2
     CFLAGS='-c -O2 -Wall'
     CPP='/lib/cpp -P -C -traditional'
     CPPFLAGS='-D__Linux -D__PGI -D__GNU -DFFT_FFTW3 -DPARALLEL -DPOINTER8'
     FFLAGS='-c -O2 -fcray-pointer -fno-whole-file -fsecond-underscore'
     LFLAGS='-lfftw3 -lfftw3f -I/usr/include -lblas -llapack -lpthread -lmpi'
     FFLAGS_GROMOS='  $(FFLAGS)' 
      FC='mpif77 -fbounds-check'
      CC='mpicc'
      LD='mpif77 -fbounds-check'


cd ../

Edit the following lines in wfnio.F and change them to:
 15       CHARACTER(len=*) TAG
 63         IF(TAG(1:2).EQ.'NI') THEN
201       IF(TAG(1:2).NE.'NI') THEN
271         IF(TAG(1:2).EQ.'NI') THEN
(See below for the reasons why)

./mkconfig.sh LINUX-x86_64-DEBIAN > Makefile
make -j5

You should now have a working binary. This was a real PITA to sort out.

sudo mkdir /opt/cpmd
sudo chown $USER /opt/cpmd
cp cpmd.x /opt/cpmd


Done! Almost...



Time to get some pseudopotential files from http://cpmd.org/download

Download the following files:
http://cpmd.org/downloadable-files/authentication/contributions/pseudo_std.tar.gz
http://cpmd.org/downloadable-files/authentication/contributions/pseudo_ext.tar.gz
http://cpmd.org/downloadable-files/authentication/contributions/pseudo_vdb.tar.gz
And some test files:
http://cpmd.org/downloadable-files/authentication/contributions/cpmd-test.tar.gz
Put them in /opt/cpmd
cd /opt/cpmd
ls *.gz|xargs -I {} tar xvf {}
echo "export PP_LIBRARY_PATH=/opt/cpmd/PP_LIBRARY" >>~/.bashrc
echo "export PATH=$PATH:/opt/cpmd" >>~/.bashrc.
source ~/.bashrc

Note:
For some reason cpmd refuses to read my pseudopotential files unless I remove the .psp ending. Doesn't matter how eagerly I'm trying to point cpmd in the right direction. You can quickly mv all the psp files like this:
cp /opt/cpmd/pseudo_extlib/*.psp /opt/cpmd/PP_LIBRARY/
cd  /opt/cpmd/PP_LIBRARY/
ls *.psp|sed 's/\./\t/g'|gawk '{print $1}'|xargs -I {} mv {}.psp {}
mv Cu_MT_PBE_SEMI_2.08.psp Cu_MT_PBE_SEMI_2.08
mv Ga.1_GO_LDA.psp Ga.1_GO_LDA
Not that this will guarantee that it'll work. I often find myself putting the full relative path in the .in file:

&ATOMS
*../../PP_LIBRARY/O_MT_PBE KLEINMAN-BYLANDER
LMAX=P
Anyway

Test your binary
cd /opt/cpmd/CPMD-test/vdw
mpirun -n 4 cpmd.x ch4-ch4.inp


 ****************************************************************
 *                                                              *
 *                            TIMING                            *
 *                                                              *
 ****************************************************************
 SUBROUTINE            CALLS         CPU TIME        ELAPSED TIME
        HIP               12            19.64               21.55
    FFT-G/S              734            11.94               12.02
     GCENER               12             4.82                4.86
      FWFFT               72             4.80                4.85
    INVFFTN              100             3.93                3.93
     INVFFT               61             3.82                3.86
    HOCKNEY                1             3.20                3.31
     FFTCOM              133             2.34                2.33
       VPSI               21             2.22                2.20
     FWFFTN               56             2.14                2.15
     XCENER               12             1.70                1.71
     RHOOFR               11             1.59                1.61
     GRADEN               12             1.58                1.58
      PHASE              133             1.46                1.43
     LOADPA                1             1.30                1.31
      ODIIS               11             0.96                0.99
   N-FFTCOM              156             0.80                0.82
    VOFRHOH               12             0.61                0.62
    VOFRHOB               12             0.57                0.57
     EICALC               12             0.43                0.43
      NUMPW                1             0.31                0.31
      ATRHO                1             0.26                0.29
 ----------------------------------------------------------------
 TOTAL TIME                             70.43               72.74
 ****************************************************************

       CPU TIME :    0 HOURS  1 MINUTES 11.75 SECONDS     
   ELAPSED TIME :    0 HOURS  1 MINUTES 14.21 SECONDS     
 ***      CPMD| SIZE OF THE PROGRAM IS  141512/ 484936 kBYTES ***

 PROGRAM CPMD ENDED AT:   Thu Jul 19 12:50:17 2012  

 ================================================================
 = COMMUNICATION TASK  AVERAGE MESSAGE LENGTH  NUMBER OF CALLS  =
 = SEND/RECEIVE              370967. BYTES                 51.  =
 = BROADCAST                  10413. BYTES                297.  =
 = GLOBAL SUMMATION            1153. BYTES                190.  =
 = GLOBAL MULTIPLICATION          0. BYTES                  1.  =
 = ALL TO ALL COMM          8815282. BYTES                313.  =
 =                             PERFORMANCE          TOTAL TIME  =
 = SEND/RECEIVE             3014.256  MB/S           0.006 SEC  =
 = BROADCAST                 271.033  MB/S           0.011 SEC  =
 = GLOBAL SUMMATION            1.996  MB/S           0.220 SEC  =
 = GLOBAL MULTIPLICATION       0.000  MB/S           0.001 SEC  =
 = ALL TO ALL COMM           643.149  MB/S           4.290 SEC  =
 = SYNCHRONISATION                                   0.067 SEC  =
 ================================================================


Almost completely and utterly done.

There are a few more things to do for a full install:
 1. cpmd2cube
Download cpmd2xyz-scripts.xyz and cpmd2cube.tar.gz and put them in ~/tmp

cd ~/tmp
tar xvf cpmd2cube.tar.gz
cd cpmd2cube/
./Configure  Linux-PC-GFORTRAN-FFTW >Makefile


Edit the Makefile
 11 #--------------- Configuration

 12 FC  = mpif77
 13 FFLAGS  =  -ffree-form -D__GFORTRAN -DFFT_FFTW3 -std=gnu -O2  -fno-whole-file
 14 LFLAGS  =
 15 LIBS    =  -lfftw3

Then compile:
make

cp *.x /opt/cpmd/

2. cpdm2xyz-scripts
tar xvf cpmd2xyz-scripts.tar.gz
mv *xyz.pl /opt/cpmd/



ERRORS I encountered on the way to success:


Why I patched wfnio.F



 (K+E1+L+N+X)           TOTAL ENERGY =          -15.69316800 A.U.
 (K)                  KINETIC ENERGY =           11.26654720 A.U.
 (E1=A-S+R)     ELECTROSTATIC ENERGY =          -14.51438081 A.U.
 (S)                           ESELF =           15.95769122 A.U.
 (R)                             ESR =            0.61466148 A.U.
 (L)    LOCAL PSEUDOPOTENTIAL ENERGY =           -8.04105016 A.U.
 (N)      N-L PSEUDOPOTENTIAL ENERGY =            1.58864226 A.U.
 (X)     EXCHANGE-CORRELATION ENERGY =           -5.99292649 A.U.
          GRADIENT CORRECTION ENERGY =           -0.34038005 A.U.

 NFI      GEMAX       CNORM           ETOT        DETOT      TCPU
   1  2.271E-02   2.070E-03     -15.693168    0.000E+00     12.31
   2  2.968E-03   6.595E-04     -16.042408   -3.492E-01     12.23
   3  2.302E-03   2.795E-04     -16.073138   -3.073E-02     12.41
   4  1.202E-03   1.028E-04     -16.079251   -6.113E-03     13.11
   5  5.911E-04   3.645E-05     -16.080017   -7.655E-04     12.86
   6  2.152E-04   1.926E-05     -16.080119   -1.020E-04     12.27
   7  7.666E-05   8.749E-06     -16.080151   -3.277E-05     12.24
   8  4.416E-05   4.054E-06     -16.080159   -8.141E-06     12.54
   9  2.411E-05   1.531E-06     -16.080162   -2.318E-06     12.14
  10  1.203E-05   5.610E-07     -16.080162   -3.935E-07     12.22
  11  6.744E-06   2.833E-07     -16.080162   -3.662E-08     12.31
At line 2 of file ./wfnio.f
Fortran runtime error: Actual string length is shorter than the declared one for dummy argument 'tag' (2/10)

Line 2 in wfnio.f is 

      SUBROUTINE W_WFNIO(NW,IERROR,NSTATE,C,TAG)


I tried compiling with my own openblas libraries:

Used the following config file base:
     IRAT=2
     CFLAGS='-c -O1 -Wall'
     CPP='/lib/cpp -P -C -traditional'
     CPPFLAGS='-D__Linux -D__PGI -D__GNU -DFFT_FFTW -DPOINTER8  -DPARALLEL'
      FFLAGS='-c -O2 -fcray-pointer -fno-whole-file -fsecond-underscore'
     LFLAGS='-L/opt/fftw/fftw-2.1.5/double/lib -l:libfftw.a -I/opt/fftw/fftw-2.1.5/double/include -I/usr/include -L/opt/openblas/lib -lopenblas'
     FFLAGS_GROMOS='  $(FFLAGS)'
      FC='mpif90'
      CC='mpicc'
      LD='mpif90'


Tested with:
!  Wavefunction optimization using default setting
!
 &CPMD
    OPTIMIZE WAVEFUNCTION
 &END
 &SYSTEM
   SYMMETRY
    1
   CELL
    10.2612  1.0    1.0   0.0 0.0 0.0
   CUTOFF
   13.
 &END
 &ATOMS
*SI_SGS  KLEINMAN-BYLANDER
  LMAX=P
   8
           .00000      .00000      .00000    1
           .00000     5.13000     5.13000    1
          5.13000      .00000     5.13000    1
          5.13000     5.13000      .00000    1
          2.56500     2.56500     2.56500    1
          2.56500     7.69500     7.69500    1
          7.69500     2.56500     7.69500    1
          7.69500     7.69500     2.56500    1
 &END

And here's the output (NOTE: it wouldn't have worked anyway due to missing pseudopot files. But that's unrelated):
 PROGRAM CPMD STARTED AT: Wed Jul 18 20:41:51 2012
 SETCNST| USING: CODATA 2006 UNITS

[LOTS OF STUFF CUT OUT]
 EXCHANGE CORRELATION FUNCTIONALS
    LDA EXCHANGE:                                            NONE
    LDA XC THROUGH PADE APPROXIMATION
    S.GOEDECKER, J.HUTTER, M.TETER PRB 54 1703 (1996)

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:#0  0x2B3D01CFA667#1  0x2B3D01CFAC34#2  0x2B3D026F64EF#3  0x2B3CFFD8CBF7Segmentation fault


Valgrind told me:

 EXCHANGE CORRELATION FUNCTIONALS 
    LDA EXCHANGE:                                            NONE
    LDA XC THROUGH PADE APPROXIMATION
    S.GOEDECKER, J.HUTTER, M.TETER PRB 54 1703 (1996)

==1406== Invalid read of size 8
==1406==    at 0x59D0BF7: dcopy_k (in /opt/openblas/lib/libopenblas_barcelona-r0.1.1.so)
==1406==    by 0x485152: setsc_ (in /home/me/tmp/CPMD/cpmd.x)
==1406==    by 0x4264EE: cpmd_ (in /home/me/tmp/CPMD/cpmd.x)
==1406==    by 0x4262EE: main (in /home/me/tmp/CPMD/cpmd.x)
==1406==  Address 0x30016a9908 is not stack'd, malloc'd or (recently) free'd
==1406== 

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7737667
#1  0x7737C34
#2  0x81314EF
#3  0x59D0BF7
==1406== 
==1406== HEAP SUMMARY:
==1406==     in use at exit: 2,152,258 bytes in 2,802 blocks
==1406==   total heap usage: 10,893 allocs, 8,091 frees, 16,699,612 bytes allocated
==1406== 
==1406== LEAK SUMMARY:
==1406==    definitely lost: 567 bytes in 20 blocks
==1406==    indirectly lost: 2,973 bytes in 9 blocks
==1406==      possibly lost: 0 bytes in 0 blocks
==1406==    still reachable: 2,148,718 bytes in 2,773 blocks
==1406==         suppressed: 0 bytes in 0 blocks
==1406== Rerun with --leak-check=full to see details of leaked memory
==1406== 
==1406== For counts of detected and suppressed errors, rerun with: -v
==1406== Use --track-origins=yes to see where uninitialised values come from
==1406== ERROR SUMMARY: 591 errors from 4 contexts (suppressed: 4 from 4)
Segmentation fault

Ergo, it seems to be a problem with the openblas libs -- but they've work just fine with nwchem and gromacs...who knows? It might be due to mixing debian fftw3 and my own openblas.

I compiled openblas according to http://verahill.blogspot.com.au/2012/05/nwchem-with-openblas.html



If you're having problems with the output below it is because fftw 2 and fftw 3 are not compatible and CPDM uses the fftw 2 iface by default.


mltfft.o: In function `mltfft_fftw_': mltfft.f:(.text+0x958): undefined reference to `fftw_f77_create_plan_' mltfft.f:(.text+0x9af): undefined reference to `fftw_f77_' mltfft.f:(.text+0x9c2): undefined reference to `fftw_f77_destroy_plan_' mltfft.f:(.text+0xaee): undefined reference to `fftw_f77_create_plan_' mltfft.f:(.text+0xb4d): undefined reference to `fftw_f77_' mltfft.f:(.text+0xd03): undefined reference to `fftw_f77_' mltfft.f:(.text+0xdc4): undefined reference to `fftw_f77_' mltfft.f:(.text+0xe6e): undefined reference to `fftw_f77_create_plan_' mltfft.f:(.text+0xe8f): undefined reference to `fftw_f77_create_plan_' collect2: ld returned 1 exit status make: *** [cpmd.x] Error 1

You can either
1. Install fftw2 (sudo apt-get install fftw-dev, fftw2), and put -l:libfftw.so.2 -l:librfftw.so.2 in your LFLAGS.
or
2. Set -DFFT_FFTW3 instead of -DFFT_FFTW.

209. Quantum Espresso on Debian

Quantum Espresso seems to be a fairly capable software package for ab initio QM and MD calculations. In their own words:
"Quantum ESPRESSO is an integrated suite of Open-Source computer codes for electronic-structure calculations and materials modeling at the nanoscale.It is based on density-functional theory, plane waves, and pseudopotentials."

Reading between the lines it seems to be particularly geared towards solid state simulations, but given that I haven't used it much (I'm just an interested observer), you may take that statement with a grain of salt.

Anyway. Here's how to get it up and running.
Don't ask me how to USE these pieces of software though. For that, read the documentation at the Quantum Espresso website or look in /opt/QE/Doc


The download page can be found here: http://qe-forge.org/frs/?group_id=10. You won't need all the packages, since the espresso-5.0.tar.gz bundles most of them.

1. Housekeeping:
sudo mkdir /opt/QE
sudo chown $USER /opt/QE
mkdir ~/tmp/QE -p
cd ~/tmp/QE

2. Downloading:
wget http://qe-forge.org/frs/download.php/211/espresso-5.0.tar.gz
wget http://qe-forge.org/frs/download.php/214/PWgui-5.0.tgz
wget http://qe-forge.org/frs/download.php/204/xspectra-5.0.tar.gz

3. Extraction:
tar xvf espresso-5.0.tar.gz
tar xvf PWgui-5.0.tgz
tar xvf xspectra-5.0.tar.gz

4. Compilation:
cd espresso-5.0/

Edit environment_variables and set them to e.g.

PREFIX=/opt/QE
TMP_DIR=/scratch
PARA_PREFIX=" mpirun -n 3"

Don't know if any of those params are ever read though.

The following parameters will depend on your system. I tried compiling with openblas without luck. who knows? It might be due to mixing debian fftw3 and my own openblas.

Instead, install libblas-dev, libfftw3-dev, libopenmpi-dev etc.

./configure --prefix=/opt/QE/bin --exec-prefix=/opt/QE/bin/ FC=mpif90 BLAS_LIBS=-lblas LIBS="-lmpi -lopen-rte -lopen-pal -ldl -lmpi_f77 -lpthread" CPPFLAGS="-I/usr/lib/openmpi/include"
cd PW/
make

cd ../
make all -j5 
echo 'export PATH=$PATH:/opt/QE/bin' >>~/.bashrc
echo 'export PSEUDO_DIR=/opt/QE/pseudo' >>~/.bashrc
(replace 5 with  the number of cores you compile with +1)

cp * -R /opt/QE/
source ~/.bashrc

For some reason I had to move everything by hand. Oh well.

[It should be enough to set the env var PSEUDO_DIR to point at /opt/QE/pseudo, but it didn't work for me. Instead I symmlinked the entire /opt/QE to ~/espresso. Desperate? Sure...
ln -s /opt/QE/ /home/me/espresso]


5. Testing PW
Some examples are found in /opt/QE/PW/examples
cd /opt/QE/PW/examples/example01
./run_examples

/opt/QE/PW/examples/example01 : starting
This example shows how to use pw.x to calculate the total energy and
the band structure of four simple systems: Si, Al, Cu, Ni.
  executables directory: /opt/QE/bin
  pseudo directory:      /opt/QE/pseudo
  temporary directory:   /scratch
  checking that needed directories and files exist... done
  running pw.x as: mpirun -n 3 /opt/QE/bin/pw.x
  cleaning /scratch... done
  running the scf calculation for Si... done
  running the band-structure calculation for Si... done
  cleaning /scratch... done
  ..
  cleaning /scratch... done
  running the scf calculation for Ni... done
  running the band-structure calculation for Ni... done
Or you can try

cd /opt/QE/PW/tests/
./check-pw.x.j
A number of tests will now be executed.  Most will pass.
IF YOU DON'T HAVE A vdw_kernel_table file one of the tests will fail.

6. Installing PWgui
Assuming you downloaded and extracted the PWgui-5.0.tgz file in step 2 above.
sudo apt-get install itk3 iwidgets4
cd ~/tmp/QE/
mv PWgui-5.0 /opt/QE/
cd /opt/QE/bin
ln -s /opt/QE/PWgui-5.0/pwgui /opt/QE/bin/pwgui

Start by typing
pwgui


settings
You can try e.g. opening /opt/QE/PW/examples/example01/results/al.scf.cg.in if you ran the test in step 5 above. Then run it.


7. Installing Xspectra
This assumes you downloaded and extracted as shown in steps 2-3 above.
cd ~/tmp/QE/
mv XSpectra/ espresso-5.0/
cd espresso-5.0/XSpectra/
make
cd ..
mv XSpectra/ /opt/QE/
cd /opt/QE/bin
ln -s /opt/QE/XSpectra/src/xspectra.x xspectra.x



Notes: in an ideal world the --prefix during configure should suffice in telling a program where to install. No luck for me here though. Also, I had to start the compile in the PW directory and only by using make without any switches, or there would be complaints about a missing libpw.a

13 July 2012

208. Weird things with desktops -- nvidia cockup?

Just in case someone else is having an equally entertaining Friday the 13th (of July 2012).

So, I had a little gnome 3 crash. You know the ones with the frowny screen and a message saying that "yeah, we probably could have let you save everything you're working one but screw you 'cos we're logging you out"? (hmm...don't remember seeing that with gnome 2/metacity -- have we in fact been turned into Metro/Win8 guinea pigs?!)

Well, anyway, I had one of those and rebooted.

I got to gdm3, logged in and...nothing. I mean, I saw the desktop background, but no menus, nothing. Nothing at all. Moving the mouse to the top left corner would give me the typical gnome-shell splash pattern and make the screen a bit darker. I didn't get anything else though (like a list over programs, icons or anything). Oh, and the conky stuff briefly flashed by each time (but in the top left area instead of flush to the right side of the screen)

Given that I played around with testing different desktops recently I figured I might have upset the update-alternatives balance, and played around with --config x-window-manager and --config x-session-manager. I mean, I was hoping that the lack of a menu of some sort was due to having the wrong window manager, in spite of all the signs pointing to me actually using gnome-shell.

That not working I spent another hour playing with installing and uninstalling nvidia. For some reason smxi pulled in the 173 driver, before replacing it with 302. Every time. Finally, I managed to get everything to the point where I could do startx with the nouveau drivers installed.

Not that things were perfect -- in fact my screen was scaled to 1024*768 (supposed to be 1920x1280), and the flicker whenever I moved my mouse was not funny, but at least it kind of worked.

So back to the terminal, sudo rmmod nouveau, sudo smxi, install the nvidia driver again -- but this time selecting nvidia current instead of debian-nvidia, and then startx

Huh. This time I kind of got the icon panel and the bottom panel, and the gnome-shell hot corner worked ok. It's almost like...no way...is it pretending I have two displays???

I then went to System settings/Displays and the bloody thing had set it up so there were two active display (I don't OWN two displays and certainly did not set this willingly).

Here it's been deactivated. But seriously, wtf???

So if you find yourself in a similar situation without panels and stuff: check your settings. Now how the hell did this come about? And who can I blame? Debian? Nvidia? Nvidia has been the cause of most of my more severe problems with debian...I just wish nouveau was a better alternative than it currently is for my setup.

PS I do realise that some of the language in the post above is more fitting for a valley girl, but screw this: I'm a busy person with a tight schedule (semester starts in two weeks and still hammering away on lectures, not to mention research) who lost three valuable Friday afternoon hours on a stupid thing. Not happy.