IMP logo
IMP Reference Guide  develop.7400db2aee,2024/11/23
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 "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() {}
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 #endif
104 };
105 
106 /** \name Gathering restraints
107  It is sometimes useful to extract all the non-RestraintSet restraints
108  from a hierarchy involving RestraintSets mixed with Restraints.
109 */
110 
111 IMPKERNELEXPORT RestraintsTemp get_restraints(const RestraintsTemp &rs);
112 
113 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
114 namespace {
115 template <class It>
116 void get_restraints_internal(It b, It e, RestraintsTemp &ret) {
117  for (It c = b; c != e; ++c) {
118  Restraint *cur = *c;
119  RestraintSet *rs = dynamic_cast<RestraintSet *>(cur);
120  if (rs) {
121  get_restraints_internal(rs->restraints_begin(), rs->restraints_end(),
122  ret);
123  } else {
124  ret.push_back(cur);
125  }
126  }
127 }
128 }
129 #endif
130 
131 template <class It>
132 inline RestraintsTemp get_restraints(It b, It e) {
133  RestraintsTemp ret;
134  get_restraints_internal(b, e, ret);
135  std::sort(ret.begin(), ret.end());
136  ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
137  return ret;
138 }
139 
140 IMPKERNEL_END_NAMESPACE
141 
142 #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:305
IMP::Vector< IMP::WeakPointer< Restraint > > RestraintsTemp
Definition: base_types.h:104
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:315
#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:283
Represents a scoring function on the model.
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:56