Source code for schrodinger.application.matsci.qb_sdk.tools
# Copyright (c) 2021, Qu & Co
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# built-in
import base64
import logging
import sys
from binascii import Error as EncodingError
# local
from .parameters import ENCODING_FMT, QUBEC_SDK_VERBOSE
[docs]def encode_geometry_xyz(geometry: list, description: str = "") -> str:
    """
    Serialize the geometry into a base64 encoded string in .xyz format
    """
    n_atoms = len(geometry)
    xyz_str = ""
    xyz_str += f"{n_atoms}\n"
    xyz_str += f"{description}\n"
    for a in geometry:
        atom_str = f"{a[0]} {' '.join([str(p) for p in a[1]])}\n"
        xyz_str += atom_str
    try:
        tmp: bytes = xyz_str.strip().encode(ENCODING_FMT)
        xyz_bytes = base64.b64encode(tmp)
    except EncodingError:
        raise EncodingError(
            f"Failed to encode the molecular geometry: {geometry}")
    return xyz_bytes.decode(ENCODING_FMT) 
# LOG_FORMAT = '%(levelname) -5s %(asctime)s %(name) -10s %(funcName) -15s %(lineno) -5d: %(message)s'
LOG_FORMAT = "QUBEC %(funcName) -15s: %(message)s"
LOG_STREAM_HANDLER = sys.stdout
[docs]def get_logger(name: str) -> logging.Logger:
    logger: logging.Logger = logging.getLogger(name)
    lvl = logging.INFO
    if QUBEC_SDK_VERBOSE:
        lvl = logging.DEBUG
    logger.setLevel(lvl)
    formatter = logging.Formatter(LOG_FORMAT)
    sh = logging.StreamHandler(LOG_STREAM_HANDLER)
    sh.setFormatter(formatter)
    logger.addHandler(sh)
    return logger