Debian Codegraph @

ccp4mg-utils/mmut/mmut_nma.cc

raw symbols SCIP
1 /*
2 mmut/mmut_nma.h: CCP4MG Molecular Graphics Program
3 Copyright (C) 2001-2008 University of York, CCLRC
4
5 This library is free software: you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 version 3, modified in accordance with the provisions of the
8 license to address the requirements of UK law.
9
10 You should have received a copy of the modified GNU Lesser General
11 Public License along with this library. If not, copies may be
12 downloaded from http://www.ccp4.ac.uk/ccp4license.php
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18 */
19 #include "mmut_nma.h"
20 #include <vector>
21 #include <matrix.h>
22 #include <cartesian.h>
23 #include <geomutil.h>
24
25 #include <iostream>
26 #include <iomanip>
27
28 #include <math.h>
29
30 #ifndef M_PI
31 #define M_PI 3.141592653589793238462643
32 #endif
33 #define kBA 1.3806503e-03 // Boltzmann constant scaled for angstroms.
34 #define TEMP 298.155
35
36
37
38 NormalModeAnalysis::NormalModeAnalysis(const std::vector<Cartesian> &carts, const int type, const double cutoff){
39 if(type==MMUT_ANM){
40 if(cutoff<0.0)
41 NMA_ANM(carts,13.0);
42 else
43 NMA_ANM(carts,cutoff);
44 _type = MMUT_ANM;
45 }else{
46 if(cutoff<0.0)
47 NMA_GNM(carts,7.0);
48 else
49 NMA_GNM(carts,cutoff);
50 _type = MMUT_GNM;
51 }
52 }
53
54 /*
55 NormalModeAnalysis::NormalModeAnalysis(const std::vector<Cartesian> &carts, const std::string &type, const double cutoff){
56 if(type=="ANM"){
57 if(cutoff<0.0)
58 NMA_ANM(carts,13.0);
59 else
60 NMA_ANM(carts,cutoff);
61 _type = MMUT_ANM;
62 }else{
63 if(cutoff<0.0)
64 NMA_GNM(carts,7.0);
65 else
66 NMA_GNM(carts,cutoff);
67 _type = MMUT_GNM;
68 }
69 }
70 */
71
72 void NormalModeAnalysis::NMA_ANM(const std::vector<Cartesian> &carts, const double cutoff){
73 /* Generate the Kirchoff matrix */
74
75 matrix k(carts.size(),carts.size());
76
77 for(unsigned i=0;i<k.get_rows();i++){
78 for(unsigned j=0;j<i;j++){
79 k(i,j) = ((LineLength(carts[i],carts[j])<cutoff)?-1:0);
80 k(j,i) = k(i,j);
81 }
82 }
83 for(unsigned i=0;i<k.get_rows();i++){
84 k(i,i) = 0.0;
85 for(unsigned j=0;j<k.get_columns();j++){
86 if(i!=j) k(i,i) -= k(i,j);
87 }
88 }
89
90 /* Generate the Hessian for anisotropic network model */
91 matrix a(k.get_rows()*3,k.get_columns()*3);
92
93 for(unsigned i=0;i<k.get_rows();i++){
94 for(unsigned j=0;j<k.get_columns();j++){
95 double len = LineLength(carts[i],carts[j]);
96 len = len * len;
97 if(i!=j){
98 double xij = (carts[i].get_x()-carts[j].get_x());
99 double yij = (carts[i].get_y()-carts[j].get_y());
100 double zij = (carts[i].get_z()-carts[j].get_z());
101 a(i*3, j*3) = k(i,j)*xij*xij/len;
102 a(i*3+1,j*3+1) = k(i,j)*yij*yij/len;
103 a(i*3+2,j*3+2) = k(i,j)*zij*zij/len;
104 a(i*3, j*3+1) = a(i*3+1,j*3) = k(i,j)*xij*yij/len;
105 a(i*3, j*3+2) = a(i*3+2,j*3) = k(i,j)*xij*zij/len;
106 a(i*3+1,j*3+2) = a(i*3+2,j*3+1) = k(i,j)*yij*zij/len;
107 }
108 }
109 }
110
111 for(unsigned i=0;i<k.get_rows();i++){
112 for(unsigned j=0;j<k.get_columns();j++){
113 if(i!=j){
114 a(i*3,i*3) -= a(i*3,j*3);
115 a(i*3+1,i*3+1) -= a(i*3+1,j*3+1);
116 a(i*3+2,i*3+2) -= a(i*3+2,j*3+2);
117 a(i*3+1,i*3) -= a(i*3+1,j*3);
118 a(i*3,i*3+1) -= a(i*3,j*3+1);
119 a(i*3+1,i*3+2) -= a(i*3+1,j*3+2);
120 a(i*3+2,i*3+1) -= a(i*3+2,j*3+1);
121 a(i*3+2,i*3) -= a(i*3+2,j*3);
122 a(i*3,i*3+2) -= a(i*3,j*3+2);
123 }
124 }
125 }
126 //std::cout << gnm[0] << "\n";
127 //std::cout << gnm[1] << "\n";
128 //std::cout << a.get_columns() << "\n";
129 //std::cout << a.Determinant() << "\n";
130 //std::cout << a << "\n";
131 // The number crunching
132 hessian = a;
133 eigen = a.SortEigenvalues(a.Eigen());
134 }
135
136 void NormalModeAnalysis::NMA_GNM(const std::vector<Cartesian> &carts, const double cutoff){
137
138 /* Generate the Kirchoff matrix for isotropic Gaussian network model */
139 matrix a(carts.size(),carts.size());
140 for(unsigned i=0;i<a.get_rows();i++){
141 for(unsigned j=0;j<i;j++){
142 a(i,j) = ((LineLength(carts[i],carts[j])<cutoff)?-1:0);
143 a(j,i) = a(i,j);
144 }
145 }
146 for(unsigned i=0;i<a.get_rows();i++){
147 a(i,i) = 0.0;
148 for(unsigned j=0;j<a.get_columns();j++){
149 if(i!=j) a(i,i) -= a(i,j);
150 }
151 }
152
153 // The number crunching
154 hessian = a;
155 eigen = a.SortEigenvalues(a.Eigen());
156
157 }
158
159 std::vector<double> NormalModeAnalysis::GetBValues() const {
160 // These B-values have to be divided by the force constant.
161 // Unfortunately we do not know that, so these values need
162 // to be scaled to agree with the experimental B-factors.
163 // This isn't quite self-defeating, we can now calculate the
164 // force constant and use it for other purposes.
165 std::vector<double> bvalues;
166
167 if(_type==MMUT_ANM){
168 std::cerr << "Warning, B-value calculation for anisotropic method not yet implemented!\n";
169 return bvalues;
170 }
171
172 matrix my_hinv = GetDiagonalInverseHessian();
173 //std::cout << "Inverse Hessian\n";
174 //std::cout << my_hinv << "\n";
175
176 double prefactor=8.0*M_PI*M_PI*kBA*TEMP;
177
178 for(unsigned i=0;i<my_hinv.get_rows();i++){
179 bvalues.push_back(prefactor*my_hinv(i,i));
180 }
181
182 return bvalues;
183 }
184
185 void NormalModeAnalysis::StoreInverseHessian() {
186 hinv = GetInverseHessian();
187 }
188
189 std::vector<std::vector<double> > NormalModeAnalysis::GetModeShapes(const double gammainv) const {
190
191 // Almost certainly only correct for GNM at the moment.
192 // Returns N-1 (or N-6 for ANM) vectors of N-values describing mobility of nodes.
193 // gammainv can be calculated by comparing experimental with calculated B-values.
194
195 std::vector<std::vector<double> > mode_shapes;
196 matrix eigenvecs = eigen[0];
197 matrix eigenvals = eigen[1];
198 int start = 1;
199 if(GetType()==MMUT_ANM){
200 start = 6;
201 }
202
203 double prefactor=8.0*M_PI*M_PI*kBA*TEMP*gammainv;
204
205 for(unsigned i=start;i<eigenvecs.get_columns();i++){
206 mode_shapes.push_back(std::vector<double>(eigenvecs.get_rows()));
207 if(fabs(eigenvals(i,0))>1e-7){
208 double einv = 1.0/eigenvals(i,0);
209 for(unsigned ii=0;ii<eigenvecs.get_rows();ii++){
210 mode_shapes.back()[ii] = prefactor*einv*eigenvecs(ii,i)*eigenvecs(ii,i);
211 }
212 }
213 }
214 return mode_shapes;
215 }
216
217 matrix NormalModeAnalysis::GetDiagonalInverseHessian() const {
218
219 if(hinv.get_rows()==eigen[0].get_rows())
220 return hinv;
221
222 matrix eigenvecs = eigen[0];
223 matrix eigenvals = eigen[1];
224 matrix my_hinv = matrix(eigenvecs.get_rows(),eigenvecs.get_rows());
225
226 int start = 1;
227 if(GetType()==MMUT_ANM){
228 start = 6;
229 }
230
231 for(unsigned i=start;i<eigenvecs.get_columns();i++){
232 if(fabs(eigenvals(i,0))>1e-7){
233 double einv = 1.0/eigenvals(i,0);
234 for(unsigned ii=0;ii<my_hinv.get_rows();ii++){
235 my_hinv(ii,ii) += einv*eigenvecs(ii,i)*eigenvecs(ii,i);
236 }
237 }
238 }
239 return my_hinv;
240
241 }
242
243 matrix NormalModeAnalysis::GetInverseHessian() const {
244
245 if(hinv.get_rows()==eigen[0].get_rows())
246 return hinv;
247
248 matrix eigenvecs = eigen[0];
249 matrix eigenvals = eigen[1];
250 matrix my_hinv = matrix(eigenvecs.get_rows(),eigenvecs.get_rows());
251
252 int start = 1;
253 if(GetType()==MMUT_ANM){
254 start = 6;
255 }
256
257 for(unsigned i=start;i<eigenvecs.get_columns();i++){
258 if(fabs(eigenvals(i,0))>1e-7){
259 double einv = 1.0/eigenvals(i,0);
260 matrix u(eigenvecs.get_rows(),1);
261 for(unsigned j=0;j<eigenvecs.get_rows();j++){
262 u(j,0) = eigenvecs(j,i);
263 }
264 matrix uu = u*u.Transpose();
265 for(unsigned ii=0;ii<my_hinv.get_rows();ii++){
266 for(unsigned jj=0;jj<my_hinv.get_columns();jj++){
267 my_hinv(ii,jj) += einv*uu(ii,jj);
268 }
269 }
270 }
271 }
272 return my_hinv;
273
274 }
275
276 std::vector<std::vector<Cartesian> > NormalModeAnalysis::GetDisplacements(const int nsteps) const{
277
278 std::vector<std::vector<Cartesian> > displacements;
279 matrix evecs = eigen[0];
280 matrix evals = eigen[1];
281
282 if(_type==MMUT_GNM){
283 std::cerr << "Cannot compute displacements for Gaussian Network Model, try Anisotropic method\n";
284 }
285
286 displacements = std::vector<std::vector<Cartesian> > (evecs.get_columns()-6);
287
288 for(unsigned k=6;k<evecs.get_columns();k++){
289 double t = 0.0; // And now we have to loop over time.
290 double freq = sqrt(evals(k,0));
291 double time_per_cycle = 1.0/freq;
292 double tstep = time_per_cycle/nsteps;
293 for(int step=0;step<nsteps;step++,t+=tstep){
294 for(unsigned i=0;i<evecs.get_rows();i+=3){
295 double x = evecs(i,k)*sin(2*M_PI*freq*t);
296 double y = evecs(i+1,k)*sin(2*M_PI*freq*t);
297 double z = evecs(i+2,k)*sin(2*M_PI*freq*t);
298 displacements[k-6].push_back(Cartesian(x,y,z));
299 }
300 }
301 }
302
303 return displacements;
304
305 }