Ejemplo n.º 1
0
    def __init__(self, gui):
        self.gui = gui
        selected = self.selection()
        if not selected.any():
            ui.error(_('No atoms selected!'))
            return

        win = ui.Window(_('Modify'))
        element = Element(callback=self.set_element)
        win.add(element)
        win.add(ui.Button(_('Change element'),
                          partial(self.set_element, element)))
        self.tag = ui.SpinBox(0, -1000, 1000, 1, self.set_tag)
        win.add([_('Tag'), self.tag])
        self.magmom = ui.SpinBox(0.0, -10, 10, 0.1, self.set_magmom)
        win.add([_('Moment'), self.magmom])

        atoms = self.gui.atoms
        Z = atoms.numbers
        if Z.ptp() == 0:
            element.Z = Z[0]

        tags = atoms.get_tags()[selected]
        if tags.ptp() == 0:
            self.tag.value = tags[0]

        magmoms = get_magmoms(atoms)[selected]
        if magmoms.round(2).ptp() == 0.0:
            self.magmom.value = round(magmoms[0], 2)
Ejemplo n.º 2
0
    def __init__(self, gui):
        self.gui = gui
        selected = self.selection()
        if not selected.any():
            ui.error(_('No atoms selected!'))
            return

        win = ui.Window(_('Modify'))
        element = Element(callback=self.set_element)
        win.add(element)
        win.add(
            ui.Button(_('Change element'), partial(self.set_element, element)))
        self.tag = ui.SpinBox(0, -1000, 1000, 1, self.set_tag)
        win.add([_('Tag'), self.tag])
        self.magmom = ui.SpinBox(0.0, -10, 10, 0.1, self.set_magmom)
        win.add([_('Moment'), self.magmom])

        atoms = self.gui.atoms
        Z = atoms.numbers
        if Z.ptp() == 0:
            element.Z = Z[0]

        tags = atoms.get_tags()[selected]
        if tags.ptp() == 0:
            self.tag.value = tags[0]

        magmoms = get_magmoms(atoms)[selected]
        if magmoms.round(2).ptp() == 0.0:
            self.magmom.value = round(magmoms[0], 2)
Ejemplo n.º 3
0
    def status(self, atoms):
        # use where here:  XXX
        natoms = len(atoms)
        indices = np.arange(natoms)[self.images.selected[:natoms]]
        ordered_indices = [
            i for i in self.images.selected_ordered if i < len(atoms)
        ]
        n = len(indices)
        self.nselected = n

        if n == 0:
            self.window.update_status_line('')
            return

        Z = atoms.numbers[indices]
        R = atoms.positions[indices]

        if n == 1:
            tag = atoms.get_tags()[indices[0]]
            text = (u' #%d %s (%s): %.3f Å, %.3f Å, %.3f Å ' %
                    ((indices[0], names[Z[0]], symbols[Z[0]]) + tuple(R[0])))
            text += _(' tag=%(tag)s') % dict(tag=tag)
            magmoms = get_magmoms(self.atoms)
            if magmoms.any():
                # TRANSLATORS: mom refers to magnetic moment
                text += _(' mom={0:1.2f}'.format(magmoms[indices][0]))
            charges = self.atoms.get_initial_charges()
            if charges.any():
                text += _(' q={0:1.2f}'.format(charges[indices][0]))
        elif n == 2:
            D = R[0] - R[1]
            d = sqrt(np.dot(D, D))
            text = u' %s-%s: %.3f Å' % (symbols[Z[0]], symbols[Z[1]], d)
        elif n == 3:
            d = []
            for c in range(3):
                D = R[c] - R[(c + 1) % 3]
                d.append(np.dot(D, D))
            a = []
            for c in range(3):
                t1 = 0.5 * (d[c] + d[(c + 1) % 3] - d[(c + 2) % 3])
                t2 = sqrt(d[c] * d[(c + 1) % 3])
                try:
                    t3 = acos(t1 / t2)
                except ValueError:
                    if t1 > 0:
                        t3 = 0
                    else:
                        t3 = pi
                a.append(t3 * 180 / pi)
            text = (u' %s-%s-%s: %.1f°, %.1f°, %.1f°' %
                    tuple([symbols[z] for z in Z] + a))
        elif len(ordered_indices) == 4:
            angle = self.atoms.get_dihedral(*ordered_indices, mic=True)
            text = (u'%s %s → %s → %s → %s: %.1f°' %
                    tuple([_('dihedral')] + [symbols[z] for z in Z] + [angle]))
        else:
            text = ' ' + formula(Z)

        self.window.update_status_line(text)
Ejemplo n.º 4
0
 def get_color_scalars(self, frame=None):
     if self.colormode == 'tag':
         return self.atoms.get_tags()
     if self.colormode == 'force':
         f = (self.get_forces()**2).sum(1)**0.5
         return f * self.images.get_dynamic(self.atoms)
     elif self.colormode == 'velocity':
         return (self.atoms.get_velocities()**2).sum(1)**0.5
     elif self.colormode == 'initial charge':
         return self.atoms.get_initial_charges()
     elif self.colormode == 'magmom':
         return get_magmoms(self.atoms)
     elif self.colormode == 'neighbors':
         from ase.neighborlist import NeighborList
         n = len(self.atoms)
         nl = NeighborList(self.get_covalent_radii(self.atoms) * 1.5,
                           skin=0,
                           self_interaction=False,
                           bothways=True)
         nl.update(self.atoms)
         return [len(nl.get_neighbors(i)[0]) for i in range(n)]
     else:
         scalars = np.array(self.atoms.get_array(self.colormode),
                            dtype=float)
         return np.ma.array(scalars, mask=np.isnan(scalars))
Ejemplo n.º 5
0
 def update_labels(self):
     index = self.window['show-labels']
     if index == 0:
         self.labels = None
     elif index == 1:
         self.labels = list(range(len(self.atoms)))
     elif index == 2:
         self.labels = list(get_magmoms(self.atoms))
     else:
         self.labels = self.atoms.get_chemical_symbols()
Ejemplo n.º 6
0
 def get_color_scalars(self, frame=None):
     if self.colormode == 'tag':
         return self.atoms.get_tags()
     if self.colormode == 'force':
         f = (self.get_forces()**2).sum(1)**0.5
         return f * self.images.get_dynamic(self.atoms)
     elif self.colormode == 'velocity':
         return (self.atoms.get_velocities()**2).sum(1)**0.5
     elif self.colormode == 'charge':
         return self.atoms.get_charges()
     elif self.colormode == 'magmom':
         return get_magmoms(self.atoms)
Ejemplo n.º 7
0
 def get_color_scalars(self, frame=None):
     if self.colormode == 'tag':
         return self.atoms.get_tags()
     if self.colormode == 'force':
         f = (self.get_forces()**2).sum(1)**0.5
         return f * self.images.get_dynamic(self.atoms)
     elif self.colormode == 'velocity':
         return (self.atoms.get_velocities()**2).sum(1)**0.5
     elif self.colormode == 'initial charge':
         return self.atoms.get_initial_charges()
     elif self.colormode == 'magmom':
         return get_magmoms(self.atoms)
Ejemplo n.º 8
0
    def activate(self):
        images = self.gui.images
        atoms = self.gui.atoms
        radio = self.radio
        radio['tag'].active = atoms.has('tags')

        # XXX not sure how to deal with some images having forces,
        # and other images not.  Same goes for below quantities
        F = images.get_forces(atoms)
        radio['force'].active = F is not None
        radio['velocity'].active = atoms.has('momenta')
        radio['initial charge'].active = atoms.has('initial_charges')
        radio['magmom'].active = get_magmoms(atoms).any()
Ejemplo n.º 9
0
Archivo: view.py Proyecto: btodac/ase
 def update_labels(self):
     index = self.window['show-labels']
     if index == 0:
         self.labels = None
     elif index == 1:
         self.labels = list(range(len(self.atoms)))
     elif index == 2:
         self.labels = list(get_magmoms(self.atoms))
     elif index == 4:
         Q = self.atoms.get_initial_charges()
         self.labels = ['{0:.4g}'.format(q) for q in Q]
     else:
         self.labels = self.atoms.get_chemical_symbols()
Ejemplo n.º 10
0
    def activate(self):
        images = self.gui.images
        atoms = self.gui.atoms
        radio = self.radio
        radio['tag'].active = atoms.has('tags')

        # XXX not sure how to deal with some images having forces,
        # and other images not.  Same goes for below quantities
        F = images.get_forces(atoms)
        radio['force'].active = F is not None
        radio['velocity'].active = atoms.has('momenta')
        radio['charge'].active = atoms.has('charges')
        radio['magmom'].active = get_magmoms(atoms).any()
Ejemplo n.º 11
0
 def get_color_scalars(self, frame=None):
     if self.colormode == 'tag':
         return self.atoms.get_tags()
     if self.colormode == 'force':
         f = (self.get_forces()**2).sum(1)**0.5
         return f * self.images.get_dynamic(self.atoms)
     elif self.colormode == 'atomic_energies':  ## ssrokyz start
         return self.atoms.get_atomic_energies()  ## ssrokyz end
     elif self.colormode == 'velocity':
         return (self.atoms.get_velocities()**2).sum(1)**0.5
     elif self.colormode == 'initial charge':
         return self.atoms.get_initial_charges()
     elif self.colormode == 'magmom':
         return get_magmoms(self.atoms)
     elif self.colormode == 'neighbors':
         from ase.neighborlist import NeighborList
         n = len(self.atoms)
         nl = NeighborList(self.get_covalent_radii(self.atoms) * 1.5,
                           skin=0,
                           self_interaction=False,
                           bothways=True)
         nl.update(self.atoms)
         return [len(nl.get_neighbors(i)[0]) for i in range(n)]
Ejemplo n.º 12
0
 def set_magmom(self):
     magmoms = get_magmoms(self.gui.atoms)
     magmoms[self.selection()] = self.magmom.value
     self.gui.atoms.set_initial_magnetic_moments(magmoms)
     self.gui.draw()
Ejemplo n.º 13
0
 def set_magmom(self):
     magmoms = get_magmoms(self.gui.atoms)
     magmoms[self.selection()] = self.magmom.value
     self.gui.atoms.set_initial_magnetic_moments(magmoms)
     self.gui.draw()
Ejemplo n.º 14
0
    def status(self, atoms):
        # use where here:  XXX
        natoms = len(atoms)
        indices = np.arange(natoms)[self.images.selected[:natoms]]
        ordered_indices = [i for i in self.images.selected_ordered
                           if i < len(atoms)]
        n = len(indices)
        self.nselected = n

        if n == 0:
            self.window.update_status_line('')
            return

        Z = atoms.numbers[indices]
        R = atoms.positions[indices]

        if n == 1:
            tag = atoms.get_tags()[indices[0]]
            text = (u' #%d %s (%s): %.3f Å, %.3f Å, %.3f Å ' %
                    ((indices[0], names[Z[0]], symbols[Z[0]]) + tuple(R[0])))
            text += _(' tag=%(tag)s') % dict(tag=tag)
            magmoms = get_magmoms(self.atoms)
            if magmoms.any():
                # TRANSLATORS: mom refers to magnetic moment
                text += _(' mom={0:1.2f}'.format(
                    magmoms[indices][0]))
            charges = self.atoms.get_initial_charges()
            if charges.any():
                text += _(' q={0:1.2f}'.format(
                    charges[indices][0]))
        elif n == 2:
            D = R[0] - R[1]
            d = sqrt(np.dot(D, D))
            text = u' %s-%s: %.3f Å' % (symbols[Z[0]], symbols[Z[1]], d)
        elif n == 3:
            d = []
            for c in range(3):
                D = R[c] - R[(c + 1) % 3]
                d.append(np.dot(D, D))
            a = []
            for c in range(3):
                t1 = 0.5 * (d[c] + d[(c + 1) % 3] - d[(c + 2) % 3])
                t2 = sqrt(d[c] * d[(c + 1) % 3])
                try:
                    t3 = acos(t1 / t2)
                except ValueError:
                    if t1 > 0:
                        t3 = 0
                    else:
                        t3 = pi
                a.append(t3 * 180 / pi)
            text = (u' %s-%s-%s: %.1f°, %.1f°, %.1f°' %
                    tuple([symbols[z] for z in Z] + a))
        elif len(ordered_indices) == 4:
            R = self.atoms.positions[ordered_indices]
            Z = self.atoms.numbers[ordered_indices]
            a = R[1] - R[0]
            b = R[2] - R[1]
            c = R[3] - R[2]
            bxa = np.cross(b, a)
            bxa /= np.sqrt(np.vdot(bxa, bxa))
            cxb = np.cross(c, b)
            cxb /= np.sqrt(np.vdot(cxb, cxb))
            angle = np.vdot(bxa, cxb)
            if angle < -1:
                angle = -1
            if angle > 1:
                angle = 1
            angle = np.arccos(angle)
            if (np.vdot(bxa, c)) > 0:
                angle = 2 * np.pi - angle
            angle = angle * 180.0 / np.pi
            text = (u'%s %s->%s->%s->%s: %.1f°' %
                    tuple([_('dihedral')] + [symbols[z] for z in Z] + [angle]))
        else:
            text = ' ' + formula(Z)

        self.window.update_status_line(text)
Ejemplo n.º 15
0
def create_info_lines(atoms, indices, ordered_indices=None):
    """Create a string of information about the selected atom(s).

    Return
    ------
    list[str]

    """
    ordered_indices = ordered_indices or indices
    num_selected = len(indices)

    if num_selected == 0:
        return []

    a_nums = atoms.numbers[indices]
    symbols = [chemical_symbols[z] for z in a_nums]
    positions = atoms.positions[indices]

    if num_selected == 1:
        x, y, z = positions[0]
        name = atomic_names[a_nums[0]]
        text = [
            f"#{indices[0]} {name} ({symbols[0]})",
            f"({x:.3f}, {y:.3f}, {z:.3f}) Å",
            _(f"tag={atoms.get_tags()[indices[0]]}"),
        ]
        magmoms = get_magmoms(atoms)
        if magmoms.any():
            text.append(_(f"mom={magmoms[indices][0]:1.2f}"))
        charges = atoms.get_initial_charges()
        if charges.any():
            text.append(_(f"q={charges[indices][0]:1.2f}"))
        known_arrays = [
            "numbers",
            "positions",
            "forces",
            "momenta",
            "initial_charges",
            "initial_magmoms",
        ]
        for key in atoms.arrays:
            if key not in known_arrays:
                val = atoms.get_array(key)[indices[0]]
                if val is not None:
                    try:
                        text.append("{0}={1:g}".format(key, val))
                    except ValueError:
                        text.append("{0}={1}".format(key, val))

        return text

    if num_selected == 2:
        dist = np.linalg.norm(positions[0] - positions[1])
        return [f"Bond Distance {symbols[0]}-{symbols[0]}: {dist:.3f} Å"]

    if num_selected == 3:
        distances = []
        for c in range(3):
            vector = positions[c] - positions[(c + 1) % 3]
            distances.append(np.dot(vector, vector))
        angles = []
        for c in range(3):
            t1 = 0.5 * (distances[c] + distances[(c + 1) % 3] -
                        distances[(c + 2) % 3])
            t2 = sqrt(distances[c] * distances[(c + 1) % 3])
            try:
                t3 = acos(t1 / t2)
            except ValueError:
                if t1 > 0:
                    t3 = 0
                else:
                    t3 = pi
            angles.append(t3 * 180 / pi)
        return [
            "Valence Angle %s-%s-%s: %.1f°, %.1f°, %.1f°" %
            tuple(symbols + angles)
        ]

    if len(ordered_indices) == 4:
        angle = atoms.get_dihedral(*ordered_indices, mic=True)
        return [
            "%s %s → %s → %s → %s: %.1f°" %
            tuple([_("Dihedral")] + symbols + [angle])
        ]

    return [f"Formula: {create_formula(a_nums)}"]