vdw_new.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <iostream>
  2. #include <Eigen/Core>
  3. using namespace Eigen;
  4. #ifndef SCALAR
  5. #define SCALAR float
  6. #endif
  7. #ifndef SIZE
  8. #define SIZE 10000
  9. #endif
  10. #ifndef REPEAT
  11. #define REPEAT 10000
  12. #endif
  13. typedef Matrix<SCALAR, Eigen::Dynamic, 1> Vec;
  14. using namespace std;
  15. SCALAR E_VDW(const Vec &interactions1, const Vec &interactions2)
  16. {
  17. return (interactions2.cwise()/interactions1)
  18. .cwise().cube()
  19. .cwise().square()
  20. .cwise().square()
  21. .sum();
  22. }
  23. int main()
  24. {
  25. //
  26. // 1 2 3 4 ... (interactions)
  27. // ka . . . . ...
  28. // rab . . . . ...
  29. // energy . . . . ...
  30. // ... ... ... ... ... ...
  31. // (variables
  32. // for
  33. // interaction)
  34. //
  35. Vec interactions1(SIZE), interactions2(SIZE); // SIZE is the number of vdw interactions in our system
  36. // SetupCalculations()
  37. SCALAR rab = 1.0;
  38. interactions1.setConstant(2.4);
  39. interactions2.setConstant(rab);
  40. // Energy()
  41. SCALAR energy = 0.0;
  42. for (unsigned int i = 0; i<REPEAT; ++i) {
  43. energy += E_VDW(interactions1, interactions2);
  44. energy *= 1 + 1e-20 * i; // prevent compiler from optimizing the loop
  45. }
  46. cout << "energy = " << energy << endl;
  47. }