Skip to content

Commit c33ed21

Browse files
committed
🎉 Initial commit. Added source files
0 parents  commit c33ed21

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

perturb.jl

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using LinearAlgebra
2+
using DelimitedFiles
3+
4+
N = 8;
5+
lambda = 0.1;
6+
H = Array(Diagonal(rand(ComplexF64,N)));
7+
8+
# For comparison with Fortran, add anharmonic term in Julia
9+
begin
10+
X = zeros(ComplexF64,N,N)
11+
for j in 1:N
12+
for i in 1:N
13+
if abs(i-j) == 1
14+
X[i,j]= complex(0.5 * sqrt(i+j+1))
15+
end
16+
end
17+
end
18+
native = H + lambda * X^4
19+
end
20+
21+
function perturb!(N,lambda,H)
22+
ccall((:perturb_,"./libperturb.so"), Cvoid, (Ref{Int64}, Ref{ComplexF64}, Ref{ComplexF64}), N, lambda, H)
23+
return H
24+
end
25+
26+
perturb!(N,lambda,H)
27+
28+
29+
# Write results to file
30+
begin
31+
open("fortrancall.txt","w") do f
32+
writedlm(f,H)
33+
end
34+
35+
open("julia.txt","w") do f
36+
writedlm(f,native)
37+
end
38+
end
39+
40+
# Norm of residuals
41+
println("norm(H - native) = ", norm(H - native))

perturbhamiltonian.f90

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
SUBROUTINE PERTURB(N, lambda, H)
3+
integer*8, intent(in) :: N
4+
complex*16, intent(in):: lambda
5+
complex*16, intent(out) :: H(N,N)
6+
complex*16, ALLOCATABLE, DIMENSION(:,:) :: X, X2, X4
7+
complex*16 :: czero=0.0_16
8+
9+
integer :: i, j
10+
11+
12+
ALLOCATE(X(N,N), X2(N,N), X4(N,N))
13+
X(:,:) = czero
14+
X2(:,:) = czero
15+
X4(:,:) = czero
16+
17+
do j=1,size(X,2) !n
18+
do i=1,size(X,1) !n
19+
if (abs(i-j) == 1) then
20+
X(i,j)= complex(0.5* sqrt(real(i) + real(j) + 1.0), 0.0)
21+
end if
22+
end do
23+
end do
24+
25+
X2 = matmul(X, X)
26+
X4 = matmul(X2, X2)
27+
28+
H = H + lambda * X4
29+
30+
DEALLOCATE(X, X2, X4)
31+
END subroutine PERTURB

0 commit comments

Comments
 (0)