IMP logo
IMP Reference Guide  2.18.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-2022 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 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
75  ModelObjectsTemp do_get_inputs() const override;
77  double weight = 1.0,
78  double max = std::numeric_limits<double>::max()) const override;
79 #endif
80  double get_last_score() const override;
81 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
82  protected:
83  Restraints do_create_decomposition() const override;
85  void do_add_score_and_derivatives(ScoreAccumulator sa) const override;
86  void do_add_score_and_derivatives_moved(
88  const ParticleIndexes &moved_pis,
89  const ParticleIndexes &reset_pis) const override;
90 #endif
91 };
92 
93 /** \name Gathering restraints
94  It is sometimes useful to extract all the non-RestraintSet restraints
95  from a hierarchy involving RestraintSets mixed with Restraints.
96 */
97 
98 IMPKERNELEXPORT RestraintsTemp get_restraints(const RestraintsTemp &rs);
99 
100 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
101 namespace {
102 template <class It>
103 void get_restraints_internal(It b, It e, RestraintsTemp &ret) {
104  for (It c = b; c != e; ++c) {
105  Restraint *cur = *c;
106  RestraintSet *rs = dynamic_cast<RestraintSet *>(cur);
107  if (rs) {
108  get_restraints_internal(rs->restraints_begin(), rs->restraints_end(),
109  ret);
110  } else {
111  ret.push_back(cur);
112  }
113  }
114 }
115 }
116 #endif
117 
118 template <class It>
119 inline RestraintsTemp get_restraints(It b, It e) {
120  RestraintsTemp ret;
121  get_restraints_internal(b, e, ret);
122  std::sort(ret.begin(), ret.end());
123  ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
124  return ret;
125 }
126 
127 IMPKERNEL_END_NAMESPACE
128 
129 #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 ScoringFunction * create_scoring_function(double weight=1.0, double max=NO_MAX) const
Create a scoring function with only this restraint.
virtual double unprotected_evaluate(DerivativeAccumulator *da) const
Return the unweighted score for the restraint.
virtual Restraints do_create_decomposition() const
Definition: Restraint.h:309
IMP::Vector< IMP::WeakPointer< Restraint > > RestraintsTemp
Definition: base_types.h:87
Macros to define containers of objects.
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
virtual Restraints do_create_current_decomposition() const
Definition: Restraint.h:319
Class for adding up scores during ScoringFunction evaluation.
virtual double get_last_score() const
Definition: Restraint.h:287
Represents a scoring function on the model.
Abstract base class for all restraints.
virtual ModelObjectsTemp do_get_inputs() const =0
Class for adding derivatives from restraints to the model.
A restraint is a term in an IMP ScoringFunction.
Definition: Restraint.h:53