1 // -*- C++ -*-
2 // Potential.cpp: Common functionality for all potentials.
3 //
4 // Copyright (C) 2001-2012 Jakob Schiotz and Center for Individual
5 // Nanoparticle Functionality, Department of Physics, Technical
6 // University of Denmark.  Email: schiotz@fysik.dtu.dk
7 //
8 // This file is part of Asap version 3.
9 //
10 // This program is free software: you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public License
12 // version 3 as published by the Free Software Foundation.  Permission
13 // to use other versions of the GNU Lesser General Public License may
14 // granted by Jakob Schiotz or the head of department of the
15 // Department of Physics, Technical University of Denmark, as
16 // described in section 14 of the GNU General Public License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // and the GNU Lesser Public License along with this program.  If not,
25 // see <http://www.gnu.org/licenses/>.
26 
27 #include "AsapPython.h"
28 #include "Asap.h"
29 #include "Atoms.h"
30 #include "Potential.h"
31 #include "Debug.h"
32 
33 // Standard mapping of the six independent parts of the stress tensor to
34 // vector notation
35 const static int stresscomp[3][3] = {{0, 5, 4}, {5, 1, 3}, {4, 3, 2}};
36 
RecoverAfterException()37 void Potential::RecoverAfterException()
38 {
39   DEBUGPRINT;
40   if (atoms != NULL && atoms->IsActive())
41     atoms->End();
42 }
43 
44 
GetVirial(PyObject * a)45 SymTensor Potential::GetVirial(PyObject *a)
46 {
47   DEBUGPRINT;
48   SymTensor result;
49   for (int i = 0; i < 6; i++)
50     result[i] = 0;
51   const vector<SymTensor> &virials = GetVirials(a);
52   for (int i = 0; i < virials.size(); i++)
53     result += virials[i];
54   DEBUGPRINT;
55   return result;
56 }
57 
58 
59 #ifdef ASAP_FOR_KIM
60 // When building as an OpenKIM model, there is no Python layer to pass through.
SetAtoms_ThroughPython(PyObject * pyatoms,Atoms * accessobj)61 void Potential::SetAtoms_ThroughPython(PyObject *pyatoms, Atoms* accessobj /* = NULL */)
62 {
63   SetAtoms(pyatoms, accessobj);
64 }
65 #else
66 // The normal version
SetAtoms_ThroughPython(PyObject * pyatoms,Atoms * accessobj)67 void Potential::SetAtoms_ThroughPython(PyObject *pyatoms, Atoms* accessobj /* = NULL */)
68 {
69   DEBUGPRINT;
70   // Call self.set_atoms(...) as implemented in Python
71   PyObject *py_accessobj;
72   if (accessobj == NULL)
73     {
74       py_accessobj = Py_None;
75       Py_INCREF(Py_None);
76     }
77   else
78     {
79 #if PY_VERSION_HEX < 0x02070000
80     py_accessobj = PyCObject_FromVoidPtr(accessobj, NULL);
81 #else
82     py_accessobj = PyCapsule_New(accessobj, "asap3.accessobj", NULL);
83 #endif
84     }
85   if (py_accessobj == NULL)
86     throw AsapPythonError();
87   PyObject *method = PyAsapString_FromString("set_atoms");
88   if (method == NULL)
89     throw AsapPythonError();
90   PyObject *result = PyObject_CallMethodObjArgs(self, method, pyatoms, py_accessobj, NULL);
91   bool error = (result == NULL);
92   Py_XDECREF(result);
93   Py_DECREF(method);
94   Py_DECREF(py_accessobj);
95   if (error)
96     throw AsapPythonError();
97   DEBUGPRINT;
98 }
99 #endif
100