MADNESS  version 0.9
tensorexcept.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 
36 #ifndef MADNESS_TENSOR_TENSOREXCPT_H__INCLUDED
37 #define MADNESS_TENSOR_TENSOREXCPT_H__INCLUDED
38 
41 
43 
44 #include <iosfwd>
45 #include <exception>
47 
48 namespace madness {
50  class TensorException : public std::exception {
51  const char* msg;
52  const char* assertion;
53  int value;
54  BaseTensor t;
55  const BaseTensor *tp;
56  int line;
57  const char *function;
58  const char *filename;
59 
60 public:
61  // Capturing the line/function/filename info is best done with the macros below
62  TensorException(const char* s, const char *a, int err, const BaseTensor* tp,
63  int lin, const char *func, const char *file)
64  : msg(s)
65  , assertion(a)
66  , value(err)
67  , tp(tp)
68  , line(lin)
69  , function(func)
70  , filename(file) {
71  // We must copy the pointed-to tensor because during unwinding it
72  // might go out of scope and dereferencing its pointer is invalid
73  if (tp != NULL)
74  new(&t) BaseTensor(*tp);
75  }
76 
77  virtual const char* what() const throw() {
78  return msg;
79  }
80 
81  virtual ~TensorException() throw() {}
82 
84  friend std::ostream& operator <<(std::ostream& out, const TensorException& e) {
85  out << "TensorException: msg='";
86  if (e.msg) out << e.msg;
87  out << "'\n";
88  if (e.assertion) out << " failed assertion='" <<
89  e.assertion << "'\n";
90  out << " value=" << e.value << "\n";
91  if (e.line) out << " line=" << e.line << "\n";
92  if (e.function) out << " function='" <<
93  e.function << "'\n";
94  if (e.filename) out << " filename='" <<
95  e.filename << "'\n";
96  if (e.tp != NULL) {
97  out << " tensor=Tensor<";
98  if (e.t.id()>=0 && e.t.id()<=TENSOR_MAX_TYPE_ID) {
99  out << tensor_type_names[e.t.id()] << ">(";
100  }
101  else {
102  out << "invalid_type_id>(";
103  }
104  if (e.t.ndim()>=0 && e.t.ndim()<TENSOR_MAXDIM) {
105  for (int i=0; i<e.t.ndim(); ++i) {
106  out << e.t.dim(i);
107  if (i != (e.t.ndim()-1)) out << ",";
108  }
109  out << ")";
110  }
111  else {
112  out << "invalid_dimensions)";
113  }
114  out << " at 0x" << (void *)(e.tp) << "\n";
115  }
116 
117  return out;
118  }
119 
120  };
121 
122 
123 #define TENSOR_STRINGIZE(X) #X
124 #define TENSOR_EXCEPTION_AT(F, L) TENSOR_STRINGIZE(F) "(" TENSOR_STRINGIZE(L) ")"
125 
126 #define TENSOR_EXCEPTION(msg,value,t) \
127  throw ::madness::TensorException("TENSOR EXCEPTION: " TENSOR_EXCEPTION_AT( __FILE__, __LINE__ ) ": " msg , \
128  0,value,t,__LINE__,__FUNCTION__,__FILE__)
129 
130 #define TENSOR_ASSERT(condition,msg,value,t) \
131 do {if (!(condition)) \
132  throw ::madness::TensorException("TENSOR ASSERTION FAILED: " TENSOR_EXCEPTION_AT( __FILE__, __LINE__ ) ": " msg , \
133  #condition,value,t,__LINE__,__FUNCTION__,__FILE__); \
134  } while (0)
135 
136 }
137 
138 #endif // MADNESS_TENSOR_TENSOREXCPT_H__INCLUDED
std::complex< double > func(int n, int t1, int t2, int t3, double xx, double yy, double zz)
Definition: wannier.cc:98
long ndim() const
Returns the number of dimensions in the tensor.
Definition: basetensor.h:144
long dim(int i) const
Returns the size of dmension i.
Definition: basetensor.h:147
virtual const char * what() const
Definition: tensorexcept.h:77
long id() const
Returns the typeid of the tensor (c.f., TensorTypeData )
Definition: basetensor.h:141
The base class for tensors defines generic capabilities.
Definition: basetensor.h:85
FLOAT a(int j, FLOAT z)
Definition: y1.cc:86
#define TENSOR_MAXDIM
Definition: tensor_macros.h:194
friend std::ostream & operator<<(std::ostream &out, const TensorException &e)
Print a TensorException to the stream (for human consumption)
Definition: tensorexcept.h:84
const char * tensor_type_names[]
TensorException(const char *s, const char *a, int err, const BaseTensor *tp, int lin, const char *func, const char *file)
Definition: tensorexcept.h:62
Declares BaseTensor.
Tensor is intended to throw only TensorExceptions.
Definition: tensorexcept.h:50
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
virtual ~TensorException()
Definition: tensorexcept.h:81
Function< complexd, 3 > function
Definition: envelopedpulse.h:45
#define TENSOR_MAX_TYPE_ID
Definition: type_data.h:119