Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PcsEnergy.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/PcsEnergy.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
36 
37 // Package headers
44 // AUTO-REMOVED #include <protocols/scoring/methods/pcs2/GridSearchIterator.hh>
49 
50 // Project headers
52 #include <basic/options/option.hh>
53 #include <basic/options/keys/PCS.OptionKeys.gen.hh>
54 
55 #include <core/pose/Pose.hh>
58 
60 
61 #include <basic/datacache/BasicDataCache.hh>
62 #include <basic/Tracer.hh>
63 
64 // Utility headers
65 
66 // Numeric headers
67 
68 // Objexx headers
69 
70 // C++ headers
71 #include <fstream>
72 
74 #include <utility/vector1.hh>
75 
76 
77 namespace protocols {
78 namespace scoring {
79 namespace methods {
80 namespace pcs2 {
81 
82 /// @details This must return a fresh instance of the CarbonHBondEnergy class,
83 /// never an instance already in use
87 ) const {
88  return new PcsEnergy;
89 }
90 
94  sts.push_back( core::scoring::pcs2 );
95  return sts;
96 }
97 
98 basic::Tracer TR_PcsEnergy("protocols.scoring.methods.pcs.PcsEnergy");
99 
100 void
102 }
103 
104 PcsEnergy &
106  std::cerr << "Error, = operator not correctly implemented in the PcsEnergy" << std::endl;
107  utility_exit_with_message("Exiting");
108  if ( this != &other ) {
109  }
110  return *this;
111 }
112 
114  parent( src )
115 {
116  // TR_PcsEnergy << " () called" << std::endl;
117 }
118 
120 }
121 
122 /// c-tor
124  parent( new PcsEnergyCreator )
125 {
126  // TR_PcsEnergy << "constructor called" << std::endl;
127 }
128 
129 /// clone
132  // TR_PcsEnergy << " clone called" << std::endl;
133  return new PcsEnergy;
134 }
135 
136 /////////////////////////////////////////////////////////////////////////////
137 // scoring
138 /////////////////////////////////////////////////////////////////////////////
139 
140 void
143  core::scoring::EnergyMap & totals
144  ) const {
145 
146  core::Size i_multi_data;
147  /*
148  core::Size static kkk(1);
149 
150  kkk++;
151  if(kkk > 50){
152  utility_exit_with_message("EXITING AFTER 50 PCS evals");
153  }
154  */
155 
156  GridSearchIteratorCA grid_it(pose);
157  PcsDataCenterManager & pcs_d_c_m = PCS_multi_data_from_pose(pose); //OK
158  //PcsDataCenterManagerSingleton & pcs_d_c_m_s = PCS_multi_data_from_noone(); //DEOSNT SPEED UP
159 
160  totals[ core::scoring::pcs2 ] = 0;
161 
162 
163 
164  //for ( i_multi_data = 1; i_multi_data <= pcs_d_c_m_s.get_n_multi_data(); ++i_multi_data){
165  for ( i_multi_data = 1; i_multi_data <= pcs_d_c_m.get_n_multi_data(); ++i_multi_data){
166  //PcsDataCenter & pcs_d_c = (pcs_d_c_m_s.get_PCS_data_all())[i_multi_data];
167  PcsDataCenter & pcs_d_c = (pcs_d_c_m.get_PCS_data_all())[i_multi_data];
168  pcs_d_c.update_X_Y_Z_all(pose);
169  totals[ core::scoring::pcs2 ] += calculate_pcs_score_on_PCS_data_center_CA( pose, false, pcs_d_c, i_multi_data, grid_it);
170  }
171 
172 
173 
174  TR_PcsEnergy << "Score found: " << totals[core::scoring::pcs2] << std::endl;
175 }
176 
177 
178 
179 
182  core::Size i_multi_data;
183  core::Size n_multi_data;
184 
185 
187 
188  if (
190 
191  //std::cerr << "PcsDataCenterManager was cached" << std::endl<< std::endl;
193  }
194 
195  // TR_PcsEnergy << "PcsDataCenterManager was NOT cached" << std::endl;
196 
197 
198 
199  using namespace basic::options;
200  using namespace basic::options::OptionKeys;
201 
202  PcsDataCenterManagerOP pcs_d_c_m_OP;
203 
204  pcs_d_c_m_OP = new PcsDataCenterManager();
205 
206  for(i_multi_data = 1; i_multi_data <= n_multi_data; ++i_multi_data ){
207 
208  utility::vector1<std::string> vec_filename;
209  utility::vector1<core::Real> vec_weight;
212 
213  // TR_PcsEnergy << "Initialization of PcsDataCenter of paramagnetic center " << i_multi_data << " / " << n_multi_data << std::endl;
214 
215  core::Size start(PcsEnergyParameterManager::get_instance()->get_PcsEnergyParameter_for(i_multi_data).get_include_only_start());
216 
217  core::Size end(PcsEnergyParameterManager::get_instance()->get_PcsEnergyParameter_for(i_multi_data).get_include_only_end());
218 
219  core::Real individual_scale(PcsEnergyParameterManager::get_instance()->get_PcsEnergyParameter_for(i_multi_data).get_individual_scale());
220 
221  core::Size i2;
222  // TR_PcsEnergy << "File(s) to open: ";
223  for( i2 = 1; i2 <= vec_filename.size() ; i2++ ){
224  //TR_PcsEnergy << vec_filename[i2] << " " ;
225  }
226  //TR_PcsEnergy << std::endl;
227 
228  if(vec_filename.size() == 0){
229  utility_exit_with_message("Missing input file for PCS. Review your setup file");
230  }
231 
232  PcsInputCenter pcs_i_c = PcsInputCenterManager::get_instance()->get_PcsInputCenter_for(vec_filename, vec_weight);
233 
234  //TR_PcsEnergy << pcs_i_c << std::endl;
235 
236  PcsDataCenterOP pcs_d_c_OP;
237  // PcsDataCenter pcs_d (pcs_i_c, exclude_residues);
238  //pcs_d = PcsDataCenter(pcs_i_c, exclude_residues);
239  //pcs_d = PcsDataCenter(pcs_i_c);
240  pcs_d_c_OP = new PcsDataCenter(pcs_i_c, start, end, individual_scale);
241 
242  //(*pcs_d_c_m_OP).get_PCS_data_all().push_back(pcs_d);
243  (*pcs_d_c_m_OP).get_PCS_data_all().push_back(*pcs_d_c_OP);
244  //pose.data().set( core::pose::datacache::CacheableDataType::PSEUDOCONTACT_SHIFT_DATA, pcs_d );
245  }
246 
247  // (*pcs_d_c_m_OP).set_n_multi_data(n_multi_data);
248 
250 
251  // TR_PcsEnergy << *(PcsInputCenterManager::get_instance());
252 
253  return *pcs_d_c_m_OP;
254 }
255 
258 
261  return(pcs_d_c_m_s);
262 }
263 
264 
265 void
267  utility::vector1<PcsTensor> const & vec_tensor,
268  numeric::xyzVector< core::Real > const & best_coo,
269  PcsDataCenter const & pcs_d_c
270 ) const
271 {
272  using namespace basic::options;
273  using namespace basic::options::OptionKeys;
274  using namespace core;
275  core::Size i, j;
276  static core::Size n_rescore(1);
277  //
278 
279 
280  if( option[ basic::options::OptionKeys::PCS::write_extra ].user() ){
282 
283  std::string file_dump (option[ basic::options::OptionKeys::PCS::write_extra ]());
284 
285  std::ofstream myfile;
286  if(n_rescore == 1){
287  myfile.open (file_dump.c_str(), std::ios::out);
288  myfile << "# Tensor: Xxx Xxy Xxz Xyy Xyz x y z" << std::endl;
289  myfile << "# Spins: res_num atom_name PCS_exp PCS_calc PCS_dev PCS_abs_dev" << std::endl;
290  }
291  else{
292  myfile.open (file_dump.c_str(), std::ios::app);
293  }
294  if (!myfile.is_open ()){
295  std::cerr << "Unable to open the file '" << file_dump <<"'" << std::endl;
296  utility_exit();
297  }
298 
299 
300  const utility::vector1<core::Real> & X_all(pcs_d_c.get_X_all());
301  const utility::vector1<core::Real> & Y_all(pcs_d_c.get_Y_all());
302  const utility::vector1<core::Real> & Z_all(pcs_d_c.get_Z_all());
303 
304  for (i = 1 ; i <= pcs_d_c.get_n_lanthanides(); ++i){
305  PcsDataLanthanide const & PCS_d_p_l (pcs_d_c.get_pcs_data_per_lanthanides_all()[i]);
306  utility::vector1<PcsInputLine> const & PCS_d_l_a_s (pcs_d_c.get_PCS_data_line_all_spin());
307 
308  myfile << "#" << PCS_d_p_l.get_filename() << " RESCORE NUMBER " << n_rescore << std::endl;
309 
310  core::Real Xxx(vec_tensor[i].get_chi_xx());
311  core::Real Xxy(vec_tensor[i].get_chi_xy());
312  core::Real Xxz(vec_tensor[i].get_chi_xz());
313  core::Real Xyy(vec_tensor[i].get_chi_yy());
314  core::Real Xyz(vec_tensor[i].get_chi_yz());
315  const utility::vector1<core::Size> & A_index( PCS_d_p_l.get_A_index());
316  // const ObjexxFCL::FArray1D< core::Real > & fstyle_b(PCS_d_p_l.get_fstyle_b());
317  const utility::vector1< core::Real > & cstyle_b(PCS_d_p_l.get_cstyle_b());
318 
319  myfile << "# Tensor: " << std::setw(10) << Xxx << " " << Xxy << " " << Xxz << " " << Xyy << " " << Xyz << " " << best_coo.x() << " " << best_coo.y() << " " << best_coo.z() << std::endl;
320 
321  for ( j = 1; j <= PCS_d_p_l.get_n_pcs(); ++j){
322  core::Real PCS_exp (cstyle_b[j]);
323  core::Size idx (A_index[j]);
324  core::Real x (X_all[idx]);
325  core::Real y (Y_all[idx]);
326  core::Real z (Z_all[idx]);
327  core::Size res_num(PCS_d_l_a_s[idx].get_residue_num());
328  std::string atom_name(PCS_d_l_a_s[idx].get_atom_name());
329  core::Real r5;
330  r5 = fill_A_line_fast(A, best_coo.x(), best_coo.y(), best_coo.z(), x, y, z);
331  core::Real PCS_calc(A[1]*Xxx + A[2]*Xxy + A[3]*Xxz + A[4]*Xyy + A[5]*Xyz);
332  PCS_calc = PCS_calc / r5 * FACT_USI_PRECALC_FOR_A_3;
333  core::Real PCS_dev (PCS_exp - PCS_calc);
334  core::Real PCS_abs_dev (fabs(PCS_exp - PCS_calc));
335  myfile << res_num <<" " << atom_name << std::setw(10) << PCS_exp << " " << PCS_calc<< " " << PCS_dev<< " " << PCS_abs_dev<< " " << std::endl;
336  }
337  }
338  n_rescore ++;
339  myfile.close();
340  }
341 }
342 
343 
346  bool,
347  PcsDataCenter & pcs_d_c,
348  core::Size i_multi_data,
349  GridSearchIteratorCA & grid_it) const{
350 
351  using namespace core;
352  using namespace basic::options;
353  using namespace basic::options::OptionKeys;
354 
355 
356  utility::vector1<PcsTensor> vec_tensor;
358 
359  core::Real pcs_score_total;
360  core::Size i;
361 
362 
363  core::Real pcs_weight_center (PcsEnergyParameterManager::get_instance()->get_PcsEnergyParameter_for(i_multi_data).get_pcs_weight());
364 
365  if (pcs_weight_center == 0){
366  return 0;
367  }
368 
369  //alloc best score and vector and coordinate
370  for ( i = 1; i <= pcs_d_c.get_n_lanthanides(); ++i){
371  PcsTensor PCS_t = PcsTensor(0, 0, 0, 0, 0, ((pcs_d_c.get_pcs_data_per_lanthanides_all())[i]).get_filename());
372  vec_tensor.push_back(PCS_t);
373  }
374  vec_score.resize(pcs_d_c.get_n_lanthanides());
375 
377 
378  //call to calculate the tensors and the score
379  pcs_score_total = CA_search_scores_and_tensors(vec_score, vec_tensor, best_coo, pdb, pcs_d_c, i_multi_data, grid_it);
380 
381  // pcs_score_total = CA_search_scores_and_tensors_with_svd(vec_score, vec_tensor, best_coo, pdb, pcs_d_c, i_multi_data, grid_it);
382 
383  // std::cerr << "x y z " << best_coo.x() << " " << best_coo.y() << " " << best_coo.z() << std::endl;
384 
385  dump_PCS_info(vec_tensor, best_coo, pcs_d_c);
386 
387  //This weight is the weight for the center, defined for each stage.
388  return (pcs_weight_center * pcs_score_total);
389 }
390 
391 
395  PcsDataCenter const & /*pcs_d_c*/,
396  core::optimization::Minimizer & minimizer,
397  utility::vector1<core::Real> & vect_to_opti
398  ) const{
399 
400  core::Size i;
401 
402  core::Real optimized_cost(minimizer.run( vect_to_opti ));
403 
404  best_coo.assign(vect_to_opti[1], vect_to_opti[2], vect_to_opti[3]);
405 
406  for(i = 1; i <= vec_best_tensor.size(); ++i){
407  vec_best_tensor[i].reset_tensor((core::Real)vect_to_opti[3 + 5*(i-1) + 1],
408  (core::Real)vect_to_opti[3 + 5*(i-1) + 2],
409  (core::Real)vect_to_opti[3 + 5*(i-1) + 3],
410  (core::Real)vect_to_opti[3 + 5*(i-1) + 4],
411  (core::Real)vect_to_opti[3 + 5*(i-1) + 5]);
412  }
413  return (optimized_cost);
414 }
415 
419  PcsDataCenter const & /*pcs_d_c*/,
420  core::optimization::Minimizer & minimizer,
421  utility::vector1<core::Real> & vect_to_opti
422  ) const{
423 
424  // core::Size i;
425 
426  core::Real optimized_cost(minimizer.run( vect_to_opti ));
427 
428  best_coo.assign(vect_to_opti[1], vect_to_opti[2], vect_to_opti[3]);
429 
430  return (optimized_cost);
431 }
432 
433 
436  PcsDataCenter const & pcs_d_c/*,
437  core::Real xM,
438  core::Real yM,
439  core::Real zM*/
440  ) const{
441 
442  core::Size i;
443 
444  utility::vector1<core::Real> vect_to_opt;
445 
446  if(vec_best_tensor.size() != pcs_d_c.get_n_lanthanides()){
447  utility_exit_with_message("n_lanthanides and vec_best_tensor size differs in minimize_tensors_from_PCS_data");
448  }
449 
450  for(i = 1; i <= vec_best_tensor.size(); ++i){
451  vect_to_opt.push_back(vec_best_tensor[i].get_chi_xx());
452  vect_to_opt.push_back(vec_best_tensor[i].get_chi_xy());
453  vect_to_opt.push_back(vec_best_tensor[i].get_chi_xz());
454  vect_to_opt.push_back(vec_best_tensor[i].get_chi_yy());
455  vect_to_opt.push_back(vec_best_tensor[i].get_chi_yz());
456  }
457  //FIX
458  TensorsOptimizerFix tensors_opt_fix(pcs_d_c);
459  // optimization::MinimizerOptions options( "dfpmin_armijo_nonmonotone_atol", 0.0000001, true, false, false );
460  // optimization::MinimizerOptions options( "dfpmin", 0.000001, true, false, false );
461  core::optimization::MinimizerOptions options( "dfpmin", 0.000001, true, false, false );
462  core::optimization::Minimizer minimizer(tensors_opt_fix, options );
463 
464  core::Real optimized_cost(minimizer.run( vect_to_opt ));
465 
466 
467  for(i = 1; i <= vec_best_tensor.size(); ++i){
468  vec_best_tensor[i].reset_tensor((core::Real)vect_to_opt[5*(i-1) + 1],
469  (core::Real)vect_to_opt[5*(i-1) + 2],
470  (core::Real)vect_to_opt[5*(i-1) + 3],
471  (core::Real)vect_to_opt[5*(i-1) + 4],
472  (core::Real)vect_to_opt[5*(i-1) + 5]);
473  }
474  return (optimized_cost);
475 }
476 
477 
478 
479 
480 //This will be called for each new pose
483  utility::vector1<PcsTensor> & vec_best_tensor,
485  core::pose::Pose const & /* pdb*/,
486  PcsDataCenter & pcs_d_c,
487  core::Size,
488  GridSearchIteratorCA & grid_it) const{
489 
490  core::Real x, y, z;
491  core::Real best_score/*, score*/; // redefined later ~Labonte
492  core::Size i;
493  core::Size size_of;
494  utility::vector1<core::Real> vec_score_temp;
495  utility::vector1<PcsTensor> vec_tensor_temp;
496 
497  //ref to do atomic best switch in order to avoid copy stuff
498  utility::vector1<core::Real> * vec_score_ref_current;
499  utility::vector1<PcsTensor> * vec_tensor_ref_current;
500 
501  //CA_SEARCH
502  TensorsOptimizer tensors_opt(pcs_d_c);
503  //optimization::MinimizerOptions options( "dfpmin_armijo_nonmonotone_atol", 0.0000001, true, false, false );
504  // optimization::MinimizerOptions options( "dfpmin", 0.00001, true, false, false );
505  //GOOD optimization::MinimizerOptions options( "dfpmin", 0.000001, true, false, false );
506  // optimization::MinimizerOptions options( "dfpmin", 0.000001, true, false, false );
507  core::optimization::MinimizerOptions options( "dfpmin", 0.000001, true, false, false );
508  core::optimization::Minimizer minimizer(tensors_opt, options );
509 
510  //some basic checking...
511  size_of = vec_best_score.size();
512  if((size_of != vec_best_tensor.size())||
513  (size_of != pcs_d_c.get_n_lanthanides())){
514  std::cerr << "Problem in CA_search_scores_and_tensors function" << std::endl;
515  std::cerr << "n_lanthanides = " << pcs_d_c.get_n_lanthanides();
516  std::cerr << "vec_best_tensor.size() = " << vec_best_tensor.size();
517  std::cerr << "vec_best_score.size() = " << vec_best_score.size() << std::endl;
518  utility_exit_with_message("Exiting");
519  }
520 
521  for( i = 1; i <= size_of; i++){
522  vec_tensor_temp.push_back(vec_best_tensor[i]);
523  }
524 
525  vec_score_temp.resize(size_of);
526 
527  //Must be after the resize statment!
528  vec_score_ref_current = & vec_score_temp;
529  vec_tensor_ref_current = & vec_tensor_temp;
530 
531  grid_it.reset();
532  best_coo.assign(0, 0, 0);
533 
534  core::Size i_junk;
535  utility::vector1<PcsTensor> vec_tensor_junk;
536  utility::vector1<core::Real> vec_score_junk;
537  for ( i_junk = 1; i_junk <= pcs_d_c.get_n_lanthanides(); ++i_junk){
538  PcsTensor PCS_t_junk = PcsTensor(0, 0, 0, 0, 0, ((pcs_d_c.get_pcs_data_per_lanthanides_all())[i_junk]).get_filename());
539  vec_tensor_junk.push_back(PCS_t_junk);
540  }
541 
543  best_score = 999999999999999999999999999.9;
544  bool test_at_least_one_iteration = false;
545  core::Size n_best_found(0);
546  core::Real epsilon(0.001);
547 
548  utility::vector1<core::Real> vect_to_opti;
549 
550  vect_to_opti.push_back(0);
551  vect_to_opti.push_back(0);
552  vect_to_opti.push_back(0);
553  core::Size ii;
554  for(ii = 1; ii <= pcs_d_c.get_n_lanthanides(); ++ii){
555  vect_to_opti.push_back(0);
556  vect_to_opti.push_back(0);
557  vect_to_opti.push_back(0);
558  vect_to_opti.push_back(0);
559  vect_to_opti.push_back(0);
560  }
561 
562  core::Size n_identical(1);
563  PcsTensor pcs_t(0, 0, 0, 0, 0, "");
565 
566  while(grid_it.next_center(x, y, z) == true){
567  //std::cerr << "Trying " << x << " " << y << " " << z << std::endl;
568  if(n_best_found > n_identical){
569  TR_PcsEnergy << "Found " << n_best_found << " the same value -> best score" << std::endl;
570  break;
571  }
572 
573  test_at_least_one_iteration = true;
574  // std::cerr << "Trying " << x << " " << y << " " << z << std::endl;
575  temp_coo.assign(x, y, z);
576  //score = 0;
577 
578 
579  vect_to_opti[1] = x;
580  vect_to_opti[2] = y;
581  vect_to_opti[3] = z;
582 
583  //X_Y_Z is updated
584  pcs_d_c.update_matrix_A_all_for_svd(x, y, z);
585 
586  for ( ii = 1; ii <= pcs_d_c.get_n_lanthanides(); ++ii){
587 
588  PcsDataLanthanide & pcs_d_l(pcs_d_l_vec[ii]);
589  pcs_d_l.calculate_tensor_only_with_svd(pcs_t);
590 
591  vect_to_opti[3 + (ii-1)*5 + 1] = pcs_t.get_chi_xx(); //0;
592  vect_to_opti[3 + (ii-1)*5 + 2] = pcs_t.get_chi_xy(); //0;
593  vect_to_opti[3 + (ii-1)*5 + 3] = pcs_t.get_chi_xz(); //0;
594  vect_to_opti[3 + (ii-1)*5 + 4] = pcs_t.get_chi_yy(); //0;
595  vect_to_opti[3 + (ii-1)*5 + 5] = pcs_t.get_chi_yz(); //0;
596  }
597 
598 
599  core::Real score(minimize_tensors_from_PCS_data(vec_tensor_junk, temp_coo, pcs_d_c, minimizer, vect_to_opti));
600  // std::cerr << "Temp found: " << score << std::endl;
601  // std::cerr << "Score: " << score << std::endl;
602  // std::cerr << "Found " << temp_coo.x() << " " << temp_coo.y() << " " << temp_coo.z() << std::endl;
603 
604  if(fabs(score - best_score) < epsilon){
605  n_best_found++;
606  }
607 
608  if ( score < best_score){
609  if(best_score - score > epsilon){
610  n_best_found = 0;
611  }
612  for(i = 1; i <= pcs_d_c.get_n_lanthanides(); ++i){
613  (*vec_tensor_ref_current)[i] = vec_tensor_junk[i];
614  (*vec_score_ref_current)[i] = score / pcs_d_c.get_n_lanthanides();
615  }
616  }
617 
618  if ( score < best_score){
619  best_score = score;
620 
621  best_coo.assign(temp_coo.x(), temp_coo.y(), temp_coo.z());
622 
623  //atomic switch
624  if((vec_score_ref_current != &vec_score_temp) && (vec_score_ref_current != &vec_best_score)){ //test to make sure...
625  std::cerr << "Problem in grid_search_scores_and_tensors function" << std::endl;
626  std::cerr << "The atomic switch is not working (1)" << std::endl;
627  utility_exit_with_message("Exiting");
628  }
629  if(vec_tensor_ref_current == &vec_best_tensor){
630  vec_tensor_ref_current = &vec_tensor_temp;
631  vec_score_ref_current = &vec_score_temp;
632  }
633  else{
634  if(vec_tensor_ref_current != &vec_tensor_temp){ //test to make sure...
635  std::cerr << "Problem in grid_search_scores_and_tensors function" << std::endl;
636  std::cerr << "The atomic switch is not working (2)" << std::endl;
637  utility_exit_with_message("Exiting");
638  }
639  vec_tensor_ref_current = &vec_best_tensor;
640  vec_score_ref_current = &vec_best_score;
641  }
642  //end atomic switch
643  }
644  } //while
645 
646 
647  if(!(n_best_found > n_identical)){
648  // TR_PcsEnergy << " All point visited " << std::endl;
649  }
650 
651  if(test_at_least_one_iteration == false){
652  utility_exit_with_message("The description of the grid search given is too restrictive");
653  }
654 
655  if( vec_tensor_ref_current == &vec_best_tensor ){
656  for(i = 1; i <= vec_best_score.size(); ++i){
657  vec_best_tensor[i].reset_from_ref(vec_tensor_temp[i]);
658  }
659  }
660 
661  if( vec_score_ref_current == &vec_best_score ){
662  for(i = 1; i <= vec_best_score.size(); ++i){
663  vec_best_score[i] = vec_score_temp[i];
664  }
665  }
666  return (best_score);
667 }
668 
669 
670 
673  utility::vector1<PcsTensor> & vec_best_tensor,
675  core::pose::Pose const & /* pdb*/,
676  PcsDataCenter & pcs_d_c,
677  core::Size,
678  GridSearchIteratorCA & grid_it) const{
679 
680  core::Real x, y, z;
681  core::Real best_score/*, score*/; // redefined later ~Labonte
682  core::Size i;
683  core::Size size_of;
684  utility::vector1<core::Real> vec_score_temp;
685  utility::vector1<PcsTensor> vec_tensor_temp;
686 
687  //ref to do atomic best switch in order to avoid copy stuff
688  utility::vector1<core::Real> * vec_score_ref_current;
689  utility::vector1<PcsTensor> * vec_tensor_ref_current;
690 
691  TensorsOptimizerSvd tensors_opt_svd(pcs_d_c);
692  //CA_SVD_SEARCH
693  //optimization::MinimizerOptions options( "dfpmin_armijo_nonmonotone_atol", 0.0000001, true, false, false );
694  // optimization::MinimizerOptions options( "dfpmin", 0.00001, true, false, false );
695  core::optimization::MinimizerOptions options( "dfpmin", 0.000001, true, false, false );
696  core::optimization::Minimizer minimizer(tensors_opt_svd, options );
697 
698  //some basic checking...
699  size_of = vec_best_score.size();
700  if((size_of != vec_best_tensor.size())||
701  (size_of != pcs_d_c.get_n_lanthanides())){
702  std::cerr << "Problem in CA_search_scores_and_tensors function" << std::endl;
703  std::cerr << "n_lanthanides = " << pcs_d_c.get_n_lanthanides();
704  std::cerr << "vec_best_tensor.size() = " << vec_best_tensor.size();
705  std::cerr << "vec_best_score.size() = " << vec_best_score.size() << std::endl;
706  utility_exit_with_message("Exiting");
707  }
708 
709  for( i = 1; i <= size_of; i++){
710  vec_tensor_temp.push_back(vec_best_tensor[i]);
711  }
712 
713  vec_score_temp.resize(size_of);
714 
715  //Must be after the resize statment!
716  vec_score_ref_current = & vec_score_temp;
717  vec_tensor_ref_current = & vec_tensor_temp;
718 
719  grid_it.reset();
720  best_coo.assign(0, 0, 0);
721 
722  core::Size i_junk;
723  utility::vector1<PcsTensor> vec_tensor_junk;
724  utility::vector1<core::Real> vec_score_junk;
725  for ( i_junk = 1; i_junk <= pcs_d_c.get_n_lanthanides(); ++i_junk){
726  PcsTensor PCS_t_junk = PcsTensor(0, 0, 0, 0, 0, ((pcs_d_c.get_pcs_data_per_lanthanides_all())[i_junk]).get_filename());
727  vec_tensor_junk.push_back(PCS_t_junk);
728  }
729 
730 
732  best_score = 999999999999999999999999999.9;
733  bool test_at_least_one_iteration = false;
734  core::Size n_best_found(0);
735  core::Real epsilon(0.0001);
736 
737 
738  utility::vector1<core::Real> vect_to_opti;
739 
740  vect_to_opti.push_back(0);
741  vect_to_opti.push_back(0);
742  vect_to_opti.push_back(0);
743 
744  core::Size n_identical(1);
745 
746  while(grid_it.next_center(x, y, z) == true){
747  // std::cerr << "Trying " << x << " " << y << " " << z << std::endl;
748  if(n_best_found > n_identical){
749  TR_PcsEnergy << "Found " << n_best_found << " the same value -> best score" << std::endl;
750  break;
751  }
752 
753  test_at_least_one_iteration = true;
754  // std::cerr << "Trying " << x << " " << y << " " << z << std::endl;
755  temp_coo.assign(x, y, z);
756  //score = 0;
757 
758 
759  vect_to_opti[1] = x;
760  vect_to_opti[2] = y;
761  vect_to_opti[3] = z;
762 
763  core::Real score(minimize_tensors_from_PCS_data_with_svd(vec_tensor_junk, temp_coo, pcs_d_c, minimizer, vect_to_opti));
764 
765  // std::cerr << "Score: " << score << std::endl;
766  // std::cerr << "Found " << temp_coo.x() << " " << temp_coo.y() << " " << temp_coo.z() << std::endl;
767 
768  if(fabs(score - best_score) < epsilon){
769  n_best_found++;
770  }
771 
772  if ( score < best_score){
773  if(best_score - score > epsilon){
774  n_best_found = 0;
775  }
776  for(i = 1; i <= pcs_d_c.get_n_lanthanides(); ++i){
777  (*vec_tensor_ref_current)[i] = vec_tensor_junk[i];
778  (*vec_score_ref_current)[i] = score / pcs_d_c.get_n_lanthanides();
779  }
780  }
781 
782  if ( score < best_score){
783  best_score = score;
784 
785  best_coo.assign(temp_coo.x(), temp_coo.y(), temp_coo.z());
786 
787  //atomic switch
788  if((vec_score_ref_current != &vec_score_temp) && (vec_score_ref_current != &vec_best_score)){ //test to make sure...
789  std::cerr << "Problem in grid_search_scores_and_tensors function" << std::endl;
790  std::cerr << "The atomic switch is not working (1)" << std::endl;
791  utility_exit_with_message("Exiting");
792  }
793  if(vec_tensor_ref_current == &vec_best_tensor){
794  vec_tensor_ref_current = &vec_tensor_temp;
795  vec_score_ref_current = &vec_score_temp;
796  }
797  else{
798  if(vec_tensor_ref_current != &vec_tensor_temp){ //test to make sure...
799  std::cerr << "Problem in grid_search_scores_and_tensors function" << std::endl;
800  std::cerr << "The atomic switch is not working (2)" << std::endl;
801  utility_exit_with_message("Exiting");
802  }
803  vec_tensor_ref_current = &vec_best_tensor;
804  vec_score_ref_current = &vec_best_score;
805  }
806  //end atomic switch
807  }
808  } //while
809 
810 
811  if(!(n_best_found > n_identical)){
812  TR_PcsEnergy << " All point visited " << std::endl;
813  }
814 
815  if(test_at_least_one_iteration == false){
816  utility_exit_with_message("The description of the grid search given is too restrictive");
817  }
818 
819  if( vec_tensor_ref_current == &vec_best_tensor ){
820  for(i = 1; i <= vec_best_score.size(); ++i){
821  vec_best_tensor[i].reset_from_ref(vec_tensor_temp[i]);
822  }
823  }
824 
825  if( vec_score_ref_current == &vec_best_score ){
826  for(i = 1; i <= vec_best_score.size(); ++i){
827  vec_best_score[i] = vec_score_temp[i];
828  }
829  }
830  return (best_score);
831 }
834 {
835  return 1; // Initial versioning
836 }
837 
838 
839 } // PCS
840 } // methods
841 } // scoring
842 } // protocols