From 80c092119e987191efdd67328a60a92d6853c2fd Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Tue, 18 May 2021 14:05:11 -0400 Subject: [PATCH 1/3] Include stl / eigen support for Pybind11 Eventually will work to wrap more atom / cube functions Signed-off-by: Geoff Hutchison --- python/core.cpp | 2 ++ python/io.cpp | 1 + 2 files changed, 3 insertions(+) diff --git a/python/core.cpp b/python/core.cpp index dd8cc124d5..497aa3a9e1 100644 --- a/python/core.cpp +++ b/python/core.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include diff --git a/python/io.cpp b/python/io.cpp index 59dafa8460..50b94a922c 100644 --- a/python/io.cpp +++ b/python/io.cpp @@ -1,4 +1,5 @@ #include +#include #include #include From 021703d01cf7f42e1e553940ebf1513d0caffc65 Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Wed, 13 Sep 2023 12:30:58 -0400 Subject: [PATCH 2/3] Add more methods and properties Signed-off-by: Geoff Hutchison --- python/core.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/python/core.cpp b/python/core.cpp index 497aa3a9e1..de9886b5ee 100644 --- a/python/core.cpp +++ b/python/core.cpp @@ -1,10 +1,11 @@ +#include #include #include -#include #include #include #include +#include namespace py = pybind11; @@ -21,6 +22,12 @@ PYBIND11_MODULE(core, m) .def_property_readonly("index", &Atom::index, "Index in the molecule") .def_property("atomic_number", &Atom::atomicNumber, &Atom::setAtomicNumber, "The atomic number") + .def_property("position", &Atom::position3D, &Atom::setPosition3D, + "The 3D position of the atom") + .def_property("formal_charge", &Atom::formalCharge, &Atom::setFormalCharge, + "The formal charge of the atom") + .def_property("is_selected", &Atom::selected, &Atom::setSelected, + "Whether the atom is selected") .def("is_valid", &Atom::isValid, "Check if the object is valid"); using bondBase = BondTemplate; @@ -43,6 +50,34 @@ PYBIND11_MODULE(core, m) Bond (Molecule::*addBond2)(const Atom&, const Atom&, unsigned char) = &Molecule::addBond; + py::class_(m, "UnitCell") + .def(py::init<>()) + .def_property_readonly("a", &UnitCell::a, "The a lattice parameter") + .def_property_readonly("b", &UnitCell::b, "The b lattice parameter") + .def_property_readonly("c", &UnitCell::c, "The c lattice parameter") + .def_property_readonly("alpha", &UnitCell::alpha, + "The alpha lattice parameter") + .def_property_readonly("beta", &UnitCell::beta, + "The beta lattice parameter") + .def_property_readonly("gamma", &UnitCell::gamma, + "The gamma lattice parameter") + .def_property_readonly("volume", &UnitCell::volume, + "The volume of the unit cell") + .def("set_cell_parameters", &UnitCell::setCellParameters, + "Set the unit cell parameters a b c alpha beta gamma") + .def_property("cell_matrix", &UnitCell::cellMatrix, + &UnitCell::setCellMatrix, "The unit cell vector matrix") + .def("to_fractional", &UnitCell::toFractional, + "Convert a cartesian vector to fractional coordinates") + .def("to_cartesian", &UnitCell::toCartesian, + "Convert a fractional vector to cartesian coordinates") + .def("wrap_fractional", &UnitCell::wrapFractional, + "Wrap a fractional vector to the unit cell") + .def("wrap_cartesian", &UnitCell::wrapCartesian, + "Wrap a cartesian vector to the unit cell") + .def("distance", &UnitCell::distance, + "Calculate the distance between two points in the unit cell"); + py::class_(m, "Molecule") .def(py::init<>()) .def("add_atom", @@ -51,14 +86,28 @@ PYBIND11_MODULE(core, m) .def("atom_count", atomCount0, "The number of atoms") .def("atom_count", atomCount1, "The number of atoms with the supplied atomic number") + .def_property_readonly("atoms", &Molecule::atoms, + "The atoms in the molecule") .def("add_bond", addBond1, "Add a new bond", py::arg("a1"), py::arg("a2"), py::arg("order") = 1) .def("add_bond", addBond2, "Add a new bond", py::arg("a1"), py::arg("a2"), py::arg("order") = 1) .def("bond_count", &Molecule::bondCount, "The number of bonds") + .def_property_readonly("bonds", &Molecule::bonds, + "The bonds in the molecule") .def("add_cube", &Molecule::addCube, py::return_value_policy::reference, "Add a new cube") .def("cube_count", &Molecule::cubeCount, "The number of cubes") + .def_property_readonly("cubes", &Molecule::cubes, + "The cubes in the molecule") + .def_property_readonly("radius", &Molecule::radius, + "The radius of the molecule") + .def_property_readonly("center", &Molecule::centerOfGeometry, + "The center of geometry of the molecule") + .def_property_readonly("mass_center", &Molecule::centerOfMass, + "The center of mass of the molecule") + .def_property_readonly("unit_cell", &Molecule::unitCell, + "The unit cell of the molecule, if defined") .def("has_custom_elements", &Molecule::hasCustomElements, "Returns true if the molecule contains any custom elements") .def("formula", &Molecule::formula, "The chemical formula of the molecule", From e90da0d5d7cb7f32e45af1e0fcd969b9e7e5aa3b Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Wed, 13 Sep 2023 12:54:50 -0400 Subject: [PATCH 3/3] Fix some compile errors Signed-off-by: Geoff Hutchison --- python/core.cpp | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/python/core.cpp b/python/core.cpp index de9886b5ee..47dbdd59ae 100644 --- a/python/core.cpp +++ b/python/core.cpp @@ -22,7 +22,7 @@ PYBIND11_MODULE(core, m) .def_property_readonly("index", &Atom::index, "Index in the molecule") .def_property("atomic_number", &Atom::atomicNumber, &Atom::setAtomicNumber, "The atomic number") - .def_property("position", &Atom::position3D, &Atom::setPosition3D, + .def_property("position", &Atom::position3d, &Atom::setPosition3d, "The 3D position of the atom") .def_property("formal_charge", &Atom::formalCharge, &Atom::setFormalCharge, "The formal charge of the atom") @@ -49,6 +49,10 @@ PYBIND11_MODULE(core, m) Bond (Molecule::*addBond1)(Index, Index, unsigned char) = &Molecule::addBond; Bond (Molecule::*addBond2)(const Atom&, const Atom&, unsigned char) = &Molecule::addBond; + Bond (Molecule::*bond0)(Index) const = &Molecule::bond; + Bond (Molecule::*bond1)(const Atom&, const Atom&) const = &Molecule::bond; + Bond (Molecule::*bond2)(Index, Index) const = &Molecule::bond; + Cube* (Molecule::*cube0)(Index) = &Molecule::cube; py::class_(m, "UnitCell") .def(py::init<>()) @@ -67,17 +71,11 @@ PYBIND11_MODULE(core, m) "Set the unit cell parameters a b c alpha beta gamma") .def_property("cell_matrix", &UnitCell::cellMatrix, &UnitCell::setCellMatrix, "The unit cell vector matrix") - .def("to_fractional", &UnitCell::toFractional, - "Convert a cartesian vector to fractional coordinates") - .def("to_cartesian", &UnitCell::toCartesian, - "Convert a fractional vector to cartesian coordinates") - .def("wrap_fractional", &UnitCell::wrapFractional, - "Wrap a fractional vector to the unit cell") - .def("wrap_cartesian", &UnitCell::wrapCartesian, - "Wrap a cartesian vector to the unit cell") .def("distance", &UnitCell::distance, "Calculate the distance between two points in the unit cell"); + UnitCell* (Molecule::*unitCell0)() = &Molecule::unitCell; + py::class_(m, "Molecule") .def(py::init<>()) .def("add_atom", @@ -86,27 +84,26 @@ PYBIND11_MODULE(core, m) .def("atom_count", atomCount0, "The number of atoms") .def("atom_count", atomCount1, "The number of atoms with the supplied atomic number") - .def_property_readonly("atoms", &Molecule::atoms, - "The atoms in the molecule") + .def("atom", &Molecule::atom, "The atom at the specified index") .def("add_bond", addBond1, "Add a new bond", py::arg("a1"), py::arg("a2"), py::arg("order") = 1) .def("add_bond", addBond2, "Add a new bond", py::arg("a1"), py::arg("a2"), py::arg("order") = 1) .def("bond_count", &Molecule::bondCount, "The number of bonds") - .def_property_readonly("bonds", &Molecule::bonds, - "The bonds in the molecule") + .def("bond", bond0, "The bond at the specified index") + .def("bond", bond1, "The bond between the specified atoms") + .def("bond", bond2, "The bond between the specified atoms") .def("add_cube", &Molecule::addCube, py::return_value_policy::reference, "Add a new cube") .def("cube_count", &Molecule::cubeCount, "The number of cubes") - .def_property_readonly("cubes", &Molecule::cubes, - "The cubes in the molecule") + .def("cube", cube0, "The cube at the specified index") .def_property_readonly("radius", &Molecule::radius, "The radius of the molecule") .def_property_readonly("center", &Molecule::centerOfGeometry, "The center of geometry of the molecule") .def_property_readonly("mass_center", &Molecule::centerOfMass, "The center of mass of the molecule") - .def_property_readonly("unit_cell", &Molecule::unitCell, + .def_property_readonly("unit_cell", unitCell0, "The unit cell of the molecule, if defined") .def("has_custom_elements", &Molecule::hasCustomElements, "Returns true if the molecule contains any custom elements")