aiidalab / aiidalab-widgets-base

Reusable widgets for AiiDAlab applications
MIT License
6 stars 17 forks source link

Bonds are wrong #575

Closed superstar54 closed 2 months ago

superstar54 commented 4 months ago

From @cpignedoli . Steps to reproduce. In QE App, select the “Silicon (diamond)” example structure, and click on “convert to primitive cell”. At every click, it completely changes the bonds, and they are wrong: see two screenshots below. Screenshot from 2024-03-05 12-32-07

superstar54 commented 4 months ago

After spent 2 hour debug. :sob: , I found it could be a bug in the nglveiw. Here is an example:

from ase import Atoms
import nglview
atoms = Atoms(
    ["Si", "Si"],
    cell = [[0.0, 2.69, 2.69], 
            [2.69, 0.0, 2.69],
            [2.69, 2.69, 0.0],
           ],
    positions = [[2.69, 2.69, 2.69], [4.04, 4.04, 4.04]],
    pbc = [True, True, True],
)
view = nglview.NGLWidget()
view.add_component(
                    nglview.ASEStructure(atoms),
                    default_representation=True,
                    name="Structure",
                )
view.add_unitcell()
cell = atoms.cell
print("cell: ", cell)
shapes = (
# axis x, y, z
("cylinder",tuple((0, 0, 0)),tuple([5, 0, 0]),tuple((1, 0, 0,)), 0.1),
("cylinder",tuple((0, 0, 0)),tuple([0, 5, 0]),tuple((0, 1, 0,)), 0.1),
("cylinder",tuple((0, 0, 0)),tuple([0, 0, 5]),tuple((0, 0, 1,)), 0.1),
("cylinder",tuple((0, 0, 0)),tuple(cell[0] ),tuple((1, 0, 0,)), 0.2),
("cylinder",tuple((0, 0, 0)),tuple(cell[1] ),tuple((0, 0, 0,)), 0.2),
("cylinder",tuple((0, 0, 0)),tuple(cell[2] ),tuple((0, 0, 0,)), 0.2),
)
view._add_shape(shapes, name = "axis")
view

In nglview, it draw the a-aixs, which align with the x-axis (small, long, red color). However, the a-axis (cell[0]=[0.0, 2.69, 2.69]) should in the y-z plane, as shown by (big, short, red color) cylinder. Thus, there is a mismatch between, the default nglview representation and the shapes added by user. Thus, when we draw bonds manually, the position of bonds are wrong in this case.

Screenshot from 2024-03-05 12-24-50

Should we draw everything (ball, cell, bonds) manually? @yakutovicha @cpignedoli Any idea how to solve this problem?