TRIQS / triqs

a Toolbox for Research on Interacting Quantum Systems
https://triqs.github.io
GNU General Public License v3.0
139 stars 72 forks source link

BUG: Deep copy of MeshBrillouinZone sets the BrillouniZone unit vectors to the identity matrix #649

Closed HugoStrand closed 5 years ago

HugoStrand commented 5 years ago

Dear all,

There is a silent but dangerous bug in the copy behavior of MeshBrillouinZone (or any object having such a mesh) in python. When making a copy using copy.deepcopy the BrillouinZone unit-vectors are not copied and instead set to the unit matrix...

Below is a test for the problem.

Best, Hugo


import copy
import numpy as np

from pytriqs.gf import MeshBrillouinZone
from pytriqs.lattice import BrillouinZone, BravaisLattice

cell = [
    [1., 0., 2.],
    [0.5, 2.5, 1.],
    [1., 2., 0.]]

bl = BravaisLattice(cell)
bz = BrillouinZone(bl)

periodization_matrix = 32 * np.eye(3, dtype=np.int32)
bzmesh = MeshBrillouinZone(bz, periodization_matrix)

bzmesh_ref = copy.deepcopy(bzmesh) # BREAKS

units = bzmesh.domain.units()
units_ref = bzmesh_ref.domain.units()

np.testing.assert_array_almost_equal(units, units_ref)

This gives the output

AssertionError: 
Arrays are not almost equal to 6 decimals

(mismatch 88.8888888889%)
 x: array([[ 2.513274, -1.256637,  1.884956],
       [-5.026548,  2.513274,  2.513274],
       [ 6.283185, -0.      , -3.141593]])
 y: array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
parcollet commented 5 years ago

Fixed in unstable

parcollet commented 5 years ago

PS : upgrade cpp2py !!