|
| 1 | +#include <cmath> |
| 2 | +#include <iostream> |
| 3 | + |
| 4 | +inline float van_der_waals(float distance, float dA, float dC) { |
| 5 | + return dA*std::pow(distance, -12) - dC*std::pow(distance, -6); |
| 6 | +} |
| 7 | + |
| 8 | +inline float electrostatic(float distance, float dQ1Q2) { |
| 9 | + return dQ1Q2/distance; |
| 10 | +} |
| 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 | +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 }; |
| 53 | + float charge[4] = {0.85, 0.95, 1.05, 1.15}; |
| 54 | + int types[4] = {0, 1, 1, 0}; |
| 55 | + float cn1[2] = {0.5, 0.7}; |
| 56 | + float cn2[2] = {0.3, 0.6}; |
| 57 | + int nexcluded[4] = {1, 1, 1, 1}; |
| 58 | + // 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, |
| 62 | + nexcluded, excluded_indices); |
| 63 | + std::cout << "Energy: " << tenergy << std::endl; |
| 64 | + return 0; |
| 65 | +} |
0 commit comments