Skip to content

Commit 68d1b43

Browse files
committed
Merge branch 'exclude_oldnew'
2 parents 19930c0 + b971759 commit 68d1b43

File tree

1 file changed

+107
-44
lines changed

1 file changed

+107
-44
lines changed

Diff for: tlm4_nonbond/exclude.cc

+107-44
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,6 @@ inline float electrostatic(float distance, float dQ1Q2) {
99
return dQ1Q2/distance;
1010
}
1111

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-
5012
const size_t NATOMS = 8*8 + 1;
5113
const float MAXCOORD = 100.0;
5214
const float MAXCHARGE = 2.0;
@@ -85,7 +47,96 @@ void display_atoms(float pos[], float charge[], int types[]) {
8547
}
8648
}
8749

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+
89140
float pos[NATOMS*3];
90141
float charge[NATOMS];
91142
int types[NATOMS];
@@ -97,12 +148,24 @@ int main() {
97148
initialize_positions(pos);
98149
initialize_charges(charge);
99150
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+
}
103160

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+
106169
std::cout << "Energy: " << tenergy << std::endl;
107170
return 0;
108171
}

0 commit comments

Comments
 (0)