#include "OpenMM.h" #include "OpenMMCWrapper.h" #include <cstring> #include <vector> using namespace OpenMM; using namespace std; extern "C" { /* OpenMM_Vec3 */ OPENMM_EXPORT OpenMM_Vec3 OpenMM_Vec3_scale(const OpenMM_Vec3 vec, double scale) { OpenMM_Vec3 result = {vec.x*scale, vec.y*scale, vec.z*scale}; return result; } /* OpenMM_Vec3Array */ OPENMM_EXPORT OpenMM_Vec3Array* OpenMM_Vec3Array_create(int size) { return reinterpret_cast<OpenMM_Vec3Array*>(new vector<Vec3>(size)); } OPENMM_EXPORT void OpenMM_Vec3Array_destroy(OpenMM_Vec3Array* array) { delete reinterpret_cast<vector<Vec3>*>(array); } OPENMM_EXPORT int OpenMM_Vec3Array_getSize(const OpenMM_Vec3Array* array) { return reinterpret_cast<const vector<Vec3>*>(array)->size(); } OPENMM_EXPORT void OpenMM_Vec3Array_resize(OpenMM_Vec3Array* array, int size) { reinterpret_cast<vector<Vec3>*>(array)->resize(size); } OPENMM_EXPORT void OpenMM_Vec3Array_append(OpenMM_Vec3Array* array, const OpenMM_Vec3 vec) { reinterpret_cast<vector<Vec3>*>(array)->push_back(Vec3(vec.x, vec.y, vec.z)); } OPENMM_EXPORT void OpenMM_Vec3Array_set(OpenMM_Vec3Array* array, int index, const OpenMM_Vec3 vec) { (*reinterpret_cast<vector<Vec3>*>(array))[index] = Vec3(vec.x, vec.y, vec.z); } OPENMM_EXPORT const OpenMM_Vec3* OpenMM_Vec3Array_get(const OpenMM_Vec3Array* array, int index) { return reinterpret_cast<const OpenMM_Vec3*>((&(*reinterpret_cast<const vector<Vec3>*>(array))[index])); } /* OpenMM_StringArray */ OPENMM_EXPORT OpenMM_StringArray* OpenMM_StringArray_create(int size) { return reinterpret_cast<OpenMM_StringArray*>(new vector<string>(size)); } OPENMM_EXPORT void OpenMM_StringArray_destroy(OpenMM_StringArray* array) { delete reinterpret_cast<vector<string>*>(array); } OPENMM_EXPORT int OpenMM_StringArray_getSize(const OpenMM_StringArray* array) { return reinterpret_cast<const vector<string>*>(array)->size(); } OPENMM_EXPORT void OpenMM_StringArray_resize(OpenMM_StringArray* array, int size) { reinterpret_cast<vector<string>*>(array)->resize(size); } OPENMM_EXPORT void OpenMM_StringArray_append(OpenMM_StringArray* array, const char* str) { reinterpret_cast<vector<string>*>(array)->push_back(string(str)); } OPENMM_EXPORT void OpenMM_StringArray_set(OpenMM_StringArray* array, int index, const char* str) { (*reinterpret_cast<vector<string>*>(array))[index] = string(str); } OPENMM_EXPORT const char* OpenMM_StringArray_get(const OpenMM_StringArray* array, int index) { return (*reinterpret_cast<const vector<string>*>(array))[index].c_str(); } /* OpenMM_BondArray */ OPENMM_EXPORT OpenMM_BondArray* OpenMM_BondArray_create(int size) { return reinterpret_cast<OpenMM_BondArray*>(new vector<pair<int, int> >(size)); } OPENMM_EXPORT void OpenMM_BondArray_destroy(OpenMM_BondArray* array) { delete reinterpret_cast<vector<pair<int, int> >*>(array); } OPENMM_EXPORT int OpenMM_BondArray_getSize(const OpenMM_BondArray* array) { return reinterpret_cast<const vector<pair<int, int> >*>(array)->size(); } OPENMM_EXPORT void OpenMM_BondArray_resize(OpenMM_BondArray* array, int size) { reinterpret_cast<vector<pair<int, int> >*>(array)->resize(size); } OPENMM_EXPORT void OpenMM_BondArray_append(OpenMM_BondArray* array, int particle1, int particle2) { reinterpret_cast<vector<pair<int, int> >*>(array)->push_back(pair<int, int>(particle1, particle2)); } OPENMM_EXPORT void OpenMM_BondArray_set(OpenMM_BondArray* array, int index, int particle1, int particle2) { (*reinterpret_cast<vector<pair<int, int> >*>(array))[index] = pair<int, int>(particle1, particle2); } OPENMM_EXPORT void OpenMM_BondArray_get(const OpenMM_BondArray* array, int index, int* particle1, int* particle2) { pair<int, int> particles = (*reinterpret_cast<const vector<pair<int, int> >*>(array))[index]; *particle1 = particles.first; *particle2 = particles.second; } /* OpenMM_ParameterArray */ OPENMM_EXPORT int OpenMM_ParameterArray_getSize(const OpenMM_ParameterArray* array) { return reinterpret_cast<const map<string, double>*>(array)->size(); } OPENMM_EXPORT double OpenMM_ParameterArray_get(const OpenMM_ParameterArray* array, const char* name) { const map<string, double>* params = reinterpret_cast<const map<string, double>*>(array); const map<string, double>::const_iterator iter = params->find(string(name)); if (iter == params->end()) throw OpenMMException("OpenMM_ParameterArray_get: No such parameter"); return iter->second; } /* OpenMM_PropertyArray */ OPENMM_EXPORT int OpenMM_PropertyArray_getSize(const OpenMM_PropertyArray* array) { return reinterpret_cast<const map<string, double>*>(array)->size(); } OPENMM_EXPORT const char* OpenMM_PropertyArray_get(const OpenMM_PropertyArray* array, const char* name) { const map<string, string>* params = reinterpret_cast<const map<string, string>*>(array); const map<string, string>::const_iterator iter = params->find(string(name)); if (iter == params->end()) throw OpenMMException("OpenMM_PropertyArray_get: No such property"); return iter->second.c_str(); } /* These methods need to be handled specially, since their C++ APIs cannot be directly translated to C. Unlike the C++ versions, the return value is allocated on the heap, and you must delete it yourself. */ OPENMM_EXPORT OpenMM_State* OpenMM_Context_getState(const OpenMM_Context* target, int types, int enforcePeriodicBox) { State result = reinterpret_cast<const Context*>(target)->getState(types, enforcePeriodicBox); return reinterpret_cast<OpenMM_State*>(new State(result)); }; OPENMM_EXPORT OpenMM_StringArray* OpenMM_Platform_loadPluginsFromDirectory(const char* directory) { vector<string> result = Platform::loadPluginsFromDirectory(string(directory)); return reinterpret_cast<OpenMM_StringArray*>(new vector<string>(result)); }; } /* */ OPENMM_EXPORT * _create(int size) { return reinterpret_cast<*>(new vector<>(size)); } OPENMM_EXPORT void _destroy(* array) { delete reinterpret_cast<vector<>*>(array); } OPENMM_EXPORT int _getSize(const * array) { return reinterpret_cast<const vector<>*>(array)->size(); } OPENMM_EXPORT void _resize(* array, int size) { reinterpret_cast<vector<>*>(array)->resize(size); } OPENMM_EXPORT void _append(* array, value) { reinterpret_cast<vector<>*>(array)->push_back(value); } OPENMM_EXPORT void _set(* array, int index, value) { (*reinterpret_cast<vector<>*>(array))[index] = value; } OPENMM_EXPORT _get(const * array, int index) { return (*reinterpret_cast<const vector<>*>(array))[index]; } /* */ OPENMM_EXPORT * _create() { return reinterpret_cast<*>(new set<>()); } OPENMM_EXPORT void _destroy(* s) { delete reinterpret_cast<set<>*>(s); } OPENMM_EXPORT int _getSize(const * s) { return reinterpret_cast<const set<>*>(s)->size(); } OPENMM_EXPORT void _insert(* s, value) { reinterpret_cast<set<>*>(s)->insert(value); } /* OpenMM:: OPENMM_EXPORT void OpenMM_ delete reinterpret_cast<*>(target); } OPENMM_EXPORT OpenMM_ , return reinterpret_cast<OpenMM_*>(new , } OPENMM_EXPORT , , }; 1