Skip to content

Commit

Permalink
Add misc. helpers for nbmolviz
Browse files Browse the repository at this point in the history
  • Loading branch information
avirshup committed Jul 29, 2017
1 parent a558b63 commit fb9b268
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion moldesign/helpers/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def not_installed_method(*args, **kwargs):


try:
from nbmolviz.uibase import Logger, display_log
from nbmolviz.uielements import Logger, display_log
exports_names('Logger', 'display_log')

except ImportError:
Expand Down
32 changes: 32 additions & 0 deletions moldesign/molecules/bonds.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from . import toplevel
from .. import units as u
from .. import mathutils
from .. import geom


@toplevel
Expand Down Expand Up @@ -95,6 +96,12 @@ def order(self, order):
if atom.molecule is self.molecule:
atom.bond_graph[nbr] = order

@property
def exists(self):
""" bool: whether or not this bond exists
"""
return self.order is not None

@property
def type(self):
return self.a1.symbol + self.SYMBOLS.get(self.order, u'?̶') + self.a2.symbol
Expand Down Expand Up @@ -129,6 +136,11 @@ def partner(self, atom):
def length(self):
return self.a1.distance(self.a2)

@length.setter
def length(self, value):
geom.set_distance(self.a1, self.a2, value,
adjustmol=self.exists and not self.is_cyclic)

@property
def name(self):
""" str: name of the bond """
Expand All @@ -151,6 +163,26 @@ def molecule(self):
def midpoint(self):
return (self.a1.position + self.a2.position) / 2.0

@property
def is_cyclic(self):
"""
bool: True if this bond is in one or more rings
"""
visited = set([self.a2])

def check_for_cycles(atom):
visited.add(atom)
for nbr in atom.bond_graph:
if nbr is self.a2 and atom is not self.a1:
return True
if nbr not in visited:
in_cycle = check_for_cycles(nbr)
if in_cycle:
return True
return False # if here, not in a cycle
check_for_cycles(self.a1)


def align(self, other, centered=True):
""" Rotates the entire molecule to align this bond with another object.
Expand Down
8 changes: 6 additions & 2 deletions moldesign/orbitals/wfn.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import numpy as np
import copy

from . import MolecularOrbitals
from . import Orbital, MolecularOrbitals
from ..utils import DotDict


Expand Down Expand Up @@ -73,8 +73,12 @@ def __init__(self, mol, num_electrons,
self.positions = positions.copy()

if self.aobasis is not None:
self.orbitals['atomic'] = self.aobasis
self.aobasis.wfn = self
atomic_orbs = [Orbital(c, basis=self.aobasis, wfn=self, name=bf.name)
for c,bf in zip(np.identity(len(self.aobasis)), self.aobasis)]
self.orbitals.atomic = MolecularOrbitals(atomic_orbs, wfn=self,
basis=self.aobasis,
orbtype='atomic')
for orb in self.aobasis.orbitals:
orb.wfn = self

Expand Down
5 changes: 4 additions & 1 deletion moldesign/units/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ def __deepcopy__(self, memo):
return result

def __hash__(self):
return hash((self._magnitude, str(self.units)))
m = self._magnitude
if isinstance(m, np.ndarray) and m.shape == ():
m = float(m)
return hash((m, str(self.units)))

def __setitem__(self, key, value):
from . import array as quantityarray
Expand Down

0 comments on commit fb9b268

Please sign in to comment.