@@ -9,44 +9,6 @@ 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[],
13
- int ntypes, int types[], float cn1[], float cn2[],
14
- int nexcluded[], int excluded_indices[]) {
15
- float energy_vdw = 0.0 ;
16
- float energy_electrostatic = 0.0 ;
17
- int excludedAtomIndex = 0 ;
18
- for (size_t index1 = 0 ; index1 < natoms - 1 ; ++index1) {
19
- int numberOfExcludedAtomsRemaining = nexcluded[index1];
20
- float charge11 = charges[index1];
21
- int type1 = types[index1];
22
- float x1 = coords[index1*3 +0 ],
23
- y1 = coords[index1*3 +1 ],
24
- z1 = coords[index1*3 +2 ];
25
- for (size_t index2 = index1+1 ; index2 < natoms; ++index2) {
26
- int maybe_excluded_atom = excluded_indices[excludedAtomIndex];
27
- if (numberOfExcludedAtomsRemaining > 0
28
- && maybe_excluded_atom == index2) {
29
- ++excludedAtomIndex;
30
- --numberOfExcludedAtomsRemaining;
31
- continue ;
32
- }
33
- float charge22 = charges[index2];
34
- int type2 = types[index2];
35
- float x2 = coords[index2*3 +0 ],
36
- y2 = coords[index2*3 +1 ],
37
- z2 = coords[index2*3 +2 ];
38
- float dA = cn1[type1*ntypes + type2];
39
- float dC = cn2[type1*ntypes + type2];
40
- float dQ1Q2 = charge11*charge22;
41
- float xd = x1 - x2, yd = y1 - y2, zd = z1 - z2;
42
- 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);
45
- }
46
- }
47
- return energy_vdw + energy_electrostatic;
48
- }
49
-
50
12
const size_t NATOMS = 8 *8 + 1 ;
51
13
const float MAXCOORD = 100.0 ;
52
14
const float MAXCHARGE = 2.0 ;
@@ -85,7 +47,96 @@ void display_atoms(float pos[], float charge[], int types[]) {
85
47
}
86
48
}
87
49
88
- int main () {
50
+
51
+ float energyold (int natoms, float coords[], float charges[],
52
+ int ntypes, int types[], float cn1[], float cn2[],
53
+ int nexcluded[], int excluded_indices[]) {
54
+ float energy_vdw = 0.0 ;
55
+ float energy_electrostatic = 0.0 ;
56
+ int excludedAtomIndex = 0 ;
57
+ int notExcluded = 1 ;
58
+ for (size_t index1 = 0 ; index1 < natoms - 1 ; ++index1) {
59
+ int numberOfExcludedAtomsRemaining = nexcluded[index1];
60
+ float charge11 = charges[index1];
61
+ int type1 = types[index1];
62
+ float x1 = coords[index1*3 +0 ],
63
+ y1 = coords[index1*3 +1 ],
64
+ z1 = coords[index1*3 +2 ];
65
+
66
+ for (size_t index2 = index1+1 ; index2 < natoms; ++index2) {
67
+ int maybe_excluded_atom = excluded_indices[excludedAtomIndex];
68
+
69
+ if (numberOfExcludedAtomsRemaining > 0 && maybe_excluded_atom == index2) {
70
+ ++excludedAtomIndex;
71
+ --numberOfExcludedAtomsRemaining;
72
+ continue ;
73
+ }
74
+
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
+ float energynew (int natoms, float coords[], float charges[],
96
+ int ntypes, int types[], float cn1[], float cn2[],
97
+ int nexcluded[], int excluded_indices[]) {
98
+ float energy_vdw = 0.0 ;
99
+ float energy_electrostatic = 0.0 ;
100
+ int excludedAtomIndex = 0 ;
101
+ int notExcluded = 1 ;
102
+ for (size_t index1 = 0 ; index1 < natoms - 1 ; ++index1) {
103
+ int numberOfExcludedAtomsRemaining = nexcluded[index1];
104
+ float charge11 = charges[index1];
105
+ int type1 = types[index1];
106
+ float x1 = coords[index1*3 +0 ],
107
+ y1 = coords[index1*3 +1 ],
108
+ z1 = coords[index1*3 +2 ];
109
+
110
+ for (size_t index2 = index1+1 ; index2 < natoms; ++index2) {
111
+ int maybe_excluded_atom = excluded_indices[excludedAtomIndex];
112
+ notExcluded = !(numberOfExcludedAtomsRemaining > 0 && maybe_excluded_atom == index2);
113
+ excludedAtomIndex += (numberOfExcludedAtomsRemaining > 0 && maybe_excluded_atom == index2);
114
+ numberOfExcludedAtomsRemaining -= (numberOfExcludedAtomsRemaining > 0 && maybe_excluded_atom == index2);
115
+ float charge22 = charges[index2];
116
+ int type2 = types[index2];
117
+ float x2 = coords[index2*3 +0 ],
118
+ y2 = coords[index2*3 +1 ],
119
+ z2 = coords[index2*3 +2 ];
120
+ float dA = cn1[type1*ntypes + type2];
121
+ float dC = cn2[type1*ntypes + type2];
122
+ float dQ1Q2 = charge11*charge22;
123
+ float xd = x1 - x2, yd = y1 - y2, zd = z1 - z2;
124
+ float distance = std::sqrt (xd*xd + yd*yd + zd*zd);
125
+ energy_vdw += van_der_waals (distance, dA, dC)*notExcluded;
126
+ energy_electrostatic += electrostatic (distance, dQ1Q2)*notExcluded;
127
+ }
128
+
129
+ }
130
+ return (energy_vdw + energy_electrostatic);
131
+ }
132
+
133
+
134
+
135
+ int main (int argc, const char * argv[]) {
136
+ std::string arg1 (argv[1 ]);
137
+ bool donew = (arg1 == " new" );
138
+ size_t num = atoi (argv[2 ]);
139
+
89
140
float pos[NATOMS*3 ];
90
141
float charge[NATOMS];
91
142
int types[NATOMS];
@@ -97,12 +148,24 @@ int main() {
97
148
initialize_positions (pos);
98
149
initialize_charges (charge);
99
150
initialize_types (types);
100
- initialize_exclusions (nexcluded, excluded_indices);
101
-
102
- // display_atoms(pos, charge, types);
151
+ initialize_exclusions (nexcluded, excluded_indices);
152
+
153
+ float tenergy = 0 ;
154
+ if (donew) {
155
+ std::cout << " New method" << " \n " ;
156
+ for ( size_t nn = 0 ; nn<num; nn++ ) {
157
+ tenergy = energynew (NATOMS, pos, charge, 2 , types, cn1, cn2, nexcluded,excluded_indices);
158
+ }
159
+ }
103
160
104
- float tenergy = energy (NATOMS, pos, charge, 2 , types, cn1, cn2,
105
- nexcluded, excluded_indices);
161
+ else {
162
+ std::cout << " Old method" << " \n " ;
163
+ for ( size_t nn = 0 ; nn<num; nn++ ) {
164
+ tenergy = energyold (NATOMS, pos, charge, 2 , types, cn1, cn2,
165
+ nexcluded, excluded_indices);
166
+ }
167
+ }
168
+
106
169
std::cout << " Energy: " << tenergy << std::endl;
107
170
return 0 ;
108
171
}
0 commit comments