コード例 #1
0
 def add_bonds(self, bonds):
     api.validate_sequence(bonds, tuple, "bonds",
                           ((lambda p: len(p) == 3,
                             "must have length  3"),
                            (lambda p: (isinstance(p[0], str)
                                        or isinstance(p[0], Atom))
                                       and (isinstance(p[1], str)
                                            or isinstance(p[1], Atom)),
                             "elements must be strings or atoms"),
                            (lambda p: p[2] in self._bond_orders,
                             "bond order must be one of "
                             + str(self._bond_orders))))
     for a1, a2, order in bonds:
         atom1 = self._get_atom(a1)
         atom2 = self._get_atom(a2)
         if atom1 is atom2:
             raise ValueError("bond between %s and itself"
                              % str(atom1.full_path()))
         p = self.common_ancestor((atom1, atom2))
         if p is self:
             self._bonds.append((atom1, atom2, order))
         else:
             raise ValueError("bond %s-%s must be defined in fragment %s"
                              % (str(atom1.full_path()),
                                 str(atom2.full_path()),
                                 p._label))
コード例 #2
0
 def add_atoms(self, atoms):
     api.validate_sequence(atoms, Atom, "atoms")
     new_attrs = set()
     for a in atoms:
         self._check_child_obj(a, new_attrs)
     for a in atoms:
         self._add_child_obj(a)
     self._atoms.extend(atoms)
コード例 #3
0
 def add_fragments(self, fragments):
     api.validate_sequence(fragments, Fragment, "fragments")
     new_attrs = set()
     for f in fragments:
         self._check_child_obj(f, new_attrs)
     for f in fragments:
         self._add_child_obj(f)
     self._fragments.extend(fragments)
コード例 #4
0
ファイル: array_model.py プロジェクト: khinsen/mosaic-python
 def __init__(self, universe, name, strings, label_type):
     api.validate_type(universe, Universe, "universe")
     api.validate_label(name, "name")
     api.validate_sequence(strings, str, "strings")
     api.validate_value(label_type, self._allowed_types, "label_type")
     self._universe = universe
     self._name = name
     self._string_array = N.array(list('\0'.join(strings) + '\0'),
                                  dtype='S')
     self._type = label_type
コード例 #5
0
 def __init__(self, universe, name, strings):
     api.validate_type(universe, Universe, "universe")
     self._universe = universe
     api.validate_label(name, "name")
     self._name = name
     api.validate_sequence(strings, str, "strings")
     if len(strings) != self._number_of_values():
         raise ValueError("incorrect number of strings")
     for s in strings:
         api.validate_ascii_string(s, "label")
     self._strings = ImmutableTuple(strings)
コード例 #6
0
 def add_molecules(self, molecules):
     api.validate_sequence(molecules, tuple, "molecules",
                           ((lambda p: len(p) == 2, "must have length 2"),
                            (lambda p: isinstance(p[0], Fragment)
                                       and isinstance(p[1], int),
                             "elements must be (fragment, count) pairs")))
     self._molecules.extend(molecules)
     self._templates.extend([f for f, c in molecules])
     self._molecule_counts.extend([c for f, c in molecules])
     self._atom_counts.extend([f.number_of_atoms for f, c in molecules])
     self._site_counts.extend([f.number_of_sites for f, c in molecules])
     self._bond_counts.extend([f.number_of_bonds for f, c in molecules])
コード例 #7
0
    def __init__(self, args):
        cell_shape, molecules, symmetry_transformations, convention = args
        api.validate_type(cell_shape, str, "cell_shape")
        api.validate_value(cell_shape, list(self._cell_parameter_array_shapes.keys()), "cell_shape")
        api.validate_sequence(
            molecules,
            ImmutableTuple,
            "molecules",
            (
                (lambda p: len(p) == 3, "must have length 3"),
                (
                    lambda p: isinstance(p[0], Fragment) and isascii(p[1]) and isinstance(p[2], int),
                    "elements must be (fragment, label, count) " "triples",
                ),
            ),
        )
        api.validate_sequence(
            symmetry_transformations,
            ImmutableTuple,
            "symmetry_transformations",
            (
                (lambda p: len(p) == 2, "must have length 2"),
                (
                    lambda p: hasattr(p[0], "shape") and p[0].shape == (3, 3) and p[0].dtype == N.float64,
                    "rotation matrix must be float64 " "and have shape (3,3)",
                ),
                (
                    lambda p: hasattr(p[1], "shape") and p[1].shape == (3,) and p[0].dtype == N.float64,
                    "translation vector must be float64 " "and have shape (3,)",
                ),
            ),
        )
        if cell_shape == "infinite" and len(symmetry_transformations) > 0:
            raise ValueError("Symmetry transformations are allowed " "only in periodic universes")
        api.validate_label(convention, "Universe.convention")

        self._cell_shape = cell_shape
        self._molecules = ImmutableTuple(
            ImmutableTuple((FragmentRef(label, fragment), count)) for fragment, label, count in molecules
        )
        self._symmetry_transformations = symmetry_transformations
        self._convention = convention

        self._fragments = ImmutableTuple(ImmutableTuple((f, l)) for f, l, c in molecules)
        self._molecule_counts = ImmutableTuple(c for f, l, c in molecules)
        self._atom_counts = ImmutableTuple(f.number_of_atoms for f, l, c in molecules)
        self._site_counts = ImmutableTuple(f.number_of_sites for f, l, c in molecules)
        self._bond_counts = ImmutableTuple(f.number_of_bonds for f, l, c in molecules)
コード例 #8
0
 def symmetry_transformations(self, symmetry_transformations):
     if self.cell_shape == "infinite" \
        and len(symmetry_transformations) > 0:
         raise ValueError("Symmetry transformations are allowed "
                          "only in periodic universes")
     api.validate_sequence(symmetry_transformations, tuple,
                           "symmetry_transformations",
                           ((lambda p: len(p) == 2, "must have length 2"),
                            (lambda p: hasattr(p[0], 'shape')
                                       and p[0].shape == (3, 3)
                                       and p[0].dtype == N.float64,
                             "rotation matrix must be float64 "
                             "and have shape (3,3)"),
                            (lambda p: hasattr(p[1], 'shape')
                                       and p[1].shape == (3,)
                                       and p[0].dtype == N.float64,
                             "translation vector must be float64 "
                             "and have shape (3,)"),))
     self._symmetry_transformations = symmetry_transformations
コード例 #9
0
    def __init__(self, args):
        species, fragments, atoms, bonds = args
        api.validate_label(species, "species")
        self.species = species

        labels = set()

        api.validate_sequence(fragments, ImmutableTuple, "fragments", ((lambda p: len(p) == 2, "must have length 2"),))
        for label, fragment in fragments:
            api.validate_label(label, "fragment label")
            if label in labels:
                raise ValueError("label %s occurs more than once" % label)
            labels.add(label)
            api.validate_type(fragment, Fragment, "fragment template")
        self.fragments = fragments

        api.validate_sequence(atoms, ImmutableTuple, "atoms", ((lambda p: len(p) == 2, "must have length 2"),))
        for label, atom in atoms:
            api.validate_label(label, "atom label")
            if label in labels:
                raise ValueError("label %s occurs more than once" % label)
            labels.add(label)
            api.validate_type(atom, Atom, "atom")
        self.atoms = atoms

        self.attrs = ImmutableDict(IT.chain(fragments, atoms))

        api.validate_sequence(bonds, tuple, "bonds", ((lambda p: len(p) == 3, "must have length 3"),))
        for a1, a2, order in bonds:
            self._validate_bond_atom(a1)
            self._validate_bond_atom(a2)
            if a1.split(".")[0] == a2.split(".")[0]:
                raise ValueError(
                    "bond between %s and %s must be defined " "in fragment %s" % (a1, a2, a1.split(".")[0])
                )
            api.validate_value(order, api.MosaicFragment._bond_orders, "bond order")
        self.bonds = bonds