/
RestraintSet.h
142 lines (119 loc) · 4.51 KB
/
RestraintSet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* \file IMP/RestraintSet.h
* \brief Used to hold a set of related restraints.
*
* Copyright 2007-2022 IMP Inventors. All rights reserved.
*
*/
#ifndef IMPKERNEL_RESTRAINT_SET_H
#define IMPKERNEL_RESTRAINT_SET_H
#include <IMP/kernel_config.h>
#include "Restraint.h"
#include "ScoringFunction.h"
#include "container_macros.h"
#include <string>
#include <cereal/access.hpp>
#include <cereal/types/base_class.hpp>
#include <cereal/types/polymorphic.hpp>
IMPKERNEL_BEGIN_NAMESPACE
//! Object used to hold a set of restraints
/** RestraintSets allow one to define a tree of restraints
and to weight various restraints. Upon evaluation,
all of the restraints in RestraintSets that have been
added to the model are evaluated
using the provided weight (weights are multiplicative
when RestraintSets are nested).
If the weight is 0, the restraints are not evaluated.
\note Restraints can belong to multiple RestraintSets.
The total effect is simply one of adding up the weights.
\headerfile RestraintSet.h "IMP/RestraintSet.h"
\advanceddoc
Talk to Daniel if you want to inherit from RestraintSet.
*/
class IMPKERNELEXPORT RestraintSet : public Restraint {
void on_add(Restraint *r);
void on_change();
static void on_remove(RestraintSet *container, Restraint *r);
void show_it(std::ostream &out) const;
friend class cereal::access;
template<class Archive> void serialize(Archive &ar) {
ar(cereal::base_class<Restraint>(this), mutable_access_restraints());
}
IMP_OBJECT_SERIALIZE_DECL(RestraintSet);
public:
//! Create an empty set that is registered with the model
RestraintSet(Model *m, double weight,
const std::string &name = "RestraintSet %1%");
//! Create an empty set that is registered with the model
RestraintSet(Model *m, const std::string &name = "RestraintSet %1%");
//! Create a set that is registered with the model
RestraintSet(const RestraintsTemp &rs, double weight,
const std::string &name = "RestraintSet %1%");
RestraintSet() {}
double unprotected_evaluate(DerivativeAccumulator *da) const override;
IMP_OBJECT_METHODS(RestraintSet);
/** @name Methods to control the nested Restraint objects
This container manages a set of Restraint objects. To
manipulate the stored set use the methods below.
*/
/**@{*/
IMP_LIST_ACTION(public, Restraint, Restraints, restraint, restraints,
Restraint *, Restraints, on_add(obj), on_change(),
if (container) on_remove(container, obj));
/**@}*/
//! Divide the list of contained restraints into non-sets and sets.
/** \return a list of all contained Restraints that are not also
RestraintSets, and another list of contained RestraintSets.
*/
std::pair<RestraintsTemp, RestraintSetsTemp> get_non_sets_and_sets() const;
public:
#ifndef IMP_DOXYGEN
ModelObjectsTemp do_get_inputs() const override;
ScoringFunction *create_scoring_function(
double weight = 1.0,
double max = std::numeric_limits<double>::max()) const override;
#endif
double get_last_score() const override;
#if !defined(IMP_DOXYGEN) && !defined(SWIG)
protected:
Restraints do_create_decomposition() const override;
Restraints do_create_current_decomposition() const override;
void do_add_score_and_derivatives(ScoreAccumulator sa) const override;
void do_add_score_and_derivatives_moved(
ScoreAccumulator sa,
const ParticleIndexes &moved_pis,
const ParticleIndexes &reset_pis) const override;
#endif
};
/** \name Gathering restraints
It is sometimes useful to extract all the non-RestraintSet restraints
from a hierarchy involving RestraintSets mixed with Restraints.
*/
IMPKERNELEXPORT RestraintsTemp get_restraints(const RestraintsTemp &rs);
#if !defined(IMP_DOXYGEN) && !defined(SWIG)
namespace {
template <class It>
void get_restraints_internal(It b, It e, RestraintsTemp &ret) {
for (It c = b; c != e; ++c) {
Restraint *cur = *c;
RestraintSet *rs = dynamic_cast<RestraintSet *>(cur);
if (rs) {
get_restraints_internal(rs->restraints_begin(), rs->restraints_end(),
ret);
} else {
ret.push_back(cur);
}
}
}
}
#endif
template <class It>
inline RestraintsTemp get_restraints(It b, It e) {
RestraintsTemp ret;
get_restraints_internal(b, e, ret);
std::sort(ret.begin(), ret.end());
ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
return ret;
}
IMPKERNEL_END_NAMESPACE
#endif /* IMPKERNEL_RESTRAINT_SET_H */