Skip to content

Commit

Permalink
Merge pull request #93 from numericalEFT/remove_triqs
Browse files Browse the repository at this point in the history
move TRIQS interface to an independent package NEFTInterface.jl
  • Loading branch information
fsxbhyy authored Oct 17, 2022
2 parents a67d608 + 84a8310 commit 24b91eb
Show file tree
Hide file tree
Showing 18 changed files with 8 additions and 1,846 deletions.
10 changes: 1 addition & 9 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
env:
JULIA_PYTHONCALL_EXE: "/usr/bin/python3"
# test for PythonCall needs python, solution from https://github.com/cjdoris/PythonCall.jl/issues/120
strategy:
fail-fast: false
matrix:
Expand All @@ -25,10 +22,9 @@ jobs:
- "nightly"
os:
- ubuntu-latest
# python-version: ["3.7"]
arch:
- x64
# - x86
- x86
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
Expand All @@ -46,10 +42,6 @@ jobs:
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
- name: Install triqs
run: sudo apt-get update && sudo apt-get install -y software-properties-common apt-transport-https curl && source /etc/lsb-release && curl -L https://users.flatironinstitute.org/~ccq/triqs3/$DISTRIB_CODENAME/public.gpg | sudo apt-key add - && sudo add-apt-repository "deb https://users.flatironinstitute.org/~ccq/triqs3/$DISTRIB_CODENAME/ /" && sudo apt-get update && sudo apt-get install -y triqs
- name: Check triqs
run: python3 -c "import triqs.version; print(triqs.version.version)"
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v2
Expand Down
8 changes: 1 addition & 7 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,17 @@ version = "0.2.0"
BrillouinZoneMeshes = "9f696214-9961-49f3-89a2-cb1ea204eb6e"
CompositeGrids = "b5136c89-beeb-4521-9139-60d2cac8be56"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Lehmann = "95bf888a-8996-4655-9f35-1c0506bdfefe"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
BrillouinZoneMeshes = "0.1"
CompositeGrids = "0.1"
FileIO = "1"
JLD2 = "0.4"
Lehmann = "0.2"
StaticArrays = "1"
BrillouinZoneMeshes = "0.1"
PythonCall = "0.9"
julia = "1.6, 1.7, 1.8"

[extras]
Expand Down
129 changes: 1 addition & 128 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,131 +130,4 @@ The imaginary-time Green's function after the Fourier transform shoud be consist

TRIQS (Toolbox for Research on Interacting Quantum Systems) is a scientific project providing a set of C++ and Python libraries for the study of interacting quantum systems. We provide a direct interface to convert TRIQS objects, such as the temporal meshes, the Brillouin zone meshes, and the multi-dimensional (blocked) Green's functions, to the equivalent objects in our package. It would help TRIQS users to make use of our package without worrying about the different internal data structures.

We rely on the package [`PythonCall.jl`](https://github.com/cjdoris/PythonCall.jl) to interface with the python language. You need to install TRIQS package from the python environment that `PythonCall` calls. We recommand you to check the sections [`Configuration`](https://cjdoris.github.io/PythonCall.jl/stable/pythoncall/#pythoncall-config) and [`Installing Python Package`](https://cjdoris.github.io/PythonCall.jl/stable/pythoncall/#python-deps) in the `PythonCall` documentation.

### Example 5: Load Triqs Temporal Mesh
First we show how to import an imaginary-time mesh from TRIQS.
```julia
using PythonCall, GreenFunc
gf = pyimport("triqs.gf")
np = pyimport("numpy")

mt = gf.MeshImTime(beta=1.0, S="Fermion", n_max=3)
mjt = from_triqs(mt)
for (i, x) in enumerate([p for p in mt.values()])
@assert mjt[i] pyconvert(Float64, x) # make sure mjt is what we want
end

```
- With the `PythonCall` package, one can import python packages with `pyimport` and directly exert python code in Julia. Here we import the Green's function module `triqs.gf` and generate a uniform imaginary-time mesh with `MeshImTime`. The user has to specify the inverse temperature, whether the particle is fermion or boson, and the number of grid points.

- Once a TRIQS object is prepared, one can simply convert it to an equivalent object in our package with `from_triqs`. The object can be a mesh, a Green's function, or a block Green's function. In this example, the TRIQS imaginary time grid is converted to an identical `ImTime` grid.

### Example 6: Load Triqs BrillouinZone

In this example, we show how the Brillouin zone mesh from TRIQS can be converted to a UniformMesh from the `BrillouinZoneMeshes` package and clarify the convention we adopted to convert a Python data structure to its Julia counterpart.

```julia
using PythonCall, GreenFunc

# construct triqs Brillouin zone mesh
lat = pyimport("triqs.lattice")
gf = pyimport("triqs.gf")
BL = lat.BravaisLattice(units=((2, 0, 0), (1, sqrt(3), 0)))
BZ = lat.BrillouinZone(BL)
nk = 4
mk = gf.MeshBrillouinZone(BZ, nk)

# load Triqs mesh and construct
mkj = from_triqs(mk)

for p in mk
pval = pyconvert(Array, p.value)
# notice that TRIQS always return a 3D point, even for 2D case(where z is always 0)
# notice also that Julia index starts from 1 while Python from 0
# points of the same linear index has the same value
ilin = pyconvert(Int, p.linear_index) + 1
@assert pval[1:2] mkj[ilin]
# points with the same linear index corresponds to REVERSED cartesian index
inds = pyconvert(Array, p.index)[1:2] .+ 1
@assert pval[1:2] mkj[reverse(inds)...]
end
```

- Julia uses column-major layout for multi-dimensional array similar as Fortran and matlab, whereas python uses row-major layout. The converted Julias Brillouin zone mesh wll be indexed differently from that in TRIQS.
- We adopted the convention so that the grid point and linear index are consistent with TRIQS counterparts, while the orders of Cartesian index
and lattice vector are reversed.
- Here's a table of 2D converted mesh v.s. the Triqs counterpart:

| Object | TRIQS | GreenFunc.jl |
| --------------- | ----------------- | -------------- |
| Linear index | mk[i]=(x, y, 0) | mkj[i]= (x, y) |
| Cartesian index | mk[i,j]=(x, y, 0) | mkj[j,i]=(x,y) |
| Lattice vector | (a1, a2) | (a2, a1) |

### Example 7: Load Triqs Greens function of a Hubbard Lattice

A TRIQS Green's function is defined on a set of meshes of continuous variables, together with the discrete inner states specified by the `target_shape`. The structure is immediately representable by `MeshArray`. In the following example, we reimplement the example 3 to first show how to generate a TRIQS Green's function of a Hubbard lattice within Julia, then convert the TRIQS Green's function to a julia `MeshArray` object. The Green's function is given by $G(q, \omega_n) = \frac{1}{i\omega_n - \epsilon_q}$ with $\epsilon_q = -2t(\cos(q_x)+\cos(q_y))$.

```julia
using PythonCall, GreenFunc

np = pyimport("numpy")
lat = pyimport("triqs.lattice")
gf = pyimport("triqs.gf")

BL = lat.BravaisLattice(units=((2, 0, 0), (1, sqrt(3), 0))) # testing with a triangular lattice so that exchanged index makes a difference
BZ = lat.BrillouinZone(BL)
nk = 20
mk = gf.MeshBrillouinZone(BZ, nk)
miw = gf.MeshImFreq(beta=1.0, S="Fermion", n_max=100)
mprod = gf.MeshProduct(mk, miw)

G_w = gf.GfImFreq(mesh=miw, target_shape=[1, 1]) #G_w.data.shape will be [201, 1, 1]
G_k_w = gf.GfImFreq(mesh=mprod, target_shape = [2, 3] ) #target_shape = [2, 3] --> innerstate = [3, 2]

# Due to different cartesian index convention in Julia and Python, the data g_k_w[n, m, iw, ik] corresponds to G_k_w.data[ik-1, iw-1, m-1, n-1])

t = 1.0
for (ik, k) in enumerate(G_k_w.mesh[0])
G_w << gf.inverse(gf.iOmega_n - 2 * t * (np.cos(k[0]) + np.cos(k[1])))
G_k_w.data[ik-1, pyslice(0, nk^2), pyslice(0, G_k_w.target_shape[0]) , pyslice(0,G_k_w.target_shape[1])] = G_w.data[pyslice(0, nk^2), pyslice(0, G_w.target_shape[0]) , pyslice(0,G_w.target_shape[1])] #pyslice = :
end

g_k_w = from_triqs(G_k_w)

#alternatively, you can use the MeshArray constructor to convert TRIQS Green's function to a MeshArray
g_k_w2 = MeshArray(G_k_w)
@assert g_k_w2 g_k_w

#Use the << operator to import python data into an existing MeshArray
g_k_w2 << G_k_w
@assert g_k_w2 g_k_w

```
- When converting a TRIQS Green's function into a `MeshArray` julia object, the `MeshProduct` from TRIQS is decomposed into separate meshes and converted to the corresponding Julia meshes. The `MeshArray` stores the meshes as a tuple object, not as a `MeshProduct`.
- The `target_shape` in TRIQS Green's function is converted to a tuple of `UnitRange{Int64}` objects that represents the discrete degrees of freedom. Data slicing with `:` is not available in `PythonCall`. One needs to use `pyslice` instead.
- As explained in Example 6, the cartesian index order of data has to be inversed during the conversion.
- We support three different interfaces for the conversion of TRIQS Green's function. One can construct a new MeshArray with `from_triqs` or `MeshArray` constructor. One can also load TRIQS Green's function into an existing `MeshArray` with the `<<` operator.

### Example 8: Load Triqs block Greens function

The block Greens function in TRIQS can be converted to a dictionary of `MeshArray` objects in julia.

```julia
using PythonCall, GreenFunc

gf = pyimport("triqs.gf")
np = pyimport("numpy")
mt = gf.MeshImTime(beta=1.0, S="Fermion", n_max=3)
lj = pyconvert(Int, @py len(mt))
G_t = gf.GfImTime(mesh=mt, target_shape=[2,3]) #target_shape = [2, 3] --> innerstate = [3, 2]
G_w = gf.GfImTime(mesh=mt, target_shape=[2,3]) #target_shape = [2, 3] --> innerstate = [3, 2]

blockG = gf.BlockGf(name_list=["1", "2"], block_list=[G_t, G_w], make_copies=false)

jblockG = from_triqs(blockG)
#The converted block Green's function is a dictionary of MeshArray corresponding to TRIQS block Green's function. The mapping between them is: jblockG["name"][i1, i2, t] = blockG["name"].data[t-1, i2-1, i1-1]

```

The interface is provided by an independent package [`NEFTInterface.jl`](https://github.com/numericalEFT/NEFTInterface.jl). We provide several examples of interfacing TRIQS and `GreenFunc.jl` in the [`NEFTInterface.jl` README](https://github.com/numericalEFT/NEFTInterface.jl).
97 changes: 0 additions & 97 deletions docs/Manifest.toml

This file was deleted.

8 changes: 3 additions & 5 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ DocMeta.setdocmeta!(GreenFunc, :DocTestSetup, :(using GreenFunc); recursive=true

makedocs(;
modules=[GreenFunc],
authors="Tao Wang, Xiansheng Cai",
repo="https://github.com/fsxbhyy/GreenFunc.jl/blob/{commit}{path}#{line}",
authors="Kun Chen, Tao Wang, Xiansheng Cai, PengCheng Hou, and Zhiyi Li",
repo="https://github.com/numericaleft/GreenFunc.jl/blob/{commit}{path}#{line}",
sitename="GreenFunc.jl",
format=Documenter.HTML(;
prettyurls=get(ENV, "CI", "false") == "true",
canonical="https://fsxbhyy.github.io/GreenFunc.jl",
canonical="https://numericaleft.github.io/GreenFunc.jl",
assets=String[]
),
pages=[
Expand All @@ -19,8 +19,6 @@ makedocs(;
"GreenFunc" => "lib/greenfunc.md",
"MeshArrays" => "lib/mesharrays.md",
"MeshGrids" => "lib/meshgrids.md",
"Triqs" => "lib/triqs.md",
"Deprecated" => "lib/deprecated.md",
]
]
)
Expand Down
Loading

0 comments on commit 24b91eb

Please sign in to comment.