openmmexampleplugin icon indicating copy to clipboard operation
openmmexampleplugin copied to clipboard

Question about vector<Vec3> in swig interface file

Open ajsilveira opened this issue 4 years ago • 5 comments

I am planning to write a new force that uses the RMSD between molecules and I will use the OpenMM RMSDForce as a base. The first thing I did was to include in the plugin the OpenMM RMSDForce, which I named MyRMSDForce for now. I am getting the following error when creating MyRMSDForce:

File "/home/silveira/miniconda3/lib/python3.6/site-packages/exampleplugin.py", line 394, in __init__
_exampleplugin.MyRMSDForce_swiginit(self, _exampleplugin.new_MyRMSDForce(*args))
TypeError: Wrong number or type of arguments for overloaded function 'new_MyRMSDForce'.
Possible C/C++ prototypes are:
ExamplePlugin::MyRMSDForce::MyRMSDForce(std::vector< OpenMM::Vec3,std::allocator< OpenMM::Vec3 > > const &,std::vector< int,std::allocator< int > > const &)
ExamplePlugin::MyRMSDForce::MyRMSDForce(std::vector< OpenMM::Vec3,std::allocator< OpenMM::Vec3 > > const &)

I think that the issue is related to the swig interface file Should I add something in exampleplugin.i to handle <vector>Vec3? Thanks!

ajsilveira avatar Sep 16 '20 19:09 ajsilveira

That's correct, you need to provide a typemap for it. if you %include the typemaps.i file from the OpenMM source it should have everything you need: https://github.com/openmm/openmm/blob/master/wrappers/python/src/swig_doxygen/swig_lib/python/typemaps.i

peastman avatar Sep 16 '20 20:09 peastman

It already has the line %include "swig/typemaps.i" as I included MyRMSDForce in the old exampleplugin.i file. I thought that Vec3 would require something similar to:


/*
 * The following lines are needed to handle std::vector.
 * Similar lines may be needed for vectors of vectors or
 * for other STL types like maps.
 */

%include "std_vector.i"
namespace std {
  %template(vectord) vector<double>;
  %template(vectori) vector<int>;

Do you think the error could be somewhere else? This is the repo: https://github.com/ajsilveira/openmm_rocsalt_plugin Thanks!

ajsilveira avatar Sep 16 '20 20:09 ajsilveira

That's a different file. swig/typemaps.i is a file that comes with SWIG. I'm talking about a file that's included in the OpenMM source code, and defines typemaps for OpenMM classes like Vec3.

peastman avatar Sep 16 '20 20:09 peastman

I copied the file wrappers/python/src/swig_doxygen/swig_lib/python/typemaps.i to the python directory in openmmexampleplugin and added the %include line as follows:

%module exampleplugin
%include "typemaps.i"
%import(module="simtk.openmm") "swig/OpenMMSwigHeaders.i"
%include "swig/typemaps.i"

I am still getting the same error.

ajsilveira avatar Sep 16 '20 21:09 ajsilveira

I got RMSDForce working as plugin and codes can be found here.

ndonyapour avatar Apr 24 '23 21:04 ndonyapour