Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvaratoms.h
1#ifndef COLVARATOMS_SOA_H
2#define COLVARATOMS_SOA_H
3
4#include "colvaratoms_gpu.h"
5#include "colvarmodule.h"
6#include "colvardeps.h"
7#include "colvar_rotation_derivative.h"
8#include <unordered_map>
9#include <mutex>
10
52class cvm::atom_group: public colvarparse, public colvardeps {
53public:
59 static ag_vector_real_t pos_aos_to_soa(const std::vector<cvm::atom_pos>& aos_in);
66 atom_group();
70 atom_group(char const *key_in);
77 ~atom_group() override;
81 int init();
88 int setup();
92 int init_dependencies() override;
94 const std::vector<feature *> &features() const override { return ag_features; }
95 std::vector<feature *> &modify_features() override { return ag_features; }
96 static void delete_features()
97 {
98 for (size_t i = 0; i < ag_features.size(); i++) {
99 delete ag_features[i];
100 }
101 ag_features.clear();
102 }
107 struct simple_atom {
108 int proxy_index;
109 int id;
110 cvm::real mass;
111 cvm::real charge;
112 cvm::atom_pos pos;
113 cvm::rvector vel;
114 cvm::rvector total_force;
115 cvm::rvector grad;
116 };
127 colvarproxy* const p,
128 cvm::residue_id const &residue,
129 std::string const &atom_name,
130 std::string const &segment_id);
138 colvarproxy* const p,
139 int atom_number);
151 colvarproxy* const p,
152 const simple_atom& atom);
157 private:
165 std::vector<int> m_atoms_ids;
169 std::vector<simple_atom> m_atoms;
173 std::unordered_map<int, int> m_atoms_ids_count;
185 void update_from_soa();
189 void sync_to_soa() const;
190 public:
191 using atom_iter = decltype(m_atoms)::iterator;
192 using const_atom_iter = decltype(m_atoms)::const_iterator;
209 inline simple_atom & operator [] (size_t const i){return m_atoms[i];}
210 inline simple_atom const & operator [] (size_t const i) const {return m_atoms[i];}
211 inline atom_iter begin(){return m_atoms.begin();}
212 inline const_atom_iter begin() const {return m_atoms.begin();}
213 inline atom_iter end() {return m_atoms.end();}
214 inline const_atom_iter end() const {return m_atoms.end();}
215 inline size_t size() const {return m_atoms.size();}
217
221 int add_atom(simple_atom const &a);
222 int add_atom_numbers(std::string const &numbers_conf);
223 int add_atoms_of_group(const atom_group *ag);
224 int add_index_group(std::string const &index_group_name, bool silent = false);
225 int add_atom_numbers_range(std::string const &range_conf);
226 int add_atom_name_residue_range(std::string const &psf_segid,
227 std::string const &range_conf);
228 int add_atom_id(int aid);
230
233 int remove_atom(atom_iter ai);
234 };
244 return atom_modifier(this);
245 }
253 void clear_soa();
257 int parse(std::string const &conf);
261 int parse_fitting_options(std::string const &group_conf);
265 int set_dummy();
269 int set_dummy_pos(cvm::atom_pos const &pos);
273 void update_total_mass();
277 void update_total_charge();
284 void print_properties(std::string const &colvar_name, int i, int j);
288 inline std::vector<int> const &ids() const
289 {
290 return atoms_ids;
291 }
292 std::string const print_atom_ids() const;
296 int create_sorted_ids();
302 inline std::vector<int> const &sorted_ids() const
303 {
304 return sorted_atoms_ids;
305 }
307 inline std::vector<int> const &sorted_ids_map() const
308 {
310 }
316 static int overlap(const atom_group &g1, const atom_group &g2);
320 void read_positions();
324 void calc_apply_roto_translation();
329 void setup_rotation_derivative();
339 void center_ref_pos();
343 void rotate(const cvm::rmatrix& rot_mat);
347 void apply_translation(cvm::rvector const &t);
355 void read_velocities();
363 void read_total_forces();
367 int calc_required_properties();
372 int calc_center_of_geometry();
376 cvm::ag_vector_real_t positions() const;
381 {
382 return cog;
383 }
388 int calc_center_of_mass();
393 {
394 return com;
395 }
401 {
402 return scalar_com_gradient;
403 }
407 cvm::ag_vector_real_t positions_shifted(cvm::rvector const &shift) const;
411 std::vector<cvm::real> velocities() const;
415 int calc_dipole(cvm::atom_pos const &dipole_center);
419 inline cvm::rvector dipole() const
420 {
421 return dip;
422 }
426 std::vector<cvm::real> total_forces() const;
430 cvm::rvector total_force() const;
436 void set_weighted_gradient(cvm::rvector const &grad);
440 void calc_fit_gradients();
472 template <bool B_ag_center, bool B_ag_rotate,
473 typename main_force_accessor_T,
474 typename fitting_force_accessor_T>
475 void calc_fit_forces_impl(
476 main_force_accessor_T accessor_main,
477 fitting_force_accessor_T accessor_fitting) const;
478
494 template <typename main_force_accessor_T, typename fitting_force_accessor_T>
495 void calc_fit_forces(
496 main_force_accessor_T accessor_main,
497 fitting_force_accessor_T accessor_fitting) const;
512 void apply_colvar_force(cvm::real const &force);
526 void apply_force(cvm::rvector const &force);
532 void do_feature_side_effects(int id) override;
536 size_t size() const {return num_atoms;}
540 void reset_atoms_data();
544 void set_ref_pos_from_aos(const std::vector<cvm::atom_pos>& pos_aos);
549 inline int& id(size_t i) {return atoms_ids[i];}
550 inline const int& id(size_t i) const {return atoms_ids[i];}
552
556 inline cvm::real& pos_x(size_t i) {return atoms_pos[i];}
557 inline cvm::real& pos_y(size_t i) {return atoms_pos[i + num_atoms];}
558 inline cvm::real& pos_z(size_t i) {return atoms_pos[i + 2 * num_atoms];}
559 inline const cvm::real& pos_x(size_t i) const {return atoms_pos[i];}
560 inline const cvm::real& pos_y(size_t i) const {return atoms_pos[i + num_atoms];}
561 inline const cvm::real& pos_z(size_t i) const {return atoms_pos[i + 2 * num_atoms];}
563
567 inline cvm::real& vel_x(size_t i) {return atoms_vel[i];}
568 inline cvm::real& vel_y(size_t i) {return atoms_vel[i + num_atoms];}
569 inline cvm::real& vel_z(size_t i) {return atoms_vel[i + 2 * num_atoms];}
570 inline const cvm::real& vel_x(size_t i) const {return atoms_vel[i];}
571 inline const cvm::real& vel_y(size_t i) const {return atoms_vel[i + num_atoms];}
572 inline const cvm::real& vel_z(size_t i) const {return atoms_vel[i + 2 * num_atoms];}
574
578 inline cvm::real& grad_x(size_t i) {return atoms_grad[i];}
579 inline cvm::real& grad_y(size_t i) {return atoms_grad[i + num_atoms];}
580 inline cvm::real& grad_z(size_t i) {return atoms_grad[i + 2 * num_atoms];}
581 inline const cvm::real& grad_x(size_t i) const {return atoms_grad[i];}
582 inline const cvm::real& grad_y(size_t i) const {return atoms_grad[i + num_atoms];}
583 inline const cvm::real& grad_z(size_t i) const {return atoms_grad[i + 2 * num_atoms];}
585
589 inline cvm::real& total_force_x(size_t i) {return atoms_total_force[i];}
590 inline cvm::real& total_force_y(size_t i) {return atoms_total_force[i + num_atoms];}
591 inline cvm::real& total_force_z(size_t i) {return atoms_total_force[i + 2 * num_atoms];}
592 inline const cvm::real& total_force_x(size_t i) const {return atoms_total_force[i];}
593 inline const cvm::real& total_force_y(size_t i) const {return atoms_total_force[i + num_atoms];}
594 inline const cvm::real& total_force_z(size_t i) const {return atoms_total_force[i + 2 * num_atoms];}
596
600 inline cvm::real& ref_pos_x(size_t i) {return ref_pos[i];}
601 inline cvm::real& ref_pos_y(size_t i) {return ref_pos[i + num_ref_pos];}
602 inline cvm::real& ref_pos_z(size_t i) {return ref_pos[i + 2 * num_ref_pos];}
603 inline const cvm::real& ref_pos_x(size_t i) const {return ref_pos[i];}
604 inline const cvm::real& ref_pos_y(size_t i) const {return ref_pos[i + num_ref_pos];}
605 inline const cvm::real& ref_pos_z(size_t i) const {return ref_pos[i + 2 * num_ref_pos];}
607
611 inline cvm::real& pos_unrotated_x(size_t i) {return atoms_pos_unrotated[i];}
612 inline cvm::real& pos_unrotated_y(size_t i) {return atoms_pos_unrotated[i + num_atoms];}
613 inline cvm::real& pos_unrotated_z(size_t i) {return atoms_pos_unrotated[i + 2 * num_atoms];}
614 inline const cvm::real& pos_unrotated_x(size_t i) const {return atoms_pos_unrotated[i];}
615 inline const cvm::real& pos_unrotated_y(size_t i) const {return atoms_pos_unrotated[i + num_atoms];}
616 inline const cvm::real& pos_unrotated_z(size_t i) const {return atoms_pos_unrotated[i + 2 * num_atoms];}
618
622 inline cvm::real& mass(size_t i) {return atoms_mass[i];}
623 inline const cvm::real& mass(size_t i) const {return atoms_mass[i];}
625
629 inline cvm::real& weight(size_t i) {return atoms_weight[i];}
630 inline const cvm::real& weight(size_t i) const {return atoms_weight[i];}
632
636 inline cvm::real& charge(size_t i) {return atoms_charge[i];}
637 inline const cvm::real& charge(size_t i) const {return atoms_charge[i];}
639
643 inline cvm::real& fit_gradients_x(size_t i) {
644 atom_group *group_for_fit = fitting_group ? fitting_group : this;
645 return group_for_fit->fit_gradients[i];}
646 inline cvm::real& fit_gradients_y(size_t i) {
647 atom_group *group_for_fit = fitting_group ? fitting_group : this;
648 return group_for_fit->fit_gradients[i + group_for_fit->size()];}
649 inline cvm::real& fit_gradients_z(size_t i) {
650 atom_group *group_for_fit = fitting_group ? fitting_group : this;
651 return group_for_fit->fit_gradients[i + 2 * group_for_fit->size()];}
652 inline const cvm::real& fit_gradients_x(size_t i) const {
653 const atom_group *group_for_fit = fitting_group ? fitting_group : this;
654 return group_for_fit->fit_gradients[i];}
655 inline const cvm::real& fit_gradients_y(size_t i) const {
656 const atom_group *group_for_fit = fitting_group ? fitting_group : this;
657 return group_for_fit->fit_gradients[i + group_for_fit->size()];}
658 inline const cvm::real& fit_gradients_z(size_t i) const {
659 const atom_group *group_for_fit = fitting_group ? fitting_group : this;
660 return group_for_fit->fit_gradients[i + 2 * group_for_fit->size()];}
662
666 inline cvm::real& group_forces_x(size_t i) {return group_forces[i];}
667 inline cvm::real& group_forces_y(size_t i) {return group_forces[i + num_atoms];}
668 inline cvm::real& group_forces_z(size_t i) {return group_forces[i + 2 * num_atoms];}
669 inline const cvm::real& group_forces_x(size_t i) const {return group_forces[i];}
670 inline const cvm::real& group_forces_y(size_t i) const {return group_forces[i + num_atoms];}
671 inline const cvm::real& group_forces_z(size_t i) const {return group_forces[i + 2 * num_atoms];}
673
676 inline simple_atom operator[](size_t i) const {
677 return simple_atom{
678 /*.proxy_index = */atoms_index[i],
679 /*.id = */atoms_ids[i],
680 /*.mass = */atoms_mass[i],
681 /*.charge = */atoms_charge[i],
682 /*.pos = */{pos_x(i), pos_y(i), pos_z(i)},
683 /*.vel = */{vel_x(i), vel_y(i), vel_z(i)},
684 /*.total_force = */{total_force_x(i), total_force_y(i), total_force_z(i)},
685 /*.grad = */{grad_x(i), grad_y(i), grad_z(i)}
686 };
687 }
688
696 public:
729 void add_atom_force(size_t i, const cvm::rvector& force);
730 private:
731 cvm::atom_group* m_ag;
732 cvm::atom_group* m_group_for_fit;
733 bool m_has_fitting_force;
734 void apply_force_with_fitting_group();
735 };
736 group_force_object get_group_force_object();
737public:
739 std::string name;
745 // TODO Make this field part of the data structures that link a group to a CVC
746 std::string key;
760 cvm::ag_vector_real_t fit_gradients;
768 static std::vector<feature *> ag_features;
769private:
771 size_t num_atoms;
773 std::vector<int> atoms_index;
775 cvm::ag_vector_real_t atoms_pos;
777 std::vector<cvm::real> atoms_charge;
779 std::vector<cvm::real> atoms_vel;
781 std::vector<cvm::real> atoms_mass;
783 cvm::ag_vector_real_t atoms_grad;
785 std::vector<cvm::real> atoms_total_force;
787 std::vector<cvm::real> atoms_weight;
789 std::vector<int> atoms_ids;
792 std::vector<int> sorted_atoms_ids;
794 std::vector<int> sorted_atoms_ids_map;
798 int index;
802 cvm::ag_vector_real_t group_forces;
804 cvm::ag_vector_real_t ref_pos;
805 size_t num_ref_pos; // TODO: Do I really need this?
815 cvm::ag_vector_real_t atoms_pos_unrotated;
819 // TODO for scalable calculations of more complex variables (e.g. rotation),
820 // use a colvarvalue of vectors to hold the entire derivative
825 std::mutex modify_lock;
826#if defined(COLVARS_CUDA) || defined (COLVARS_HIP)
827 std::unique_ptr<colvars_gpu::colvaratoms_gpu> gpu_atom_group;
828 friend class colvars_gpu::colvaratoms_gpu;
829public:
830 std::unique_ptr<colvars_gpu::colvaratoms_gpu>& get_gpu_atom_group() {
831 return gpu_atom_group;
832 }
833#elif defined (COLVARS_SYCL)
834 // TODO
835#endif
836};
837
838#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:901
A rotation between two sets of coordinates (for the moment a wrapper for colvarmodule::quaternion)
Definition: colvartypes.h:1366
vector of real numbers with three components
Definition: colvartypes.h:726
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:98
int residue_id
Residue identifier.
Definition: colvarmodule.h:223
Base class containing parsing functions; all objects which need to parse input inherit from this.
Definition: colvarparse.h:27
Definition: colvarproxy.h:567
The main class for calculating the atom group properties on GPU.
Definition: colvaratoms_gpu.h:95
The temporary AoS interface to modify cvm::atom_group.
Definition: colvaratoms.h:156
cvm::real m_total_mass
Total mass of the atom group.
Definition: colvaratoms.h:177
std::vector< int > m_atoms_ids
Internal atom IDs (populated during initialization)
Definition: colvaratoms.h:165
std::vector< simple_atom > m_atoms
AoS layout of the atoms.
Definition: colvaratoms.h:169
cvm::atom_group * m_ag
Pointer to the SoA atom group to be modified.
Definition: colvaratoms.h:161
std::unordered_map< int, int > m_atoms_ids_count
A map to avoid doubly counting atoms.
Definition: colvaratoms.h:173
void sync_to_soa() const
Synchronize the atoms from this temporary AoS object to m_ag.
Definition: colvaratoms.cpp:279
int remove_atom(atom_iter ai)
Remove an atom object from this group.
Definition: colvaratoms.cpp:354
cvm::real m_total_charge
Total charge of the atom group.
Definition: colvaratoms.h:181
void update_from_soa()
Populate this temporary AoS object with the atoms from m_ag.
Definition: colvaratoms.cpp:266
A helper class for applying forces on an atom group in a way that is aware of the fitting group....
Definition: colvaratoms.h:695
void add_atom_force(size_t i, const cvm::rvector &force)
Apply force to atom i.
Definition: colvaratoms.cpp:1805
Store the information of a group of atoms in a structure-of-arrays (SoA) style.
Definition: colvaratoms.h:52
cvm::atom_pos center_of_geometry() const
Return the center of geometry of the atomic positions.
Definition: colvaratoms.h:380
cvm::real total_mass
Total mass of the atom group.
Definition: colvaratoms.h:762
cvm::atom_pos ref_pos_cog
Center of geometry of the reference coordinates; regardless of whether f_ag_center is true,...
Definition: colvaratoms.h:809
std::vector< int > atoms_index
SOA atom proxy indices (size: num_atoms)
Definition: colvaratoms.h:773
std::vector< cvm::real > atoms_mass
SOA atom mass (size: num_atoms)
Definition: colvaratoms.h:781
cvm::ag_vector_real_t group_forces
The temporary forces acting on the main group atoms. Currently this is only used for calculating the ...
Definition: colvaratoms.h:802
std::mutex modify_lock
Lock for modifier.
Definition: colvaratoms.h:825
cvm::rvector dipole() const
Return the (previously calculated) dipole of the atom group.
Definition: colvaratoms.h:419
std::string key
Keyword used to define the group.
Definition: colvaratoms.h:746
int init()
Set default values for common flags.
Definition: colvaratoms.cpp:152
simple_atom operator[](size_t i) const
Read-only operator[].
Definition: colvaratoms.h:676
bool b_user_defined_fit
Indicates that the user has explicitly set centerToReference or rotateReference, and the correspondin...
Definition: colvaratoms.h:758
cvm::rvector dip
Dipole moment of the atom group.
Definition: colvaratoms.h:823
std::vector< int > const & ids() const
Internal atom IDs (populated during initialization)
Definition: colvaratoms.h:288
int init_dependencies() override
Initialize dependency tree.
Definition: colvaratoms.cpp:186
cvm::rvector center_of_mass_scalar_gradient() const
Return previously gradient of scalar variable with respect to the COM.
Definition: colvaratoms.h:400
std::vector< cvm::real > atoms_total_force
SOA atom total forces (size: 3 * num_atoms)
Definition: colvaratoms.h:785
static std::vector< feature * > ag_features
Implementation of the feature list for atom group.
Definition: colvaratoms.h:768
cvm::rotation rot
The rotation calculated automatically if f_ag_rotate is defined.
Definition: colvaratoms.h:751
atom_group()
Default constructor.
Definition: colvaratoms.cpp:96
std::vector< cvm::real > atoms_weight
Atom masses divided by total mass (size: num_atoms)
Definition: colvaratoms.h:787
cvm::ag_vector_real_t ref_pos
use reference coordinates for f_ag_center or f_ag_rotate
Definition: colvaratoms.h:804
std::vector< int > atoms_ids
Internal atom IDs for host code.
Definition: colvaratoms.h:789
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:792
cvm::atom_pos dummy_atom_pos
Dummy atom position.
Definition: colvaratoms.h:796
cvm::atom_pos com
Center of mass.
Definition: colvaratoms.h:817
cvm::atom_pos center_of_mass() const
Return the center of mass (COM) of the atomic positions.
Definition: colvaratoms.h:392
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:743
cvm::ag_vector_real_t atoms_pos
SOA atom positions (size: 3 * num_atoms)
Definition: colvaratoms.h:775
size_t size() const
Return the number of atoms of this group.
Definition: colvaratoms.h:536
const std::vector< feature * > & features() const override
Implementation of the feature list accessor for atom group.
Definition: colvaratoms.h:94
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:26
bool noforce
Don't apply any force on this group (use its coordinates only to calculate a colvar)
Definition: colvaratoms.h:754
std::string name
Optional name to reuse properties of this in other groups.
Definition: colvaratoms.h:739
cvm::ag_vector_real_t atoms_pos_unrotated
Unrotated atom positions for fit gradients.
Definition: colvaratoms.h:815
std::vector< int > const & sorted_ids() const
Sorted internal atom IDs (populated on-demand by create_sorted_ids);.
Definition: colvaratoms.h:302
size_t num_atoms
Number of atoms.
Definition: colvaratoms.h:771
int index
Index in the colvarproxy arrays (if the group is scalable)
Definition: colvaratoms.h:798
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:749
cvm::atom_pos cog_orig
Center of geometry before any fitting.
Definition: colvaratoms.h:813
cvm::atom_pos cog
Center of geometry.
Definition: colvaratoms.h:811
cvm::rvector scalar_com_gradient
The derivative of a scalar variable with respect to the COM.
Definition: colvaratoms.h:821
cvm::ag_vector_real_t atoms_grad
SOA atom gradients (size: 3 * num_atoms)
Definition: colvaratoms.h:783
std::vector< int > sorted_atoms_ids_map
Map entries of sorted_atoms_ids onto the original positions in the group.
Definition: colvaratoms.h:794
std::vector< cvm::real > atoms_charge
SOA atom charges (size: num_atoms)
Definition: colvaratoms.h:777
std::vector< cvm::real > atoms_vel
SOA atom velocities (size: 3 * num_atoms)
Definition: colvaratoms.h:779
cvm::real total_charge
Total charge of the atom group.
Definition: colvaratoms.h:764
atom_modifier get_atom_modifier()
Get the atom modifier object associated with this SoA atom group.
Definition: colvaratoms.h:243
rotation_derivative * rot_deriv
Rotation derivative;.
Definition: colvaratoms.h:766
int setup()
Update data required to calculate cvc's.
Definition: colvaratoms.cpp:246
static ag_vector_real_t 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:12
std::vector< int > const & sorted_ids_map() const
Map entries of sorted_atoms_ids onto the original positions in the group.
Definition: colvaratoms.h:307
cvm::ag_vector_real_t fit_gradients
Derivatives of the fitting transformation.
Definition: colvaratoms.h:760
Declaration of the class for calculating atom group properties on GPU.
Collective variables main module.
A simplified class of cvm::atom that can be used with cvm::atom_group::atom_modifier.
Definition: colvaratoms.h:107
Helper class for calculating the derivative of rotation.
Definition: colvar_rotation_derivative.h:42