Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Energies.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/Energies.hh
11 /// @brief Energies class
12 /// @author Phil Bradley
13 
14 
15 #ifndef INCLUDED_core_scoring_Energies_hh
16 #define INCLUDED_core_scoring_Energies_hh
17 
18 // Package Headers
20 #include <basic/datacache/BasicDataCache.hh>
21 // AUTO-REMOVED #include <basic/Tracer.fwd.hh>
22 
26 // AUTO-REMOVED #include <core/scoring/EnergyGraph.hh>
36 
37 #ifdef WIN32 //VC++ needs full class declaration
38 #include <core/scoring/ContextGraph.hh> // WIN32 INCLUDE
39 #include <core/scoring/LREnergyContainer.hh> // WIN32 INCLUDE
40 #endif
41 
42 // Project headers
43 #include <core/graph/Graph.fwd.hh>
44 #include <core/pose/Pose.fwd.hh>
45 
47 
48 #include <core/id/AtomID.fwd.hh>
51 
52 // Utility Headers
53 #include <utility/PyAssert.hh>
54 #include <utility/pointer/ReferenceCount.hh>
55 
56 // STL Headers
57 #include <map>
58 
59 #include <utility/vector1.hh>
60 #include <ObjexxFCL/FArray1D.hh>
61 
62 //Auto Headers
64 namespace core {
65 namespace scoring {
66 
67 
68  /// A cached energies object
69 
70  /**
71  @li Stores total, residue, and residue-pair energies, as well as
72  residue neighbor information.
73 
74  @li Meant to replace fullatom_energies:: namespace.
75 
76  @li Stores residue neighbor information as well as cached residue
77  pair energies in O(N) space using a graph.
78 
79  @li Also stores a DomainMap object which is used during scoring
80  to know which rsd pairs have changed relative orientation and
81  which residues have changed internally
82 
83  @note We distinguish between two kinds of per-residue (1D) energy:
84  onebody residue energies and twobody residue energies. Onebody
85  residue energies are things like dunbrack, intrares, Paa, which
86  depend only on the state of the residue in question. Twobody
87  residue energies (like the residue atr energy) are summations
88  of twobody interactions involving a single residue. The onebody
89  energies can be reused at positions whose internal phi/psi/chi
90  conformation hasn't changed. Twobody residue energies, on the other
91  hand must be invalidated if the structure has changed at all.
92  **/
93 
95 {
96 
97 public:
102  typedef basic::datacache::BasicDataCache BasicDataCache;
103 
104  //typedef scoring::NeighborEnergies NeighborEnergies;
105 
109 
111 
113 
114 public:
115  /// ctor -- ensure correct initial state
116  Energies();
117 
118  // Explicit copy ctor to avoid #include .hh's
119  Energies( Energies const & src );
120 
121  // Explicit assignmnet operator to avoid #include .hh's
122  virtual Energies const & operator = ( Energies const & rhs );
123 
124  ///@details determine whether my type is the same as another Conformation's
125  virtual
126  bool
127  same_type_as_me( Energies const & other, bool recurse = true ) const;
128 
129  /// dtor
130  virtual
131  ~Energies();
132 
133  virtual
134  EnergiesOP
135  clone() const;
136 
137 
138  /// @brief Pose must claim its Energies object; this should happen once,
139  /// at the time the Pose is allocated. Future copying of the Energies object
140  /// will not change ownership. The purpose of ownership is to allow lazy context-graph
141  /// creation. When context graphs are requested that have not been created, they
142  /// must be created and their edges must be updated to represent the current conformation
143  /// of the pose.
144  void set_owner( pose::PoseAP owner );
145 
146  //////////////////////////
147  /// energy access
148  //////////////////////////
149 
150  /// @brief Returns the total score
151  ///
152  /// example(s):
153  /// pose.energies().total_energy()
154  /// See also:
155  /// Energies
156  /// Energies.residue_total_energy
157  /// Energies.residue_total_energies
158  /// Pose
159  /// ScoreFunction
160  /// ScoreFunction.show
161  /// create_score_function
162  Real
163  total_energy() const
164  {
165  return total_energy_;
166  }
167 
168  Real &
170  {
171  return total_energy_;
172  }
173 
174  /// @brief Returns the total_energies EnergyMap after first computing the
175  /// component energies if they are not up-to-date
176  EnergyMap const &
177  total_energies() const;
178 
179  /// @brief Returns a non-const reference to the total_energies EnergyMap
180  /// so that external sources may append additional information to the Energies
181  /// object. This is primarily useful for outputting score data with structures
182  /// when those terms are not part of the energy function.
183  /// This function will update the component energies if they are not up-to-date.
184  EnergyMap &
185  total_energies();
186 
187  /// @brief Read access to the components of the one-body energies.
188  EnergyMap const &
189  onebody_energies( int const seqpos ) const
190  {
191  return onebody_energies_[ seqpos ];
192  }
193 
194  /// @brief Write access to the components of the one-body energies.
195  /// This access is intended only for the ScoreFunction.
196  EnergyMap &
197  onebody_energies( int const seqpos )
198  {
199  return onebody_energies_[ seqpos ];
200  }
201 
202  /// @brief Read access to the components of the "finalized" energies;
203  /// These will include any score component calculated in the finalize
204  /// phase of score function evaluation. These energies are copied
205  /// between Energies objects, and are not evaluated during the component-
206  /// energy update.
207  EnergyMap const &
209  return finalized_energies_;
210  }
211 
212  /// @brief Write access to the components of the "finalized" energies.
213  /// This access is intended only for the ScoreFunction.
214  EnergyMap &
216  return finalized_energies_;
217  }
218 
219  /// @brief Returns the unweighted total_energies EnergyMap for
220  /// Residue <seqpos>
221  /// @note: only evaluated when requested (lazy!), inaccessible during
222  /// minimization, EnergyMap is an EMapVector
223  ///
224  /// example(s):
225  /// r3 = pose.energies().residue_total_energies(3)
226  /// r3[fa_sol]
227  /// See also:
228  /// Energies
229  /// Energies.residue_total_energy
230  /// Pose
231  /// ScoreFunction
232  /// ScoreFunction.show
233  /// create_score_function
234  /// EMapVector
235  EnergyMap const &
236  residue_total_energies( int const seqpos ) const
237  {
238  assert( !use_nblist() && energies_updated() );
239 // PyAssert( (!use_nblist()) && (energies_updated()), "Energies::residue_total_energies(): the Energies object isn't ready! Has it been scored?" );
240  PyAssert( (seqpos>0) && (seqpos<=int(size())), "Energies::residue_total_energies( int const seqpos ): variable seqpos is out of range!" );
242  return residue_total_energies_[ seqpos ];
243  }
244 
245  /// @brief Returns the weighted total energy of residue <seqpos>
246  ///
247  /// example(s):
248  /// pose.energies().residue_total_energy(3)
249  /// See also:
250  /// Energies
251  /// Energies.residue_total_energies
252  /// Pose
253  /// ScoreFunction
254  /// ScoreFunction.show
255  /// create_score_function
256  Real
257  residue_total_energy( int const seqpos ) const
258  {
259  assert( !use_nblist() && energies_updated() );
260 // PyAssert( (!use_nblist()) && (energies_updated()), "Energies::residue_total_energy(): the Energies object isn't ready! Has it been scored?" );
261  PyAssert( (seqpos>0) && (seqpos<=int(size())), "Energies::residue_total_energy( int const seqpos ): variable seqpos is out of range!" );
263  return residue_total_energy_[ seqpos ];
264  }
265 
266  /// @brief Read access to the EnergyGraph.
267  EnergyGraph const &
268  energy_graph() const;
269 
270  /// @brief Write access to the EnergyGraph.
271  EnergyGraph &
272  energy_graph();
273 
274  /// @brief get the graph encoding # neighbors within 10 Angstroms
275  /// If the graph has not been requested up until this point, then it will
276  /// be instantiated and filled. If the pose has changed size since the last
277  /// score function evaluation (or if the pose has never been scored) this
278  /// function will exit.
279  TenANeighborGraph const &
280  tenA_neighbor_graph() const;
281 
282  /// @brief Write access to the graph encoding # neighbors within 10 Angstroms
283  /// If the graph has not been requested up until this point, then it will
284  /// be instantiated and filled. If the pose has changed size since the last
285  /// score function evaluation (or if the pose has never been scored) this
286  /// function will exit.
289 
290  /// @brief get the graph encoding # neighbors within 12 Angstroms
292  twelveA_neighbor_graph() const;
293 
296 
297 
300 
303 
304  /// @brief Allows non-scorefunction components of Rosetta to impose requirements on
305  /// the context graphs that this object maintains.
306  void
308 
309  /// @brief Returns an EnergyMap of the ScoreFunction weights from the last
310  /// scoring
311  ///
312  /// example(s):
313  /// we = pose.energies().weights()
314  /// we[fa_atr]
315  /// See also:
316  /// Energies
317  /// Energies.residue_total_energies
318  /// Energies.residue_total_energy
319  /// Pose
320  /// ScoreFunction
321  /// ScoreFunction.show
322  /// create_score_function
323  /// EMapVector
324  EnergyMap weights() const;
325 
326  // tex 10/31/08 - added this method for access by the silent-file
327  // classes, so that we can initialize weights from silent-files.
328  /// @brief Setter for the weights in this Energies object.
329  void weights( EnergyMap new_weights );
330 
331  /// @brief update the residue neighbors
332  void
334  DomainMap const & domain_map_in,
335  pose::Pose const & pose
336  );
337 
338  ///
339  bool
341  {
342  return ( graph_state_ == GOOD );
343  }
344 
345  /// @brief Returns true if the score is up-to-date
346  ///
347  /// example(s):
348  /// pose.energies().energies_updated()
349  /// See also:
350  /// Energies
351  /// Energies.residue_total_energy
352  /// Energies.residue_total_energies
353  /// Pose
354  /// ScoreFunction
355  /// ScoreFunction.show
356  /// create_score_function
357  bool
359  {
360  return ( energy_state_ == GOOD );
361  }
362 
363 
364  /// @brief check if rsd has changed internal conformation, necessitating, recomputation of 1d energies like dun,intra,prob,etc
365  //
366  bool
367  res_moved( int const seqpos ) const;
368 
369  void
370  reset_res_moved( int const seqpos );
371 
372  /// @brief for debugging -- forget all stored energies, does not change size
373  void
374  clear_energies();
375 
376 
377  /// @brief Returns the number of held residue energies
378  ///
379  /// example(s):
380  /// r3 = pose.energies().residue_total_energies(3)
381  /// r3[fa_sol]
382  /// See also:
383  /// Energies
384  /// Energies.residue_total_energies
385  /// Energies.residue_total_energy
386  /// Pose
387  /// ScoreFunction
388  /// ScoreFunction.show
389  /// create_score_function
390  inline
391  Size
392  size() const
393  {
394  return size_;
395  }
396 
397  /// are we in the midst of a scoring calculation?
398  bool
399  scoring() const
400  {
401  return scoring_;
402  }
403 
404  ///
405  void
406  show( std::ostream & out ) const;
407 
408  ///
409  void
410  show( std::ostream & out, Size res ) const;
411 
412  //wrapper function of energies.show() for Pyrosetta
413  void
414  show() const {show(std::cout);};
415 
416  /// @brief Shows the energy information of residue <seqpos>
417  /// @note: wrapper function of energies.show(Size) for Pyrosetta
418  ///
419  /// example(s):
420  /// pose.energies().show(3)
421  /// See also:
422  /// Energies
423  /// Energies.residue_total_energies
424  /// Energies.residue_total_energy
425  /// Pose
426  /// ScoreFunction
427  /// ScoreFunction.show
428  /// create_score_function
429  void
430  show(Size res) const {show(std::cout, res);};
431 
432  void
433  show_totals( std::ostream & out ) const;
434 
435  void
436  show_total_headers( std::ostream & out ) const;
437 
438  friend std::ostream & operator<<(std::ostream& out, const Energies& e );
439 
440  /// @brief called (eg by pose) to notify us of a change to the structure
441  /**
442  Triggers clearing of the total energies and the twobody rsd energies
443  PHIL -- should also mark the neighbor links as invalid somehow...
444  Called by pose when someone tries to access us, if the Conformation
445  indicates that the structure has moved since the last score evaluation
446 
447  const b/c called inside const access methods
448  **/
449  void
450  structure_has_moved( Size const nres ) const;
451 
452  /// @brief Notification of the start of a scoring calculation.
453 
454  void
456  scoring::ScoreFunction const & sfxn,
457  pose::Pose const & pose // for the nbr calculation
458  );
459 
460  /// @brief signal from the scorefxn that scoring is over
461  void
462  scoring_end(
463  scoring::ScoreFunction const & scorefxn
464  );
465 
466  /// @brief get scorefxn info
469  {
470  return *scorefxn_info_;
471  }
472 
473  /// @brief kill everything (that nobody forgot about)
474  void
475  clear();
476 
477  ///
478  bool
479  use_nblist() const
480  {
481  return use_nblist_;
482  }
483 
484  bool
487  }
488 
491 
493  minimization_graph() const;
494 
495  void
497 
498  ///
499  scoring::NeighborList const &
500  nblist( EnergiesCacheableDataType::Enum const & type ) const;
501 
502  ///
503  void
504  set_nblist(
505  EnergiesCacheableDataType::Enum const & type,
506  scoring::NeighborListOP nblist_in
507  );
508 
509  ///
510  void
512  pose::Pose const & pose,
513  DomainMap const & domain_map_in,
514  bool const use_nblist_auto_update
515  );
516 
517  ///
518  void
519  reset_nblist();
520 
521  /// @brief BasicDataCache indexed by enum in core/scoring/EnergiesCacheableDataType.hh
522  BasicDataCache const &
523  data() const
524  {
525  return data_cache_;
526  }
527 
528  /// @brief BasicDataCache indexed by enum in core/scoring/EnergiesCacheableDataType.hh
531  {
532  return data_cache_;
533  }
534 
535  /// @brief instructs Pose whether the domain map info in the Conformation object should be discarded
536  bool discard_conformation_domain_map() const;
537 
538  /// @brief Return the color assigned to a particular residue (index = pos) as held in the
539  /// domain map describing how this residue has moved with respect to the other residues in the
540  /// pose.
541  ///
542  /// CAUTION new behavior: domain_map may not return 0 for residues that have undergone internal
543  /// degree of freedom changes since the last scoring. Ask the res_moved() method for that information
544  int
545  domain_map( int const pos ) const
546  {
547  require_scoring();
548  return domain_map_(pos);
549  }
550 
551  /// @brief Read access for the domain map.
552  DomainMap const &
553  domain_map() const
554  {
555  require_scoring();
556  return domain_map_;
557  }
558 
559  void
561 
564 
567 
568 protected:
569 
570  /// @brief get access to the point graph. For derived classes
572  point_graph();
573 
574 /// @brief Write access to the EnergyGraph.
575  EnergyGraph &
577 
579  context_graphs() const;
580 
582  required_context_graphs() const;
583 
584  Real
586 
587  void
589 
591  owner() const;
592 
593  /// @brief Read access for the domain map. There is only one
594  /// difference to the public interface to the domain_map_:
595  /// we don't reequire that scoring is performed. This function
596  /// is used during minimization by derived classes of the minimizer
597  int
598  domain_map_during_minimization( int const pos ) const
599  {
600  return domain_map_(pos);
601  }
602 
603 
604  /////////////////////////////////////////////////////////////////////////////
605  /////////////////////////////////////////////////////////////////////////////
606  // private methods
607  /////////////////////////////////////////////////////////////////////////////
608  /////////////////////////////////////////////////////////////////////////////
609 
610 private:
611 
612  /// @brief should push this into energygraph once it stabilizes
613  enum EnergyState {
615  };
616 
617  /// @brief Internal method to resize data that is dependent on the number
618  /// of residues in the Pose.
619  void
620  set_size( Size const size_in );
621 
622 
623  /// @brief will die if not in the middle of an energy evaluation
624  inline
625  void
626  require_scoring() const;
627 
628  /// @brief Lazy component evaluation for residue pair energies;
629  void
631 
632  /// @brief Lazy update of residue total component energies; only compute them when they are requested.
633  /// Forces an update of the energy_graph_.
634  void
636 
637  /// @brief Lazy update of the per-residue total weighted energy. Only computed when requested.
638  /// Does not force an update of the energy_graph_
639  void
641 
642 
643  /// @brief sum the residue energies to this type to get a total energy
644  void
645  accumulate_residue_energies( ScoreType const & type ) const;
646 
647  /// @brief update the context graphs and the energy graph according to a new
648  /// score function type
649  void
651 
652  /// @brief Save state information from the domain map and wipe dirty energies
653  void
655 
656  /// @brief Delete edges from energy_graph and tenA_neighbor_graph according to
657  /// the movement history described by the domain map.
659 
660  //// @brief Delete edges for a particular graph using the domain map data
662 
663  /// @brief Reset the "already computed" status for pairs of residues represented
664  /// in a particular long-range energy container using the domain map.
666 
667  /// @brief Detect the new set of neighbors given the structure of the Pose
668  /// (find_neighbors()) and add new edges to the neighbor graphs so that the
669  /// status of the neighbor graphs is current wrt the current conformation.
670  virtual
671  void update_neighbor_links( pose::Pose const & pose );
672 
673  /// @brief Create a point graph representing the xyz coordinates of the "neighbor
674  /// atoms." (Each residue_type indicates one of its atoms to be used for
675  /// neighbor detection -- e.g. CB for most amino acids, CA for GLY.) The point
676  /// graph will then be used in a call to find_neighbors to add upper-edges to
677  /// neighboring residues.
678  virtual
679  void fill_point_graph( pose::Pose const & pose, conformation::PointGraphOP pg ) const;
680 
681  /// @brief During Energies copy-ctor and assignment operator, copy over the
682  /// neighbor lists objects (clone them).
683  void copy_nblists( Energies const & other );
684 
685  /// @brief During Energies copy-ctor and assignment operator, copy over the
686  /// context graphs and the historical information about who required them
687  /// (a score term, e.g. the HBondEnergy, or some other function, e.g. pack_rotamers)
688  void copy_context_graphs( Energies const & other );
689 
690  //// @brief During Energies copy-ctor and assignment operator, copy over
691  /// the long-range energy containers from the source Energies object.
692  void copy_lr_energy_containers( Energies const & other );
693 
694  /// @brief Internal method that handles the bookkeeping for intializing a
695  /// new context graph, either requested by a scoring term (external = false)
696  /// or some other function (external = true ).
697  virtual
698  void
699  require_context_graph_( scoring::ContextGraphType type, bool external ) const;
700 
701 #ifdef USEBOOSTSERIALIZE
702  friend class boost::serialization::access;
703 
704  template<class Archive>
705  void serialize(Archive & ar, const unsigned int version) {
706  ar & scorefxn_weights_;
707  ar & total_energies_;
708  }
709 #endif
710 
711 private:
712 
713  /////////////////////////////////////////////////////////////////////////////
714  /////////////////////////////////////////////////////////////////////////////
715  // data
716  /////////////////////////////////////////////////////////////////////////////
717  /////////////////////////////////////////////////////////////////////////////
718 
719  /// our internal nres
720  /**
721  Used for dimensioning new Energy1D's, eg
722  and for checking the validity of the NeighborEnergies by comparing
723  against their size
724  **/
726 
727  /// In order to do lazy context-graph creation, the Energies object must be able to access
728  /// coordinates stored in the pose. This now means an Energies object cannot
729  /// live independently of a Pose!
731 
733 
734  /// @brief The collection of context graphs used by the context-dependent energy components
735  /// which the Energies object is responsible for maintaining. Context graphs
736  /// are allocated as requested, (e.g. in a call to tenA_neighbor_graph() ), if
737  /// they have not yet been allocated. If a portion of the code requires a context
738  /// graph (e.g the packer), it may call require_context_graph( int cgtype ), which
739  /// will create the context graph immediately
741  /// those required by non-score function entities (e.g. the packer).
743  /// OR of the sfxn required context graphs and the externally required ones.
745  /// The maximum neighbor cutoff for all required context graphs;
747 
749 
750  /// atom-atom neighborlists
751  std::map< EnergiesCacheableDataType::Enum, scoring::NeighborListOP > nblist_;
755 
756  /// cached onebody energies -- expensive -- shortly to be replaced by an FArray2D which can be smarly indexed into
757  /// to access only the active one-body energy terms.
759 
760  /// cached energy component totals -- summed twobody and one body interactions for each residue
763  /// cached energy totals -- only the weighted sum, no components.
765  mutable utility::vector1< Real > residue_total_energy_; // The weighted sum of all interaction energies for each residue
766 
767  /// cached total energies
770 
771  /// Energies computed during the finalize() stage of scoring.
773 
774  /// info about last score evaluation
776 
777  /// ScoreFunction weights from the last scoring call
779 
780  /// Domain map, stores information about the rigid-bodies whose internal conformation is unchanged since
781  /// the last score calc'n
782  /**
783  If domain_map_(i) == 0 then residue i has changed internal conformation. -- no longer true
784  If domain_map_(i) > 0 and domain_map_(i) == domain_map_(j), then residues
785  i and j are unchanged wrt one another
786  **/
788 
789  /// are we within a scoring evaluation?
790  bool scoring_;
791 
794 
795  /// @brief BasicDataCache indexed by enum in core/scoring/EnergiesCacheableDataType.hh
796  /// @warning DataCache must always be initialized with the number of cacheable
797  /// data types -- see the last enum entry.
799  //std::map< CacheableDataType, CacheableDataOP > cached_data_;
800 
801  /// Keep this guy between score function evaluations to
802  /// avoid the expense of recreating it each time; this data does not
803  /// need to be copied in either the copy-ctor or the assignment operator
804  /// Its purpose is solely to improve performance and the data is used
805  /// only inside the neighbor calculation function call.
807 };
808 
809 
810 inline
811 void
813 {
814  if ( !scoring_ ) {
815  utility_exit_with_message(
816  "Energies:: operation only permitted during scoring." );
817  }
818 }
819 
820 
821 } // namespace scoring
822 } // namespace core
823 
824 
825 #endif // INCLUDED_core_scoring_Energies_HH