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

Add PeriodicCollapser #313

Merged
merged 45 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
d4cc6de
Update docs.
andrewtarzia Mar 12, 2021
4f7e4c8
WIP: Add periodic optimisation. Does not work currently.
andrewtarzia Mar 12, 2021
6acd221
Update lattice constants in init of Cof toplogy graph.
andrewtarzia Mar 17, 2021
0d85970
Update periodic topology graphs.
andrewtarzia Mar 17, 2021
f06079f
Merge branch 'master' of https://github.com/lukasturcani/stk into per…
andrewtarzia Mar 17, 2021
58957d0
Minor.
andrewtarzia Mar 17, 2021
1de617e
Update tests.
andrewtarzia Mar 17, 2021
f7ef4b2
Update docs.
andrewtarzia Mar 17, 2021
a62cc19
Update docs.
andrewtarzia Mar 17, 2021
c077d4e
Remove redundant methods.
lukasturcani Apr 20, 2021
51cec1d
Remove lattice constant changes
lukasturcani Apr 20, 2021
cc72065
Fix up final things
lukasturcani Apr 20, 2021
aa4185f
Revert tests
lukasturcani Apr 20, 2021
fd9aa39
wip
lukasturcani Apr 20, 2021
4aa677e
add some docs
lukasturcani Apr 20, 2021
fb7f245
wip
lukasturcani Apr 20, 2021
91b751d
wip
lukasturcani Apr 21, 2021
c20ea67
wip
lukasturcani Apr 21, 2021
3599a8c
Add collapsed tests
lukasturcani Apr 21, 2021
6ad50ae
Remove redundant comments
lukasturcani Apr 21, 2021
eb7cc1b
fix test
lukasturcani Apr 21, 2021
a714fa9
wip
lukasturcani Apr 21, 2021
f096f73
Account for lattice size in PeriodicInfo
lukasturcani Apr 25, 2021
eb89c24
Undo changes
lukasturcani Apr 25, 2021
9a8a1a4
remove stuff
lukasturcani Apr 25, 2021
4c18840
wip
lukasturcani Apr 25, 2021
c8475f6
tests passing
lukasturcani Apr 25, 2021
b131e81
wip
lukasturcani Apr 26, 2021
a3a5044
tests passing
lukasturcani Apr 26, 2021
23d2fb4
Add docs
lukasturcani Apr 26, 2021
55c0d5b
update docs
lukasturcani Apr 26, 2021
eb09847
update docs
lukasturcani Apr 26, 2021
2438064
clean up docs
lukasturcani Apr 26, 2021
ce160dc
typo
lukasturcani Apr 26, 2021
5d19579
docs
lukasturcani Apr 26, 2021
e3a8d87
docs
lukasturcani Apr 26, 2021
d8e02ff
typo
lukasturcani Apr 26, 2021
afa6da7
fix child class
lukasturcani Apr 26, 2021
97858b2
wip
lukasturcani Apr 26, 2021
f512cf3
add clone test
lukasturcani Apr 26, 2021
c49de15
wip
lukasturcani Apr 26, 2021
1a4e9ce
use zip longest
lukasturcani Apr 26, 2021
21bc12f
add test case
lukasturcani Apr 26, 2021
d5f645b
docs
lukasturcani Apr 26, 2021
168a360
docs
lukasturcani Apr 26, 2021
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
83 changes: 71 additions & 12 deletions src/stk/molecular/molecules/constructed_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,10 @@ def __init__(self, topology_graph):

"""

construction_result = topology_graph.construct()
super().__init__(
atoms=construction_result.get_atoms(),
bonds=construction_result.get_bonds(),
position_matrix=construction_result.get_position_matrix(),
self._init_from_construction_result(
obj=self,
construction_result=topology_graph.construct(),
)
self._atom_infos = construction_result.get_atom_infos()
self._bond_infos = construction_result.get_bond_infos()
self._num_building_blocks = {
building_block:
topology_graph.get_num_building_block(building_block)
for building_block in topology_graph.get_building_blocks()
}

@classmethod
def init(
Expand Down Expand Up @@ -132,6 +123,74 @@ def init(
molecule._num_building_blocks = dict(num_building_blocks)
return molecule

@classmethod
def init_from_construction_result(
cls,
construction_result,
):
"""
Initialize a :class:`.ConstructedMolecule`.

Parameters
----------
construction_result : :class:`.ConstructionResult`
The result of a construction, from which the
:class:`.ConstructedMolecule` should be initialized.

Returns
-------
:class:`.ConstructedMolecule`
The constructed molecule.

"""

return cls._init_from_construction_result(
obj=cls.__new__(cls),
construction_result=construction_result,
)

@staticmethod
def _init_from_construction_result(
obj,
construction_result,
):
"""
Initialize a :class:`.ConstructedMolecule`.

This modifies `obj`.

Parameters
----------
obj : :class:`.ConstructedMolecule`
The constructed molecule to initialize.

construction_result : :class:`.ConstructionResult`
The result of a construction, from which the
:class:`.ConstructedMolecule` should be initialized.

Returns
-------
:class:`.ConstructedMolecule`
The `obj` instance.

"""

super(ConstructedMolecule, obj).__init__(
atoms=construction_result.get_atoms(),
bonds=construction_result.get_bonds(),
position_matrix=construction_result.get_position_matrix(),
)
obj._atom_infos = construction_result.get_atom_infos()
obj._bond_infos = construction_result.get_bond_infos()
obj._num_building_blocks = {
building_block: construction_result.get_num_building_block(
building_block=building_block,
)
for building_block
in construction_result.get_building_blocks()
}
return obj

def clone(self):
clone = super().clone()
clone._atom_infos = self._atom_infos
Expand Down
60 changes: 51 additions & 9 deletions src/stk/molecular/topology_graphs/cof/cof.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
from functools import partial
from operator import getitem

from ..topology_graph import TopologyGraph, NullOptimizer, EdgeGroup
from ..topology_graph import (
TopologyGraph,
NullOptimizer,
EdgeGroup,
PeriodicConstructionResult,
)
from .vertices import _UnaligningVertex
from .edge import _CofEdge
from ...reactions import GenericReactionFactory
Expand Down Expand Up @@ -95,10 +100,12 @@ class Cof(TopologyGraph):

For :class:`.Cof` topologies, it is recommend to use the
:class:`.Collapser` optimizer if using the nonperiodic form.
However, no optimizer is valid for periodic systems currently.
For periodic systems, the :class:`.PeriodicCollapser` is
recommended.

.. code-block:: python

# Nonperiodic.
cof = stk.ConstructedMolecule(
topology_graph=stk.cof.Honeycomb(
building_blocks=(bb1, bb2),
Expand All @@ -109,21 +116,33 @@ class Cof(TopologyGraph):
),
)

*Accessing the Periodic Unit Cell*
# Periodic.
cof = stk.ConstructedMolecule(
topology_graph=stk.cof.PeriodicHoneycomb(
building_blocks=(bb1, bb2),
lattice_size=(3, 3, 1),
optimizer=stk.PeriodicCollapser(),
),
)

The same :class:`.Cof` instance can be built as a periodic
structure, which has a unit cell (assuming as P1 space group) that
can be accessed at any time from the :class:`.TopologyGraph`
instance.
*Accessing the Periodic Information*

When building periodic :class:`.Cof` instances, the periodic
information, such as the unit cell, can be accessed if you use the
:class:`.PeriodicConstructionResult` returned by calling
:meth:`.Cof.construct`

.. code-block:: python

topology_graph = stk.cof.PeriodicHoneycomb(
building_blocks=(bb1, bb2),
lattice_size=(3, 3, 1),
)
cof = stk.ConstructedMolecule(topology_graph)
periodic_info = topology.get_periodic_info()
construction_result = topology_graph.construct()
cof = stk.ConstructedMolecule.init_from_construction_result(
construction_result=construction_result,
)
periodic_info = construction_result.get_periodic_info()
cell_matrix = periodic_info.get_cell_matrix()
# Can access all unit-cell parameters.
a = periodic_info.get_a()
Expand All @@ -132,6 +151,13 @@ class Cof(TopologyGraph):
alpha = periodic_info.get_alpha()
beta = periodic_info.get_beta()
gamma = periodic_info.get_gamma()
# Write to .pdb file.
writer = stk.PdbWriter()
writer.write(
molecule=cof,
path='cof.pdb',
periodic_info=periodic_info,
)

*Structural Isomer Construction*

Expand Down Expand Up @@ -663,6 +689,22 @@ def _get_building_block_vertices(
def _get_lattice_constants(self):
return self._lattice_constants

def construct(self):
"""
Construct a :class:`.ConstructedMolecule`.

Returns
-------
:class:`.PeriodicConstructionResult`
The data describing the :class:`.ConstructedMolecule`.

"""

return super().construct()

def _get_construction_result(self, state):
return PeriodicConstructionResult(state, self._lattice_size)

def _get_scale(self, building_block_vertices):
return 5*max(
bb.get_maximum_diameter()
Expand Down
15 changes: 13 additions & 2 deletions src/stk/molecular/topology_graphs/cof/periodic_hexagonal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""

import numpy as np
import warnings

from ...reactions import GenericReactionFactory
from .cof import Cof
from .vertices import _LinearCofVertex, _NonLinearCofVertex
Expand All @@ -28,8 +30,8 @@ class PeriodicHexagonal(Cof):
| 6-functional groups: 0 to 3
| 2-functional groups: 4 to 15

Note that :class:`.Optimizer` does not optimize the
:class:`.PeriodicInfo`.
Note that optimizers may not optimize the :class:`.PeriodicInfo`.
The documentation of the optimizer will state if it does.

See :class:`.Cof` for more details and examples.

Expand Down Expand Up @@ -133,6 +135,15 @@ def get_periodic_info(self):

"""

warnings.warn(
'You called get_periodic_info() on a topology graph '
'instance. This method will be removed in any version '
'of stk released on, or after, 21/10/21. Please call '
'the construct() method instead. This will return a '
'PeriodicConstructionResult which provides the new '
'get_periodic_info() method.'
)

lattice_constants = self._get_lattice_constants()

return PeriodicInfo(
Expand Down
15 changes: 13 additions & 2 deletions src/stk/molecular/topology_graphs/cof/periodic_honeycomb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""

import numpy as np
import warnings

from ...reactions import GenericReactionFactory
from .cof import Cof
from .vertices import _LinearCofVertex, _NonLinearCofVertex
Expand All @@ -28,8 +30,8 @@ class PeriodicHoneycomb(Cof):
| 3-functional groups: 0 to 1
| 2-functional groups: 2 to 4

Note that :class:`.Optimizer` does not optimize the
:class:`.PeriodicInfo`.
Note that optimizers may not optimize the :class:`.PeriodicInfo`.
The documentation of the optimizer will state if it does.

See :class:`.Cof` for more details and examples.

Expand Down Expand Up @@ -133,6 +135,15 @@ def get_periodic_info(self):

"""

warnings.warn(
'You called get_periodic_info() on a topology graph '
'instance. This method will be removed in any version '
'of stk released on, or after, 21/10/21. Please call '
'the construct() method instead. This will return a '
'PeriodicConstructionResult which provides the new '
'get_periodic_info() method.'
)

lattice_constants = self._get_lattice_constants()

return PeriodicInfo(
Expand Down
15 changes: 13 additions & 2 deletions src/stk/molecular/topology_graphs/cof/periodic_kagome.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""

import numpy as np
import warnings

from ...reactions import GenericReactionFactory
from .cof import Cof
from .vertices import _LinearCofVertex, _NonLinearCofVertex
Expand All @@ -28,8 +30,8 @@ class PeriodicKagome(Cof):
| 4-functional groups: 0 to 2
| 2-functional groups: 3 to 8

Note that :class:`.Optimizer` does not optimize the
:class:`.PeriodicInfo`.
Note that optimizers may not optimize the :class:`.PeriodicInfo`.
The documentation of the optimizer will state if it does.

See :class:`.Cof` for more details and examples.

Expand Down Expand Up @@ -133,6 +135,15 @@ def get_periodic_info(self):

"""

warnings.warn(
'You called get_periodic_info() on a topology graph '
'instance. This method will be removed in any version '
'of stk released on, or after, 21/10/21. Please call '
'the construct() method instead. This will return a '
'PeriodicConstructionResult which provides the new '
'get_periodic_info() method.'
)

lattice_constants = self._get_lattice_constants()

return PeriodicInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""

import numpy as np
import warnings

from ...reactions import GenericReactionFactory
from .cof import Cof
from .vertices import _NonLinearCofVertex
Expand All @@ -27,8 +29,8 @@ class PeriodicLinkerlessHoneycomb(Cof):

| 3-functional groups: 0 to 1

Note that :class:`.Optimizer` does not optimize the
:class:`.PeriodicInfo`.
Note that optimizers may not optimize the :class:`.PeriodicInfo`.
The documentation of the optimizer will state if it does.

See :class:`.Cof` for more details and examples.

Expand Down Expand Up @@ -132,6 +134,15 @@ def get_periodic_info(self):

"""

warnings.warn(
'You called get_periodic_info() on a topology graph '
'instance. This method will be removed in any version '
'of stk released on, or after, 21/10/21. Please call '
'the construct() method instead. This will return a '
'PeriodicConstructionResult which provides the new '
'get_periodic_info() method.'
)

lattice_constants = self._get_lattice_constants()

return PeriodicInfo(
Expand Down
14 changes: 12 additions & 2 deletions src/stk/molecular/topology_graphs/cof/periodic_square.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import numpy as np
import warnings
from ...reactions import GenericReactionFactory
from .cof import Cof
from .vertices import _LinearCofVertex, _NonLinearCofVertex
Expand All @@ -28,8 +29,8 @@ class PeriodicSquare(Cof):
| 4-functional groups: 0
| 2-functional groups: 1 to 2

Note that :class:`.Optimizer` does not optimize the
:class:`.PeriodicInfo`.
Note that optimizers may not optimize the :class:`.PeriodicInfo`.
The documentation of the optimizer will state if it does.

See :class:`.Cof` for more details and examples.

Expand Down Expand Up @@ -133,6 +134,15 @@ def get_periodic_info(self):

"""

warnings.warn(
'You called get_periodic_info() on a topology graph '
'instance. This method will be removed in any version '
'of stk released on, or after, 21/10/21. Please call '
'the construct() method instead. This will return a '
'PeriodicConstructionResult which provides the new '
'get_periodic_info() method.'
)

lattice_constants = self._get_lattice_constants()

return PeriodicInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .vertex import * # noqa
from .topology_graph import * # noqa
from .optimizers import * # noqa
from .construction_result import * # noqa
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .construction_result import ConstructionResult
from .periodic import PeriodicConstructionResult


__all__ = [
'ConstructionResult',
'PeriodicConstructionResult',
]
Loading