## This script describes how to install the correct branch of the su2 library and its dependencies
## This was tested on 2025-07-30, on the cluster juwels-booster at FZ Juelich (https://apps.fz-juelich.de/jsc/hps/juwels/booster-overview.html) 
## and on the cluster Marvin at the University of Bonn (https://www.hpc.uni-bonn.de/en/systems/marvin).
## This script should be adaptable to other HPC clusters.
## At the end of the file is a small test of functionality.


## create the folder where you want to install the code and go to it, e.g. mkdir -p ~/mycode; cd ~/mycode
## copy the contents of the folder 'generation' to your installation folder
## alternatively
## mkdir code
## cd code
## git clone git@github.com:HISKP-LQCD/su2.git
## cd su2
## git checkout 0bcdb4bad6cb51f7c20fca16f73d76760f5b3504
## cd ../..



## juwels booster does not allow to pull from git repos directly, therefore we have to pull the repos by mounting the folder on our home computer.
## e.g. sshfs yourusername@juwels-booster.fz-juelich.de:/p/home/jusers/yourusername/juwels/mycode mnt_juwels/; cd mnt_juwels
## pull the software
export mycode=$(pwd)
cd ${mycode}/code
git clone git@github.com:jbeder/yaml-cpp.git
git clone git@github.com:xtensor-stack/xtl.git
git clone git@github.com:xtensor-stack/xtensor.git
cd ${mycode}


## The rest is done on the cluster
## load the needed modules. This has to be adapted for other clusters
## juwels-booster
module --force purge
module load Stages/2025
module load GCCcore/.13.3.0
module load Boost/1.86.0
module load CMake/3.30.3
module load Eigen/3.4.0
module list

## marvin
module --force purge
module load GCCcore/13.3.0
module load Boost/1.85.0-GCC-13.3.0
module load CMake/3.29.3-GCCcore-13.3.0
module load Eigen/3.4.0-GCCcore-13.3.0
module list

## checkout the correct branches of the code
export mycode=$(pwd)
cd ${mycode}/code/xtl
git checkout d11fb6b5f4c417025124ed2c62175284846a1914
cd ${mycode}/code/xtensor
git checkout 8c0a484f04eccd0dbc0e25eb58a97de000fb048b

## make directory structure for installation
cd ${mycode}
mkdir build
mkdir install
mkdir build/xtl
mkdir build/xtensor
mkdir build/yaml-cpp
mkdir build/su2
mkdir install/xtl
mkdir install/xtensor
mkdir install/yaml-cpp
mkdir install/su2

## your folder should look like this:
## tree -L 2
## .
## ├── build
## │   ├── su2
## │   ├── xtensor
## │   ├── xtl
## │   └── yaml-cpp
## ├── code
## │   ├── su2
## │   ├── xtensor
## │   ├── xtl
## │   └── yaml-cpp
## └── install
##     ├── su2
##     ├── xtensor
##     ├── xtl
##     └── yaml-cpp


## install the dependencies
cd ${mycode}/build/xtl/
cmake -S ${mycode}/code/xtl -B ${mycode}/build/xtl -DCMAKE_INSTALL_PREFIX=${mycode}/install/xtl
make install

cd ${mycode}/build/xtensor
cmake -S ${mycode}/code/xtensor -B ${mycode}/build/xtensor -DCMAKE_INSTALL_PREFIX=${mycode}/install/xtensor -DCMAKE_PREFIX_PATH=${mycode}/install/xtl/
make install

cd ${mycode}/build/yaml-cpp/
cmake -S ${mycode}/code/yaml-cpp -B ${mycode}/build/yaml-cpp -DCMAKE_INSTALL_PREFIX=${mycode}/install/yaml-cpp
make install


## install and compile the source code for the ensemble generation
## if you want to use CMake, download the project https://doi.org/10.5281/zenodo.11176392 and copy the file CMakeLists.txt into the directory with the source code for the ensemble generation.
## Then do the following for the installation:
cd ${mycode}/build/su2/

export bdir=$(pwd -P) # build directory
export yaml_cpp="${mycode}/install/yaml-cpp/"
export xtl="${mycode}/install/xtl/"
export xtensor="${mycode}/install/xtensor/"

echo $bdir
echo $yaml_cpp
echo $xtl
echo $xtensor

cmake  \
-D CMAKE_BUILD_TYPE=release \
-S ${mycode}/code/su2 \
-B ${mycode}/build/su2 \
-D CMAKE_PREFIX_PATH="$yaml_cpp;$xtl;$xtensor" \
-D CMAKE_INSTALL_PREFIX=${mycode}/install/su2
  
make install
make


## Test:
mkdir ${mycode}/try_su2
cd ${mycode}/try_su2
## generate file test_input
${mycode}/build/su2/u1-main -f test_input

## cat test_input 
## geometry:
##   X: 3
##   Y: 3
##   Z: 1
##   T: 3
##   ndims: 3
## 
## monomials:
##   gauge:
##     beta: 1.650000
##     anisotropic:
##       xi: 1
## 
## metropolis:
##   do_mcmc: true
##   conf_dir: ./conf
##   conf_basename: config_u1
##   n_meas: 30
##   N_save: 10
##   delta: 0.606061
##   restart_condition: hot
##   lenghty_conf_name: true
## 
## omeas:
##   offline:
##     conf_dir: ./conf
##     lenghty_conf_name: true
## 
##   res_dir: .
##   icounter: 0
##   n_meas: 30
##   nstep: 10
##   potential:
##     potentialplanar: false 
##     potentialnonplanar: true
##     sizeWloops: 0.5
##     n_apesmear: 0
##     alpha: 1
##     append: true

# ## Output: 
# ## Parsing input file: test_input
# ## metropolis:do_mcmc=1
# ## Metropolis Algorithm for U(1) gauge theory
# ## GIT branch su3 on commit 0bcdb4b
# ## Cleaned yaml node:
# geometry:
#   X: 3
#   Y: 3
#   Z: 1
#   T: 3
#   ndims: 3
# monomials:
#   gauge:
#     beta: 1.650000
#     anisotropic:
#       xi: 1
# metropolis:
#   do_mcmc: true
#   conf_dir: ./conf
#   conf_basename: config_u1
#   n_meas: 30
#   N_save: 10
#   delta: 0.606061
#   restart_condition: hot
#   lenghty_conf_name: true
# omeas:
#   offline:
#     conf_dir: ./conf
#     lenghty_conf_name: true
#   res_dir: .
#   icounter: 0
#   n_meas: 30
#   nstep: 10
#   potential:
#     potentialplanar: false
#     potentialnonplanar: true
#     sizeWloops: 0.5
#     n_apesmear: 0
#     alpha: 1
#     append: true
# ## geometry:X=3
# ## geometry:Y=3
# ## geometry:Z=1
# ## geometry:T=3
# ## geometry:ndims=3
# ## geometry:bc=periodic (default)
# ## Warning: ndims==3 --> flattening the 'z' direction
# ## monomials:gauge:beta=1.65
# ## monomials:gauge:anisotropic:xi=1
# ## metropolis:do_mcmc=1
# ## metropolis:n_meas=30
# ## metropolis:N_save=10
# ## metropolis:seed=13526463 (default)
# ## metropolis:conf_dir=./conf
# ## metropolis:conf_basename=config_u1
# ## metropolis:lenghty_conf_name=1
# ## metropolis:beta_str_width=6 (default)
# ## metropolis:restart_condition=hot
# ## metropolis:delta=0.606061
# ## metropolis:N_hit=10 (default)
# ## omeas:offline:conf_dir=./conf
# ## omeas:offline:conf_basename=conf (default)
# ## omeas:offline:lenghty_conf_name=1
# ## omeas:offline:beta_str_width=6 (default)
# ## omeas:res_dir=.
# ## omeas:verbosity=0 (default)
# ## omeas:restart=0 (default)
# ## omeas:icounter=0
# ## omeas:nstep=10
# ## omeas:n_meas=30
# ## omeas:Wloop=0 (default)
# ## omeas:potential:potentialplanar=0
# ## omeas:potential:potentialnonplanar=1
# ## omeas:potential:append=1
# ## omeas:potential:smear_spatial_only=0 (default)
# ## omeas:potential:smear_temporal_only=0 (default)
# ## omeas:potential:n_apesmear=0
# ## omeas:potential:alpha=1
# ## omeas:potential:sizeWloops=0.5
# ## Normalization factor: A = 2/(d*(d-1)*N_lat*N_c) = 1.234567901234568e-02
# ## Acceptance rate parcentage: rho = rate/(i+1)
# ## Initial Plaquette P: -4.645804525294518e-02
# ## Plaquette after rnd trafo: -4.645804525294527e-02
# For parallel computing an even number of points in T is needed!
# Continuing with one thread.
# threads 1
# i E Q E_ss Q_ss 
# 0 5.634413507845668e-01 -0.000000000000000e+00 5.095227966407124e-01 -0.000000000000000e+00
# 1 4.317778285178042e-01 -0.000000000000000e+00 4.741622814925510e-01 -0.000000000000000e+00
# 2 2.692167167956377e-01 -0.000000000000000e+00 2.394504601235053e-01 -0.000000000000000e+00
# 3 2.892092209766479e-01 -0.000000000000000e+00 2.544812333279960e-01 -0.000000000000000e+00
# 4 3.262644704380747e-01 -0.000000000000000e+00 3.163562844024106e-01 -0.000000000000000e+00
# 5 3.701705715341399e-01 -0.000000000000000e+00 4.252521891390114e-01 -0.000000000000000e+00
# 6 3.364305696182703e-01 -0.000000000000000e+00 2.334668245883491e-01 -0.000000000000000e+00
# 7 3.055675990700683e-01 -0.000000000000000e+00 4.091621336192662e-01 -0.000000000000000e+00
# 8 3.749832346152537e-01 -0.000000000000000e+00 4.231687605208316e-01 -0.000000000000000e+00
# 9 3.242367326882052e-01 -0.000000000000000e+00 3.543598603653535e-01 -0.000000000000000e+00
# 10 2.735816262509211e-01 -0.000000000000000e+00 2.678576097196704e-01 -0.000000000000000e+00
# ## measure nonplanar potential
# 11 2.414133808424481e-01 -0.000000000000000e+00 2.316476100018637e-01 -0.000000000000000e+00
# 12 2.630740177684026e-01 -0.000000000000000e+00 2.350937928972758e-01 -0.000000000000000e+00
# 13 3.335204579870082e-01 -0.000000000000000e+00 3.771442575300962e-01 -0.000000000000000e+00
# 14 3.286561979797196e-01 -0.000000000000000e+00 3.848128784825318e-01 -0.000000000000000e+00
# 15 3.633935580636156e-01 -0.000000000000000e+00 4.154729341898775e-01 -0.000000000000000e+00
# 16 2.744435152746401e-01 -0.000000000000000e+00 1.926031509010100e-01 -0.000000000000000e+00
# 17 2.902367833317264e-01 -0.000000000000000e+00 3.381360943149745e-01 -0.000000000000000e+00
# 18 2.766818791577389e-01 -0.000000000000000e+00 2.749257609056434e-01 -0.000000000000000e+00
# 19 3.224475765401649e-01 -0.000000000000000e+00 2.748668576774217e-01 -0.000000000000000e+00
# 20 3.411274403518730e-01 -0.000000000000000e+00 2.823556364096869e-01 -0.000000000000000e+00
# ## measure nonplanar potential
# 21 3.471197918982440e-01 -0.000000000000000e+00 3.513126059121228e-01 -0.000000000000000e+00
# 22 2.320484491609613e-01 -0.000000000000000e+00 2.300934876521937e-01 -0.000000000000000e+00
# 23 2.973984431216110e-01 -0.000000000000000e+00 3.229125990795017e-01 -0.000000000000000e+00
# 24 2.929167096548871e-01 -0.000000000000000e+00 2.660971263172641e-01 -0.000000000000000e+00
# 25 2.713134636480585e-01 -0.000000000000000e+00 2.091761233686701e-01 -0.000000000000000e+00
# 26 3.111259984348184e-01 -0.000000000000000e+00 3.285367711799684e-01 -0.000000000000000e+00
# 27 2.478830098771290e-01 -0.000000000000000e+00 2.377255505513586e-01 -0.000000000000000e+00
# 28 3.296251102986459e-01 -0.000000000000000e+00 3.552221545937449e-01 -0.000000000000000e+00
# 29 3.216463500284021e-01 -0.000000000000000e+00 3.194755164466339e-01 -0.000000000000000e+00
# ## Acceptance rate 3.659670781893002e-01 temporal acceptance rate 3.580246913580247e-01

