Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test PR for pycv #991

Closed
wants to merge 103 commits into from
Closed
Show file tree
Hide file tree
Changes from 102 commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
1cf992c
Attempting merge of pycv in v2.6
tonigi Oct 17, 2019
9d3542b
install PYCV prereqs and set PYTHON_BIN to python3
tonigi Oct 18, 2019
0444286
workaround the request for documenting components by adding the "py-"…
tonigi Oct 18, 2019
e3467bc
Delete README.md.upstream
tonigi Oct 18, 2019
5797ead
plumed-nest entry
tonigi Oct 18, 2019
04be32d
Update README.md
tonigi Oct 21, 2019
440212e
Merge tag 'v2.6.0' into v2.6-pycv-devel
tonigi May 23, 2020
7fa1259
update pybind to 2.5.0
tonigi May 23, 2020
e95e894
readme
tonigi May 23, 2020
bf6681f
readme
tonigi May 23, 2020
dd08b9f
add versions
tonigi May 23, 2020
147d476
add versions
tonigi May 23, 2020
f627fac
Merge tag 'v2.8.0' into v2.8-pycv-devel
tonigi Sep 5, 2022
1d64a49
regtest fixed
tonigi Sep 5, 2022
d023fd5
Attempting merge of pycv in v2.6
tonigi Oct 17, 2019
135d4cd
install PYCV prereqs and set PYTHON_BIN to python3
tonigi Oct 18, 2019
26439da
workaround the request for documenting components by adding the "py-"…
tonigi Oct 18, 2019
3e94e0e
Delete README.md.upstream
tonigi Oct 18, 2019
59bd537
plumed-nest entry
tonigi Oct 18, 2019
77e37a9
Update README.md
tonigi Oct 21, 2019
c7de834
update pybind to 2.5.0
tonigi May 23, 2020
007550b
add versions
tonigi May 23, 2020
66c8842
add versions
tonigi May 23, 2020
7dd5033
regtest fixed
tonigi Sep 5, 2022
517846c
update pybind 2.10.0
tonigi Sep 5, 2022
e6f9e17
travis restore
tonigi Sep 5, 2022
56990ca
add --embed for python
tonigi May 15, 2023
2946065
Merge remote-tracking branch 'origin/v2.8-pycv-devel' into pycvComm
Iximiel May 16, 2023
40b852a
remove possibly unused options for conda builds
tonigi May 16, 2023
fdada51
modernized the python interface
Iximiel Jun 9, 2023
9023646
updated gitignores
Iximiel Jun 9, 2023
ccd238d
added a test for the scopedIntepreter
Iximiel Jun 9, 2023
fa0c671
adjusted the tests
Iximiel Jun 13, 2023
e6e35f9
Merge branch 'v2.8-pycv-devel' of https://github.com/giorginolab/plum…
Iximiel Jun 14, 2023
5adb806
the base functionality is there
Iximiel Jun 19, 2023
9157624
now without verbose overloads
Iximiel Jun 19, 2023
aab51cf
setting up a better interface
Iximiel Jun 20, 2023
62cfede
passes the tests :)
Iximiel Jun 20, 2023
fd34d9a
small cosmetic changes
Iximiel Jun 20, 2023
40f6629
adding some "unit tests" to pyVector3d
Iximiel Jun 20, 2023
fc790dc
added and tested the interface to getPositions
Iximiel Jun 21, 2023
7c669a6
consolidating tests
Iximiel Jun 21, 2023
a3a28ea
added a toNumpy method for vector3D
Iximiel Jun 21, 2023
87a676c
the example should figure more clear now
Iximiel Jun 28, 2023
5a21e2b
added a test for the pbcs
Iximiel Aug 1, 2023
96a4006
now you can get the atomic positions as a np array
Iximiel Sep 21, 2023
22f1dbb
moved the embedded module in a separate source
Iximiel Sep 22, 2023
6e321ec
added an interface to the Pbcs
Iximiel Sep 22, 2023
6127962
moved test for clarity
Iximiel Sep 25, 2023
cf37bb8
adding getBox and getInvBox
Iximiel Sep 25, 2023
77393b9
Squashed commit of the following:
Iximiel Sep 27, 2023
1702a06
run astyle
Iximiel Sep 27, 2023
b0fa124
Changed the multicomponent return format
Iximiel Oct 2, 2023
cc3a06f
added the skeleton for the update func
Iximiel Oct 2, 2023
fa9d204
added a way to make data persist
Iximiel Oct 3, 2023
8b8a5dd
added charge and mass accessors but no tests
Iximiel Oct 12, 2023
74ae9de
added charge and mass accessors but no tests
Iximiel Oct 12, 2023
f413d99
Merge branch 'pycvCommUpdate' of https://github.com/Iximiel/plumed2 i…
Iximiel Oct 12, 2023
f0b7cce
first draft for the NL
Iximiel Oct 16, 2023
5f5b4c8
NL works, adding a test-example for the coordination
Iximiel Oct 17, 2023
5201992
the COORDINATIOn works (but no virial and devs)
Iximiel Oct 17, 2023
6a8d31a
added virial (for single value)
Iximiel Oct 17, 2023
009fd89
astyle'd
Iximiel Oct 17, 2023
488d303
setup rt-coord to test the virial
Iximiel Oct 17, 2023
f506f67
now the comment is more truthful
Iximiel Oct 17, 2023
5c85ab4
merged and changed how to compile the so
Iximiel Nov 9, 2023
3748b78
unification of the return interface
Iximiel Nov 9, 2023
f6ceb91
udpated tests + added list or tuple compatibility for return
Iximiel Nov 9, 2023
5d6f44a
added getAbsoluteIndex
Iximiel Nov 9, 2023
5fa428d
change in how AbsoluteIndexes are got
Iximiel Nov 10, 2023
e613092
preparing to add array getter for mass and charges
Iximiel Nov 10, 2023
d20ad68
Merge branch 'pycvCommUpdate' into pycvColvarMethods
Iximiel Nov 10, 2023
8fa06c5
added masses an charges array
Iximiel Nov 10, 2023
10a37b4
f Added some infomration on the ongoing MD
Iximiel Nov 10, 2023
f2560c0
set up the tests to be easier to work with
Iximiel Nov 10, 2023
5bb6bd7
set up the test for makeWhole
Iximiel Nov 14, 2023
5aa8f97
some reordering in the code
Iximiel Nov 14, 2023
197ba8b
moved the tree of pycv
Iximiel Nov 14, 2023
d9c0768
removed pybind11 from the directory
Iximiel Nov 14, 2023
59de055
moved the tree of pycv
Iximiel Nov 14, 2023
de7f0ab
set up a rationale for installation/compilation
Iximiel Nov 14, 2023
2e93bc9
moved also the tests
Iximiel Nov 14, 2023
018426b
clearer readme
Iximiel Nov 14, 2023
c557bec
Merge remote-tracking branch 'home/master' into pycvTree
Iximiel Nov 16, 2023
82e47b2
updated the LOAD to use the GLOBAL kw
Iximiel Nov 16, 2023
40caacd
preparing init for returning dictionary
Iximiel Nov 16, 2023
da034dd
Squashed commit of the following:
Iximiel Nov 23, 2023
b694f39
added a "doc" example
Iximiel Nov 23, 2023
4e8e14a
setting up function
Iximiel Nov 23, 2023
9e5b2e0
Squashed commit of the following:
Iximiel Nov 23, 2023
be6f6c9
preparinf for the emebedding
Iximiel Nov 23, 2023
77e548c
tested mdInfo in function
Iximiel Nov 24, 2023
847b106
now the function is embedded
Iximiel Nov 24, 2023
48295bc
changed how to threat errors, types and some movements
Iximiel Nov 24, 2023
8d726d1
added a test for func derivatives
Iximiel Nov 24, 2023
4b690d3
added arguments returned as ndarray
Iximiel Nov 24, 2023
8f98761
added components to pcvFunc
Iximiel Nov 24, 2023
f9aca88
applyed astyle
Iximiel Nov 24, 2023
87cb499
applyed astyle
Iximiel Nov 24, 2023
6ec0d6c
added some methods for function arguments
Iximiel Nov 24, 2023
8d5dfba
moved pycv in a 'plugins' folder
Iximiel Nov 29, 2023
bc2a118
Removing modifications to the respect of the master branch
Iximiel Nov 29, 2023
7a34d29
rmoved the old pycv tests
Iximiel Nov 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/pycv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Make.inc
*.o
*.so
env
142 changes: 142 additions & 0 deletions plugins/pycv/ActionWithPython.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Copyright (c) 2019 of Toni Giorgino

The pycv module is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

The pycv module is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with plumed. If not, see <http://www.gnu.org/licenses/>.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

#include "ActionWithPython.h"

#include "core/ActionWithValue.h"

#include <pybind11/embed.h> // everything needed for embedding
#include <pybind11/numpy.h>

#include <iostream>


namespace py = pybind11;


namespace PLMD {
namespace pycv {


// We can only have one interpreter globally. This is less than ideal
// because CVs can interfere with each other. The whole purpose of
// this superclass is to make a singleton. Putting it in the
// constructor makes it so that the interpreter is only initialized if
// one of the PYCV actions are used.
// https://pybind11.readthedocs.io/en/stable/reference.html#_CPPv422initialize_interpreterb

unsigned PlumedScopedPythonInterpreter::use_count=0;
std::unique_ptr<py::scoped_interpreter> PlumedScopedPythonInterpreter::interpreterGuard =
nullptr;
std::mutex PlumedScopedPythonInterpreter::interpreterMutex{};

PlumedScopedPythonInterpreter::PlumedScopedPythonInterpreter(::PLMD::Log&outlog)
:log(outlog) {
std::lock_guard<std::mutex> lk(interpreterMutex);
if(use_count==0 && Py_IsInitialized()) {
//this addresses the "calling pycv within a python interpreter problem"
++use_count;
}

if(use_count==0) {
std::cerr<< "------ initialized Python interpreter\n";
log<< "------ initialized Python interpreter\n";
interpreterGuard = std::make_unique<py::scoped_interpreter>();
} else {
std::cerr << "------ Python interpreter already initializated\n";
log << "------ Python interpreter already initializated\n";
}
++use_count;
}

PlumedScopedPythonInterpreter::~PlumedScopedPythonInterpreter() {
// Finalization is tricky, because it should happen AFTER the
// destruction of ALL the python declared variables
std::lock_guard<std::mutex> lk(interpreterMutex);
--use_count;
if(use_count==0) {
interpreterGuard.reset(nullptr);
std::cerr << "------ Python interpreter finalized\n";
log << "------ Python interpreter finalized\n";
}
}

ActionWithPython::ActionWithPython (const ActionOptions&ao)
:Action(ao),guard(log) {}

void ActionWithPython::pyParseFlag(const char* key, const ::pybind11::dict &initDict, bool& returnValue) {
parseFlag(key, returnValue);
if(initDict.contains(key)) {
bool defaultRet;
keywords.getLogicalDefault(key,defaultRet);
if (returnValue!=defaultRet) {
error(std::string("you specified the same keyword ").append(key)+ " both in python and in the settings file");
}
returnValue = initDict[key].cast<bool>();
}
}
/******************************************************************************/
//Value/components init
void initializeValue(::PLMD::ActionWithValue& action,pybind11::dict &settingsDict) {
action.log << " will have a single component";
bool withDerivatives=false;
if(settingsDict.contains("derivative")) {
withDerivatives=settingsDict["derivative"].cast<bool>();
}
if(withDerivatives) {
action.addValueWithDerivatives();
action.log << " WITH derivatives\n";
} else {
action.addValue();
action.log << " WITHOUT derivatives\n";
}
}

void initializeComponent(::PLMD::ActionWithValue& action,const std::string&name,py::dict &settingsDict) {
bool withDerivatives=false;
if(settingsDict.contains("derivative")) {
withDerivatives=settingsDict["derivative"].cast<bool>();
}

if(withDerivatives) {
action.addComponentWithDerivatives(name);
action.log << " WITH derivatives\n";
} else {
action.addComponent(name);
action.log << " WITHOUT derivatives\n";
}
}

void valueSettings(py::dict &settings, Value* valPtr) {
if(settings.contains("period")) {
if (settings["period"].is_none()) {
valPtr->setNotPeriodic();
} else {
py::tuple t = settings["period"];
if(t.size()!=2) {
plumed_merror("period must have exactly 2 components");
}
//the ballad py::str(t[0]).cast<std::string>() is to not care about the type of input of the user
std::string min=py::str(t[0]).cast<std::string>();
std::string max=py::str(t[1]).cast<std::string>();
valPtr->setDomain(min, max);
}
}
}

} // namespace pycv
} // namespace PLMD
81 changes: 81 additions & 0 deletions plugins/pycv/ActionWithPython.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Copyright (c) 2019 of Toni Giorgino

The pycv module is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

The pycv module is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with plumed. If not, see <http://www.gnu.org/licenses/>.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#ifndef __PLUMED_pycv_ActionWithPython_h //{
#define __PLUMED_pycv_ActionWithPython_h

#include <mutex>
#include <string>

#include "core/Action.h"

#include <pybind11/embed.h> // everything needed for embedding

namespace PLMD {
class Value;
namespace pycv {

using pycvComm_t = double;
static constexpr auto PYTHONCV_CITATION = "Giorgino, (2019). PYCV: a PLUMED 2 Module Enabling the Rapid Prototyping of Collective Variables in Python. Journal of Open Source Software, 4(42), 1773. doi:10.21105/joss.01773";
static constexpr auto BIASING_DISABLED = "PYCV: Gradient was expected as a second return value but is missing. Biasing won't work\n";
static constexpr std::string_view PYCV_COMPONENTPREFIX="py";

///This class act both as a guard for the interpreter and a a case container for the python module and functions
class PlumedScopedPythonInterpreter final {
public:
PlumedScopedPythonInterpreter(::PLMD::Log&);
~PlumedScopedPythonInterpreter();
private:
::PLMD::Log& log;
static unsigned use_count;
static std::unique_ptr<::pybind11::scoped_interpreter> interpreterGuard;
static std::mutex interpreterMutex;
};

class ActionWithPython: public virtual ::PLMD::Action {
//the guard MUST be set up before the python objects
// (so that it can be destroyed after them)
PlumedScopedPythonInterpreter guard;
public:
explicit ActionWithPython (const ActionOptions&);
///redefinition of parse to avoid confict between plumed.dat and python options
template<typename T>
void pyParse(const char* key, const ::pybind11::dict &initDict, T& returnValue);
///redefinition of parseFlag to avoid confict between plumed.dat and python options
void pyParseFlag(const char* key, const ::pybind11::dict &initDict, bool& returnValue);
};

template<typename T>
void ActionWithPython::pyParse(
const char* key, const ::pybind11::dict &initDict, T& returnValue) {
T initVal(returnValue);
parse(key,returnValue);
//this is not robust, but with no access to Action::line we cannot use Tools::findKeyword
if(initDict.contains(key)) {
if (returnValue != initVal) {
error(std::string("you specified the same keyword ").append(key)+ " both in python and in the settings file");
}
returnValue = initDict[key].cast<T>();
}
}

void initializeValue(::PLMD::ActionWithValue&, pybind11::dict &);
void initializeComponent(::PLMD::ActionWithValue&,const std::string&,pybind11::dict &);
void valueSettings( pybind11::dict &r, Value* valPtr);

} // namespace pycv
} // namespace PLMD
#endif //__PLUMED_pycv_ActionWithPython_h //}
14 changes: 14 additions & 0 deletions plugins/pycv/COPYRIGHT
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (c) 2019 of Toni Giorgino

The pycv module is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

The pycv module is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with plumed. If not, see <http://www.gnu.org/licenses/>.
37 changes: 37 additions & 0 deletions plugins/pycv/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
include Make.inc

#Dependency tracking based on https://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr
#this assumes gcc
DEPDIR := .deps
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.d

OBJS = ActionWithPython.o PythonCVInterface.o PythonFunction.o PlumedPythonEmbeddedModule.o

ADDCPPFLAGS=$(shell python3-config --cflags --embed) $(shell python -m pybind11 --includes) $(PLUMED_INCLUDE)
ADDCLDFLAGS=$(shell python3-config --ldflags --embed)

ifeq ($(SOEXT),dylib)
SONAME_OPTION:=-Wl,-install_name
else
SONAME_OPTION:=-Wl,-soname
endif

all: PythonCVInterface.$(SOEXT)


%.o: %.cpp $(DEPDIR)/%.d | $(DEPDIR)
@echo Compiling object $@
@$(CXX) -c $(DEPFLAGS) $(CPPFLAGS) $(ADDCPPFLAGS) $(CXXFLAGS) $< -o $@

$(DEPDIR): ; @mkdir -p $@

DEPFILES := $(OBJS:%.o=$(DEPDIR)/%.d)
$(DEPFILES):
include $(wildcard $(DEPFILES))

PythonCVInterface.$(SOEXT): $(OBJS)
@echo Compiling linking $@
@$(LDSHARED) $(ADDCLDFLAGS) $(SONAME_OPTION),"$(notdir $@)" $(DYNAMIC_LIBS) $(PLUMED_KERNEL) -o $@ $^

clean:
rm -f $(OBJS) PythonCVInterface.$(SOEXT)
Loading
Loading