How to use the cclib.io.filewriter function in cclib

To help you get started, we’ve selected a few cclib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cclib / cclib / test / io / testfilewriter.py View on Github external
def test_init(self):
        """Does the class initialize correctly?"""
        fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/dvb_gopt.adfout")
        data = cclib.io.ccread(fpath)
        writer = cclib.io.filewriter.Writer(data)

        # The object should keep the ccData instance passed to its constructor.
        self.assertEqual(writer.ccdata, data)
github cclib / cclib / test / io / testxyzwriter.py View on Github external
def test_subclass(self):
        """Is the writer a subclass of the abstract file writer?"""
        fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/dvb_gopt.adfout")
        self.assertTrue(os.path.exists(fpath))
        data = cclib.io.ccread(fpath)
        writer = cclib.io.xyzwriter.XYZ(data)
        self.assertTrue(isinstance(writer, cclib.io.filewriter.Writer))
        self.assertTrue(issubclass(type(writer), cclib.io.filewriter.Writer))
github cclib / cclib / src / cclib / io / wfxwriter.py View on Github external
def _list_format(data, per_line, style=WFX_FIELD_FMT):
    """Format lists for pretty print."""
    template = style * per_line
    leftover = len(data) % per_line
    # Template for last line.
    last_template = style * leftover

    pretty_list = [template%tuple(data[i:i+per_line])
                    for i in range(0, len(data) - leftover, per_line)]
    if leftover:
        return pretty_list + [last_template%tuple(data[-1*leftover:])]
    return  pretty_list


class WFXWriter(filewriter.Writer):
    """A writer for wfx files."""

    required_attrs = ('natom', 'atomcoords', 'atomnos', 'gbasis', 'charge',
                       'homos', 'mult', 'mocoeffs')

    def _title(self):
        """Section: Title
        Return filename without extension to be used as title."""
        title = "Written by cclib."
        if self.jobfilename is not None:
            return os.path.basename(os.path.splitext(self.jobfilename)[0]) +\
                    '. ' + title
        return title

    def _keywords(self):
        """Section: Keywords.
github cclib / cclib / cclib / io / moldenwriter.py View on Github external
def round_molden(num, p=6):
    """Molden style number rounding in [Atoms] section."""
    # Digit at pth position after dot.
    p_digit = math.floor(abs(num) * 10 ** p) % 10
    # If the 6th digit after dot is greater than 5, but is not 7,
    # round the number upto 6th place.
    # Else truncate at 6th digit after dot.
    if p_digit > 5 and p_digit != 7:
        return round(num, p)
    if num >= 0:
        return math.floor(num * 10 ** p) / 10 ** p
    else:
        return math.ceil(num * 10 ** p) / 10 ** p


class MOLDEN(filewriter.Writer):
    """A writer for MOLDEN files."""

    required_attrs = ('atomcoords', 'atomnos', 'natom')

    def _title(self, path):
        """Return filename without extension to be used as title."""
        title = os.path.basename(os.path.splitext(path)[0])
        return title

    def _coords_from_ccdata(self, index):
        """Create [Atoms] section using geometry at the given index."""
        elements = [self.pt.element[Z] for Z in self.ccdata.atomnos]
        if self.ghost is not None:
            elements = [self.ghost if e is None else e for e in elements]
        elif None in elements:
            raise ValueError('It seems that there is at least one ghost atom ' +
github cclib / cclib / cclib / io / cmlwriter.py View on Github external
# the terms of the BSD 3-Clause License.

"""A writer for chemical markup language (CML) files."""

try:
    import openbabel as ob
    _has_openbabel = True
except ImportError:
    _has_openbabel = False

import xml.etree.cElementTree as ET

from cclib.io import filewriter


class CML(filewriter.Writer):
    """A writer for chemical markup language (CML) files."""

    def __init__(self, ccdata, *args, **kwargs):
        """Initialize the CML writer object.

        Inputs:
          ccdata - An instance of ccData, parsed from a logfile.
        """

        # Call the __init__ method of the superclass
        super(CML, self).__init__(ccdata, *args, **kwargs)

    def generate_repr(self):
        """Generate the CML representation of the logfile data."""

        # Create the base molecule.
github cclib / cclib / cclib / io / cjsonwriter.py View on Github external
try:
    import openbabel as ob
    _has_openbabel = True
except ImportError:
    _has_openbabel = False

import os.path
import json
import numpy as np

from cclib.io import filewriter
from cclib.parser.data import ccData


class CJSON(filewriter.Writer):
    """A writer for chemical JSON (CJSON) files."""

    def __init__(self, ccdata, terse=False, *args, **kwargs):
        """Initialize the chemical JSON writer object.

        Inputs:
          ccdata - An instance of ccData, parsed from a logfile.
        """

        super(CJSON, self).__init__(ccdata, terse=terse, *args, **kwargs)

    def pathname(self, path):
        """Return filename without extension to be used as name."""
        name = os.path.basename(os.path.splitext(path)[0])
        return name
github cclib / cclib / cclib / io / xyzwriter.py View on Github external
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017, the cclib development team
#
# This file is part of cclib (http://cclib.github.io) and is distributed under
# the terms of the BSD 3-Clause License.

"""A writer for XYZ (Cartesian coordinate) files."""

from cclib.io import filewriter


class XYZ(filewriter.Writer):
    """A writer for XYZ (Cartesian coordinate) files."""

    def __init__(self, ccdata, splitfiles=False,
                 firstgeom=False, lastgeom=False, allgeom=False,
                 *args, **kwargs):
        """Initialize the XYZ writer object.

        Inputs:
          ccdata - An instance of ccData, parse from a logfile.
          splitfiles - Boolean to write multiple files if multiple files are requested. [TODO]
          firstgeom - Boolean to write the first available geometry from the logfile.
          lastgeom - Boolean to write the last available geometry from the logfile.
          allgeom - Boolean to write all available geometries from the logfile.
        """

        self.required_attrs = ('natom', 'atomcoords', 'atomnos')
github cclib / cclib / src / cclib / io / wfxwriter.py View on Github external
def _energy(self):
        """Section: Energy = T + Vne + Vee + Vnn.
        The total energy of the molecule.
        HF and KSDFT: SCF energy        (scfenergies),
        MP2         : MP2 total energy  (mpenergies),
        CCSD        : CCSD total energy (ccenergies).
        """
        energy = 0
        if hasattr(self.ccdata, 'ccenergies'):
            energy = self.ccdata.ccenergies[-1]
        elif hasattr(self.ccdata, 'mpenergies'):
            energy = self.ccdata.mpenergies[-1][-1]
        elif hasattr(self.ccdata, 'scfenergies'):
            energy = self.ccdata.scfenergies[-1]
        else:
            raise filewriter.MissingAttributeError(
                'scfenergies/mpenergies/ccenergies')
        return WFX_FIELD_FMT % (utils.convertor(energy, 'eV', 'hartree'))