• MindSponge分子动力学模拟——自定义控制器(2024.05)


    技术背景

    分子动力学模拟中的控制器(Controller)可以被用于修改模拟过程中的原子坐标和原子速度等参量,从而达到控制系统特定参量的目的。例如控温器可以用于实现NVT系综,控压器可用于实现NPT系综。而在MindSponge分子动力学模拟框架下,控温控压都可以基于控制器Controller来实现。关于更多的MindSponge分子动力学模拟框架的信息,如安装和基本使用等,可以阅读MindSponge专栏里面的文章。

    自定义Controller

    Controller位于control路径下,除了已经定义好的一些控温控压算法之外,用户还可以直接继承Controller来实现自己需要的系统控制算法。例如最简单的,这里我们定义一个缩放原子速度的控制器:

    class MyController(Controller):
        def construct(self, 
                      coordinate: Tensor,
                      velocity: Tensor,
                      **kwargs):
            return super().construct(coordinate, velocity/2, **kwargs)
    

    它的功能是保持原子当前位置不变,但是速度减半,简单表述就是:

    vcontrol=v2

    完整的MindSponge用例如下所示:

    from mindspore import context, Tensor
    # 选择MindSpore图模式,0号GPU硬件
    context.set_context(mode=context.GRAPH_MODE, device_target='GPU', device_id=0)
    from sponge import set_global_units, Sponge, Molecule, WithEnergyCell, UpdaterMD, ForceField
    from sponge.function import VelocityGenerator
    from sponge.callback import RunInfo
    from sponge.control import Controller
    # 自定义控制器
    class MyController(Controller):
        def construct(self, 
                      coordinate: Tensor,
                      velocity: Tensor,
                      **kwargs):
            return super().construct(coordinate, velocity/2, **kwargs)
    # 设置全局分子动力学模拟单位
    set_global_units('nm', 'kj/mol')
    # 根据内置模板生成水分子系统
    system = Molecule(template='water.spce.yaml')
    # 将分子沿X轴方向复制一份,现在体系有2个水分子
    system.reduplicate([0.3, 0, 0])
    # 基于前面的分子系统,再复制一份,然后合并到system里面,此时一共是4个水分子
    new_sys = system.copy([0, 0, -0.3])
    system.append(new_sys)
    # 选择SPCE力场
    potential = ForceField(system, parameters='SPCE')
    # 类似于深度学习中的WithLossCell,绑定了分子系统和力场的信息
    withenergy = WithEnergyCell(system, potential)
    # 配置MD参数
    temp = 300
    vgen = VelocityGenerator(temp)
    velocity = vgen(system.shape, system.atom_mass)
    # 构建迭代器,并且传入我们自定义的控制器
    updater = UpdaterMD(
        system=system,
        time_step=1e-3,
        velocity=velocity,
        integrator='velocity_verlet',
        temperature=300,
        controller=MyController(system),
    )
    # 绑定系统、力场和迭代器三者的内容
    mini = Sponge(withenergy, optimizer=updater)
    # 使用回调函数,每一步都会打印输出能量、温度等信息
    run_info = RunInfo(1)
    mini.run(5, callbacks=[run_info])
    

    因为每次控制器被调用都会使得速度减半,而且按照执行的顺序,控制器在常规的控温之后,因此运行过程中体系的温度无法达到我们预期的300K:

    [MindSPONGE] Started simulation at 2024-05-15 16:07:54
    [MindSPONGE] Step: 1, E_pot: 110.0423, E_kin: 11.559962, E_tot: 121.60226, Temperature: 84.26327
    [MindSPONGE] Step: 2, E_pot: 111.15905, E_kin: 11.305634, E_tot: 122.46468, Temperature: 82.40941
    [MindSPONGE] Step: 3, E_pot: 121.5491, E_kin: 8.962629, E_tot: 130.51173, Temperature: 65.330696
    [MindSPONGE] Step: 4, E_pot: 126.55731, E_kin: 7.8556476, E_tot: 134.41296, Temperature: 57.26165
    [MindSPONGE] Step: 5, E_pot: 118.11452, E_kin: 9.807281, E_tot: 127.9218, Temperature: 71.48756
    [MindSPONGE] Finished simulation at 2024-05-15 16:07:57
    [MindSPONGE] Simulation time: 3.06 seconds.
    --------------------------------------------------------------------------------
    

    如果去掉这个降低速度的控制器,那么体系的温度和动能一下子就上去了,并且在稳定之后,温度会逐渐收敛在300K附近:

    [MindSPONGE] Started simulation at 2024-05-15 16:09:10
    [MindSPONGE] Step: 1, E_pot: 110.0423, E_kin: 64.28456, E_tot: 174.32686, Temperature: 468.5852
    [MindSPONGE] Step: 2, E_pot: 116.11336, E_kin: 58.616272, E_tot: 174.72963, Temperature: 427.26773
    [MindSPONGE] Step: 3, E_pot: 120.5004, E_kin: 54.469967, E_tot: 174.97037, Temperature: 397.04434
    [MindSPONGE] Step: 4, E_pot: 119.11376, E_kin: 55.627754, E_tot: 174.74152, Temperature: 405.48373
    [MindSPONGE] Step: 5, E_pot: 114.84599, E_kin: 59.536713, E_tot: 174.3827, Temperature: 433.97702
    [MindSPONGE] Finished simulation at 2024-05-15 16:09:12
    [MindSPONGE] Simulation time: 2.84 seconds.
    --------------------------------------------------------------------------------
    

    总结概要

    本文介绍了在MindSponge分子动力学模拟框架先实现自定义Controller控制器的方法,通过调控体系中的原子坐标和原子速度等,来控制系综的参量。MindSponge分子模拟框架基于MindSpore深度学习框架开发而成,对于开发者尤其是深度学习开发者来说,非常的友好。

    版权声明

    本文首发链接为:https://www.cnblogs.com/dechinphy/p/controller.html

    作者ID:DechinPhy

    更多原著文章:https://www.cnblogs.com/dechinphy/

    请博主喝咖啡:https://www.cnblogs.com/dechinphy/gallery/image/379634.html

  • 相关阅读:
    FEBAN 增强
    docker安装nextcloud+onlyoffice+https
    AutoSAR(基础入门篇)13.3-Mcal Dio配置
    常见vue问题
    笑看k8s基础存储卷妙用
    18.Tornado_个人信息案例
    多层高速PCB设计学习笔记(二)基本设计原则及EMC分析
    CSS媒体查询-物理像素-逻辑像素
    [PyTorch][chapter 56][GAN 代码实现]
    Java Spring Cloud VII 之 内置断言
  • 原文地址:https://www.cnblogs.com/dechinphy/p/18096072/controller