@@ -3,66 +3,66 @@ using NDTensors: data, Block, blockoffsets, BlockOffsets, BlockSparse
33
44# Helper function for HDF5 write/read of BlockSparse
55function offsets_to_array (boff:: BlockOffsets{N} ) where {N}
6- nblocks = length (boff)
7- asize = (N + 1 ) * nblocks
8- n = 1
9- a = Vector {Int} (undef, asize)
10- for bo in pairs (boff)
11- for j in 1 : N
12- a[n] = bo[1 ][j]
13- n += 1
6+ nblocks = length (boff)
7+ asize = (N + 1 ) * nblocks
8+ n = 1
9+ a = Vector {Int} (undef, asize)
10+ for bo in pairs (boff)
11+ for j in 1 : N
12+ a[n] = bo[1 ][j]
13+ n += 1
14+ end
15+ a[n] = bo[2 ]
16+ n += 1
1417 end
15- a[n] = bo[2 ]
16- n += 1
17- end
18- return a
18+ return a
1919end
2020
2121# Helper function for HDF5 write/read of BlockSparse
2222function array_to_offsets (a, N:: Int )
23- asize = length (a)
24- nblocks = div (asize, N + 1 )
25- boff = BlockOffsets {N} ()
26- j = 0
27- for b in 1 : nblocks
28- insert! (boff, Block (ntuple (i -> (a[j + i]), N)), a[j + N + 1 ])
29- j += (N + 1 )
30- end
31- return boff
23+ asize = length (a)
24+ nblocks = div (asize, N + 1 )
25+ boff = BlockOffsets {N} ()
26+ j = 0
27+ for b in 1 : nblocks
28+ insert! (boff, Block (ntuple (i -> (a[j + i]), N)), a[j + N + 1 ])
29+ j += (N + 1 )
30+ end
31+ return boff
3232end
3333
34- function HDF5. write (parent:: Union{HDF5.File,HDF5.Group} , name:: String , B:: BlockSparse )
35- g = create_group (parent, name)
36- attributes (g)[" type" ] = " BlockSparse{$(eltype (B)) }"
37- attributes (g)[" version" ] = 1
38- if eltype (B) != Nothing
39- write (g, " ndims" , ndims (B))
40- write (g, " data" , data (B))
41- off_array = offsets_to_array (blockoffsets (B))
42- write (g, " offsets" , off_array)
43- end
34+ function HDF5. write (parent:: Union{HDF5.File, HDF5.Group} , name:: String , B:: BlockSparse )
35+ g = create_group (parent, name)
36+ attributes (g)[" type" ] = " BlockSparse{$(eltype (B)) }"
37+ attributes (g)[" version" ] = 1
38+ return if eltype (B) != Nothing
39+ write (g, " ndims" , ndims (B))
40+ write (g, " data" , data (B))
41+ off_array = offsets_to_array (blockoffsets (B))
42+ write (g, " offsets" , off_array)
43+ end
4444end
4545
4646function HDF5. read (
47- parent:: Union{HDF5.File,HDF5.Group} , name:: AbstractString , :: Type{Store}
48- ) where {Store<: BlockSparse }
49- g = open_group (parent, name)
50- ElT = eltype (Store)
51- typestr = " BlockSparse{$ElT }"
52- if read (attributes (g)[" type" ]) != typestr
53- error (" HDF5 group or file does not contain $typestr data" )
54- end
55- N = read (g, " ndims" )
56- off_array = read (g, " offsets" )
57- boff = array_to_offsets (off_array, N)
58- # Attribute __complex__ is attached to the "data" dataset
59- # by the h5 library used by C++ version of ITensor:
60- if haskey (attributes (g[" data" ]), " __complex__" )
61- M = read (g, " data" )
62- nelt = size (M, 1 ) * size (M, 2 )
63- data = Vector (reinterpret (ComplexF64, reshape (M, nelt)))
64- else
65- data = read (g, " data" )
66- end
67- return BlockSparse (data, boff)
47+ parent:: Union{HDF5.File, HDF5.Group} , name:: AbstractString , :: Type{Store}
48+ ) where {Store <: BlockSparse }
49+ g = open_group (parent, name)
50+ ElT = eltype (Store)
51+ typestr = " BlockSparse{$ElT }"
52+ if read (attributes (g)[" type" ]) != typestr
53+ error (" HDF5 group or file does not contain $typestr data" )
54+ end
55+ N = read (g, " ndims" )
56+ off_array = read (g, " offsets" )
57+ boff = array_to_offsets (off_array, N)
58+ # Attribute __complex__ is attached to the "data" dataset
59+ # by the h5 library used by C++ version of ITensor:
60+ if haskey (attributes (g[" data" ]), " __complex__" )
61+ M = read (g, " data" )
62+ nelt = size (M, 1 ) * size (M, 2 )
63+ data = Vector (reinterpret (ComplexF64, reshape (M, nelt)))
64+ else
65+ data = read (g, " data" )
66+ end
67+ return BlockSparse (data, boff)
6868end
0 commit comments