Skip to content

Commit 3acfc36

Browse files
authored
Expose support to install diagham
* implement and expose function to install diagham automatically * move diagham installation * format * fix test
1 parent fc37942 commit 3acfc36

5 files changed

Lines changed: 125 additions & 99 deletions

File tree

src/DiagHamInterface.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Interface to the DiagHam library.
44
module DiagHamInterface
55

66
export execute_diagham_script
7+
export install_diagham
78

89
export read_matrix_from_txt
910
export write_to_txt
@@ -17,6 +18,7 @@ using Preferences
1718
include("utility/backup.jl")
1819
include("utility/diagham_path.jl")
1920
include("utility/execute_script.jl")
21+
include("utility/diagham_install.jl")
2022
include("utility/fileending.jl")
2123
include("utility/numbers.jl")
2224
include("utility/standards.jl")

src/utility/diagham_install.jl

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
install_diagham(; source_dir, build_dir=nothing, run_dir=nothing, configure_options=["--enable-fqhe","--enable-fti","--with-blas-libs=-lopenblas","--with-lapack-libs=","--enable-lapack"])
3+
4+
Checkout and build DiagHam from source. Returns the path to the build directory.
5+
6+
Parameters are keyword-only and intended to be easily overridden from tests or user code.
7+
"""
8+
function install_diagham(;
9+
source_dir,
10+
build_dir = nothing,
11+
run_dir = nothing,
12+
configure_options = ["--enable-fqhe", "--enable-fti", "--with-blas-libs=-lopenblas", "--with-lapack-libs=", "--enable-lapack"],
13+
)
14+
source_dir = expanduser(source_dir)
15+
16+
isnothing(build_dir) && (build_dir = joinpath(source_dir, "build"))
17+
build_dir = expanduser(build_dir)
18+
!isnothing(run_dir) && (run_dir = expanduser(run_dir))
19+
20+
# Check if already built (look for a known executable)
21+
exe_check = joinpath(build_dir, "FQHE", "src", "Programs", "FQHEOnTorus", "FQHETorusFermionsTwoBodyGeneric")
22+
if isdir(build_dir) && isfile(exe_check)
23+
@info "DiagHam already installed at $build_dir"
24+
return build_dir
25+
end
26+
27+
# Checkout DiagHam
28+
if !isdir(source_dir)
29+
@info "Checking out DiagHam from SVN repository..."
30+
run(`svn checkout "https://www.nick-ux.org/diagham/svn/DiagHam/trunk" $source_dir`)
31+
else
32+
@info "DiagHam source already exists at $source_dir"
33+
end
34+
35+
!isdir(build_dir) && mkdir(build_dir)
36+
# Check if it uses autotools or cmake
37+
if isfile(joinpath(source_dir, "CMakeLists.txt"))
38+
# CMake-based build
39+
40+
@info "Configuring DiagHam with CMake..."
41+
cd(build_dir) do
42+
run(`cmake $source_dir -DBUILD_FQHE=ON`)
43+
end
44+
45+
@info "Building DiagHam..."
46+
cd(build_dir) do
47+
nprocs = Sys.CPU_THREADS
48+
run(`make -j$nprocs`)
49+
end
50+
elseif isfile(joinpath(source_dir, "configure"))
51+
# Autotools-based build (older DiagHam versions)
52+
@info "Configuring DiagHam with autotools..."
53+
54+
cd(build_dir) do
55+
# Run configure from build dir pointing to source dir with FQHE and LAPACK enabled
56+
configure_script = joinpath(source_dir, "configure")
57+
run(`$configure_script $(configure_options...)`)
58+
59+
@info "Building DiagHam..."
60+
nprocs = Sys.CPU_THREADS
61+
run(`make -j$nprocs`)
62+
end
63+
elseif isfile(joinpath(source_dir, "configure.in")) || isfile(joinpath(source_dir, "Makefile.am"))
64+
# Need to run autoreconf first
65+
@info "Running autoreconf to generate configure script..."
66+
cd(source_dir) do
67+
run(`autoreconf -i`)
68+
end
69+
70+
cd(build_dir) do
71+
# Run configure from build dir pointing to source dir with FQHE and LAPACK enabled
72+
configure_script = joinpath(source_dir, "configure")
73+
run(`$configure_script --enable-fqhe --enable-fti --with-blas-libs=-lopenblas --with-lapack-libs= --enable-lapack`)
74+
75+
@info "Building DiagHam..."
76+
nprocs = Sys.CPU_THREADS
77+
run(`make -j$nprocs`)
78+
end
79+
else
80+
error("Could not determine build system for DiagHam")
81+
end
82+
83+
!isdir(run_dir) && !isnothing(run_dir) && mkdir(run_dir)
84+
85+
@info "DiagHam installed successfully at $build_dir"
86+
87+
if !@has_preference("diagham_path")
88+
set_diagham_path(build_dir)
89+
end
90+
91+
return build_dir
92+
end

test/diagham/setup.jl

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -9,115 +9,38 @@ const DIAGHAM_SOURCE_DIR = joinpath(dirname(@__DIR__), "diagham_source")
99
const DIAGHAM_BUILD_DIR = joinpath(DIAGHAM_SOURCE_DIR, "build")
1010
const DIAGHAM_RUN_DIR = joinpath(DIAGHAM_BUILD_DIR, "run")
1111

12-
"""
13-
install_diagham()
14-
15-
Checkout and build DiagHam from source. Returns the path to the build directory.
16-
"""
17-
function install_diagham()
18-
# Check if already built
19-
if isdir(DIAGHAM_BUILD_DIR) && isfile(joinpath(DIAGHAM_BUILD_DIR, "FQHE", "src", "Programs", "FQHEOnTorus", "FQHETorusFermionsTwoBodyGeneric"))
20-
@info "DiagHam already installed at $DIAGHAM_BUILD_DIR"
21-
return DIAGHAM_BUILD_DIR
22-
end
23-
24-
# Checkout DiagHam if not exists
25-
if !isdir(DIAGHAM_SOURCE_DIR)
26-
@info "Checking out DiagHam from SVN repository..."
27-
mkdir(dirname(DIAGHAM_SOURCE_DIR))
28-
run(`svn checkout https://www.nick-ux.org/diagham/svn/DiagHam/trunk $DIAGHAM_SOURCE_DIR`)
29-
else
30-
@info "DiagHam source already exists at $DIAGHAM_SOURCE_DIR"
31-
end
32-
33-
# Check if it uses autotools or cmake
34-
if isfile(joinpath(DIAGHAM_SOURCE_DIR, "CMakeLists.txt"))
35-
# CMake-based build
36-
if !isdir(DIAGHAM_BUILD_DIR)
37-
mkdir(DIAGHAM_BUILD_DIR)
38-
end
12+
using DiagHamInterface: install_diagham
3913

40-
@info "Configuring DiagHam with CMake..."
41-
cd(DIAGHAM_BUILD_DIR) do
42-
run(`cmake $DIAGHAM_SOURCE_DIR -DBUILD_FQHE=ON`)
43-
end
44-
45-
@info "Building DiagHam..."
46-
cd(DIAGHAM_BUILD_DIR) do
47-
nprocs = Sys.CPU_THREADS
48-
run(`make -j$nprocs`)
49-
end
50-
elseif isfile(joinpath(DIAGHAM_SOURCE_DIR, "configure"))
51-
# Autotools-based build (older DiagHam versions)
52-
@info "Configuring DiagHam with autotools..."
53-
54-
if !isdir(DIAGHAM_BUILD_DIR)
55-
mkdir(DIAGHAM_BUILD_DIR)
56-
end
57-
58-
cd(DIAGHAM_BUILD_DIR) do
59-
# Run configure from build dir pointing to source dir with FQHE and LAPACK enabled
60-
configure_script = joinpath(DIAGHAM_SOURCE_DIR, "configure")
61-
run(`$configure_script --enable-fqhe --enable-fti --with-blas-libs=-lopenblas --with-lapack-libs= --enable-lapack`)
62-
63-
@info "Building DiagHam..."
64-
nprocs = Sys.CPU_THREADS
65-
run(`make -j$nprocs`)
66-
end
67-
elseif isfile(joinpath(DIAGHAM_SOURCE_DIR, "configure.in")) || isfile(joinpath(DIAGHAM_SOURCE_DIR, "Makefile.am"))
68-
# Need to run autoreconf first
69-
@info "Running autoreconf to generate configure script..."
70-
cd(DIAGHAM_SOURCE_DIR) do
71-
run(`autoreconf -i`)
72-
end
73-
74-
if !isdir(DIAGHAM_BUILD_DIR)
75-
mkdir(DIAGHAM_BUILD_DIR)
76-
end
77-
78-
cd(DIAGHAM_BUILD_DIR) do
79-
# Run configure from build dir pointing to source dir with FQHE and LAPACK enabled
80-
configure_script = joinpath(DIAGHAM_SOURCE_DIR, "configure")
81-
run(`$configure_script --enable-fqhe --enable-fti --with-blas-libs=-lopenblas --with-lapack-libs= --enable-lapack`)
82-
83-
@info "Building DiagHam..."
84-
nprocs = Sys.CPU_THREADS
85-
run(`make -j$nprocs`)
86-
end
87-
else
88-
error("Could not determine build system for DiagHam")
89-
end
14+
# Delegate installation to the exported package function so tests and users
15+
# use the shared implementation. The test constants are still defined here
16+
# for convenience and passed to the package function below.
9017

91-
if !isdir(DIAGHAM_RUN_DIR)
92-
mkdir(DIAGHAM_RUN_DIR)
93-
end
94-
95-
@info "DiagHam installed successfully at $DIAGHAM_BUILD_DIR"
96-
return DIAGHAM_BUILD_DIR
97-
end
98-
99-
"""
100-
diagham_available()
101-
102-
Check if DiagHam is available (built and ready to use).
103-
"""
104-
function diagham_available()
105-
# Check for common FQHE executables
18+
function diagham_available(build_dir::AbstractString = DIAGHAM_BUILD_DIR)
10619
possible_paths = [
107-
joinpath(DIAGHAM_BUILD_DIR, "FQHE", "src", "Programs", "FQHEOnDisk", "FQHEDiskFermionsTwoBodyGeneric"),
108-
joinpath(DIAGHAM_BUILD_DIR, "FQHE", "src", "Programs", "FQHEOnTorus", "FQHETorusFermionsTwoBodyGeneric"),
109-
joinpath(DIAGHAM_BUILD_DIR, "FTI", "src", "Programs", "FTI", "FTIGenericInteractionFromFileTwoBands"),
110-
joinpath(DIAGHAM_BUILD_DIR, "src", "Programs", "GenericHamiltonianDiagonalization"),
111-
joinpath(DIAGHAM_BUILD_DIR, "src", "Programs", "GenericOverlap"),
20+
joinpath(build_dir, "FQHE", "src", "Programs", "FQHEOnDisk", "FQHEDiskFermionsTwoBodyGeneric"),
21+
joinpath(build_dir, "FQHE", "src", "Programs", "FQHEOnTorus", "FQHETorusFermionsTwoBodyGeneric"),
22+
joinpath(build_dir, "FTI", "src", "Programs", "FTI", "FTIGenericInteractionFromFileTwoBands"),
23+
joinpath(build_dir, "src", "Programs", "GenericHamiltonianDiagonalization"),
24+
joinpath(build_dir, "src", "Programs", "GenericOverlap"),
11225
]
11326
return any(isfile, possible_paths)
11427
end
11528

116-
if !diagham_available()
29+
30+
"""
31+
Call the package `install_diagham` with the test-local defaults.
32+
"""
33+
function ensure_diagham_installed()
34+
if diagham_available(DIAGHAM_BUILD_DIR)
35+
@info "DiagHam already installed at $DIAGHAM_BUILD_DIR"
36+
return DIAGHAM_BUILD_DIR
37+
end
38+
11739
@info "DiagHam not installed, attempting installation..."
11840
try
119-
install_diagham()
41+
install_diagham(; source_dir = DIAGHAM_SOURCE_DIR)
12042
catch e
12143
@warn "Failed to install DiagHam: $e"
12244
end
45+
return DIAGHAM_BUILD_DIR
12346
end

test/diagham/test_execute_diagham_script.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ using Test
77
include("setup.jl")
88

99
@testset "execute_diagham_script (requires DiagHam)" begin
10+
11+
@test ensure_diagham_installed() == DIAGHAM_BUILD_DIR
1012
if diagham_available()
1113
# Set the DiagHam path for these tests
1214
@test DiagHamInterface.set_diagham_path(DIAGHAM_BUILD_DIR) == expanduser(DIAGHAM_BUILD_DIR)

test/diagham/test_write_matrix_elements.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ const TEST_ATOL_COMPLEX = 1.0e-10
6666
# Read back and verify coefficients are preserved
6767
header, read_indices, read_coeffs = read_matrix_elements(filename)
6868
@test all(isapprox.(read_coeffs, coeffs; atol = TEST_ATOL_REAL))
69+
@test read_indices == indices
70+
@test label == header
6971
end
7072
end
7173

@@ -97,6 +99,11 @@ const TEST_ATOL_COMPLEX = 1.0e-10
9799

98100
# Check that complex coefficients are preserved
99101
@test all(isapprox.(read_coeffs, coeffs; atol = TEST_ATOL_COMPLEX))
102+
@test read_indices == indices
103+
@test label == header
104+
105+
header, read_indices, read_coeffs = read_matrix_elements(filename; conjugate = true)
106+
@test all(isapprox.(read_coeffs, conj.(coeffs); atol = TEST_ATOL_COMPLEX))
100107
end
101108
end
102109

0 commit comments

Comments
 (0)