modified on 22 October 2014 at 08:39 ••• 16,599 views


From Predictive Chemistry

Jump to: navigation, search

Libnwchem is a patch to the 6.5 Revision of NWChem that adds three useful features.

  1. push_inp_string and pop_inp_string (nw_inp_from_file.F) to fake string input as input from an nwchem parameter file
    • push_inp_cstring (push_inp_cstring.c) to provide a 0-terminated c-string interface to same
  2. an nwchem_init function (libnwchem.F) to perform just the start-up tasks of nwchem.F and return its runtime database handle
    • a libnwchem target to the GNUMakefile (sorry, no cygwin right now)
  3., and related python wrappers to call nwchem's top-level functions and handle io from the rtdb
    • various fixes to error handling in rtdb

Warning: I've observed some strangeness with the test for a "movecs" file during SCF. If the python script changes the geometry, but the movecs file from a previous SCF calc. is left unchanged, SCF may be skipped. This results in similar property output for (actually) different structures.



You can download the patch File:Libnwchem-6.5.patch.gz. Next, get the nwchem sources from the link at the top of the page. To unpack and patch them, use

tar xjf Nwchem-6.5.revision26243-src.2014-09-10.tar.bz2
mv Nwchem-6.5.revision26243-src nwchem-6.5
cd nwchem-6.5/src
for i in Util_md_sockets Hbar Tcenxtask Parallelmpi Makefile_gcc4x Bcast_ccsd Elpa_syncs; do
  zcat $i.patch.gz | patch -p0
  rm -f $i.patch.gz
i=Hnd_giaxyz_noinline # there's always that one.
zcat $i.patch.gz | (cd NWints/hondo && patch -p0)
rm -f $i.patch.gz
# Finally, apply the libnwchem patch.
zcat ../../libnwchem-6.5.patch.gz | patch -p1
cd ..
bash ../ # the script is at the bottom of this page.

The mv line is really required, since nwchem's build can fail if the path to the path to the source directory is too long!


For most Linux systems (but not BSD-based OSX), the -fPIC flag is required. This is such a PITA to add to nwchem's compilation that my solution is an ugly hack. Replace the real compiler with a shell script in your $HOME/bin directory, and make sure your path has $HOME/bin in front of all other directories.

(for icc)

# file $HOME/bin/icc
/opt/intel/bin/icc -fPIC $@

(or gcc)

# file $HOME/bin/gcc
/usr/bin/gcc -fPIC $@

I use the following script for compiling NWChem:

(on 64-bit Linux with icc)

# file
export NWCHEM_TOP=$HOME/build/nwchem-6.5
export USE_MPI=y
export USE_MPIF=y
export USE_MPIF4=y
export MPI_LIB=/apps/openmpi/1.4.5/lib
export MPI_INCLUDE=/apps/openmpi/1.4.5/include
export LIBMPI="-lpthread -lmpi_f90 -lmpi_f77 -lmpi -lopen-rte -lopen-pal -lrdmacm -libverbs
-lpsm_infinipath -lnuma -ldl -Wl,--export-dynamic -lnsl -lutil -lm -ldl"
# export IB_INCLUDE <Location of Infiniband libraries>/include
export CC=icc
export FCC=ifort
export FOPTIMIZE="-O2 -fPIC"
export FFLAGS="-O2 -fPIC"
export CFLAGS="-O2 -fPIC"
export MKLROOT=/apps/intel/mkl
# see intel's link-line advisor for generating the next line.  Use the options for single dynamic library.
# (the following is for MKL 10.3.6 with icc 11.1)
export BLASOPT="-i8 -I$MKLROOT/include/intel64/ilp64 -I$MKLROOT/include $MKLROOT/lib/intel64/libmkl_blas95_ilp64.a
$MKLROOT/lib/intel64/libmkl_lapack95_ilp64.a -Wl,--start-group $MKLROOT/lib/intel64/libmkl_intel_ilp64.a
$MKLROOT/lib/intel64/libmkl_core.a $MKLROOT/lib/intel64/libmkl_sequential.a -Wl,--end-group -lpthread -lm"
# These may be helpful for compiling with CUDA, but beware - the resulting program no longer loads without cuda libraries!
#export TCE_CUDA=n
#export CUDA=$CUDA_HOME/bin/nvcc
#export CUDA_FLAGS="-arch=sm_35"
#export CUDA_INCLUDE="-I$CUDA_HOME/include"
#export CUDA_LIBS="-L$CUDA_HOME/lib64 -lcudart -lcufft"
# -L/usr/X11R6/lib -lGL -lGLU -lX11 -lXi -lXmu -lglut -lGLEW
cd $NWCHEM_TOP/src
#make realclean # These two lines let you start again (theoretically).
#find . -name dependencies -exec rm {} \; -print
make nwchem_config
make nwchem
#make -B -C ddscf # in case you just changed something in the ddscf directory
cp util/cfileprefix.fh include/
make libnwchem

The line: cp util/cfileprefix.fh include/ is required since nwchem has a really strange way of finding dependencies that works when compiling $(MAKE) stubs.o inside nwchem's target, but doesn't work when compiling $(MAKE) stubs.o inside libnwchem's target in the same makefile!

(on 64/32 OSX with gcc)

to appear!


Inspecting the patch file, you will notice that the python wrapper was added into the src/python directory. It also includes a simple test (, which opens from its compiled location and does geometry optimization on a water molecule. To use it yourself,

  1. cd to nwchem-6.5/src/python
  3. edit to make sure the CDLL() command has the right path to (in a pinch, just move to the current dir, and change the CDLL line to CDLL(""))
  4. run: mkdir scratch; mkdir perm
    • these two dirs are the default scratch and perm directories
  5. run: python
  6. if all goes well, you should see normal nwchem output (listing basis functions, showing geometry convergence, and ending with a citation note)


The patch is released without any warranty or indeed any guarantee of working at all. If you have lots of ($$$) around, shoot me an email.