Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EnergyGraph.hh
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/scoring/EnergyGraph.hh
11 /// @brief Energy graph class declaration
12 /// @author Andrew Leaver-Fay (leaverfa@email.unc.edu)
13 
14 #ifndef INCLUDED_core_scoring_EnergyGraph_hh
15 #define INCLUDED_core_scoring_EnergyGraph_hh
16 
17 // Unit Headers
19 
20 // Project Headers
22 #include <core/graph/Graph.hh>
23 #include <core/graph/ArrayPool.hh>
25 
26 // Utility headers
27 #include <utility/pointer/ReferenceCount.hh>
28 
29 #include <utility/vector1.hh>
30 
31 
32 namespace core {
33 namespace scoring {
34 
35 /// Class EnergyNode holds the result of a domainmap update from the
36 /// Conformation object held by a pose; if the internal degrees of freedom
37 /// for a residue (corresponding to a node in this graph) have changed
38 /// (and are marked with color "0" in the domainmap), then the EnergyNode
39 /// object will hold that information for the ScoringFunction to retrieve
40 class EnergyNode : public graph::Node
41 {
42 public:
44 
45 public:
46  EnergyNode( graph::Graph * owner, Size index );
47  virtual ~EnergyNode();
48  virtual void copy_from( parent const * source );
49 
50  virtual void print() const;
51  virtual Size count_static_memory() const;
52  virtual Size count_dynamic_memory() const;
53 
54  bool moved() const; // getter
55  void moved( bool ); // setter
56 
57 private:
58  bool moved_;
59 
60 };
61 
62 /// Class EnergyEdge holds scores for pair interactions for short-ranged energy
63 /// functions. It also records whether or not it has been scored -- when an
64 /// edge is added to the graph, it sets "energies_computed_" as false, and the
65 /// ScoreFunction class marks edges as having their energies computed once it
66 /// computes them.
67 class EnergyEdge : public graph::Edge
68 {
69 public:
72 public:
73  EnergyEdge( EnergyGraph * owner, Size n1, Size n2 );
74  EnergyEdge( EnergyGraph * owner, EnergyEdge const & example_edge );
75  virtual ~EnergyEdge();
76 
77  /// @brief Copy the data held on the example edge, source.
78  /// The source edge must be castable to class EnergyEdge.
79  virtual void copy_from( parent const * source );
80 
81  /// @brief Store the energies held in the input emap on this edge; only
82  /// those ScoreTypes which are active are stored.
83  inline void store_active_energies( EnergyMap const & emap );
84 
85  /// @brief Store the intersection of the energies held in the input emap on this edge:
86  /// The intersection is between the set of active ScoreTypes and the ScoreTypes given
87  /// in the input "subset" list. subset need not be sorted.
88  inline void store_active_energies( EnergyMap const & emap, ScoreTypes const & subset );
89 
90  /// @brief Load an energy map with the non-zero
91  inline EnergyMap fill_energy_map() const;
92 
93  /// @brief Add the non-zero elements into the energy map
94  inline void add_to_energy_map( EnergyMap & emap ) const;
95 
96  /// @brief Add the non-zero elements into the energy map
97  inline void add_to_energy_map( EnergyMap & emap, ScoreTypes const & subset ) const;
98 
99  /// @brief Read the value stored on this edge for a particular score type.
100  inline Real operator[] ( ScoreType st ) const;
101 
102  /// @brief Compute the weighted energy for the components stored on this edge
103  inline Real dot( EnergyMap const & weights ) const;
104 
105  //EnergyMap & energy_map() { return energy_map_;}
106  //EnergyMap const & energy_map() const { return energy_map_;}
107 
109  void square_distance( DistanceSquared dsqr ) { dsqr_ = dsqr; }
110 
111  void mark_energies_computed();
113 
115 
116  virtual Size count_static_memory() const;
117  virtual Size count_dynamic_memory() const;
118 
119 protected:
120  /// Downcasts
121 
122  inline
123  EnergyGraph const *
124  get_energy_owner() const;
125 
126  inline
127  EnergyGraph *
129 
130 private:
131 
133  DistanceSquared dsqr_; // measured between the nbr atoms
135 };
136 
137 /// @brief Class to hold the component energies between pairs of residues.
138 /// Each node represents a residue in its corresponding structure.
139 /// Each edge in the graph holds a two-body energy map representing the
140 /// unweighted components of the energy function for those terms with non-zero
141 /// weight. The EnergyGraph may be accessed from the pose's Energies object,
142 /// but at a price of an extra score evaluation. This second score evaluation
143 /// may be avoided if you use the ScoreFunction::score_components( pose ) method.
144 class EnergyGraph : public graph::Graph
145 {
146 public:
148 
149 public:
150  virtual ~EnergyGraph();
151 
153  EnergyGraph();
154  EnergyGraph( EnergyGraph const & src );
155 
156  EnergyGraph const & operator = ( EnergyGraph const & rhs );
157 
158  //bool energy_exists( scoring::ScoreType const & type ) const;
159 
161 
162  /// @brief Set the active score types, and return true if the new score types
163  /// are the same as the old score types and the graph state is still good.
164  /// Returns false if the score types have changed, indicating that the graph
165  /// has dropped all of its edges;
166  bool active_score_types( ScoreTypes const & active );
167 
168  /// @brief Add an energy edge to the graph and store the square distance
169  void
170  add_energy_edge( Size index1, Size index2, DistanceSquared dsq );
171 
172  /// @brief Add an energy edge to the graph and set the energies
173  /// for the non-zero-weighted components of the input emap.
174  /// void
175  /// add_energy_edge( Size index1, Size index2, EnergyMap const & emap );
176 
177  inline
178  EnergyNode const *
179  get_energy_node( Size index ) const
180  {
181  return static_cast< EnergyNode const * > ( get_node( index ));
182  }
183 
184  inline
185  EnergyNode *
187  {
188  return static_cast< EnergyNode * > ( get_node( index ));
189  }
190 
191 
193  EnergyEdge const * find_energy_edge( Size n1, Size n2) const;
194 
195  virtual void delete_edge( graph::Edge * edge );
196 
197  /// @brief As an edge is deleted from the graph, it must reliquish hold over its
198  /// array-pool element so that the element may be reused by new edges.
200 
203  return score_type_2_active_;
204  }
205 
206  ScoreTypes const &
208  return active_2b_score_types_;
209  }
210 
211  /// @brief Give non-const access to the array pool -- this function should only
212  /// be called by class EnergyEdge. I wish C++ let me declare this function private
213  /// and that EnergyEdge could be a "friend" of this function.
215 
216 protected:
217  virtual Size count_static_memory() const;
218  virtual Size count_dynamic_memory() const;
219 
220  virtual graph::Node * create_new_node( Size index );
221  virtual graph::Edge * create_new_edge( Size index1, Size index2 );
222  virtual graph::Edge * create_new_edge( graph::Edge const * example_edge );
223 
224 private:
225 
228 
230  ///@brief these are flag values; <0 has a special meaning, so they need to be ints
232 };
233 
234 
235 /// @details Store only the active terms -- active meaning with non-zero weight.
236 inline
238  ScoreTypes const & active( get_energy_owner()->active_2b_score_types() );
239 
240  for ( Size ii = 1, iilag = 0; ii <= active.size(); ++ii, ++iilag ) {
241  array_[ iilag ] = emap[ active[ ii ] ];
242  }
243 
244 }
245 
246 /// @details Subset may specify ScoreTypes that are not active, but only the active
247 /// ScoreTypes have their eneriges stored.
248 inline void
250 {
251  utility::vector1< int > const & st2active( get_energy_owner()->score_type_2_active() );
252 
253  for ( Size ii = 1, iilag = 0; ii <= subset.size(); ++ii, ++iilag ) {
254  if ( st2active[ subset[ ii ]] >= 0 ) {
255  array_[ st2active[ subset[ ii ]] ] = emap[ subset[ ii ] ];
256  }
257  }
258 
259 }
260 
261 
262 
263 /// @details Only load the non-zero terms -- the zero entries are already
264 inline
265 EnergyMap
267 {
268  EnergyMap emap;
269  ScoreTypes const & active( get_energy_owner()->active_2b_score_types() );
270 
271  for ( Size ii = 1, iilag = 0; ii <= active.size(); ++ii, ++iilag ) {
272  emap[ active[ ii ] ] = array_[ iilag ];
273  }
274  return emap;
275 }
276 
277 /// @brief Add the non-zero elements into the energy map
278 inline
280 {
281  ScoreTypes const & active( get_energy_owner()->active_2b_score_types() );
282 
283  for ( Size ii = 1, iilag = 0; ii <= active.size(); ++ii, ++iilag ) {
284  emap[ active[ ii ] ] += array_[ iilag ];
285  }
286 }
287 
288 /// @brief Add the non-zero elements into the energy map
289 inline
290 void EnergyEdge::add_to_energy_map( EnergyMap & emap, ScoreTypes const & subset ) const
291 {
292  utility::vector1< int > const & st2active( get_energy_owner()->score_type_2_active() );
293 
294  for ( Size ii = 1, iilag = 0; ii <= subset.size(); ++ii, ++iilag ) {
295  if ( st2active[ subset[ ii ]] >= 0 ) {
296  emap[ subset[ ii ] ] += array_[ st2active[ subset[ ii ]] ];
297  }
298  }
299 
300 }
301 
302 
303 
304 /// @details The owner stores a map from score types to indices indicating
305 /// score types not represented in the mapping with an index of -1.
306 inline
307 Real
309  int aid = get_energy_owner()->score_type_2_active()[ st ];
310  if ( aid >= 0 ) {
311  return array_[ aid ];
312  } else {
313  return 0;
314  }
315 }
316 
317 /// @brief Compute the weighted energy for the components stored on this edge
318 inline
319 Real
320 EnergyEdge::dot( EnergyMap const & weights ) const
321 {
322  Real weighted_sum( 0.0 );
323  ScoreTypes const & active( get_energy_owner()->active_2b_score_types() );
324 
325  for ( Size ii = 1, iilag = 0; ii <= active.size(); ++ii, ++iilag ) {
326  weighted_sum += weights[ active[ ii ] ] * array_[ iilag ];
327  }
328  return weighted_sum;
329 }
330 
331 
332 EnergyGraph const *
334  return static_cast< EnergyGraph const * > (get_owner());
335 }
336 
337 
338 EnergyGraph *
340  return static_cast< EnergyGraph * > (get_owner());
341 }
342 
343 
344 } //namespace scoring
345 } //namespace core
346 
347 #endif
348