Pteros  2.0
Molecular modeling library for human beings!
system.h
1 /*
2  * This file is a part of
3  *
4  * ============================================
5  * ### Pteros molecular modeling library ###
6  * ============================================
7  *
8  * https://github.com/yesint/pteros
9  *
10  * (C) 2009-2021, Semen Yesylevskyy
11  *
12  * All works, which use Pteros, should cite the following papers:
13  *
14  * 1. Semen O. Yesylevskyy, "Pteros 2.0: Evolution of the fast parallel
15  * molecular analysis library for C++ and python",
16  * Journal of Computational Chemistry, 2015, 36(19), 1480–1488.
17  * doi: 10.1002/jcc.23943.
18  *
19  * 2. Semen O. Yesylevskyy, "Pteros: Fast and easy to use open-source C++
20  * library for molecular analysis",
21  * Journal of Computational Chemistry, 2012, 33(19), 1632–1636.
22  * doi: 10.1002/jcc.22989.
23  *
24  * This is free software distributed under Artistic License:
25  * http://www.opensource.org/licenses/artistic-license-2.0.php
26  *
27 */
28 
29 
30 #pragma once
31 
32 #include <string>
33 #include <vector>
34 #include <functional>
35 #include <memory>
36 #include <Eigen/Core>
37 #include <Eigen/Dense>
38 #include "pteros/core/atom.h"
39 #include "pteros/core/force_field.h"
40 #include "pteros/core/periodic_box.h"
41 #include "pteros/core/typedefs.h"
42 #include "pteros/core/atom_handler.h"
43 
44 
45 namespace pteros {
46 
51 struct Frame {
53  std::vector<Eigen::Vector3f> coord;
55  std::vector<Eigen::Vector3f> vel;
57  std::vector<Eigen::Vector3f> force;
61  float time;
62 
63  Frame(): time(0.0) {}
64 
65  bool has_vel() const { return !vel.empty(); }
66  bool has_force() const { return !force.empty(); }
67 
69  void swap(int i, int j);
70 };
71 
72 //====================================================================================
73 
74 //Forward declarations
75 class Selection;
76 class FileHandler;
77 class FileContent;
78 class AtomHandler;
79 
80 //====================================================================================
81 
93 class System {
94  // System and Selection are friends because they are closely integrated.
95  friend class Selection;
96  // Selection_parser must access internals of Selection
97  friend class SelectionParser;
98  // Needs an access for constructing the system in IO handlers
99  friend class SystemBuilder;
100 
101 public:
102  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105 
107  System();
108 
110  System(std::string fname);
111 
113  System(const System& other);
114 
116  System& operator=(const System& other);
117 
119  System(const Selection& sel);
120 
122  virtual ~System();
123 
125 
129 
132  Selection append(const System& sys);
133 
138  Selection append(const Selection& sel, bool current_frame=false);
139 
142  Selection append(const Atom& at, Vector3f_const_ref coord);
143 
155  Selection append(const AtomHandler& at);
156 
160  void rearrange(const std::vector<std::string>& sel_strings);
161 
165  void rearrange(const std::vector<Selection>& sel_vec);
166 
172  void rearrange(const std::vector<std::string>& begin_strings, const std::vector<std::string>& end_strings);
173 
179  void rearrange(const std::vector<Selection>& begin_vec, const std::vector<Selection>& end_vec);
180 
185  std::vector<std::pair<std::string,int>> rearrange_by_resname();
186 
188  void keep(const std::string& sel_str);
189 
191  void keep(const Selection& sel);
192 
194  void remove(const std::string& sel_str);
195 
198  void remove(Selection& sel);
199 
203  void distribute(const Selection sel, Vector3i_const_ref ncopies, Matrix3f_const_ref shift);
204 
206 
207 
208  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
211 
213  inline int num_frames() const { return traj.size(); }
214 
216  inline int num_atoms() const { return atoms.size(); }
218 
219  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239 
241  Selection select(std::string str, int fr = 0);
242  Selection operator()(std::string str, int fr = 0);
243 
244  Selection select(int ind1, int ind2);
245  Selection operator()(int ind1, int ind2);
246 
247  Selection select(const std::vector<int>& ind);
248  Selection operator()(const std::vector<int>& ind);
249 
250  Selection select(std::vector<int>::iterator it1,
251  std::vector<int>::iterator it2);
252  Selection operator()(std::vector<int>::iterator it1,
253  std::vector<int>::iterator it2);
254 
255  Selection select(const std::function<void(const System&,int,std::vector<int>&)>& callback, int fr = 0);
256  Selection operator()(const std::function<void(const System&,int,std::vector<int>&)>& callback, int fr = 0);
258 
261  Selection select_all() const;
262  Selection operator()();
264 
265  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
268 
277  void load(std::string fname,
278  int b=0,
279  int e=-1,
280  int skip = 0,
281  std::function<bool(System*,int)> on_frame = 0);
282 
290  bool load(const std::unique_ptr<FileHandler> &handler,
291  FileContent what,
292  std::function<bool(System*,int)> on_frame = 0);
293 
294  void write(std::string fname, int b=-1,int e=-1) const;
295 
296  void write(const std::unique_ptr<FileHandler>& handler, FileContent what,int b=-1,int e=-1) const;
297 
298 
301  std::vector<std::pair<std::string, Selection> > load_gromacs_ndx(std::string fname);
302 
304 
329  void set_filter(std::string str);
331  void set_filter(int ind1, int ind2);
332  void set_filter(const std::vector<int>& ind);
333  void set_filter(std::vector<int>::iterator it1,
334  std::vector<int>::iterator it2);
336 
338 
339 
340  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
343 
345  int frame_dup(int fr);
346 
348  void frame_append(const Frame& fr);
349 
351  void frame_copy(int fr1, int fr2);
352 
357  void frame_delete(int b = 0, int e = -1);
358 
360  void frame_swap(int fr1, int fr2);
362 
363 
364  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
367 
369  inline PeriodicBox& box(int fr=0){ return traj[fr].box; }
370 
372  inline const PeriodicBox& box(int fr=0) const { return traj[fr].box; }
373 
375  inline float& time(int fr=0){ return traj[fr].time; }
376 
378  inline const float& time(int fr=0) const { return traj[fr].time; }
379 
381  inline Eigen::Vector3f& xyz(int ind, int fr=0){ return traj[fr].coord[ind]; }
382 
384  inline const Eigen::Vector3f& xyz(int ind, int fr=0) const { return traj[fr].coord[ind]; }
385 
387  inline Eigen::Vector3f& vel(int ind, int fr=0){ return traj[fr].vel[ind]; }
388 
390  inline const Eigen::Vector3f& vel(int ind, int fr=0) const { return traj[fr].vel[ind]; }
391 
393  inline Eigen::Vector3f& force(int ind, int fr=0){ return traj[fr].force[ind]; }
394 
396  inline const Eigen::Vector3f& force(int ind, int fr=0) const { return traj[fr].force[ind]; }
397 
399  inline Atom& atom(int ind) { return atoms[ind]; }
400 
402  inline const Atom& atom(int ind) const { return atoms[ind]; }
403 
405  inline Frame& frame(int fr){ return traj[fr]; }
406 
408  inline const Frame& frame(int fr) const { return traj[fr]; }
409 
411 
414  AtomIterator atoms_begin(){ return atoms.begin(); }
415  AtomIterator atoms_end(){ return atoms.end(); }
416  std::vector<Frame>::iterator traj_begin(){ return traj.begin(); }
417  std::vector<Frame>::iterator traj_end(){ return traj.end(); }
418 
419  AtomHandler operator[](const std::pair<int,int>& ind_fr);
420 
421  /*
422  class atom_iterator;
424  atom_iterator begin(int fr);
426  atom_iterator end(int fr);
427  */
428 
430 
431  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
435 
437  Selection atoms_dup(const std::vector<int>& ind);
438 
440  Selection atoms_add(const std::vector<Atom>& atm,
441  const std::vector<Eigen::Vector3f>& crd);
442 
444  void atoms_delete(const std::vector<int>& ind);
445 
447  void atom_move(int i, int j);
448 
451  Selection atom_clone(int source);
452 
454  void atom_swap(int i, int j);
455 
457  Selection atom_add_1h(int target, int at1, int at2, int at3, float dist=0.109, bool pbc=true);
458 
460  Selection atom_add_2h(int target, int at1, int at2, float dist=0.109, bool pbc=true);
461 
463  Selection atom_add_3h(int target, int at1, float dist=0.109, bool pbc=true);
464 
466 
467 
468  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
471 
473  void wrap(int fr, Array3i_const_ref pbc = fullPBC);
474 
476 
477  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
480 
482  float distance(int i, int j, int fr, Array3i_const_ref pbc = fullPBC) const;
483 
485  float angle(int i, int j, int k, int fr, Array3i_const_ref pbc = fullPBC) const;
486 
488  float dihedral(int i, int j, int k, int l, int fr, Array3i_const_ref pbc = fullPBC) const;
489 
491 
492  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
495 
497  void clear();
498 
499  bool force_field_ready(){return force_field.ready;}
500 
503  return force_field;
504  }
505 
508  void assign_resindex(int start=0);
509 
512  void sort_by_resindex();
513 
516  void clear_vel();
517 
520  void clear_force();
521 
525  Eigen::Vector2f get_energy_for_list(const std::vector<Eigen::Vector2i>& pairs,
526  const std::vector<float>& dist,
527  std::vector<Eigen::Vector2f>* pair_en=nullptr);
528 
529 
531 
532 protected:
533 
534  // Holds all atom attributes except the coordinates
535  std::vector<Atom> atoms;
536 
537  // Coordinates for any number of frames
538  std::vector<Frame> traj;
539 
540  // Force field parameters
541  ForceField force_field;
542 
543  // Indexes for filtering
544  std::vector<int> filter;
545  // Filter selection text for text-based filters
546  std::string filter_text;
547 
548  void filter_atoms();
549  void filter_coord(int fr);
550 };
551 
552 } // namespace
553 
554 
pteros::Selection
Selection class.
Definition: selection.h:65
pteros::System::frame_delete
void frame_delete(int b=0, int e=-1)
Delete specified range of frames.
Definition: system.cpp:427
pteros::System::load
void load(std::string fname, int b=0, int e=-1, int skip=0, std::function< bool(System *, int)> on_frame=0)
Read structure, trajectory or topology from file.
Definition: system.cpp:135
pteros::Frame::time
float time
Timestamp.
Definition: system.h:61
pteros::System::append
Selection append(const System &sys)
Append other system to this one Returns selection corresponding to appended atoms.
Definition: system.cpp:821
pteros::System::wrap
void wrap(int fr, Array3i_const_ref pbc=fullPBC)
Wrap all system to the periodic box for given frame.
Definition: system.cpp:1233
pteros::System::select_all
Selection select_all() const
Convenience functions to select all.
Definition: system.cpp:1153
pteros::System::~System
virtual ~System()
Destructor.
Definition: system.cpp:411
pteros::System::xyz
const Eigen::Vector3f & xyz(int ind, int fr=0) const
Read only access for given coordinate of given frame.
Definition: system.h:384
pteros::System::atom
Atom & atom(int ind)
Read/Write access for given atom.
Definition: system.h:399
pteros::System::clear_force
void clear_force()
Delete forces from all frames.
Definition: system.cpp:536
pteros::System::atom_add_3h
Selection atom_add_3h(int target, int at1, float dist=0.109, bool pbc=true)
Add 3 hydrogens to target atom according to positions of single adjacent atom assuming tetrahedral sy...
Definition: system.cpp:789
pteros::System::distance
float distance(int i, int j, int fr, Array3i_const_ref pbc=fullPBC) const
Get distance between two atoms for given frame (periodic in given dimensions if needed).
Definition: system.cpp:1189
pteros::System::vel
const Eigen::Vector3f & vel(int ind, int fr=0) const
Read only access for given velocity of given frame.
Definition: system.h:390
pteros::System::rearrange
void rearrange(const std::vector< std::string > &sel_strings)
Rearranges the atoms in the order of provided selection strings.
Definition: system.cpp:930
pteros::System::xyz
Eigen::Vector3f & xyz(int ind, int fr=0)
Read/Write access for given coordinate of given frame.
Definition: system.h:381
pteros::System::get_energy_for_list
Eigen::Vector2f get_energy_for_list(const std::vector< Eigen::Vector2i > &pairs, const std::vector< float > &dist, std::vector< Eigen::Vector2f > *pair_en=nullptr)
Low level energy evaluation function Returns total energy Individual pair energies could be returned ...
Definition: system.cpp:1252
pteros::System::dihedral
float dihedral(int i, int j, int k, int l, int fr, Array3i_const_ref pbc=fullPBC) const
Get dihedral angle in degrees between three atoms for given frame (periodic in given dimensions if ne...
Definition: system.cpp:1211
pteros::System::num_frames
int num_frames() const
Returns the number of frames in selection.
Definition: system.h:213
pteros::System::angle
float angle(int i, int j, int k, int fr, Array3i_const_ref pbc=fullPBC) const
Get angle in degrees between three atoms for given frame (periodic in given dimensions if needed).
Definition: system.cpp:1198
pteros::System::atoms_dup
Selection atoms_dup(const std::vector< int > &ind)
Adds new atoms, which are duplicates of existing ones by index. Atoms are placed at the end of the sy...
Definition: system.cpp:541
pteros::System::sort_by_resindex
void sort_by_resindex()
Sorts atoms by resindex arranging atoms with the same resindexes into contigous pieces.
Definition: system.cpp:495
pteros::System::clear
void clear()
Clears the system and prepares for loading completely new structure.
Definition: system.cpp:85
pteros::System::get_force_field
ForceField & get_force_field()
Returns internal Force_field object.
Definition: system.h:502
pteros::System::frame_swap
void frame_swap(int fr1, int fr2)
Swaps two specified frames.
Definition: system.cpp:448
pteros::System
The system of atoms.
Definition: system.h:93
pteros::Frame::force
std::vector< Eigen::Vector3f > force
Forces of atoms.
Definition: system.h:57
pteros::System::atoms_delete
void atoms_delete(const std::vector< int > &ind)
Delete the set of atoms by indexes.
Definition: system.cpp:610
pteros::System::frame_append
void frame_append(const Frame &fr)
Appends provided frame to trajectory.
Definition: system.cpp:472
pteros::System::atoms_begin
AtomIterator atoms_begin()
Iterators and indexing {.
Definition: system.h:414
pteros::System::box
PeriodicBox & box(int fr=0)
Read/write access for periodic box for given frame.
Definition: system.h:369
pteros::System::set_filter
void set_filter(std::string str)
Filters narrow set of atoms and coordinates which are loaded from data files.
Definition: system.cpp:367
pteros::System::frame_dup
int frame_dup(int fr)
Duplicates given frame and adds it to the end of frame vector.
Definition: system.cpp:413
pteros::System::atoms_add
Selection atoms_add(const std::vector< Atom > &atm, const std::vector< Eigen::Vector3f > &crd)
Adds new atoms from supplied vectors of atoms and coordinates. Atoms are placed at the end of the sys...
Definition: system.cpp:576
pteros::System::box
const PeriodicBox & box(int fr=0) const
Read only access for periodic box for given frame.
Definition: system.h:372
pteros::System::frame_copy
void frame_copy(int fr1, int fr2)
Copy all frame data from fr1 to fr2. Fr2 is overwritten!
Definition: system.cpp:420
pteros::System::time
float & time(int fr=0)
Read/Write access to the time stamp of given frame.
Definition: system.h:375
pteros::System::frame
const Frame & frame(int fr) const
Get read only reference for given frame.
Definition: system.h:408
pteros::System::operator=
System & operator=(const System &other)
Assignment operator.
Definition: system.cpp:75
pteros::System::keep
void keep(const std::string &sel_str)
Keep only atoms given by selection string.
Definition: system.cpp:1036
pteros::AtomHandler
Auxilary type used to incapsulate the atom and its current coordinates Used internally in Selection::...
Definition: atom_handler.h:49
pteros::Frame::vel
std::vector< Eigen::Vector3f > vel
Velocities of atoms.
Definition: system.h:55
pteros::System::load_gromacs_ndx
std::vector< std::pair< std::string, Selection > > load_gromacs_ndx(std::string fname)
Load Gromacs .ndx file and crease selections acording to it from existing system Returns a vector of ...
Definition: system.cpp:333
pteros::System::force
Eigen::Vector3f & force(int ind, int fr=0)
Read/Write access for given force of given frame.
Definition: system.h:393
pteros::System::frame
Frame & frame(int fr)
Get read/write reference for given frame.
Definition: system.h:405
pteros::System::distribute
void distribute(const Selection sel, Vector3i_const_ref ncopies, Matrix3f_const_ref shift)
Creates multiple copies of selection in the system and distributes them in a grid given by 3 translat...
Definition: system.cpp:1066
pteros::System::clear_vel
void clear_vel()
Delete velocities from all frames.
Definition: system.cpp:531
pteros::ForceField
Force field parameters of the system.
Definition: force_field.h:53
pteros::System::System
System()
Default constructor.
Definition: system.cpp:51
pteros::System::atom_swap
void atom_swap(int i, int j)
Swap two atoms.
Definition: system.cpp:712
pteros::System::atom_add_2h
Selection atom_add_2h(int target, int at1, int at2, float dist=0.109, bool pbc=true)
Add two hydrogens to target atom according to positions of 2 adjacent atoms assuming tetrahedral symm...
Definition: system.cpp:755
pteros::Frame::box
PeriodicBox box
Periodic box.
Definition: system.h:59
pteros::System::force
const Eigen::Vector3f & force(int ind, int fr=0) const
Read only access for given force of given frame.
Definition: system.h:396
pteros::Frame::coord
std::vector< Eigen::Vector3f > coord
Coordinates of atoms.
Definition: system.h:53
pteros::System::atom
const Atom & atom(int ind) const
Read only access for given atom.
Definition: system.h:402
pteros::System::atom_move
void atom_move(int i, int j)
Move atom i to other position. Atom is inserted instead of atom j, shift is performed towards previou...
Definition: system.cpp:650
pteros::PeriodicBox
Class encapsulating all operations with arbitrary triclinic periodic boxes This class stores the peri...
Definition: periodic_box.h:43
pteros::System::vel
Eigen::Vector3f & vel(int ind, int fr=0)
Read/Write access for given velocity of given frame.
Definition: system.h:387
pteros::SelectionParser
Selection parser class.
Definition: selection_parser.h:59
pteros::System::num_atoms
int num_atoms() const
Returns the number of atoms in selection.
Definition: system.h:216
pteros::System::atom_clone
Selection atom_clone(int source)
Duplicate single target atom and puts it immediately after the source.
Definition: system.cpp:705
pteros::Frame
Definition of single trajectory frame.
Definition: system.h:51
pteros::System::remove
void remove(const std::string &sel_str)
Remove atoms given by selection string.
Definition: system.cpp:1050
pteros::System::atom_add_1h
Selection atom_add_1h(int target, int at1, int at2, int at3, float dist=0.109, bool pbc=true)
Add single hydrogen to target atom according to positions of 3 adjacent atoms assuming tetrahedral sy...
Definition: system.cpp:724
pteros::System::assign_resindex
void assign_resindex(int start=0)
Assign unique resindexes This is usually done automatically upon loading a structure from file.
Definition: system.cpp:476
pteros::System::rearrange_by_resname
std::vector< std::pair< std::string, int > > rearrange_by_resname()
Rearranges the atoms in the alphabetical order of residue names.
Definition: system.cpp:1013
pteros::Atom
Class which represents a single atom.
Definition: atom.h:39
pteros::Frame::swap
void swap(int i, int j)
Swap atoms i and j taking care of v and f.
Definition: system.cpp:1284
pteros::ForceField::ready
bool ready
Is the force field properly set up?
Definition: force_field.h:89
pteros::System::time
const float & time(int fr=0) const
Read only access to the time stamp of given frame.
Definition: system.h:378