IMP logo
IMP Reference Guide  2.16.0
The Integrative Modeling Platform
RestraintSet.h
Go to the documentation of this file.
1 /**
2  * \file IMP/RestraintSet.h
3  * \brief Used to hold a set of related restraints.
4  *
5  * Copyright 2007-2021 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_RESTRAINT_SET_H
10 #define IMPKERNEL_RESTRAINT_SET_H
11 
12 #include <IMP/kernel_config.h>
13 #include "Restraint.h"
14 #include "container_macros.h"
15 #include <string>
16 
17 IMPKERNEL_BEGIN_NAMESPACE
18 
19 //! Object used to hold a set of restraints
20 /** RestraintSets allow one to define a tree of restraints
21  and to weight various restraints. Upon evaluation,
22  all of the restraints in RestraintSets that have been
23  added to the model are evaluated
24  using the provided weight (weights are multiplicative
25  when RestraintSets are nested).
26 
27  If the weight is 0, the restraints are not evaluated.
28 
29  \note Restraints can belong to multiple RestraintSets.
30  The total effect is simply one of adding up the weights.
31 
32  \headerfile RestraintSet.h "IMP/RestraintSet.h"
33  \advanceddoc
34 
35  Talk to Daniel if you want to inherit from RestraintSet.
36 */
37 class IMPKERNELEXPORT RestraintSet : public Restraint {
38  void on_add(Restraint *r);
39  void on_change();
40  static void on_remove(RestraintSet *container, Restraint *r);
41  void show_it(std::ostream &out) const;
42 
43  public:
44  //! Create an empty set that is registered with the model
45  RestraintSet(Model *m, double weight,
46  const std::string &name = "RestraintSet %1%");
47  //! Create an empty set that is registered with the model
48  RestraintSet(Model *m, const std::string &name = "RestraintSet %1%");
49  //! Create a set that is registered with the model
50  RestraintSet(const RestraintsTemp &rs, double weight,
51  const std::string &name = "RestraintSet %1%");
52 
53  double unprotected_evaluate(DerivativeAccumulator *da) const IMP_OVERRIDE;
54 
56  /** @name Methods to control the nested Restraint objects
57 
58  This container manages a set of Restraint objects. To
59  manipulate the stored set use the methods below.
60  */
61  /**@{*/
62  IMP_LIST_ACTION(public, Restraint, Restraints, restraint, restraints,
63  Restraint *, Restraints, on_add(obj), on_change(),
64  if (container) on_remove(container, obj));
65  /**@}*/
66 
67  //! Divide the list of contained restraints into non-sets and sets.
68  /** \return a list of all contained Restraints that are not also
69  RestraintSets, and another list of contained RestraintSets.
70  */
71  std::pair<RestraintsTemp, RestraintSetsTemp> get_non_sets_and_sets() const;
72 
73  public:
74 #ifndef IMP_DOXYGEN
77  double weight = 1.0,
78  double max = std::numeric_limits<double>::max()) const IMP_OVERRIDE;
79 #endif
80  double get_last_score() const IMP_OVERRIDE;
81  double get_last_last_score() const IMP_OVERRIDE;
82 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
83  protected:
84  Restraints do_create_decomposition() const IMP_OVERRIDE;
85  Restraints do_create_current_decomposition() const IMP_OVERRIDE;
86  void do_add_score_and_derivatives(ScoreAccumulator sa) const IMP_OVERRIDE;
87  void do_add_score_and_derivatives_moved(
89  const ParticleIndexes &moved_pis,
90  const ParticleIndexes &reset_pis) const IMP_OVERRIDE;
91 #endif
92 };
93 
94 /** \name Gathering restraints
95  It is sometimes useful to extract all the non-RestraintSet restraints
96  from a hierarchy involving RestraintSets mixed with Restraints.
97 */
98 
99 IMPKERNELEXPORT RestraintsTemp get_restraints(const RestraintsTemp &rs);
100 
101 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
102 namespace {
103 template <class It>
104 void get_restraints_internal(It b, It e, RestraintsTemp &ret) {
105  for (It c = b; c != e; ++c) {
106  Restraint *cur = *c;
107  RestraintSet *rs = dynamic_cast<RestraintSet *>(cur);
108  if (rs) {
109  get_restraints_internal(rs->restraints_begin(), rs->restraints_end(),
110  ret);
111  } else {
112  ret.push_back(cur);
113  }
114  }
115 }
116 }
117 #endif
118 
119 template <class It>
120 inline RestraintsTemp get_restraints(It b, It e) {
121  RestraintsTemp ret;
122  get_restraints_internal(b, e, ret);
123  std::sort(ret.begin(), ret.end());
124  ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
125  return ret;
126 }
127 
128 IMPKERNEL_END_NAMESPACE
129 
130 #endif /* IMPKERNEL_RESTRAINT_SET_H */
RestraintsTemp get_restraints(const Subset &s, const ParticleStatesTable *pst, const DependencyGraph &dg, RestraintSet *rs)
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
virtual Restraints do_create_decomposition() const
Definition: Restraint.h:297
IMP::Vector< IMP::WeakPointer< Restraint > > RestraintsTemp
Definition: base_types.h:87
Macros to define containers of objects.
ScoringFunction * create_scoring_function(RestraintType *rs, double weight=1.0, double max=NO_MAX, std::string name=std::string())
Create a ScoringFunction on a single restraint.
Definition: generic.h:23
Object used to hold a set of restraints.
Definition: RestraintSet.h:37
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:73
Class for adding up scores during ScoringFunction evaluation.
virtual double get_last_score() const
Definition: Restraint.h:278
Represents a scoring function on the model.
Abstract base class for all restraints.
virtual ModelObjectsTemp do_get_inputs() const =0
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.
Class for adding derivatives from restraints to the model.
A restraint is a term in an IMP ScoringFunction.
Definition: Restraint.h:54