Esempio n. 1
0
    def to_tuple(self, use_hexhash=False):
        self.types_resolved = {}
        keydict = self.types()['keydict']
        params = self.__init__.__code__.co_varnames[1:]

        keys = [self.types()['name']]
        for param in params:
            if param in keydict:
                val = getattr(self, param)
                try:
                    if issubclass(keydict[param], HttkObject):
                        val = keydict[param].use(val)
                except TypeError as e:
                    pass
                if use_hexhash and hasattr(val, 'hexhash'):
                    val = val.hexhash
                elif hasattr(val, 'to_tuple'):
                    val = val.to_tuple()
                elif is_sequence(val):
                    out = []
                    for x in val:
                        if use_hexhash and hasattr(x, 'hexhash'):
                            out += [x.hexhash]
                        elif hasattr(x, 'to_tuple'):
                            out += [x.to_tuple()]
                        else:
                            out += [x]
                    val = tuple(out)
                keys += [(param, val)]

        keys = tuple(keys)
        #print("TUPLE:",keys)
        return keys
Esempio n. 2
0
    def create(cls, assignments=None):
        """
        Create a new assignment object,

       assignments: a list-style object with one entry per 'atom type'. Any sensible type accepted, most notably,
                     integers (for atom number)
        """
        if isinstance(assignments, SiteAssignment):
            return assignments

        if isinstance(assignments, dict):
            if 'assignments' in assignments:
                assignments = assignments['assignments']
            else:
                #raise Exception("Assignments.create: assignments is a dict, but does not contain the necessary key.")
                assignments = [Assignment.create(assignments)]

        newassignments = []
        if is_sequence(assignments):
            for x in assignments:
                if not isinstance(x, dict) and is_sequence(x):
                    for y in x:
                        newassignments += [Assignment.use(y)]
                else:
                    newassignments += [Assignment.use(x)]
        else:
            newassignments = [Assignment.use(assignments)]

        ratiosum = 0
        for a in newassignments:
            ratiosum += a.ratio

        if ratiosum > 1:
            print("Ratiosum:", ratiosum)
            print("Assignments:", assignments)
            raise Exception("siteassignment.create: sum of ratios for one site larger than 1, broken structure.")

        return cls(newassignments)
Esempio n. 3
0
 def coords(results, match):
     results['out'] = True
     idx = ord(match.group(2)) - 65
     if results['group'] != idx:
         add_groupdata(results)
     if match.group(4) == 'alpha':
         results['wyckoff'] += ['&']
     else:
         results['wyckoff'] += [match.group(4)]
     results['group'] = idx
     occus = assignments[idx]
     if is_sequence(occus):
         for i in range(len(occus)):
             occu = occus[i]
             try:
                 ratio = str(occu.ratio.to_float())
             except AttributeError:
                 ratio = str(occu.ratio)
             results['groupdata'].append([
                 occu.symbol + str(i), occu.symbol,
                 match.group(3),
                 match.group(4),
                 match.group(5),
                 match.group(6),
                 match.group(7), ratio
             ])
     else:
         ratio = '1'
         results['groupdata'].append([
             assignments[idx], assignments[idx],
             match.group(3),
             match.group(4),
             match.group(5),
             match.group(6),
             match.group(7), ratio
         ])
Esempio n. 4
0
def periodicity_to_pbc(periodicity):
    if is_sequence(periodicity):
        pbc = [bool(x) for x in periodicity]
    else:
        pbc = [False] * periodicity + [True] * (3 - periodicity)
    return pbc
Esempio n. 5
0
def structure_to_ase_atoms(struct):
    ensure_ase_is_imported()

    struct = Structure.use(struct)

    if struct.has_uc_repr:
        symbollist, scaled_positions = httk.iface.ase_if.uc_structure_to_symbols_and_scaled_positions(
            struct)
        scaled_positions = scaled_positions.to_floats()
        cell = struct.uc_basis.to_floats()
        symbols = []
        for s in symbollist:
            if is_sequence(s):
                if len(s) == 1:
                    symbols += [s[0]]
                else:
                    symbols += [str(s)]
            else:
                symbols += [s]

        atoms = Atoms(symbols=symbols,
                      cell=cell,
                      scaled_positions=scaled_positions,
                      pbc=True)

    elif struct.has_rc_repr:
        symbollist, scaled_positions = httk.iface.ase_if.rc_structure_to_symbols_and_scaled_positions(
            struct)
        symbols = []
        for s in symbollist:
            if is_sequence(s):
                if len(s) == 1:
                    symbols += [s[0]]
                else:
                    symbols += [str(s)]
            else:
                symbols += [s]

        hall = struct.rc_sites.hall_symbol
        symops = get_symops_strs(hall)
        rot, trans = ase.spacegroup.spacegroup.parse_sitesym(symops)
        spgnbr, setting = spacegroup_get_number_and_setting(hall)
        spg = ase.spacegroup.spacegroup.spacegroup_from_data(
            no=spgnbr,
            symbol=hall,
            centrosymmetric=None,
            scaled_primitive_cell=None,
            reciprocal_cell=None,
            subtrans=None,
            sitesym=None,
            rotations=rot,
            translations=trans,
            datafile=None)

        atoms = crystal(symbols,
                        scaled_positions,
                        spg,
                        cellpar=[
                            float(struct.rc_a),
                            float(struct.rc_b),
                            float(struct.rc_c),
                            float(struct.rc_alpha),
                            float(struct.rc_beta),
                            float(struct.rc_gamma)
                        ])
    else:
        raise Exception(
            "ase_glue.structure_to_ase_atoms: structure has neither primcell, nor representative, representation."
        )

    return atoms
Esempio n. 6
0
def structure_to_jmol(iof, struct, extbonds=True, repeat=None, copies=None):
    """
    Converts structure into jmol format.

    Example output format::
        load data 'model'
        1
        Computation1
        Al 0 0 0
        end 'model' { 4 4 4 } supercell "x, y, z " unitcell [
        2.025 2.025 0
        2.025 0 2.025
        0 2.025 2.025
        ]
        set slabByAtom TRUE
        unitcell {1/1 1/1 1/1}
        delete (NOT (unitcell OR connected(unitcell)))
        {connected(unitcell) AND NOT unitcell}.radius = 0
        restrict cell={2 2 2}
        center visible
        zoom 0
    """

    if repeat is None:
        supercell = "supercell \"x, y, z\""
    else:
        supercell = "supercell \"" + str(int(repeat[0])) + "x, " + str(
            int(repeat[1])) + "y, " + str(int(repeat[2])) + "z \""

    if copies is None:
        copies = "{ 1 1 1 }"
    else:
        copies = "{ " + str(int(copies[0])) + " " + str(int(
            copies[1])) + " " + str(int(copies[2])) + " }"

    iof = httk.IoAdapterFileWriter.use(iof)
    f = iof.file

    nl = "|"

    if struct.has_rc_repr:
        basis = struct.rc_basis.to_floats()
        coords = struct.rc_cartesian_coords.to_floats()
        symbollist = struct.rc_occupationssymbols
        spacegroup = struct.rc_sites.hall_symbol
    elif struct.has_uc_repr:
        basis = struct.uc_basis.to_floats()
        coords = struct.uc_cartesian_coords.to_floats()
        symbollist = struct.uc_occupationssymbols
        spacegroup = 'P 1'
    else:
        raise Exception(
            "httk.jmol_if.structure_to_jmol: structure has neither representative nor primcell representation?"
        )

    symbols = []
    for s in symbollist:
        if is_sequence(s):
            if len(s) == 1:
                symbols += [s[0]]
            else:
                symbols += [str(s)]
        else:
            symbols += [s]

    _write(f, "load data 'model'" + nl)
    _write(f, str(len(symbols)) + nl)
    _write(f, "Computation1" + nl)

    for i in range(len(symbols)):
        _write(
            f, symbols[i] + " " + str(coords[i][0]) + " " + str(coords[i][1]) +
            " " + str(coords[i][2]) + str(nl))
        #print("XX",symbols[i]+" "+str(coords[i][0])+" "+str(coords[i][1])+" "+str(coords[i][2])+str(nl))

    _write(f, "end 'model' ")
    _write(
        f, " " + copies + " " + supercell + " spacegroup '" + spacegroup +
        "' unitcell [ ")
    for i in range(3):
        _write(
            f,
            str(basis[i][0]) + " " + str(basis[i][1]) + " " +
            str(basis[i][2]) + " ")
    _write(f, "];\n")

    #_write(f, "show data;\n")

    iof.close()