Skip to content

Commit

Permalink
Log wrong user inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
wangenau committed Feb 3, 2021
1 parent 28e199c commit 809153b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
24 changes: 20 additions & 4 deletions var_mesh/gen_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ def var_mesh(mesh, thres=1e-6, precise=True, mode='pyscf'):
# Initialize logger
verbose = mesh.verbose
log = logger.Logger(stdout, verbose)
# Handle user inputs
if not isinstance(mesh, dft.gen_grid.Grids):
log.error('The mesh parameter has to be a Grids object.')
if not isinstance(thres, float) and not isinstance(thres, int):
log.error('The threshold parameter has to be a number.')
if thres < 0:
log.error('The threshold parameter has to be positive.')
if not isinstance(precise, bool):
log.error('The precise parameter has to be a boolean.')
if not isinstance(mode, str):
log.error('The mode parameter has to be a string.')
mode = mode.lower()
supported = ('pyscf', 'erkale', 'gamess')
if mode not in supported:
log.error('The mode parameter has to be one of \'%s\'.',
'\', \''.join(supported))
if mode != 'pyscf' and precise:
log.warn('The precise parameter has no effect when using %s mode.',
mode)

# Create calculator to generate the initial density
mf = dft.RKS(mesh.mol)
mf.max_cycle = 0
Expand All @@ -52,10 +72,6 @@ def var_mesh(mesh, thres=1e-6, precise=True, mode='pyscf'):
mf.mol.verbose = 0
mf.grids.verbose = 0

mode = mode.lower()
if mode != 'pyscf' and precise:
log.warn('The precise option has no effect when using %s mode.', mode)

# Enter coarse grid search
types = atom_types(mesh.mol)
steps = get_steps()
Expand Down
33 changes: 31 additions & 2 deletions var_mesh/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from pyscf.dft.gen_grid import Grids
from pyscf.dft.radi import BRAGG_RADII
from pyscf.gto import charge
from pyscf.lib import logger
from sys import stdout

# Color some atoms with their respective CPK color
# https://en.wikipedia.org/wiki/CPK_coloring
Expand All @@ -32,6 +35,15 @@ def plot_mesh_3d(mesh, weight=False, **kwargs):
Scale grid points by their weights when set to ``True``. Integers
will scale grid points.
'''
# Initialize logger
verbose = mesh.verbose
log = logger.Logger(stdout, verbose)
# Handle user inputs
if not isinstance(mesh, Grids):
log.error('The mesh parameter has to be a Grids object.')
if not isinstance(weight, bool) and not isinstance(weight, int):
log.error('The weight parameter has to be a boolean or an integer.')

mol = mesh.mol
coords = mesh.coords
atoms = mol.atom_coords()
Expand Down Expand Up @@ -75,15 +87,32 @@ def plot_mesh_2d(mesh, weight=False, plane='xy'):
will scale grid points.
plane : str
Contains the plane to project on to. Needs two elements of ``x``,
``y``, and ``z``.
Contains the plane to project on to. Needs two elements of ``'x'``,
``'y'``, and ``'z'``.
'''
# Dictionary to map input axes to their coordinates
ax = {
'x': 0,
'y': 1,
'z': 2
}

# Initialize logger
verbose = mesh.verbose
log = logger.Logger(stdout, verbose)
# Handle user inputs
if not isinstance(mesh, Grids):
log.error('The mesh parameter has to be a Grids object.')
if not isinstance(weight, bool) and not isinstance(weight, int):
log.error('The weight parameter has to be a boolean or an integer.')
if not isinstance(plane, str):
log.error('The plane parameter has to be a string.')
if len(plane) != 2:
log.error('The plane parameter has to be a string of length 2.')
if not set(plane).issubset(ax.keys()):
log.error('The plane parameter only accepts combinations of \'%s\'.',
'\', \''.join(ax.keys()))

ax1, ax2 = list(plane.lower())
mol = mesh.mol
coords = mesh.coords
Expand Down

0 comments on commit 809153b

Please sign in to comment.