HarmonicBondJaxGenerator

Source code in dmff/generators/classical.py
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class HarmonicBondJaxGenerator:
    def __init__(self, ff: Hamiltonian):
        self.name = "HarmonicBondForce"
        self.ff: Hamiltonian = ff
        self.fftree: ForcefieldTree = ff.fftree
        self.paramtree: Dict = ff.paramtree

    def extract(self):
        """
        extract forcefield paramters from ForcefieldTree. 
        """
        lengths = self.fftree.get_attribs(f"{self.name}/Bond", "length")
        # get_attribs will return a list of list.
        ks = self.fftree.get_attribs(f"{self.name}/Bond", "k")
        self.paramtree[self.name] = {}
        self.paramtree[self.name]["length"] = jnp.array(lengths)
        self.paramtree[self.name]["k"] = jnp.array(ks)

    def overwrite(self):
        """
        update parameters in the fftree by using paramtree of this generator.
        """
        self.fftree.set_attrib(f"{self.name}/Bond", "length",
                               self.paramtree[self.name]["length"])
        self.fftree.set_attrib(f"{self.name}/Bond", "k",
                               self.paramtree[self.name]["k"])

    def createForce(self, sys, data, nonbondedMethod, nonbondedCutoff, args):
        """
        This method will create a potential calculation kernel. It usually should do the following:

        1. Match the corresponding bond parameters according to the atomic types at both ends of each bond.

        2. Create a potential calculation kernel, and pass those mapped parameters to the kernel.

        3. assign the jax potential to the _jaxPotential.

        Args:
            Those args are the same as those in createSystem.
        """

        # initialize typemap
        matcher = TypeMatcher(self.fftree, "HarmonicBondForce/Bond")

        map_atom1, map_atom2, map_param = [], [], []

        if not matcher.useSmirks:
            n_bonds = len(data.bonds)
            # build map
            for i in range(n_bonds):
                idx1 = data.bonds[i].atom1
                idx2 = data.bonds[i].atom2
                type1 = data.atomType[data.atoms[idx1]]
                type2 = data.atomType[data.atoms[idx2]]
                ifFound, ifForward, nfunc = matcher.matchGeneral([type1, type2])
                if not ifFound:
                    raise DMFFException(
                        f"No parameter for bond ({idx1},{type1}) - ({idx2},{type2})"
                    )
                map_atom1.append(idx1)
                map_atom2.append(idx2)
                map_param.append(nfunc)
        else:
            rdmol = args.get("rdmol", None)
            matches_dict = matcher.matchSmirks(rdmol)
            for bond in rdmol.GetBonds():
                beginAtomIdx = bond.GetBeginAtomIdx()
                endAtomIdx = bond.GetEndAtomIdx()
                query = (beginAtomIdx, endAtomIdx) if beginAtomIdx < endAtomIdx else (endAtomIdx, beginAtomIdx)
                map_atom1.append(query[0])
                map_atom2.append(query[1])
                try:
                    map_param.append(matches_dict[query])
                except KeyError as e:
                    raise DMFFException(
                        f"No parameter for bond between Atom{beginAtomIdx} and Atom{endAtomIdx}"
                    )

        map_atom1 = np.array(map_atom1, dtype=int)
        map_atom2 = np.array(map_atom2, dtype=int)
        map_param = np.array(map_param, dtype=int)    

        bforce = HarmonicBondJaxForce(map_atom1, map_atom2, map_param)
        self._force_latest = bforce

        def potential_fn(positions, box, pairs, params):
            return bforce.get_energy(positions, box, pairs,
                                     params[self.name]["k"],
                                     params[self.name]["length"])

        self._jaxPotential = potential_fn
        # self._top_data = data

    def getJaxPotential(self):
        return self._jaxPotential

createForce(sys, data, nonbondedMethod, nonbondedCutoff, args)

This method will create a potential calculation kernel. It usually should do the following:

  1. Match the corresponding bond parameters according to the atomic types at both ends of each bond.

  2. Create a potential calculation kernel, and pass those mapped parameters to the kernel.

  3. assign the jax potential to the _jaxPotential.

Source code in dmff/generators/classical.py
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def createForce(self, sys, data, nonbondedMethod, nonbondedCutoff, args):
    """
    This method will create a potential calculation kernel. It usually should do the following:

    1. Match the corresponding bond parameters according to the atomic types at both ends of each bond.

    2. Create a potential calculation kernel, and pass those mapped parameters to the kernel.

    3. assign the jax potential to the _jaxPotential.

    Args:
        Those args are the same as those in createSystem.
    """

    # initialize typemap
    matcher = TypeMatcher(self.fftree, "HarmonicBondForce/Bond")

    map_atom1, map_atom2, map_param = [], [], []

    if not matcher.useSmirks:
        n_bonds = len(data.bonds)
        # build map
        for i in range(n_bonds):
            idx1 = data.bonds[i].atom1
            idx2 = data.bonds[i].atom2
            type1 = data.atomType[data.atoms[idx1]]
            type2 = data.atomType[data.atoms[idx2]]
            ifFound, ifForward, nfunc = matcher.matchGeneral([type1, type2])
            if not ifFound:
                raise DMFFException(
                    f"No parameter for bond ({idx1},{type1}) - ({idx2},{type2})"
                )
            map_atom1.append(idx1)
            map_atom2.append(idx2)
            map_param.append(nfunc)
    else:
        rdmol = args.get("rdmol", None)
        matches_dict = matcher.matchSmirks(rdmol)
        for bond in rdmol.GetBonds():
            beginAtomIdx = bond.GetBeginAtomIdx()
            endAtomIdx = bond.GetEndAtomIdx()
            query = (beginAtomIdx, endAtomIdx) if beginAtomIdx < endAtomIdx else (endAtomIdx, beginAtomIdx)
            map_atom1.append(query[0])
            map_atom2.append(query[1])
            try:
                map_param.append(matches_dict[query])
            except KeyError as e:
                raise DMFFException(
                    f"No parameter for bond between Atom{beginAtomIdx} and Atom{endAtomIdx}"
                )

    map_atom1 = np.array(map_atom1, dtype=int)
    map_atom2 = np.array(map_atom2, dtype=int)
    map_param = np.array(map_param, dtype=int)    

    bforce = HarmonicBondJaxForce(map_atom1, map_atom2, map_param)
    self._force_latest = bforce

    def potential_fn(positions, box, pairs, params):
        return bforce.get_energy(positions, box, pairs,
                                 params[self.name]["k"],
                                 params[self.name]["length"])

    self._jaxPotential = potential_fn

extract()

extract forcefield paramters from ForcefieldTree.

Source code in dmff/generators/classical.py
44
45
46
47
48
49
50
51
52
53
def extract(self):
    """
    extract forcefield paramters from ForcefieldTree. 
    """
    lengths = self.fftree.get_attribs(f"{self.name}/Bond", "length")
    # get_attribs will return a list of list.
    ks = self.fftree.get_attribs(f"{self.name}/Bond", "k")
    self.paramtree[self.name] = {}
    self.paramtree[self.name]["length"] = jnp.array(lengths)
    self.paramtree[self.name]["k"] = jnp.array(ks)

overwrite()

update parameters in the fftree by using paramtree of this generator.

Source code in dmff/generators/classical.py
55
56
57
58
59
60
61
62
def overwrite(self):
    """
    update parameters in the fftree by using paramtree of this generator.
    """
    self.fftree.set_attrib(f"{self.name}/Bond", "length",
                           self.paramtree[self.name]["length"])
    self.fftree.set_attrib(f"{self.name}/Bond", "k",
                           self.paramtree[self.name]["k"])

NonbondedJaxGenerator

Source code in dmff/generators/classical.py
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
class NonbondedJaxGenerator:
    def __init__(self, ff: Hamiltonian):
        self.name = "NonbondedForce"
        self.ff = ff
        self.fftree = ff.fftree
        self.paramtree = ff.paramtree
        self.paramtree[self.name] = {}
        self.paramtree[self.name]["sigfix"] = jnp.array([])
        self.paramtree[self.name]["epsfix"] = jnp.array([])

        self.from_force = []
        self.from_residue = []
        self.ra2idx = {}
        self.idx2rai = {}

        self.useBCC = False
        self.useVsite = False

    def extract(self):
        self.from_residue = self.fftree.get_attribs(
            "NonbondedForce/UseAttributeFromResidue", "name")
        self.from_force = [
            i for i in ["charge", "sigma", "epsilon"]
            if i not in self.from_residue
        ]
        # Build per-atom array for from_force
        for prm in self.from_force:
            vals = self.fftree.get_attribs("NonbondedForce/Atom", prm)
            self.paramtree[self.name][prm] = jnp.array(vals)

        # Build per-atom array for from_residue
        residues = self.fftree.get_nodes("Residues/Residue")
        resvals = {k: [] for k in self.from_residue}
        for resnode in residues:
            resname = resnode.attrs["name"]
            resvals[resname] = []
            atomname = resnode.get_attribs("Atom", "name")
            shift = len(self.ra2idx)
            for natom, aname in enumerate(atomname):
                self.ra2idx[(resname, natom)] = shift + natom
                self.idx2rai[shift + natom] = (resname, atomname, natom)
            for prm in self.from_residue:
                atomval = resnode.get_attribs("Atom", prm)
                resvals[prm].extend(atomval)
        for prm in self.from_residue:
            self.paramtree[self.name][prm] = jnp.array(resvals[prm])

        # Build coulomb14scale and lj14scale
        coulomb14scale, lj14scale = self.fftree.get_attribs(
            "NonbondedForce", ["coulomb14scale", "lj14scale"])[0]
        self.paramtree[self.name]["coulomb14scale"] = jnp.array(
            [coulomb14scale])
        self.paramtree[self.name]["lj14scale"] = jnp.array([lj14scale])

        # Build BondChargeCorrection
        bccs = self.fftree.get_attribs("NonbondedForce/BondChargeCorrection", "bcc")
        self.paramtree[self.name]['bcc'] = jnp.array(bccs).reshape(-1, 1)
        self.useBCC = len(bccs) > 0

        # Build VirtualSite
        vsite_types = self.fftree.get_attribs("NonbondedForce/VirtualSite", "vtype")
        self.paramtree[self.name]['vsite_types'] = jnp.array(vsite_types, dtype=int)
        vsite_distance = self.fftree.get_attribs("NonbondedForce/VirtualSite", "distance")
        self.paramtree[self.name]['vsite_distances'] = jnp.array(vsite_distance)
        self.useVsite = len(vsite_types) > 0

    def overwrite(self):
        # write coulomb14scale
        self.fftree.set_attrib("NonbondedForce", "coulomb14scale",
                               self.paramtree[self.name]["coulomb14scale"])
        # write lj14scale
        self.fftree.set_attrib("NonbondedForce", "lj14scale",
                               self.paramtree[self.name]["lj14scale"])
        # write prm from force
        for prm in self.from_force:
            self.fftree.set_attrib("NonbondedForce/Atom", prm,
                                   self.paramtree[self.name][prm])
        # write prm from residue
        residues = self.fftree.get_nodes("Residues/Residue")
        for prm in self.from_residue:
            vals = self.paramtree[self.name][prm]
            data = []
            for idx in range(vals.shape[0]):
                rname, atomname, aidx = self.idx2rai[idx]
                data.append((rname, aidx, vals[idx]))

            for resnode in residues:
                tmp = sorted(
                    [d for d in data if d[0] == resnode.attrs["name"]],
                    key=lambda x: x[1])
                resnode.set_attrib("Atom", prm, [t[2] for t in tmp])

        # write BCC
        if self.useBCC:
            self.fftree.set_attrib(
                "NonbondedForce/BondChargeCorrection", "bcc",
                self.paramtree[self.name]['bcc']
            )

    def createForce(self, system, data, nonbondedMethod, nonbondedCutoff, args):
        # Build Covalent Map
        self.covalent_map = build_covalent_map(data, 6)

        methodMap = {
            app.NoCutoff: "NoCutoff",
            app.CutoffPeriodic: "CutoffPeriodic",
            app.CutoffNonPeriodic: "CutoffNonPeriodic",
            app.PME: "PME",
        }
        if nonbondedMethod not in methodMap:
            raise DMFFException("Illegal nonbonded method for NonbondedForce")
        isNoCut = False
        if nonbondedMethod is app.NoCutoff:
            isNoCut = True

        mscales_coul = jnp.array([0.0, 0.0, 0.0, 1.0, 1.0,
                                  1.0])  # mscale for PME
        mscales_coul = mscales_coul.at[2].set(
            self.paramtree[self.name]["coulomb14scale"][0])
        mscales_lj = jnp.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0])  # mscale for LJ
        mscales_lj = mscales_lj.at[2].set(
            self.paramtree[self.name]["lj14scale"][0])

        # set PBC
        if nonbondedMethod not in [app.NoCutoff, app.CutoffNonPeriodic]:
            ifPBC = True
        else:
            ifPBC = False

        nbmatcher = TypeMatcher(self.fftree, "NonbondedForce/Atom")


        rdmol = args.get("rdmol", None)

        if self.useVsite:
            vsitematcher = TypeMatcher(self.fftree, "NonbondedForce/VirtualSite")
            vsite_matches_dict = vsitematcher.matchSmirksNoSort(rdmol)
            vsiteObj = VirtualSite(vsite_matches_dict)

            def addVsiteFunc(pos, params):
                func = vsiteObj.getAddVirtualSiteFunc()
                newpos = func(pos, params[self.name]['vsite_types'], params[self.name]['vsite_distances'])
                return newpos

            self._addVsiteFunc = addVsiteFunc
            rdmol = vsiteObj.addVirtualSiteToMol(rdmol)
            self.vsiteObj = vsiteObj

            # expand covalent map
            ori_dim = self.covalent_map.shape[0]
            new_dim = ori_dim + len(vsite_matches_dict)
            cov_map = np.zeros((new_dim, new_dim), dtype=int)
            cov_map[:ori_dim, :ori_dim] += np.array(self.covalent_map, dtype=int)

            map_to_parents = np.arange(new_dim)
            for i, match in enumerate(vsite_matches_dict.keys()):
                map_to_parents[ori_dim + i] = match[0]
            for i in range(len(vsite_matches_dict)):
                parent_i = map_to_parents[ori_dim + i]
                for j in range(new_dim):
                    parent_j = map_to_parents[j]
                    cov_map[ori_dim + i, j] = cov_map[parent_i, parent_j]
                    cov_map[j, ori_dim + i] = cov_map[parent_j, parent_i]
                # keep diagonal 0
                cov_map[ori_dim + i, ori_dim + i] = 0
                # keep vsite and its parent atom 1
                cov_map[parent_i, ori_dim + i] = 1
                cov_map[ori_dim + i, parent_i] = 1
            self.covalent_map = jnp.array(cov_map)

        # Load Lennard-Jones parameters
        maps = {}
        if not nbmatcher.useSmirks:
            for prm in self.from_force:
                maps[prm] = []
                for atom in data.atoms:
                    atype = data.atomType[atom]
                    ifFound, _, nnode = nbmatcher.matchGeneral([atype])
                    if not ifFound:
                        raise DMFFException(
                            "AtomType of %s mismatched in NonbondedForce" %
                            (str(atom)))
                    maps[prm].append(nnode)
                maps[prm] = jnp.array(maps[prm], dtype=int)
        else:
            lj_matches_dict = nbmatcher.matchSmirks(rdmol)
            for prm in self.from_force:
                maps[prm] = []
                for i in range(rdmol.GetNumAtoms()):
                    try:
                        maps[prm].append(lj_matches_dict[(i,)])
                    except KeyError as e:
                        raise DMFFException(
                            f"No parameter for atom {i}"
                        )
                maps[prm] = jnp.array(maps[prm], dtype=int)

        for prm in self.from_residue:
            maps[prm] = []
            for atom in data.atoms:
                templateName = self.ff.templateNameForResidue[atom.residue.index]
                aidx = data.atomTemplateIndexes[atom]
                resname, aname = templateName, atom.name
                maps[prm].append(self.ra2idx[(resname, aidx)])

        # Virtual Site
        if self.useVsite:
            # expand charges
            chg = jnp.zeros(
                (len(self.paramtree[self.name]['charge']) + len(vsite_matches_dict),), 
                dtype=self.paramtree[self.name]['charge'].dtype
            )
            self.paramtree[self.name]['charge'] = chg.at[:len(self.paramtree[self.name]['charge'])].set(
                self.paramtree[self.name]['charge']
            )
            maps_chg = [int(x) for x in maps['charge']]
            for i in range(len(vsite_matches_dict)):
                maps_chg.append(len(maps['charge']) + i)
            maps['charge'] = jnp.array(maps_chg, dtype=int)

        # BCC parameters
        if self.useBCC:
            bccmatcher = TypeMatcher(self.fftree, "NonbondedForce/BondChargeCorrection")

            if bccmatcher.useSmirks:
                bcc_matches_dict = bccmatcher.matchSmirksBCC(rdmol)
                self.top_mat = np.zeros((rdmol.GetNumAtoms(), self.paramtree[self.name]['bcc'].shape[0]))

                for bond in rdmol.GetBonds():
                    beginAtomIdx = bond.GetBeginAtomIdx()
                    endAtomIdx = bond.GetEndAtomIdx()
                    query1, query2 = (beginAtomIdx, endAtomIdx), (endAtomIdx, beginAtomIdx)
                    if query1 in bcc_matches_dict:
                        nnode = bcc_matches_dict[query1]
                        self.top_mat[query1[0], nnode] += 1
                        self.top_mat[query1[1], nnode] -= 1
                    elif query2 in bcc_matches_dict:
                        nnode = bcc_matches_dict[query2]
                        self.top_mat[query2[0], nnode] += 1
                        self.top_mat[query2[1], nnode] -= 1
                    else:
                        warnings.warn(
                            f"No BCC parameter for bond between Atom{beginAtomIdx} and Atom{endAtomIdx}"
                        )
            else:
                raise DMFFException(
                    "Only SMIRKS-based parametrization is supported for BCC"
                )
        else:
            self.top_mat = None

        # NBFIX
        map_nbfix = []
        map_nbfix = jnp.array(map_nbfix, dtype=jnp.int32).reshape(-1, 2)

        if unit.is_quantity(nonbondedCutoff):
            r_cut = nonbondedCutoff.value_in_unit(unit.nanometer)
        else:
            r_cut = nonbondedCutoff
        if "switchDistance" in args and args["switchDistance"] is not None:
            r_switch = args["switchDistance"]
            r_switch = (r_switch if not unit.is_quantity(r_switch) else
                        r_switch.value_in_unit(unit.nanometer))
            ifSwitch = True
        else:
            r_switch = r_cut
            ifSwitch = False

        # PME Settings
        if nonbondedMethod is app.PME:
            a, b, c = system.getDefaultPeriodicBoxVectors()
            box = jnp.array([a._value, b._value, c._value])
            self.ethresh = args.get("ethresh", 1e-6)
            self.coeff_method = args.get("PmeCoeffMethod", "openmm")
            self.fourier_spacing = args.get("PmeSpacing", 0.1)
            kappa, K1, K2, K3 = setup_ewald_parameters(r_cut, self.ethresh,
                                                       box,
                                                       self.fourier_spacing,
                                                       self.coeff_method)

        map_lj = jnp.array(maps["sigma"])
        map_charge = jnp.array(maps["charge"])

        # Free Energy Settings #
        isFreeEnergy = args.get("isFreeEnergy", False)
        if isFreeEnergy:
            vdwLambda = args.get("vdwLambda", 0.0)
            coulLambda = args.get("coulLambda", 0.0)
            ifStateA = args.get("ifStateA", True)

            # soft-cores
            vdwSoftCore = args.get("vdwSoftCore", False)
            coulSoftCore = args.get("coulSoftCore", False)
            scAlpha = args.get("scAlpha", 0.0)
            scSigma = args.get("scSigma", 0.0)

            # couple
            coupleIndex = args.get("coupleIndex", [])
            if len(coupleIndex) > 0:
                coupleMask = [False for _ in range(len(data.atoms))]
                for atomIndex in coupleIndex:
                    coupleMask[atomIndex] = True
                coupleMask = jnp.array(coupleMask, dtype=bool)
            else:
                coupleMask = None

        if not isFreeEnergy:
            ljforce = LennardJonesForce(r_switch,
                                        r_cut,
                                        map_lj,
                                        map_nbfix,
                                        isSwitch=ifSwitch,
                                        isPBC=ifPBC,
                                        isNoCut=isNoCut)
        else:
            ljforce = LennardJonesFreeEnergyForce(r_switch,
                                                  r_cut,
                                                  map_lj,
                                                  map_nbfix,
                                                  isSwitch=ifSwitch,
                                                  isPBC=ifPBC,
                                                  isNoCut=isNoCut,
                                                  feLambda=vdwLambda,
                                                  coupleMask=coupleMask,
                                                  useSoftCore=vdwSoftCore,
                                                  ifStateA=ifStateA,
                                                  sc_alpha=scAlpha,
                                                  sc_sigma=scSigma)

        ljenergy = ljforce.generate_get_energy()

        # dispersion correction
        useDispersionCorrection = args.get("useDispersionCorrection", False)
        if useDispersionCorrection:
            numTypes = self.paramtree[self.name]["sigma"].shape[0]
            countVec = np.zeros(numTypes, dtype=int)
            countMat = np.zeros((numTypes, numTypes), dtype=int)
            types, count = np.unique(map_lj, return_counts=True)

            for typ, cnt in zip(types, count):
                countVec[typ] += cnt
            for i in range(numTypes):
                for j in range(i, numTypes):
                    if i != j:
                        countMat[i, j] = countVec[i] * countVec[j]
                    else:
                        countMat[i, i] = countVec[i] * (countVec[i] - 1) // 2
            assert np.sum(countMat) == len(map_lj) * (len(map_lj) - 1) // 2

            colv_pairs = np.argwhere(
                np.logical_and(self.covalent_map > 0, self.covalent_map <= 3))
            for pair in colv_pairs:
                if pair[0] <= pair[1]:
                    tmp = (map_lj[pair[0]], map_lj[pair[1]])
                    t1, t2 = min(tmp), max(tmp)
                    countMat[t1, t2] -= 1

            if not isFreeEnergy:
                ljDispCorrForce = LennardJonesLongRangeForce(
                    r_cut, map_lj, map_nbfix, countMat)
            else:
                ljDispCorrForce = LennardJonesLongRangeFreeEnergyForce(
                    r_cut, map_lj, map_nbfix, countMat, vdwLambda, ifStateA,
                    coupleMask)
            ljDispEnergyFn = ljDispCorrForce.generate_get_energy()

        if not isFreeEnergy:
            if nonbondedMethod is not app.PME:
                # do not use PME
                if nonbondedMethod in [
                        app.CutoffPeriodic, app.CutoffNonPeriodic
                ]:
                    # use Reaction Field
                    coulforce = CoulReactionFieldForce(r_cut,
                                                       map_charge,
                                                       isPBC=ifPBC,
                                                       topology_matrix=self.top_mat)
                if nonbondedMethod is app.NoCutoff:
                    # use NoCutoff
                    coulforce = CoulNoCutoffForce(map_charge, topology_matrix=self.top_mat)
            else:
                coulforce = CoulombPMEForce(r_cut, map_charge, kappa,
                                            (K1, K2, K3), topology_matrix=self.top_mat)
        else:
            assert nonbondedMethod is app.PME, "Only PME is supported in free energy calculations"
            assert not self.useBCC, "BCC usage in free energy calculations is not supported yet"
            coulforce = CoulombPMEFreeEnergyForce(r_cut,
                                                  map_charge,
                                                  kappa, (K1, K2, K3),
                                                  coulLambda,
                                                  ifStateA=ifStateA,
                                                  coupleMask=coupleMask,
                                                  useSoftCore=coulSoftCore,
                                                  sc_alpha=scAlpha,
                                                  sc_sigma=scSigma)

        coulenergy = coulforce.generate_get_energy()

        if not isFreeEnergy:

            def potential_fn(positions, box, pairs, params):

                # check whether args passed into potential_fn are jnp.array and differentiable
                # note this check will be optimized away by jit
                # it is jit-compatiable
                isinstance_jnp(positions, box, params)

                ljE = ljenergy(positions, box, pairs,
                               params[self.name]["epsilon"],
                               params[self.name]["sigma"],
                               params[self.name]["epsfix"],
                               params[self.name]["sigfix"], mscales_lj)

                if not self.useBCC:
                    coulE = coulenergy(positions, box, pairs,
                                    params[self.name]["charge"], mscales_coul)
                else:
                    coulE = coulenergy(positions, box, pairs,
                                    params[self.name]["charge"], params[self.name]["bcc"], mscales_coul)

                if useDispersionCorrection:
                    ljDispEnergy = ljDispEnergyFn(box,
                                                  params[self.name]['epsilon'],
                                                  params[self.name]['sigma'],
                                                  params[self.name]['epsfix'],
                                                  params[self.name]['sigfix'])

                    return ljE + coulE + ljDispEnergy
                else:
                    return ljE + coulE

            self._jaxPotential = potential_fn
        else:
            # Free Energy
            @jit_condition()
            def potential_fn(positions, box, pairs, params, vdwLambda,
                             coulLambda):
                ljE = ljenergy(positions, box, pairs,
                               params[self.name]["epsilon"],
                               params[self.name]["sigma"],
                               params[self.name]["epsfix"],
                               params[self.name]["sigfix"], mscales_lj,
                               vdwLambda)
                coulE = coulenergy(positions, box, pairs,
                                   params[self.name]["charge"], mscales_coul,
                                   coulLambda)

                if useDispersionCorrection:
                    ljDispEnergy = ljDispEnergyFn(box,
                                                  params[self.name]['epsilon'],
                                                  params[self.name]['sigma'],
                                                  params[self.name]['epsfix'],
                                                  params[self.name]['sigfix'],
                                                  vdwLambda)
                    return ljE + coulE + ljDispEnergy
                else:
                    return ljE + coulE

            self._jaxPotential = potential_fn

    def getJaxPotential(self):
        return self._jaxPotential

    def getAddVsiteFunc(self):
        """
        Get function to add coordinates for virtual sites
        """
        return self._addVsiteFunc

    def getVsiteObj(self):
        """
        Get `dmff.classical.vsite.VirtualSite` object
        """
        if self.useVsite:
            return self.vsiteObj
        else:
            return None

    def getTopologyMatrix(self):
        """
        Get topology Matrix
        """
        return self.top_mat

getAddVsiteFunc()

Get function to add coordinates for virtual sites

Source code in dmff/generators/classical.py
980
981
982
983
984
def getAddVsiteFunc(self):
    """
    Get function to add coordinates for virtual sites
    """
    return self._addVsiteFunc

getTopologyMatrix()

Get topology Matrix

Source code in dmff/generators/classical.py
995
996
997
998
999
def getTopologyMatrix(self):
    """
    Get topology Matrix
    """
    return self.top_mat

getVsiteObj()

Get dmff.classical.vsite.VirtualSite object

Source code in dmff/generators/classical.py
986
987
988
989
990
991
992
993
def getVsiteObj(self):
    """
    Get `dmff.classical.vsite.VirtualSite` object
    """
    if self.useVsite:
        return self.vsiteObj
    else:
        return None

PeriodicTorsionJaxGenerator

Source code in dmff/generators/classical.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
class PeriodicTorsionJaxGenerator:
    def __init__(self, ff):
        self.name = "PeriodicTorsionForce"
        self.ff = ff
        self.fftree = ff.fftree
        self.paramtree = ff.paramtree
        self.meta = {}

        self.meta["prop_order"] = defaultdict(list)
        self.meta["prop_nodeidx"] = defaultdict(list)

        self.meta["impr_order"] = defaultdict(list)
        self.meta["impr_nodeidx"] = defaultdict(list)

        self.max_pred_prop = 0
        self.max_pred_impr = 0

    def extract(self):
        propers = self.fftree.get_nodes("PeriodicTorsionForce/Proper")
        impropers = self.fftree.get_nodes("PeriodicTorsionForce/Improper")
        self.paramtree[self.name] = {}
        # propers
        prop_phase = defaultdict(list)
        prop_k = defaultdict(list)
        for nnode, node in enumerate(propers):
            for key in node.attrs:
                if "periodicity" in key:
                    order = int(key[-1])
                    phase = float(node.attrs[f"phase{order}"])
                    k = float(node.attrs[f"k{order}"])
                    periodicity = int(node.attrs[f"periodicity{order}"])
                    if self.max_pred_prop < periodicity:
                        self.max_pred_prop = periodicity
                    prop_phase[f"{periodicity}"].append(phase)
                    prop_k[f"{periodicity}"].append(k)
                    self.meta[f"prop_order"][f"{periodicity}"].append(order)
                    self.meta[f"prop_nodeidx"][f"{periodicity}"].append(nnode)

        self.paramtree[self.name]["prop_phase"] = {}
        self.paramtree[self.name]["prop_k"] = {}
        for npred in range(1, self.max_pred_prop + 1):
            self.paramtree[self.name]["prop_phase"][f"{npred}"] = jnp.array(
                prop_phase[f"{npred}"])
            self.paramtree[self.name]["prop_k"][f"{npred}"] = jnp.array(
                prop_k[f"{npred}"])
        if self.max_pred_prop == 0:
            del self.paramtree[self.name]["prop_phase"]
            del self.paramtree[self.name]["prop_k"]

        # impropers
        impr_phase = defaultdict(list)
        impr_k = defaultdict(list)
        for nnode, node in enumerate(impropers):
            for key in node.attrs:
                if "periodicity" in key:
                    order = int(key[-1])
                    phase = float(node.attrs[f"phase{order}"])
                    k = float(node.attrs[f"k{order}"])
                    periodicity = int(node.attrs[f"periodicity{order}"])
                    if self.max_pred_impr < periodicity:
                        self.max_pred_impr = periodicity
                    impr_phase[f"{periodicity}"].append(phase)
                    impr_k[f"{periodicity}"].append(k)
                    self.meta[f"impr_order"][f"{periodicity}"].append(order)
                    self.meta[f"impr_nodeidx"][f"{periodicity}"].append(nnode)

        self.paramtree[self.name]["impr_phase"] = {}
        self.paramtree[self.name]["impr_k"] = {}
        for npred in range(1, self.max_pred_impr + 1):
            self.paramtree[self.name]["impr_phase"][f"{npred}"] = jnp.array(
                impr_phase[f"{npred}"])
            self.paramtree[self.name]["impr_k"][f"{npred}"] = jnp.array(
                impr_k[f"{npred}"])
        if self.max_pred_impr == 0:
            del self.paramtree[self.name]["impr_phase"]
            del self.paramtree[self.name]["impr_k"]

    def overwrite(self):
        propers = self.fftree.get_nodes("PeriodicTorsionForce/Proper")
        impropers = self.fftree.get_nodes("PeriodicTorsionForce/Improper")
        prop_data = [{} for _ in propers]
        impr_data = [{} for _ in impropers]
        # make propers
        for periodicity in range(1, self.max_pred_prop + 1):
            nterms = len(
                self.paramtree[self.name][f"prop_phase"][f"{periodicity}"])
            for nitem in range(nterms):
                phase = self.paramtree[
                    self.name][f"prop_phase"][f"{periodicity}"][nitem]
                k = self.paramtree[
                    self.name][f"prop_k"][f"{periodicity}"][nitem]
                nodeidx = self.meta[f"prop_nodeidx"][f"{periodicity}"][nitem]
                order = self.meta[f"prop_order"][f"{periodicity}"][nitem]
                prop_data[nodeidx][f"phase{order}"] = phase
                prop_data[nodeidx][f"k{order}"] = k
        if "prop_phase" in self.paramtree[self.name]:
            self.fftree.set_node("PeriodicTorsionForce/Proper", prop_data)

        # make impropers
        for periodicity in range(1, self.max_pred_impr + 1):
            nterms = len(
                self.paramtree[self.name][f"impr_phase"][f"{periodicity}"])
            for nitem in range(nterms):
                phase = self.paramtree[
                    self.name][f"impr_phase"][f"{periodicity}"][nitem]
                k = self.paramtree[
                    self.name][f"impr_k"][f"{periodicity}"][nitem]
                nodeidx = self.meta[f"impr_nodeidx"][f"{periodicity}"][nitem]
                order = self.meta[f"impr_order"][f"{periodicity}"][nitem]
                impr_data[nodeidx][f"phase{order}"] = phase
                impr_data[nodeidx][f"k{order}"] = k
        if "impr_phase" in self.paramtree[self.name]:
            self.fftree.set_node("PeriodicTorsionForce/Improper", impr_data)

    def createForce(self, sys, data, nonbondedMethod, nonbondedCutoff, args):
        """
        Create force for torsions
        """
        # Proper Torsions
        proper_matcher = TypeMatcher(self.fftree,
                                     "PeriodicTorsionForce/Proper")
        map_prop_atom1 = {i: [] for i in range(1, self.max_pred_prop + 1)}
        map_prop_atom2 = {i: [] for i in range(1, self.max_pred_prop + 1)}
        map_prop_atom3 = {i: [] for i in range(1, self.max_pred_prop + 1)}
        map_prop_atom4 = {i: [] for i in range(1, self.max_pred_prop + 1)}
        map_prop_param = {i: [] for i in range(1, self.max_pred_prop + 1)}
        n_matched_props = 0

        if not proper_matcher.useSmirks:
            for torsion in data.propers:
                types = [data.atomType[data.atoms[torsion[i]]] for i in range(4)]
                ifFound, ifForward, nnode = proper_matcher.matchGeneral(types)
                if not ifFound:
                    continue
                # find terms for node
                for periodicity in range(1, self.max_pred_prop + 1):
                    idx = findItemInList(
                        nnode, self.meta[f"prop_nodeidx"][f"{periodicity}"])
                    if idx < 0:
                        continue
                    n_matched_props += 1
                    map_prop_atom1[periodicity].append(torsion[0])
                    map_prop_atom2[periodicity].append(torsion[1])
                    map_prop_atom3[periodicity].append(torsion[2])
                    map_prop_atom4[periodicity].append(torsion[3])
                    map_prop_param[periodicity].append(idx)
        else:
            from rdkit import Chem

            rdmol = args.get("rdmol", None)
            proper_patt = Chem.MolFromSmarts("[*:1]~[*:2]-[*:3]~[*:4]")
            propers = rdmol.GetSubstructMatches(proper_patt)
            matches_dict = proper_matcher.matchSmirks(rdmol)
            for match in propers:
                torsion = (match[3], match[2], match[1], match[0]) if match[2] < match[1] else match
                try:
                    nnode = matches_dict[torsion]
                    ifFound = True
                    n_matched_props += 1
                except KeyError:
                    ifFound = False

                if not ifFound:
                    continue

                for periodicity in range(1, self.max_pred_prop + 1):
                    idx = findItemInList(nnode, self.meta['prop_nodeidx'][f"{periodicity}"])
                    if idx < 0:
                        continue
                    map_prop_atom1[periodicity].append(torsion[0])
                    map_prop_atom2[periodicity].append(torsion[1])
                    map_prop_atom3[periodicity].append(torsion[2])
                    map_prop_atom4[periodicity].append(torsion[3])
                    map_prop_param[periodicity].append(idx)

        # Improper Torsions
        impr_matcher = TypeMatcher(self.fftree,
                                   "PeriodicTorsionForce/Improper")
        try:
            ordering = self.fftree.get_attribs("PeriodicTorsionForce",
                                               "ordering")[0]
        except KeyError as e:
            ordering = "default"

        map_impr_atom1 = {i: [] for i in range(1, self.max_pred_impr + 1)}
        map_impr_atom2 = {i: [] for i in range(1, self.max_pred_impr + 1)}
        map_impr_atom3 = {i: [] for i in range(1, self.max_pred_impr + 1)}
        map_impr_atom4 = {i: [] for i in range(1, self.max_pred_impr + 1)}
        map_impr_param = {i: [] for i in range(1, self.max_pred_impr + 1)}
        n_matched_imprs = 0

        if not impr_matcher.useSmirks:
            for impr in data.impropers:
                match = impr_matcher.matchImproper(impr, data, ordering=ordering)
                if match is not None:
                    (a1, a2, a3, a4, nnode) = match
                    n_matched_imprs += 1
                    # find terms for node
                    for periodicity in range(1, self.max_pred_impr + 1):
                        idx = findItemInList(
                            nnode, self.meta[f"impr_nodeidx"][f"{periodicity}"])
                        if idx < 0:
                            continue
                        if ordering == 'smirnoff':
                            # Add all torsions in trefoil
                            map_impr_atom1[periodicity].append(a1)
                            map_impr_atom2[periodicity].append(a2)
                            map_impr_atom3[periodicity].append(a3)
                            map_impr_atom4[periodicity].append(a4)
                            map_impr_param[periodicity].append(idx)
                            map_impr_atom1[periodicity].append(a1)
                            map_impr_atom2[periodicity].append(a3)
                            map_impr_atom3[periodicity].append(a4)
                            map_impr_atom4[periodicity].append(a2)
                            map_impr_param[periodicity].append(idx)
                            map_impr_atom1[periodicity].append(a1)
                            map_impr_atom2[periodicity].append(a4)
                            map_impr_atom3[periodicity].append(a2)
                            map_impr_atom4[periodicity].append(a3)
                            map_impr_param[periodicity].append(idx)
                        else:
                            map_impr_atom1[periodicity].append(a1)
                            map_impr_atom2[periodicity].append(a2)
                            map_impr_atom3[periodicity].append(a3)
                            map_impr_atom4[periodicity].append(a4)
                            map_impr_param[periodicity].append(idx)
        else:
            rdmol = args.get("rdmol", None)

            if rdmol is None:
                raise DMFFException("No rdkit.Chem.Mol object is provided")

            matches_dict = impr_matcher.matchSmirksImproper(rdmol)
            for torsion, nnode in matches_dict.items():
                n_matched_imprs += 1
                for periodicity in range(1, self.max_pred_impr+ 1):
                    idx = findItemInList(nnode, self.meta['impr_nodeidx'][f"{periodicity}"])
                    if idx < 0:
                        continue
                    map_impr_atom1[periodicity].append(torsion[0])
                    map_impr_atom2[periodicity].append(torsion[1])
                    map_impr_atom3[periodicity].append(torsion[2])
                    map_impr_atom4[periodicity].append(torsion[3])
                    map_impr_param[periodicity].append(idx)

        # Sum proper and improper torsions
        props = [
            PeriodicTorsionJaxForce(jnp.array(map_prop_atom1[p], dtype=int),
                                    jnp.array(map_prop_atom2[p], dtype=int),
                                    jnp.array(map_prop_atom3[p], dtype=int),
                                    jnp.array(map_prop_atom4[p], dtype=int),
                                    jnp.array(map_prop_param[p], dtype=int), p)
            for p in range(1, self.max_pred_prop + 1)
        ]
        imprs = [
            PeriodicTorsionJaxForce(jnp.array(map_impr_atom1[p], dtype=int),
                                    jnp.array(map_impr_atom2[p], dtype=int),
                                    jnp.array(map_impr_atom3[p], dtype=int),
                                    jnp.array(map_impr_atom4[p], dtype=int),
                                    jnp.array(map_impr_param[p], dtype=int), p)
            for p in range(1, self.max_pred_impr + 1)
        ]
        self._props_latest = props
        self._imprs_latest = imprs

        def potential_fn(positions, box, pairs, params):
            prop_sum = sum([
                props[i].get_energy(
                    positions, box, pairs,
                    params["PeriodicTorsionForce"]["prop_k"][f"{i+1}"],
                    params["PeriodicTorsionForce"]["prop_phase"][f"{i+1}"])
                for i in range(self.max_pred_prop)
            ])
            impr_sum = sum([
                imprs[i].get_energy(
                    positions, box, pairs,
                    params["PeriodicTorsionForce"]["impr_k"][f"{i+1}"],
                    params["PeriodicTorsionForce"]["impr_phase"][f"{i+1}"])
                for i in range(self.max_pred_impr)
            ])

            return prop_sum + impr_sum

        self._jaxPotential = potential_fn

    def getJaxPotential(self):
        return self._jaxPotential

createForce(sys, data, nonbondedMethod, nonbondedCutoff, args)

Create force for torsions

Source code in dmff/generators/classical.py
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
def createForce(self, sys, data, nonbondedMethod, nonbondedCutoff, args):
    """
    Create force for torsions
    """
    # Proper Torsions
    proper_matcher = TypeMatcher(self.fftree,
                                 "PeriodicTorsionForce/Proper")
    map_prop_atom1 = {i: [] for i in range(1, self.max_pred_prop + 1)}
    map_prop_atom2 = {i: [] for i in range(1, self.max_pred_prop + 1)}
    map_prop_atom3 = {i: [] for i in range(1, self.max_pred_prop + 1)}
    map_prop_atom4 = {i: [] for i in range(1, self.max_pred_prop + 1)}
    map_prop_param = {i: [] for i in range(1, self.max_pred_prop + 1)}
    n_matched_props = 0

    if not proper_matcher.useSmirks:
        for torsion in data.propers:
            types = [data.atomType[data.atoms[torsion[i]]] for i in range(4)]
            ifFound, ifForward, nnode = proper_matcher.matchGeneral(types)
            if not ifFound:
                continue
            # find terms for node
            for periodicity in range(1, self.max_pred_prop + 1):
                idx = findItemInList(
                    nnode, self.meta[f"prop_nodeidx"][f"{periodicity}"])
                if idx < 0:
                    continue
                n_matched_props += 1
                map_prop_atom1[periodicity].append(torsion[0])
                map_prop_atom2[periodicity].append(torsion[1])
                map_prop_atom3[periodicity].append(torsion[2])
                map_prop_atom4[periodicity].append(torsion[3])
                map_prop_param[periodicity].append(idx)
    else:
        from rdkit import Chem

        rdmol = args.get("rdmol", None)
        proper_patt = Chem.MolFromSmarts("[*:1]~[*:2]-[*:3]~[*:4]")
        propers = rdmol.GetSubstructMatches(proper_patt)
        matches_dict = proper_matcher.matchSmirks(rdmol)
        for match in propers:
            torsion = (match[3], match[2], match[1], match[0]) if match[2] < match[1] else match
            try:
                nnode = matches_dict[torsion]
                ifFound = True
                n_matched_props += 1
            except KeyError:
                ifFound = False

            if not ifFound:
                continue

            for periodicity in range(1, self.max_pred_prop + 1):
                idx = findItemInList(nnode, self.meta['prop_nodeidx'][f"{periodicity}"])
                if idx < 0:
                    continue
                map_prop_atom1[periodicity].append(torsion[0])
                map_prop_atom2[periodicity].append(torsion[1])
                map_prop_atom3[periodicity].append(torsion[2])
                map_prop_atom4[periodicity].append(torsion[3])
                map_prop_param[periodicity].append(idx)

    # Improper Torsions
    impr_matcher = TypeMatcher(self.fftree,
                               "PeriodicTorsionForce/Improper")
    try:
        ordering = self.fftree.get_attribs("PeriodicTorsionForce",
                                           "ordering")[0]
    except KeyError as e:
        ordering = "default"

    map_impr_atom1 = {i: [] for i in range(1, self.max_pred_impr + 1)}
    map_impr_atom2 = {i: [] for i in range(1, self.max_pred_impr + 1)}
    map_impr_atom3 = {i: [] for i in range(1, self.max_pred_impr + 1)}
    map_impr_atom4 = {i: [] for i in range(1, self.max_pred_impr + 1)}
    map_impr_param = {i: [] for i in range(1, self.max_pred_impr + 1)}
    n_matched_imprs = 0

    if not impr_matcher.useSmirks:
        for impr in data.impropers:
            match = impr_matcher.matchImproper(impr, data, ordering=ordering)
            if match is not None:
                (a1, a2, a3, a4, nnode) = match
                n_matched_imprs += 1
                # find terms for node
                for periodicity in range(1, self.max_pred_impr + 1):
                    idx = findItemInList(
                        nnode, self.meta[f"impr_nodeidx"][f"{periodicity}"])
                    if idx < 0:
                        continue
                    if ordering == 'smirnoff':
                        # Add all torsions in trefoil
                        map_impr_atom1[periodicity].append(a1)
                        map_impr_atom2[periodicity].append(a2)
                        map_impr_atom3[periodicity].append(a3)
                        map_impr_atom4[periodicity].append(a4)
                        map_impr_param[periodicity].append(idx)
                        map_impr_atom1[periodicity].append(a1)
                        map_impr_atom2[periodicity].append(a3)
                        map_impr_atom3[periodicity].append(a4)
                        map_impr_atom4[periodicity].append(a2)
                        map_impr_param[periodicity].append(idx)
                        map_impr_atom1[periodicity].append(a1)
                        map_impr_atom2[periodicity].append(a4)
                        map_impr_atom3[periodicity].append(a2)
                        map_impr_atom4[periodicity].append(a3)
                        map_impr_param[periodicity].append(idx)
                    else:
                        map_impr_atom1[periodicity].append(a1)
                        map_impr_atom2[periodicity].append(a2)
                        map_impr_atom3[periodicity].append(a3)
                        map_impr_atom4[periodicity].append(a4)
                        map_impr_param[periodicity].append(idx)
    else:
        rdmol = args.get("rdmol", None)

        if rdmol is None:
            raise DMFFException("No rdkit.Chem.Mol object is provided")

        matches_dict = impr_matcher.matchSmirksImproper(rdmol)
        for torsion, nnode in matches_dict.items():
            n_matched_imprs += 1
            for periodicity in range(1, self.max_pred_impr+ 1):
                idx = findItemInList(nnode, self.meta['impr_nodeidx'][f"{periodicity}"])
                if idx < 0:
                    continue
                map_impr_atom1[periodicity].append(torsion[0])
                map_impr_atom2[periodicity].append(torsion[1])
                map_impr_atom3[periodicity].append(torsion[2])
                map_impr_atom4[periodicity].append(torsion[3])
                map_impr_param[periodicity].append(idx)

    # Sum proper and improper torsions
    props = [
        PeriodicTorsionJaxForce(jnp.array(map_prop_atom1[p], dtype=int),
                                jnp.array(map_prop_atom2[p], dtype=int),
                                jnp.array(map_prop_atom3[p], dtype=int),
                                jnp.array(map_prop_atom4[p], dtype=int),
                                jnp.array(map_prop_param[p], dtype=int), p)
        for p in range(1, self.max_pred_prop + 1)
    ]
    imprs = [
        PeriodicTorsionJaxForce(jnp.array(map_impr_atom1[p], dtype=int),
                                jnp.array(map_impr_atom2[p], dtype=int),
                                jnp.array(map_impr_atom3[p], dtype=int),
                                jnp.array(map_impr_atom4[p], dtype=int),
                                jnp.array(map_impr_param[p], dtype=int), p)
        for p in range(1, self.max_pred_impr + 1)
    ]
    self._props_latest = props
    self._imprs_latest = imprs

    def potential_fn(positions, box, pairs, params):
        prop_sum = sum([
            props[i].get_energy(
                positions, box, pairs,
                params["PeriodicTorsionForce"]["prop_k"][f"{i+1}"],
                params["PeriodicTorsionForce"]["prop_phase"][f"{i+1}"])
            for i in range(self.max_pred_prop)
        ])
        impr_sum = sum([
            imprs[i].get_energy(
                positions, box, pairs,
                params["PeriodicTorsionForce"]["impr_k"][f"{i+1}"],
                params["PeriodicTorsionForce"]["impr_phase"][f"{i+1}"])
            for i in range(self.max_pred_impr)
        ])

        return prop_sum + impr_sum

    self._jaxPotential = potential_fn