@@ -9,27 +9,30 @@ inline float electrostatic(float distance, float dQ1Q2) {
9
9
return dQ1Q2/distance;
10
10
}
11
11
12
- float energy (int natoms, float coords[], float charges[],
12
+ float energyold (int natoms, float coords[], float charges[],
13
13
int ntypes, int types[], float cn1[], float cn2[],
14
14
int nexcluded[], int excluded_indices[]) {
15
15
float energy_vdw = 0.0 ;
16
16
float energy_electrostatic = 0.0 ;
17
17
int excludedAtomIndex = 0 ;
18
+ int notExcluded = 1 ;
18
19
for (size_t index1 = 0 ; index1 < natoms - 1 ; ++index1) {
19
20
int numberOfExcludedAtomsRemaining = nexcluded[index1];
20
21
float charge11 = charges[index1];
21
22
int type1 = types[index1];
22
23
float x1 = coords[index1*3 +0 ],
23
24
y1 = coords[index1*3 +1 ],
24
25
z1 = coords[index1*3 +2 ];
26
+
25
27
for (size_t index2 = index1+1 ; index2 < natoms; ++index2) {
26
28
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) {
29
31
++excludedAtomIndex;
30
32
--numberOfExcludedAtomsRemaining;
31
33
continue ;
32
34
}
35
+
33
36
float charge22 = charges[index2];
34
37
int type2 = types[index2];
35
38
float x2 = coords[index2*3 +0 ],
@@ -40,26 +43,87 @@ float energy(int natoms, float coords[], float charges[],
40
43
float dQ1Q2 = charge11*charge22;
41
44
float xd = x1 - x2, yd = y1 - y2, zd = z1 - z2;
42
45
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 ;
45
48
}
49
+
46
50
}
47
- return energy_vdw + energy_electrostatic;
51
+ return ( energy_vdw + energy_electrostatic) ;
48
52
}
49
53
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 };
53
103
float charge[4 ] = {0.85 , 0.95 , 1.05 , 1.15 };
54
104
int types[4 ] = {0 , 1 , 1 , 0 };
55
105
float cn1[2 ] = {0.5 , 0.7 };
56
106
float cn2[2 ] = {0.3 , 0.6 };
57
107
int nexcluded[4 ] = {1 , 1 , 1 , 1 };
58
108
// 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,
62
115
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
+
63
127
std::cout << " Energy: " << tenergy << std::endl;
64
128
return 0 ;
65
129
}
0 commit comments