forked from plumed/plumed2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptimalAlignment.cpp
399 lines (318 loc) · 11.4 KB
/
OptimalAlignment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Copyright (c) 2013 The plumed team
(see the PEOPLE file at the root of the distribution for a list of names)
See http://www.plumed-code.org for more information.
This file is part of plumed, version 2.0.
plumed is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
plumed is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with plumed. If not, see <http://www.gnu.org/licenses/>.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#include "OptimalAlignment.h"
#include "Kearsley.h"
#include "Log.h"
#include <cmath>
#include <iostream>
#include <cstdlib>
#include "Random.h"
using namespace std;
namespace PLMD{
OptimalAlignment::OptimalAlignment( const std::vector<double> & align, const std::vector<double> & displace, const std::vector<Vector> & p0, const std::vector<Vector> & p1 , Log* &log )
:log(log){
// kearsley init to null
mykearsley=NULL;
if (mykearsley==NULL) {
mykearsley=new Kearsley(p0,p1,align,log);
}
// copy the structure into place
assignP0(p0);
assignP1(p1);
assignAlign(align);
assignDisplace(displace);
// basic check
if(p0.size() != p1.size()){
(this->log)->printf("THE SIZE OF THE TWO FRAMES TO BE ALIGNED ARE DIFFERENT\n");
}
// fast behaviour: if all the alignment and displacement are 1.0 then go for fast
fast=true;
for (unsigned i=0;i<align.size();i++ ){
if(align[i]!=displace[i] || align[i]!=1.0)fast=false;
}
}
OptimalAlignment::~OptimalAlignment(){
if (mykearsley!=NULL)delete mykearsley;
}
void OptimalAlignment::assignP0( const std::vector<Vector> & p0 ){
this->p0=p0;
if(mykearsley!=NULL){mykearsley->assignP0(p0);}else{cerr<<"kearsley is not initialized"<<endl; exit(0);}
}
void OptimalAlignment::assignP1( const std::vector<Vector> & p1 ){
this->p1=p1;
if(mykearsley!=NULL){mykearsley->assignP1(p1);}else{cerr<<"kearsley is not initialized"<<endl; exit(0);}
}
void OptimalAlignment::assignAlign( const std::vector<double> & align ){
this->align=align;
if(mykearsley!=NULL){mykearsley->assignAlign(align);}else{cerr<<"kearsley is not initialized"<<endl; exit(0);}
}
void OptimalAlignment::assignDisplace( const std::vector<double> & displace ){
this->displace=displace;
}
double OptimalAlignment::calculate(bool squared, std::vector<Vector> & derivatives){
bool rmsd=!squared ;
double err;
// at this point everything should be already in place for calculating the alignment (p1,p2,align)
// here everything is done with kearsley algorithm. Extension to other optimal alignment algos is
// possible here below with a switch
err=mykearsley->calculate(rmsd); // this calculates the MSD: transform into RMSD
// check findiff alignment
//mykearsley->finiteDifferenceInterface(rmsd);
if(fast){
//log->printf("Doing fast: ERR %12.6f \n",err);
derrdp0=mykearsley->derrdp0;
derrdp1=mykearsley->derrdp1;
derivatives=derrdp0;
}else{
/// TODO this interface really sucks since is strongly asymmetric should be re-engineered.
err=weightedAlignment(rmsd);
//log->printf("Doing slow: ERR %12.6f \n",err);
derivatives=derrdp0;
}
// destroy the kearsley object?
return err;
}
#ifdef __INTEL_COMPILER
#pragma intel optimization_level 2
#endif
/// this does the weighed alignment if the vector of alignment is different from displacement
double OptimalAlignment::weightedAlignment( bool rmsd){
double tmp0,tmp1,walign,wdisplace,const1,ret;
unsigned i,k,l,m,n,o,oo,mm;
unsigned natoms=p0.size();
Kearsley *myk=mykearsley; /// just a shortcut
/// TODO : these two blocks can be calculated once forever after the initialization (exception for certain methods?)
/// clear derivatives
if (derrdp0.size()!=natoms)derrdp0.resize(natoms);
if (derrdp1.size()!=natoms)derrdp1.resize(natoms);
// clear the container
for(i=0;i<natoms;i++){
derrdp0[i][0]=derrdp0[i][1]=derrdp0[i][2]=0.;
derrdp1[i][0]=derrdp1[i][1]=derrdp1[i][2]=0.;
}
walign=0.;
vector<int> alignmap;
for(i=0;i<natoms;i++){
if (align[i]>0.){
alignmap.push_back(i);
walign+=align[i];
}
if (align[i]<0.){cerr<<"FOUND ALIGNMENT WEIGHT NEGATIVE!"<<endl;exit(0);};
}
wdisplace=0.;
vector<int> displacemap;
for(i=0;i<natoms;i++){
if (displace[i]>0.){
displacemap.push_back(i);
wdisplace+=displace[i];
}
if (displace[i]<0.){cerr<<"FOUND ALIGNMENT WEIGHT NEGATIVE!"<<endl;exit(0);};
}
tmp0=0.;
vector<Vector> array_n_3;
array_n_3.resize(natoms);
for(i=0;i<array_n_3.size();i++)array_n_3[i][0]=array_n_3[i][1]=array_n_3[i][2]=0.;
// err= (1/totdisplace) sum_k_l displace_k*((p0reset_k_l- sum_k_m rot_l_m*p1reset_k_m )**2)
//for(kk=0;kk<displacemap.size();kk++){
// k=displacemap[kk];
for(k=0;k<natoms;k++){
for(l=0;l<3;l++){
tmp1=0.;
// contribution from rotated reference frame //
for(m=0;m<3;m++){
tmp1-=myk->rotmat0on1[l][m]*myk->p1reset[k][m];
}
// contribution from running centered frame //
tmp1+= myk->p0reset[k][l];
array_n_3[k][l]=tmp1; // store coefficents for derivative usage//
tmp0+=tmp1*tmp1*displace[k]; //squared distance added//
}
}
tmp0=tmp0/wdisplace;
// log->printf(" ERRR NEW %f \n",tmp0);
ret=tmp0;
/* DERIVATIVE CALCULATION:respect to running frame */
for(k=0;k<natoms;k++){
for(l=0;l<3;l++){
tmp1 =2.*array_n_3[k][l]*displace[k]/wdisplace ; //ok
const1=2.*align[k]/(walign*wdisplace);
if(const1>0.){
for(oo=0;oo<displacemap.size();oo++){
o=displacemap[oo];
tmp1 -=const1*array_n_3[o][l]*displace[o]; //ok
}
}
for(mm=0;mm<displacemap.size();mm++){
m=displacemap[mm];
const1=2.* displace[m]/wdisplace ;
for(n=0;n<3;n++){
tmp0=0.;
for(o=0;o<3;o++){
int ind=n*3*3*natoms+o*3*natoms+l*natoms+k; //ok
tmp0+=myk->dmatdp0[ind]*myk->p1reset[m][o];
}
tmp0*=-const1*array_n_3[m][n];
tmp1+=tmp0;
}
}
derrdp0[k][l]=tmp1;
}
}
//exit(0);
//return ret;
bool do_frameref_der=true;
// derivatives of
// err= (1/totdisplace) sum_k_l displace_k*((p0reset_k_l- sum_m rot_l_m*p1reset_k_m )**2)
// respect p1:
// derr_dp1=(1/totdisplace) sum_k_l 2*displace_k*(p0reset_k_l- sum_m rot_l_m*p1reset_k_m )
// *d/dp1 ( p0reset_k_l- sum_m rot_l_m*p1reset_k_m)
// =
// (1/totdisplace) sum_k_l 2*displace_k*(p0reset_k_l- sum_m rot_l_m*p1reset_k_m )*
// *(d/dp1 p0reset_k_l
// - sum_m (d/dp1 rot_l_m)*p1reset_k_m
// - sum_m rot_l_m*(d/dp1 p1reset_k_m ) )
// =
// sum_k_l 2*displace_k/totdisplace* array_n_3_k_l
// *(- sum_m (d/dp1 rot_l_m)*p1reset_k_m
// - sum_m rot_l_m*(d/dp1 p1reset_k_m ) )
if(do_frameref_der){
for(k=0;k<natoms;k++){
// for(kk=0;kk<displacemap.size();kk++){
// k=displacemap[kk];
for(l=0;l<3;l++){
tmp1=0.;
for(mm=0;mm<displacemap.size();mm++){
m=displacemap[mm];
const1=2.* displace[m]/wdisplace ;
for(n=0;n<3;n++){
tmp0=0.;
for(o=0;o<3;o++){
int ind=n*3*3*natoms+o*3*natoms+l*natoms+k;
tmp0+=myk->dmatdp1[ind]*myk->p1reset[m][o];
}
tmp0*=-const1*array_n_3[m][n];
tmp1+= tmp0;
}
}
tmp0=0.;
for(o=0;o<3;o++){
tmp0+=array_n_3[k][o]*myk->rotmat0on1[o][l];
}
tmp1+=-tmp0*2.*displace[k]/wdisplace;
tmp0=0.;
for(mm=0;mm<displacemap.size();mm++){
m=displacemap[mm];
for(o=0;o<3;o++){
tmp0+=array_n_3[m][o]*myk->rotmat0on1[o][l]*displace[m];
}
}
tmp1 += tmp0*2.*align[k]/(walign*wdisplace);
derrdp1[k][l]=tmp1;
}
}
}
/// probably not really the way it should be
if (rmsd){
ret=sqrt(ret);
double tmp=0.5/ret;
for(unsigned i=0;i<natoms;i++){
derrdp0[i][0]=derrdp0[i][0]*tmp;
derrdp0[i][1]=derrdp0[i][1]*tmp;
derrdp0[i][2]=derrdp0[i][2]*tmp;
derrdp1[i][0]=derrdp1[i][0]*tmp;
derrdp1[i][1]=derrdp1[i][1]*tmp;
derrdp1[i][2]=derrdp1[i][2]*tmp;
}
}
return ret;
}
double OptimalAlignment::weightedFindiffTest( bool rmsd){
Random rnd;
log->printf("Entering rmsd finite difference test system\n ");
log->printf("RMSD OR MSD: %s\n",(rmsd)?"rmsd":"msd");
log->printf("-------------------------------------------\n");
log->printf("TEST1: derivative of the value (derr_dr0/derr_dr1)\n");
//// test 1
double step=1.e-8,olderr,delta,err;
vector<Vector> fakederivatives;
fakederivatives.resize(p0.size());
fast=false;
// randomizing alignments and displacements
/* for (i=0;i<p0.size();i++){
// draw a random number
delta=drand48();
delta1=drand48();
if(delta>delta1){
align[i]=delta;
}else{align[i]=0.;};
delta=drand48();
delta1=drand48();
if(delta>delta1){
displace[i]=delta;
}else{displace[i]=0.;}
}*/
//// get initial value of the error and derivative of it
assignAlign(align);
assignDisplace(displace);
olderr=calculate(rmsd, fakederivatives);
log->printf("INITIAL ERROR VALUE: %e\n",olderr);
// randomizing alignments and displacements
log->printf("TESTING: derrdp0 \n");
for(unsigned j=0;j<3;j++){
for(unsigned i=0;i<derrdp0.size();i++){
// random displacement
delta=(rnd.RandU01()-0.5)*2*step;
p0[i][j]+=delta;
assignP0( p0 );
err=calculate(rmsd, fakederivatives);
p0[i][j]-=delta;
assignP0( p0 );
switch(j){
case 0:
log->printf("TESTING: X %4u ANAL %18.9f NUMER %18.9f DELTA %18.9f DISP %6.2f ALIGN %6.2f \n",i,derrdp0[i][j],(err-olderr)/delta,derrdp0[i][j]-(err-olderr)/delta,displace[i],align[i]);break;
case 1:
log->printf("TESTING: Y %4u ANAL %18.9f NUMER %18.9f DELTA %18.9f DISP %6.2f ALIGN %6.2f \n",i,derrdp0[i][j],(err-olderr)/delta,derrdp0[i][j]-(err-olderr)/delta,displace[i],align[i]);break;
case 2:
log->printf("TESTING: Z %4u ANAL %18.9f NUMER %18.9f DELTA %18.9f DISP %6.2f ALIGN %6.2f \n",i,derrdp0[i][j],(err-olderr)/delta,derrdp0[i][j]-(err-olderr)/delta,displace[i],align[i]);break;
}
}
}
log->printf("TESTING: derrdp1 \n");
for(unsigned j=0;j<3;j++){
for(unsigned i=0;i<derrdp1.size();i++){
// random displacement
delta=(rnd.RandU01()-0.5)*2*step;
p1[i][j]+=delta;
assignP1( p1 );
err=calculate(rmsd, fakederivatives);
p1[i][j]-=delta;
assignP1( p1 );
switch(j){
case 0:
log->printf("TESTING: X %4u ANAL %18.9f NUMER %18.9f DELTA %18.9f DISP %6.2f ALIGN %6.2f \n",i,derrdp1[i][j],(err-olderr)/delta,derrdp1[i][j]-(err-olderr)/delta,displace[i],align[i]);break;
case 1:
log->printf("TESTING: Y %4u ANAL %18.9f NUMER %18.9f DELTA %18.9f DISP %6.2f ALIGN %6.2f \n",i,derrdp1[i][j],(err-olderr)/delta,derrdp1[i][j]-(err-olderr)/delta,displace[i],align[i]);break;
case 2:
log->printf("TESTING: Z %4u ANAL %18.9f NUMER %18.9f DELTA %18.9f DISP %6.2f ALIGN %6.2f \n",i,derrdp1[i][j],(err-olderr)/delta,derrdp1[i][j]-(err-olderr)/delta,displace[i],align[i]);break;
}
}
}
exit(0);
// This is to avoid warnings:
return 0.0;
}
}