Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvaratoms.h
1#ifndef COLVARATOMS_SOA_H
2#define COLVARATOMS_SOA_H
3
4#include "colvarmodule.h"
5#include "colvardeps.h"
6#include "colvar_rotation_derivative.h"
7#include <unordered_map>
8#include <mutex>
9
51class cvm::atom_group: public colvarparse, public colvardeps {
52public:
58 static std::vector<cvm::real> pos_aos_to_soa(const std::vector<cvm::atom_pos>& aos_in);
65 atom_group();
69 atom_group(char const *key_in);
76 ~atom_group() override;
80 int init();
87 int setup();
91 int init_dependencies() override;
93 const std::vector<feature *> &features() const override { return ag_features; }
94 std::vector<feature *> &modify_features() override { return ag_features; }
95 static void delete_features()
96 {
97 for (size_t i = 0; i < ag_features.size(); i++) {
98 delete ag_features[i];
99 }
100 ag_features.clear();
101 }
106 struct simple_atom {
107 int proxy_index;
108 int id;
109 cvm::real mass;
110 cvm::real charge;
111 cvm::atom_pos pos;
112 cvm::rvector vel;
113 cvm::rvector total_force;
114 cvm::rvector grad;
115 };
126 colvarproxy* const p,
127 cvm::residue_id const &residue,
128 std::string const &atom_name,
129 std::string const &segment_id);
137 colvarproxy* const p,
138 int atom_number);
150 colvarproxy* const p,
151 const simple_atom& atom);
156 private:
164 std::vector<int> m_atoms_ids;
168 std::vector<simple_atom> m_atoms;
172 std::unordered_map<int, int> m_atoms_ids_count;
184 void update_from_soa();
188 void sync_to_soa() const;
189 public:
190 using atom_iter = decltype(m_atoms)::iterator;
191 using const_atom_iter = decltype(m_atoms)::const_iterator;
208 inline simple_atom & operator [] (size_t const i){return m_atoms[i];}
209 inline simple_atom const & operator [] (size_t const i) const {return m_atoms[i];}
210 inline atom_iter begin(){return m_atoms.begin();}
211 inline const_atom_iter begin() const {return m_atoms.begin();}
212 inline atom_iter end() {return m_atoms.end();}
213 inline const_atom_iter end() const {return m_atoms.end();}
214 inline size_t size() const {return m_atoms.size();}
216
220 int add_atom(simple_atom const &a);
221 int add_atom_numbers(std::string const &numbers_conf);
222 int add_atoms_of_group(const atom_group *ag);
223 int add_index_group(std::string const &index_group_name, bool silent = false);
224 int add_atom_numbers_range(std::string const &range_conf);
225 int add_atom_name_residue_range(std::string const &psf_segid,
226 std::string const &range_conf);
227 int add_atom_id(int aid);
229
232 int remove_atom(atom_iter ai);
233 };
243 return atom_modifier(this);
244 }
252 void clear_soa();
256 int parse(std::string const &conf);
260 int parse_fitting_options(std::string const &group_conf);
264 int set_dummy();
268 int set_dummy_pos(cvm::atom_pos const &pos);
272 void update_total_mass();
276 void update_total_charge();
283 void print_properties(std::string const &colvar_name, int i, int j);
287 inline std::vector<int> const &ids() const
288 {
289 return atoms_ids;
290 }
291 std::string const print_atom_ids() const;
295 int create_sorted_ids();
301 inline std::vector<int> const &sorted_ids() const
302 {
303 return sorted_atoms_ids;
304 }
306 inline std::vector<int> const &sorted_ids_map() const
307 {
309 }
315 static int overlap(const atom_group &g1, const atom_group &g2);
319 void read_positions();
323 void calc_apply_roto_translation();
328 void setup_rotation_derivative();
338 void center_ref_pos();
342 void rotate(const cvm::rmatrix& rot_mat);
346 void apply_translation(cvm::rvector const &t);
354 void read_velocities();
362 void read_total_forces();
366 int calc_required_properties();
371 int calc_center_of_geometry();
375 std::vector<cvm::real> positions() const;
380 {
381 return cog;
382 }
387 int calc_center_of_mass();
392 {
393 return com;
394 }
400 {
401 return scalar_com_gradient;
402 }
406 std::vector<cvm::real> positions_shifted(cvm::rvector const &shift) const;
410 std::vector<cvm::real> velocities() const;
414 int calc_dipole(cvm::atom_pos const &dipole_center);
418 inline cvm::rvector dipole() const
419 {
420 return dip;
421 }
425 std::vector<cvm::real> total_forces() const;
429 cvm::rvector total_force() const;
435 void set_weighted_gradient(cvm::rvector const &grad);
439 void calc_fit_gradients();
469 template <bool B_ag_center, bool B_ag_rotate,
470 typename main_force_accessor_T,
471 typename fitting_force_accessor_T>
472 void calc_fit_forces_impl(
473 main_force_accessor_T accessor_main,
474 fitting_force_accessor_T accessor_fitting) const;
475
491 template <typename main_force_accessor_T, typename fitting_force_accessor_T>
492 void calc_fit_forces(
493 main_force_accessor_T accessor_main,
494 fitting_force_accessor_T accessor_fitting) const;
509 void apply_colvar_force(cvm::real const &force);
523 void apply_force(cvm::rvector const &force);
529 void do_feature_side_effects(int id) override;
533 size_t size() const {return num_atoms;}
537 void reset_atoms_data();
541 void set_ref_pos_from_aos(const std::vector<cvm::atom_pos>& pos_aos);
546 inline int& id(size_t i) {return atoms_ids[i];}
547 inline const int& id(size_t i) const {return atoms_ids[i];}
549
553 inline cvm::real& pos_x(size_t i) {return atoms_pos[i];}
554 inline cvm::real& pos_y(size_t i) {return atoms_pos[i + num_atoms];}
555 inline cvm::real& pos_z(size_t i) {return atoms_pos[i + 2 * num_atoms];}
556 inline const cvm::real& pos_x(size_t i) const {return atoms_pos[i];}
557 inline const cvm::real& pos_y(size_t i) const {return atoms_pos[i + num_atoms];}
558 inline const cvm::real& pos_z(size_t i) const {return atoms_pos[i + 2 * num_atoms];}
560
564 inline cvm::real& vel_x(size_t i) {return atoms_vel[i];}
565 inline cvm::real& vel_y(size_t i) {return atoms_vel[i + num_atoms];}
566 inline cvm::real& vel_z(size_t i) {return atoms_vel[i + 2 * num_atoms];}
567 inline const cvm::real& vel_x(size_t i) const {return atoms_vel[i];}
568 inline const cvm::real& vel_y(size_t i) const {return atoms_vel[i + num_atoms];}
569 inline const cvm::real& vel_z(size_t i) const {return atoms_vel[i + 2 * num_atoms];}
571
575 inline cvm::real& grad_x(size_t i) {return atoms_grad[i];}
576 inline cvm::real& grad_y(size_t i) {return atoms_grad[i + num_atoms];}
577 inline cvm::real& grad_z(size_t i) {return atoms_grad[i + 2 * num_atoms];}
578 inline const cvm::real& grad_x(size_t i) const {return atoms_grad[i];}
579 inline const cvm::real& grad_y(size_t i) const {return atoms_grad[i + num_atoms];}
580 inline const cvm::real& grad_z(size_t i) const {return atoms_grad[i + 2 * num_atoms];}
582
586 inline cvm::real& total_force_x(size_t i) {return atoms_total_force[i];}
587 inline cvm::real& total_force_y(size_t i) {return atoms_total_force[i + num_atoms];}
588 inline cvm::real& total_force_z(size_t i) {return atoms_total_force[i + 2 * num_atoms];}
589 inline const cvm::real& total_force_x(size_t i) const {return atoms_total_force[i];}
590 inline const cvm::real& total_force_y(size_t i) const {return atoms_total_force[i + num_atoms];}
591 inline const cvm::real& total_force_z(size_t i) const {return atoms_total_force[i + 2 * num_atoms];}
593
597 inline cvm::real& ref_pos_x(size_t i) {return ref_pos[i];}
598 inline cvm::real& ref_pos_y(size_t i) {return ref_pos[i + num_ref_pos];}
599 inline cvm::real& ref_pos_z(size_t i) {return ref_pos[i + 2 * num_ref_pos];}
600 inline const cvm::real& ref_pos_x(size_t i) const {return ref_pos[i];}
601 inline const cvm::real& ref_pos_y(size_t i) const {return ref_pos[i + num_ref_pos];}
602 inline const cvm::real& ref_pos_z(size_t i) const {return ref_pos[i + 2 * num_ref_pos];}
604
608 inline cvm::real& pos_unrotated_x(size_t i) {return atoms_pos_unrotated[i];}
609 inline cvm::real& pos_unrotated_y(size_t i) {return atoms_pos_unrotated[i + num_atoms];}
610 inline cvm::real& pos_unrotated_z(size_t i) {return atoms_pos_unrotated[i + 2 * num_atoms];}
611 inline const cvm::real& pos_unrotated_x(size_t i) const {return atoms_pos_unrotated[i];}
612 inline const cvm::real& pos_unrotated_y(size_t i) const {return atoms_pos_unrotated[i + num_atoms];}
613 inline const cvm::real& pos_unrotated_z(size_t i) const {return atoms_pos_unrotated[i + 2 * num_atoms];}
615
619 inline cvm::real& mass(size_t i) {return atoms_mass[i];}
620 inline const cvm::real& mass(size_t i) const {return atoms_mass[i];}
622
626 inline cvm::real& weight(size_t i) {return atoms_weight[i];}
627 inline const cvm::real& weight(size_t i) const {return atoms_weight[i];}
629
633 inline cvm::real& charge(size_t i) {return atoms_charge[i];}
634 inline const cvm::real& charge(size_t i) const {return atoms_charge[i];}
636
640 inline cvm::real& fit_gradients_x(size_t i) {
641 atom_group *group_for_fit = fitting_group ? fitting_group : this;
642 return group_for_fit->fit_gradients[i];}
643 inline cvm::real& fit_gradients_y(size_t i) {
644 atom_group *group_for_fit = fitting_group ? fitting_group : this;
645 return group_for_fit->fit_gradients[i + group_for_fit->size()];}
646 inline cvm::real& fit_gradients_z(size_t i) {
647 atom_group *group_for_fit = fitting_group ? fitting_group : this;
648 return group_for_fit->fit_gradients[i + 2 * group_for_fit->size()];}
649 inline const cvm::real& fit_gradients_x(size_t i) const {
650 const atom_group *group_for_fit = fitting_group ? fitting_group : this;
651 return group_for_fit->fit_gradients[i];}
652 inline const cvm::real& fit_gradients_y(size_t i) const {
653 const atom_group *group_for_fit = fitting_group ? fitting_group : this;
654 return group_for_fit->fit_gradients[i + group_for_fit->size()];}
655 inline const cvm::real& fit_gradients_z(size_t i) const {
656 const atom_group *group_for_fit = fitting_group ? fitting_group : this;
657 return group_for_fit->fit_gradients[i + 2 * group_for_fit->size()];}
659
663 inline cvm::real& group_forces_x(size_t i) {return group_forces[i];}
664 inline cvm::real& group_forces_y(size_t i) {return group_forces[i + num_atoms];}
665 inline cvm::real& group_forces_z(size_t i) {return group_forces[i + 2 * num_atoms];}
666 inline const cvm::real& group_forces_x(size_t i) const {return group_forces[i];}
667 inline const cvm::real& group_forces_y(size_t i) const {return group_forces[i + num_atoms];}
668 inline const cvm::real& group_forces_z(size_t i) const {return group_forces[i + 2 * num_atoms];}
670
673 inline simple_atom operator[](size_t i) const {
674 return simple_atom{
675 /*.proxy_index = */atoms_index[i],
676 /*.id = */atoms_ids[i],
677 /*.mass = */atoms_mass[i],
678 /*.charge = */atoms_charge[i],
679 /*.pos = */{pos_x(i), pos_y(i), pos_z(i)},
680 /*.vel = */{vel_x(i), vel_y(i), vel_z(i)},
681 /*.total_force = */{total_force_x(i), total_force_y(i), total_force_z(i)},
682 /*.grad = */{grad_x(i), grad_y(i), grad_z(i)}
683 };
684 }
685
693 public:
726 void add_atom_force(size_t i, const cvm::rvector& force);
727 private:
728 cvm::atom_group* m_ag;
729 cvm::atom_group* m_group_for_fit;
730 bool m_has_fitting_force;
731 void apply_force_with_fitting_group();
732 };
733 group_force_object get_group_force_object();
734public:
736 std::string name;
742 // TODO Make this field part of the data structures that link a group to a CVC
743 std::string key;
757 std::vector<cvm::real> fit_gradients;
765 static std::vector<feature *> ag_features;
766private:
768 size_t num_atoms;
770 std::vector<int> atoms_index;
772 std::vector<cvm::real> atoms_pos;
774 std::vector<cvm::real> atoms_charge;
776 std::vector<cvm::real> atoms_vel;
778 std::vector<cvm::real> atoms_mass;
780 std::vector<cvm::real> atoms_grad;
782 std::vector<cvm::real> atoms_total_force;
784 std::vector<cvm::real> atoms_weight;
786 std::vector<int> atoms_ids;
789 std::vector<int> sorted_atoms_ids;
791 std::vector<int> sorted_atoms_ids_map;
795 int index;
799 std::vector<cvm::real> group_forces;
801 std::vector<cvm::real> ref_pos;
802 size_t num_ref_pos; // TODO: Do I really need this?
812 std::vector<cvm::real> atoms_pos_unrotated;
816 // TODO for scalable calculations of more complex variables (e.g. rotation),
817 // use a colvarvalue of vectors to hold the entire derivative
822 std::mutex modify_lock;
823};
824
825#endif // COLVARATOMS_SOA_H
Parent class for a member object of a bias, cv or cvc etc. containing features and their dependencies...
Definition: colvardeps.h:34
2-dimensional array of real numbers with three components along each dimension (works with colvarmodu...
Definition: colvartypes.h:891
A rotation between two sets of coordinates (for the moment a wrapper for colvarmodule::quaternion)
Definition: colvartypes.h:1359
vector of real numbers with three components
Definition: colvartypes.h:723
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:150
int residue_id
Residue identifier.
Definition: colvarmodule.h:275
Base class containing parsing functions; all objects which need to parse input inherit from this.
Definition: colvarparse.h:27
Definition: colvarproxy.h:559
The temporary AoS interface to modify cvm::atom_group.
Definition: colvaratoms.h:155
cvm::real m_total_mass
Total mass of the atom group.
Definition: colvaratoms.h:176
std::vector< int > m_atoms_ids
Internal atom IDs (populated during initialization)
Definition: colvaratoms.h:164
std::vector< simple_atom > m_atoms
AoS layout of the atoms.
Definition: colvaratoms.h:168
cvm::atom_group * m_ag
Pointer to the SoA atom group to be modified.
Definition: colvaratoms.h:160
std::unordered_map< int, int > m_atoms_ids_count
A map to avoid doubly counting atoms.
Definition: colvaratoms.h:172
void sync_to_soa() const
Synchronize the atoms from this temporary AoS object to m_ag.
Definition: colvaratoms.cpp:256
int remove_atom(atom_iter ai)
Remove an atom object from this group.
Definition: colvaratoms.cpp:332
cvm::real m_total_charge
Total charge of the atom group.
Definition: colvaratoms.h:180
void update_from_soa()
Populate this temporary AoS object with the atoms from m_ag.
Definition: colvaratoms.cpp:243
A helper class for applying forces on an atom group in a way that is aware of the fitting group....
Definition: colvaratoms.h:692
void add_atom_force(size_t i, const cvm::rvector &force)
Apply force to atom i.
Definition: colvaratoms.cpp:1754
Store the information of a group of atoms in a structure-of-arrays (SoA) style.
Definition: colvaratoms.h:51
cvm::atom_pos center_of_geometry() const
Return the center of geometry of the atomic positions.
Definition: colvaratoms.h:379
cvm::real total_mass
Total mass of the atom group.
Definition: colvaratoms.h:759
cvm::atom_pos ref_pos_cog
Center of geometry of the reference coordinates; regardless of whether f_ag_center is true,...
Definition: colvaratoms.h:806
std::vector< cvm::real > group_forces
The temporary forces acting on the main group atoms. Currently this is only used for calculating the ...
Definition: colvaratoms.h:799
std::vector< int > atoms_index
SOA atom indices (size: num_atoms)
Definition: colvaratoms.h:770
std::vector< cvm::real > atoms_mass
SOA atom mass (size: num_atoms)
Definition: colvaratoms.h:778
std::vector< cvm::real > atoms_grad
SOA atom gradients (size: 3 * num_atoms)
Definition: colvaratoms.h:780
std::mutex modify_lock
Lock for modifier.
Definition: colvaratoms.h:822
cvm::rvector dipole() const
Return the (previously calculated) dipole of the atom group.
Definition: colvaratoms.h:418
std::string key
Keyword used to define the group.
Definition: colvaratoms.h:743
int init()
Set default values for common flags.
Definition: colvaratoms.cpp:136
simple_atom operator[](size_t i) const
Read-only operator[].
Definition: colvaratoms.h:673
bool b_user_defined_fit
Indicates that the user has explicitly set centerToReference or rotateReference, and the correspondin...
Definition: colvaratoms.h:755
cvm::rvector dip
Dipole moment of the atom group.
Definition: colvaratoms.h:820
std::vector< int > const & ids() const
Internal atom IDs (populated during initialization)
Definition: colvaratoms.h:287
int init_dependencies() override
Initialize dependency tree.
Definition: colvaratoms.cpp:163
cvm::rvector center_of_mass_scalar_gradient() const
Return previously gradient of scalar variable with respect to the COM.
Definition: colvaratoms.h:399
std::vector< cvm::real > atoms_total_force
SOA atom total forces (size: 3 * num_atoms)
Definition: colvaratoms.h:782
static std::vector< feature * > ag_features
Implementation of the feature list for atom group.
Definition: colvaratoms.h:765
cvm::rotation rot
The rotation calculated automatically if f_ag_rotate is defined.
Definition: colvaratoms.h:748
atom_group()
Default constructor.
Definition: colvaratoms.cpp:84
std::vector< cvm::real > atoms_weight
Atom masses divided by total mass (size: num_atoms)
Definition: colvaratoms.h:784
std::vector< int > atoms_ids
Internal atom IDs for host code.
Definition: colvaratoms.h:786
std::vector< int > sorted_atoms_ids
Sorted list of internal atom IDs (populated on-demand by create_sorted_ids); used to read coordinate ...
Definition: colvaratoms.h:789
cvm::atom_pos dummy_atom_pos
Dummy atom position.
Definition: colvaratoms.h:793
cvm::atom_pos com
Center of mass.
Definition: colvaratoms.h:814
std::vector< cvm::real > atoms_pos
SOA atom positions (size: 3 * num_atoms)
Definition: colvaratoms.h:772
cvm::atom_pos center_of_mass() const
Return the center of mass (COM) of the atomic positions.
Definition: colvaratoms.h:391
bool b_dummy
If this option is on, this group merely acts as a wrapper for a fixed position; any calls to atoms wi...
Definition: colvaratoms.h:740
size_t size() const
Return the number of atoms of this group.
Definition: colvaratoms.h:533
const std::vector< feature * > & features() const override
Implementation of the feature list accessor for atom group.
Definition: colvaratoms.h:93
static simple_atom init_atom_from_proxy(colvarproxy *const p, cvm::residue_id const &residue, std::string const &atom_name, std::string const &segment_id)
Initialize an instance of cvm::atom_group::simple_atom from colvarproxy.
Definition: colvaratoms.cpp:27
bool noforce
Don't apply any force on this group (use its coordinates only to calculate a colvar)
Definition: colvaratoms.h:751
std::string name
Optional name to reuse properties of this in other groups.
Definition: colvaratoms.h:736
std::vector< int > const & sorted_ids() const
Sorted internal atom IDs (populated on-demand by create_sorted_ids);.
Definition: colvaratoms.h:301
std::vector< cvm::real > fit_gradients
Derivatives of the fitting transformation.
Definition: colvaratoms.h:757
size_t num_atoms
Number of atoms.
Definition: colvaratoms.h:768
int index
Index in the colvarproxy arrays (if the group is scalable)
Definition: colvaratoms.h:795
cvm::atom_group * fitting_group
If f_ag_center or f_ag_rotate is true, use this group to define the transformation (default: this gro...
Definition: colvaratoms.h:746
cvm::atom_pos cog_orig
Center of geometry before any fitting.
Definition: colvaratoms.h:810
cvm::atom_pos cog
Center of geometry.
Definition: colvaratoms.h:808
cvm::rvector scalar_com_gradient
The derivative of a scalar variable with respect to the COM.
Definition: colvaratoms.h:818
std::vector< int > sorted_atoms_ids_map
Map entries of sorted_atoms_ids onto the original positions in the group.
Definition: colvaratoms.h:791
std::vector< cvm::real > atoms_charge
SOA atom charges (size: num_atoms)
Definition: colvaratoms.h:774
std::vector< cvm::real > atoms_pos_unrotated
Unrotated atom positions for fit gradients.
Definition: colvaratoms.h:812
std::vector< cvm::real > atoms_vel
SOA atom velocities (size: 3 * num_atoms)
Definition: colvaratoms.h:776
cvm::real total_charge
Total charge of the atom group.
Definition: colvaratoms.h:761
atom_modifier get_atom_modifier()
Get the atom modifier object associated with this SoA atom group.
Definition: colvaratoms.h:242
rotation_derivative * rot_deriv
Rotation derivative;.
Definition: colvaratoms.h:763
int setup()
Update data required to calculate cvc's.
Definition: colvaratoms.cpp:223
std::vector< cvm::real > ref_pos
use reference coordinates for f_ag_center or f_ag_rotate
Definition: colvaratoms.h:801
std::vector< int > const & sorted_ids_map() const
Map entries of sorted_atoms_ids onto the original positions in the group.
Definition: colvaratoms.h:306
static std::vector< cvm::real > pos_aos_to_soa(const std::vector< cvm::atom_pos > &aos_in)
A helper function to re-arrange the a vector of cvm::atom_pos (in AoS style xyz......
Definition: colvaratoms.cpp:13
Collective variables main module.
A simplified class of cvm::atom that can be used with cvm::atom_group::atom_modifier.
Definition: colvaratoms.h:106
Helper class for calculating the derivative of rotation.
Definition: colvar_rotation_derivative.h:40