OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
reference_density_kernels.hh
Go to the documentation of this file.
1 #ifndef REFERENCE_DENSITY_KERNELS_H_
2 #define REFERENCE_DENSITY_KERNELS_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * OpenMM *
6  * -------------------------------------------------------------------------- *
7  * This is part of the OpenMM molecular simulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org. *
11  * *
12  * Portions copyright (c) 2014 Stanford University and the Authors. *
13  * Authors: Peter Eastman *
14  * Contributors: *
15  * *
16  * Permission is hereby granted, free of charge, to any person obtaining a *
17  * copy of this software and associated documentation files (the "Software"), *
18  * to deal in the Software without restriction, including without limitation *
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
20  * and/or sell copies of the Software, and to permit persons to whom the *
21  * Software is furnished to do so, subject to the following conditions: *
22  * *
23  * The above copyright notice and this permission notice shall be included in *
24  * all copies or substantial portions of the Software. *
25  * *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
29  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
30  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
31  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
32  * USE OR OTHER DEALINGS IN THE SOFTWARE. *
33  * -------------------------------------------------------------------------- */
34 
36 #include <openmm/Platform.h>
37 #include <vector>
38 #include <set>
39 #include <limits>
40 
41 namespace ost{ namespace mol{ namespace mm{
42 
47 public:
48  ReferenceCalcDensityForceKernel(std::string name, const OpenMM::Platform& platform) : CalcDensityForceKernel(name, platform), data_(NULL) {
49  }
50 
52 
59  void initialize(const OpenMM::System& system, const DensityForce& force);
68  double execute(OpenMM::ContextImpl& context, bool includeForces, bool includeEnergy);
75  void copyParametersToContext(OpenMM::ContextImpl& context, const DensityForce& force);
76 private:
77 
78  inline Real GetValue(int a, int b, int c){
79  return data_[a * idx_helper_one_ + b * idx_helper_two_ + c];
80  }
81 
82  inline Real GetIntpolValue(Real x, Real y, Real z){
83 
84  //distances to origin
85  Real dx = x - x_origin_;
86  Real dy = y - y_origin_;
87  Real dz = z - z_origin_;
88 
89  int x_bin = std::floor(dx * one_over_x_sampling_);
90  int y_bin = std::floor(dy * one_over_y_sampling_);
91  int z_bin = std::floor(dz * one_over_z_sampling_);
92 
93  if(x_bin < 0 || x_bin >= (x_extent_-1) ||
94  y_bin < 0 || y_bin >= (y_extent_-1) ||
95  z_bin < 0 || z_bin >= (z_extent_-1)) return 0.0;
96 
97  //distances in fraction of bin towards base bin
98  dx = (dx - x_bin * x_sampling_) * one_over_x_sampling_;
99  dy = (dy - y_bin * y_sampling_) * one_over_y_sampling_;
100  dz = (dz - z_bin * z_sampling_) * one_over_z_sampling_;
101 
102  intpol_values_[0] = this->GetValue(x_bin,y_bin,z_bin);
103  intpol_values_[1] = this->GetValue(x_bin+1,y_bin,z_bin);
104  intpol_values_[2] = this->GetValue(x_bin,y_bin+1,z_bin);
105  intpol_values_[3] = this->GetValue(x_bin+1,y_bin+1,z_bin);
106  intpol_values_[4] = this->GetValue(x_bin,y_bin,z_bin+1);
107  intpol_values_[5] = this->GetValue(x_bin+1,y_bin,z_bin+1);
108  intpol_values_[6] = this->GetValue(x_bin,y_bin+1,z_bin+1);
109  intpol_values_[7] = this->GetValue(x_bin+1,y_bin+1,z_bin+1);
110 
111  //do first bilinear interpolation
112  Real f11 = (1.0 - dx) * intpol_values_[0] + dx * intpol_values_[1];
113  Real f12 = (1.0 - dx) * intpol_values_[2] + dx * intpol_values_[3];
114  Real f21 = (1.0 - dx) * intpol_values_[4] + dx * intpol_values_[5];
115  Real f22 = (1.0 - dx) * intpol_values_[6] + dx * intpol_values_[7];
116 
117  Real f1 = (1.0 - dy) * f11 + dy * f12;
118  Real f2 = (1.0 - dy) * f21 + dy * f22;
119 
120  return (1.0 - dz) * f1 + dz * f2;
121  }
122 
123  //this stuff is required to store and extract the internal density values
124  Real* data_;
125  Real intpol_values_[8];
126  int idx_helper_one_;
127  int idx_helper_two_;
128  Real x_sampling_;
129  Real y_sampling_;
130  Real z_sampling_;
131  Real one_over_x_sampling_;
132  Real one_over_y_sampling_;
133  Real one_over_z_sampling_;
134  Real half_x_sampling_;
135  Real half_y_sampling_;
136  Real half_z_sampling_;
137  Real x_origin_;
138  Real y_origin_;
139  Real z_origin_;
140  int x_extent_;
141  int y_extent_;
142  int z_extent_;
143 
144  //this is stuff used in the actual force calculation
145  Real resolution_;
146  Real s_;
147  Real one_s_;
148  Real square_one_s_;
149  Real one_sqrt_2_pi_s_;
150  Real padding_dist_;
151  Real scaling_;
152  std::vector<Real> density_values_;
153  std::vector<Real> body_values_;
154 
155  //this is stuff containing information of the simulated system
156  int num_bodies_;
157  int num_particles_;
158  std::vector<float> particle_masses_;
159  std::vector<std::vector<int> > bodies_;
160 };
161 
162 }}} // ns
163 
164 #endif /*REFERENCE_DENSITY_KERNELS_H_*/
float Real
Definition: base.hh:44
ReferenceCalcDensityForceKernel(std::string name, const OpenMM::Platform &platform)
void copyParametersToContext(OpenMM::ContextImpl &context, const DensityForce &force)
double execute(OpenMM::ContextImpl &context, bool includeForces, bool includeEnergy)
void initialize(const OpenMM::System &system, const DensityForce &force)