1 // Copyright (C) 2008-2011 Jakob Schiotz and Center for Individual
2 // Nanoparticle Functionality, Department of Physics, Technical
3 // University of Denmark.  Email: schiotz@fysik.dtu.dk
4 //
5 // This file is part of Asap version 3.
6 // Asap is released under the GNU Lesser Public License (LGPL) version 3.
7 // However, the parts of Asap distributed within the OpenKIM project
8 // (including this file) are also released under the Common Development
9 // and Distribution License (CDDL) version 1.0.
10 //
11 // This program is free software: you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public License
13 // version 3 as published by the Free Software Foundation.  Permission
14 // to use other versions of the GNU Lesser General Public License may
15 // granted by Jakob Schiotz or the head of department of the
16 // Department of Physics, Technical University of Denmark, as
17 // described in section 14 of the GNU General Public License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // and the GNU Lesser Public License along with this program.  If not,
26 // see <http://www.gnu.org/licenses/>.
27 
28 #include "AsapPython.h"
29 #include "Exception.h"
30 #include <iostream>
31 #include "Debug.h"
32 #ifdef _OPENMP
33 #include <omp.h>
34 #endif
35 
AsapError(const char * m)36 AsapError::AsapError(const char *m) : message()
37 {
38   message << m;
39 }
40 
AsapError(const AsapError & ex)41 AsapError::AsapError(const AsapError& ex) : message()
42 {
43   message << ex.GetMessage();
44 }
45 
46 
GetMessage() const47 string AsapError::GetMessage() const
48 {
49   return message.str();
50 }
51 
52 #ifndef ASAP_FOR_KIM
AsapNotImplementedError(const char * m)53 AsapNotImplementedError::AsapNotImplementedError(const char *m)
54 {
55   // We need to raise a PropertyNotImplementedError available in the asap3
56   // Python module, either defined there or imported from ASE.
57   PyObject *asap = PyImport_ImportModule("asap3");
58   if (asap == NULL)
59   {
60     PyErr_SetString(PyExc_RuntimeError, "Failed to make a PropertyNotImplementedError (module loading)");
61     return;
62   }
63   PyObject *PropertyNIE = PyObject_GetAttrString(asap, "PropertyNotImplementedError");
64   if (PropertyNIE == NULL)
65   {
66     Py_DECREF(asap);
67     PyErr_SetString(PyExc_RuntimeError, "Failed to make a PropertyNotImplementedError (attribute)");
68     return;
69   }
70   PyErr_SetString(PropertyNIE, m);
71   Py_DECREF(PropertyNIE);
72   Py_DECREF(asap);
73 }
74 #endif // not ASAP_FOR_KIM
75 
AssertionFailed(const char * expression,const char * file,int line,const char * func)76 AssertionFailed::AssertionFailed(const char *expression, const char *file,
77 				 int line, const char *func)
78   : message()
79 {
80   DEBUGPRINT;
81   message << file << ":" << line << ": ";
82   if (func)
83     message << func << ": ";
84   message << "Assertion '" << expression << "' failed.";
85   std::cerr << message.str() << std::endl;
86   DEBUGPRINT;
87 }
88 
AssertionFailed(const AssertionFailed & ex)89 AssertionFailed::AssertionFailed(const AssertionFailed& ex)
90   : message()
91 {
92   DEBUGPRINT;
93   message << ex.GetMessage();
94 }
95 
GetMessage() const96 string AssertionFailed::GetMessage() const
97 {
98   DEBUGPRINT;
99   return message.str();
100 }
101 
102 #ifdef _OPENMP
103 
104 namespace ASAPSPACE {
105 
AsapThrowHelper(const AsapError & err)106 void AsapThrowHelper(const AsapError &err)
107 {
108 #pragma omp critical
109   {
110     if(AsapGlobalException == NULL)
111       {
112         AsapGlobalException = new AsapError(err);
113         std::cerr << "\n\nASAP EXCEPTION IN OPEN-MP CODE:" << std::endl;
114         std::cerr << err.GetMessage() << std::endl;
115         std::cerr << "ATTEMPTING TO PROPAGATE EXCEPTION (May fail due to OpenMP)" << std::endl;
116       }
117   }
118 }
119 
AsapThrowHelper(const AsapPythonError & err)120 void AsapThrowHelper(const AsapPythonError &err)
121 {
122 #pragma omp critical
123   {
124     if(AsapGlobalException == NULL)
125       {
126         AsapGlobalException = new AsapPythonError(err);
127         std::cerr << "\n\nASAP EXCEPTION IN OPEN-MP CODE:" << std::endl;
128         std::cerr << "  -  a Python error occurred! " << std::endl;
129         std::cerr << "ATTEMPTING TO PROPAGATE EXCEPTION (May fail due to OpenMP)" << std::endl;
130       }
131   }
132 }
133 
134 } // end namespace
135 
136 #endif // _OPENMP
137