From 669accb6e561f7be4900f3156a5f75bc841a2f6a Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Wed, 18 Feb 2026 11:24:30 -0500 Subject: [PATCH] use Exception instead of std exceptions --- SeQuant/core/binary_node.hpp | 3 +- SeQuant/core/context.hpp | 2 +- SeQuant/core/eval/backends/btas/result.hpp | 3 +- SeQuant/core/eval/backends/tapp/result.hpp | 3 +- SeQuant/core/eval/backends/tapp/tensor.hpp | 4 +- .../core/eval/backends/tiledarray/result.hpp | 3 +- SeQuant/core/eval/eval_expr.cpp | 2 +- SeQuant/core/export/export.hpp | 12 ++- SeQuant/core/export/itf.cpp | 8 +- SeQuant/core/export/itf.hpp | 17 ++-- SeQuant/core/export/julia_itensor.hpp | 5 +- SeQuant/core/export/julia_tensor_kit.hpp | 5 +- .../core/export/julia_tensor_operations.cpp | 4 +- .../core/export/julia_tensor_operations.hpp | 8 +- SeQuant/core/export/python_einsum.hpp | 20 ++--- SeQuant/core/export/text_generator.hpp | 3 +- SeQuant/core/expressions/abstract_tensor.cpp | 2 +- SeQuant/core/expressions/constant.hpp | 11 ++- SeQuant/core/expressions/expr.cpp | 4 +- SeQuant/core/expressions/expr.hpp | 14 +-- SeQuant/core/expressions/tensor.hpp | 5 +- SeQuant/core/hugenholtz.hpp | 3 +- SeQuant/core/index.hpp | 3 +- SeQuant/core/index_space_registry.hpp | 88 +++++++++---------- SeQuant/core/io/latex/latex.cpp | 4 +- SeQuant/core/io/latex/latex.hpp | 7 +- .../io/serialization/v1/ast_conversions.hpp | 4 +- .../core/io/serialization/v1/serialize.cpp | 3 +- SeQuant/core/op.hpp | 27 +++--- SeQuant/core/rational.hpp | 14 +-- SeQuant/core/runtime.hpp | 5 +- SeQuant/core/space.cpp | 2 +- SeQuant/core/space.hpp | 7 +- SeQuant/core/tensor_canonicalizer.cpp | 2 +- SeQuant/core/tensor_canonicalizer.hpp | 2 +- SeQuant/core/tensor_network/v1.cpp | 2 +- SeQuant/core/tensor_network/v1.hpp | 13 ++- SeQuant/core/tensor_network/v2.cpp | 2 +- SeQuant/core/tensor_network/v2.hpp | 9 +- SeQuant/core/tensor_network/v3.cpp | 2 +- SeQuant/core/tensor_network/v3.hpp | 7 +- SeQuant/core/utility/expr.cpp | 6 +- SeQuant/core/utility/expr.hpp | 2 +- SeQuant/core/utility/indices.hpp | 2 +- SeQuant/core/utility/singleton.hpp | 11 +-- SeQuant/core/wick.hpp | 24 ++--- SeQuant/core/wick.impl.hpp | 2 +- SeQuant/domain/mbpt/biorthogonalization.cpp | 6 +- SeQuant/domain/mbpt/op.cpp | 10 +-- SeQuant/domain/mbpt/op.hpp | 3 +- SeQuant/domain/mbpt/op_registry.cpp | 17 ++-- SeQuant/domain/mbpt/spin.cpp | 61 ++++++------- SeQuant/domain/mbpt/utils.cpp | 7 +- SeQuant/domain/mbpt/vac_av.cpp | 2 +- tests/unit/test_expr.cpp | 4 +- tests/unit/test_latex.cpp | 6 +- tests/unit/test_math.cpp | 2 +- tests/unit/test_tensor_network.cpp | 14 +-- tests/unit/test_utilities.cpp | 4 +- tests/unit/test_wick.cpp | 4 +- 60 files changed, 249 insertions(+), 282 deletions(-) diff --git a/SeQuant/core/binary_node.hpp b/SeQuant/core/binary_node.hpp index 1e1166204b..30054c2208 100644 --- a/SeQuant/core/binary_node.hpp +++ b/SeQuant/core/binary_node.hpp @@ -5,7 +5,6 @@ #include #include -#include #include #include @@ -243,7 +242,7 @@ class FullBinaryNode { if (n) return n; else - throw std::runtime_error( + throw Exception( "Dereferenced nullptr: use leaf() or root() methods to check for " "leaf and root nodes"); } diff --git a/SeQuant/core/context.hpp b/SeQuant/core/context.hpp index 5096231638..3facf87c19 100644 --- a/SeQuant/core/context.hpp +++ b/SeQuant/core/context.hpp @@ -129,7 +129,7 @@ class Context { /// was default constructed) std::shared_ptr index_space_registry() const; /// @return a pointer to the IndexSpaceRegistry for this context. - /// @throw std::logic_error if the IndexSpaceRegistry is null + /// @throw Exception if the IndexSpaceRegistry is null std::shared_ptr mutable_index_space_registry() const; /// \return IndexSpaceMetric of this context IndexSpaceMetric metric() const; diff --git a/SeQuant/core/eval/backends/btas/result.hpp b/SeQuant/core/eval/backends/btas/result.hpp index aa7cff67ed..af56c0c1f0 100644 --- a/SeQuant/core/eval/backends/btas/result.hpp +++ b/SeQuant/core/eval/backends/btas/result.hpp @@ -5,6 +5,7 @@ #include #include +#include #include @@ -28,7 +29,7 @@ auto column_symmetrize_btas(btas::Tensor const& arr) { size_t const rank = arr.rank(); if (rank % 2 != 0) - throw std::domain_error("This function only supports even-ranked tensors"); + throw Exception("This function only supports even-ranked tensors"); perm_t perm = iota(size_t{0}, rank) | ranges::to; diff --git a/SeQuant/core/eval/backends/tapp/result.hpp b/SeQuant/core/eval/backends/tapp/result.hpp index 111710a01a..37d22af0d7 100644 --- a/SeQuant/core/eval/backends/tapp/result.hpp +++ b/SeQuant/core/eval/backends/tapp/result.hpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace sequant { @@ -27,7 +28,7 @@ auto column_symmetrize_tapp(TAPPTensor const& arr) { size_t const rank = arr.rank(); if (rank % 2 != 0) - throw std::domain_error("This function only supports even-ranked tensors"); + throw Exception("This function only supports even-ranked tensors"); perm_t perm = iota(size_t{0}, rank) | ranges::to; diff --git a/SeQuant/core/eval/backends/tapp/tensor.hpp b/SeQuant/core/eval/backends/tapp/tensor.hpp index a6a9abd2c2..2e8e2b224d 100644 --- a/SeQuant/core/eval/backends/tapp/tensor.hpp +++ b/SeQuant/core/eval/backends/tapp/tensor.hpp @@ -4,6 +4,7 @@ #ifdef SEQUANT_HAS_TAPP #include +#include #include @@ -13,7 +14,6 @@ #include #include #include -#include #include #include @@ -60,7 +60,7 @@ inline void check_error(TAPP_error err) { if (!TAPP_check_success(err)) { char msg[256]; TAPP_explain_error(err, sizeof(msg), msg); - throw std::runtime_error(std::string("TAPP error: ") + msg); + throw Exception(std::string("TAPP error: ") + msg); } } diff --git a/SeQuant/core/eval/backends/tiledarray/result.hpp b/SeQuant/core/eval/backends/tiledarray/result.hpp index 17388edcba..44b39ee397 100644 --- a/SeQuant/core/eval/backends/tiledarray/result.hpp +++ b/SeQuant/core/eval/backends/tiledarray/result.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -28,7 +29,7 @@ auto column_symmetrize_ta(TA::DistArray const& arr) { size_t const rank = arr.trange().rank(); if (rank % 2 != 0) - throw std::domain_error("This function only supports even-ranked tensors"); + throw Exception("This function only supports even-ranked tensors"); TA::DistArray result; diff --git a/SeQuant/core/eval/eval_expr.cpp b/SeQuant/core/eval/eval_expr.cpp index 371a4e77a3..e805cea82e 100644 --- a/SeQuant/core/eval/eval_expr.cpp +++ b/SeQuant/core/eval/eval_expr.cpp @@ -545,7 +545,7 @@ EvalExprNode binarize(ExprPtr const& expr, IndexSet const& uncontract) { if (expr->is()) // return binarize(expr->as(), uncontract); - throw std::logic_error("Encountered unsupported expression in binarize."); + throw Exception("Encountered unsupported expression in binarize."); } } // namespace impl diff --git a/SeQuant/core/export/export.hpp b/SeQuant/core/export/export.hpp index a15fbd327f..0088da689d 100644 --- a/SeQuant/core/export/export.hpp +++ b/SeQuant/core/export/export.hpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -75,7 +74,7 @@ class GenerationVisitor { } else if (node->is_variable()) { m_generator.persist(node->as_variable(), m_ctx); } else { - throw std::runtime_error( + throw Exception( "Root nodes are expected to be of type tensor or variable"); } @@ -139,7 +138,7 @@ class GenerationVisitor { return; } if (!node->is_tensor() && !node->is_variable()) { - throw std::runtime_error( + throw Exception( "Unexpected expression type in " "GenerationVisitor::load_or_create"); } @@ -842,14 +841,13 @@ void export_groups(Range groups, Generator &generator, Context ctx) { if (size(groups) > 1) { if (!generator.supports_named_sections()) { - throw std::runtime_error("The generator for '" + - generator.get_format_name() + - "' doesn't support named sections"); + throw Exception("The generator for '" + generator.get_format_name() + + "' doesn't support named sections"); } for (const ExpressionGroup ¤t : groups) { if (!current.is_named()) { - throw std::runtime_error( + throw Exception( "Can't have unnamed groups when exporting multiple groups at once"); } } diff --git a/SeQuant/core/export/itf.cpp b/SeQuant/core/export/itf.cpp index b78f1b639e..aa0b5aadbd 100644 --- a/SeQuant/core/export/itf.cpp +++ b/SeQuant/core/export/itf.cpp @@ -50,8 +50,8 @@ std::string ItfContext::get_name(const IndexSpace &space) const { auto it = m_space_names.find(space); if (it == m_space_names.end()) { - throw std::runtime_error("No name known for index space '" + - toUtf8(space.base_key()) + "'"); + throw Exception("No name known for index space '" + + toUtf8(space.base_key()) + "'"); } return it->second; @@ -61,8 +61,8 @@ std::string ItfContext::get_tag(const IndexSpace &space) const { auto it = m_tags.find(space); if (it == m_tags.end()) { - throw std::runtime_error("No tag known for index space '" + - toUtf8(space.base_key()) + "'"); + throw Exception("No tag known for index space '" + + toUtf8(space.base_key()) + "'"); } return it->second; diff --git a/SeQuant/core/export/itf.hpp b/SeQuant/core/export/itf.hpp index ea11d5d9bb..78b2af7d0e 100644 --- a/SeQuant/core/export/itf.hpp +++ b/SeQuant/core/export/itf.hpp @@ -100,7 +100,7 @@ class ItfContext : public ReorderingContext { /// Generator for ITF. ITF (Integrated Tensor Framework) is a domain-specific /// language for tensor contractions used by parts of the Molpro quantum /// chemistry program. -/// WIREs Comput Mol Sci 2012, 2: 242–253 doi: 10.1002/wcms.82 +/// WIREs Comput Mol Sci 2012, 2: 242-253 doi: 10.1002/wcms.82 template class ItfGenerator : public Generator { public: @@ -131,7 +131,7 @@ class ItfGenerator : public Generator { std::string represent(const Index &idx, const Context &ctx) const override { if (idx.has_proto_indices()) { - throw std::runtime_error("ITF doesn't support proto indices"); + throw Exception("ITF doesn't support proto indices"); } const std::size_t ordinal = idx.ordinal().value(); @@ -203,8 +203,7 @@ class ItfGenerator : public Generator { void create(const Tensor &tensor, bool zero_init, const Context &ctx) override { if (!zero_init) { - throw std::runtime_error( - "Can't create ITF tensor without setting it to zero"); + throw Exception("Can't create ITF tensor without setting it to zero"); } m_generated += "alloc " + represent(tensor, ctx) + "\n"; @@ -240,8 +239,7 @@ class ItfGenerator : public Generator { void create(const Variable &variable, bool zero_init, const Context &ctx) override { if (!zero_init) { - throw std::runtime_error( - "Can't create ITF variable without setting it to zero"); + throw Exception("Can't create ITF variable without setting it to zero"); } m_generated += "alloc " + represent(variable, ctx) + "\n"; @@ -406,7 +404,7 @@ class ItfGenerator : public Generator { const Product &product = expr.as(); if (product.factors().size() > 2) { - throw std::runtime_error("ITF can only handle binary contractions"); + throw Exception("ITF can only handle binary contractions"); } std::string repr; @@ -426,11 +424,10 @@ class ItfGenerator : public Generator { return repr; } else if (expr.is()) { // TODO: Handle on-the-fly antisymmetrization (K[abij] - K[baij]) - throw std::runtime_error("ITF doesn't support explicit summation"); + throw Exception("ITF doesn't support explicit summation"); } - throw std::runtime_error( - "Unsupported expression type in ItfGenerator::compute"); + throw Exception("Unsupported expression type in ItfGenerator::compute"); } }; diff --git a/SeQuant/core/export/julia_itensor.hpp b/SeQuant/core/export/julia_itensor.hpp index d3b77178d7..3c57ffe5c0 100644 --- a/SeQuant/core/export/julia_itensor.hpp +++ b/SeQuant/core/export/julia_itensor.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -41,7 +42,7 @@ class JuliaITensorGenerator : public JuliaTensorOperationsGenerator { void create(const Tensor &tensor, bool zero_init, const Context &ctx) override { if (!zero_init) { - throw std::runtime_error( + throw Exception( "In Julia tensors can't be created without being initialized"); } @@ -78,7 +79,7 @@ class JuliaITensorGenerator : public JuliaTensorOperationsGenerator { void create(const Variable &variable, bool zero_init, const Context &ctx) override { if (!zero_init) { - throw std::runtime_error( + throw Exception( "Julia doesn't support declaring a variable without initializing it"); } diff --git a/SeQuant/core/export/julia_tensor_kit.hpp b/SeQuant/core/export/julia_tensor_kit.hpp index 6fe3989fbe..7f17a95940 100644 --- a/SeQuant/core/export/julia_tensor_kit.hpp +++ b/SeQuant/core/export/julia_tensor_kit.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -35,7 +36,7 @@ class JuliaTensorKitGenerator : public JuliaTensorOperationsGenerator { void create(const Tensor &tensor, bool zero_init, const Context &ctx) override { if (!zero_init) { - throw std::runtime_error( + throw Exception( "In Julia tensors can't be created without being initialized"); } @@ -79,7 +80,7 @@ class JuliaTensorKitGenerator : public JuliaTensorOperationsGenerator { const std::size_t num_indices = tensor.num_indices(); if (braRank == 0 && num_indices > 0) { - throw std::runtime_error( + throw Exception( "It is not (yet) clear how to represent a zero-dimensional domain " "for a tensor in TensorKit"); } diff --git a/SeQuant/core/export/julia_tensor_operations.cpp b/SeQuant/core/export/julia_tensor_operations.cpp index d5279e180b..0bcf04a873 100644 --- a/SeQuant/core/export/julia_tensor_operations.cpp +++ b/SeQuant/core/export/julia_tensor_operations.cpp @@ -16,8 +16,8 @@ std::string JuliaTensorOperationsGeneratorContext::get_tag( auto it = m_index_tags.find(space); if (it == m_index_tags.end()) { - throw std::runtime_error("No known tags for indices of space \"" + - toUtf8(space.base_key()) + "\""); + throw Exception("No known tags for indices of space \"" + + toUtf8(space.base_key()) + "\""); } return it->second; diff --git a/SeQuant/core/export/julia_tensor_operations.hpp b/SeQuant/core/export/julia_tensor_operations.hpp index 0938478d4a..53dd112fdb 100644 --- a/SeQuant/core/export/julia_tensor_operations.hpp +++ b/SeQuant/core/export/julia_tensor_operations.hpp @@ -77,7 +77,7 @@ class JuliaTensorOperationsGenerator : public Generator { std::string represent(const Index &idx, const Context &) const override { if (idx.has_proto_indices()) { - throw std::runtime_error("Proto Indices are not (yet) supported!"); + throw Exception("Proto Indices are not (yet) supported!"); } return toUtf8(idx.full_label()); @@ -127,7 +127,7 @@ class JuliaTensorOperationsGenerator : public Generator { void create(const Tensor &tensor, bool zero_init, const Context &ctx) override { if (!zero_init) { - throw std::runtime_error( + throw Exception( "In Julia tensors can't be created without being initialized"); } @@ -171,7 +171,7 @@ class JuliaTensorOperationsGenerator : public Generator { void create(const Variable &variable, bool zero_init, const Context &ctx) override { if (!zero_init) { - throw std::runtime_error( + throw Exception( "Julia doesn't support declaring a variable without initializing it"); } @@ -303,7 +303,7 @@ class JuliaTensorOperationsGenerator : public Generator { return repr; } - throw std::runtime_error("Unsupported expression type in to_julia_expr"); + throw Exception("Unsupported expression type in to_julia_expr"); } std::string tensor_name(const Tensor &tensor, const Context &ctx) const { diff --git a/SeQuant/core/export/python_einsum.hpp b/SeQuant/core/export/python_einsum.hpp index 3bf08e1246..99b0ddd111 100644 --- a/SeQuant/core/export/python_einsum.hpp +++ b/SeQuant/core/export/python_einsum.hpp @@ -142,7 +142,7 @@ class PythonEinsumGeneratorBase : public Generator { std::string represent(const Index &idx, const Context &) const override { if (idx.has_proto_indices()) { - throw std::runtime_error("Proto Indices are not (yet) supported!"); + throw Exception("Proto Indices are not (yet) supported!"); } // Convert index label to a single character for einsum notation @@ -153,7 +153,7 @@ class PythonEinsumGeneratorBase : public Generator { // If the label is longer, use its first character (this may need // customization) if (label.empty()) { - throw std::runtime_error("Empty index label"); + throw Exception("Empty index label"); } // Return first character preserving case to avoid conflicts @@ -207,8 +207,7 @@ class PythonEinsumGeneratorBase : public Generator { void create(const Variable &variable, bool zero_init, const Context &ctx) override { if (!zero_init) { - throw std::runtime_error( - "Python variables must be initialized when created"); + throw Exception("Python variables must be initialized when created"); } m_generated += m_indent + represent(variable, ctx) + " = 0.0\n"; @@ -412,7 +411,7 @@ class PythonEinsumGeneratorBase : public Generator { if (found) { // Fallback: use numbered indices (not standard einsum, but necessary) - throw std::runtime_error("Too many unique indices for einsum notation"); + throw Exception("Too many unique indices for einsum notation"); } } @@ -525,7 +524,7 @@ class PythonEinsumGeneratorBase : public Generator { // For sums, we can't use a single einsum call // This should be handled at a higher level by generating multiple // statements - throw std::runtime_error( + throw Exception( "Sum expressions should be handled by generating multiple einsum " "calls"); } @@ -589,8 +588,7 @@ class PythonEinsumGeneratorBase : public Generator { return result; } - throw std::runtime_error( - "Unsupported expression type for Python scalar expression"); + throw Exception("Unsupported expression type for Python scalar expression"); } }; @@ -623,8 +621,7 @@ class NumPyEinsumGenerator void create(const Tensor &tensor, bool zero_init, const Context &ctx) override { if (!zero_init) { - throw std::runtime_error( - "Python tensors must be initialized when created"); + throw Exception("Python tensors must be initialized when created"); } m_generated += m_indent + represent(tensor, ctx) + " = " + module_prefix() + @@ -729,8 +726,7 @@ class PyTorchEinsumGenerator void create(const Tensor &tensor, bool zero_init, const Context &ctx) override { if (!zero_init) { - throw std::runtime_error( - "PyTorch tensors must be initialized when created"); + throw Exception("PyTorch tensors must be initialized when created"); } // PyTorch doesn't support the 'order' parameter - tensors are always diff --git a/SeQuant/core/export/text_generator.hpp b/SeQuant/core/export/text_generator.hpp index 8c46935407..c4f1bc9a1d 100644 --- a/SeQuant/core/export/text_generator.hpp +++ b/SeQuant/core/export/text_generator.hpp @@ -307,8 +307,7 @@ class TextGenerator : public Generator { return repr; } - throw std::runtime_error( - "Unsupported expression type in TextGenerator::compute"); + throw Exception("Unsupported expression type in TextGenerator::compute"); } }; diff --git a/SeQuant/core/expressions/abstract_tensor.cpp b/SeQuant/core/expressions/abstract_tensor.cpp index af255cc7e5..6aba8d267f 100644 --- a/SeQuant/core/expressions/abstract_tensor.cpp +++ b/SeQuant/core/expressions/abstract_tensor.cpp @@ -57,7 +57,7 @@ ExprPtr remove_tensor(const ExprPtr& expr, std::wstring label) { else if (expr->is() || expr->is()) return expr; else - throw std::runtime_error("Invalid Expr type in remove_tensor"); + throw Exception("Invalid Expr type in remove_tensor"); } } // namespace sequant diff --git a/SeQuant/core/expressions/constant.hpp b/SeQuant/core/expressions/constant.hpp index 33049ee3c7..f7aea74d04 100644 --- a/SeQuant/core/expressions/constant.hpp +++ b/SeQuant/core/expressions/constant.hpp @@ -50,7 +50,7 @@ class Constant : public Expr { /// @tparam T the result type; default to the type of value_ /// @return the value cast to ResultType - /// @throw std::invalid_argument if conversion to T is not possible + /// @throw Exception if conversion to T is not possible /// @throw boost::numeric::positive_overflow or /// boost::numeric::negative_overflow if cast fails template @@ -62,8 +62,7 @@ class Constant : public Expr { return T(numeric_cast(value_.real()), numeric_cast(value_.imag())); } else - throw std::invalid_argument( - "Constant::value: cannot convert value to type T"); + throw Exception("Constant::value: cannot convert value to type T"); } std::wstring to_latex() const override { @@ -81,7 +80,7 @@ class Constant : public Expr { if (that.is()) { value_ *= that.as().value(); } else { - throw std::logic_error("Constant::operator*=(that): not valid for that"); + throw Exception("Constant::operator*=(that): not valid for that"); } return *this; } @@ -90,7 +89,7 @@ class Constant : public Expr { if (that.is()) { value_ += that.as().value(); } else { - throw std::logic_error("Constant::operator+=(that): not valid for that"); + throw Exception("Constant::operator+=(that): not valid for that"); } return *this; } @@ -99,7 +98,7 @@ class Constant : public Expr { if (that.is()) { value_ -= that.as().value(); } else { - throw std::logic_error("Constant::operator-=(that): not valid for that"); + throw Exception("Constant::operator-=(that): not valid for that"); } return *this; } diff --git a/SeQuant/core/expressions/expr.cpp b/SeQuant/core/expressions/expr.cpp index 9ce13bb4de..2229dea1b6 100644 --- a/SeQuant/core/expressions/expr.cpp +++ b/SeQuant/core/expressions/expr.cpp @@ -106,12 +106,12 @@ std::size_t ExprPtr::size() const { return this->get()->size(); } std::wstring ExprPtr::to_latex() const { return as_shared_ptr()->to_latex(); } -std::logic_error Expr::not_implemented(const char *fn) const { +Exception Expr::not_implemented(const char *fn) const { std::ostringstream oss; oss << "Expr::" << fn << " not implemented in this derived class (type_name=" << type_name() << ")"; - return std::logic_error(oss.str().c_str()); + return Exception(oss.str()); } std::wstring Expr::to_latex() const { throw not_implemented("to_latex"); } diff --git a/SeQuant/core/expressions/expr.hpp b/SeQuant/core/expressions/expr.hpp index 6e568a5613..db1da6e275 100644 --- a/SeQuant/core/expressions/expr.hpp +++ b/SeQuant/core/expressions/expr.hpp @@ -337,25 +337,25 @@ class Expr : public std::enable_shared_from_this, /// @brief in-place multiply @c *this by @c that /// @return reference to @c *this - /// @throw std::logic_error if not implemented for this class, or cannot be + /// @throw Exception if not implemented for this class, or cannot be /// implemented for the particular @c that virtual Expr &operator*=(const Expr &that); /// @brief in-place non-commutatively-multiply @c *this by @c that /// @return reference to @c *this - /// @throw std::logic_error if not implemented for this class, or cannot be + /// @throw Exception if not implemented for this class, or cannot be /// implemented for the particular @c that virtual Expr &operator^=(const Expr &that); /// @brief in-place add @c that to @c *this /// @return reference to @c *this - /// @throw std::logic_error if not implemented for this class, or cannot be + /// @throw Exception if not implemented for this class, or cannot be /// implemented for the particular @c that virtual Expr &operator+=(const Expr &that); /// @brief in-place subtract @c that from @c *this /// @return reference to @c *this - /// @throw std::logic_error if not implemented for this class, or cannot be + /// @throw Exception if not implemented for this class, or cannot be /// implemented for the particular @c that virtual Expr &operator-=(const Expr &that); @@ -369,7 +369,7 @@ class Expr : public std::enable_shared_from_this, typename = std::enable_if_t, Expr>>> static bool visit_impl(E &&expr, Visitor &&visitor, const bool atoms_only) { if (expr.weak_from_this().use_count() == 0) - throw std::invalid_argument( + throw Exception( "Expr::visit: cannot visit expressions not managed by shared_ptr"); for (auto &subexpr_ptr : expr.expr()) { const auto subexpr_is_an_atom = subexpr_ptr->is_atom(); @@ -512,9 +512,9 @@ class Expr : public std::enable_shared_from_this, private: /// @input[in] fn the name of function that is missing in this class - /// @return a std::logic_error object containing a message describing that @p + /// @return an Exception object containing a message describing that @p /// fn is missing from this type - std::logic_error not_implemented(const char *fn) const; + Exception not_implemented(const char *fn) const; }; // class Expr template <> diff --git a/SeQuant/core/expressions/tensor.hpp b/SeQuant/core/expressions/tensor.hpp index b6aa1296b0..132609b5c8 100644 --- a/SeQuant/core/expressions/tensor.hpp +++ b/SeQuant/core/expressions/tensor.hpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -507,10 +506,10 @@ class Tensor : public Expr, public AbstractTensor, public MutatableLabeled { slots(), [](const Index &idx) { return idx.nonnull(); }); } /// @return number of indices in bra/ket - /// @throw std::logic_error if bra and ket ranks do not match + /// @throw Exception if bra and ket ranks do not match std::size_t rank() const { if (bra_rank() != ket_rank()) { - throw std::logic_error("Tensor::rank(): bra rank != ket rank"); + throw Exception("Tensor::rank(): bra rank != ket rank"); } return bra_rank(); } diff --git a/SeQuant/core/hugenholtz.hpp b/SeQuant/core/hugenholtz.hpp index 14a3f5207c..910b3acb9a 100644 --- a/SeQuant/core/hugenholtz.hpp +++ b/SeQuant/core/hugenholtz.hpp @@ -135,8 +135,7 @@ class HugenholtzVertex { void insert(const size_t edge_idx, const Edge& edge) { // preconditions if (edge_idx > edge_to_group_.size()) { - throw std::out_of_range( - "HugenholtzVertex::insert : can only insert or append"); + throw Exception("HugenholtzVertex::insert : can only insert or append"); } auto grp_it = std::find_if( diff --git a/SeQuant/core/index.hpp b/SeQuant/core/index.hpp index 26c1f1cd87..e874355623 100644 --- a/SeQuant/core/index.hpp +++ b/SeQuant/core/index.hpp @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -912,7 +911,7 @@ class Index : public Taggable { /// generated Index objects inline void check_nonreserved() const { if (ordinal_ && *ordinal_ >= min_tmp_index()) { - throw std::invalid_argument( + throw Exception( "Index ctor: ordinal must be less than the value returned by " "min_tmp_index()"); } diff --git a/SeQuant/core/index_space_registry.hpp b/SeQuant/core/index_space_registry.hpp index 65cae50190..b57d0c8eed 100644 --- a/SeQuant/core/index_space_registry.hpp +++ b/SeQuant/core/index_space_registry.hpp @@ -213,13 +213,13 @@ class IndexSpaceRegistry { /// @param type IndexSpace::Type /// @param qns IndexSpace::QuantumNumbers /// @return IndexSpace associated with that key. - /// @throw std::invalid_argument if matching space is not found + /// @throw Exception if matching space is not found const IndexSpace& retrieve(const IndexSpace::Type& type, const IndexSpace::QuantumNumbers& qns) const { if (const auto* ptr = retrieve_ptr(type, qns)) { return *ptr; } else - throw std::invalid_argument( + throw Exception( "IndexSpaceRegistry::retrieve(type,qn): missing { IndexSpace::Type=" + to_string(type) + " , IndexSpace::QuantumNumbers=" + to_string(qns) + " } combination"); @@ -240,14 +240,13 @@ class IndexSpaceRegistry { /// @brief retrieve an IndexSpace from the registry by the IndexSpace::Attr /// @param space_attr an IndexSpace::Attr /// @return IndexSpace associated with that key. - /// @throw std::invalid_argument if matching space is not found + /// @throw Exception if matching space is not found const IndexSpace& retrieve(const IndexSpace::Attr& space_attr) const { if (const auto* ptr = retrieve_ptr(space_attr)) { return *ptr; } else - throw std::invalid_argument( - std::string("IndexSpaceRegistry::retrieve(attr): attr=") + - to_string(space_attr) + " is missing"); + throw Exception(std::string("IndexSpaceRegistry::retrieve(attr): attr=") + + to_string(space_attr) + " is missing"); } /// queries presence of a registered IndexSpace @@ -288,12 +287,12 @@ class IndexSpaceRegistry { /// @brief add an IndexSpace to this registry. /// @param IS an IndexSpace /// @return reference to `this` - /// @throw std::invalid_argument if `IS.base_key()` or `IS.attr()` matches + /// @throw Exception if `IS.base_key()` or `IS.attr()` matches /// an already registered IndexSpace IndexSpaceRegistry& add(const IndexSpace& IS) { auto it = spaces_->find(IS.base_key()); if (it != spaces_->end()) { - throw std::invalid_argument( + throw Exception( (std::string("IndexSpaceRegistry::add(index_space): space with " "index_space.base_key()=") + toUtf8(IS.base_key()) + @@ -305,7 +304,7 @@ class IndexSpaceRegistry { // IS.attr() if (ranges::any_of(*spaces_, [&IS](auto&& is) { return IS.attr() == is.attr(); })) { - throw std::invalid_argument( + throw Exception( (std::string("IndexSpaceRegistry::add(index_space): space with " "index_space.attr()=") + to_string(IS.attr()) + @@ -330,7 +329,7 @@ class IndexSpaceRegistry { /// - any of { is_vacuum_occupied , is_reference_occupied , is_complete , /// is_hole , is_particle } /// @return reference to `this` - /// @throw std::invalid_argument if `type_label` or `type` matches + /// @throw Exception if `type_label` or `type` matches /// an already registered IndexSpace template IndexSpaceRegistry& add(S&& type_label, IndexSpace::Type type, @@ -405,7 +404,7 @@ class IndexSpaceRegistry { IndexSpace::Attr space_attr; long count = 0; if (components.size() <= 1) { - throw std::invalid_argument( + throw Exception( "IndexSpaceRegistry::add_unIon: must have at least two components"); } for (auto&& component : components) { @@ -473,7 +472,7 @@ class IndexSpaceRegistry { IndexSpace::Attr space_attr; long count = 0; if (components.size() <= 1) { - throw std::invalid_argument( + throw Exception( "IndexSpaceRegistry::add_intersection: must have at least two " "components"); } @@ -568,12 +567,12 @@ class IndexSpaceRegistry { template bool valid_intersection(S1&& space1_key, S2&& space2_key) const { if (!contains(space1_key)) - throw std::invalid_argument( + throw Exception( std::string("IndexSpaceRegistry::valid_intersection(s1,s2): space " "with key s1=") + toUtf8(space1_key) + " must be added to the registry first"); if (!contains(space2_key)) - throw std::invalid_argument( + throw Exception( std::string( "IndexSpaceRegistry::valid_intersection(s1,s2): space key s2=") + toUtf8(space2_key) + " must be added to the registry first"); @@ -607,7 +606,7 @@ class IndexSpaceRegistry { find_by_attr({intersection_type, space1.qns()}); // the nullspace is a reasonable return value for intersection if (intersection_space == IndexSpace::null && intersection_type) { - throw std::invalid_argument( + throw Exception( std::string("intersection(s1=") + to_string(space1) + ",s2=" + to_string(space2) + ": no space with resulting type=" + to_string(intersection_type) + @@ -627,12 +626,12 @@ class IndexSpaceRegistry { template const IndexSpace& intersection(S1&& space1_key, S2&& space2_key) const { if (!contains(space1_key)) - throw std::invalid_argument( + throw Exception( std::string( "IndexSpaceRegistry::intersection(s1,s2): space with key s1=") + toUtf8(space1_key) + " must be added to the registry first"); if (!contains(space2_key)) - throw std::invalid_argument( + throw Exception( std::string( "IndexSpaceRegistry::intersection(s1,s2): space key s2=") + toUtf8(space2_key) + " must be added to the registry first"); @@ -680,12 +679,12 @@ class IndexSpaceRegistry { template bool valid_unIon(S1&& space1_key, S2&& space2_key) const { if (!contains(space1_key)) - throw std::invalid_argument( + throw Exception( std::string( "IndexSpaceRegistry::valid_unIon(s1,s2): space with key s1=") + toUtf8(space1_key) + " must be added to the registry first"); if (!contains(space2_key)) - throw std::invalid_argument( + throw Exception( std::string("IndexSpaceRegistry::valid_unIon(s1,s2): space key s2=") + toUtf8(space2_key) + " must be added to the registry first"); return this->valid_unIon( @@ -707,11 +706,11 @@ class IndexSpaceRegistry { const IndexSpace& unIon(const IndexSpace& space1, const IndexSpace& space2) const { if (!contains(space1)) - throw std::invalid_argument( + throw Exception( std::string("IndexSpaceRegistry::valid_unIon(s1,s2): space s1=") + to_string(space1) + " must be added to the registry first"); if (!contains(space2)) - throw std::invalid_argument( + throw Exception( std::string("IndexSpaceRegistry::valid_unIon(s1,s2): space s2=") + to_string(space2) + " must be added to the registry first"); @@ -720,7 +719,7 @@ class IndexSpaceRegistry { } else { bool same_qns = space1.qns() == space2.qns(); if (!same_qns) { - throw std::invalid_argument( + throw Exception( std::string("IndexSpaceRegistry::valid_unIon(s1,s2): spaces s1=") + to_string(space1) + " and s2=" + to_string(space2) + " must have identical " @@ -729,7 +728,7 @@ class IndexSpaceRegistry { auto unIontype = space1.type().unIon(space2.type()); const IndexSpace& unIonSpace = find_by_attr({unIontype, space1.qns()}); if (unIonSpace == IndexSpace::null) { - throw std::invalid_argument( + throw Exception( std::string("IndexSpaceRegistry::valid_unIon(s1,s2), s1=") + to_string(space1) + " and s2=" + to_string(space2) + ": the result is not registered, must register first."); @@ -747,11 +746,11 @@ class IndexSpaceRegistry { template const IndexSpace& unIon(S1&& space1_key, S2&& space2_key) const { if (!contains(space1_key)) - throw std::invalid_argument( + throw Exception( std::string("IndexSpaceRegistry::unIon(s1,s2): space with key s1=") + toUtf8(space1_key) + " must be added to the registry first"); if (!contains(space2_key)) - throw std::invalid_argument( + throw Exception( std::string("IndexSpaceRegistry::unIon(s1,s2): space key s2=") + toUtf8(space2_key) + " must be added to the registry first"); return this->unIon(*(this->retrieve_ptr(std::forward(space1_key))), @@ -865,7 +864,7 @@ class IndexSpaceRegistry { template bool is_base(S&& space_key) const { if (!contains(space_key)) - throw std::invalid_argument( + throw Exception( std::string("IndexSpaceRegistry::is_base(s): space with key s=") + toUtf8(space_key) + " must be added to the registry first"); return this->is_base(*(this->retrieve_ptr(std::forward(space_key)))); @@ -904,7 +903,7 @@ class IndexSpaceRegistry { template bool is_pure_occupied(S&& space_key) const { if (!contains(space_key)) - throw std::invalid_argument( + throw Exception( std::string( "IndexSpaceRegistry::is_pure_occupied(s): space with key s=") + toUtf8(space_key) + " must be added to the registry first"); @@ -934,7 +933,7 @@ class IndexSpaceRegistry { template bool is_pure_unoccupied(S&& space_key) const { if (!contains(space_key)) - throw std::invalid_argument( + throw Exception( std::string( "IndexSpaceRegistry::is_pure_unoccupied(s): space with key s=") + toUtf8(space_key) + " must be added to the registry first"); @@ -955,7 +954,7 @@ class IndexSpaceRegistry { template bool contains_occupied(S&& space_key) const { if (!contains(space_key)) - throw std::invalid_argument( + throw Exception( std::string( "IndexSpaceRegistry::contains_occupied(s): space with key s=") + toUtf8(space_key) + " must be added to the registry first"); @@ -976,7 +975,7 @@ class IndexSpaceRegistry { template bool contains_unoccupied(S&& space_key) const { if (!contains(space_key)) - throw std::invalid_argument( + throw Exception( std::string( "IndexSpaceRegistry::contains_unoccupied(s): space with key s=") + toUtf8(space_key) + " must be added to the registry first"); @@ -1028,13 +1027,13 @@ class IndexSpaceRegistry { } /// @return the space occupied in vacuum state for any set of quantum numbers - /// @throw std::invalid_argument if @p nulltype_ok is false and + /// @throw Exception if @p nulltype_ok is false and /// vacuum_occupied_space had not been specified const IndexSpace::Type& vacuum_occupied_space( bool nulltype_ok = false) const { if (!std::get<0>(vacocc_)) { if (nulltype_ok) return IndexSpace::Type::null; - throw std::invalid_argument( + throw Exception( "vacuum occupied space has not been specified, invoke " "vacuum_occupied_space(IndexSpace::Type) or " "vacuum_occupied_space(container::map(refocc_)) { if (nulltype_ok) return IndexSpace::Type::null; - throw std::invalid_argument( + throw Exception( "reference occupied space has not been specified, invoke " "reference_occupied_space(IndexSpace::Type) or " "reference_occupied_space(container::map(complete_)) { if (nulltype_ok) return IndexSpace::Type::null; - throw std::invalid_argument( + throw Exception( "complete space has not been specified, call " "complete_space(IndexSpace::Type)"); } else @@ -1250,12 +1249,12 @@ class IndexSpaceRegistry { } /// @return default space in which holes can be created - /// @throw std::invalid_argument if @p nulltype_ok is false and + /// @throw Exception if @p nulltype_ok is false and /// hole_space had not been specified const IndexSpace::Type& hole_space(bool nulltype_ok = false) const { if (!std::get<0>(hole_space_)) { if (nulltype_ok) return IndexSpace::Type::null; - throw std::invalid_argument( + throw Exception( "active hole space has not been specified, invoke " "hole_space(IndexSpace::Type) or " "hole_space(container::map(particle_space_)) { if (nulltype_ok) return IndexSpace::Type::null; - throw std::invalid_argument( + throw Exception( "active particle space has not been specified, invoke " "particle_space(IndexSpace::Type) or " "particle_space(container::map greek_characters_to_string_impl( const Char ch = *it; if (sizeof(Char) == 1 && !is_ascii(ch)) - throw std::invalid_argument( + throw Exception( "greek_characters_to_string(str): currently only supports " "non-ASCII characters in str if Char is a wide character (wchar_t, " "char16_t, or char32_t)"); @@ -153,7 +153,7 @@ std::basic_string diactrics_to_string_impl( if (it + 1 != end) next_ch = *(it + 1); if (sizeof(Char) == 1 && ((it == begin && !is_ascii(ch)) || (next_ch && !is_ascii(*next_ch)))) { - throw std::invalid_argument( + throw Exception( "diactrics_to_string(str): currently only supports " "non-ASCII characters in str if Char is a wide character (wchar_t, " "char16_t, or char32_t)"); diff --git a/SeQuant/core/io/latex/latex.hpp b/SeQuant/core/io/latex/latex.hpp index dfba00b2ff..c09715588b 100644 --- a/SeQuant/core/io/latex/latex.hpp +++ b/SeQuant/core/io/latex/latex.hpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -115,7 +116,7 @@ std::basic_string greek_characters_to_string_impl( /// `[0x3B1,0x3C9]` /// and `[0391,03A9]`replaced with their LaTeX equivalents /// @warning if @p str contains non-ASCII characters `Char` must be `wchar_t` -/// @throw std::invalid_argument if @p Char is narrow and @p str contains +/// @throw Exception if @p Char is narrow and @p str contains /// non-ASCII characters template std::basic_string greek_characters_to_string( @@ -144,7 +145,7 @@ std::basic_string diactrics_to_string_impl( /// @param str input string /// @return with some diactrics replaced with their LaTeX equivalents /// @warning if @p str contains non-ASCII characters `Char` must be `wchar_t` -/// @throw std::invalid_argument if @p Char is narrow and @p str contains +/// @throw Exception if @p Char is narrow and @p str contains /// non-ASCII characters /// @note only some combined Unicode characters are currently supported template @@ -167,7 +168,7 @@ std::basic_string diactrics_to_string( /// @return @p str with some Unicode characters replaced by their LaTeX /// equivalents /// @warning if @p str contains non-ASCII characters `Char` must be `wchar_t` -/// @throw std::invalid_argument if @p Char is narrow and @p str contains +/// @throw Exception if @p Char is narrow and @p str contains /// non-ASCII characters /// @note only some combined Unicode characters are currently supported /// @internal useful resources diff --git a/SeQuant/core/io/serialization/v1/ast_conversions.hpp b/SeQuant/core/io/serialization/v1/ast_conversions.hpp index 02e23bb99d..cae45a75f4 100644 --- a/SeQuant/core/io/serialization/v1/ast_conversions.hpp +++ b/SeQuant/core/io/serialization/v1/ast_conversions.hpp @@ -52,7 +52,7 @@ Index to_index(const io::serialization::v1::ast::Index &index, throw SerializationError(offset, length, "Unknown index space '" + toUtf8(current.label) + "' in proto index specification"); - } catch (const std::invalid_argument &e) { + } catch (const Exception &e) { auto [offset, length] = get_pos(current, position_cache, begin); throw SerializationError(offset, length, "Invalid index '" + toUtf8(current.label) + "_" + @@ -71,7 +71,7 @@ Index to_index(const io::serialization::v1::ast::Index &index, "Unknown index space '" + toUtf8(index.label.label) + "' in index specification"); - } catch (const std::invalid_argument &e) { + } catch (const Exception &e) { auto [offset, length] = get_pos(index.label, position_cache, begin); throw SerializationError(offset, length, "Invalid index '" + toUtf8(index.label.label) + diff --git a/SeQuant/core/io/serialization/v1/serialize.cpp b/SeQuant/core/io/serialization/v1/serialize.cpp index 199a527acd..12d7f13ee7 100644 --- a/SeQuant/core/io/serialization/v1/serialize.cpp +++ b/SeQuant/core/io/serialization/v1/serialize.cpp @@ -11,7 +11,6 @@ #include #include -#include #include #include #include @@ -233,7 +232,7 @@ std::wstring to_string(const Expr& expr, const SerializationOptions& options) { else if (expr.is()) return details::to_string(expr.as(), options); else - throw std::runtime_error("Unsupported expr type for serialize!"); + throw Exception("Unsupported expr type for serialize!"); } std::wstring to_string(const ResultExpr& result, diff --git a/SeQuant/core/op.hpp b/SeQuant/core/op.hpp index 13169b71e8..c41749d0ce 100644 --- a/SeQuant/core/op.hpp +++ b/SeQuant/core/op.hpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -201,8 +200,7 @@ bool is_pure_qpcreator(const Op &op, op.action() == Action::Create); } case Vacuum::MultiProduct: - throw std::logic_error( - "is_pure_qpcreator: cannot handle MultiProduct vacuum"); + throw Exception("is_pure_qpcreator: cannot handle MultiProduct vacuum"); } SEQUANT_UNREACHABLE; @@ -224,8 +222,7 @@ bool is_qpcreator(const Op &op, (isr->contains_unoccupied(op.index().space()) && op.action() == Action::Create); case Vacuum::MultiProduct: - throw std::logic_error( - "is_qpcreator: cannot handle MultiProduct vacuum"); + throw Exception("is_qpcreator: cannot handle MultiProduct vacuum"); } } @@ -250,8 +247,7 @@ IndexSpace qpcreator_space( op.index().space(), isr->vacuum_unoccupied_space(op.index().space().qns())); case Vacuum::MultiProduct: - throw std::logic_error( - "qpcreator_space: cannot handle MultiProduct vacuum"); + throw Exception("qpcreator_space: cannot handle MultiProduct vacuum"); } SEQUANT_UNREACHABLE; @@ -274,7 +270,7 @@ bool is_pure_qpannihilator( op.action() == Action::Create); } case Vacuum::MultiProduct: - throw std::logic_error( + throw Exception( "is_pure_qpannihilator: cannot handle MultiProduct vacuum"); } @@ -298,8 +294,7 @@ bool is_qpannihilator(const Op &op, op.action() == Action::Annihilate); } case Vacuum::MultiProduct: - throw std::logic_error( - "is_qpannihilator: cannot handle MultiProduct vacuum"); + throw Exception("is_qpannihilator: cannot handle MultiProduct vacuum"); } SEQUANT_UNREACHABLE; @@ -323,8 +318,7 @@ IndexSpace qpannihilator_space( op.index().space(), isr->vacuum_unoccupied_space(op.index().space().qns())); case Vacuum::MultiProduct: - throw std::logic_error( - "qpcreator_space: cannot handle MultiProduct vacuum"); + throw Exception("qpcreator_space: cannot handle MultiProduct vacuum"); } SEQUANT_UNREACHABLE; @@ -593,12 +587,11 @@ class NormalOperator : public Operator, } /// @return number of creators/annihilators - /// @throw std::logic_error if the operator is not particle number conserving + /// @throw Exception if the operator is not particle number conserving /// (i.e. if ncreators() != nannihilators() ) auto rank() const { if (ncreators() != nannihilators()) { - throw std::logic_error( - "NormalOperator::rank(): ncreators != nannihilators"); + throw Exception("NormalOperator::rank(): ncreators != nannihilators"); } return ncreators(); } @@ -924,7 +917,7 @@ class NormalOperator : public Operator, void _permute_aux(std::span perm) override final { if (perm.size() != 0) - throw std::invalid_argument( + throw Exception( "NormalOperator::_permute_aux(p): there are no aux indices, p must " "be null"); } @@ -1052,7 +1045,7 @@ class NormalOperatorSequence : public container::svector>, }) == this->end(); if (!all_same_vaccum) { - throw std::invalid_argument( + throw Exception( "NormalOperatorSequence expects all constituent " "NormalOperator objects to use same vacuum"); } diff --git a/SeQuant/core/rational.hpp b/SeQuant/core/rational.hpp index 567d93d591..0cb9ce3ad3 100644 --- a/SeQuant/core/rational.hpp +++ b/SeQuant/core/rational.hpp @@ -2,6 +2,7 @@ #define SEQUANT_CORE_RATIONAL_H #include +#include #include #include @@ -58,9 +59,8 @@ inline rational to_rational( T t, T eps = std::sqrt(std::numeric_limits::epsilon()), std::size_t max_niter = 1000) { if (std::isnan(t) || std::isinf(t)) { - throw std::invalid_argument( - "sequant::to_rational: cannot make a rational out of " + - std::to_string(t)); + throw Exception("sequant::to_rational: cannot make a rational out of " + + std::to_string(t)); } // e.g. // https://gist.github.com/mikeando/7073d62385a34a61a6f7#file-main2-cpp-L42 @@ -93,10 +93,10 @@ inline rational to_rational( } ++niter; } - throw std::invalid_argument( - "sequant::rationalize: could not rationalize " + std::to_string(f) + - " to eps=" + std::to_string(tol) + " in " + std::to_string(max_niter) + - " iterations"); // unreachable + throw Exception("sequant::rationalize: could not rationalize " + + std::to_string(f) + " to eps=" + std::to_string(tol) + + " in " + std::to_string(max_niter) + + " iterations"); // unreachable }; if (eps == 0) // exact conversion ... rarely what's desired return rational(t); diff --git a/SeQuant/core/runtime.hpp b/SeQuant/core/runtime.hpp index 6275f57270..f3d62e1e6b 100644 --- a/SeQuant/core/runtime.hpp +++ b/SeQuant/core/runtime.hpp @@ -7,13 +7,13 @@ #include #include -#include #include #include #include #include #include +#include #ifdef SEQUANT_HAS_EXECUTION_HEADER #include @@ -41,8 +41,7 @@ inline int& nthreads_accessor() { /// sets the number of threads to use for concurrent work inline void set_num_threads(int nt) { - if (nt < 1) - throw std::invalid_argument("set_num_threads(nthreads): invalid nthreads"); + if (nt < 1) throw Exception("set_num_threads(nthreads): invalid nthreads"); detail::nthreads_accessor() = nt; } diff --git a/SeQuant/core/space.cpp b/SeQuant/core/space.cpp index 22fb718b23..773bfe6c92 100644 --- a/SeQuant/core/space.cpp +++ b/SeQuant/core/space.cpp @@ -14,7 +14,7 @@ IndexSpace retrieve(StrView label) { auto registry_ptr = get_default_context().index_space_registry(); if (!registry_ptr) { - throw std::runtime_error( + throw Exception( "Can't use IndexSpace(string) without an active index space registry"); } diff --git a/SeQuant/core/space.hpp b/SeQuant/core/space.hpp index e0d4891dea..e8d48540a5 100644 --- a/SeQuant/core/space.hpp +++ b/SeQuant/core/space.hpp @@ -376,11 +376,10 @@ class IndexSpace { /// exception type thrown when ancountered unknown/invalid /// IndexSpace::base_key() or Index::label() - struct bad_key : std::invalid_argument { - bad_key() : std::invalid_argument("bad key") {} + struct bad_key : Exception { + bad_key() : Exception("bad key") {} template - bad_key(S &&key) - : std::invalid_argument(std::string("bad key: ") + toUtf8(key)) {} + bad_key(S &&key) : Exception(std::string("bad key: ") + toUtf8(key)) {} }; struct KeyCompare { diff --git a/SeQuant/core/tensor_canonicalizer.cpp b/SeQuant/core/tensor_canonicalizer.cpp index 0ac6088929..523c4cbe8f 100644 --- a/SeQuant/core/tensor_canonicalizer.cpp +++ b/SeQuant/core/tensor_canonicalizer.cpp @@ -243,7 +243,7 @@ std::shared_ptr TensorCanonicalizer::instance( std::wstring_view label) { auto inst_ptr = instance_ptr(label); if (!inst_ptr) - throw std::runtime_error( + throw Exception( "must first register canonicalizer via " "TensorCanonicalizer::register_instance(...)"); return inst_ptr; diff --git a/SeQuant/core/tensor_canonicalizer.hpp b/SeQuant/core/tensor_canonicalizer.hpp index 55c9f8c96c..0156845426 100644 --- a/SeQuant/core/tensor_canonicalizer.hpp +++ b/SeQuant/core/tensor_canonicalizer.hpp @@ -42,7 +42,7 @@ class TensorCanonicalizer { /// @return a TensorCanonicalizer previously registered via /// TensorCanonicalizer::register_instance() with @c label or to the default /// canonicalizer - /// @throw std::runtime_error if no canonicalizer has been registered + /// @throw Exception if no canonicalizer has been registered static std::shared_ptr instance( std::wstring_view label = L""); diff --git a/SeQuant/core/tensor_network/v1.cpp b/SeQuant/core/tensor_network/v1.cpp index 8b6b8b95d9..48ed2bb50f 100644 --- a/SeQuant/core/tensor_network/v1.cpp +++ b/SeQuant/core/tensor_network/v1.cpp @@ -905,7 +905,7 @@ void TensorNetworkV1::init_edges() const { // add proto indices to the grand list of proto indices for (auto &&proto_idx : terminals.idx().proto_indices()) { if (proto_idx.has_proto_indices()) - throw std::runtime_error( + throw Exception( "TensorNetworkV1 does not support recursive protoindices"); // for // now // no diff --git a/SeQuant/core/tensor_network/v1.hpp b/SeQuant/core/tensor_network/v1.hpp index 2f839aed79..c7e74c592a 100644 --- a/SeQuant/core/tensor_network/v1.hpp +++ b/SeQuant/core/tensor_network/v1.hpp @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -112,7 +111,7 @@ class TensorNetworkV1 { switch (t.slot_type) { case TensorIndexSlotType::Aux: if (second_.slot_type != TensorIndexSlotType::Aux) { - throw std::logic_error( + throw Exception( "TensorNetworkV1::Edge::connect_to: aux slot cannot be " "connected to a non-aux slot"); } @@ -120,14 +119,14 @@ class TensorNetworkV1 { // - can connect bra slot to ket slot, and vice versa case TensorIndexSlotType::Bra: if (second_.slot_type != TensorIndexSlotType::Ket) { - throw std::logic_error( + throw Exception( "TensorNetworkV1::Edge::connect_to: bra slot can only be " "connected to a ket slot"); } break; case TensorIndexSlotType::Ket: if (second_.slot_type != TensorIndexSlotType::Bra) { - throw std::logic_error( + throw Exception( "TensorNetworkV1::Edge::connect_to: ket slot can only be " "connected to a bra slot"); } @@ -201,7 +200,7 @@ class TensorNetworkV1 { }; public: - /// @throw std::logic_error if exprptr_range contains a non-tensor + /// @throw Exception if exprptr_range contains a non-tensor /// @note uses RTTI template TensorNetworkV1(ExprPtrRange &exprptr_range) { @@ -213,7 +212,7 @@ class TensorNetworkV1 { tensors_.emplace_back(t); tensor_input_ordinals_.emplace_back(count++); } else { - throw std::logic_error( + throw Exception( "TensorNetworkV1::TensorNetworkV1: non-tensors in the given " "expression range"); } @@ -229,7 +228,7 @@ class TensorNetworkV1 { } } } - throw std::logic_error( + throw Exception( "TensorNetworkV1::TensorNetworkV1: non-tensors in the given expression " "range"); } diff --git a/SeQuant/core/tensor_network/v2.cpp b/SeQuant/core/tensor_network/v2.cpp index c2221827ed..088459b8f2 100644 --- a/SeQuant/core/tensor_network/v2.cpp +++ b/SeQuant/core/tensor_network/v2.cpp @@ -1170,7 +1170,7 @@ void TensorNetworkV2::init_edges() { for (auto &&proto_idx : current.idx().proto_indices()) { // for now no recursive proto indices if (proto_idx.has_proto_indices()) - throw std::runtime_error( + throw Exception( "TensorNetworkV2 does not support recursive protoindices"); proto_indices.emplace(proto_idx); } diff --git a/SeQuant/core/tensor_network/v2.hpp b/SeQuant/core/tensor_network/v2.hpp index 5003c1bf02..1a5697ef7a 100644 --- a/SeQuant/core/tensor_network/v2.hpp +++ b/SeQuant/core/tensor_network/v2.hpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -106,21 +105,21 @@ class TensorNetworkV2 { vertex.getOrigin() != Origin::Aux) || (first->getOrigin() != Origin::Aux && vertex.getOrigin() == Origin::Aux)) { - throw std::logic_error( + throw Exception( "TensorNetworkV2::Edge::connect_to: aux slot cannot be connected " "to a non-aux slot"); } // - can connect bra slot to ket slot, and vice versa if (first->getOrigin() == Origin::Bra && vertex.getOrigin() != Origin::Ket) { - throw std::logic_error( + throw Exception( "TensorNetworkV2::Edge::connect_to: bra slot can only be " "connected " "to a ket slot"); } if (first->getOrigin() == Origin::Ket && vertex.getOrigin() != Origin::Bra) { - throw std::logic_error( + throw Exception( "TensorNetworkV2::Edge::connect_to: ket slot can only be " "connected " "to a bra slot"); @@ -438,7 +437,7 @@ class TensorNetworkV2 { auto tensor_ptr = std::dynamic_pointer_cast(clone); if (!tensor_ptr) { - throw std::invalid_argument( + throw Exception( "TensorNetworkV2::TensorNetworkV2: tried to add non-tensor to " "network"); } diff --git a/SeQuant/core/tensor_network/v3.cpp b/SeQuant/core/tensor_network/v3.cpp index 75fb1a3613..5cca5e93ae 100644 --- a/SeQuant/core/tensor_network/v3.cpp +++ b/SeQuant/core/tensor_network/v3.cpp @@ -1454,7 +1454,7 @@ void TensorNetworkV3::init_edges() { for (auto &&proto_idx : current.idx().proto_indices()) { // for now no recursive proto indices if (proto_idx.has_proto_indices()) - throw std::runtime_error( + throw Exception( "TensorNetworkV3 does not support recursive protoindices"); proto_indices.emplace(proto_idx); } diff --git a/SeQuant/core/tensor_network/v3.hpp b/SeQuant/core/tensor_network/v3.hpp index 4009e00a85..86a1819675 100644 --- a/SeQuant/core/tensor_network/v3.hpp +++ b/SeQuant/core/tensor_network/v3.hpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -146,12 +145,12 @@ class TensorNetworkV3 { const Index *index = nullptr; /// @param vertex a vertex to be added - /// @throw std::invalid_argument if @p vertex is already connected by this + /// @throw Exception if @p vertex is already connected by this /// Edge void add_vertex(const Vertex &vertex) { auto [it, inserted] = this->vertices.emplace(vertex); if (!inserted) - throw std::invalid_argument( + throw Exception( "TensorNetworkV3::Edge::add_vertex(v): v is already connected by " "this Edge"); } @@ -468,7 +467,7 @@ class TensorNetworkV3 { auto tensor_ptr = std::dynamic_pointer_cast(clone); if (!tensor_ptr) { - throw std::invalid_argument( + throw Exception( "TensorNetworkV3::TensorNetworkV3: tried to add non-tensor to " "network"); } diff --git a/SeQuant/core/utility/expr.cpp b/SeQuant/core/utility/expr.cpp index eb4a84f7d5..f449bfd0ad 100644 --- a/SeQuant/core/utility/expr.cpp +++ b/SeQuant/core/utility/expr.cpp @@ -443,7 +443,7 @@ ExprPtr transform_expr(const ExprPtr &expr, } else if (term->is() || term->is()) { result->append(1, term->clone()); } else { - throw std::runtime_error("Invalid Expr type in transform_product"); + throw Exception("Invalid Expr type in transform_product"); } } result->scale(scaling_factor); @@ -466,7 +466,7 @@ ExprPtr transform_expr(const ExprPtr &expr, } return result; } else { - throw std::runtime_error("Invalid Expr type in transform_expr"); + throw Exception("Invalid Expr type in transform_expr"); } } @@ -532,7 +532,7 @@ std::optional pop_tensor(ExprPtr &expression, return tensor; } - throw std::runtime_error("Unhandled expression type in pop_tensor"); + throw Exception("Unhandled expression type in pop_tensor"); } } // namespace sequant diff --git a/SeQuant/core/utility/expr.hpp b/SeQuant/core/utility/expr.hpp index c21d61c97b..2f2f29e8c8 100644 --- a/SeQuant/core/utility/expr.hpp +++ b/SeQuant/core/utility/expr.hpp @@ -94,7 +94,7 @@ template > ExprPtr &replace(ExprPtr &expr, const ExprPtr &target, const ExprPtr &replacement, EqualityComparator cmp = {}) { if (!target->is_atom()) { - throw std::runtime_error( + throw Exception( "Replacement of composite expressions is not yet implemented"); } diff --git a/SeQuant/core/utility/indices.hpp b/SeQuant/core/utility/indices.hpp index 18e548dd8e..d9ff3115e5 100644 --- a/SeQuant/core/utility/indices.hpp +++ b/SeQuant/core/utility/indices.hpp @@ -240,7 +240,7 @@ IndexGroups get_unique_indices(const Expr& expr) { } else if (expr.is()) { return get_unique_indices(expr.as()); } else { - throw std::runtime_error( + throw Exception( "Encountered unsupported expression type in get_unique_indices"); } } diff --git a/SeQuant/core/utility/singleton.hpp b/SeQuant/core/utility/singleton.hpp index ffa97d55c5..306641e389 100644 --- a/SeQuant/core/utility/singleton.hpp +++ b/SeQuant/core/utility/singleton.hpp @@ -5,10 +5,11 @@ #ifndef SEQUANT_CORE_UTILITY_SINGLETON_HPP #define SEQUANT_CORE_UTILITY_SINGLETON_HPP +#include #include #include #include -#include + #include namespace sequant { @@ -51,7 +52,7 @@ class Singleton { public: /// @return reference to the instance - /// @throw std::logic_error if the instance has not been constructed (because + /// @throw Exception if the instance has not been constructed (because /// Derived is not default-constructible and set_instance() had not been /// called) static Derived& instance() { @@ -60,7 +61,7 @@ class Singleton { if constexpr (derived_is_default_constructible) { return set_default_instance(); } else - throw std::logic_error( + throw Exception( "sequant::Singleton: is not default-constructible and set_instance() " "has not been called"); } @@ -83,7 +84,7 @@ class Singleton { /// @tparam Args a parameter pack type /// @param args a parameter pack /// @return reference to the newly-created instance - /// @throw std::logic_error if the instance has already been constructed + /// @throw Exception if the instance has already been constructed template static Derived& set_instance(Args&&... args) { // WARNING: can't check constructibility since the ctor may be private @@ -92,7 +93,7 @@ class Singleton { // constructible with Args"); std::scoped_lock lock(instance_mutex()); if (instance_accessor() != nullptr) - throw std::logic_error( + throw Exception( "sequant::Singleton::set_instance: instance has already been " "constructed"); instance_accessor() = std::move( diff --git a/SeQuant/core/wick.hpp b/SeQuant/core/wick.hpp index 21340706bd..2c8e386147 100644 --- a/SeQuant/core/wick.hpp +++ b/SeQuant/core/wick.hpp @@ -107,7 +107,7 @@ class WickTheorem { /// spin-orbital normal-ordered operators By default compute() assumes /// spin-orbital operators. /// @param sf if true, will complete full contractions only. - /// @throw std::invalid_argument if @c sf does not match the contents of + /// @throw Exception if @c sf does not match the contents of /// get_default_context().spbasis() [[deprecated( "get_default_context().spbasis() should be used to specify spin-free " @@ -115,7 +115,7 @@ class WickTheorem { spinfree(bool sf) { if (!((sf && get_default_context(S).spbasis() == SPBasis::Spinfree) || (!sf && get_default_context(S).spbasis() == SPBasis::Spinor))) { - throw std::invalid_argument( + throw Exception( "WickTheorem::Spinfree(sf): sf must match the contents of " "get_default_context(S).spbasis() (N.B. WickTheorem::Spinfree() is " "deprecated, no longer should be used)"); @@ -144,7 +144,7 @@ class WickTheorem { /// Specifies the external indices; by default assume all indices are summed /// over /// @param external_indices external (nonsummed) indices - /// @throw std::logic_error if WickTheorem::set_external_indices or + /// @throw Exception if WickTheorem::set_external_indices or /// WickTheorem::compute had already been invoked template WickTheorem &set_external_indices(IndexContainer &&external_indices) { @@ -164,7 +164,7 @@ class WickTheorem { ss << L"WickTheorem::set_external_indices: " L"external index " + io::latex::to_string(Index(v)) + L" repeated"; - throw std::invalid_argument(toUtf8(ss.str())); + throw Exception(toUtf8(ss.str())); } }); } @@ -189,7 +189,7 @@ class WickTheorem { /// will not constrain connectivity /// @param op_index_pairs the list of pairs of op indices to be connected in /// the result - /// @throw std::invalid_argument if @p op_index_pairs contains duplicates + /// @throw Exception if @p op_index_pairs contains duplicates ///@{ /// @tparam IndexPairContainer a sequence of std::pair @@ -206,7 +206,7 @@ class WickTheorem { return false; }; if (has_duplicates(op_index_pairs)) { - throw std::invalid_argument( + throw Exception( "WickTheorem::set_nop_connections(arg): arg contains duplicates"); } @@ -217,12 +217,12 @@ class WickTheorem { decltype(op_index_pairs)>::value_type::first_type>; if (static_cast(opidx_pair.first) >= input_->size() || static_cast(opidx_pair.second) >= input_->size()) { - throw std::invalid_argument( + throw Exception( "WickTheorem::set_nop_connections: nop index out of range"); } if constexpr (signed_indices) { if (opidx_pair.first < 0 || opidx_pair.second < 0) { - throw std::invalid_argument( + throw Exception( "WickTheorem::set_nop_connections: nop index out of range"); } } @@ -410,7 +410,7 @@ class WickTheorem { /// Product, or a Sum /// @note the canonicalization method is controlled by the default Context /// @warning this is not reentrant, but is optionally threaded internally - /// @throw std::logic_error if input's vacuum does not match the current + /// @throw Exception if input's vacuum does not match the current /// context vacuum ExprPtr compute(bool count_only = false, bool skip_input_canonicalization = false); @@ -528,7 +528,7 @@ class WickTheorem { /// @pre @p expr has been expanded (i.e. cannot contain a Sum as a /// subexpression) /// @note protoindices of external indices are external - /// @throw std::invalid_argument if any of @p expr subexpressions is a Sum + /// @throw Exception if any of @p expr subexpressions is a Sum void extract_indices(const Expr &expr, bool force_external = false) const; /// upsizes `{nop,index}_topological_partition_`, filling new entries with @@ -562,7 +562,7 @@ class WickTheorem { input_ = nopseq; if (input_->vacuum() != get_default_context(S).vacuum()) - throw std::logic_error( + throw Exception( "WickTheorem::init_input(): input vacuum " "must match the default context vacuum"); @@ -614,7 +614,7 @@ class WickTheorem { !(get_default_context(S).vacuum() == Vacuum::Physical || (S == Statistics::FermiDirac && get_default_context(S).vacuum() == Vacuum::SingleProduct))) - throw std::logic_error( + throw Exception( "WickTheorem::compute: spinfree=true supported only for physical " "vacuum and for Fermi facuum"); diff --git a/SeQuant/core/wick.impl.hpp b/SeQuant/core/wick.impl.hpp index b7d8bba603..093722be64 100644 --- a/SeQuant/core/wick.impl.hpp +++ b/SeQuant/core/wick.impl.hpp @@ -753,7 +753,7 @@ ExprPtr WickTheorem::compute(const bool count_only, } else { ranges::find_if(summands, [this](const auto &summand) { if (summand.template is()) // summands must not be a Sum - throw std::invalid_argument( + throw Exception( "WickTheorem::compute(expr): expr is a Sum with one of " "the " "summands also a Sum, WickTheorem can only accept a fully " diff --git a/SeQuant/domain/mbpt/biorthogonalization.cpp b/SeQuant/domain/mbpt/biorthogonalization.cpp index 67d1a7adc0..20182fda1a 100644 --- a/SeQuant/domain/mbpt/biorthogonalization.cpp +++ b/SeQuant/domain/mbpt/biorthogonalization.cpp @@ -56,7 +56,7 @@ using ParticlePairings = container::svector; /// /// \return Vector of rational coefficients representing the first row /// -/// \throw std::runtime_error if n_particles is not in the range [1,5] +/// \throw Exception if n_particles is not in the range [1,5] // clang-format on std::vector hardcoded_biorthogonalizer_row( std::size_t n_particles) { @@ -127,7 +127,7 @@ std::vector hardcoded_biorthogonalizer_row( ratio(-13, 7560), ratio(37, 15120), ratio(-23, 30240)}; default: - throw std::runtime_error( + throw Exception( "hardcoded biorthogonal coefficients only available for ranks 1-5, " "requested rank is : " + std::to_string(n_particles)); @@ -323,7 +323,7 @@ ExprPtr create_expr_for(const ParticlePairings& ref_pairing, }); if (it == pairings.end()) { - throw std::runtime_error( + throw Exception( "Missing explicit expression for a required index pairing in " "biorthogonalization"); } diff --git a/SeQuant/domain/mbpt/op.cpp b/SeQuant/domain/mbpt/op.cpp index 34b4d21241..675eab9889 100644 --- a/SeQuant/domain/mbpt/op.cpp +++ b/SeQuant/domain/mbpt/op.cpp @@ -8,8 +8,6 @@ #include #include -#include - namespace sequant::mbpt { std::vector cardinal_tensor_labels() { @@ -273,7 +271,7 @@ qns_t combine(qns_t a, qns_t b) { } return result; } else { - throw std::runtime_error("Unsupported vacuum context."); + throw Exception("Unsupported vacuum context."); } } @@ -399,7 +397,7 @@ std::wstring to_latex(const mbpt::Operator& op) { if (!is_definite(nann_p) || !is_definite(ncre_h) || !is_definite(ncre_p) || !is_definite(nann_h)) { - throw std::invalid_argument( + throw Exception( "to_latex(const Operator& op): " "can only handle generic operators with definite cre/ann numbers"); } @@ -1205,7 +1203,7 @@ bool can_change_qns(const ExprPtr& op_or_op_product, const qns_t& target_qns, qns = combine(qnc, qns); // apply the operator qnc on the source qns return qns.overlaps_with(target_qns); } else - throw std::invalid_argument( + throw Exception( "sequant::mbpt::sr::contains_rank(op_or_op_product): op_or_op_product " "must be mbpt::sr::op_t or Product thereof"); } @@ -1250,7 +1248,7 @@ ExprPtr expectation_value_impl(ExprPtr expr, // N.B. reference < vacuum is not yet supported if (isr->reference_occupied_space().intersection( isr->vacuum_occupied_space()) != isr->vacuum_occupied_space()) { - throw std::invalid_argument( + throw Exception( "mbpt::tensor::expectation_value_impl: vacuum occupied orbitals must " "be same as or " "subset of the reference orbital set."); diff --git a/SeQuant/domain/mbpt/op.hpp b/SeQuant/domain/mbpt/op.hpp index 91a387080b..855a29fa6f 100644 --- a/SeQuant/domain/mbpt/op.hpp +++ b/SeQuant/domain/mbpt/op.hpp @@ -41,7 +41,6 @@ #include #include #include -#include #include #include #include @@ -216,7 +215,7 @@ class QuantumNumberChange SEQUANT_ASSERT(isr_base_spaces.size() > 0); return isr_base_spaces.size() * 2; } else { - throw std::logic_error("unknown Vacuum type"); + throw Exception("unknown Vacuum type"); } } diff --git a/SeQuant/domain/mbpt/op_registry.cpp b/SeQuant/domain/mbpt/op_registry.cpp index 1f2dec7ac3..62357c491d 100644 --- a/SeQuant/domain/mbpt/op_registry.cpp +++ b/SeQuant/domain/mbpt/op_registry.cpp @@ -2,6 +2,7 @@ // Created by Ajay Melekamburath on 12/14/25. // +#include #include #include @@ -10,12 +11,12 @@ namespace sequant::mbpt { void OpRegistry::validate_op(const std::wstring& op) const { if (!reserved::is_nonreserved(op)) { - throw std::runtime_error("mbpt::OpRegistry::add: operator " + toUtf8(op) + - " uses a reserved label"); + throw Exception("mbpt::OpRegistry::add: operator " + toUtf8(op) + + " uses a reserved label"); } if (this->contains(op)) { - throw std::runtime_error("mbpt::OpRegistry::add: operator " + toUtf8(op) + - " already exists in registry"); + throw Exception("mbpt::OpRegistry::add: operator " + toUtf8(op) + + " already exists in registry"); } } @@ -37,8 +38,8 @@ OpRegistry& OpRegistry::add(const std::wstring& op, OpClass action) { OpRegistry& OpRegistry::remove(const std::wstring& op) { if (!this->contains(op)) { - throw std::runtime_error("mbpt::OpRegistry::remove: operator " + - toUtf8(op) + " does not exist in registry"); + throw Exception("mbpt::OpRegistry::remove: operator " + toUtf8(op) + + " does not exist in registry"); } ops_->erase(op); return *this; @@ -51,8 +52,8 @@ bool OpRegistry::contains(const std::wstring& op) const { OpClass OpRegistry::to_class(const std::wstring& op) const { auto it = ops_->find(op); if (it == ops_->end()) { - throw std::runtime_error("mbpt::OpRegistry::to_class: operator " + - toUtf8(op) + " does not exist in registry"); + throw Exception("mbpt::OpRegistry::to_class: operator " + toUtf8(op) + + " does not exist in registry"); } return it->second; } diff --git a/SeQuant/domain/mbpt/spin.cpp b/SeQuant/domain/mbpt/spin.cpp index b282e5fe18..3a25559246 100644 --- a/SeQuant/domain/mbpt/spin.cpp +++ b/SeQuant/domain/mbpt/spin.cpp @@ -41,7 +41,6 @@ #include #include #include -#include #include #include #include @@ -248,8 +247,8 @@ ExprPtr swap_bra_ket(const ExprPtr& expr) { } else if (term->is() || term->is()) { result->append(1, term); } else { - throw std::runtime_error("Invalid Expr type in product_swap: " + - term->type_name()); + throw Exception("Invalid Expr type in product_swap: " + + term->type_name()); } } return result; @@ -266,8 +265,7 @@ ExprPtr swap_bra_ket(const ExprPtr& expr) { } return result; } else { - throw std::runtime_error("Invalid Expr type in swap_bra_ket: " + - expr->type_name()); + throw Exception("Invalid Expr type in swap_bra_ket: " + expr->type_name()); } } @@ -288,7 +286,7 @@ ExprPtr append_spin(const ExprPtr& expr, } else if (term->is() || term->is()) { spin_product->append(1, term); } else { - throw std::runtime_error( + throw Exception( "Invalid Expr type in append_spin::add_spin_to_product: " + term->type_name()); } @@ -310,7 +308,7 @@ ExprPtr append_spin(const ExprPtr& expr, return expr; } - throw std::runtime_error("Unsupported Expr type in append_spin"); + throw Exception("Unsupported Expr type in append_spin"); } ExprPtr remove_spin(const ExprPtr& expr) { @@ -337,7 +335,7 @@ ExprPtr remove_spin(const ExprPtr& expr) { } else if (term->is() || term->is()) { result->append(1, term); } else { - throw std::runtime_error( + throw Exception( "Invalid Expr type in remove_spin::remove_spin_from_product: " + term->type_name()); } @@ -358,8 +356,7 @@ ExprPtr remove_spin(const ExprPtr& expr) { } else if (expr->is() || expr->is()) { return expr; } else { - throw std::runtime_error("Invalid Expr type in remove_spin: " + - expr->type_name()); + throw Exception("Invalid Expr type in remove_spin: " + expr->type_name()); } } @@ -498,7 +495,7 @@ ExprPtr expand_antisymm(const ExprPtr& expr, bool skip_spinsymm) { } else if (term->is() || term->is()) { temp.append(1, term, Product::Flatten::No); } else { - throw std::runtime_error( + throw Exception( "Invalid Expr type in expand_antisymm::expand_product: " + term->type_name()); } @@ -517,8 +514,8 @@ ExprPtr expand_antisymm(const ExprPtr& expr, bool skip_spinsymm) { } return result; } else { - throw std::runtime_error("Invalid Expr type in expand_antisymm: " + - expr->type_name()); + throw Exception("Invalid Expr type in expand_antisymm: " + + expr->type_name()); } } @@ -701,8 +698,8 @@ ExprPtr symmetrize_expr(const ProductPtr& product) { } else if (term->is() || term->is()) { new_product.append(1, term); } else { - throw std::runtime_error("Invalid Expr type in symmetrize_expr: " + - term->type_name()); + throw Exception("Invalid Expr type in symmetrize_expr: " + + term->type_name()); } } result->append(ex(new_product)); @@ -723,8 +720,8 @@ ExprPtr symmetrize_expr(const ExprPtr& expr) { } return result; } else { - throw std::runtime_error("Invalid Expr type in symmetrize_expr: " + - expr->type_name()); + throw Exception("Invalid Expr type in symmetrize_expr: " + + expr->type_name()); } } @@ -742,8 +739,7 @@ ExprPtr expand_A_op(const ExprPtr& expr) { return result; } - throw std::runtime_error("Invalid Expr type in expand_A_op: " + - expr->type_name()); + throw Exception("Invalid Expr type in expand_A_op: " + expr->type_name()); } container::svector> P_maps(const Tensor& P) { @@ -807,8 +803,8 @@ ExprPtr expand_P_op(const ProductPtr& product) { } else if (term->is() || term->is()) { new_product->append(1, term); } else { - throw std::runtime_error("Invalid Expr type in expand_P_op: " + - term->type_name()); + throw Exception("Invalid Expr type in expand_P_op: " + + term->type_name()); } } result->append(new_product); @@ -829,8 +825,7 @@ ExprPtr expand_P_op(const ExprPtr& expr) { } return result; } else { - throw std::runtime_error("Invalid Expr type in expand_P_op: " + - expr->type_name()); + throw Exception("Invalid Expr type in expand_P_op: " + expr->type_name()); } } @@ -1062,8 +1057,8 @@ ExprPtr closed_shell_spintrace_impl(const ExprPtr& expression, } return result; } else { - throw std::runtime_error("Invalid Expr type in closed_shell_spintrace: " + - expr->type_name()); + throw Exception("Invalid Expr type in closed_shell_spintrace: " + + expr->type_name()); } } @@ -1205,8 +1200,8 @@ ExprPtr swap_spin(const ExprPtr& expr) { } else if (t->is() || t->is()) { result.append(1, t, Product::Flatten::No); } else { - throw std::runtime_error( - "Invalid Expr type in swap_spin::swap_product: " + t->type_name()); + throw Exception("Invalid Expr type in swap_spin::swap_product: " + + t->type_name()); } } return ex(result); @@ -1223,8 +1218,7 @@ ExprPtr swap_spin(const ExprPtr& expr) { } return ex(result); } else { - throw std::runtime_error("Invalid Expr type in swap_spin: " + - expr->type_name()); + throw Exception("Invalid Expr type in swap_spin: " + expr->type_name()); } } @@ -1483,7 +1477,7 @@ std::vector open_shell_spintrace_impl( cBra.insert(cBra.end(), tnsr.bra().begin(), tnsr.bra().end()); cKet.insert(cKet.end(), tnsr.ket().begin(), tnsr.ket().end()); } else if (term->is() || term->is()) { - throw std::runtime_error( + throw Exception( "Nested Product and Sum not supported in spin_symm_product"); } } @@ -1676,7 +1670,7 @@ ExprPtr spintrace_impl(const ExprPtr& expression, IdxGroups&& ext_index_groups, return ex(0); } } else if (expr.is() || expr.is()) { - throw std::runtime_error( + throw Exception( "Nested sums/products not supported in spin_trace_product"); } } @@ -1690,7 +1684,7 @@ ExprPtr spintrace_impl(const ExprPtr& expression, IdxGroups&& ext_index_groups, } else { // Would need some sort of recursion but it is not clear how that would // interact with other code in here yet so prefer to error instead. - throw std::runtime_error( + throw Exception( "spin_trace_product: Nested products or sums inside of a Product " "not supported (yet)"); } @@ -1833,8 +1827,7 @@ ExprPtr spintrace_impl(const ExprPtr& expression, IdxGroups&& ext_index_groups, } return result; } else { - throw std::runtime_error("Invalid Expr type in spintrace: " + - expr->type_name()); + throw Exception("Invalid Expr type in spintrace: " + expr->type_name()); } detail::reset_idx_tags(result); diff --git a/SeQuant/domain/mbpt/utils.cpp b/SeQuant/domain/mbpt/utils.cpp index 4750af0f2b..5969e5e2af 100644 --- a/SeQuant/domain/mbpt/utils.cpp +++ b/SeQuant/domain/mbpt/utils.cpp @@ -78,7 +78,7 @@ ExprPtr lst(ExprPtr A, ExprPtr B, size_t commutator_rank, LSTOptions options) { } else if (A.is() || A.is()) return A; else - throw std::invalid_argument( + throw Exception( "mbpt::lst(A, B, commutator_rank, options): Unsupported expression " "type"); } @@ -89,7 +89,7 @@ ExprPtr screen_vac_av(ExprPtr expr, bool skip_clone) { auto screen = [](const ExprPtr& term) { if (!(term->is() || term->is())) { - throw std::invalid_argument("op::screen_terms: Unsupported term type"); + throw Exception("op::screen_terms: Unsupported term type"); } return op::can_change_qns(term, qns_t{}) ? term : ex(0); }; @@ -117,8 +117,7 @@ ExprPtr screen_vac_av(ExprPtr expr, bool skip_clone) { }); return result; } else - throw std::invalid_argument( - "mbpt::screen_terms(expr): Unsupported expression type"); + throw Exception("mbpt::screen_terms(expr): Unsupported expression type"); } } // namespace sequant::mbpt diff --git a/SeQuant/domain/mbpt/vac_av.cpp b/SeQuant/domain/mbpt/vac_av.cpp index 7a533967be..8483bda83e 100644 --- a/SeQuant/domain/mbpt/vac_av.cpp +++ b/SeQuant/domain/mbpt/vac_av.cpp @@ -139,7 +139,7 @@ ExprPtr expectation_value_impl( } else if (expr.is() || expr.is()) { return expr; // vacuum is normalized } - throw std::invalid_argument( + throw Exception( "mbpt::op::detail::expectation_value_impl(expr): unknown expression " "type"); } diff --git a/tests/unit/test_expr.cpp b/tests/unit/test_expr.cpp index 65767d82ea..a457a6e2f3 100644 --- a/tests/unit/test_expr.cpp +++ b/tests/unit/test_expr.cpp @@ -259,7 +259,7 @@ TEST_CASE("expr", "[elements]") { REQUIRE(ex->value() == 2); REQUIRE(ex->value() == 2); REQUIRE(ex->value>() == std::complex{2, 0}); - REQUIRE_THROWS_AS(ex->value(), std::invalid_argument); + REQUIRE_THROWS_AS(ex->value(), Exception); // sequant::rational is convertible to bool REQUIRE_NOTHROW(ex->value()); REQUIRE_THROWS_AS(std::make_shared(-2)->value(), @@ -287,7 +287,7 @@ TEST_CASE("expr", "[elements]") { SECTION("adjoint") { { // not implemented by default const auto e = std::make_shared(); - REQUIRE_THROWS_AS(e->adjoint(), std::logic_error); + REQUIRE_THROWS_AS(e->adjoint(), Exception); } { // implemented in Adjointable const auto e = std::make_shared(); diff --git a/tests/unit/test_latex.cpp b/tests/unit/test_latex.cpp index d44bd8225b..370e16a689 100644 --- a/tests/unit/test_latex.cpp +++ b/tests/unit/test_latex.cpp @@ -19,7 +19,7 @@ TEST_CASE("latex", "[util]") { using namespace std::string_literals; REQUIRE(io::latex::greek_characters_to_string("alpha"s) == "alpha"); REQUIRE_THROWS_AS(io::latex::greek_characters_to_string("α"s) == "\\alpha", - std::invalid_argument); + Exception); REQUIRE(io::latex::greek_characters_to_string(std::wstring(L"alpha")) == L"alpha"); @@ -36,10 +36,10 @@ TEST_CASE("latex", "[util]") { u8"alpha"); REQUIRE_THROWS_AS(io::latex::greek_characters_to_string( std::u8string(u8"α")) == u8"\\alpha", - std::invalid_argument); + Exception); REQUIRE_THROWS_AS(io::latex::greek_characters_to_string( std::u8string(u8"Γ")) == u8"\\Gamma", - std::invalid_argument); + Exception); REQUIRE(io::latex::greek_characters_to_string(std::u16string(u"alpha")) == u"alpha"); diff --git a/tests/unit/test_math.cpp b/tests/unit/test_math.cpp index 14ad4ed0cc..dc234835e8 100644 --- a/tests/unit/test_math.cpp +++ b/tests/unit/test_math.cpp @@ -36,7 +36,7 @@ TEST_CASE("math", "[elements]") { REQUIRE(to_rational(1. / 7) == rational{1, 7}); REQUIRE(to_rational(M_PI) == rational{99023, 31520}); REQUIRE(to_rational(M_E) == rational{23225, 8544}); - REQUIRE_THROWS_AS(to_rational(std::nan("NaN")), std::invalid_argument); + REQUIRE_THROWS_AS(to_rational(std::nan("NaN")), Exception); } } diff --git a/tests/unit/test_tensor_network.cpp b/tests/unit/test_tensor_network.cpp index 5805dc5666..40898104da 100644 --- a/tests/unit/test_tensor_network.cpp +++ b/tests/unit/test_tensor_network.cpp @@ -377,7 +377,7 @@ TEST_CASE("tensor_network", "[elements]") { REQUIRE_NOTHROW(TN(*t1_x_t2)); auto t1_x_t2_p_t2 = t1 * (t2 + t2); // can only use a flat tensor product - REQUIRE_THROWS_AS(TN(*t1_x_t2_p_t2), std::logic_error); + REQUIRE_THROWS_AS(TN(*t1_x_t2_p_t2), Exception); } { // with NormalOperators @@ -882,7 +882,7 @@ TEST_CASE("tensor_network_v2", "[elements][valgrind_skip]") { Edge e5(v8, &dummy); e5.connect_to(v6); Edge e6(v8, &dummy); - REQUIRE_THROWS_AS(e6.connect_to(v7), std::logic_error); + REQUIRE_THROWS_AS(e6.connect_to(v7), Exception); // Due to tensor symmetries, these edges are considered equal REQUIRE(e1 == e2); @@ -913,12 +913,12 @@ TEST_CASE("tensor_network_v2", "[elements][valgrind_skip]") { REQUIRE_NOTHROW(TensorNetworkV2(*t1_x_t2)); auto t1_x_t2_p_t2 = t1 * (t2 + t2); // can only use a flat tensor product - REQUIRE_THROWS_AS(TensorNetworkV2(*t1_x_t2_p_t2), std::logic_error); + REQUIRE_THROWS_AS(TensorNetworkV2(*t1_x_t2_p_t2), Exception); // must be covariant: no bra to bra or ket to ket t2->adjoint(); auto t1_x_t2_adjoint = t1 * t2; - REQUIRE_THROWS_AS(TensorNetworkV2(t1_x_t2_adjoint), std::logic_error); + REQUIRE_THROWS_AS(TensorNetworkV2(t1_x_t2_adjoint), Exception); } { // with NormalOperators @@ -1470,8 +1470,8 @@ TEST_CASE("tensor_network_v3", "[elements][valgrind_skip]") { Edge e4({v4, v6}, &dummy); // can't connect same vertex more than once - REQUIRE_THROWS_AS(Edge({v4, v6, v6}), std::invalid_argument); - REQUIRE_THROWS_AS(e4.connect_to(v6), std::invalid_argument); + REQUIRE_THROWS_AS(Edge({v4, v6, v6}), Exception); + REQUIRE_THROWS_AS(e4.connect_to(v6), Exception); Edge e5(v8, &dummy); e5.connect_to(v6); @@ -1530,7 +1530,7 @@ TEST_CASE("tensor_network_v3", "[elements][valgrind_skip]") { REQUIRE_NOTHROW(TN(*t1_x_t2)); auto t1_x_t2_p_t2 = t1 * (t2 + t2); // can only use a flat tensor product - REQUIRE_THROWS_AS(TN(*t1_x_t2_p_t2), std::invalid_argument); + REQUIRE_THROWS_AS(TN(*t1_x_t2_p_t2), Exception); // must be covariant: no bra to bra or ket to ket if (sequant::assert_behavior() == sequant::AssertBehavior::Throw) { diff --git a/tests/unit/test_utilities.cpp b/tests/unit/test_utilities.cpp index 6fca97c58b..761e5f89b4 100644 --- a/tests/unit/test_utilities.cpp +++ b/tests/unit/test_utilities.cpp @@ -204,7 +204,7 @@ TEST_CASE("utilities", "[utilities]") { for (auto&& thr : threads) thr.join(); for (auto result : thread_results) CHECK(result == 0); CHECK_THROWS_AS(Singleton>::set_instance(1), - std::logic_error); + sequant::Exception); CHECK(Singleton>::instance().s() == 0); } // non-default-constructible Singleton @@ -216,7 +216,7 @@ TEST_CASE("utilities", "[utilities]") { threads.emplace_back([&result = thread_results[t]]() { try { Singleton>::instance().s(); - } catch (std::logic_error&) { + } catch (sequant::Exception&) { result = 0; return; } catch (...) { diff --git a/tests/unit/test_wick.cpp b/tests/unit/test_wick.cpp index e5770f1883..5022b18f2f 100644 --- a/tests/unit/test_wick.cpp +++ b/tests/unit/test_wick.cpp @@ -141,11 +141,11 @@ TEST_CASE("wick", "[algorithms][wick][valgrind_skip]") { if (get_default_context().spbasis() == SPBasis::Spinor) { REQUIRE_NOTHROW(wick1.spinfree(false)); - REQUIRE_THROWS_AS(wick1.spinfree(true), std::invalid_argument); + REQUIRE_THROWS_AS(wick1.spinfree(true), Exception); } if (get_default_context().spbasis() == SPBasis::Spinfree) { REQUIRE_NOTHROW(wick1.spinfree(true)); - REQUIRE_THROWS_AS(wick1.spinfree(false), std::invalid_argument); + REQUIRE_THROWS_AS(wick1.spinfree(false), Exception); } SEQUANT_PRAGMA_GCC(diagnostic pop)