DFT-FE 1.1.0-pre
Density Functional Theory With Finite-Elements
Loading...
Searching...
No Matches
Exceptions.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (c) 2017-2025 The Regents of the University of Michigan and DFT-FE
4// authors.
5//
6// This file is part of the DFT-FE code.
7//
8// The DFT-FE code is free software; you can use it, redistribute
9// it, and/or modify it under the terms of the GNU Lesser General
10// Public License as published by the Free Software Foundation; either
11// version 2.1 of the License, or (at your option) any later version.
12// The full text of the license can be found in the file LICENSE at
13// the top level of the DFT-FE distribution.
14//
15// ---------------------------------------------------------------------
16
17/*
18 * @author Bikash Kanungo
19 */
20#ifndef dftfeExceptions_h
21#define dftfeExceptions_h
22
23#include <string>
24#include <stdexcept>
25/**
26@brief provides an interface for exception handling.
27It two overrides on the assert(expr) function in C/C++ and two wrappers on
28std::exception.
29
30The overrides on assert(expr) are useful for debug mode testing. That is,
31you want the assert to be executed *only in debug mode* and not in release
32mode (i.e., if NDEBUG is defined). The two assert overrides are
331. DFTFE_Assert(expr): same as std::assert(expr). Throws an assert
34if expr is false
352. DFTFE_AssertWithMsg(expr,msg): same as above but takes an
36additional message in the form of string to display if expr is false.
37
38It also provides two preprocessor flags, DFTFE_DISABLE_ASSERT and
39DFTFE_ENABLE_ASSERT, that you can set to override the NDEBUG flag in a
40particular source file. This is provided to allow selective enabling or
41disabling of Assert and AssertWithMsg without any relation to whether NDEBUG
42is defined or not (NDEBUG is typically defined globally for all files
43through compiler options).
44For example, if in a file you have
45#define DFTFE_DISABLE_ASSERT
46#include "Exceptions.h"
47then it would disable all calls to Assert or AssertWithMsg in that file,
48regardless of whether NDEBUG is defined. Also, it has no bearing on
49std::assert (i.e., any calls to std::assert in that file will still be
50governed by NDEBUG). Similarly, if in a file you have
51#define
52DFTFE_ENABLE_ASSERT
53#include "Exceptions.h"
54then it would enable all calls to Assert or AssertWithMsg in that file,
55regardless of whether NDEBUG is defined.
56Also, it has no bearning on std::assert (i.e., any calls
57to std::assert in that file will still be governed by NDEBUG)
58
59It also provides two wrappers on std::exception and its derived classes
60(e.g., std::runtime_error, std::domain_error, etc.) The two wrappers are:
611. dftfe::utils::throwException(expr,msg): a generic exception handler
62which throws an optional message (msg) if expr evaluates to false. It
63combines std::exception with an additional messgae. (Note: the
64std::exception has no easy way of taking in a message).
652. dftfe::utils::throwException<T>(expr, msg): similar to the above, but
66takes a specific derived class of std::exception handler as a template
67parameter. The derived std::exception must have a constructor that takes in
68a string. For the ease of the user, we have typedef-ed some commonly used
69derived classes of std::exception. A user can use the typedefs as the
70template parameter instead. Available typedefs LogicError - std::logic_error
71 InvalidArgument - std::invalid_argument
72 DomainError - std::domain_error
73 LengthError - std::length_error
74 OutOfRangeError - std::out_of_range
75 FutureError - std::future_error
76 RuntimeError - std::runtime_error
77 OverflowError - std::overflow_error
78 UnderflowError - std::underflow_error
79*/
80
81#undef DFTFE_Assert
82#undef DFTFE_AssertWithMsg
83
84#if defined(DFTFE_DISABLE_ASSERT) || \
85 (!defined(DFTFE_ENABLE_ASSERT) && defined(NDEBUG))
86# include <assert.h> // .h to support old libraries w/o <cassert> - effect is the same
87# define DFTFE_Assert(expr) ((void)0)
88# define DFTFE_AssertWithMsg(expr, msg) ((void)0)
89
90#elif defined(DFTFE_ENABLE_ASSERT) && defined(NDEBUG)
91# undef NDEBUG // disabling NDEBUG to forcibly enable assert for sources that
92 // set DFTFE_ENABLE_ASSERT even when in release mode (with
93 // NDEBUG)
94# include <assert.h> // .h to support old libraries w/o <cassert> - effect is the same
95# define DFTFE_Assert(expr) assert(expr)
96# define DFTFE_AssertWithMsg(expr, msg) assert((expr) && (msg))
97
98#else
99# include <assert.h> // .h to support old libraries w/o <cassert> - effect is the same
100# define DFTFE_Assert(expr) assert(expr)
101# define DFTFE_AssertWithMsg(expr, msg) assert((expr) && (msg))
102
103#endif
104
105#ifdef DFTFE_WITH_DEVICE_LANG_CUDA
106# include <DeviceExceptions.cu.h>
107#elif DFTFE_WITH_DEVICE_LANG_HIP
108# include <DeviceExceptions.hip.h>
109#endif
110
111#define MPICHECK(cmd) \
112 do \
113 { \
114 int e = cmd; \
115 if (e != MPI_SUCCESS) \
116 { \
117 printf("Failed: MPI error %s:%d '%d'\n", __FILE__, __LINE__, e); \
118 exit(EXIT_FAILURE); \
119 } \
120 } \
121 while (0)
122
123namespace dftfe
124{
125 namespace utils
126 {
127 typedef std::logic_error LogicError;
128 typedef std::invalid_argument InvalidArgument;
129 typedef std::domain_error DomainError;
130 typedef std::length_error LengthError;
131 typedef std::out_of_range OutOfRangeError;
132 typedef std::runtime_error RuntimeError;
133 typedef std::overflow_error OverflowError;
134 typedef std::underflow_error UnderflowError;
135
136 void
137 throwException(bool condition, std::string msg = "");
138
139 template <class T>
140 void
141 throwException(bool condition, std::string msg = "");
142
143 } // namespace utils
144} // namespace dftfe
145#include "Exceptions.t.cc"
146#endif // dftfeExceptions_h
std::domain_error DomainError
Definition Exceptions.h:129
void throwException(bool condition, std::string msg="")
std::overflow_error OverflowError
Definition Exceptions.h:133
std::underflow_error UnderflowError
Definition Exceptions.h:134
std::invalid_argument InvalidArgument
Definition Exceptions.h:128
std::length_error LengthError
Definition Exceptions.h:130
std::logic_error LogicError
Definition Exceptions.h:127
std::runtime_error RuntimeError
Definition Exceptions.h:132
std::out_of_range OutOfRangeError
Definition Exceptions.h:131
Definition pseudoPotentialToDftfeConverter.cc:34