Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tsoc
openmm
Commits
87ce9ccd
Commit
87ce9ccd
authored
Oct 27, 2015
by
Robert McGibbon
Browse files
Clean up some of the swig typemaps
parent
381e8145
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
47 deletions
+66
-47
wrappers/python/src/swig_doxygen/swig_lib/python/typemaps.i
wrappers/python/src/swig_doxygen/swig_lib/python/typemaps.i
+51
-47
wrappers/python/tests/TestSwigWrappers.py
wrappers/python/tests/TestSwigWrappers.py
+15
-0
No files found.
wrappers/python/src/swig_doxygen/swig_lib/python/typemaps.i
View file @
87ce9ccd
%
fragment
(
"Py_StripOpenMMUnits"
,
"header"
)
{
%
fragment
(
"Vec3_to_PyVec3"
,
"header"
)
{
/**
* Convert an OpenMM::Vec3 into a Python simtk.openmm.Vec3 object
*
* Returns a new reference.
*/
PyObject
*
Vec3_to_PyVec3
(
const
OpenMM
::
Vec3&
v
)
{
static
PyObject
*
__s_mm
=
NULL
;
static
PyObject
*
__s_Vec3
=
NULL
;
if
(
__s_mm
==
NULL
)
{
__s_mm
=
PyImport_AddModule
(
"simtk.openmm"
)
;
__s_Vec3
=
PyObject_GetAttrString
(
__s_mm
,
"Vec3"
)
;
}
PyObject
*
tuple
=
Py_BuildValue
(
"(d,d,d)"
,
v
[
0
],
v
[
1
],
v
[
2
])
;
PyObject
*
PyVec3
=
PyObject_CallObject
(
__s_Vec3
,
tuple
)
;
Py_DECREF
(
tuple
)
;
return
PyVec3
;
}
}
static
PyObject
*
__s_Quantity
=
NULL
;
%
fragment
(
"Py_StripOpenMMUnits"
,
"header"
)
{
static
PyObject
*
__s_md_unit_system_tuple
=
NULL
;
static
PyObject
*
__s_bar_tuple
=
NULL
;
/**
* Strip any OpenMM units of an input PyObject.
*
* This is equivalent to the following Python code
*
* >>> from simtk import unit
* >>> if isinstance(input, unit.Quantity)
* ... if input.is_compatible(unit.bar)
* ... return input.value_in_unit(unit.bar)
* ... return input.value_in_unit_system(unit.md_input_system)
* ... return input
*
* Returns a new reference.
*/
PyObject
*
Py_StripOpenMMUnits
(
PyObject
*
input
)
{
PyObject
*
Py_StripOpenMMUnits
(
PyObject
*
input
)
{
static
PyObject
*
__s_Quantity
=
NULL
;
static
PyObject
*
__s_md_unit_system_tuple
=
NULL
;
static
PyObject
*
__s_bar_tuple
=
NULL
;
if
(
__s_Quantity
==
NULL
)
{
if
(
__s_Quantity
==
NULL
)
{
PyObject
*
module
=
NULL
;
PyObject
*
module
=
NULL
;
module
=
PyImport_ImportModule
(
"simtk.unit"
)
;
module
=
PyImport_ImportModule
(
"simtk.unit"
)
;
...
@@ -412,22 +446,14 @@ int Py_SequenceToVecVecVecDouble(PyObject* obj, std::vector<std::vector<std::vec
...
@@ -412,22 +446,14 @@ int Py_SequenceToVecVecVecDouble(PyObject* obj, std::vector<std::vector<std::vec
}
}
%
typemap
(
argout
)
std
::
vector
<
Vec3
>
&
{
%
typemap
(
argout
,
fragment
=
"Vec3_to_PyVec3"
)
std
::
vector
<
Vec3
>
&
{
int
i
,
n
;
int
n
=
(
*
$
1
)
.
size
()
;
PyObject
*
pyList
;
PyObject
*
pyList
=
PyList_New
(
n
)
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
n
=
(
*
$
1
)
.
size
()
;
pyList
=
PyList_New
(
n
)
;
PyObject
*
mm
=
PyImport_AddModule
(
"simtk.openmm"
)
;
// borrowed ref.
PyObject
*
vec3
=
PyObject_GetAttrString
(
mm
,
"Vec3"
)
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
OpenMM
::
Vec3&
v
=
(
*
$
1
)
.
at
(
i
)
;
OpenMM
::
Vec3&
v
=
(
*
$
1
)
.
at
(
i
)
;
PyObject
*
args
=
Py_BuildValue
(
"(d,d,d)"
,
v
[
0
],
v
[
1
],
v
[
2
])
;
PyObject
*
pyVec
=
Vec3_to_PyVec3
(
v
)
;
PyObject
*
pyVec
=
PyObject_CallObject
(
vec3
,
args
)
;
Py_DECREF
(
args
)
;
PyList_SET_ITEM
(
pyList
,
i
,
pyVec
)
;
PyList_SET_ITEM
(
pyList
,
i
,
pyVec
)
;
}
}
Py_DECREF
(
vec3
)
;
$
result
=
pyList
;
$
result
=
pyList
;
}
}
...
@@ -437,42 +463,20 @@ int Py_SequenceToVecVecVecDouble(PyObject* obj, std::vector<std::vector<std::vec
...
@@ -437,42 +463,20 @@ int Py_SequenceToVecVecVecDouble(PyObject* obj, std::vector<std::vector<std::vec
}
}
%
typemap
(
out
,
fragment
=
"Vec3_to_PyVec3"
)
Vec3
{
$
result
=
Vec3_to_PyVec3
(
*
$
1
)
;
%
typemap
(
out
)
Vec3
{
PyObject
*
mm
=
PyImport_AddModule
(
"simtk.openmm"
)
;
// borrowed ref
PyObject
*
vec3
=
PyObject_GetAttrString
(
mm
,
"Vec3"
)
;
PyObject
*
args
=
Py_BuildValue
(
"(d,d,d)"
,
(
$
1
)[
0
],
(
$
1
)[
1
],
(
$
1
)[
2
])
;
$
result
=
PyObject_CallObject
(
vec3
,
args
)
;
Py_DECREF
(
vec3
)
;
Py_DECREF
(
args
)
;
}
}
%
typemap
(
out
)
const
Vec3&
{
%
typemap
(
out
,
fragment
=
"Vec3_to_PyVec3"
)
const
Vec3&
{
PyObject
*
mm
=
PyImport_AddModule
(
"simtk.openmm"
)
;
// borrowed ref
$
result
=
Vec3_to_PyVec3
(
*
$
1
)
;
PyObject
*
vec3
=
PyObject_GetAttrString
(
mm
,
"Vec3"
)
;
PyObject
*
args
=
Py_BuildValue
(
"(d,d,d)"
,
(
*
$
1
)[
0
],
(
*
$
1
)[
1
],
(
*
$
1
)[
2
])
;
$
result
=
PyObject_CallObject
(
vec3
,
args
)
;
Py_DECREF
(
vec3
)
;
Py_DECREF
(
args
)
;
}
}
/* Convert C++ (Vec3&, Vec3&, Vec3&) object to python tuple or tuples */
/* Convert C++ (Vec3&, Vec3&, Vec3&) object to python tuple or tuples */
%
typemap
(
argout
)
(
Vec3&
a
,
Vec3&
b
,
Vec3&
c
)
{
%
typemap
(
argout
,
fragment
=
"Vec3_to_PyVec3"
)
(
Vec3&
a
,
Vec3&
b
,
Vec3&
c
)
{
// %typemap(argout) (Vec3& a, Vec3& b, Vec3& c)
PyObject
*
pyVec1
=
Vec3_to_PyVec3
(
*
$
1
)
;
PyObject
*
mm
=
PyImport_AddModule
(
"simtk.openmm"
)
;
// borrowed ref
PyObject
*
pyVec2
=
Vec3_to_PyVec3
(
*
$
2
)
;
PyObject
*
vec3
=
PyObject_GetAttrString
(
mm
,
"Vec3"
)
;
PyObject
*
pyVec3
=
Vec3_to_PyVec3
(
*
$
3
)
;
PyObject
*
args1
=
Py_BuildValue
(
"(d,d,d)"
,
(
*
$
1
)[
0
],
(
*
$
1
)[
1
],
(
*
$
1
)[
2
])
;
PyObject
*
args2
=
Py_BuildValue
(
"(d,d,d)"
,
(
*
$
2
)[
0
],
(
*
$
2
)[
1
],
(
*
$
2
)[
2
])
;
PyObject
*
args3
=
Py_BuildValue
(
"(d,d,d)"
,
(
*
$
3
)[
0
],
(
*
$
3
)[
1
],
(
*
$
3
)[
2
])
;
PyObject
*
pyVec1
=
PyObject_CallObject
(
vec3
,
args1
)
;
PyObject
*
pyVec2
=
PyObject_CallObject
(
vec3
,
args2
)
;
PyObject
*
pyVec3
=
PyObject_CallObject
(
vec3
,
args3
)
;
Py_DECREF
(
args1
)
;
Py_DECREF
(
args2
)
;
Py_DECREF
(
args3
)
;
Py_DECREF
(
vec3
)
;
PyObject
*
o
,
*
o2
,
*
o3
;
PyObject
*
o
,
*
o2
,
*
o3
;
o
=
Py_BuildValue
(
"[N, N, N]"
,
pyVec1
,
pyVec2
,
pyVec3
)
;
o
=
Py_BuildValue
(
"[N, N, N]"
,
pyVec1
,
pyVec2
,
pyVec3
)
;
if
((
!
$
result
)
||
(
$
result
==
Py_None
))
{
if
((
!
$
result
)
||
(
$
result
==
Py_None
))
{
...
...
wrappers/python/tests/TestSwigWrappers.py
0 → 100644
View file @
87ce9ccd
import
unittest
import
simtk.openmm
as
mm
class
TestSwigWrappers
(
unittest
.
TestCase
):
def
test_1
(
self
):
# This tests for a refcounting bug in the swig wrappers
# that was previously problematic.
# See https://github.com/pandegroup/openmm/issues/1214
for
cycle
in
range
(
10
):
system
=
mm
.
System
()
system
.
getDefaultPeriodicBoxVectors
()
if
__name__
==
'__main__'
:
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment