<commit_before>"""
Sile object for reading/writing ref files from ScaleUp
"""

from __future__ import division, print_function

# Import sile objects
from .sile import SileScaleUp
from ..sile import *

# Import the geometry object
from sisl import Geometry, Atom, SuperCell
from sisl._help import ensure_array
from sisl.units import unit_convert

import numpy as np

__all__ = ['REFSileScaleUp', 'restartSileScaleUp']

Bohr2Ang = unit_convert('Bohr', 'Ang')
Ang2Bohr = unit_convert('Ang', 'Bohr')


class REFSileScaleUp(SileScaleUp):
    """ REF file object for ScaleUp """

    @Sile_fh_open
    def read_sc(self):
        """ Reads a supercell from the Sile """
        # 1st line is number of supercells
        nsc = ensure_array(map(int, self.readline().split()[:3]), np.int32)
        self.readline() # natoms, nspecies
        self.readline() # species
        cell = ensure_array(map(float, self.readline().split()[:9]), np.float64)
        return SuperCell(cell * Bohr2Ang)

    @Sile_fh_open
    def read_geometry(self, primary=False):
        """ Reads a geometry from the Sile """
        # 1st line is number of supercells
        nsc = ensure_array(map(int, self.readline().split()[:3]), np.int32)
        na, ns = map(int, self.readline().split()[:2])
        # Convert species to atom objects
        species = [Atom(s) for s in self.readline().split()[:ns]]

        # Total number of super-cells
        if primary:
            ns = 1
        else:
            ns = np.prod(nsc)

        cell = ensure_array(map(float, self.readline().split()[:9]), np.float64)
        cell.shape = (3, 3)
        if primary:
            cell[0, :] /= nsc[0]
            cell[1, :] /= nsc[1]
            cell[2, :] /= nsc[2]
        sc = SuperCell(cell * Bohr2Ang)

        # Create list of coordinates and atoms
        xyz = np.empty([na * ns, 3], np.float64)
        atoms = [None] * na * ns

        # Read the geometry
        for ia in range(na * ns):

            # Retrieve line
            #   ix  iy  iz  ia  is   x  y  z
            line = self.readline().split()

            atoms[ia] = species[int(line[4]) - 1]
            xyz[ia, :] = map(float, line[5:8])

        return Geometry(xyz * Bohr2Ang, atoms, sc=sc)

    @Sile_fh_open
    def write_geometry(self, geom, fmt='18.8e'):
        """ Writes the geometry to the contained file """
        # Check that we can write to the file
        sile_raise_write(self)
        # Number of supercells
        ns = np.prod(geom.sc.nsc // 2 + 1)

        # 1st line is number of supercells
        self._write('{:5d}{:5d}{:5d}\n'.format(*geom.sc.nsc // 2 + 1))
        # natoms, nspecies
        self._write('{:5d}{:5d}\n'.format(len(geom), len(geom.atom.atom)))

        s = ''
        for a, _ in geom.atom:
            # Append the species label
            s += '{:<10}'.format(a.tag)
        self._write(s + '\n')

        fmt_str = '{{:{0}}} '.format(fmt) * 9 + '\n'
        self._write(fmt_str.format(*(geom.cell*Ang2Bohr).reshape(-1)))

        # Create line
        #   ix  iy  iz  ia  is   x  y  z
        line = '{:5d}{:5d}{:5d}{:5d}{:5d}' + '{{:{0}}}'.format(fmt) * 3 + '\n'<commit_msg>bug: fixed map in scaleup<commit_after>"""
Sile object for reading/writing ref files from ScaleUp
"""

from __future__ import division, print_function

# Import sile objects
from .sile import SileScaleUp
from ..sile import *

# Import the geometry object
from sisl import Geometry, Atom, SuperCell
from sisl._help import ensure_array
from sisl.units import unit_convert

import numpy as np

__all__ = ['REFSileScaleUp', 'restartSileScaleUp']

Bohr2Ang = unit_convert('Bohr', 'Ang')
Ang2Bohr = unit_convert('Ang', 'Bohr')


class REFSileScaleUp(SileScaleUp):
    """ REF file object for ScaleUp """

    @Sile_fh_open
    def read_sc(self):
        """ Reads a supercell from the Sile """
        # 1st line is number of supercells
        nsc = ensure_array(map(int, self.readline().split()[:3]), np.int32)
        self.readline() # natoms, nspecies
        self.readline() # species
        cell = ensure_array(map(float, self.readline().split()[:9]), np.float64)
        return SuperCell(cell * Bohr2Ang)

    @Sile_fh_open
    def read_geometry(self, primary=False):
        """ Reads a geometry from the Sile """
        # 1st line is number of supercells
        nsc = ensure_array(map(int, self.readline().split()[:3]), np.int32)
        na, ns = map(int, self.readline().split()[:2])
        # Convert species to atom objects
        species = [Atom(s) for s in self.readline().split()[:ns]]

        # Total number of super-cells
        if primary:
            # Only read in the primary unit-cell
            ns = 1
        else:
            ns = np.prod(nsc)

        cell = ensure_array(map(float, self.readline().split()[:9]), np.float64)
        cell.shape = (3, 3)
        if primary:
            cell[0, :] /= nsc[0]
            cell[1, :] /= nsc[1]
            cell[2, :] /= nsc[2]
        sc = SuperCell(cell * Bohr2Ang)

        # Create list of coordinates and atoms
        xyz = np.empty([na * ns, 3], np.float64)
        atoms = [None] * na * ns

        # Read the geometry
        for ia in range(na * ns):

            # Retrieve line
            #   ix  iy  iz  ia  is   x  y  z
            line = self.readline().split()

            atoms[ia] = species[int(line[4]) - 1]
            xyz[ia, :] = ensure_array(map(float, line[5:8]), np.float64)

        return Geometry(xyz * Bohr2Ang, atoms, sc=sc)

    @Sile_fh_open
    def write_geometry(self, geom, fmt='18.8e'):
        """ Writes the geometry to the contained file """
        # Check that we can write to the file
        sile_raise_write(self)
        # Number of supercells
        ns = np.prod(geom.sc.nsc // 2 + 1)

        # 1st line is number of supercells
        self._write('{:5d}{:5d}{:5d}\n'.format(*geom.sc.nsc // 2 + 1))
        # natoms, nspecies
        self._write('{:5d}{:5d}\n'.format(len(geom), len(geom.atom.atom)))

        s = ''
        for a, _ in geom.atom:
            # Append the species label
            s += '{:<10}'.format(a.tag)
        self._write(s + '\n')

        fmt_str = '{{:{0}}} '.format(fmt) * 9 + '\n'
        self._write(fmt_str.format(*(geom.cell*Ang2Bohr).reshape(-1)))

        # Create line
        #   ix  iy  iz  ia  is   x  y  z
        line = '{:5d}{:5d}{:5d}{:5d}{:5d}' + '{{:{0}}}'.format(fmt) * 3 + '\n'
{'fraction_non_alphanumeric': 0.14540059347181009, 'fraction_numerical': 0.0196782758082149, 'mean_word_length': 2.2197083961789845, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7622579', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2172, 'n_words': 566}