General Usage

O2scl

General usage contents

Namespaces

Most of the classes and functions reside in the namespace o2scl. Numerical constants (many of them based on the GSL constants) are placed in separate namespaces (o2scl_const). The O₂scl functions and classes for HDF5 output are in the o2scl_hdf namespace. There are also two namespaces which hold integration coefficients, o2scl_inte_gk_coeffs and o2scl_inte_qng_coeffs. There are also some namespaces for the linear algebra functions, see Linear Algebra for more information on these.

Documentation conventions

In the following documentation, function parameters are denoted by parameter, except when used in mathematical formulas as in \(\mathrm{variable}\).

Basic error handling

Error handling is a hybrid approach combining GSL with C++ exceptions. An abstract type has been defined which operates as a GSL-like error hander. The default error handler is a implementation of this abstract type which throws a C++ exception when an error is encountered. The various exceptions, and their correspondence with the GSL error codes, are given in GSL error codes and C++ exception types. By default in O₂scl, the default GSL error handler is replaced with the O₂scl default error handler, i.e. GSL functions will throw C++ exceptions.

Library users can use the O₂scl handler with the macro O2SCL_ERR which calls the O₂scl error handler. The error handler, o2scl::err_hnd is a global pointer to an object of type err_hnd_type. There is a global default error handler, o2scl::def_err_hnd err_hnd_cpp, which throws C++ exceptions, and an alternate default error handler, o2scl::alt_err_hnd, of type err_hnd_gsl, which outputs an error message and aborts execution. The global error handler can be replaced by simply assigning the address of a descendant of err_hnd_type to o2scl::err_hnd.

Note

The alternate error handler is particularly useful to avoid throwing exceptions across DLL boundaries, e.g. when O₂scl is loaded as a DLL in python.

The C++ exception classes are also mapped to the list of GSL error codes (including a few extra ones for O₂scl), which is given in below in GSL error codes and C++ exception types. One can instruct the library to use the GSL-like O₂scl error handler o2scl::alt_err_hnd by default, by defining the constant O2SCL_USE_GSL_HANDLER. This is also useful if one wants to compile without C++ exceptions (e.g. to avoid the small overhead incurred with C++ exceptions).

What is an error?

O₂scl assumes that errors are events which should happen infrequently. Error handling strategies are often time-consuming and they are not a replacement for normal code flow. However, even with this in mind, one can still distinguish a large spectrum of posibillities from “fatal” errors, those likely to corrupt the stack and/or cause a dreaded “segmentation fault” and “non-fatal” errors, those errors which might cause incorrect results, but might be somehow recoverable. One of the purposes of error handling is to decide if and how these different types of errors should be handled differently.

Sometimes, it is undesirable to abort execution upon a failure to reach numerical convergence. While these failures are treated as errors (and by default an exception is thrown), some of the classes which attempt to reach numerical convergence have an option (e.g. o2scl::mroot::err_nonconv) to turn this default behavior off for these convergence errors. To set these “convergence” errors in code provided by the user, the macros O2SCL_CONV and O2SCL_CONV_RET can be used. Functions which may have convergence errors sometimes return int, to indicate which convergence error was returned when the value of err_nonconv has been set to false.

Of course, the standard C++ try and catch mechanism of error handling may also be used for finer-grained control.

Another related issue is that O₂scl often calls functions which are supplied by the user, these user-designed functions may create errors, and the library needs to decide how to deal with them, even though it knows little about what is actually happening inside these user-defined functions. For this reason, O₂scl does not typically try to handle any exceptions or errors occuring in user-specified functions.

GSL error codes and C++ exception types

See also the description of the error codes in err_hnd.h

enumerator success

Success.

enumerator gsl_continue

iteration has not converged

Error codes associated with exc_exception:

enumerator gsl_failure

Failure.

enumerator exc_efailed

generic failure

enumerator exc_esanity

sanity check failed - shouldn’t happen

enumerator exc_eunsup

requested feature is not supported by the hardware

enumerator exc_eunimpl

requested feature not (yet) implemented

Error codes associated with exc_range_error:

enumerator exc_edom

input domain error, e.g sqrt(-1)

enumerator exc_erange

output range error, e.g. exp(1e100)

enumerator exc_eundrflw

underflow

Error codes associated with exc_runtime_error:

enumerator exc_efault

invalid pointer

enumerator exc_efactor

factorization failed

enumerator exc_enomem

malloc failed

enumerator exc_ebadfunc

problem with user-supplied function

enumerator exc_erunaway

iterative process is out of control

enumerator exc_emaxiter

exceeded max number of iterations

enumerator exc_etol

failed to reach the specified tolerance

enumerator exc_eloss

loss of accuracy

enumerator exc_eround

failed because of roundoff error

enumerator exc_esing

apparent singularity detected

enumerator exc_ediverge

integral or series is divergent

enumerator exc_ecache

cache limit exceeded

enumerator exc_etable

limit exceeded

enumerator exc_enoprog

iteration is not making progress toward solution

enumerator exc_enoprogj

evaluations are not improving the solution

enumerator exc_etolf

cannot reach the specified tolerance in f

enumerator exc_etolx

cannot reach the specified tolerance in x

enumerator exc_etolg

cannot reach the specified tolerance in

enumerator exc_enotfound

Generic “not found” result.

enumerator exc_outsidecons

Outside constraint region.

Error codes associated with exc_invalid_argument:

enumerator exc_einval

invalid argument supplied by user

enumerator exc_ebadtol

user specified an invalid tolerance

enumerator exc_ebadlen

matrix, vector lengths are not conformant

enumerator exc_enotsqr

matrix not square

enumerator exc_eindex

Invalid index for array or matrix.

Error codes associated with exc_overflow_error:

enumerator exc_ezerodiv

tried to divide by zero

enumerator exc_eovrflw

overflow

Error codes associated with exc_ios_failure:

enumerator exc_eof

end of file

enumerator exc_efilenotfound

File not found.

Error codes associated with exc_logic_error:

enumerator exc_ememtype

Incorrect type for memory object.

Objects and scope

O₂scl objects frequently take inputs which are of the form of a reference to a smaller object. This is particularly convenient because it allows a lot of flexibility, while providing a certain degree of safety. In many cases, the user retains the responsibility of ensuring that input objects do not go out of scope before they are utilized by objects which require them. This is actually no different than the requirements on the user imposed by GSL, for example.

Member functions which store pointers to user-specified objects should warn that they are doing so in the documentation for the class. (AWS: If they do not, then this is a bug and you should let me know.)

For example, say that a user wants to solve several equations using a mroot_hybrids object and use the functions o2scl::mroot_hybrids::set() and o2scl::mroot_hybrids::iterate(). Since the function is specified by the call to set(), it is important that this function object does not go out of scope before the call to iterate() occurs.

Define constants

Various define constants used in O₂scl are listed below. Note that after O₂scl is installed, acol -v will report some details about how O₂scl was compiled.

  • O2SCL_CBLAS_NAMESPACE - This flag is internally used by some of the headers in the src/linalg directory to select between o2scl_cblas and o2scl_cblas_bracket. The end-user should not need to use this.

  • O2SCL_COND_FLAG - Internally used to handle the option of compiling with or without Armadillo or Eigen support. The end-user should not need to use this.

  • O2SCL_DATA_DIR - Used by O₂scl classes during installation to refer to the directory where data is stored. After installation, this is accessible through the global object named o2scl_settings and the command acol -v. In a typical installation, the end-user should not need to use this.

  • O2SCL_DOC_DIR - Used by O₂scl classes during installation to refer to the directory where the documentation is stored. After installation, this is accessible through the global object named o2scl_settings and the command acol -v. In a typical installation, the end-user should not need to use this.

  • O2SCL_HDF5_COMP - Define this flag when O₂scl is compiled and for code which uses O₂scl to include support for HDF5 compression. The command acol -v reports whether or not HDF5 compression support was enabled during compilation.

  • O2SCL_MPI - Flag to allow MPI functionality in O₂scl classes which contain MPI code (see also O2SCL_OPENMP). All current MPI functionality in O₂scl is header only, thus MPI support does not need to be specified to the configure script.

  • O2SCL_NEVER_DEFINED - Used internally to comment out large sections of code. This should never be defined by the end-user.

  • O2SCL_NO_EXCEPTIONS - If this is defined, then the error handler calls o2scl::err_hnd_gsl::set() instead of throwing a C++ exception. Used in src/base/exception.cpp. This is useful, for example, with the g++ flag -fno-exceptions.

  • O2SCL_NO_SYSTEM_FUNC - If this is defined, then the cli class will never allow shell commands, independent of the setting of o2scl::cli::shell_cmd_allowed.

  • O2SCL_NO_RANGE_CHECK - If this is defined, range checking is turned off where it is used in table, tensor, permutation and the functions in o2scl_cblas. Some O₂scl header files use this define constant and so range checking can be turned off or on separately from the setting that was used during installation.

  • O2SCL_OLDER_COMPILER - A flag which can be defined both during compilation of O₂scl and compilation of code using O₂scl for compilers which do not have std::initializer_list, std::to_string, std::uniform_distribution and cannot run mcmc_para_ts.

  • O2SCL_PLAIN_HDF5_HEADER - If true, assume HDF5 include statements should be of the form #include <hdf5.h> independent of the automatically determined operating system type.

  • O2SCL_SET_* flags - These flags indicate support for external libraries. The header files o2scl/set_*.h automatically define these constants if O₂scl was compiled with the associated external libraries (i.e. using a --enable-* flag to the configure script).

  • O2SCL_SNAP - If true, then make some modifications to enable compiling O₂scl with snap.

  • O2SCL_UBUNTU_PKG - If true, don’t use current date and time macros to avoid Ubuntu packaging errors (used in src/base/lib_settings.cpp and src/hdf/acolm.cpp). The end-user should not need to use this macro.

  • O2SCL_USE_BOOST_FILESYSTEM - Doesn’t do anything (yet).

  • O2SCL_USE_GSL_HANDLER - If this is defined, then an object of type err_hnd_gsl is the default error handler. Used in src/base/exception.cpp

Error handler objects

These objects are defined in src/base/err_hnd.h:

err_hnd_type *o2scl::err_hnd

The global error handler pointer.

This is set by the def_err_hnd constructor to point to that object.

err_hnd_gsl o2scl::alt_err_hnd

An alternate GSL-like error handler.

This object is defined in src/base/exception.h:

err_hnd_cpp o2scl::def_err_hnd

The default error handler.