Skip to content

Commit

Permalink
Added the "center" keyword argument to Cursors, CursorsM and System (…
Browse files Browse the repository at this point in the history
…plus legacy.System.System).

Also added "origin" and "zero" as recognised options for constructing vectors, e.g.

```
c = mols.cursor()
c.make_whole(center="origin")
```

will work.
  • Loading branch information
chryswoods committed Jun 23, 2024
1 parent 26a79ee commit 89ae69e
Show file tree
Hide file tree
Showing 47 changed files with 337 additions and 132 deletions.
59 changes: 59 additions & 0 deletions corelib/src/libs/SireSystem/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4162,6 +4162,65 @@ void System::makeWhole()
this->makeWhole(PropertyMap());
}

void System::makeWhole(const Vector &center, const PropertyMap &map)
{
if (this->needsAccepting())
{
this->accept();
}

if (not this->containsProperty(map["space"]))
return;

if (not this->property(map["space"]).isA<Space>())
return;

const auto &space = this->property(map["space"]).asA<Space>();

if (not space.isPeriodic())
return;

PropertyMap m = map;
m.set("space", space);

// get a list of all molecules in the system
const SelectorMol mols(*this);

SelectorMol changed_mols;

for (const auto &mol : mols)
{
auto new_mol = mol.move().makeWhole(center, m).commit();

if (new_mol.data().version() != mol.data().version())
{
changed_mols.append(new_mol);
}
}

if (not changed_mols.isEmpty())
{
Delta delta(*this, true);

// this ensures that only a single copy of System is used - prevents
// unnecessary copying
this->operator=(System());
delta.update(changed_mols.toMolecules());
this->operator=(delta.apply());

if (this->needsAccepting())
{
delta = Delta();
this->accept();
}
}
}

void System::makeWhole(const Vector &center)
{
this->makeWhole(center, PropertyMap());
}

const char *System::typeName()
{
return QMetaType::typeName(qMetaTypeId<System>());
Expand Down
4 changes: 4 additions & 0 deletions corelib/src/libs/SireSystem/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ namespace SireSystem
void makeWhole();
void makeWhole(const SireBase::PropertyMap &map);

void makeWhole(const SireMaths::Vector &center);
void makeWhole(const SireMaths::Vector &center,
const SireBase::PropertyMap &map);

static const System &null();

protected:
Expand Down
7 changes: 7 additions & 0 deletions src/sire/maths/_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ class containing 3 double precision values. These values
"""

def __init__(self, *args, **kwargs):
if len(args) == 1:
# check for "zero" or "origin"
arg0 = str(args[0]).strip().lower()

if arg0 == "zero" or arg0 == "origin":
args = [0.0, 0.0, 0.0]

from ..units import angstrom
from .. import u

Expand Down
8 changes: 4 additions & 4 deletions src/sire/mol/_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2624,7 +2624,7 @@ def delete_frame(self, *args, **kwargs):

return self

def make_whole(self, *args, map=None):
def make_whole(self, center=None, map=None):
"""
Make all of the atoms operated on by this cursor whole
(they won't be broken across a periodic box boundary)
Expand All @@ -2634,7 +2634,7 @@ def make_whole(self, *args, map=None):
which they should be wrapped.
"""
for cursor in self._cursors:
cursor.make_whole(*args, map=map)
cursor.make_whole(center=center, map=map)

return self

Expand Down Expand Up @@ -3779,7 +3779,7 @@ def delete_frame(self, *args, **kwargs):

return self

def make_whole(self, *args, map=None):
def make_whole(self, center=None, map=None):
"""
Make all of the atoms operated on by this cursor whole
(they won't be broken across a periodic box boundary)
Expand All @@ -3789,7 +3789,7 @@ def make_whole(self, *args, map=None):
which they should be wrapped.
"""
for cursor in self._cursors:
cursor.make_whole(*args, map=map)
cursor.make_whole(center=center, map=map)

return self

Expand Down
22 changes: 17 additions & 5 deletions src/sire/system/_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,30 @@ def numbers(self):
"""Return the numbers of all of the molecules in this System"""
return self.molecules().numbers()

def make_whole(self, map=None):
def make_whole(self, center=None, map=None):
"""
Make all of the molecules in this system whole. This
maps each molecule into the current space, such that no
molecule is broken across a periodic box boundary
"""
if map is None:
self._system.make_whole()
if center is None:
if map is None:
self._system.make_whole()
else:
from ..base import create_map

self._system.make_whole(map=create_map(map))
else:
from ..base import create_map
from ..maths import Vector

center = Vector(center)

if map is None:
self._system.make_whole(center=center)
else:
from ..base import create_map

self._system.make_whole(map=create_map(map))
self._system.make_whole(center=center, map=create_map(map))

self._molecules = None

Expand Down
20 changes: 17 additions & 3 deletions tests/mol/test_make_whole.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ def test_auto_make_whole_on_load_frame(wrapped_mols):


def test_auto_make_whole_on_load():
mols = sr.load_test_files(
"wrapped.rst7", "wrapped.prm7", map={"make_whole": True}
)
mols = sr.load_test_files("wrapped.rst7", "wrapped.prm7", map={"make_whole": True})

_assert_correct_com(mols[0].evaluate().center_of_mass())

Expand All @@ -84,3 +82,19 @@ def test_auto_make_whole_on_load_no_breakage(kigaki_mols):
kigaki_mols[0].evaluate().center_of_mass()
== mols[0].evaluate().center_of_mass()
)


def test_make_whole_center_args(ala_mols):
mols = ala_mols

c = mols[0].cursor()
c.make_whole(center="origin")

c = mols.cursor()
c.make_whole(center=0)

c = mols[0].atoms().cursor()
c.make_whole(center=(1, 2, 3))

mols = mols.clone()
mols.make_whole(center=("1A", "2A", "3A"))
8 changes: 5 additions & 3 deletions wrapper/System/AngleComponent.pypp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace bp = boost::python;

SireSystem::AngleComponent __copy__(const SireSystem::AngleComponent &other){ return SireSystem::AngleComponent(other); }

#include "Helpers/copy.hpp"

#include "Qt/qdatastream.hpp"

#include "Helpers/str.hpp"
Expand Down Expand Up @@ -237,9 +239,9 @@ void register_AngleComponent_class(){
AngleComponent_exposer.staticmethod( "theta021" );
AngleComponent_exposer.staticmethod( "theta102" );
AngleComponent_exposer.staticmethod( "typeName" );
AngleComponent_exposer.def( "__copy__", &__copy__);
AngleComponent_exposer.def( "__deepcopy__", &__copy__);
AngleComponent_exposer.def( "clone", &__copy__);
AngleComponent_exposer.def( "__copy__", &__copy__<SireSystem::AngleComponent>);
AngleComponent_exposer.def( "__deepcopy__", &__copy__<SireSystem::AngleComponent>);
AngleComponent_exposer.def( "clone", &__copy__<SireSystem::AngleComponent>);
AngleComponent_exposer.def( "__rlshift__", &__rlshift__QDataStream< ::SireSystem::AngleComponent >,
bp::return_internal_reference<1, bp::with_custodian_and_ward<1,2> >() );
AngleComponent_exposer.def( "__rrshift__", &__rrshift__QDataStream< ::SireSystem::AngleComponent >,
Expand Down
8 changes: 5 additions & 3 deletions wrapper/System/AssignerGroup.pypp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace bp = boost::python;

SireSystem::AssignerGroup __copy__(const SireSystem::AssignerGroup &other){ return SireSystem::AssignerGroup(other); }

#include "Helpers/copy.hpp"

#include "Qt/qdatastream.hpp"

const char* pvt_get_name(const SireSystem::AssignerGroup&){ return "SireSystem::AssignerGroup";}
Expand Down Expand Up @@ -197,9 +199,9 @@ void register_AssignerGroup_class(){

}
AssignerGroup_exposer.staticmethod( "typeName" );
AssignerGroup_exposer.def( "__copy__", &__copy__);
AssignerGroup_exposer.def( "__deepcopy__", &__copy__);
AssignerGroup_exposer.def( "clone", &__copy__);
AssignerGroup_exposer.def( "__copy__", &__copy__<SireSystem::AssignerGroup>);
AssignerGroup_exposer.def( "__deepcopy__", &__copy__<SireSystem::AssignerGroup>);
AssignerGroup_exposer.def( "clone", &__copy__<SireSystem::AssignerGroup>);
AssignerGroup_exposer.def( "__rlshift__", &__rlshift__QDataStream< ::SireSystem::AssignerGroup >,
bp::return_internal_reference<1, bp::with_custodian_and_ward<1,2> >() );
AssignerGroup_exposer.def( "__rrshift__", &__rrshift__QDataStream< ::SireSystem::AssignerGroup >,
Expand Down
8 changes: 5 additions & 3 deletions wrapper/System/CheckPoint.pypp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace bp = boost::python;

SireSystem::CheckPoint __copy__(const SireSystem::CheckPoint &other){ return SireSystem::CheckPoint(other); }

#include "Helpers/copy.hpp"

#include "Qt/qdatastream.hpp"

#include "Helpers/str.hpp"
Expand Down Expand Up @@ -72,9 +74,9 @@ void register_CheckPoint_class(){

}
CheckPoint_exposer.staticmethod( "typeName" );
CheckPoint_exposer.def( "__copy__", &__copy__);
CheckPoint_exposer.def( "__deepcopy__", &__copy__);
CheckPoint_exposer.def( "clone", &__copy__);
CheckPoint_exposer.def( "__copy__", &__copy__<SireSystem::CheckPoint>);
CheckPoint_exposer.def( "__deepcopy__", &__copy__<SireSystem::CheckPoint>);
CheckPoint_exposer.def( "clone", &__copy__<SireSystem::CheckPoint>);
CheckPoint_exposer.def( "__rlshift__", &__rlshift__QDataStream< ::SireSystem::CheckPoint >,
bp::return_internal_reference<1, bp::with_custodian_and_ward<1,2> >() );
CheckPoint_exposer.def( "__rrshift__", &__rrshift__QDataStream< ::SireSystem::CheckPoint >,
Expand Down
8 changes: 5 additions & 3 deletions wrapper/System/CloseMols.pypp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace bp = boost::python;

SireSystem::CloseMols __copy__(const SireSystem::CloseMols &other){ return SireSystem::CloseMols(other); }

#include "Helpers/copy.hpp"

#include "Qt/qdatastream.hpp"

const char* pvt_get_name(const SireSystem::CloseMols&){ return "SireSystem::CloseMols";}
Expand Down Expand Up @@ -207,9 +209,9 @@ void register_CloseMols_class(){

}
CloseMols_exposer.staticmethod( "typeName" );
CloseMols_exposer.def( "__copy__", &__copy__);
CloseMols_exposer.def( "__deepcopy__", &__copy__);
CloseMols_exposer.def( "clone", &__copy__);
CloseMols_exposer.def( "__copy__", &__copy__<SireSystem::CloseMols>);
CloseMols_exposer.def( "__deepcopy__", &__copy__<SireSystem::CloseMols>);
CloseMols_exposer.def( "clone", &__copy__<SireSystem::CloseMols>);
CloseMols_exposer.def( "__rlshift__", &__rlshift__QDataStream< ::SireSystem::CloseMols >,
bp::return_internal_reference<1, bp::with_custodian_and_ward<1,2> >() );
CloseMols_exposer.def( "__rrshift__", &__rrshift__QDataStream< ::SireSystem::CloseMols >,
Expand Down
8 changes: 5 additions & 3 deletions wrapper/System/ComponentConstraint.pypp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace bp = boost::python;

SireSystem::ComponentConstraint __copy__(const SireSystem::ComponentConstraint &other){ return SireSystem::ComponentConstraint(other); }

#include "Helpers/copy.hpp"

#include "Qt/qdatastream.hpp"

#include "Helpers/str.hpp"
Expand Down Expand Up @@ -114,9 +116,9 @@ void register_ComponentConstraint_class(){

}
ComponentConstraint_exposer.staticmethod( "typeName" );
ComponentConstraint_exposer.def( "__copy__", &__copy__);
ComponentConstraint_exposer.def( "__deepcopy__", &__copy__);
ComponentConstraint_exposer.def( "clone", &__copy__);
ComponentConstraint_exposer.def( "__copy__", &__copy__<SireSystem::ComponentConstraint>);
ComponentConstraint_exposer.def( "__deepcopy__", &__copy__<SireSystem::ComponentConstraint>);
ComponentConstraint_exposer.def( "clone", &__copy__<SireSystem::ComponentConstraint>);
ComponentConstraint_exposer.def( "__rlshift__", &__rlshift__QDataStream< ::SireSystem::ComponentConstraint >,
bp::return_internal_reference<1, bp::with_custodian_and_ward<1,2> >() );
ComponentConstraint_exposer.def( "__rrshift__", &__rrshift__QDataStream< ::SireSystem::ComponentConstraint >,
Expand Down
8 changes: 5 additions & 3 deletions wrapper/System/Constraints.pypp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace bp = boost::python;

SireSystem::Constraints __copy__(const SireSystem::Constraints &other){ return SireSystem::Constraints(other); }

#include "Helpers/copy.hpp"

#include "Qt/qdatastream.hpp"

#include "Helpers/str.hpp"
Expand Down Expand Up @@ -271,9 +273,9 @@ void register_Constraints_class(){

}
Constraints_exposer.staticmethod( "typeName" );
Constraints_exposer.def( "__copy__", &__copy__);
Constraints_exposer.def( "__deepcopy__", &__copy__);
Constraints_exposer.def( "clone", &__copy__);
Constraints_exposer.def( "__copy__", &__copy__<SireSystem::Constraints>);
Constraints_exposer.def( "__deepcopy__", &__copy__<SireSystem::Constraints>);
Constraints_exposer.def( "clone", &__copy__<SireSystem::Constraints>);
Constraints_exposer.def( "__rlshift__", &__rlshift__QDataStream< ::SireSystem::Constraints >,
bp::return_internal_reference<1, bp::with_custodian_and_ward<1,2> >() );
Constraints_exposer.def( "__rrshift__", &__rrshift__QDataStream< ::SireSystem::Constraints >,
Expand Down
8 changes: 5 additions & 3 deletions wrapper/System/DihedralComponent.pypp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace bp = boost::python;

SireSystem::DihedralComponent __copy__(const SireSystem::DihedralComponent &other){ return SireSystem::DihedralComponent(other); }

#include "Helpers/copy.hpp"

#include "Qt/qdatastream.hpp"

#include "Helpers/str.hpp"
Expand Down Expand Up @@ -249,9 +251,9 @@ void register_DihedralComponent_class(){
DihedralComponent_exposer.staticmethod( "theta012" );
DihedralComponent_exposer.staticmethod( "theta123" );
DihedralComponent_exposer.staticmethod( "typeName" );
DihedralComponent_exposer.def( "__copy__", &__copy__);
DihedralComponent_exposer.def( "__deepcopy__", &__copy__);
DihedralComponent_exposer.def( "clone", &__copy__);
DihedralComponent_exposer.def( "__copy__", &__copy__<SireSystem::DihedralComponent>);
DihedralComponent_exposer.def( "__deepcopy__", &__copy__<SireSystem::DihedralComponent>);
DihedralComponent_exposer.def( "clone", &__copy__<SireSystem::DihedralComponent>);
DihedralComponent_exposer.def( "__rlshift__", &__rlshift__QDataStream< ::SireSystem::DihedralComponent >,
bp::return_internal_reference<1, bp::with_custodian_and_ward<1,2> >() );
DihedralComponent_exposer.def( "__rrshift__", &__rrshift__QDataStream< ::SireSystem::DihedralComponent >,
Expand Down
8 changes: 5 additions & 3 deletions wrapper/System/DistanceComponent.pypp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace bp = boost::python;

SireSystem::DistanceComponent __copy__(const SireSystem::DistanceComponent &other){ return SireSystem::DistanceComponent(other); }

#include "Helpers/copy.hpp"

#include "Qt/qdatastream.hpp"

#include "Helpers/str.hpp"
Expand Down Expand Up @@ -143,9 +145,9 @@ void register_DistanceComponent_class(){
}
DistanceComponent_exposer.staticmethod( "r" );
DistanceComponent_exposer.staticmethod( "typeName" );
DistanceComponent_exposer.def( "__copy__", &__copy__);
DistanceComponent_exposer.def( "__deepcopy__", &__copy__);
DistanceComponent_exposer.def( "clone", &__copy__);
DistanceComponent_exposer.def( "__copy__", &__copy__<SireSystem::DistanceComponent>);
DistanceComponent_exposer.def( "__deepcopy__", &__copy__<SireSystem::DistanceComponent>);
DistanceComponent_exposer.def( "clone", &__copy__<SireSystem::DistanceComponent>);
DistanceComponent_exposer.def( "__rlshift__", &__rlshift__QDataStream< ::SireSystem::DistanceComponent >,
bp::return_internal_reference<1, bp::with_custodian_and_ward<1,2> >() );
DistanceComponent_exposer.def( "__rrshift__", &__rrshift__QDataStream< ::SireSystem::DistanceComponent >,
Expand Down
8 changes: 5 additions & 3 deletions wrapper/System/DoubleDistanceComponent.pypp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace bp = boost::python;

SireSystem::DoubleDistanceComponent __copy__(const SireSystem::DoubleDistanceComponent &other){ return SireSystem::DoubleDistanceComponent(other); }

#include "Helpers/copy.hpp"

#include "Qt/qdatastream.hpp"

#include "Helpers/str.hpp"
Expand Down Expand Up @@ -180,9 +182,9 @@ void register_DoubleDistanceComponent_class(){
DoubleDistanceComponent_exposer.staticmethod( "r01" );
DoubleDistanceComponent_exposer.staticmethod( "r23" );
DoubleDistanceComponent_exposer.staticmethod( "typeName" );
DoubleDistanceComponent_exposer.def( "__copy__", &__copy__);
DoubleDistanceComponent_exposer.def( "__deepcopy__", &__copy__);
DoubleDistanceComponent_exposer.def( "clone", &__copy__);
DoubleDistanceComponent_exposer.def( "__copy__", &__copy__<SireSystem::DoubleDistanceComponent>);
DoubleDistanceComponent_exposer.def( "__deepcopy__", &__copy__<SireSystem::DoubleDistanceComponent>);
DoubleDistanceComponent_exposer.def( "clone", &__copy__<SireSystem::DoubleDistanceComponent>);
DoubleDistanceComponent_exposer.def( "__rlshift__", &__rlshift__QDataStream< ::SireSystem::DoubleDistanceComponent >,
bp::return_internal_reference<1, bp::with_custodian_and_ward<1,2> >() );
DoubleDistanceComponent_exposer.def( "__rrshift__", &__rrshift__QDataStream< ::SireSystem::DoubleDistanceComponent >,
Expand Down
Loading

0 comments on commit 89ae69e

Please sign in to comment.