diff --git a/MindSPONGE/src/sponge/colvar/atoms/atoms.py b/MindSPONGE/src/sponge/colvar/atoms/atoms.py index 78fe10076cd5aebe74a9f98c08768b25232597f8..2ad41b7e0fbddb5cceb308560a3403e2f8be660c 100644 --- a/MindSPONGE/src/sponge/colvar/atoms/atoms.py +++ b/MindSPONGE/src/sponge/colvar/atoms/atoms.py @@ -174,6 +174,22 @@ class Atoms(AtomsBase): Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> import numpy as np + >>> from mindspore import Tensor + >>> from sponge.colvar import Atoms + >>> crd = Tensor(np.random.random((4, 3)), ms.float32) + >>> crd + Tensor(shape=[4, 3], dtype=Float32, value= + [[ 2.47492954e-01, 9.78153408e-01, 1.44034222e-01], + [ 2.36211464e-01, 3.35842371e-01, 8.39536846e-01], + [ 8.82235169e-01, 5.98322928e-01, 6.68052316e-01], + [ 7.17712820e-01, 4.72498119e-01, 1.69098437e-01]]) + >>> atom_1 = Atoms(0) + >>> atom_1(crd) + Tensor(shape=[1, 3], dtype=Float32, value= + [[ 2.47492954e-01, 9.78153408e-01, 1.44034222e-01]]) """ def __init__(self, index: Union[Tensor, ndarray, List[int]], diff --git a/MindSPONGE/src/sponge/colvar/atoms/center.py b/MindSPONGE/src/sponge/colvar/atoms/center.py index 6c4c3a8fbe63f1c8ed5e6423e7ae120f079ba6ac..b99bf560a89805a2ffac66e1e5fdb3c4f199faaf 100644 --- a/MindSPONGE/src/sponge/colvar/atoms/center.py +++ b/MindSPONGE/src/sponge/colvar/atoms/center.py @@ -71,6 +71,18 @@ class Center(AtomsBase): Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> import numpy as np + >>> from mindspore import Tensor + >>> from sponge.colvar import Center + >>> crd = Tensor(np.random.random((4, 3)), ms.float32) + >>> mass = Tensor(np.random.random(4), ms.float32) + >>> atoms = Tensor([0, 2], ms.int32) + >>> ct = Center(atoms, mass[atoms]) + >>> ct(crd) + Tensor(shape=[1, 3], dtype=Float32, value= + [[ 7.61003494e-01, 6.70868099e-01, 5.67968249e-01]]) """ def __init__(self, atoms: Union[AtomsBase, Tensor, ndarray, list], @@ -120,7 +132,7 @@ class Center(AtomsBase): def set_mass(self, mass: Tensor, batched: bool = False): """set the mass of atoms""" - self.mass = get_ms_array(mass, ms.int32) + self.mass = get_ms_array(mass, ms.float32) if self.mass is None: self.total_mass = None else: @@ -153,7 +165,6 @@ class Center(AtomsBase): """ # (B, ..., G, D) <- (B, A, D) atoms = self.atoms(coordinate, pbc_box) - if self.mass is None: # (B, ..., 1, D) or (B, ..., D) <- (B, ..., G, D) center = self.reduce_mean(atoms, self.axis) diff --git a/MindSPONGE/src/sponge/colvar/atoms/vector.py b/MindSPONGE/src/sponge/colvar/atoms/vector.py index 000fca3cc3ac8afaad95e132c5fd8f86236cdf89..bfc7c0f53af0cc07640c551a0eb561b8f643d60f 100644 --- a/MindSPONGE/src/sponge/colvar/atoms/vector.py +++ b/MindSPONGE/src/sponge/colvar/atoms/vector.py @@ -70,6 +70,22 @@ class Vector(AtomsBase): Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> import numpy as np + >>> from mindspore import Tensor + >>> from sponge.colvar import Vector + >>> crd = Tensor(np.random.random((4, 3)), ms.float32) + >>> crd + Tensor(shape=[4, 3], dtype=Float32, value= + [[ 2.47492954e-01, 9.78153408e-01, 1.44034222e-01], + [ 2.36211464e-01, 3.35842371e-01, 8.39536846e-01], + [ 8.82235169e-01, 5.98322928e-01, 6.68052316e-01], + [ 7.17712820e-01, 4.72498119e-01, 1.69098437e-01]]) + >>> vc02 = Vector(atoms0=[0], atoms1=[2]) + >>> vc02(crd) + Tensor(shape=[1, 3], dtype=Float32, value= + [[ 6.34742200e-01, -3.79830480e-01, 5.24018109e-01]]) """ def __init__(self, atoms: AtomsBase = None, diff --git a/MindSPONGE/src/sponge/colvar/basic/angle.py b/MindSPONGE/src/sponge/colvar/basic/angle.py index d0e5eab3ca8a97af25bc188bb842ebe3db7da593..5285c301e31d6c3cdfc5125b0e492c502d14641e 100644 --- a/MindSPONGE/src/sponge/colvar/basic/angle.py +++ b/MindSPONGE/src/sponge/colvar/basic/angle.py @@ -81,6 +81,22 @@ class Angle(Colvar): Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> from sponge import Sponge + >>> from sponge.colvar import Angle + >>> from sponge.callback import RunInfo + >>> cv_angle = Angle([0, 1, 2]) + >>> # system is the Molecule object defined by user. + >>> # energy is the Energy object defined by user. + >>> # opt is the Optimizer object defined by user. + >>> md = Sponge(system, potential=energy, optimizer=opt, metrics={'angle': cv_angle}) + >>> run_info = RunInfo(1000) + >>> md.run(2000, callbacks=[run_info]) + [MindSPONGE] Started simulation at 2024-02-19 15:43:11 + [MindSPONGE] Step: 1000, E_pot: -117.30916, angle: 1.9461793 + [MindSPONGE] Step: 2000, E_pot: -131.60872, angle: 1.9336755 + [MindSPONGE] Finished simulation at 2024-02-19 15:44:03 + [MindSPONGE] Simulation time: 51.27 seconds. """ def __init__(self, diff --git a/MindSPONGE/src/sponge/colvar/basic/distance.py b/MindSPONGE/src/sponge/colvar/basic/distance.py index f6d5742b32c9ab96f9203e022456615a55be1e90..37b4f59a0ba2136ca0e9017ae03c59067362eeea 100644 --- a/MindSPONGE/src/sponge/colvar/basic/distance.py +++ b/MindSPONGE/src/sponge/colvar/basic/distance.py @@ -72,6 +72,22 @@ class Distance(Colvar): Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> from sponge import Sponge + >>> from sponge.colvar import Distance + >>> from sponge.callback import RunInfo + >>> cv_bond = Distance([0, 1]) + >>> # system is the Molecule object defined by user. + >>> # energy is the Energy object defined by user. + >>> # opt is the Optimizer object defined by user. + >>> md = Sponge(system, potential=energy, optimizer=opt, metrics={'bond': cv_bond}) + >>> run_info = RunInfo(1000) + >>> md.run(2000, callbacks=[run_info]) + [MindSPONGE] Started simulation at 2024-02-19 15:43:11 + [MindSPONGE] Step: 1000, E_pot: -117.30916, bond: 1.4806036 + [MindSPONGE] Step: 2000, E_pot: -131.60872, bond: 1.4821533 + [MindSPONGE] Finished simulation at 2024-02-19 15:44:03 + [MindSPONGE] Simulation time: 51.27 seconds. """ def __init__(self, diff --git a/MindSPONGE/src/sponge/colvar/basic/torsion.py b/MindSPONGE/src/sponge/colvar/basic/torsion.py index 6cb73d2948426042272917ef19c9fe38ede767bf..998fa56915c0e2493d6f751bf9c5d9ee297757a9 100644 --- a/MindSPONGE/src/sponge/colvar/basic/torsion.py +++ b/MindSPONGE/src/sponge/colvar/basic/torsion.py @@ -91,6 +91,22 @@ class Torsion(Colvar): Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> from sponge import Sponge + >>> from sponge.colvar import Torsion + >>> from sponge.callback import RunInfo + >>> cv_dihedral = Torsion([0, 1, 2, 3]) + >>> # system is the Molecule object defined by user. + >>> # energy is the Energy object defined by user. + >>> # opt is the Optimizer object defined by user. + >>> md = Sponge(system, potential=energy, optimizer=opt, metrics={'dihedral': cv_dihedral}) + >>> run_info = RunInfo(1000) + >>> md.run(2000, callbacks=[run_info]) + [MindSPONGE] Started simulation at 2024-02-19 15:43:11 + [MindSPONGE] Step: 1000, E_pot: -117.30916, dihedral: 2.1488183 + [MindSPONGE] Step: 2000, E_pot: -131.60872, dihedral: 2.143513 + [MindSPONGE] Finished simulation at 2024-02-19 15:44:03 + [MindSPONGE] Simulation time: 51.27 seconds. """ def __init__(self, diff --git a/MindSPONGE/src/sponge/control/controller.py b/MindSPONGE/src/sponge/control/controller.py index ec88fe298a8066f77890e77408eb72daa7a4f433..6612de1e224ebd765bd13550be56c29e25e6eeac 100644 --- a/MindSPONGE/src/sponge/control/controller.py +++ b/MindSPONGE/src/sponge/control/controller.py @@ -76,6 +76,58 @@ class Controller(Cell): 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. + -------------------------------------------------------------------------------- """ def __init__(self, system: Molecule, diff --git a/MindSPONGE/src/sponge/core/simulation/energy.py b/MindSPONGE/src/sponge/core/simulation/energy.py index cc068af9c04be924f505efb569d6c8ae2642ecd0..0facbe7bac9e9a6d8e5bb06512d42cefc352858b 100644 --- a/MindSPONGE/src/sponge/core/simulation/energy.py +++ b/MindSPONGE/src/sponge/core/simulation/energy.py @@ -73,6 +73,23 @@ class WithEnergyCell(Cell): Supported Platforms: ``Ascend`` ``GPU`` + + Examples: + >>> from sponge import WithEnergyCell, RunOneStepCell, Sponge + >>> from sponge.callback import RunInfo + >>> # system is the Molecule object defined by user. + >>> # energy is the Energy object defined by user. + >>> # opt is the Optimizer object defined by user. + >>> sim = WithEnergyCell(system, energy) + >>> one_step = RunOneStepCell(energy=sim, optimizer=opt) + >>> md = Sponge(one_step) + >>> run_info = RunInfo(800) + >>> md.run(2000, callbacks=[run_info]) + [MindSPONGE] Started simulation at 2023-09-04 17:06:26 + [MindSPONGE] Step: 800, E_pot: -150.88245, E_kin: 69.84598, E_tot: -81.03647, Temperature: 418.42694 + [MindSPONGE] Step: 1600, E_pot: -163.72491, E_kin: 57.850487, E_tot: -105.87443, Temperature: 346.56543 + [MindSPONGE] Finished simulation at 2023-09-04 17:07:13 + [MindSPONGE] Simulation time: 47.41 seconds. """ def __init__(self, diff --git a/MindSPONGE/src/sponge/core/simulation/run.py b/MindSPONGE/src/sponge/core/simulation/run.py index 3bb79bdbd502d35254d4d8e85295817a6934cf5e..e67e1f05131588ee00fdede30e9fc07510b19110 100644 --- a/MindSPONGE/src/sponge/core/simulation/run.py +++ b/MindSPONGE/src/sponge/core/simulation/run.py @@ -75,6 +75,23 @@ class RunOneStepCell(Cell): Supported Platforms: ``Ascend`` ``GPU`` + + Examples: + >>> from sponge import WithEnergyCell, RunOneStepCell, Sponge + >>> from sponge.callback import RunInfo + >>> # system is the Molecule object defined by user. + >>> # energy is the Energy object defined by user. + >>> # opt is the Optimizer object defined by user. + >>> sim = WithEnergyCell(system, energy) + >>> one_step = RunOneStepCell(energy=sim, optimizer=opt) + >>> md = Sponge(one_step) + >>> run_info = RunInfo(800) + >>> md.run(2000, callbacks=[run_info]) + [MindSPONGE] Started simulation at 2023-09-04 17:06:26 + [MindSPONGE] Step: 800, E_pot: -150.88245, E_kin: 69.84598, E_tot: -81.03647, Temperature: 418.42694 + [MindSPONGE] Step: 1600, E_pot: -163.72491, E_kin: 57.850487, E_tot: -105.87443, Temperature: 346.56543 + [MindSPONGE] Finished simulation at 2023-09-04 17:07:13 + [MindSPONGE] Simulation time: 47.41 seconds. """ def __init__(self, energy: WithEnergyCell = None, diff --git a/MindSPONGE/src/sponge/function/functions.py b/MindSPONGE/src/sponge/function/functions.py index 1369214c5d2c573151111db359571b88305bae13..49f11fde05643949e6efcf834f7a5a0a16735f62 100644 --- a/MindSPONGE/src/sponge/function/functions.py +++ b/MindSPONGE/src/sponge/function/functions.py @@ -625,6 +625,25 @@ def coordinate_in_pbc(position: Tensor, pbc_box: Tensor, offset: float = 0) -> T Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> import numpy as np + >>> from mindspore import Tensor + >>> from sponge.function import coordinate_in_pbc + >>> crd = Tensor(np.random.random((4, 3)), ms.float32) + >>> pbc_box = Tensor([[0.5, 0.5, 0.5]], ms.float32) + >>> crd + Tensor(shape=[4, 3], dtype=Float32, value= + [[ 4.94492769e-01, 4.85243529e-01, 1.63403198e-01], + [ 5.60526669e-01, 6.17091954e-01, 3.65307808e-01], + [ 7.81092644e-01, 3.17117482e-01, 1.41929969e-01], + [ 9.59174633e-01, 3.53236049e-02, 4.85624045e-01]]) + >>> coordinate_in_pbc(crd, pbc_box) + Tensor(shape=[4, 3], dtype=Float32, value= + [[ 4.94492769e-01, 4.85243529e-01, 1.63403198e-01], + [ 6.05266690e-02, 1.17091954e-01, 3.65307808e-01], + [ 2.81092644e-01, 3.17117482e-01, 1.41929969e-01], + [ 4.59174633e-01, 3.53236049e-02, 4.85624045e-01]]) """ pbc_box = pbc_box_reshape(F.stop_gradient(pbc_box), position.ndim) @@ -703,6 +722,16 @@ def calc_vector_pbc(initial: Tensor, terminal: Tensor, pbc_box: Tensor) -> Tenso Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> import numpy as np + >>> from mindspore import Tensor + >>> from sponge.function import calc_vector_pbc + >>> crd = Tensor(np.random.random((4, 3)), ms.float32) + >>> pbc_box = Tensor([[3, 3, 3]], ms.float32) + >>> calc_vector_pbc(crd[0], crd[1], pbc_box) + Tensor(shape=[1, 3], dtype=Float32, value= + [[ 6.60338998e-02, 1.31848425e-01, 2.01904625e-01]]) """ return vector_in_pbc(terminal-initial, pbc_box) @@ -727,6 +756,18 @@ def calc_vector(initial: Tensor, terminal: Tensor, pbc_box: Tensor = None) -> Te Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> import numpy as np + >>> from mindspore import Tensor + >>> from sponge.function import calc_vector + >>> crd = Tensor(np.random.random((4, 3)), ms.float32) + >>> pbc_box = Tensor([[3, 3, 3]], ms.float32) + >>> calc_vector(crd[0], crd[1]) + Tensor(shape=[3], dtype=Float32, value= [ 6.60338998e-02, 1.31848425e-01, 2.01904610e-01]) + >>> calc_vector(crd[0], crd[1], pbc_box) + Tensor(shape=[1, 3], dtype=Float32, value= + [[ 6.60338998e-02, 1.31848425e-01, 2.01904625e-01]]) """ vector = terminal - initial @@ -791,6 +832,15 @@ def calc_distance_pbc(position_a: Tensor, Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> from mindspore import Tensor + >>> from sponge.function import calc_distance_pbc + >>> tensor_a = Tensor([[1, 2, 3], [4, 5, 6]], ms.float32) + >>> tensor_b = Tensor([[1, 2, 4], [6, 8, 10]], ms.float32) + >>> pbc_box = Tensor([[3, 3, 3]], ms.float32) + >>> calc_distance_pbc(tensor_a, tensor_b, pbc_box) + Tensor(shape=[2], dtype=Float32, value= [ 1.00000000e+00, 1.41421354e+00]) """ vec = calc_vector_pbc(position_a, position_b, pbc_box) @@ -822,6 +872,17 @@ def calc_distance(position_a: Tensor, Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> from mindspore import Tensor + >>> from sponge.function import calc_distance + >>> tensor_a = Tensor([[1, 2, 3], [4, 5, 6]], ms.float32) + >>> tensor_b = Tensor([[1, 2, 4], [6, 8, 10]], ms.float32) + >>> calc_distance(tensor_a, tensor_b) + Tensor(shape=[2], dtype=Float32, value= [ 1.00000000e+00, 5.38516474e+00]) + >>> pbc_box = Tensor([[3, 3, 3]], ms.float32) + >>> calc_distance(tensor_a, tensor_b, pbc_box) + >>> Tensor(shape=[2], dtype=Float32, value= [ 1.00000000e+00, 1.41421354e+00]) """ vec = calc_vector_nopbc(position_a, position_b) @@ -939,6 +1000,15 @@ def calc_angle_pbc(position_a: Tensor, Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> from mindspore import Tensor + >>> import numpy as np + >>> from sponge.function import calc_angle_pbc + >>> crd = Tensor(np.random.random((3, 3)), ms.float32) + >>> pbc_box = Tensor([[3, 3, 3]], ms.float32) + >>> calc_angle_pbc(crd[0], crd[1], crd[2], pbc_box) + >>> Tensor(shape=[1], dtype=Float32, value= [ 6.17621064e-01]) """ # (B, ..., D) @@ -979,6 +1049,15 @@ def calc_angle(position_a: Tensor, Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> from mindspore import Tensor + >>> import numpy as np + >>> from sponge.function import calc_angle + >>> crd = Tensor(np.random.random((3, 3)), ms.float32) + >>> pbc_box = Tensor([[3, 3, 3]], ms.float32) + >>> calc_angle(crd[0], crd[1], crd[2], pbc_box) + >>> Tensor(shape=[1], dtype=Float32, value= [ 6.17621064e-01]) """ if pbc_box is None: @@ -1110,6 +1189,15 @@ def calc_torsion_pbc(position_a: Tensor, Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> import numpy as np + >>> from mindspore import Tensor + >>> from sponge.function import calc_torsion_pbc + >>> crd = Tensor(np.random.random((4, 3)), ms.float32) + >>> pbc_box = Tensor([[3, 3, 3]], ms.float32) + >>> calc_torsion_pbc(crd[0], crd[1], crd[2], crd[3], pbc_box) + Tensor(shape=[1], dtype=Float32, value= [-2.33294296e+00]) """ vec_ba = calc_vector_pbc(position_b, position_a, pbc_box) @@ -1153,6 +1241,15 @@ def calc_torsion(position_a: Tensor, Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> import mindspore as ms + >>> import numpy as np + >>> from mindspore import Tensor + >>> from sponge.function import calc_torsion + >>> crd = Tensor(np.random.random((4, 3)), ms.float32) + >>> pbc_box = Tensor([[3, 3, 3]], ms.float32) + >>> calc_torsion(crd[0], crd[1], crd[2], crd[3], pbc_box) + Tensor(shape=[1], dtype=Float32, value= [-2.33294296e+00]) """ if pbc_box is None: diff --git a/MindSPONGE/src/sponge/function/operations.py b/MindSPONGE/src/sponge/function/operations.py index 2d00c4cb5cb42ed75d5883535ee3fddf15400dfe..99f3cc320636acd236aecbadb97b7ea8fa108ccf 100644 --- a/MindSPONGE/src/sponge/function/operations.py +++ b/MindSPONGE/src/sponge/function/operations.py @@ -55,6 +55,20 @@ class GetVector(Cell): conditions based on whether the ``pbc_box`` is provided. Default: ``None`` . + Examples: + >>> import mindspore as ms + >>> import numpy as np + >>> from sponge.function import GetVector + >>> from mindspore import Tensor + >>> crd = Tensor(np.random.random((4, 3)), ms.float32) + >>> pbc_box = Tensor([[0.5, 0.5, 0.5]], ms.float32) + >>> gd = GetVector(use_pbc=True) + >>> gd(crd[0], crd[1], pbc_box) + Tensor(shape=[1, 3], dtype=Float32, value= + [[ 5.14909625e-02, -4.62748706e-02, -1.20763242e-01]]) + >>> gd = GetVector(use_pbc=False) + >>> gd(crd[0], crd[1]) + Tensor(shape=[3], dtype=Float32, value= [ 5.14909625e-02, 4.53725129e-01, -1.20763242e-01]) """ def __init__(self, use_pbc: bool = None): @@ -196,6 +210,19 @@ class GetDistance(GetVector): axis (int): The axis of the space dimension of the coordinate. Default: ``-1`` . + Examples: + >>> import mindspore as ms + >>> import numpy as np + >>> from sponge.function import GetDistance + >>> from mindspore import Tensor + >>> crd = Tensor(np.random.random((4, 3)), ms.float32) + >>> pbc_box = Tensor([[0.5, 0.5, 0.5]], ms.float32) + >>> gd = GetDistance(use_pbc=True, keepdims=False) + >>> gd(crd[0], crd[1], pbc_box) + Tensor(shape=[1], dtype=Float32, value= [ 1.39199302e-01]) + >>> gd = GetDistance(use_pbc=False, keepdims=False) + >>> gd(crd[0], crd[1]) + Tensor(shape=[], dtype=Float32, value= 0.472336) """ def __init__(self, @@ -231,7 +258,10 @@ class GetDistance(GetVector): distance (Tensor): Tensor of shape (B, ...). Data type is float. """ - vector = self.calc_vector(initial, terminal, pbc_box) + if self._use_pbc: + vector = self.calc_vector(initial, terminal, pbc_box) + else: + vector = self.calc_vector(initial, terminal) if self.norm is None: return ops.norm(vector, None, self.axis, self.keepdims) @@ -255,6 +285,17 @@ class VelocityGenerator(Cell): length_unit (str): Length unit. Default: ``None`` energy_unit (str): energy unit. Default: ``None`` + Examples: + >>> from sponge import UpdaterMD + >>> from sponge.function import VelocityGenerator + >>> vgen = VelocityGenerator(300) + >>> velocity = vgen(system.shape, system.atom_mass) + >>> opt = UpdaterMD(system=system, + ... time_step=1e-3, + ... velocity=velocity, + ... integrator='velocity_verlet', + ... temperature=300, + ... thermostat='langevin') """ #pylint: disable=invalid-name diff --git a/MindSPONGE/src/sponge/function/units.py b/MindSPONGE/src/sponge/function/units.py index be52bd1797883c78190292bb90697cf84e80ed54..82e4e0ab6bcfc6ad0b2c2647e58c39e4d8b57e7b 100644 --- a/MindSPONGE/src/sponge/function/units.py +++ b/MindSPONGE/src/sponge/function/units.py @@ -309,6 +309,14 @@ class Energy: Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` + + Examples: + >>> from sponge.function import Energy + >>> ene = Energy(1.0, 'kcal/mol') + >>> ene.change_unit('kj/mol') + + >>> ene.value + 4.184 """ def __init__(self, value: float, unit: str = 'kj/mol', **kwargs): @@ -657,6 +665,18 @@ class Units: Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` + + Examples: + >>> from sponge.function import Units + >>> unit = Units(length_unit='nm', energy_unit='kj/mol') + >>> unit.convert_energy_to('kcal/mol') + 0.2390057361376673 + >>> unit.convert_energy_from('kcal/mol') + 4.184 + >>> unit.convert_length_to('A') + 10.0 + >>> unit.convert_length_from('A') + 0.1 """ def __init__(self, @@ -1098,6 +1118,13 @@ def get_energy(energy: Union[Energy, float], unit: Union[str, Units] = None) -> Returns: Float, a tensor of energy in specific unit. + + Examples: + >>> from sponge.function import get_energy + >>> get_energy(ene) + 4.184 + >>> get_energy(ene, 'kcal/mol') + 1.0 """ if isinstance(energy, dict): energy = Energy(**energy) @@ -1153,6 +1180,11 @@ def set_global_units(length_unit: Union[str, Units, Length, float, int] = None, Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` + + Examples: + >>> from sponge import set_global_units + >>> set_global_units('nm', 'kj/mol') + """ global GLOBAL_UNITS GLOBAL_UNITS.set_units(length_unit, energy_unit, units) diff --git a/MindSPONGE/src/sponge/optimizer/md.py b/MindSPONGE/src/sponge/optimizer/md.py index ad8a66236479406f9b05566f7e6e03bf5e18612b..be34caa727dcb7a94d5af43927bbc0cc1a540ff6 100644 --- a/MindSPONGE/src/sponge/optimizer/md.py +++ b/MindSPONGE/src/sponge/optimizer/md.py @@ -91,6 +91,19 @@ class UpdaterMD(Updater): A: Number of atoms. D: Spatial dimension of the simulation system. Usually is 3. + + Examples: + >>> from sponge import UpdaterMD + >>> from sponge.function import VelocityGenerator + >>> vgen = VelocityGenerator(300) + >>> # system表示自定义的分子系统 + >>> velocity = vgen(system.shape, system.atom_mass) + >>> opt = UpdaterMD(system=system, + ... time_step=1e-3, + ... velocity=velocity, + ... integrator='velocity_verlet', + ... temperature=300, + ... thermostat='langevin') """ @opt_init_args_register diff --git a/MindSPONGE/src/sponge/potential/energy/energy.py b/MindSPONGE/src/sponge/potential/energy/energy.py index f764aac1a94e93b971c2ac0cdfdf15c8df30931d..0528a1b69e7b63e7725e4aca3d2e3b6ea28d87c6 100644 --- a/MindSPONGE/src/sponge/potential/energy/energy.py +++ b/MindSPONGE/src/sponge/potential/energy/energy.py @@ -77,6 +77,29 @@ class EnergyCell(Cell): Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> from mindspore import Tensor + >>> from mindspore.nn import Adam + >>> from sponge.potential import EnergyCell, ForceFieldBase + >>> from sponge import WithEnergyCell, Sponge + >>> from sponge.callback import RunInfo + >>> class MyEnergy(EnergyCell): + ... def construct(self, coordinate: Tensor, **kwargs): + ... return coordinate.sum()[None, None] + >>> # system表示自定义的分子系统 + >>> potential = MyEnergy(system) + >>> forcefield = ForceFieldBase(potential) + >>> withenergy = WithEnergyCell(system, forcefield) + >>> opt = Adam(system.trainable_params(), 1e-3) + >>> mini = Sponge(withenergy, optimizer=opt) + >>> run_info = RunInfo(5) + >>> mini.run(10, callbacks=[run_info]) + [MindSPONGE] Started simulation at 2024-03-22 11:08:34 + [MindSPONGE] Step: 5, E_pot: 0.31788814 + [MindSPONGE] Step: 10, E_pot: 0.13788882 + [MindSPONGE] Finished simulation at 2024-03-22 11:08:35 + [MindSPONGE] Simulation time: 0.98 seconds. + -------------------------------------------------------------------------------- """ def __init__(self, name: str = 'energy', diff --git a/MindSPONGE/src/sponge/system/molecule/molecule.py b/MindSPONGE/src/sponge/system/molecule/molecule.py index d64400881587f377be3f2f3f9de10d8128702a35..b9c20720522cdbc142b64f6364f1585395b1c08a 100644 --- a/MindSPONGE/src/sponge/system/molecule/molecule.py +++ b/MindSPONGE/src/sponge/system/molecule/molecule.py @@ -120,6 +120,30 @@ class Molecule(Cell): Supported Platforms: ``Ascend`` ``GPU`` + Examples: + >>> from sponge import Molecule + >>> system = Molecule(atoms=['O', 'H', 'H'], + ... coordinate=[[0, 0, 0], [0.1, 0, 0], [-0.0333, 0.0943, 0]], + ... bonds=[[[0, 1], [0, 2]]]) + >>> print ('The number of atoms in the system is: ', system.num_atoms) + The number of atoms in the system is: 3 + >>> print ('All the atom names in the system are: ', system.atom_name) + All the atom names in the system are: [['O' 'H' 'H']] + >>> print ('The coordinates of atoms are: \n{}'.format(system.coordinate.asnumpy())) + The coordinates of atoms are: + [[[ 0. 0. 0. ] + [ 0.1 0. 0. ] + [-0.0333 0.0943 0. ]]] + >>> system = Molecule(template='water.spce.yaml') + >>> print ('The number of atoms in the system is: ', system.num_atoms) + The number of atoms in the system is: 3 + >>> print ('All the atom names in the system are: ', system.atom_name) + All the atom names in the system are: [['O' 'H1' 'H2']] + >>> print ('The coordinates of atoms are: \n{}'.format(system.coordinate.asnumpy())) + The coordinates of atoms are: + [[[ 0. 0. 0. ] + [ 0.08164904 0.0577359 0. ] + [-0.08164904 0.0577359 0. ]]] """ diff --git a/MindSPONGE/src/sponge/system/molecule/protein.py b/MindSPONGE/src/sponge/system/molecule/protein.py index fcc8c9458a03930bd038da59a3189f5fc16d4f24..73d0594f67e13d130011576d3f7e0c38761aab14 100644 --- a/MindSPONGE/src/sponge/system/molecule/protein.py +++ b/MindSPONGE/src/sponge/system/molecule/protein.py @@ -85,6 +85,19 @@ class Protein(Molecule): D: Spatial dimension of the simulation system. Usually is 3. + Examples: + >>> from sponge import Protein + >>> system = Protein('case1.pdb', rebuild_hydrogen=True) + [MindSPONGE] Adding 57 hydrogen atoms for the protein molecule in 0.007 seconds. + >>> print ('The number of atoms in the system is: ', system.num_atoms) + The number of atoms in the system is: 57 + >>> print ('All the atom names in the system are: ', system.atom_name) + All the atom names in the system are: [['N' 'CA' 'CB' 'C' 'O' 'H1' 'H2' 'H3' 'HA' 'HB1' 'HB2' 'HB3' 'N' 'CA' + 'CB' 'CG' 'CD' 'NE' 'CZ' 'NH1' 'NH2' 'C' 'O' 'H' 'HA' 'HB2' 'HB3' 'HG2' + 'HG3' 'HD2' 'HD3' 'HE' 'HH11' 'HH12' 'HH21' 'HH22' 'N' 'CA' 'CB' 'C' + 'O' 'H' 'HA' 'HB1' 'HB2' 'HB3' 'N' 'CA' 'CB' 'C' 'O' 'OXT' 'H' 'HA' + 'HB1' 'HB2' 'HB3']] + """ def __init__(self,