faunus
atomdata.h
1 #pragma once
2 #include "core.h"
3 #include "aux/json_support.h"
4 #include <algorithm>
5 #include <range/v3/range/conversion.hpp>
6 
7 namespace Faunus {
8 
17 {
18  using key_type = std::string;
19  std::map<key_type, double> data;
20  friend void to_json(json&, const InteractionData&);
21 
22  public:
23  bool contains(const key_type& name) const; // like C++20 map::contains
24  double at(const key_type& name) const; // like map::at()
25  double& at(const key_type& name); // like map::at()
26  void insert_or_assign(const key_type& name, double value); // like C++17 map::insert_or_assign
27 };
28 
29 void to_json(json& j, const InteractionData& a);
30 void from_json(const json& j, InteractionData& a);
31 void from_single_use_json(SingleUseJSON& j, InteractionData& a);
32 
37 {
38  protected:
39  friend void from_json(const json&, SpheroCylinderData&);
40  friend void to_json(json&, const SpheroCylinderData&);
41 
42  public:
43  enum class PatchType
44  {
45  None = 0,
46  Full = 1,
47  Capped = 2,
48  Invalid = 3
49  };
50  double chiral_angle = 0.0;
51  double length = 0.0;
52  double patch_angle = 0.0;
53  double patch_angle_switch =
54  0.0;
55  PatchType type = PatchType::None;
56 };
57 
58 void from_json(const json& j, SpheroCylinderData& psc);
59 void to_json(json& j, const SpheroCylinderData& psc);
60 
66 
70 class AtomData
71 { // has to be a class when a constant reference is used
72  public:
73  using index_type = std::size_t;
74 
75  private:
76  index_type _id = 0;
77  friend void to_json(json&, const AtomData&);
78  friend void from_json(const json&, AtomData&);
79 
80  public:
81  std::string name;
82  double charge = 0;
83  double mw = 1;
84  double sigma = 0;
85  double activity = 0;
87  double alphax = 0;
88  std::optional<double> dp = std::nullopt;
89  std::optional<double> dprot = std::nullopt;
90  double tension = 0;
91  double tfe = 0;
92  Point mu = {0, 0, 0};
93  double mulen = 0;
94  bool hydrophobic = false;
95  bool implicit = false;
96  double scattering_f0 = 1.0;
98  interaction;
99  SpheroCylinderData sphero_cylinder;
100 
101  index_type& id();
102  const index_type& id() const;
103 };
104 
105 void to_json(json& j, const AtomData& a);
106 void from_json(const json& j, AtomData& a);
107 
114 void from_json(const json& j, std::vector<AtomData>& atom_vector);
115 
116 extern std::vector<AtomData> atoms;
117 
119 template <typename T>
120 concept RequireNamedElements = requires(T db) {
121  { db.begin() };
122  { db.begin()->name } -> std::convertible_to<std::string>;
123  { std::is_integral_v<typename std::ranges::range_value_t<T>::index_type> };
124 };
125 
134 auto findName(RequireNamedElements auto& range, std::string_view name)
135 {
136  return std::find_if(range.begin(), range.end(), [&](auto& i) { return i.name == name; });
137 }
138 
139 auto findName(const RequireNamedElements auto& range, std::string_view name)
140 {
141  return std::find_if(range.begin(), range.end(), [&](const auto& i) { return i.name == name; });
142 }
143 
148 {
149  explicit UnknownAtomError(std::string_view atom_name);
150 };
151 
161 AtomData& findAtomByName(std::string_view name);
162 
177 template <RequireNamedElements T>
178 auto names2ids(const T& database, const std::vector<std::string>& names)
179 {
180  namespace rv = std::views;
181 
182  auto is_wildcard = [](auto& name) { return name == "*"; };
183  if (std::ranges::any_of(names, is_wildcard)) { // return all id's from database
184  return database | rv::transform([](auto& i) { return i.id(); }) | ranges::to_vector;
185  }
186 
187  auto name_to_id = [&](auto& name) {
188  if (auto iter = findName(database, name); iter != database.end()) {
189  return iter->id();
190  }
191  throw std::out_of_range("name '" + name + "' not found");
192  };
193  return names | rv::transform(name_to_id) | ranges::to_vector;
194 }
195 
196 } // namespace Faunus
nlohmann::json json
JSON object.
Definition: json_support.h:10
Eigen::Vector3d Point
3D vector used for positions, velocities, forces etc.
Definition: coordinates.h:7
AtomData & findAtomByName(std::string_view name)
Finds an atom by its name in the global Faunus atoms lexicon.
Definition: atomdata.cpp:301
PatchType
Definition: atomdata.h:43
auto names2ids(const T &database, const std::vector< std::string > &names)
Search for name in database and return id()
Definition: atomdata.h:178
double T
floating point size
Definition: units.h:73
Static properties for patchy sphero cylinders (PSC)
Definition: atomdata.h:36
Common ancestor of Faunus specific runtime errors.
Definition: core.h:50
std::vector< Faunus::AtomData > atoms
Global instance of atom list.
Definition: atomdata.cpp:242
Patch stops before the end caps.
Patch runs the full length of the SC.
Cell list class templates.
Definition: actions.cpp:11
NLOHMANN_JSON_SERIALIZE_ENUM(SpheroCylinderData::PatchType, {{SpheroCylinderData::PatchType::Invalid, nullptr}, {SpheroCylinderData::PatchType::Full, "full"}, {SpheroCylinderData::PatchType::Capped, "capped"}, {SpheroCylinderData::PatchType::None, "none"}}) class AtomData
General properties for atoms.
Definition: atomdata.h:61
auto findName(RequireNamedElements auto &range, std::string_view name)
Finds the first element with a member attribute name matching the input.
Definition: atomdata.h:134
Like json, but delete entries after access.
Definition: json_support.h:26
concept RequireNamedElements
Concept for named database such as vector<AtomData>, vector<MoleculeData> etc.
Definition: atomdata.h:120
A stub to hold various parameters of interactions.
Definition: atomdata.h:16
An exception to indicate an unknown atom name in the input.
Definition: atomdata.h:147