"""Rydiqule Custom Exceptions and WarningsThis module defines a number of custom Errors and Warnings for use in Rydiqule.It ensures that all RydiquleWarnings are always shown.This behavior can be changed by calling:external+python:func:`warnings.simplefilter`.It also defines custom exception handlers to hide theraise statement associated with RydiquleErrors.This behavior can be suppressed by calling:func:`rq.debug_state(True) <debug_state>`."""importwarningsimporttracebackimportsys###### Rydiqule Exception Definitions
[docs]classRydiquleError(Exception):"""A *rydiqule* error Indicates a Rydiqule-specific error. It is a thin wrapper around Exception. """pass
[docs]classAtomError(RydiquleError):"""An error in interacting with ARC"""pass
[docs]classCouplingNotAllowedError(RydiquleError):"""Indicated coupling is not allowed (eg dipole-forbidden)"""pass
# setup concealing of final frame in RydiquleError tracebacks unless in DEBUG modeDEBUG=False
[docs]defset_debug_state(state:bool):"""Controls DEBUG state of rydiqule. Parameters ---------- state: bool If True, full error tracebacks for RydiquleErrors will be shown. If False (default behavior), final raise statement is suppressed in the traceback. """globalDEBUGDEBUG=state
[docs]defdebug_state():"""Returns current rydiqule debug state Returns ------- bool Current debug state """returnDEBUG
# suppress final frame of RydiquleError tracebacks for normal usage# ensure we use current excepthook as fallback, in case someone has overridden alreadycurrent_excepthook=sys.excepthook
[docs]defquiet_exception_handler(exception_type,exception,tb):ifissubclass(exception_type,RydiquleError)andnotDEBUG:tb_counter=1tb_han=tbwhiletb_han.tb_nextisnotNone:tb_counter+=1tb_han=tb_han.tb_next# -1 strips off the final raise statement from the tracebacktraceback.print_exception(exception_type,exception,tb,limit=tb_counter-1)else:current_excepthook(exception_type,exception,tb)
sys.excepthook=quiet_exception_handler# suppress RydiquleError tracebacks in IPython (ie Jupyter)try:fromIPythonimportget_ipythonipython=get_ipython()ifipythonisnotNone:# don't patch if IPython isn't being useddefexception_handler(self,etype,evalue,tb,tb_offset=None):ifnotDEBUG:tb_han=tb# assumes at least 2 levels to the tracebackwhiletb_han.tb_next.tb_nextisnotNone:tb_han=tb_han.tb_next# stop the traceback at the 2nd to last leveltb_han.tb_next=None# standard IPython's printoutself.showtraceback((etype,evalue,tb),tb_offset=tb_offset)ipython.set_custom_exc((RydiquleError,),exception_handler)delipython,get_ipythonexceptImportError:# if ipython not available, don't patchpass###### Rydiqule Warning Definitions
[docs]classRydiquleWarning(UserWarning):"""Indicates a rydiqule-specfic warning. All other rydiqule warnings derive from this class. Disable globally by calling :external+python:func:`warnings.simplefilter('ignore', rq.RydiquleWarning) <warnings.simplefilter>` or locally using the :external+python:class:`warnings.catch_warnings` context manager. """pass
[docs]classRWAWarning(RydiquleWarning):"""Indicates the coupling is using a large transition frequency outside the rotating wave approximation. """pass
[docs]classPopulationNotConservedWarning(RydiquleWarning):"""Indicates population will not be conserved in the model."""pass
[docs]classTimeDependenceWarning(RydiquleWarning):"""Indicates a time-dependent coupling is being used in a steady-state context"""pass
[docs]classNLJMWarning(RydiquleWarning):"""Indicates `Cell` has likely been called with the old state specification [n, l, j, m] for a solve that did not intend to use fine-structure magnetic sublevels. """pass
# Ensure Rydiqule warnings are always raised# can be overridden by user after importwarnings.filterwarnings('always',category=RydiquleWarning,append=True)