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/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 June 2009
30  ////////////////////////////////////////////////
31 
32 
33 // Unit headers
36 // AUTO-REMOVED #include <protocols/scoring/methods/pcs/PseudocontactShiftInput.hh> // REQUIRED FOR WINDOWS
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 #include <sstream>
52 // AUTO-REMOVED #include <iostream>
53 
54 #include <utility/vector1.hh>
55 
56 
57 namespace protocols{
58 namespace scoring{
59 namespace methods{
60 namespace pcs{
61 
62 basic::Tracer TR_tsr_opt("protocols.scoring.methods.pcs.TensorsOptimizer");
63 
65  pcs_d_(PCS_data())
66 {
67  utility_exit_with_message("You shouldn't call the empty constructor for class TensorsOptimizer");
68 }
69 
70 
72  pcs_d_(pcs_d)
73 {
74 }
75 
77 }
78 
81  core::Size i, j, idx;
82  core::Size n_la, n_pcs;
83  core::Real x, y, z;
84  core::Real PCS_calc, PCS_exp, score, score_la;;
86 
87  score = 0;
88  n_la = pcs_d_.get_n_lanthanides();
89 
90  if(n_la != (vars.size() - 3) / 5){
91  TR_tsr_opt << "n_la: " << n_la << " (vars.size() - 3) / 5):" << (vars.size() - 3) / 5 << " vars.size(): " << vars.size() <<std::endl;
92  utility_exit_with_message("The number of lanthanides is inconsistent with the derivatives size of vars");
93  }
94 
95  //those are the lanthanides current coordinates.
96  core::Real xM(vars[1]);
97  core::Real yM(vars[2]);
98  core::Real zM(vars[3]);
99 
101 
105 
106  if(n_la != pcs_d_p_l_a.size()){
107  utility_exit_with_message("The number of lanthanides is inconsistent");
108  }
109 
110  for (i = 1; i <= n_la; i++){
111  core::Real Xxx(vars[3 + 5*(i-1) + 1]);
112  core::Real Xxy(vars[3 + 5*(i-1) + 2]);
113  core::Real Xxz(vars[3 + 5*(i-1) + 3]);
114  core::Real Xyy(vars[3 + 5*(i-1) + 4]);
115  core::Real Xyz(vars[3 + 5*(i-1) + 5]);
116 
117  PCS_data_per_lanthanides const & pcs_d_p_l(pcs_d_p_l_a[i]);
118  utility::vector1<core::Size> const & A_index( pcs_d_p_l.get_A_index());
119  ObjexxFCL::FArray1D< core::Real > const & fstyle_b(pcs_d_p_l.get_fstyle_b());
120 
121  core::Real weight(pcs_d_p_l.get_weight());
122  n_pcs = pcs_d_p_l.get_n_pcs();
123 
124  // std::cerr << weight << " applyed to minimization" << std::endl;
125  score_la = 0;
126  for (j = 1; j <= n_pcs; j++){
127  idx = A_index[j]; //index on the vector X_all_, Y_all_, Z_all_
128  x = X_all[idx];
129  y = Y_all[idx];
130  z = Z_all[idx];
131  fill_A_line(A, xM, yM, zM, x, y, z);
132  PCS_calc = A[1]*Xxx + A[2]*Xxy + A[3]*Xxz + A[4]*Xyy + A[5]*Xyz;
133  PCS_exp = fstyle_b(j);
134  score_la += (PCS_calc-PCS_exp) * (PCS_calc-PCS_exp);
135  }
136  score += score_la * weight / pcs_d_p_l.get_normalization_factor() / pcs_d_p_l.get_normalization_factor();
137  }
138  /*
139  std::cerr << "current: ";
140  for (i = 1; i<= vars.size(); i++){
141  std::cerr << vars[i] << " ";
142  }
143  std::cerr << std::endl;
144  */
145 
146  return(sqrt(score));
147 }
148 
149 void
152  ) const{
153  core::Size i, j, k, idx;
154  core::Size n_la, n_pcs;
155  core::Real x, y, z, xS, yS, zS;
156  core::Real PCS_calc, PCS_exp, common, common2, common3;
157  core::Real r5, r2, r3;
158 
160 
161  n_la = pcs_d_.get_n_lanthanides();
162 
163  if(n_la != (dE_dvars.size() - 3) / 5){
164  utility_exit_with_message("The number of lanthanides is inconsistent with the derivativessize of dE_dvars");
165  }
166 
167  //lets' init everyone to zero because we are going to use +=
168  for (i = 1; i <= dE_dvars.size(); i++){
169  dE_dvars[i] = 0;
170  }
171 
172  //those are the lanthanides current coordinates.
173  core::Real xM(vars[1]);
174  core::Real yM(vars[2]);
175  core::Real zM(vars[3]);
176 
178 
182 
183  if(n_la != pcs_d_p_l_a.size()){
184  utility_exit_with_message("The number of lanthanides is inconsistent");
185  }
186 
187  for (i = 1; i <= n_la; i++){
188  core::Real Xxx(vars[3 + 5*(i-1) + 1]);
189  core::Real Xxy(vars[3 + 5*(i-1) + 2]);
190  core::Real Xxz(vars[3 + 5*(i-1) + 3]);
191  core::Real Xyy(vars[3 + 5*(i-1) + 4]);
192  core::Real Xyz(vars[3 + 5*(i-1) + 5]);
193 
194  PCS_data_per_lanthanides const & pcs_d_p_l(pcs_d_p_l_a[i]);
195  utility::vector1<core::Size> const & A_index( pcs_d_p_l.get_A_index());
196  ObjexxFCL::FArray1D< core::Real > const & fstyle_b(pcs_d_p_l.get_fstyle_b());
197 
198  core::Real weight(pcs_d_p_l.get_weight());
199  n_pcs = pcs_d_p_l.get_n_pcs();
200  for (j = 1; j <= n_pcs; j++){
201  idx = A_index[j]; //index on the vector X_all_, Y_all_, Z_all_
202  xS = X_all[idx];
203  yS = Y_all[idx];
204  zS = Z_all[idx];
205  fill_A_line(A, xM, yM, zM, xS, yS, zS);
206  PCS_calc = A[1]*Xxx + A[2]*Xxy + A[3]*Xxz + A[4]*Xyy + A[5]*Xyz;
207  PCS_exp = fstyle_b(j);
208  common = 2 * (PCS_calc - PCS_exp) * weight;
209 
210  //This is for the derivatve of Xxx, Xxy, Xxz, Xyy, Xyz
211  for (k = 1; k <= 5; k++){
212  dE_dvars[3 + 5*(i-1) + k] += common * A[k];
213  }
214 
215  //This is for the derivatve of xM, yM, zM
216  //could be a little bit optimized?
217  x=xS-xM;
218  y=yS-yM;
219  z=zS-zM;
220  r2=x*x+y*y+z*z;
221  r3=sqrt(r2)*r2;
222  r5=r3*r2;
223  common2 = 5*r3*4*PCS_calc * numeric::constants::d::pi / 10000.0 ;
224  common3 = common/(4*r5 * numeric::constants::d::pi) *10000.0 ;
225  dE_dvars[1] += (x*common2 - 2*(x*Xxx + y*Xxy + z*Xxz)) * common3;
226  dE_dvars[2] += (y*common2 - 2*(x*Xxy + y*Xyy + z*Xyz)) * common3;
227  dE_dvars[3] += (z*common2 - 2*(-z*Xxx + x*Xxz -z*Xyy + y*Xyz)) * common3;
228 
229  }
230  }
231 
232 
233  /*
234  for (i = 1; i<= dE_dvars.size(); i++){
235  std::cerr << dE_dvars[i] << " ";
236  }
237 
238  std::cerr <<" should be equalt to:" << std::endl;
239  dfunc_test(vars);
240  std::cerr <<" current: ";
241 
242  for (i = 1; i<= dE_dvars.size(); i++){
243  std::cerr << vars[i] << " ";
244  }
245  std::cerr << std::endl;
246  */
247 }
248 
249 
250 /*
251 //This function is meant to calculate numeric derivatives to test.
252 void
253 TensorsOptimizer::dfunc_test(optimization::Multivec const & vars) const{
254 
255  core::Size i, j, k, idx;
256  core::Size n_la, n_pcs;
257  core::Real x, y, z, xS, yS, zS;
258  core::Real PCS_calc, PCS_exp, common, common2, common3, PCS_calc_p_d, PCS_calc_m_d;
259  core::Real r5, r2, r3;
260  core::Real delta(0.00001);
261 
262  utility::vector1<core::Real> A(5, 0);
263 
264  utility::vector1<core::Real> A_p_d(5, 0);
265  utility::vector1<core::Real> A_m_d(5, 0);
266 
267  n_la = pcs_d_.get_n_lanthanides();
268 
269  optimization::Multivec dE_dvars(3+5*n_la);
270 
271  if(n_la != (dE_dvars.size() - 3) / 5){
272  utility_exit_with_message("The number of lanthanides is inconsistent with the derivativessize of dE_dvars");
273  }
274 
275  //lets' init everyone to zero because we are going to use +=
276  for (i = 1; i <= dE_dvars.size(); i++){
277  dE_dvars[i] = 0;
278  }
279 
280  //those are the lanthanides current coordinates.
281  core::Real xM(vars[1]);
282  core::Real yM(vars[2]);
283  core::Real zM(vars[3]);
284 
285  const utility::vector1<PCS_data_per_lanthanides> & pcs_d_p_l_a(pcs_d_.get_pcs_data_per_lanthanides_all());
286 
287  const utility::vector1<core::Real> & X_all(pcs_d_.get_X_all());
288  const utility::vector1<core::Real> & Y_all(pcs_d_.get_Y_all());
289  const utility::vector1<core::Real> & Z_all(pcs_d_.get_Z_all());
290 
291  if(n_la != pcs_d_p_l_a.size()){
292  utility_exit_with_message("The number of lanthanides is inconsistent");
293  }
294 
295  for (i = 1; i <= n_la; i++){
296  core::Real Xxx(vars[3 + 5*(i-1) + 1]);
297  core::Real Xxy(vars[3 + 5*(i-1) + 2]);
298  core::Real Xxz(vars[3 + 5*(i-1) + 3]);
299  core::Real Xyy(vars[3 + 5*(i-1) + 4]);
300  core::Real Xyz(vars[3 + 5*(i-1) + 5]);
301  const PCS_data_per_lanthanides & pcs_d_p_l(pcs_d_p_l_a[i]);
302  const utility::vector1<core::Size> & A_index( pcs_d_p_l.get_A_index());
303  const FArray1D< core::Real > & fstyle_b(pcs_d_p_l.get_fstyle_b());
304  core::Real weight(pcs_d_p_l.get_weight());
305  n_pcs = pcs_d_p_l.get_n_pcs();
306  for (j = 1; j <= n_pcs; j++){
307  idx = A_index[j]; //index on the vector X_all_, Y_all_, Z_all_
308  xS = X_all[idx];
309  yS = Y_all[idx];
310  zS = Z_all[idx];
311  fill_A_line(A, xM, yM, zM, xS, yS, zS);
312  PCS_calc = A[1]*Xxx + A[2]*Xxy + A[3]*Xxz + A[4]*Xyy + A[5]*Xyz;
313  PCS_exp = fstyle_b(j);
314  common = 2.0 * (PCS_calc - PCS_exp) * weight;
315 
316 
317 
318 
319  PCS_calc_p_d = A[1]*(Xxx+delta) + A[2]*Xxy + A[3]*Xxz + A[4]*Xyy + A[5]*Xyz;
320  PCS_calc_m_d = A[1]*(Xxx-delta) + A[2]*Xxy + A[3]*Xxz + A[4]*Xyy + A[5]*Xyz;
321  dE_dvars[3 + 5*(i-1) + 1] += common * (PCS_calc_p_d - PCS_calc_m_d) / (2.0*delta);
322 
323  PCS_calc_p_d = A[1]*Xxx + A[2]*(Xxy+delta) + A[3]*Xxz + A[4]*Xyy + A[5]*Xyz;
324  PCS_calc_m_d = A[1]*Xxx + A[2]*(Xxy-delta) + A[3]*Xxz + A[4]*Xyy + A[5]*Xyz;
325  dE_dvars[3 + 5*(i-1) + 2] += common * (PCS_calc_p_d - PCS_calc_m_d) / (2.0*delta);
326 
327  PCS_calc_p_d = A[1]*Xxx + A[2]*Xxy + A[3]*(Xxz+delta) + A[4]*Xyy + A[5]*Xyz;
328  PCS_calc_m_d = A[1]*Xxx + A[2]*Xxy + A[3]*(Xxz-delta) + A[4]*Xyy + A[5]*Xyz;
329  dE_dvars[3 + 5*(i-1) + 3] += common * (PCS_calc_p_d - PCS_calc_m_d) / (2.0*delta);
330 
331  PCS_calc_p_d = A[1]*Xxx + A[2]*Xxy + A[3]*Xxz + A[4]*(Xyy+delta) + A[5]*Xyz;
332  PCS_calc_m_d = A[1]*Xxx + A[2]*Xxy + A[3]*Xxz + A[4]*(Xyy-delta) + A[5]*Xyz;
333  dE_dvars[3 + 5*(i-1) + 4] += common * (PCS_calc_p_d - PCS_calc_m_d) / (2.0*delta);
334 
335  PCS_calc_p_d = A[1]*Xxx + A[2]*Xxy + A[3]*Xxz + A[4]*Xyy + A[5]*(Xyz+delta);
336  PCS_calc_m_d = A[1]*Xxx + A[2]*Xxy + A[3]*Xxz + A[4]*Xyy + A[5]*(Xyz-delta);
337  dE_dvars[3 + 5*(i-1) + 5] += common * (PCS_calc_p_d - PCS_calc_m_d) / (2.0*delta);
338 
339 
340  fill_A_line(A_p_d, xM + delta, yM, zM, xS, yS, zS);
341  PCS_calc_p_d = A_p_d[1]*Xxx + A_p_d[2]*Xxy + A_p_d[3]*Xxz + A_p_d[4]*Xyy + A_p_d[5]*Xyz;
342  fill_A_line(A_m_d, xM - delta, yM, zM, xS, yS, zS);
343  PCS_calc_m_d = A_m_d[1]*Xxx + A_m_d[2]*Xxy + A_m_d[3]*Xxz + A_m_d[4]*Xyy + A_m_d[5]*Xyz;
344  dE_dvars[1] += common * (PCS_calc_p_d - PCS_calc_m_d) / (2.0*delta);
345 
346  fill_A_line(A_p_d, xM, yM + delta, zM, xS, yS, zS);
347  PCS_calc_p_d = A_p_d[1]*Xxx + A_p_d[2]*Xxy + A_p_d[3]*Xxz + A_p_d[4]*Xyy + A_p_d[5]*Xyz;
348  fill_A_line(A_m_d, xM, yM - delta, zM, xS, yS, zS);
349  PCS_calc_m_d = A_m_d[1]*Xxx + A_m_d[2]*Xxy + A_m_d[3]*Xxz + A_m_d[4]*Xyy + A_m_d[5]*Xyz;
350  dE_dvars[2] += common * (PCS_calc_p_d - PCS_calc_m_d) / (2.0*delta);
351 
352 
353  fill_A_line(A_p_d, xM, yM, zM + delta, xS, yS, zS);
354  PCS_calc_p_d = A_p_d[1]*Xxx + A_p_d[2]*Xxy + A_p_d[3]*Xxz + A_p_d[4]*Xyy + A_p_d[5]*Xyz;
355  fill_A_line(A_m_d, xM, yM, zM - delta, xS, yS, zS);
356  PCS_calc_m_d = A_m_d[1]*Xxx + A_m_d[2]*Xxy + A_m_d[3]*Xxz + A_m_d[4]*Xyy + A_m_d[5]*Xyz;
357  dE_dvars[3] += common * (PCS_calc_p_d - PCS_calc_m_d) / (2.0*delta);
358 
359  }
360  }
361 
362 
363  for (i = 1; i<= dE_dvars.size(); i++){
364  std::cerr << dE_dvars[i] << " ";
365  }
366  std::cerr << std::endl;
367 
368 }
369 */
370 
371 
372 }//namespace pcs
373 }//namespace methods
374 }//namespace scoring
375 }//namespace protocols