IMP logo
IMP Reference Guide  develop.ef7183ce54,2026/10/07
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-2026 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 "ScoringFunction.h"
15 #include "container_macros.h"
16 #include <string>
17 #include <cereal/access.hpp>
18 #include <cereal/types/base_class.hpp>
19 #include <cereal/types/polymorphic.hpp>
20 
21 IMPKERNEL_BEGIN_NAMESPACE
22 
23 //! Object used to hold a set of restraints
24 /** RestraintSets allow one to define a tree of restraints
25  and to weight various restraints. Upon evaluation,
26  all of the restraints in RestraintSets that have been
27  added to the model are evaluated
28  using the provided weight (weights are multiplicative
29  when RestraintSets are nested).
30 
31  If the weight is 0, the restraints are not evaluated.
32 
33  \note Restraints can belong to multiple RestraintSets.
34  The total effect is simply one of adding up the weights.
35 
36  \headerfile RestraintSet.h "IMP/RestraintSet.h"
37  \advanceddoc
38 
39  Talk to Daniel if you want to inherit from RestraintSet.
40 */
41 class IMPKERNELEXPORT RestraintSet : public Restraint {
42  void on_add(Restraint *r);
43  void on_change();
44  static void on_remove(RestraintSet *container, Restraint *r);
45  void show_it(std::ostream &out) const;
46 
47  friend class cereal::access;
48 
49  template<class Archive> void serialize(Archive &ar) {
50  ar(cereal::base_class<Restraint>(this), mutable_access_restraints());
51  }
52 
54 
55  public:
56  //! Create an empty set that is registered with the model
57  RestraintSet(Model *m, double weight,
58  const std::string &name = "RestraintSet %1%");
59  //! Create an empty set that is registered with the model
60  RestraintSet(Model *m, const std::string &name = "RestraintSet %1%");
61  //! Create a set that is registered with the model
62  RestraintSet(const RestraintsTemp &rs, double weight,
63  const std::string &name = "RestraintSet %1%");
64  RestraintSet() { is_aggregate_ = true; }
65 
66  double unprotected_evaluate(DerivativeAccumulator *da) const override;
67 
69  /** @name Methods to control the nested Restraint objects
70 
71  This container manages a set of Restraint objects. To
72  manipulate the stored set use the methods below.
73  */
74  /**@{*/
75  IMP_LIST_ACTION(public, Restraint, Restraints, restraint, restraints,
76  Restraint *, Restraints, on_add(obj), on_change(),
77  if (container) on_remove(container, obj));
78  /**@}*/
79 
80  //! Divide the list of contained restraints into non-sets and sets.
81  /** \return a list of all contained Restraints that are not also
82  RestraintSets, and another list of contained RestraintSets.
83  */
84  std::pair<RestraintsTemp, RestraintSetsTemp> get_non_sets_and_sets() const;
85 
86  public:
87 #ifndef IMP_DOXYGEN
88  ModelObjectsTemp do_get_inputs() const override;
90  double weight = 1.0,
91  double max = std::numeric_limits<double>::max()) const override;
92 #endif
93  double get_last_score() const override;
94 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
95  protected:
96  Restraints do_create_decomposition() const override;
98  void do_add_score_and_derivatives(ScoreAccumulator sa) const override;
99  void do_add_score_and_derivatives_moved(
100  ScoreAccumulator sa,
101  const ParticleIndexes &moved_pis,
102  const ParticleIndexes &reset_pis) const override;
103 
104  public:
105  void clear_moved_cache() override {
107  for (auto it = restraints_begin(); it != restraints_end(); ++it) {
108  (*it)->clear_moved_cache();
109  }
110  }
111 #endif
112 };
113 
114 /** \name Gathering restraints
115  It is sometimes useful to extract all the non-RestraintSet restraints
116  from a hierarchy involving RestraintSets mixed with Restraints.
117 */
118 
119 IMPKERNELEXPORT RestraintsTemp get_restraints(const RestraintsTemp &rs);
120 
121 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
122 namespace {
123 template <class It>
124 void get_restraints_internal(It b, It e, RestraintsTemp &ret) {
125  for (It c = b; c != e; ++c) {
126  Restraint *cur = *c;
127  RestraintSet *rs = dynamic_cast<RestraintSet *>(cur);
128  if (rs) {
129  get_restraints_internal(rs->restraints_begin(), rs->restraints_end(),
130  ret);
131  } else {
132  ret.push_back(cur);
133  }
134  }
135 }
136 }
137 #endif
138 
139 template <class It>
140 inline RestraintsTemp get_restraints(It b, It e) {
141  RestraintsTemp ret;
142  get_restraints_internal(b, e, ret);
143  std::sort(ret.begin(), ret.end());
144  ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
145  return ret;
146 }
147 
148 IMPKERNEL_END_NAMESPACE
149 
150 #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:319
IMP::Vector< IMP::WeakPointer< Restraint > > RestraintsTemp
Definition: base_types.h:120
Macros to define containers of objects.
Object used to hold a set of restraints.
Definition: RestraintSet.h:41
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
virtual Restraints do_create_current_decomposition() const
Definition: Restraint.h:329
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
Class for adding up scores during ScoringFunction evaluation.
virtual double get_last_score() const
Definition: Restraint.h:294
Represents a scoring function on the model.
Represents a scoring function on the model.
Abstract base class for all restraints.
virtual void clear_moved_cache()
Clear any caches used by unprotected_evaluate_moved().
Definition: Restraint.h:144
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:56