Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TensorsOptimizer.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10  //////////////////////////////////////////////
11  /// @begin
12  ///
13  /// @file protocols/scoring/methods/pcs2/TensorsOptimizer.cc
14  ///
15  /// @brief
16  ///
17  /// @detailed
18  ///
19  /// @param
20  ///
21  /// @return
22  ///
23  /// @remarks
24  ///
25  /// @references
26  ///
27  /// @authorsv Christophe Schmitz
28  ///
29  /// @last_modified February 2010
30  ////////////////////////////////////////////////
31 
32 
33 // Unit headers
37 // Package headers
38 
39 // Project headers
40 #include <basic/Tracer.hh>
41 
42 // Utility headers
43 #include <utility/exit.hh>
44 
45 // Numeric headers
46 #include <numeric/constants.hh>
47 
48 // Objexx headers
49 
50 // C++ headers
51 // AUTO-REMOVED #include <iomanip>
52 
53 #include <utility/vector1.hh>
54 
55 
56 
57 
58 
59 namespace protocols{
60 namespace scoring{
61 namespace methods{
62 namespace pcs2{
63 
64 basic::Tracer TR_TensorsOptimizer("protocols.scoring.methods.pcs.TensorsOptimizer");
65 
66 
67  /*
68 TensorsOptimizer::TensorsOptimizer():
69  pcs_d_c_(PcsDataCenter())
70 {
71 
72  utility_exit_with_message("You shouldn't call the empty constructor for class TensorsOptimizer");
73 }
74  */
75 
77 pcs_d_c_(pcs_d_c)
78 {
79 }
80 
82 }
83 
84 
85 bool
87  // return false;
88 
89  core::Real limit(4000);
90  core::Size i;
91  // core::Real Xxx, Xxy, Xxz, Xyy, Xyz;
92  // core::Real xM, yM, zM;
93  core::Size n_la;
94 
95  n_la = pcs_d_c_.get_n_lanthanides();
96 
97  core::Real xM(vars[1]);
98  core::Real yM(vars[1]);
99  core::Real zM(vars[1]);
100  if( (xM*xM + yM*yM + zM*zM) > 90000){ //300 A from 0 0 0
101  //TR_TensorsOptimizer<< " LARGE X Y Z -> ABANDON RESEARCH"<< std::endl;
102  return true;
103  }
104 
105  for (i = 1; i <= n_la; i++){
106  core::Real Xxx(vars[3 + 5*(i-1) + 1]);
107  core::Real Xxy(vars[3 + 5*(i-1) + 2]);
108  core::Real Xxz(vars[3 + 5*(i-1) + 3]);
109  core::Real Xyy(vars[3 + 5*(i-1) + 4]);
110  core::Real Xyz(vars[3 + 5*(i-1) + 5]);
111  if((fabs(Xxx) + fabs(Xxy) + fabs(Xxz) + fabs(Xyy) + fabs(Xyz)) > limit){
112  // TR_TensorsOptimizer<< " LARGE chi values -> ABANDON RESEARCH"<< std::endl;
113  return true;
114  }
115  }
116  return false;
117 }
118 
121  core::Size i, j, idx;
122  core::Size n_pcs;
123  // core::Real x, y, z;
124  core::Real PCS_calc, PCS_exp, score_la;;
125  // utility::vector1<core::Real> A(5, 0);
126 
127 
128  // core::Real static previous_score(0);
129 
130 
131  core::Real score(0);
132 
134 
135  //those are the lanthanides current coordinates.
136  core::Real xM(vars[1]);
137  core::Real yM(vars[2]);
138  core::Real zM(vars[3]);
139 
140  pcs_d_c_.update_matrix_A_all(xM, yM, zM);
141 
143 
144  if(n_la != pcs_d_l_vec.size()){
145  utility_exit_with_message("The number of lanthanides is inconsistent");
146  }
147 
148 
150 
151  for (i = 1; i <= n_la; ++i){
152  core::Real Xxx(vars[3 + 5*(i-1) + 1]);
153  core::Real Xxy(vars[3 + 5*(i-1) + 2]);
154  core::Real Xxz(vars[3 + 5*(i-1) + 3]);
155  core::Real Xyy(vars[3 + 5*(i-1) + 4]);
156  core::Real Xyz(vars[3 + 5*(i-1) + 5]);
157 
158  PcsDataLanthanide const & pcs_d_l(pcs_d_l_vec[i]);
159  utility::vector1<core::Size> const & A_index( pcs_d_l.get_A_index());
160  utility::vector1< core::Real > const & cstyle_b(pcs_d_l.get_cstyle_b());
161  utility::vector1< core::Real > const & cstyle_b_individual_scale_(pcs_d_l.get_cstyle_b_individual_scale());
162 
163 
164  core::Real weight(pcs_d_l.get_weight());
165 
166  n_pcs = pcs_d_l.get_n_pcs();
167  core::Real individual_scale(pcs_d_l.get_individual_scale());
168 
169  score_la = 0;
170  for (j = 1; j <= n_pcs; ++j){
171  idx = A_index[j]; //index on the vector X_all_, Y_all_, Z_all_
172  PCS_calc = A_all[idx][1]*Xxx + A_all[idx][2]*Xxy + A_all[idx][3]*Xxz + A_all[idx][4]*Xyy + A_all[idx][5]*Xyz;
173  PCS_exp = cstyle_b[j];
174 
175  if(individual_scale >= 0.0){
176  score_la += (PCS_calc-PCS_exp) * (PCS_calc-PCS_exp) * cstyle_b_individual_scale_[j];
177  }
178  else{
179 
180  score_la += (PCS_calc-PCS_exp) * (PCS_calc-PCS_exp);
181  }
182  }
183  score += sqrt(score_la) * weight * pcs_d_l.get_normalization_factor_inversed();
184  }
185 
186  return(score);
187 }
188 
189 void
192  ) const{
193 
194  core::Size i;
195  core::Real delta(0.0001);
196 
197  for(i = 1; i <= vars.size(); i++){
198  core::optimization::Multivec vars_bis_pd (vars);
199  core::optimization::Multivec vars_bis_md (vars);
200  vars_bis_pd[i] += delta;
201  vars_bis_md[i] -= delta;
202  core::Real value_pd (func(vars_bis_pd) / delta / 2);
203  core::Real value_md (func(vars_bis_md) / delta / 2);
204  dE_dvars[i] = value_pd - value_md;
205  }
206 }
207 
208 void
211  ) const{
212 
213  dfunc_exact(vars, dE_dvars);
214  // dfunc_numeric(vars, dE_dvars);
215  /*
216  core::Size i;
217  std::cerr << "df_exact ";
218  for(i = 1; i <= vars.size(); i++){
219  std::cerr << std::setw(8) << dE_dvars[i] << " " ;
220  }
221  std::cerr << std::endl;
222 
223  dfunc_numeric(vars, dE_dvars);
224  std::cerr << "df_numer ";
225  for(i = 1; i <= vars.size(); i++){
226  std::cerr << std::setw(8)<< dE_dvars[i] << " " ;
227  }
228  std::cerr << std::endl << std::endl;
229 */
230 }
231 
232 
233 
236  return(func(vars));
237 }
238 
239 void
242  ) const{
243  core::Size i, j, k, idx;
244  core::Size n_la, n_pcs;
245  core::Real x, y, z, xS, yS, zS;
246  core::Real PCS_calc, PCS_exp, common2, common3;
247  core::Real r5, r2, r3;
248 
250 
251 
252 
253 
254  n_la = pcs_d_c_.get_n_lanthanides();
255 
256  if(n_la != (dE_dvars.size() - 3) / 5){
257  utility_exit_with_message("The number of lanthanides is inconsistent with the derivativessize of dE_dvars");
258  }
259 
260  //lets' init everyone to zero because we are going to use +=
261  for (i = 1; i <= dE_dvars.size(); i++){
262  dE_dvars[i] = 0;
263  }
264 
266  vec_temp.resize(dE_dvars.size(), 0);
267 
268  //those are the lanthanides current coordinates.
269  core::Real xM(vars[1]);
270  core::Real yM(vars[2]);
271  core::Real zM(vars[3]);
272 
274 
275  // pcs_d_c_.update_matrix_A_all(xM, yM, zM); //DONE BY func call
277 
281 
282  if(n_la != pcs_d_l_vec.size()){
283  utility_exit_with_message("The number of lanthanides is inconsistent");
284  }
285 
286  for (i = 1; i <= n_la; i++){
287  core::Size p;
288  for (p = 1; p <= vec_temp.size(); p++){
289  vec_temp[p] = 0;
290  }
291 
292  core::Real Xxx(vars[3 + 5*(i-1) + 1]);
293  core::Real Xxy(vars[3 + 5*(i-1) + 2]);
294  core::Real Xxz(vars[3 + 5*(i-1) + 3]);
295  core::Real Xyy(vars[3 + 5*(i-1) + 4]);
296  core::Real Xyz(vars[3 + 5*(i-1) + 5]);
297 
298  PcsDataLanthanide const & pcs_d_l(pcs_d_l_vec[i]);
299  utility::vector1<core::Size> const & A_index( pcs_d_l.get_A_index());
300  utility::vector1< core::Real > const & cstyle_b(pcs_d_l.get_cstyle_b());
301  utility::vector1< core::Real > const & cstyle_b_individual_scale_(pcs_d_l.get_cstyle_b_individual_scale());
302 
303  core::Real weight(pcs_d_l.get_weight());
304  n_pcs = pcs_d_l.get_n_pcs();
305 
306  core::Real square_sum = 0;
307  core::Real individual_scale(pcs_d_l.get_individual_scale());
308  core::Real indi_norm_invers(1.0);
309  for (j = 1; j <= n_pcs; j++){
310  idx = A_index[j]; //index on the vector X_all_, Y_all_, Z_all_
311  xS = X_all[idx];
312  yS = Y_all[idx];
313  zS = Z_all[idx];
314  PCS_calc = A_all[idx][1]*Xxx + A_all[idx][2]*Xxy + A_all[idx][3]*Xxz + A_all[idx][4]*Xyy + A_all[idx][5]*Xyz;
315  PCS_exp = cstyle_b[j];
316 
317  core::Real deviation(PCS_calc - PCS_exp);
318 
319  if(individual_scale >=0.0){
320  //#ifdef INDIVIDUAL_NORMALIZATION
321  indi_norm_invers = cstyle_b_individual_scale_[j];
322  square_sum += deviation * deviation * indi_norm_invers;
323  }
324  else{
325  square_sum += deviation * deviation;
326  }
327 
328  //This is for the derivatve of Xxx, Xxy, Xxz, Xyy, Xyz
329  for (k = 1; k <= 5; k++){
330  if(individual_scale >=0.0){
331  vec_temp[3 + 5*(i-1) + k] += deviation * A_all[idx][k] * indi_norm_invers;
332  }
333  else{
334  vec_temp[3 + 5*(i-1) + k] += deviation * A_all[idx][k];
335  }
336  }
337 
338  //This is for the derivatve of xM, yM, zM; could be a little bit optimized?
339  x=xS-xM;
340  y=yS-yM;
341  z=zS-zM;
342  r2=x*x+y*y+z*z;
343  r3=sqrt(r2)*r2;
344  r5=r3*r2;
345 
346  common2 = FACT_20_PI_OVER_10000*PCS_calc*r3;
347 
348  if(individual_scale >=0.0){
349  common3 = deviation / r5 * indi_norm_invers;
350  }
351  else{
352  common3 = deviation/r5;
353  }
354  vec_temp[1] += (x*common2 - 2*( x*Xxx + y*Xxy + z*Xxz)) * common3;
355  vec_temp[2] += (y*common2 - 2*( x*Xxy + y*Xyy + z*Xyz)) * common3;
356  vec_temp[3] += (z*common2 - 2*(-z*Xxx + x*Xxz - z*Xyy + y*Xyz)) * common3;
357  }
358 
359  core::Real one_over_RMSD(1.0/sqrt(square_sum));
360  core::Real common_temp( one_over_RMSD * weight * pcs_d_l.get_normalization_factor_inversed() );
361 
362  for (k = 1; k <= 5; k++){
363  dE_dvars[3 + 5*(i-1) + k] += vec_temp[3 + 5*(i-1) + k] * common_temp;
364  }
365  dE_dvars[1] += common_temp * vec_temp[1];
366  dE_dvars[2] += common_temp * vec_temp[2];
367  dE_dvars[3] += common_temp * vec_temp[3];
368  }
369 
370  dE_dvars[1] *= FACT_10000_OVER_4PI;
371  dE_dvars[2] *= FACT_10000_OVER_4PI;
372  dE_dvars[3] *= FACT_10000_OVER_4PI;
373 }
374 
375 }//namespace pcs2
376 }//namespace methods
377 }//namespace scoring
378 }//namespace protocols