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
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;
96 int proxy_gpu_buffers_reallocated() override;
98 virtual const std::vector<feature *> &features() const override { return ag_features; }
99 std::vector<feature *> &modify_features() override { return ag_features; }
100 static void delete_features()
101 {
102 for (size_t i = 0; i < ag_features.size(); i++) {
103 delete ag_features[i];
104 }
105 ag_features.clear();
106 }
111 struct simple_atom {
112 int proxy_index;
113 int id;
114 cvm::real mass;
115 cvm::real charge;
116 cvm::atom_pos pos;
117 cvm::rvector vel;
118 cvm::rvector total_force;
119 cvm::rvector grad;
120 };
131 colvarproxy* const p,
132 cvm::residue_id const &residue,
133 std::string const &atom_name,
134 std::string const &segment_id);
142 colvarproxy* const p,
143 int atom_number);
155 colvarproxy* const p,
156 const simple_atom& atom);
161 private:
169 std::vector<int> m_atoms_ids;
173 std::vector<simple_atom> m_atoms;
177 std::unordered_map<int, int> m_atoms_ids_count;
189 void update_from_soa();
193 void sync_to_soa() const;
194 public:
195 using atom_iter = decltype(m_atoms)::iterator;
196 using const_atom_iter = decltype(m_atoms)::const_iterator;
213 inline simple_atom & operator [] (size_t const i){return m_atoms[i];}
214 inline simple_atom const & operator [] (size_t const i) const {return m_atoms[i];}
215 inline atom_iter begin(){return m_atoms.begin();}
216 inline const_atom_iter begin() const {return m_atoms.begin();}
217 inline atom_iter end() {return m_atoms.end();}
218 inline const_atom_iter end() const {return m_atoms.end();}
219 inline size_t size() const {return m_atoms.size();}
221
225 int add_atom(simple_atom const &a);
226 int add_atom_numbers(std::string const &numbers_conf);
227 int add_atoms_of_group(const atom_group *ag);
228 int add_index_group(std::string const &index_group_name, bool silent = false);
229 int add_atom_numbers_range(std::string const &range_conf);
230 int add_atom_name_residue_range(std::string const &psf_segid,
231 std::string const &range_conf);
232 int add_atom_id(int aid);
234
237 int remove_atom(atom_iter ai);
238 };
248 return atom_modifier(this);
249 }
257 void clear_soa();
261 int parse(std::string const &conf);
265 int parse_fitting_options(std::string const &group_conf);
269 int set_dummy();
273 int set_dummy_pos(cvm::atom_pos const &pos);
277 void update_total_mass();
281 void update_total_charge();
288 void print_properties(std::string const &colvar_name, int i, int j);
292 inline std::vector<int> const &ids() const
293 {
294 return atoms_ids;
295 }
296 std::string const print_atom_ids() const;
300 int create_sorted_ids();
306 inline std::vector<int> const &sorted_ids() const
307 {
308 return sorted_atoms_ids;
309 }
311 inline std::vector<int> const &sorted_ids_map() const
312 {
314 }
320 static int overlap(const atom_group &g1, const atom_group &g2);
324 void read_positions();
328 void calc_apply_roto_translation();
333 void setup_rotation_derivative();
343 void center_ref_pos();
347 void rotate(const cvm::rmatrix& rot_mat);
351 void apply_translation(cvm::rvector const &t);
359 void read_velocities();
367 void read_total_forces();
371 int calc_required_properties();
376 int calc_center_of_geometry();
380 cvm::ag_vector_real_t positions() const;
385 {
386 return cog;
387 }
392 int calc_center_of_mass();
397 {
398 return com;
399 }
405 {
406 return scalar_com_gradient;
407 }
411 int positions_shifted(cvm::rvector const &shift, cvm::ag_vector_real_t& out) const;
415 std::vector<cvm::real> velocities() const;
419 int calc_dipole(cvm::atom_pos const &dipole_center);
423 inline cvm::rvector dipole() const
424 {
425 return dip;
426 }
430 std::vector<cvm::real> total_forces() const;
434 cvm::rvector total_force() const;
440 void set_weighted_gradient(cvm::rvector const &grad);
444 void calc_fit_gradients();
476 template <bool B_ag_center, bool B_ag_rotate,
477 typename main_force_accessor_T,
478 typename fitting_force_accessor_T>
479 void calc_fit_forces_impl(
480 main_force_accessor_T accessor_main,
481 fitting_force_accessor_T accessor_fitting) const;
482
498 template <typename main_force_accessor_T, typename fitting_force_accessor_T>
499 void calc_fit_forces(
500 main_force_accessor_T accessor_main,
501 fitting_force_accessor_T accessor_fitting) const;
516 void apply_colvar_force(cvm::real const &force);
530 void apply_force(cvm::rvector const &force);
536 void do_feature_side_effects(int id) override;
540 size_t size() const {return num_atoms;}
544 void reset_atoms_data();
548 void set_ref_pos_from_aos(const std::vector<cvm::atom_pos>& pos_aos);
553 inline int& id(size_t i) {return atoms_ids[i];}
554 inline const int& id(size_t i) const {return atoms_ids[i];}
556
560 inline cvm::real& pos_x(size_t i) {return atoms_pos[i];}
561 inline cvm::real& pos_y(size_t i) {return atoms_pos[i + num_atoms];}
562 inline cvm::real& pos_z(size_t i) {return atoms_pos[i + 2 * num_atoms];}
563 inline const cvm::real& pos_x(size_t i) const {return atoms_pos[i];}
564 inline const cvm::real& pos_y(size_t i) const {return atoms_pos[i + num_atoms];}
565 inline const cvm::real& pos_z(size_t i) const {return atoms_pos[i + 2 * num_atoms];}
567
571 inline cvm::real& vel_x(size_t i) {return atoms_vel[i];}
572 inline cvm::real& vel_y(size_t i) {return atoms_vel[i + num_atoms];}
573 inline cvm::real& vel_z(size_t i) {return atoms_vel[i + 2 * num_atoms];}
574 inline const cvm::real& vel_x(size_t i) const {return atoms_vel[i];}
575 inline const cvm::real& vel_y(size_t i) const {return atoms_vel[i + num_atoms];}
576 inline const cvm::real& vel_z(size_t i) const {return atoms_vel[i + 2 * num_atoms];}
578
582 inline cvm::real& grad_x(size_t i) {return atoms_grad[i];}
583 inline cvm::real& grad_y(size_t i) {return atoms_grad[i + num_atoms];}
584 inline cvm::real& grad_z(size_t i) {return atoms_grad[i + 2 * num_atoms];}
585 inline const cvm::real& grad_x(size_t i) const {return atoms_grad[i];}
586 inline const cvm::real& grad_y(size_t i) const {return atoms_grad[i + num_atoms];}
587 inline const cvm::real& grad_z(size_t i) const {return atoms_grad[i + 2 * num_atoms];}
589
593 inline cvm::real& total_force_x(size_t i) {return atoms_total_force[i];}
594 inline cvm::real& total_force_y(size_t i) {return atoms_total_force[i + num_atoms];}
595 inline cvm::real& total_force_z(size_t i) {return atoms_total_force[i + 2 * num_atoms];}
596 inline const cvm::real& total_force_x(size_t i) const {return atoms_total_force[i];}
597 inline const cvm::real& total_force_y(size_t i) const {return atoms_total_force[i + num_atoms];}
598 inline const cvm::real& total_force_z(size_t i) const {return atoms_total_force[i + 2 * num_atoms];}
600
604 inline cvm::real& ref_pos_x(size_t i) {return ref_pos[i];}
605 inline cvm::real& ref_pos_y(size_t i) {return ref_pos[i + num_ref_pos];}
606 inline cvm::real& ref_pos_z(size_t i) {return ref_pos[i + 2 * num_ref_pos];}
607 inline const cvm::real& ref_pos_x(size_t i) const {return ref_pos[i];}
608 inline const cvm::real& ref_pos_y(size_t i) const {return ref_pos[i + num_ref_pos];}
609 inline const cvm::real& ref_pos_z(size_t i) const {return ref_pos[i + 2 * num_ref_pos];}
611
615 inline cvm::real& pos_unrotated_x(size_t i) {return atoms_pos_unrotated[i];}
616 inline cvm::real& pos_unrotated_y(size_t i) {return atoms_pos_unrotated[i + num_atoms];}
617 inline cvm::real& pos_unrotated_z(size_t i) {return atoms_pos_unrotated[i + 2 * num_atoms];}
618 inline const cvm::real& pos_unrotated_x(size_t i) const {return atoms_pos_unrotated[i];}
619 inline const cvm::real& pos_unrotated_y(size_t i) const {return atoms_pos_unrotated[i + num_atoms];}
620 inline const cvm::real& pos_unrotated_z(size_t i) const {return atoms_pos_unrotated[i + 2 * num_atoms];}
622
626 inline cvm::real& mass(size_t i) {return atoms_mass[i];}
627 inline const cvm::real& mass(size_t i) const {return atoms_mass[i];}
629
633 inline cvm::real& weight(size_t i) {return atoms_weight[i];}
634 inline const cvm::real& weight(size_t i) const {return atoms_weight[i];}
636
640 inline cvm::real& charge(size_t i) {return atoms_charge[i];}
641 inline const cvm::real& charge(size_t i) const {return atoms_charge[i];}
643
647 inline cvm::real& fit_gradients_x(size_t i) {
648 atom_group *group_for_fit = fitting_group ? fitting_group : this;
649 return group_for_fit->fit_gradients[i];}
650 inline cvm::real& fit_gradients_y(size_t i) {
651 atom_group *group_for_fit = fitting_group ? fitting_group : this;
652 return group_for_fit->fit_gradients[i + group_for_fit->size()];}
653 inline cvm::real& fit_gradients_z(size_t i) {
654 atom_group *group_for_fit = fitting_group ? fitting_group : this;
655 return group_for_fit->fit_gradients[i + 2 * group_for_fit->size()];}
656 inline const cvm::real& fit_gradients_x(size_t i) const {
657 const atom_group *group_for_fit = fitting_group ? fitting_group : this;
658 return group_for_fit->fit_gradients[i];}
659 inline const cvm::real& fit_gradients_y(size_t i) const {
660 const atom_group *group_for_fit = fitting_group ? fitting_group : this;
661 return group_for_fit->fit_gradients[i + group_for_fit->size()];}
662 inline const cvm::real& fit_gradients_z(size_t i) const {
663 const atom_group *group_for_fit = fitting_group ? fitting_group : this;
664 return group_for_fit->fit_gradients[i + 2 * group_for_fit->size()];}
666
670 inline cvm::real& group_forces_x(size_t i) {return group_forces[i];}
671 inline cvm::real& group_forces_y(size_t i) {return group_forces[i + num_atoms];}
672 inline cvm::real& group_forces_z(size_t i) {return group_forces[i + 2 * num_atoms];}
673 inline const cvm::real& group_forces_x(size_t i) const {return group_forces[i];}
674 inline const cvm::real& group_forces_y(size_t i) const {return group_forces[i + num_atoms];}
675 inline const cvm::real& group_forces_z(size_t i) const {return group_forces[i + 2 * num_atoms];}
677
680 inline simple_atom operator[](size_t i) const {
681 return simple_atom{
682 /*.proxy_index = */atoms_index[i],
683 /*.id = */atoms_ids[i],
684 /*.mass = */atoms_mass[i],
685 /*.charge = */atoms_charge[i],
686 /*.pos = */{pos_x(i), pos_y(i), pos_z(i)},
687 /*.vel = */{vel_x(i), vel_y(i), vel_z(i)},
688 /*.total_force = */{total_force_x(i), total_force_y(i), total_force_z(i)},
689 /*.grad = */{grad_x(i), grad_y(i), grad_z(i)}
690 };
691 }
692
700 public:
733 void add_atom_force(size_t i, const cvm::rvector& force);
734 private:
735 cvm::atom_group* m_ag;
736 cvm::atom_group* m_group_for_fit;
737 bool m_has_fitting_force;
738 void apply_force_with_fitting_group();
739 };
740 group_force_object get_group_force_object();
741public:
743 std::string name;
749 // TODO Make this field part of the data structures that link a group to a CVC
750 std::string key;
764 cvm::ag_vector_real_t fit_gradients;
772 static std::vector<feature *> ag_features;
773private:
775 size_t num_atoms;
777 std::vector<int> atoms_index;
779 cvm::ag_vector_real_t atoms_pos;
781 std::vector<cvm::real> atoms_charge;
783 std::vector<cvm::real> atoms_vel;
785 std::vector<cvm::real> atoms_mass;
787 cvm::ag_vector_real_t atoms_grad;
789 std::vector<cvm::real> atoms_total_force;
791 std::vector<cvm::real> atoms_weight;
793 std::vector<int> atoms_ids;
796 std::vector<int> sorted_atoms_ids;
798 std::vector<int> sorted_atoms_ids_map;
802 int index;
806 cvm::ag_vector_real_t group_forces;
808 cvm::ag_vector_real_t ref_pos;
809 size_t num_ref_pos; // TODO: Do I really need this?
819 cvm::ag_vector_real_t atoms_pos_unrotated;
823 // TODO for scalable calculations of more complex variables (e.g. rotation),
824 // use a colvarvalue of vectors to hold the entire derivative
829 std::mutex modify_lock;
830#if defined(COLVARS_CUDA) || defined (COLVARS_HIP)
831 std::unique_ptr<colvars_gpu::colvaratoms_gpu> gpu_atom_group;
832 friend class colvars_gpu::colvaratoms_gpu;
833public:
834 std::unique_ptr<colvars_gpu::colvaratoms_gpu>& get_gpu_atom_group() {
835 return gpu_atom_group;
836 }
837#elif defined (COLVARS_SYCL)
838 // TODO
839#endif
840};
841
842#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:903
A rotation between two sets of coordinates (for the moment a wrapper for colvarmodule::quaternion)
Definition: colvartypes.h:1368
vector of real numbers with three components
Definition: colvartypes.h:728
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:98
int residue_id
Residue identifier.
Definition: colvarmodule.h:247
Definition: colvarproxy.h:564
The main class for calculating the atom group properties on GPU.
Definition: colvaratoms_gpu.h:85
The temporary AoS interface to modify cvm::atom_group.
Definition: colvaratoms.h:160
cvm::real m_total_mass
Total mass of the atom group.
Definition: colvaratoms.h:181
std::vector< int > m_atoms_ids
Internal atom IDs (populated during initialization)
Definition: colvaratoms.h:169
std::vector< simple_atom > m_atoms
AoS layout of the atoms.
Definition: colvaratoms.h:173
cvm::atom_group * m_ag
Pointer to the SoA atom group to be modified.
Definition: colvaratoms.h:165
std::unordered_map< int, int > m_atoms_ids_count
A map to avoid doubly counting atoms.
Definition: colvaratoms.h:177
void sync_to_soa() const
Synchronize the atoms from this temporary AoS object to m_ag.
Definition: colvaratoms.cpp:292
int remove_atom(atom_iter ai)
Remove an atom object from this group.
Definition: colvaratoms.cpp:367
cvm::real m_total_charge
Total charge of the atom group.
Definition: colvaratoms.h:185
void update_from_soa()
Populate this temporary AoS object with the atoms from m_ag.
Definition: colvaratoms.cpp:279
A helper class for applying forces on an atom group in a way that is aware of the fitting group....
Definition: colvaratoms.h:699
void add_atom_force(size_t i, const cvm::rvector &force)
Apply force to atom i.
Definition: colvaratoms.cpp:1831
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:384
cvm::real total_mass
Total mass of the atom group.
Definition: colvaratoms.h:766
cvm::atom_pos ref_pos_cog
Center of geometry of the reference coordinates; regardless of whether f_ag_center is true,...
Definition: colvaratoms.h:813
std::vector< int > atoms_index
SOA atom proxy indices (size: num_atoms)
Definition: colvaratoms.h:777
std::vector< cvm::real > atoms_mass
SOA atom mass (size: num_atoms)
Definition: colvaratoms.h:785
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:806
std::mutex modify_lock
Lock for modifier.
Definition: colvaratoms.h:829
cvm::rvector dipole() const
Return the (previously calculated) dipole of the atom group.
Definition: colvaratoms.h:423
std::string key
Keyword used to define the group.
Definition: colvaratoms.h:750
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:680
bool b_user_defined_fit
Indicates that the user has explicitly set centerToReference or rotateReference, and the correspondin...
Definition: colvaratoms.h:762
cvm::rvector dip
Dipole moment of the atom group.
Definition: colvaratoms.h:827
std::vector< int > const & ids() const
Internal atom IDs (populated during initialization)
Definition: colvaratoms.h:292
int init_dependencies() override
Initialize dependency tree.
Definition: colvaratoms.cpp:191
cvm::rvector center_of_mass_scalar_gradient() const
Return previously gradient of scalar variable with respect to the COM.
Definition: colvaratoms.h:404
std::vector< cvm::real > atoms_total_force
SOA atom total forces (size: 3 * num_atoms)
Definition: colvaratoms.h:789
static std::vector< feature * > ag_features
Implementation of the feature list for atom group.
Definition: colvaratoms.h:772
cvm::rotation rot
The rotation calculated automatically if f_ag_rotate is defined.
Definition: colvaratoms.h:755
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:791
cvm::ag_vector_real_t ref_pos
use reference coordinates for f_ag_center or f_ag_rotate
Definition: colvaratoms.h:808
std::vector< int > atoms_ids
Internal atom IDs for host code.
Definition: colvaratoms.h:793
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:796
cvm::atom_pos dummy_atom_pos
Dummy atom position.
Definition: colvaratoms.h:800
cvm::atom_pos com
Center of mass.
Definition: colvaratoms.h:821
cvm::atom_pos center_of_mass() const
Return the center of mass (COM) of the atomic positions.
Definition: colvaratoms.h:396
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:747
cvm::ag_vector_real_t atoms_pos
SOA atom positions (size: 3 * num_atoms)
Definition: colvaratoms.h:779
size_t size() const
Return the number of atoms of this group.
Definition: colvaratoms.h:540
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:758
int proxy_gpu_buffers_reallocated() override
Reset the GPU graphs when the GPU buffers in proxy are re-allocated.
Definition: colvaratoms.cpp:251
std::string name
Optional name to reuse properties of this in other groups.
Definition: colvaratoms.h:743
cvm::ag_vector_real_t atoms_pos_unrotated
Unrotated atom positions for fit gradients.
Definition: colvaratoms.h:819
std::vector< int > const & sorted_ids() const
Sorted internal atom IDs (populated on-demand by create_sorted_ids);.
Definition: colvaratoms.h:306
size_t num_atoms
Number of atoms.
Definition: colvaratoms.h:775
int index
Index in the colvarproxy arrays (if the group is scalable)
Definition: colvaratoms.h:802
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:753
cvm::atom_pos cog_orig
Center of geometry before any fitting.
Definition: colvaratoms.h:817
cvm::atom_pos cog
Center of geometry.
Definition: colvaratoms.h:815
cvm::rvector scalar_com_gradient
The derivative of a scalar variable with respect to the COM.
Definition: colvaratoms.h:825
cvm::ag_vector_real_t atoms_grad
SOA atom gradients (size: 3 * num_atoms)
Definition: colvaratoms.h:787
std::vector< int > sorted_atoms_ids_map
Map entries of sorted_atoms_ids onto the original positions in the group.
Definition: colvaratoms.h:798
std::vector< cvm::real > atoms_charge
SOA atom charges (size: num_atoms)
Definition: colvaratoms.h:781
virtual const std::vector< feature * > & features() const override
Implementation of the feature list accessor for atom group.
Definition: colvaratoms.h:98
std::vector< cvm::real > atoms_vel
SOA atom velocities (size: 3 * num_atoms)
Definition: colvaratoms.h:783
cvm::real total_charge
Total charge of the atom group.
Definition: colvaratoms.h:768
atom_modifier get_atom_modifier()
Get the atom modifier object associated with this SoA atom group.
Definition: colvaratoms.h:247
rotation_derivative * rot_deriv
Rotation derivative;.
Definition: colvaratoms.h:770
int setup()
Update data required to calculate cvc's.
Definition: colvaratoms.cpp:259
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:311
cvm::ag_vector_real_t fit_gradients
Derivatives of the fitting transformation.
Definition: colvaratoms.h:764
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:111
Helper class for calculating the derivative of rotation.
Definition: colvar_rotation_derivative.h:42