Add python api to gromacs build
[alexxy/gromacs.git] / src / python / sip / definitions.sip
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2011,2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35
36 %Exception std::out_of_range(SIP_IndexError) {
37
38 %TypeHeaderCode
39 #include <stdexcept>
40 %End
41
42 %RaiseCode
43     SIP_BLOCK_THREADS
44     PyErr_SetString(PyExc_IndexError, sipExceptionRef.what());
45     SIP_UNBLOCK_THREADS
46 %End
47 };
48
49 template<T> class RealVector /NoDefaultCtors/ {
50 %TypeHeaderCode
51 #include "vec.h"
52 %End
53
54 public:
55     T x;
56     T y;
57     T z;
58     T operator[] (unsigned int i) throw (std::out_of_range);
59     PyObject* __str__();
60     %MethodCode
61         size_t size = PyOS_snprintf(NULL, 0, "Vector [%f, %f, %f]", sipCpp->x, sipCpp->y, sipCpp->z);
62         char str[size + 1];
63         PyOS_snprintf(str, size + 1, "Vector [%f, %f, %f]", sipCpp->x, sipCpp->y, sipCpp->z);
64
65         return PyUnicode_FromString(str);
66     %End
67 };
68
69 template<T, Adaptor, Inner> class Array /NoDefaultCtors/ {
70 %TypeHeaderCode
71 #include "vec.h"
72 %End
73
74 public:
75     Adaptor operator[] (unsigned int i) throw (std::out_of_range);
76     SIP_SSIZE_T __len__();
77     %MethodCode
78         sipRes = sipCpp->len();
79     %End
80 };
81
82 class Matrix /NoDefaultCtors/ {
83 %TypeHeaderCode
84 #include "vec.h"
85 %End
86
87 public:
88     py_rvec __getitem__(int) throw (std::out_of_range);
89     %MethodCode
90         try {
91             sipRes = new py_rvec(sipCpp->get(a0));
92         } catch (const std::out_of_range &e) {
93             sipIsErr = 1;
94             SIP_BLOCK_THREADS
95             PyErr_SetString(PyExc_IndexError, e.what());
96             SIP_UNBLOCK_THREADS
97         }
98     %End
99     double __getitem__(SIP_PYTUPLE) throw (std::out_of_range);
100     %MethodCode
101         size_t i, j;
102         if (PyArg_ParseTuple(a0, "nn", &i, &j))
103             try {
104                 sipRes = sipCpp->get(i, j);
105             } catch (const std::out_of_range &e) {
106                 sipIsErr = 1;
107                 SIP_BLOCK_THREADS
108                 PyErr_SetString(PyExc_IndexError, e.what());
109                 SIP_UNBLOCK_THREADS
110             }
111         else
112             sipIsErr = 1;
113     %End
114 };
115
116 typedef double* rvec;
117 typedef double* dvec;
118 typedef double** matrix;
119 typedef double** tensor;
120 typedef int* ivec;
121 typedef int** imatrix;
122
123 typedef RealVector<double> py_rvec;
124 typedef Array<double, py_rvec, rvec> rvecs;