sponge.control.Controller

View Source On Gitee
class sponge.control.Controller(system: Molecule, control_step: int = 1, **kwargs)[source]

Base class for the controller module in MindSPONGE. The Controller used in Updater to control the values of seven variables during the simulation process: coordinate, velocity, force, energy, kinetics, virial and pbc_box.

Parameters
  • system (Molecule) – Simulation system

  • control_step (int) – Step interval for controller execution. Default: 1

  • kwargs (dict) – Other parameters for extension

Inputs:
  • coordinate (Tensor) - Tensor of shape (B, A, D). Data type is float.

  • velocity (Tensor) - Tensor of shape (B, A, D). Data type is float.

  • force (Tensor) - Tensor of shape (B, A, D). Data type is float.

  • energy (Tensor) - Tensor of shape (B, 1). Data type is float.

  • kinetics (Tensor) - Tensor of shape (B, D). Data type is float.

  • virial (Tensor) - Tensor of shape (B, D). Data type is float.

  • pbc_box (Tensor) - Tensor of shape (B, D). Data type is float.

  • step (int) - Simulation step. Default: 0

Outputs:
  • coordinate, Tensor of shape (B, A, D). Data type is float.

  • velocity, Tensor of shape (B, A, D). Data type is float.

  • force, Tensor of shape (B, A, D). Data type is float.

  • energy, Tensor of shape (B, 1). Data type is float.

  • kinetics, Tensor of shape (B, D). Data type is float.

  • virial, Tensor of shape (B, D). Data type is float.

  • pbc_box, Tensor of shape (B, D). Data type is float.

Note

B: Number of walkers in simulation. A: Number of atoms. D: Spatial dimension of the simulation system. Usually is 3.

Supported Platforms:

Ascend GPU

Examples

>>> from sponge import WithEnergyCell, UpdaterMD, Sponge
>>> from sponge.callback import RunInfo
>>> from sponge.control import Controller
>>> # system is the Molecule object defined by user.
>>> # potential is the Energy object defined by user.
>>> withenergy = WithEnergyCell(system, potential)
>>> updater = UpdaterMD(
...     system=system,
...     time_step=1e-3,
...     velocity=velocity,
...     integrator='velocity_verlet',
...     temperature=300,
... )
>>> mini = Sponge(withenergy, optimizer=updater)
>>> run_info = RunInfo(1)
>>> mini.run(5, callbacks=[run_info])
[MindSPONGE] Started simulation at 2024-03-22 14:21:27
[MindSPONGE] Step: 1, E_pot: 110.0423, E_kin: 46.251404, E_tot: 156.2937, Temperature: 337.1373
[MindSPONGE] Step: 2, E_pot: 107.28819, E_kin: 48.7815, E_tot: 156.0697, Temperature: 355.57977
[MindSPONGE] Step: 3, E_pot: 112.72148, E_kin: 43.82708, E_tot: 156.54855, Temperature: 319.46582
[MindSPONGE] Step: 4, E_pot: 119.34253, E_kin: 37.759735, E_tot: 157.10226, Temperature: 275.23953
[MindSPONGE] Step: 5, E_pot: 119.16531, E_kin: 37.857212, E_tot: 157.02252, Temperature: 275.95004
[MindSPONGE] Finished simulation at 2024-03-22 14:21:30
[MindSPONGE] Simulation time: 2.96 seconds.
--------------------------------------------------------------------------------
>>> class MyController(Controller):
...     def construct(self,
...                 coordinate: Tensor,
...                 velocity: Tensor,
...                 **kwargs):
...         return super().construct(coordinate, velocity/100, **kwargs)
>>> updater = UpdaterMD(
...     system=system,
...     time_step=1e-3,
...     velocity=velocity,
...     integrator='velocity_verlet',
...     temperature=300,
...     controller=MyController(system)
... )
>>> mini = Sponge(withenergy, optimizer=updater)
>>> mini.run(5, callbacks=[run_info])
[MindSPONGE] Started simulation at 2024-03-22 14:22:54
[MindSPONGE] Step: 1, E_pot: 110.0423, E_kin: 0.005846547, E_tot: 110.04814, Temperature: 0.042616848
[MindSPONGE] Step: 2, E_pot: 113.94534, E_kin: 0.005484298, E_tot: 113.95083, Temperature: 0.03997633
[MindSPONGE] Step: 3, E_pot: 117.96643, E_kin: 0.0051222043, E_tot: 117.97155, Temperature: 0.037336946
[MindSPONGE] Step: 4, E_pot: 115.83751, E_kin: 0.005331765, E_tot: 115.84284, Temperature: 0.038864482
[MindSPONGE] Step: 5, E_pot: 107.83249, E_kin: 0.006089137, E_tot: 107.83858, Temperature: 0.044385143
[MindSPONGE] Finished simulation at 2024-03-22 14:22:57
[MindSPONGE] Simulation time: 3.00 seconds.
--------------------------------------------------------------------------------
property boltzmann: float

Boltzmann constant in current unit.

Returns

float, Boltzmann constant in current unit.

get_com(coordinate: Tensor, keepdims: bool = True)[source]

Get coordinate of center of mass.

Parameters
  • coordinate (Tensor) – Tensor of atomic coordinates. Tensor shape is (B, A, D). Data type is float.

  • keepdims (bool) – If this is set to True, the second axis will be left in the result as dimensions with size one. Default: True.

Returns

Tensor, Tensor of the coordinate of the center of mass. Tensor shape is (B, A, D) or (B, D).

Data type is float.

get_com_velocity(velocity: Tensor, keepdims: bool = True)[source]

Calculate velocity of center of mass.

Parameters
  • velocity (Tensor) – Tensor of velocity. Tensor shape is (B, A, D). Data type is float.

  • keepdims (bool) – If this is set to True, the second axis will be left in the result as dimensions with size one. Default: True.

Returns

Tensor, Tensor of the velocity of the center of mass.

Tensor shape is (B, A, D) or (B, D). Data type is float.

get_kinetics(velocity: Tensor)[source]

Calculate kinetics according to velocity.

Parameters

velocity (Tensor) – Tensor of atomic velocities. Tensor shape is (B, A, D). Data type is float.

Returns

Tensor, Tensor of kinetics. Tensor shape is (B, D). Data type is float.

get_pressure(kinetics: Tensor, virial: Tensor, pbc_box: Tensor)[source]

Calculate pressure according to kinetics, viral and PBC box.

Parameters
  • kinetics (Tensor) – Tensor of kinetics. Tensor shape is (B, D). Data type is float.

  • virial (Tensor) – Tensor of virial. Tensor shape is (B, D). Data type is float.

  • pbc_box (Tensor) – Tensor of PBC box. Tensor shape is (B, D). Data type is float.

Returns

Tensor, Tensor of pressure. Tensor shape is (B, D). Data type is float.

get_temperature(kinetics: Tensor = None)[source]

Calculate temperature according to velocity.

Parameters

kinetics (Tensor) – Tensor of kinetics. Tensor shape is (B, D). Data type is float. Default: None.

Returns

Tensor, Tensor of temperature. The shape of the Tensor is (B). Data type is float.

get_volume(pbc_box: Tensor)[source]

Calculate volume according to PBC box

Parameters

pbc_box (Tensor) – Tensor of PBC box. Tensor shape is (B, D). Data type is float.

Returns

Tensor, Tensor of volume. The shape of the Tensor is (B), and the data type is float.

set_degrees_of_freedom(dofs: int)[source]

Set degrees of freedom (DOFs).

Parameters

dofs (int) – Degrees of freedom.

set_time_step(dt: float)[source]

Set simulation time step.

Parameters

dt (float) – Time step.

update_coordinate(coordinate: Tensor)[source]

Update the coordinate of the simulation system.

Parameters

coordinate (Tensor) – Tensor of atomic coordinates. Tensor shape is (B, A, D). Data type is float.

Returns

Tensor, has the same data type and shape as original coordinate.

update_pbc_box(pbc_box: Tensor)[source]

Update the parameter of PBC box.

Parameters

pbc_box (Tensor) – Tensor of PBC box. Tensor shape is (B, D). Data type is float.

Returns

Tensor, has the same data type and shape as original pbc_box.