#///////////////////////////////////////////////////////////////////////
#
#      Author:          M. Shiga
#      Last updated:    Nov 25, 2018 by M. Shiga
#      Description:     makefile for the PIMD code
#
#///////////////////////////////////////////////////////////////////////
#-----------------------------------------------------------------------
#     Fortran compiler
#-----------------------------------------------------------------------

FCMP = mpif90
#FCMP = mpiifort -g -O2 -traceback

#-----------------------------------------------------------------------
#     C compiler
#-----------------------------------------------------------------------

CC = gcc

#-----------------------------------------------------------------------
#     machine dependent compile options
#-----------------------------------------------------------------------

#     MAC OSX:
OPTS = -g -O4 -Wall -framework Accelerate
#OPTS += -fbounds-check -Wuninitialized -fbacktrace
#OPTS = -Ofast -L/home/mshiga/lib/ -llapack -lrefblas -ltmglib
OPTS += -fallow-argument-mismatch
#OPTS += -Wno-argument-mismatch

#     compile PIMD with lapack
# LAPACK = -Dlapack

#     compile PIMD without lapack
# LAPACK = -Dnolapack
#LAPACK = -lmkl_gf_lp64  -lmkl_sequential -lmkl_core

#     compile PIMD with PME
# PME = -Dpme

#     compile PIMD without PME
PME = -Dnopme

#-----------------------------------------------------------------------
#     version
#-----------------------------------------------------------------------

#     Current version
OPTS +=

#     Next version
# OPTS += -Dnextver

#-----------------------------------------------------------------------
#     Usage of SMASH
#-----------------------------------------------------------------------

#     compile PIMD without smash
SMASH =

#     compile PIMD with smash
# SMASH = -Dsmash

#     smash with openmp
# ifdef SMASH
# SMASHOPT = -DnoMPI -openmp -xHOST $(OPTS)
# SMASHOPTMPI = -openmp -xHOST $(OPTS)
# endif

#     smash with mpi
ifdef SMASH
SMASHOPT = -DnoMPI $(OPTS)
SMASHOPTMPI = $(OPTS)
endif

#     smash library
ifdef SMASH
LIBSMASH    = ../lib/libsmash.a
LIBSMASHMPI = ../lib/libsmashmpi.a
endif

#     perodic boundary condition ... this is for next version!
# PBC = -Dpbc

#     directory of smash
ifdef SMASH
  SMASHSRCDIR = ../lib/smash/smash.2.2.0.src
  ifdef PBC
    SMASHDIR = ../lib/smash/smash.2.2.0.pbc
  else
    SMASHDIR = ../lib/smash/smash.2.2.0.modified
  endif
endif

#-----------------------------------------------------------------------
#     Usage of NTCHEM
#-----------------------------------------------------------------------

#     compile PIMD without ntchem
NTCHEM =

#     compile PIMD with ntchem (with OPENMP)
# NTCHEM = -Dntchem -Domp

#     compile PIMD with ntchem (without OPENMP)
# NTCHEM = -Dntchem

#     ntchem with mpi/openmp
ifdef NTCHEM
NTCHEMOPT  =  -mkl=parallel -cxxlib -lstdc++ -nofor-main
LIBNTCHEM  =  ../lib/libmntchem_mpiomp.a $(NTCHEMOPT)
endif

#     ntchem with mpi
# ifdef NTCHEM
# LIBNTCHEM  =  ../lib/libmntchem_mpi.a $(NTCHEMOPT)
# endif

#-----------------------------------------------------------------------
#     Usage of PHASE0
#-----------------------------------------------------------------------

#     compile PIMD with PHASE/0
# PHASE0 = -Dphase0
PHASE0 =

ifdef PHASE0
LIBPHASE0 = -L../lib/phase0 -lphase -lesm #-L. -lfftw3
LIBMKL = -L../lib -L$(MKLROOT)/lib/intel64 -L$(HOME)/mkl \
 -lmkl_scalapack_lp64 -lmkl_blacs_intelmpi_lp64 -lmkl_intel_lp64 \
 -lmkl_sequential -lmkl_cdft_core -lfftw3x_cdft_lp64 -lmkl_core -lm -ldl
endif

#-----------------------------------------------------------------------
#     Usage of VASP
#-----------------------------------------------------------------------

#     compile PIMD without VASP
VASP =

#     compile PIMD with VASP
# VASP = -Dvasp

#     libraries
ifdef VASP
LIBVASP = -L../lib -lvasp -ldmy
LIBMKL  = -L../lib -lmkl_intel_lp64 -lmkl_sequential -lmkl_core \
 -lpthread
endif

#-----------------------------------------------------------------------
#     Usage of QE
#-----------------------------------------------------------------------

#     compile PIMD with QE
# QE = -Dqe

#     compile PIMD without QE
QE =

#     libraries
ifdef QE
LIBQE = -L../lib/qe/q-e-qe-6.2.1/qelib -lqe -L. -lfftw3
#LIBMKL = -L../lib -lmkl_intel_lp64 -lmkl_sequential -lmkl_core \
# -lpthread
endif

#-----------------------------------------------------------------------
#     Usage of AENET
#-----------------------------------------------------------------------

#     reset AENET flag
AENETFLAG =

#     usage of c utility for i/o
# AENETFLAG += -Dioutils

#     usage of neighbor list
AENETFLAG += -Dnlist

#     compile PIMD with AENET
# AENET += -Daenet2 $(AENETFLAG)

#     directory of AENET
ifdef AENET
#LIBAENET = -I../lib/aenet/src_modified ../lib/libaenet.a \
# ../lib/liblbfgsb.a
LIBAENET = -L../lib/aenet/src_modified -laenet \
 -I../lib/aenet/src_modified ../lib/liblbfgsb.a
endif

#-----------------------------------------------------------------------
#     Usage of ABINITMP
#-----------------------------------------------------------------------

#     compile PIMD with abinitmp
# ABINITMP = -Dabinitmp

#     compile PIMD without abinitmp
ABINITMP = 

#     mkl library
ifdef ABINITMP
LIBABINITMP = -L../lib -labinitmp
LIBMKL      = -L../lib -lmkl_intel_lp64 -lmkl_sequential \
 -lmkl_core -lpthread
endif

#-----------------------------------------------------------------------
#     Usage of DFTB+ as static library
#-----------------------------------------------------------------------

#     compile PIMD with DFTB+ as a static library
# DFTBLIB = -Ddftblib

#     compile PIMD without DFTB+ as a static library
DFTBLIB = 

ifdef DFTBLIB
   #  Add openmp flag to make compilation comply with DFTB+
   OPTS   += -fopenmp
   #  old version
   #  DFTBPDIR = /home/bo/QM-Software/dftbplus/_build/_install/
   #  DFTBPINC := -I$(DFTBPDIR)/include/dftb+/modfiles/
   #  DFTBPLIB := -L$(DFTBPDIR)/lib/ -ldftbplus
   #  DFTB+ installation includes the DFTD3 library
   #  # DFTBPLIB += -ldftd3
   #  new version
   DFTBPDIR = /Users/shiga/Downloads/dftb+/dftbplus-21.1/_build/
   DFTBPLIB := -L$(DFTBPDIR)/prog/dftb+/ -ldftbplus
   DFTBPLIB += -L$(DFTBPDIR)/external/dftd3/origin/lib/ -ldftd3
   DFTBPINC := -I$(DFTBPDIR)/prog/dftb+/include/
endif

#-----------------------------------------------------------------------
#     Usage of CP2K as static library - without MPI
#-----------------------------------------------------------------------

#     compile PIMD without CP2K as a static library
CP2KFLAG = 

#     compile PIMD with CP2K as a static library
#CP2KFLAG = -Dlibcp2k

#     calculate mulliken and hirshfeld charges
#CP2KFLAG += -Dlibcp2kcharge

ifdef CP2KLIBFLAG
   CP2KDIR = /home/g3/a184053/QM-Programs/cp2k_new
   CP2KVER = Linux-x86-64-intel-minimal/ssmp
   CP2KINC  = -I$(CP2KDIR)/obj/$(CP2KVER)/
   CP2KLIB  = -L$(CP2KDIR)/lib/$(CP2KVER)/ -lcp2k \
 -L$(CP2KDIR)/lib/$(CP2KVER)/exts/dbcsr/ -ldbcsr 
endif

#-----------------------------------------------------------------------
#     Usage of CP2K as static library - with MPI
#-----------------------------------------------------------------------

#     compile PIMD without CP2K as a static library
CP2KMPIFLAG = 

#     compile PIMD with CP2K as a static library
#CP2KMPIFLAG = -Dlibcp2kmpi

#     calculate mulliken and hirshfeld charges
#CP2KMPIFLAG += -Dlibcp2kcharge

ifdef CP2KMPIFLAG
   CP2KDIRMPI = /home/g3/a184053/QM-Programs/cp2k_new
   CP2KVER = Linux-x86-64-intel-minimal/psmp
   CP2KINCMPI  = -I$(CP2KDIRMPI)/obj/$(CP2KVER)/
   CP2KLIBMPI  = -L$(CP2KDIRMPI)/lib/$(CP2KVER)/ \
 -lcp2k -L$(CP2KDIRMPI)/lib/$(CP2KVER)/exts/dbcsr/ -ldbcsr 
endif

#-----------------------------------------------------------------------
#     Usage of N2P2
#-----------------------------------------------------------------------

N2P2FLAG =
# N2P2FLAG = -Dn2p2

ifdef N2P2FLAG
  N2P2DIR = ../lib/n2p2/n2p2-2.2.0.modified/lib
  LIBN2P2 = -L/usr/local/Cellar/gsl/2.8/lib -lgsl -lc++ \
 $(N2P2DIR)/libnnp.a $(N2P2DIR)/libnnptrain.a
endif

#-----------------------------------------------------------------------
#     Usage of LIBNNP
#-----------------------------------------------------------------------

LIBNNPFLAG =
# LIBNNPFLAG = -Dlibnnp

ifdef LIBNNPFLAG
  LIBNNPDIR = ../lib/libnnp
#  LIBNNPDIR = /home/mshiga/pimd/bochum/lib/libnnp
  LIBNNP = $(LIBNNPDIR)/libnnp.a
  LIBNNPINC = -I$(LIBNNPDIR)/build/
endif

#-----------------------------------------------------------------------
#     Usage of PFP
#-----------------------------------------------------------------------

#PFPFLAG = 
PFPFLAG = -Dpfp

ifdef PFPFLAG
#  LIBPFP =  -llapack -lblas -lstdc++ -lpython3.11
  LIBPFP = \
 -L/usr/local/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib \
 -lpython3.11 -Wl,-rpath,/usr/local/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib
endif

#-----------------------------------------------------------------------
#     compile options for next PIMD version
#-----------------------------------------------------------------------

#    current version (explicit format for LJ, old QM/MM-ee)
VERSION = 

#    next version (pairwise format of LJ, neighbor list, new QM/MM-ee)
#VERSION = -Dnext

#-----------------------------------------------------------------------
#     compile options and links for PIMD
#-----------------------------------------------------------------------

CODES  = $(SMASH) $(NTCHEM) $(VASP) $(QE) $(ABINITMP) \
         $(LAPACK) $(PME) $(VERSION) $(AENET) $(DFTBLIB) \
         $(CP2KFLAG) $(CP2KMPIFLAG) $(PHASE0) $(N2P2FLAG) \
         $(PFPFLAG)

LIB    = $(LIBABINITMP) $(LIBVASP) $(LIBQE) $(LIBPHASE0) $(LIBMKL) \
         $(LIBSMASH) $(LIBNTCHEM) $(LIBAENET) $(LIBLBFGS) \
         $(CP2KLIB) $(DFTBPLIB) $(LIBN2P2) $(LIBNNPFLAG) $(LIBPFP)

LIBMPI = $(LIBABINITMP) $(LIBVASP) $(LIBQE) $(LIBPHASE0) $(LIBMKL) \
         $(LIBSMASHMPI) $(LIBNTCHEM) $(LIBAENET) $(LIBLBFGS) \
         $(CP2KLIB) $(CP2KLIBMPI) $(DFTBPLIB) $(LIBNNP) $(LIBN2P2) \
         $(LIBPFP)

FLAGMP = $(LAPACK) $(CODES) $(OPTS) $(CP2KINC) $(CP2KINCMPI) \
         $(DFTBPINC) $(LIBNNPINC) $(LIBN2P2INC)

#-----------------------------------------------------------------------
#     compile PIMD
#-----------------------------------------------------------------------

include makefile.inc

#-----------------------------------------------------------------------
#     compile SMASH, AENET
#-----------------------------------------------------------------------

include makefile.smash.inc
#include makefile.aenet.inc

