|
| 1 | +#include <iostream> |
| 2 | +#include <cstdio> |
| 3 | +#include <string> |
| 4 | +#include <cmath> |
| 5 | + |
| 6 | +#define TENM3 10e-3 |
| 7 | +#define MIN(XXX,YYY) ((XXX<YYY)?XXX:YYY) |
| 8 | +#define MAX(XXX,YYY) ((XXX<YYY)?YYY:XXX) |
| 9 | + |
| 10 | + |
| 11 | +int enzyme_dup; |
| 12 | +int enzyme_out; |
| 13 | +int enzyme_const; |
| 14 | + |
| 15 | +struct Dihedral { |
| 16 | + float sinPhase; |
| 17 | + float cosPhase; |
| 18 | + float V; |
| 19 | + float DN; |
| 20 | + float IN; |
| 21 | + int I1; |
| 22 | + int I2; |
| 23 | + int I3; |
| 24 | + int I4; |
| 25 | +}; |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + extern double __enzyme_autodiff(void*, ...); |
| 30 | + |
| 31 | +void dump_vec(const std::string& msg, float* deriv, size_t num) { |
| 32 | + std::cout << msg << " "; |
| 33 | + for ( int i=0; i<num; i++ ) { |
| 34 | + std::cout << deriv[i] << " "; |
| 35 | + } |
| 36 | + std::cout << "\n"; |
| 37 | +} |
| 38 | + |
| 39 | +#define power2(VAR) (VAR*VAR) |
| 40 | +#define mysqrt(VAR) (std::sqrt(VAR)) |
| 41 | +#define reciprocal(VAR) (1.0/VAR) |
| 42 | + |
| 43 | +float dihedral_energy(Dihedral* dihedral_begin, Dihedral* dihedral_end, float* pos) { |
| 44 | +#define USE_EXPLICIT_DECLARES 1 |
| 45 | +#define DECLARE_FLOAT(VAR) float VAR; |
| 46 | + float EraseLinearDihedral; |
| 47 | + float sinPhase; |
| 48 | + float cosPhase; |
| 49 | + float V; |
| 50 | + float DN; |
| 51 | + float IN; |
| 52 | + float x1, y1, z1; |
| 53 | + float x2, y2, z2; |
| 54 | + float x3, y3, z3; |
| 55 | + float x4, y4, z4; |
| 56 | +#include "_DihedralEnergy_termDeclares.cc" |
| 57 | +#define DIHEDRAL_SET_PARAMETER(VAR) { VAR = dihedral->VAR; } |
| 58 | +#define DIHEDRAL_SET_POSITION(VAR,IDX,OFFSET) { VAR = pos[IDX+OFFSET]; } |
| 59 | +#define DIHEDRAL_ENERGY_ACCUMULATE(VAR) { result += VAR; } |
| 60 | + int I1; |
| 61 | + int I2; |
| 62 | + int I3; |
| 63 | + int I4; |
| 64 | + float result = 0.0; |
| 65 | + float SinNPhi; |
| 66 | + float CosNPhi; |
| 67 | + for ( auto dihedral = dihedral_begin; dihedral < dihedral_end; dihedral++ ) { |
| 68 | +#include "_DihedralEnergy_termCode.cc" |
| 69 | + } |
| 70 | +#undef DECLARE_FLOAT |
| 71 | +#undef DIHEDRAL_SET_PARAMETER |
| 72 | +#undef DIHEDRAL_SET_POSITION |
| 73 | +#undef DIHEDRAL_ENERGY_ACCUMULATE |
| 74 | + return result; |
| 75 | +} |
| 76 | + |
| 77 | +void grad_dihedral_energy(Dihedral* dihedral_begin, Dihedral* dihedral_end, float* pos, float* deriv ) { |
| 78 | + __enzyme_autodiff( (void*)stretch_energy, |
| 79 | + enzyme_const, dihedral_begin, |
| 80 | + enzyme_const, dihedral_end, |
| 81 | + enzyme_dup, pos, deriv ); |
| 82 | +} |
| 83 | + |
| 84 | +float old_dihedral_energy(Dihedral* dihedral_begin, Dihedral*dihedral_end, float* pos, float* deriv) { |
| 85 | +#define USE_EXPLICIT_DECLARES 1 |
| 86 | +#define DECLARE_FLOAT(VAR) float VAR; |
| 87 | + float sinPhase; |
| 88 | + float cosPhase; |
| 89 | + float V; |
| 90 | + float DN; |
| 91 | + float IN; |
| 92 | + float x1, y1, z1; |
| 93 | + float x2, y2, z2; |
| 94 | + float x3, y3, z3; |
| 95 | + float x4, y4, z4; |
| 96 | +#include "_Dihedral_termDeclares.cc" |
| 97 | +#define DIHEDRAL_SET_PARAMETER(VAR) { VAR = dihedral->VAR; } |
| 98 | +#define DIHEDRAL_SET_POSITION(VAR,IDX,OFFSET) { VAR = pos[IDX+OFFSET]; } |
| 99 | +#define DIHEDRAL_ENERGY_ACCUMULATE(VAR) { result += VAR; } |
| 100 | +#define DIHEDRAL_FORCE_ACCUMULATE(IDX,OFFSET,VAR) { deriv[IDX+OFFSET] += VAR; } |
| 101 | + int I1; |
| 102 | + int I2; |
| 103 | + int I3; |
| 104 | + int I4; |
| 105 | + float result = 0.0; |
| 106 | + bool calcForce = true; |
| 107 | +#define DIHEDRAL_CALC_FORCE 1 |
| 108 | + for ( auto dihedral = dihedral_begin; dihedral<dihedral_end; dihedral++ ) { |
| 109 | +#include "_Dihedral_termCode.cc" |
| 110 | + } |
| 111 | +#undef DECLARE_FLOAT |
| 112 | +#undef DIHEDRAL_SET_PARAMETER |
| 113 | +#undef DIHEDRAL_SET_POSITION |
| 114 | +#undef DIHEDRAL_ENERGY_ACCUMULATE |
| 115 | + return result; |
| 116 | +} |
| 117 | + |
| 118 | +void zeroVec(float* deriv, size_t num) { |
| 119 | + for (int i=0; i<num; i++ ) { |
| 120 | + deriv[i] = 0.0; |
| 121 | + } |
| 122 | +} |
| 123 | +int main( int argc, const char* argv[] ) { |
| 124 | + float pos[12] = {0.0, 19.0, 3.0, 10.0, 7.0, 80.0, |
| 125 | + 20.0, 15.0, 17.0, 25.0, 44.0, 23.0 }; |
| 126 | + float deriv[12]; |
| 127 | + Stretch stretch[] = { {10.0, 2.0, 0, 3}, {20.0, 3.0, 6, 9} }; |
| 128 | + |
| 129 | + dump_vec("pos", pos, 12); |
| 130 | + float energy = 0.0; |
| 131 | + std::string arg1(argv[1]); |
| 132 | + bool donew = (arg1 == "new"); |
| 133 | + size_t num = atoi(argv[2]); |
| 134 | + if (donew) { |
| 135 | + std::cout << "New method" << "\n"; |
| 136 | + for ( size_t nn = 0; nn<num; nn++ ) { |
| 137 | + zeroVec(deriv,12); |
| 138 | + grad_dihedral_energy( &dihedral[0], &dihedral[2], pos, deriv); |
| 139 | + } |
| 140 | + } else { |
| 141 | + std::cout << "Old method" << "\n"; |
| 142 | + for ( size_t nn = 0; nn<num; nn++ ) { |
| 143 | + zeroVec(deriv,12); |
| 144 | + energy = old_dihedral_energy( &dihedral[0], &dihedral[2], pos, deriv ); |
| 145 | + } |
| 146 | + } |
| 147 | + std::cout << "Energy = " << energy << "\n"; |
| 148 | + dump_vec("deriv", deriv, 12); |
| 149 | + |
| 150 | +} |
0 commit comments