Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AtomTreeMultifunc.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 /// @file core/optimization/AtomTreeMultifunc.hh
11 /// @brief Atom tree multifunction class
12 /// @author Phil Bradley
13 
14 /// Unit headers
16 
17 /// Package headers
21 
22 /// Project headers
23 #include <basic/prof.hh>
24 // AUTO-REMOVED #include <core/conformation/Residue.hh>
25 #include <core/pose/Pose.hh>
27 
28 // AUTO-REMOVED #include <core/scoring/ResidueNeighborList.hh>
30 // AUTO-REMOVED #include <core/scoring/etable/EtableEnergy.hh>
32 // AUTO-REMOVED #include <core/scoring/MinimizationGraph.hh>
36 #include <core/scoring/Energies.hh>
37 
38 /// Utility headers
39 #include <utility/string_util.hh>
40 
41 #include <utility/vector1.hh>
42 
43 
44 namespace core {
45 namespace optimization {
46 
47 Real
49  PROF_START( basic::FUNC );
51  Real const score( score_function_( pose_ ) );
52  PROF_STOP( basic::FUNC );
53  return score;
54 }
55 
56 void
57 AtomTreeMultifunc::dfunc( Multivec const & vars, Multivec & dE_dvars ) const
58 {
59  PROF_START( basic::DFUNC );
60  // in atom_tree_minimize.cc
61  atom_tree_dfunc( pose_, min_map_, score_function_, vars, dE_dvars );
62  // optional derivative checking
63  if ( deriv_check_ ) {
65  }
66  PROF_STOP( basic::DFUNC );
67 }
68 
70 {
71  deriv_check_result_ = deriv_check_result;
72 }
73 
75  return pose_;
76 }
77 
78 /// @details Useful debugging code that can be re-enabled by changing the boolean
79 /// variables at the top of this function.
80 void
81 AtomTreeMultifunc::dump( Multivec const & vars, Multivec const & vars2 ) const {
82  bool debug_inaccurateG = false; // disables everything below
83  bool check_score_components = true;
84  bool check_score_components_verbose = false;
85  bool check_rama = false;
86  bool check_hbonds = true;
87  //bool check_nblist = true;
88 
89  if ( ! debug_inaccurateG ) return;
90 
91  static int count_dumped( 0 );
92  static bool after( true ); // dump two poses, a before and an after. Note, dumping poses as pdbs is often useless.
93 
94  if ( after ) { ++count_dumped; after = false; }
95  else { after = true; }
96 
97  pose::Pose pose1( pose_ );
98  pose::Pose pose2( pose_ );
99  min_map_.copy_dofs_to_pose( pose1, vars );
100  min_map_.copy_dofs_to_pose( pose2, vars2 );
101 
103  Real score_vars( score_function_( pose_ ) );
104 
105  min_map_.copy_dofs_to_pose( pose_, vars2 );
106  Real score_vars2( score_function_( pose_ ) );
107 
108  Real alt_score_vars = score_function_( pose1 );
109  pose1.dump_pdb( "atomtree_multifunc_error_pose_before" + utility::to_string( count_dumped ) + ".pdb" );
110 
111  Real alt_score_vars2 = score_function_( pose2 );
112  pose2.dump_pdb( "atomtree_multifunc_error_pose_after" + utility::to_string( count_dumped ) + ".pdb" );
113 
114  std::cerr << "starting pose energies: " << score_vars << " vs " << alt_score_vars << std::endl;
115  pose1.energies().total_energies().show_weighted( std::cerr, score_function_.weights() );
116  std::cerr << std::endl;
117  std::cerr << "moved pose energies: " << score_vars2 << " vs " << alt_score_vars2 << std::endl;
118  pose2.energies().total_energies().show_weighted( std::cerr, score_function_.weights() );
119  std::cerr << std::endl;
120  using namespace scoring;
121 
122  if ( check_score_components ) {
123  // slow! Iterate through all the components and check their derivatives one by one.
124  const_cast< bool & > (deriv_check_) = true;
125  if ( check_score_components_verbose ) {
126  const_cast< bool & > (deriv_check_verbose_) = true;
127  }
128  Multivec dvars( vars );
129  scoring::EnergyMap orig_weights( score_function_.weights() );
130  for ( Size ii = 1; ii <= scoring::n_score_types; ++ii ) {
131  using namespace scoring;
132 
133  if ( score_function_.weights()[ (ScoreType ) ii ] == 0.0 ) continue;
134 
135  for ( Size jj = 1; jj <= scoring::n_score_types; ++jj ) {
136  if ( jj == ii ) {
137  const_cast< scoring::ScoreFunction & > (score_function_).set_weight( (scoring::ScoreType) jj, orig_weights[ (scoring::ScoreType) jj ]);
138  } else if ( score_function_.weights()[ (scoring::ScoreType ) jj ] != 0.0 ) {
139  const_cast< scoring::ScoreFunction & > (score_function_).set_weight( (scoring::ScoreType) jj, 1e-9 );
140  }
141  }
142  std::cout << "Checking score type: " << scoring::ScoreType( ii ) << std::endl;
143  dfunc( vars, dvars ); // invokes numeric derivative checker.
144  }
145  for ( Size ii = 1; ii <= scoring::n_score_types; ++ii ) {
146  if ( orig_weights[ scoring::ScoreType( ii ) ] != 0 ) {
147  const_cast< scoring::ScoreFunction & > (score_function_).set_weight( (scoring::ScoreType)ii, orig_weights[ (scoring::ScoreType) ii ]);
148  }
149  }
150  const_cast< bool & > (deriv_check_) = false;
151  const_cast< bool & > (deriv_check_verbose_) = false;
152  }
153 
154  if ( check_rama ) {
155  // useful if rama seems to be the culprit. This is the only piece of code
156  // that invokes eval_rama_score_all, so that function may be hacked to provide
157  // clearer debugging output.
159  }
160 
161  if ( check_hbonds ) {
162  scoring::hbonds::HBondSet hbond_set;
163  fill_hbond_set( pose1, true, hbond_set );
164 
165  for ( Size ii = 1; ii <= hbond_set.nhbonds(); ++ii ) {
166  scoring::hbonds::HBond const & iihbond = hbond_set.hbond( ii );
167  std::cerr << "Hbond " << ii <<
168  " d: " << iihbond.don_res() << " " << iihbond.don_hatm() <<
169  " a: " << iihbond.acc_res() << " " << iihbond.acc_atm() <<
170  " e: " << iihbond.energy() << " " << iihbond.weight() <<
171  " good? " << hbond_set.allow_hbond( iihbond ) <<
172  /*" f1: " << iihbond.deriv().first.x() <<
173  " " << iihbond.deriv().first.y() <<
174  " " << iihbond.deriv().first.z() <<
175  " f2: " << iihbond.deriv().second.x() <<
176  " " << iihbond.deriv().second.y() <<
177  " " << iihbond.deriv().second.z() <<*/ std::endl;
178  }
179  }
180 
181  /*if ( check_nblist ) {
182  using namespace id;
183  using namespace etable;
184 
185  typedef utility::pointer::owning_ptr< EtableEnergy > EtableEnergyOP;
186  EtableEnergyOP etabE =
187  dynamic_cast< EtableEnergy * > (
188  (ScoringManager::get_instance()->energy_method( fa_atr, score_function_.energy_method_options() )).get() );
189  EnergyMap const & w( score_function_.weights() );
190 
191  assert( pose1.energies().minimization_graph() );
192  MinimizationGraphCOP g1 = pose1.energies().minimization_graph();
193  assert( pose2.energies().minimization_graph() );
194  MinimizationGraphCOP g2 = pose2.energies().minimization_graph();
195  for ( graph::Graph::EdgeListConstIter
196  me1_iter = g1->const_edge_list_begin(), me1_iter_end = g1->const_edge_list_end(),
197  me2_iter = g2->const_edge_list_begin(), me2_iter_end = g2->const_edge_list_end();
198  me1_iter != me1_iter_end && me2_iter != me2_iter_end; ++me1_iter, ++me2_iter ) {
199  Size const r1( (*me1_iter)->get_first_node_ind() );
200  Size const r2( (*me1_iter)->get_second_node_ind() );
201  assert( r1 == (*me2_iter)->get_first_node_ind() );
202  assert( r2 == (*me2_iter)->get_second_node_ind() );
203  MinimizationEdge const & me1( static_cast< MinimizationEdge const & > ( ** me1_iter ));
204  MinimizationEdge const & me2( static_cast< MinimizationEdge const & > ( ** me2_iter ));
205 
206  ResiduePairNeighborList const & nbl1( static_cast< ResiduePairNeighborList const & >
207  ( * me1.res_pair_min_data().get_data( etab_pair_nblist )() ) );
208  ResiduePairNeighborList const & nbl2( static_cast< ResiduePairNeighborList const & >
209  ( * me2.res_pair_min_data().get_data( etab_pair_nblist )() ) );
210 
211  for ( Size ii = 1; ii <= pose1.residue( r1 ).natoms(); ++ii ) {
212  for ( Size jj = 1; jj <= pose1.residue( r2 ).natoms(); ++jj ) {
213  //if ( nbl1.r1_narrow_neighbors(ii)[ jj ].in_list() != nbl2.r1_narrow_neighbors(ii)[ jj ].in_list() ){
214  Real d2_1;
215  Real d2_2;
216  Real atrE1( 0.0 ), repE1( 0.0 ), solE1( 0.0 ), bogus1( 0.0 );
217  etabE->atom_pair_energy( pose1.residue( r1 ).atom( ii ), pose1.residue( r2 ).atom( jj ),
218  nbl1.r1_narrow_neighbors(ii)[ jj ].data().weight(), atrE1, repE1, solE1, bogus1, d2_1 );
219 
220  Real atrE2( 0.0 ), repE2( 0.0 ), solE2( 0.0 ), bogus2( 0.0 );
221  etabE->atom_pair_energy( pose2.residue( r1 ).atom( ii ), pose2.residue( r2 ).atom( jj ),
222  nbl2.r1_narrow_neighbors(ii)[ jj ].data().weight(), atrE2, repE2, solE2, bogus2, d2_2 );
223  Real e1 = w[ fa_atr ] * atrE1 + w[ fa_rep ] * repE1 + w[ fa_sol ] * solE1;
224  Real e2 = w[ fa_atr ] * atrE2 + w[ fa_rep ] * repE2 + w[ fa_sol ] * solE2;
225  if ( std::abs( e1 - e2 ) > 1e-4 ) {
226 
227  std::cout << " nblist change: " << r1 << " " << ii << " and " << r2 << " " << jj << " d1: " <<
228  std::sqrt( d2_1 ) << " d2: " << std::sqrt( d2_2 ) << " repE1: " <<
229  repE1 << " repE2: " << repE2 << " e1: " << e1 << " e2: " << e2 << std::endl;
230  }
231  }
232  }
233  }
234  }*/
235 }
236 
238  pose::Pose & pose_in,
239  MinimizerMap & min_map_in,
240  scoring::ScoreFunction const & scorefxn_in,
241  bool const deriv_check_in,
242  bool const deriv_check_verbose_in
243 ) :
244  pose_( pose_in ),
245  min_map_( min_map_in ),
246  score_function_( scorefxn_in ),
247  deriv_check_( deriv_check_in ),
248  deriv_check_verbose_( deriv_check_verbose_in ),
250 {}
251 
253 
255  return min_map_;
256 }
257 
259  return score_function_;
260 }
261 
262 } // namespace optimization
263 } // namespace core
264