faunus
sasa.h
1 #pragma once
2 
3 #ifndef FAUNUS_SASA_H
4 #define FAUNUS_SASA_H
5 
6 #include "celllistimpl.h"
7 #include "particle.h"
8 #include <range/v3/numeric.hpp>
9 #include <numbers>
10 
11 namespace Faunus {
12 
13 class Space;
14 
15 namespace Geometry {
16 class Chameleon;
17 }
18 
19 struct Change;
20 
21 namespace SASA {
22 
23 using index_type = size_t;
24 using GeometryType = Geometry::Chameleon;
25 
31 class SASABase
32 {
33  public:
34  struct Neighbours
35  {
36  std::vector<index_type> indices;
38  index_type index;
39  };
40 
41  bool needs_syncing = false;
45 
46  protected:
47  double probe_radius = 1.4;
48  std::vector<double> areas;
49  std::vector<double> sasa_radii;
50  int slices_per_atom = 20;
51  const double two_pi = 2.0 * std::numbers::pi;
52  const Particle* first_particle;
53 
58  [[nodiscard]] inline index_type indexOf(const Particle& particle) const
59  {
60  return static_cast<index_type>(std::addressof(particle) - first_particle);
61  }
62 
63  [[nodiscard]] double calcSASAOfParticle(const Neighbours& neighbour) const;
64  double exposedArcLength(std::vector<std::pair<double, double>>& arcs) const;
65 
66  public:
67  [[nodiscard]] double calcSASAOfParticle(const Space& spc, const Particle& particle) const;
68 
77  template <typename TBegin, typename TEnd>
78  double calcSASA(const Space& spc, TBegin begin, TEnd end) const
79  {
80  return ranges::accumulate(begin, end, 0.0, [&spc, this](auto& area, const auto& species) {
81  return area + this->calcSASAOf(spc, species);
82  });
83  }
84 
85  template <typename TSpecies> double calcSASAOf(const Space& spc, const TSpecies& species) const;
86 
87  void updateSASA(const std::vector<SASABase::Neighbours>& neighbours_data,
88  const std::vector<index_type>& target_indices);
89 
90  virtual void init(const Space& spc) = 0;
91  [[nodiscard]] virtual std::vector<SASABase::Neighbours>
92  calcNeighbourData(const Space& spc, const std::vector<index_type>& target_indices) const = 0;
93  [[nodiscard]] virtual SASABase::Neighbours
94  calcNeighbourDataOfParticle(const Space& spc, index_type target_index) const = 0;
95  virtual void update(const Space& spc, const Change& change) = 0;
96  [[nodiscard]] const std::vector<double>& getAreas() const;
97  SASABase(const Space& spc, double probe_radius, int slices_per_atom);
98  virtual ~SASABase() = default;
99 };
100 
105 class SASA : public SASABase
106 {
107  public:
108  void init(const Space& spc) override;
109  [[nodiscard]] std::vector<SASABase::Neighbours>
110  calcNeighbourData(const Space& spc,
111  const std::vector<index_type>& target_indices) const override;
112  [[nodiscard]] SASA::Neighbours
113  calcNeighbourDataOfParticle(const Space& spc, index_type target_index) const override;
114  void update([[maybe_unused]] const Space& spc, [[maybe_unused]] const Change& change) override;
115  SASA(const Space& spc, double probe_radius, int slices_per_atom);
116  SASA(const json& j, const Space& spc);
117 };
118 
126 template <typename CellList> class SASACellList : public SASABase
127 {
128  private:
129  using CellCoord = typename CellList::Grid::CellCoord;
130  std::unique_ptr<CellList> cell_list;
131  double cell_length;
132  std::vector<CellCoord>
133  cell_offsets;
134 
135  public:
136  SASACellList(const Space& spc, double probe_radius, int slices_per_atom);
137  SASACellList(const json& j, const Space& spc);
138  ~SASACellList() override = default;
139  void init(const Space& spc) override;
140  [[nodiscard]] SASABase::Neighbours
141  calcNeighbourDataOfParticle(const Space& spc, index_type target_index) const override;
142  [[nodiscard]] std::vector<SASABase::Neighbours>
143  calcNeighbourData(const Space& spc,
144  const std::vector<index_type>& target_indices) const override;
145  void update(const Space& spc, const Change& change) override;
146 
147  private:
148  template <typename TBegin, typename TEnd>
149  void createCellList(TBegin begin, TEnd end, const GeometryType& geometry);
150  void updateMatterChange(const Space& spc, const Change& change);
151  void updatePositionsChange(const Space& spc, const Change& change);
152 };
153 
154 using PeriodicGrid = CellList::Grid::Grid3DPeriodic;
155 using FixedGrid = CellList::Grid::Grid3DFixed;
156 
157 template <typename TMember, typename TIndex>
159 template <typename TMember, typename TIndex>
161 
162 template <class TGrid, template <typename, typename> class TContainer = DenseContainer>
164  CellList::CellListType<index_type, TGrid, CellList::CellListBase, TContainer>>;
165 
170 
171 extern template class SASACellList<DensePeriodicCellList>;
172 extern template class SASACellList<DenseFixedCellList>;
173 extern template class SASACellList<SparsePeriodicCellList>;
174 extern template class SASACellList<SparseFixedCellList>;
175 
176 } // namespace SASA
177 } // namespace Faunus
178 
179 #endif // FAUNUS_SASA_H
nlohmann::json json
JSON object.
Definition: json_support.h:10
Container based on a map suitable for system with a non-uniform or low member density.
Definition: celllistimpl.h:521
std::vector< Point > PointVector
Vector of 3D vectors.
Definition: core.h:24
Geometry class for spheres, cylinders, cuboids, hexagonal prism, truncated octahedron, slits.
Definition: geometry.h:342
index_type index
index of particle whose neighbours are in indices
Definition: sasa.h:38
derived class of SASABase which uses O(N^2) neighbour search
Definition: sasa.h:105
index_type indexOf(const Particle &particle) const
first particle in ParticleVector
Definition: sasa.h:58
A mixin using spatial coordinates instead of cell coordinates when inserting or removing a member...
Definition: celllistimpl.h:937
double calcSASA(const Space &spc, TBegin begin, TEnd end) const
calculates total sasa of either particles or groups between given iterators
Definition: sasa.h:78
std::vector< double > sasa_radii
Radii buffer for all particles.
Definition: sasa.h:49
std::vector< index_type > indices
indices of neighbouring particles in ParticleVector
Definition: sasa.h:36
PointVector points
vectors to neighbouring particles
Definition: sasa.h:37
Particle class for storing positions, id, and other properties.
Definition: particle.h:220
Cell list class templates.
Definition: actions.cpp:11
base class for calculating solvent accessible surface areas of target particles derived classes imple...
Definition: sasa.h:31
Specify changes made to a system.
Definition: space.h:29
derived class of SASABase which uses cell lists for neighbour search currently can be used only for g...
Definition: sasa.h:126
Placeholder for atoms and molecules.
Definition: space.h:92
std::vector< double > areas
vector holding SASA area of each atom
Definition: sasa.h:48
Container based on a vector suitable for system with a uniform and high member density.
Definition: celllistimpl.h:450