@@ -9,27 +9,30 @@ inline float electrostatic(float distance, float dQ1Q2) {
99 return dQ1Q2/distance;
1010}
1111
12- float energy (int natoms, float coords[], float charges[],
12+ float energyold (int natoms, float coords[], float charges[],
1313 int ntypes, int types[], float cn1[], float cn2[],
1414 int nexcluded[], int excluded_indices[]) {
1515 float energy_vdw = 0.0 ;
1616 float energy_electrostatic = 0.0 ;
1717 int excludedAtomIndex = 0 ;
18+ int notExcluded = 1 ;
1819 for (size_t index1 = 0 ; index1 < natoms - 1 ; ++index1) {
1920 int numberOfExcludedAtomsRemaining = nexcluded[index1];
2021 float charge11 = charges[index1];
2122 int type1 = types[index1];
2223 float x1 = coords[index1*3 +0 ],
2324 y1 = coords[index1*3 +1 ],
2425 z1 = coords[index1*3 +2 ];
26+
2527 for (size_t index2 = index1+1 ; index2 < natoms; ++index2) {
2628 int maybe_excluded_atom = excluded_indices[excludedAtomIndex];
27- if (numberOfExcludedAtomsRemaining > 0
28- && maybe_excluded_atom == index2) {
29+
30+ if (numberOfExcludedAtomsRemaining > 0 && maybe_excluded_atom == index2) {
2931 ++excludedAtomIndex;
3032 --numberOfExcludedAtomsRemaining;
3133 continue ;
3234 }
35+
3336 float charge22 = charges[index2];
3437 int type2 = types[index2];
3538 float x2 = coords[index2*3 +0 ],
@@ -40,26 +43,87 @@ float energy(int natoms, float coords[], float charges[],
4043 float dQ1Q2 = charge11*charge22;
4144 float xd = x1 - x2, yd = y1 - y2, zd = z1 - z2;
4245 float distance = std::sqrt (xd*xd + yd*yd + zd*zd);
43- energy_vdw += van_der_waals (distance, dA, dC);
44- energy_electrostatic += electrostatic (distance, dQ1Q2);
46+ energy_vdw += van_der_waals (distance, dA, dC)*notExcluded ;
47+ energy_electrostatic += electrostatic (distance, dQ1Q2)*notExcluded ;
4548 }
49+
4650 }
47- return energy_vdw + energy_electrostatic;
51+ return ( energy_vdw + energy_electrostatic) ;
4852}
4953
50- int main () {
51- float pos[12 ] = {0.0 , 19.0 ,3.0 , 10.0 , 7.0 , 80.0 ,
52- 20.0 , 15.0 ,17.0 , 25.0 , 44.0 , 23.0 };
54+
55+ float energynew (int natoms, float coords[], float charges[],
56+ int ntypes, int types[], float cn1[], float cn2[],
57+ int nexcluded[], int excluded_indices[]) {
58+ float energy_vdw = 0.0 ;
59+ float energy_electrostatic = 0.0 ;
60+ int excludedAtomIndex = 0 ;
61+ int notExcluded = 1 ;
62+ for (size_t index1 = 0 ; index1 < natoms - 1 ; ++index1) {
63+ int numberOfExcludedAtomsRemaining = nexcluded[index1];
64+ float charge11 = charges[index1];
65+ int type1 = types[index1];
66+ float x1 = coords[index1*3 +0 ],
67+ y1 = coords[index1*3 +1 ],
68+ z1 = coords[index1*3 +2 ];
69+
70+ for (size_t index2 = index1+1 ; index2 < natoms; ++index2) {
71+ int maybe_excluded_atom = excluded_indices[excludedAtomIndex];
72+ notExcluded = !(numberOfExcludedAtomsRemaining > 0 && maybe_excluded_atom == index2);
73+ excludedAtomIndex += (numberOfExcludedAtomsRemaining > 0 && maybe_excluded_atom == index2);
74+ numberOfExcludedAtomsRemaining -= (numberOfExcludedAtomsRemaining > 0 && maybe_excluded_atom == index2);
75+ float charge22 = charges[index2];
76+ int type2 = types[index2];
77+ float x2 = coords[index2*3 +0 ],
78+ y2 = coords[index2*3 +1 ],
79+ z2 = coords[index2*3 +2 ];
80+ float dA = cn1[type1*ntypes + type2];
81+ float dC = cn2[type1*ntypes + type2];
82+ float dQ1Q2 = charge11*charge22;
83+ float xd = x1 - x2, yd = y1 - y2, zd = z1 - z2;
84+ float distance = std::sqrt (xd*xd + yd*yd + zd*zd);
85+ energy_vdw += van_der_waals (distance, dA, dC)*notExcluded;
86+ energy_electrostatic += electrostatic (distance, dQ1Q2)*notExcluded;
87+ }
88+
89+ }
90+ return (energy_vdw + energy_electrostatic);
91+ }
92+
93+
94+
95+ int main (int argc, const char * argv[]) {
96+ std::string arg1 (argv[1 ]);
97+ bool donew = (arg1 == " new" );
98+ size_t num = atoi (argv[2 ]);
99+
100+
101+ float pos[12 ] = {0.0 , 19.0 , 3.0 , 10.0 , 7.0 , 80.0 ,
102+ 20.0 , 15.0 , 17.0 , 25.0 , 44.0 , 23.0 };
53103 float charge[4 ] = {0.85 , 0.95 , 1.05 , 1.15 };
54104 int types[4 ] = {0 , 1 , 1 , 0 };
55105 float cn1[2 ] = {0.5 , 0.7 };
56106 float cn2[2 ] = {0.3 , 0.6 };
57107 int nexcluded[4 ] = {1 , 1 , 1 , 1 };
58108 // just exclude self-interactions.
59- int excluded_indices[4 ] = {0 , 1 , 2 , 3 };
60-
61- float tenergy = energy (4 , pos, charge, 2 , types, cn1, cn2,
109+ int excluded_indices[4 ] = {1 , 2 , 3 , 0 };
110+ float tenergy = 0 ;
111+ if (donew) {
112+ std::cout << " New method" << " \n " ;
113+ for ( size_t nn = 0 ; nn<num; nn++ ) {
114+ tenergy = energynew (4 , pos, charge, 2 , types, cn1, cn2,
62115 nexcluded, excluded_indices);
116+ }
117+ }
118+
119+ else {
120+ std::cout << " Old method" << " \n " ;
121+ for ( size_t nn = 0 ; nn<num; nn++ ) {
122+ tenergy = energyold (4 , pos, charge, 2 , types, cn1, cn2,
123+ nexcluded, excluded_indices);
124+ }
125+ }
126+
63127 std::cout << " Energy: " << tenergy << std::endl;
64128 return 0 ;
65129}
0 commit comments