INSTALL.rst
a526e8f5
 =====================
 Installation of Kwant
 =====================
76459aa4
 
a526e8f5
 Ready-to-use Kwant packages are available for many platforms (like GNU/Linux,
 Mac OS X, Microsoft Windows).  See the `installation page of the Kwant website
164f611b
 <https://kwant-project.org/install>`_ for instructions on how to install Kwant
a526e8f5
 on your platform.  This is the recommended way for new users.
76459aa4
 
a526e8f5
 The remainder of this section documents how to build Kwant from source.  This
 information is mostly of interest to contributors and packagers.
76459aa4
 
 
a526e8f5
 ********************
 Generic instructions
 ********************
2a1bfc72
 
eb1430bd
 Obtaining the source code
 =========================
 
 Source distributions of Kwant (and Tinyarray) are available at the `downloads
 section of the Kwant website <https://downloads.kwant-project.org/kwant/>`_ as well
 as `PyPI <https://pypi.python.org/pypi/kwant>`_.  The sources may be also
 cloned directly from the `official Kwant git repository
 <https://gitlab.kwant-project.org/kwant/kwant>`_.
 
 
2a1bfc72
 Prerequisites
 =============
 
fa012f86
 Building Kwant requires
39b65acd
  * `Python <https://www.python.org/>`_ 3.6 or above (Kwant 1.1 is the last
164f611b
    version to support Python 2),
39b65acd
  * `NumPy <http://numpy.org/>`_ 1.13.3 or newer,
  * `SciPy <https://www.scipy.org/>`_ 0.19.1 or newer,
c451f672
  * `LAPACK <http://netlib.org/lapack/>`_ and `BLAS <http://netlib.org/blas/>`_,
    (For best performance we recommend the free `OpenBLAS
164f611b
    <http://www.openblas.net/>`_ or the nonfree `MKL
    <https://software.intel.com/en-us/intel-mkl>`_.)
6eb2c201
  * `Tinyarray <https://gitlab.kwant-project.org/kwant/tinyarray>`_ 1.2 or newer,
 a NumPy-like Python package optimized for very small arrays,
c451f672
  * An environment which allows to compile Python extensions written in C and
2a1bfc72
    C++.
 
 The following software is highly recommended though not strictly required:
39b65acd
  * `matplotlib <http://matplotlib.org/>`_ 2.1.1 or newer, for the module `kwant.plotter` and the tutorial,
  * `SymPy <http://sympy.org/>`_ 1.1.1 or newer, for the subpackage `kwant.continuum`.
8c0f0a9b
  * `Qsymm <https://pypi.org/project/qsymm/>`_ 1.2.6 or newer, for the subpackage `kwant.qsymm`.
c451f672
  * `MUMPS <http://graal.ens-lyon.fr/MUMPS/>`_, a sparse linear algebra library
fa012f86
    that will in many cases speed up Kwant several times and reduce the memory
    footprint.  (Kwant uses only the sequential, single core version
    of MUMPS.  The advantages due to MUMPS as used by Kwant are thus independent
    of the number of CPU cores of the machine on which Kwant runs.)
39b65acd
  * The `py.test testing framework <http://pytest.org/>`_ 3.3.2 or newer for running the
fa012f86
    tests included with Kwant.
2a1bfc72
 
fa012f86
 In addition, to build a copy of Kwant that has been checked-out directly from
39b65acd
 version control, you will also need `Cython <http://cython.org/>`_ 0.26.1 or
c7ed2337
 newer.  You do not need Cython to build Kwant that has been unpacked from a
 source .tar.gz-file.
e1006005
 
158c80a3
 
a526e8f5
 Building and installing Kwant
 =============================
2a1bfc72
 
fa012f86
 Kwant can be built and installed following the `usual Python conventions
164f611b
 <https://docs.python.org/3/install/index.html>`_ by running the following
 commands in the root directory of the Kwant distribution. ::
2a1bfc72
 
4b0f010f
     python3 setup.py build
     python3 setup.py install
2a1bfc72
 
 Depending on your system, you might have to run the second command with
a526e8f5
 administrator privileges (e.g. prefixing it with ``sudo``).
2a1bfc72
 
158c80a3
 After installation, tests can be run with::
2a1bfc72
 
4b0f010f
     python3 -c 'import kwant; kwant.test()'
2a1bfc72
 
ba423e4c
 The tutorial examples can be found in the directory ``tutorial`` inside the root
fa012f86
 directory of the Kwant source distribution.
ba423e4c
 
a526e8f5
 (Cython will be run automatically when the source tree has been checked out of
 version control.  Kwant tarballs include the Cython-generated files, and
 cythonization is disabled when building not from git.  If ever necessary, this
 default can be overridden by giving the ``--cython`` option to setup.py.)
 
 
efb2ef37
 .. _build-configuration:
 
a526e8f5
 Build configuration
 ===================
 
092f8c28
 Kwant contains several extension modules.  The compilation and linking of these
 modules can be configured by editing a build configuration file.  By default,
 this file is ``build.conf`` in the root directory of the Kwant distribution.  A
 different path may be provided using the ``--configfile=PATH`` option.
 
 This configuration file consists of
22a3547f
 sections, one for each extension module that is contained in Kwant, led by a
16f4d000
 ``[section name]`` header and followed by ``key = value`` lines.
 
 The sections bear the names of the extension modules, for example
87abda2e
 ``[kwant.operator]``.  There can be also a
16f4d000
 ``[DEFAULT]`` section that provides default values for all extensions, also
 those not explicitly present in the file.
 
 Possible keys are the keyword arguments for ``distutils.core.Extension`` (For a
 complete list, see its `documentation
164f611b
 <https://docs.python.org/3/distutils/apiref.html#distutils.core.Extension>`_).
a526e8f5
 The corresponding values are whitespace-separated lists of strings.
 
10faf93a
 Example ``build.conf`` for compiling Kwant with C assertions and Cython's line
 trace feature::
16f4d000
 
     [DEFAULT]
     undef_macros = NDEBUG
10faf93a
     define_macros = CYTHON_TRACE=1
16f4d000
 
87abda2e
 Kwant can optionally be linked against MUMPS.  The main
16f4d000
 application of build configuration is adopting the build process to the various
87abda2e
 deployments of MUMPS. MUMPS will be not linked
16f4d000
 against by default, except on Debian-based systems when the package
 ``libmumps-scotch-dev`` is installed.
 
87abda2e
 The section ``[kwant.linalg._mumps]`` may be used to adapt the build process.
 (For simplicity and backwards compatibility, ``[mumps]`` is an aliases for the above.)
16f4d000
 
a526e8f5
 
 Example ``build.conf`` for linking Kwant against a self-compiled MUMPS, `SCOTCH
 <http://www.labri.fr/perso/pelegrin/scotch/>`_ and `METIS
 <http://glaros.dtc.umn.edu/gkhome/metis/metis/overview>`_::
 
     [mumps]
     libraries = zmumps mumps_common pord metis esmumps scotch scotcherr mpiseq gfortran
 
 The detailed syntax of ``build.conf`` is explained in the `documentation of
 Python's configparser module
164f611b
 <https://docs.python.org/3/library/configparser.html#supported-ini-file-structure>`_.
a526e8f5
 
 
 Building the documentation
 ==========================
 
 To build the documentation, the `Sphinx documentation generator
164f611b
 <http://www.sphinx-doc.org/en/stable/>`_ is required with ``numpydoc`` extension
cca0a19f
 (version 0.5 or newer), as well as ``jupyter-sphinx`` (version 0.2 or newer).
 If PDF documentation is to be built, the tools
 from the `libRSVG <https://wiki.gnome.org/action/show/Projects/LibRsvg>`_
16165acc
 (Debian/Ubuntu package ``librsvg2-bin``) and a Sphinx extension
 ``sphinxcontrib-svg2pdfconverter`` are needed to convert SVG drawings into the
 PDF format.
a526e8f5
 
 As a prerequisite for building the documentation, Kwant must have been built
4b0f010f
 successfully using ``python3 setup.py build`` as described above (or Kwant must
a526e8f5
 be already installed in Python's search path).  HTML documentation is built by
 entering the ``doc`` subdirectory of the Kwant package and executing ``make
 html``.  PDF documentation is generated by executing ``make latex`` followed
 by ``make all-pdf`` in ``doc/build/latex``.
 
 Because of some quirks of how Sphinx works, it might be necessary to execute
 ``make clean`` between building HTML and PDF documentation.  If this is not
 done, Sphinx may mistakenly use PNG files for PDF output or other problems may
 appear.
 
 ****************************
 Hints for specific platforms
 ****************************
2a1bfc72
 
bb5bf832
 Unix-like systems (GNU/Linux)
44875651
 =============================
2a1bfc72
 
fa012f86
 Kwant should run on all recent Unix-like systems.  The following instructions
a526e8f5
 have been verified to work on Debian 8 (Jessie) or newer, and on Ubuntu 14.04 or
8b75021c
 newer.  For other distributions step 1 will likely have to be adapted.  If
 Ubuntu-style ``sudo`` is not available, the respective command must be run as
 root.
c451f672
 
6d3e64a1
 1. Install the required packages.  On Debian-based systems like Ubuntu this can
    be done by running the command ::
2a1bfc72
 
87abda2e
        sudo apt-get install python3-dev python3-setuptools python3-scipy python3-matplotlib python3-pytest python3-sympy g++ gfortran libmumps-scotch-dev
2a1bfc72
 
fa012f86
 2. Unpack Tinyarray, enter its directory. To build and install, run ::
2a1bfc72
 
4b0f010f
        python3 setup.py build
        sudo python3 setup.py install
2a1bfc72
 
fa012f86
 3. Inside the Kwant source distribution's root directory run ::
2a1bfc72
 
4b0f010f
        python3 setup.py build
        sudo python3 setup.py install
2a1bfc72
 
4b0f010f
 By default the package will be installed under ``/usr/local``.  Run ``python3
a526e8f5
 setup.py --help install`` for installation options.
2a1bfc72
 
 
44875651
 Mac OS X: MacPorts
 ==================
2a1bfc72
 
4b0f010f
 The following instructions are valid for Kwant 1.1 with Python 2.7.  They need
 to be updated for Kwant 1.2.  (Help is welcome.)
 
f06ede15
 The required dependencies of Kwant are best installed with one of the packaging
76459aa4
 systems. Here we only consider the case of `MacPorts
164f611b
 <https://www.macports.org>`_ in detail. Some remarks for homebrew are given
76459aa4
 below.
2a1bfc72
 
a526e8f5
 1. Install a recent version of MacPorts, as explained in the `installation
ff7eb28c
    instructions of MacPorts <https://www.macports.org/install.php>`_.
2a1bfc72
 
76459aa4
 2. Install the required dependencies::
2a1bfc72
 
76459aa4
        sudo port install gcc47 python27 py27-numpy py27-scipy py27-matplotlib mumps_seq
5126be75
        sudo port select --set python python27
2a1bfc72
 
76459aa4
 3. Unpack Tinyarray, enter its directory, build and install::
2a1bfc72
 
        python setup.py build
        sudo python setup.py install
 
65d9583a
 4. Unpack Kwant, go to the Kwant directory, and edit ``build.conf`` to read::
5126be75
 
        [mumps]
76459aa4
        include_dirs = /opt/local/include
        library_dirs = /opt/local/lib
        libraries = zmumps_seq mumps_common_seq pord_seq esmumps scotch scotcherr mpiseq gfortran
5126be75
 
65d9583a
 5. Then, build and install Kwant. ::
2a1bfc72
 
76459aa4
        CC=gcc-mp-4.7 LDSHARED='gcc-mp-4.7 -shared -undefined dynamic_lookup' python setup.py build
2a1bfc72
        sudo python setup.py install
 
db933bc0
 You might note that installing Kwant on Mac OS X is somewhat more involved than
76459aa4
 installing on Linux. Part of the reason is that we need to mix Fortran and C
 code in Kwant: While C code is usually compiled using Apple compilers,
 Fortran code must be compiled with the Gnu Fortran compiler (there is
 no Apple Fortran compiler). For this reason we force the Gnu compiler suite
 with the environment variables ``CC`` and ``LDSHARED`` as shown above.
 
bb5bf832
 
44875651
 Mac OS X: homebrew
 ==================
76459aa4
 
4b0f010f
 The following instructions are valid for Kwant 1.1 with Python 2.7.  They need
 to be updated for Kwant 1.2.  (Help is welcome.)
 
f06ede15
 It is also possible to build Kwant using homebrew. The dependencies can be
76459aa4
 installed as ::
 
31fa8b62
     brew install gcc python
76459aa4
     brew tap homebrew/science
31fa8b62
     brew tap homebrew/python
a526e8f5
     brew tap kwant-project/kwant
cee12aad
     pip install pytest pytest-runner six
76459aa4
     brew install numpy scipy matplotlib
 
 Note that during the installation you will be told which paths to add when you
 want to compile/link against scotch/metis/mumps; you need to add these to the
a526e8f5
 build.conf file. Also, when linking against MUMPS, one needs also to link
 against METIS (in addition to the libraries needed for MacPorts).
2a1bfc72
 
 
a526e8f5
 Microsoft Windows
 =================
99d8a50d
 
 Our efforts to compile Kwant on Windows using only free software (MinGW) were
 only moderately successful.  At the end of a very complicated process we
 obtained packages that worked, albeit unreliably.  As the only recommended way
 to compile Python extensions on Windows is using Visual C++, it may well be that
 there exists no easy solution.
 
 It is possible to compile Kwant on Windows using non-free compilers, however we
 (the authors of Kwant) have no experience with this.  The existing Windows
 binary installers of Kwant and Tinyarray were kindly prepared by Christoph
 Gohlke.