IMP logo
IMP Reference Guide  develop.94629e1a1f,2022/05/26
The Integrative Modeling Platform
Go to the documentation of this file.
1 /**
2  * \file IMP/core/TypedPairScore.h
3  * \brief Delegate to another PairScore depending on particle types.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  */
11 #include <IMP/core/core_config.h>
12 #include <IMP/PairScore.h>
13 #include <IMP/pair_macros.h>
17 //! Delegate to another PairScore depending on particle types.
18 /** Each particle is given an integer type, which is used at evaluate time
19  to select a PairScore to use for a given pair of particles. (The ordering
20  of the particles does not matter.) After creating the object, call
21  set_pair_score() to tell it the PairScore objects you want to use for each
22  pair of particle types.
24  \note For efficiency, you should probably use the
25  container::PredicatePairRestraint instead.
26  */
27 class IMPCOREEXPORT TypedPairScore : public PairScore {
28  // The key used for the particle types.
29  IntKey typekey_;
30  typedef std::map<std::pair<Int, Int>, IMP::PointerMember<PairScore> >
31  ScoreMap;
32  // Mapping from particle types to PairScores.
33  ScoreMap score_map_;
34  // Whether to throw an exception for invalid particle types.
35  bool allow_invalid_types_;
37  PairScore *get_pair_score(const ParticlePair &pp) const;
39  public:
40  //! Constructor.
41  /** \param[in] typekey The IntKey used to denote the type of each particle.
42  \param[in] allow_invalid_types Desired behavior for particle pairs that
43  have types not covered by set_pair_score(). If true, the score
44  returned for these pairs is zero. If false, evaluate() raises
45  a ValueException.
46  */
47  TypedPairScore(IntKey typekey, bool allow_invalid_types = true);
49  //! Set the particle's type.
50  /** At evaluate time, if a given particle does not have the typekey
51  attribute, this method is called. Here it does nothing, but it could
52  be overridden in a subclass to automatically set the type of a particle,
53  e.g. from other particle attributes such as an atom or residue name.
54  */
55  virtual void set_particle_type(Particle *) const {}
57  //! Set the PairScore to delegate to for a given pair of particle types.
58  /** \param[in] ps PairScore to use at evaluate time.
59  \param[in] atype First particle type.
60  \param[in] btype Second particle type.
61  */
62  void set_pair_score(PairScore *ps, Int atype, Int btype) {
63  score_map_
64  [std::pair<Int, Int>(std::min(atype, btype), std::max(atype, btype))] =
66  }
68  virtual double evaluate_index(Model *m,
69  const ParticleIndexPair &p,
70  DerivativeAccumulator *da) const override;
72  Model *m, const ParticleIndexes &pis) const override;
75  ;
76 };
Abstract class for scoring object(s) of type ParticleIndexPair.
Definition: PairScore.h:42
Macros for various classes.
Definition: pair_macros.h:25
A class to store an fixed array of same-typed values.
Definition: Array.h:33
virtual void set_particle_type(Particle *) const
Set the particle's type.
Define the basic things needed by any Object.
Definition: object_macros.h:25
void set_pair_score(PairScore *ps, Int atype, Int btype)
Set the PairScore to delegate to for a given pair of particle types.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:73
Define PairScore.
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const =0
Overload this method to specify the inputs.
Class to handle individual particles of a Model object.
Definition: Particle.h:41
int Int
Basic integer value.
Definition: types.h:34
Delegate to another PairScore depending on particle types.
virtual double evaluate_index(Model *m, const ParticleIndexPair &vt, DerivativeAccumulator *da) const =0
Compute the score and the derivative if needed.
Class for adding derivatives from restraints to the model.