Quick Start

Introduction

In this tutorial, you will get a quick view of weas-widget. To run this tutorial, you need to install weas-widget. Open a terminal and run:

pip install weas-widget

Load structure

One can load a structure from ASE or Pymatgen.

[1]:
from ase.build import molecule
from weas_widget import WeasWidget
atoms = molecule("C2H6SO")
viewer = WeasWidget()
viewer.from_ase(atoms)
viewer

[1]:

Edit the structure with mouse and keyboard

WEAS supports editing the atoms directly in the GUI and synchronizing with the structure of the Python object.

Select Atoms

There are three methods for selecting atoms:

  • Single Selection: Click directly on an atom to select it.

  • Box Selection: Hold the Shift key and drag to select a group of atoms.

  • Lasso Selection: Hold Shift + Alt and drag to select a group of atoms.

Move, Rotate selected atoms

Press the transform shortcut, and move your mouse.

Operation

Shortcut

Move

g

Rotate

r

Duplicate

d

Delete selected atoms

Press the Delete key to delete the selected atoms

Export edited atoms

One can export the edited atoms to ASE or Pymatgen

[2]:
viewer.to_ase()
[2]:
Atoms(symbols='SOC2H6', pbc=False)

Change the style

GUI

On the top left, click open controls. One can change the viewer settings directly in the GUI:

  • model style

    • Ball

    • Ball and Stick

    • Polyhedra

    • Stick only

  • color type

    • CPK

    • VESTA

    • JMOL

  • material type

    • Standard

    • Phong

    • Basic

  • atom label

    • None

    • Atom sybol

    • Atom index

Python

One can use Python change the viewer settings. For example, change atoms style

[3]:
# ball and stick
viewer.avr.model_style = 1

One set the style for individue atom

[4]:
# only draw stick (bond) for first four atoms
viewer.avr.model_sticks = [1, 1, 1, 1, 0, 0, 0, 0]
viewer.avr.draw()

Set scale for each atoms:

[5]:
viewer.avr.atom_scales = [1, 1, 1, 1, 1, 0.6, 0.6, 0.6, 1.5, 1.5]
viewer.avr.draw()

Select atoms

One can get the indices of the selected atoms by:

[6]:
viewer.avr.selected_atoms_indices
[6]:
[]

One can set the indices of the selected atoms, thus highlight them by:

[7]:
viewer.avr.selected_atoms_indices = [0, 1, 2]

Crystal

For a nice visualization of a crystal, one usually shows the polyhedra and the atoms on the unit cell boundary, as well as the bonded atoms outside the cell.

[8]:
from weas_widget import WeasWidget
viewer1 = WeasWidget()
viewer1.load_example("tio2.cif")
# show polyhedra
viewer1.avr.model_style = 2
# show boundary atoms
viewer1.avr.boundary = [[-0.1, 1.1], [-0.1, 1.1], [-0.1, 1.1]]
# show bonded atoms outside the cell
viewer1.avr.show_bonded_atoms = True
# Change color tyoe to "VESTA"
viewer1.avr.color_type = "VESTA"
viewer1
[8]:

Animation

One can read a trajectory file, such as an xyz file, for animation. You can play the animation using timeline bar.

[9]:
from ase.build import molecule
from weas_widget import WeasWidget
atoms = molecule("C2H6SO")
# create a list of atoms by rotating it.
images = []
for i in range(0, 360, 10):
    new_atoms = atoms.copy()
    new_atoms.rotate("z", i)
    images.append(new_atoms)
viewer = WeasWidget()
viewer.from_ase(images)
viewer
[9]:

Isosurface

Here is an example of drawing isosurfaces for HOMO of H2O molecule.

[10]:
from weas_widget import WeasWidget
from ase.io.cube import read_cube_data
volume, atoms = read_cube_data("../../examples/h2o-homo.cube")
viewer = WeasWidget()
viewer.from_ase(atoms)
viewer.avr.iso.volumetric_data = {"values": volume}
viewer.avr.iso.settings = {"positive": {"isovalue": 0.001},
                           "negative": {"isovalue": -0.001, "color": "yellow"},}
viewer
[10]:

Vector Field

Magnetic moment

Show the magnetic moment as a vector field.

[11]:
from ase.build import bulk
from weas_widget import WeasWidget
import numpy as np
atoms = bulk("Fe", cubic=True)
atoms*=[2, 2, 1]
atoms.set_array("moment", np.ones(len(atoms)))
viewer = WeasWidget()
viewer.from_ase(atoms)
viewer.avr.model_style = 1
viewer.camera.setting = {"direction": [0, -1, 0]}
viewer
[11]:

One can add any vector field by:

[12]:
# origin of the vector
origins = atoms.positions
# the vertor
vectors = [[0, 0, 1]]*len(atoms)
viewer.avr.vf.settings["test"]= {"origins": origins, "vectors": vectors, "color": "red"}

Phonon visualization

One can visualize the phonon dispersion via lattice vibrations. One only need to use the eigenstates (calculated with an external software) to generate the trajectory.

[13]:
import numpy as np
from ase.build import bulk
from weas_widget import WeasWidget
atoms = bulk("Fe", cubic=True)
phonon_setting = {"eigenvectors": np.array([[[0, 0], [0, 0],[0.5, 0]],
                                    [[0, 0], [0, 0], [-0.5, 0]]]
                                   ),
        "kpoint": [0, 0, 0], # optional
        "amplitude": 5, # scale the motion of the atoms
        "factor": 1.5, # scale the length of the arrows
        "nframes": 20,
        "repeat": [4, 4, 1],
        "color": "blue",
        "radius": 0.1,
        }
viewer = WeasWidget()
viewer.from_ase(atoms)
viewer.avr.phonon_setting = phonon_setting
viewer.camera.setting = {"direction": [0, -1, 0]}
viewer
[13]:

Color by attribute

Color by attribute is a powerful tool to visualize the data. For example, color the atoms by their index.

[14]:
from ase.build import molecule
from weas_widget import WeasWidget
atoms = molecule("C2H6SO")
viewer = WeasWidget()
viewer.from_ase(atoms)
viewer.avr.model_style = 1
viewer.avr.color_by = "Index"
viewer.avr.color_ramp = ["red", "yellow", "blue"]
viewer
[14]:

Camera setting

One can set the direction and zoom of the camera:

viewer.camera.setting = {"direction": [0, 5, 1], "zoom": 2}

Export image

Save image to a path by:

viewer.save_image("/homg/xing/filename.png")

Trigger the download panel by:

Real world example

Adsorption

[15]:
from ase.build import surface, molecule
from weas_widget import WeasWidget
au111 = surface("Au", (1, 1, 1), 4, vacuum=5.0)
co = molecule("CO")
au111 += co
viewer = WeasWidget()
viewer.from_ase(au111)
viewer
[15]:
[16]:
au111_co = viewer.to_ase()
au111_co.write("au111_co.xyz")

Large system

WEAS can handle thousands of atoms.

[17]:
from ase.build import surface, molecule
from weas_widget import WeasWidget
co = molecule("CO")
co.center(3.0)
co.pbc = [True, True, True]
co *= [10, 10, 10]
print("Number of atoms: ", len(co))
viewer2 = WeasWidget()
viewer2.from_ase(co)
viewer2
Number of atoms:  2000
[17]: