Collective Variables Module - Developer Documentation
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
colvarvalue.h
1 // -*- c++ -*-
2 
3 #ifndef COLVARVALUE_H
4 #define COLVARVALUE_H
5 
6 #include "colvarmodule.h"
7 #include "colvartypes.h"
8 
9 
32 
33 
34 class colvarvalue {
35 
36 public:
37 
43  enum Type {
62  };
63 
66 
69 
72 
75 
78 
81  std::vector<Type> elem_types;
82 
85  std::vector<int> elem_indices;
86 
89  std::vector<int> elem_sizes;
90 
92  static inline bool type_checking()
93  {
94  return true;
95  }
96 
98  static std::string const type_desc(Type t);
99 
101  static std::string const type_keyword(Type t);
102 
104  static size_t num_df(Type t);
105 
107  static size_t num_dimensions(Type t);
108 
110  size_t size() const;
111 
114  inline colvarvalue()
116  {}
117 
119  inline colvarvalue(Type const &vti)
120  : value_type(vti)
121  {
122  reset();
123  }
124 
126  inline colvarvalue(cvm::real const &x)
128  {}
129 
133  inline colvarvalue(cvm::rvector const &v, Type vti = type_3vector)
134  : value_type(vti), rvector_value(v)
135  {}
136 
139  : value_type(vti), quaternion_value(q)
140  {}
141 
144 
146  colvarvalue(colvarvalue const &x);
147 
148 
150  void reset();
151 
156  void apply_constraints();
157 
159  inline Type type() const
160  {
161  return value_type;
162  }
163 
165  void type(Type const &vti);
166 
168  void type(colvarvalue const &x);
169 
172  void is_derivative();
173 
175  cvm::real norm2() const;
176 
178  inline cvm::real norm() const
179  {
180  return std::sqrt(this->norm2());
181  }
182 
184  cvm::real dist2(colvarvalue const &x2) const;
185 
187  colvarvalue dist2_grad(colvarvalue const &x2) const;
188 
190  colvarvalue & operator = (colvarvalue const &x);
191 
192  void operator += (colvarvalue const &x);
193  void operator -= (colvarvalue const &x);
194  void operator *= (cvm::real const &a);
195  void operator /= (cvm::real const &a);
196 
197  // Binary operators (return values)
198  friend colvarvalue operator + (colvarvalue const &x1, colvarvalue const &x2);
199  friend colvarvalue operator - (colvarvalue const &x1, colvarvalue const &x2);
200  friend colvarvalue operator * (colvarvalue const &x, cvm::real const &a);
201  friend colvarvalue operator * (cvm::real const &a, colvarvalue const &x);
202  friend colvarvalue operator / (colvarvalue const &x, cvm::real const &a);
203 
205  friend cvm::real operator * (colvarvalue const &x1, colvarvalue const &x2);
206 
207  // Cast to scalar
208  inline operator cvm::real() const
209  {
210  if (value_type != type_scalar) {
211  cvm::error("Error: trying to use a variable of type \""+
212  type_desc(value_type)+"\" as one of type \""+
213  type_desc(type_scalar)+"\".\n");
214  }
215  return real_value;
216  }
217 
218  // Cast to 3-vector
219  inline operator cvm::rvector() const
220  {
221  if ((value_type != type_3vector) &&
224  cvm::error("Error: trying to use a variable of type \""+
225  type_desc(value_type)+"\" as one of type \""+
226  type_desc(type_3vector)+"\".\n");
227  }
228  return rvector_value;
229  }
230 
231  // Cast to quaternion
232  inline operator cvm::quaternion() const
233  {
234  if ((value_type != type_quaternion) &&
236  cvm::error("Error: trying to use a variable of type \""+
237  type_desc(value_type)+"\" as one of type \""+
238  type_desc(type_quaternion)+"\".\n");
239  }
240  return quaternion_value;
241  }
242 
243  // Create a n-dimensional vector from one of the basic types, or return the existing vector
244  cvm::vector1d<cvm::real> const as_vector() const;
245 
246 
248  inline bool is_scalar() const
249  {
250  return (value_type == type_scalar);
251  }
252 
253 
257  void add_elem(colvarvalue const &x);
258 
260  colvarvalue const get_elem(int const i_begin, int const i_end, Type const vt) const;
261 
263  void set_elem(int const i_begin, int const i_end, colvarvalue const &x);
264 
266  void set_random();
267 
269  colvarvalue const get_elem(int const icv) const;
270 
272  void set_elem(int const icv, colvarvalue const &x);
273 
275  inline cvm::real operator [] (int const i) const
276  {
277  if (vector1d_value.size() > 0) {
278  return vector1d_value[i];
279  } else {
280  cvm::error("Error: trying to use as a vector a variable that is not initialized as such.\n");
281  return 0.0;
282  }
283  }
284 
286  inline cvm::real & operator [] (int const i)
287  {
288  if (vector1d_value.size() > 0) {
289  return vector1d_value[i];
290  } else {
291  cvm::error("Error: trying to use as a vector a variable that is not initialized as such.\n");
292  real_value = 0.0;
293  return real_value;
294  }
295  }
296 
297 
299  int static check_types(colvarvalue const &x1, colvarvalue const &x2);
300 
302  int static check_types_assign(Type const &vt1, Type const &vt2);
303 
305  void undef_op() const;
306 
307 
309  friend std::ostream & operator << (std::ostream &os, colvarvalue const &q);
310 
312  friend std::istream & operator >> (std::istream &is, colvarvalue &q);
313 
317  size_t output_width(size_t const &real_width) const;
318 
320  std::string to_simple_string() const;
321 
323  int from_simple_string(std::string const &s);
324 
325 
326  // optimized routines for operations on arrays of colvar values;
327  // xv and result are assumed to have the same number of elements
328 
331  void static inner_opt(colvarvalue const &x,
332  std::vector<colvarvalue>::iterator &xv,
333  std::vector<colvarvalue>::iterator const &xv_end,
334  std::vector<cvm::real>::iterator &result);
335 
338  void static inner_opt(colvarvalue const &x,
339  std::list<colvarvalue>::iterator &xv,
340  std::list<colvarvalue>::iterator const &xv_end,
341  std::vector<cvm::real>::iterator &result);
342 
346  void static p2leg_opt(colvarvalue const &x,
347  std::vector<colvarvalue>::iterator &xv,
348  std::vector<colvarvalue>::iterator const &xv_end,
349  std::vector<cvm::real>::iterator &result);
350 
353  void static p2leg_opt(colvarvalue const &x,
354  std::list<colvarvalue>::iterator &xv,
355  std::list<colvarvalue>::iterator const &xv_end,
356  std::vector<cvm::real>::iterator &result);
357 
358 };
359 
360 
361 inline size_t colvarvalue::size() const
362 {
363  switch (value_type) {
365  default:
366  return 0; break;
368  return 1; break;
372  return 3; break;
375  return 4; break;
377  return vector1d_value.size(); break;
378  }
379 }
380 
381 
383  colvarvalue const &x2)
384 {
386  return COLVARS_OK;
387  }
388 
389  if (x1.type() != x2.type()) {
390  if (((x1.type() == type_unit3vector) &&
391  (x2.type() == type_unit3vectorderiv)) ||
392  ((x2.type() == type_unit3vector) &&
393  (x1.type() == type_unit3vectorderiv)) ||
394  ((x1.type() == type_quaternion) &&
395  (x2.type() == type_quaternionderiv)) ||
396  ((x2.type() == type_quaternion) &&
397  (x1.type() == type_quaternionderiv))) {
398  return COLVARS_OK;
399  } else {
400  cvm::error("Trying to perform an operation between two colvar "
401  "values with different types, \""+
403  "\" and \""+
405  "\".\n");
406  return COLVARS_ERROR;
407  }
408  }
409 
410  if (x1.type() == type_vector) {
411  if (x1.vector1d_value.size() != x2.vector1d_value.size()) {
412  cvm::error("Trying to perform an operation between two vector colvar "
413  "values with different sizes, "+
414  cvm::to_str(x1.vector1d_value.size())+
415  " and "+
416  cvm::to_str(x2.vector1d_value.size())+
417  ".\n");
418  return COLVARS_ERROR;
419  }
420  }
421  return COLVARS_OK;
422 }
423 
424 
426  colvarvalue::Type const &vt2)
427 {
429  return COLVARS_OK;
430  }
431 
432  if (vt1 != type_notset) {
433  if (((vt1 == type_unit3vector) &&
434  (vt2 == type_unit3vectorderiv)) ||
435  ((vt2 == type_unit3vector) &&
436  (vt1 == type_unit3vectorderiv)) ||
437  ((vt1 == type_quaternion) &&
438  (vt2 == type_quaternionderiv)) ||
439  ((vt2 == type_quaternion) &&
440  (vt1 == type_quaternionderiv))) {
441  return COLVARS_OK;
442  } else {
443  if (vt1 != vt2) {
444  cvm::error("Trying to assign a colvar value with type \""+
445  type_desc(vt2)+"\" to one with type \""+
446  type_desc(vt1)+"\".\n");
447  return COLVARS_ERROR;
448  }
449  }
450  }
451  return COLVARS_OK;
452 }
453 
454 
456 {
457  check_types_assign(this->type(), x.type());
458  value_type = x.type();
459 
460  switch (this->type()) {
462  this->real_value = x.real_value;
463  break;
467  this->rvector_value = x.rvector_value;
468  break;
472  break;
478  break;
480  default:
481  undef_op();
482  break;
483  }
484  return *this;
485 }
486 
487 
488 inline void colvarvalue::operator += (colvarvalue const &x)
489 {
490  colvarvalue::check_types(*this, x);
491 
492  switch (this->type()) {
494  this->real_value += x.real_value;
495  break;
499  this->rvector_value += x.rvector_value;
500  break;
504  break;
506  this->vector1d_value += x.vector1d_value;
507  break;
509  default:
510  undef_op();
511  }
512 }
513 
514 
515 inline void colvarvalue::operator -= (colvarvalue const &x)
516 {
517  colvarvalue::check_types(*this, x);
518 
519  switch (value_type) {
521  real_value -= x.real_value;
522  break;
527  break;
531  break;
533  this->vector1d_value -= x.vector1d_value;
534  break;
536  default:
537  undef_op();
538  }
539 }
540 
541 
542 inline void colvarvalue::operator *= (cvm::real const &a)
543 {
544  switch (value_type) {
546  real_value *= a;
547  break;
550  rvector_value *= a;
551  break;
554  quaternion_value *= a;
555  break;
557  this->vector1d_value *= a;
558  break;
560  default:
561  undef_op();
562  }
563 }
564 
565 
566 inline void colvarvalue::operator /= (cvm::real const &a)
567 {
568  switch (value_type) {
570  real_value /= a; break;
574  rvector_value /= a; break;
577  quaternion_value /= a; break;
579  this->vector1d_value /= a;
580  break;
582  default:
583  undef_op();
584  }
585 }
586 
587 
588 inline cvm::vector1d<cvm::real> const colvarvalue::as_vector() const
589 {
590  switch (value_type) {
592  {
594  v[0] = real_value;
595  return v;
596  }
600  return rvector_value.as_vector();
603  return quaternion_value.as_vector();
605  return vector1d_value;
607  default:
608  return cvm::vector1d<cvm::real>(0);
609  }
610 }
611 
612 
614 {
615  switch (value_type) {
617  return (this->real_value)*(this->real_value);
621  return (this->rvector_value).norm2();
624  return (this->quaternion_value).norm2();
626  if (elem_types.size() > 0) {
627  // if we have information about non-scalar types, use it
628  cvm::real result = 0.0;
629  size_t i;
630  for (i = 0; i < elem_types.size(); i++) {
631  result += (this->get_elem(i)).norm2();
632  }
633  return result;
634  } else {
635  return vector1d_value.norm2();
636  }
637  break;
639  default:
640  return 0.0;
641  }
642 }
643 
644 
645 inline cvm::real colvarvalue::dist2(colvarvalue const &x2) const
646 {
647  colvarvalue::check_types(*this, x2);
648 
649  switch (this->type()) {
651  return (this->real_value - x2.real_value)*(this->real_value - x2.real_value);
653  return (this->rvector_value - x2.rvector_value).norm2();
656  // angle between (*this) and x2 is the distance
657  return std::acos(this->rvector_value * x2.rvector_value) * std::acos(this->rvector_value * x2.rvector_value);
660  // angle between (*this) and x2 is the distance, the quaternion
661  // object has it implemented internally
662  return this->quaternion_value.dist2(x2.quaternion_value);
664  return (this->vector1d_value - x2.vector1d_value).norm2();
666  default:
667  this->undef_op();
668  return 0.0;
669  };
670 }
671 
672 
673 #endif
std::string to_simple_string() const
Formats value as a script-friendly string (space separated list)
Definition: colvarvalue.cpp:566
Scalar number, implemented as colvarmodule::real (default)
Definition: colvarvalue.h:47
void apply_constraints()
If the variable has constraints (e.g. unitvector or quaternion), transform it to satisfy them; this f...
Definition: colvarvalue.cpp:131
1-dimensional vector of real numbers with four components and a quaternion algebra ...
Definition: colvartypes.h:1025
colvarvalue()
Default constructor: this class defaults to a scalar number and always behaves like it unless you cha...
Definition: colvarvalue.h:114
Undefined type.
Definition: colvarvalue.h:45
static std::string const type_desc(Type t)
Runtime description of value types.
Definition: colvarvalue.cpp:12
static void inner_opt(colvarvalue const &x, std::vector< colvarvalue >::iterator &xv, std::vector< colvarvalue >::iterator const &xv_end, std::vector< cvm::real >::iterator &result)
Optimized routine for the inner product of one collective variable with an array. ...
Definition: colvarvalue.cpp:717
static void p2leg_opt(colvarvalue const &x, std::vector< colvarvalue >::iterator &xv, std::vector< colvarvalue >::iterator const &xv_end, std::vector< cvm::real >::iterator &result)
Optimized routine for the second order Legendre polynomial, (3cos^2(w)-1)/2, of one collective variab...
Definition: colvarvalue.cpp:799
cvm::real dist2(colvarvalue const &x2) const
Square distance between this colvarvalue and another.
Definition: colvarvalue.h:645
3-dimensional vector that is a derivative of a unitvector
Definition: colvarvalue.h:53
cvm::real real_value
Real data member.
Definition: colvarvalue.h:68
static int check_types_assign(Type const &vt1, Type const &vt2)
Ensure that the two types are the same within an assignment, or that the left side is type_notset...
Definition: colvarvalue.h:425
colvarvalue(Type const &vti)
Constructor from a type specification.
Definition: colvarvalue.h:119
4-dimensional unit vector representing a rotation, implemented as colvarmodule::quaternion ...
Definition: colvarvalue.h:55
std::vector< Type > elem_types
If vector1d_value is a concatenation of colvarvalues, keep track of the individual types...
Definition: colvarvalue.h:81
int from_simple_string(std::string const &s)
Parses value from a script-friendly string (space separated list)
Definition: colvarvalue.cpp:593
bool is_scalar() const
Whether this variable is a real number.
Definition: colvarvalue.h:248
vector of real numbers with three components
Definition: colvartypes.h:709
friend std::istream & operator>>(std::istream &is, colvarvalue &q)
Formatted input operator.
Definition: colvarvalue.cpp:657
3-dimensional unit vector, implemented as colvarmodule::rvector
Definition: colvarvalue.h:51
void add_elem(colvarvalue const &x)
Definition: colvarvalue.cpp:280
void set_random()
Make each element a random number in N(0,1)
Definition: colvarvalue.cpp:341
std::vector< int > elem_indices
If vector1d_value is a concatenation of colvarvalues, these mark the initial components of each colva...
Definition: colvarvalue.h:85
Needed to iterate through enum.
Definition: colvarvalue.h:61
Collective variables main module.
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:85
cvm::real norm2() const
Square norm of this colvarvalue.
Definition: colvarvalue.h:613
static std::string const type_keyword(Type t)
User keywords for specifying value types in the configuration.
Definition: colvarvalue.cpp:37
colvarvalue(cvm::rvector const &v, Type vti=type_3vector)
Copy constructor from rvector base type (Note: this sets by default a type type_3vector ...
Definition: colvarvalue.h:133
4-dimensional vector that is a derivative of a quaternion
Definition: colvarvalue.h:57
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:34
cvm::real dist2(cvm::quaternion const &Q2) const
Square distance from another quaternion on the 4-dimensional unit sphere: returns the square of the a...
Definition: colvartypes.h:1307
size_t size() const
Number of dimensions of this variable.
Definition: colvarvalue.h:361
void set_elem(int const i_begin, int const i_end, colvarvalue const &x)
Set elements of the vector from a single colvarvalue.
Definition: colvarvalue.cpp:308
static int check_types(colvarvalue const &x1, colvarvalue const &x2)
Ensure that the two types are the same within a binary operator.
Definition: colvarvalue.h:382
void reset()
Set to the null value for the data type currently defined.
Definition: colvarvalue.cpp:106
size_t output_width(size_t const &real_width) const
Definition: colvarvalue.cpp:695
static size_t num_dimensions(Type t)
Number of dimensions for each supported type (used to allocate vector1d_value)
Definition: colvarvalue.cpp:84
colvarvalue(cvm::quaternion const &q, Type vti=type_quaternion)
Copy constructor from quaternion base type.
Definition: colvarvalue.h:138
Type value_type
Current type of this colvarvalue object.
Definition: colvarvalue.h:65
colvarvalue const get_elem(int const i_begin, int const i_end, Type const vt) const
Get a single colvarvalue out of elements of the vector.
Definition: colvarvalue.cpp:296
static size_t num_df(Type t)
Number of degrees of freedom for each supported type.
Definition: colvarvalue.cpp:61
static void error(std::string const &message, int code=COLVARS_ERROR)
Print a message to the main log and set global error code.
Definition: colvarmodule.cpp:1536
static std::string to_str(T const &x, size_t const &width=0, size_t const &prec=0)
Quick conversion of an object to a string.
Definition: colvarmodule.h:615
void is_derivative()
Definition: colvarvalue.cpp:195
colvarvalue & operator=(colvarvalue const &x)
Assignment operator (type of x is checked)
Definition: colvarvalue.h:455
friend std::ostream & operator<<(std::ostream &os, colvarvalue const &q)
Formatted output operator.
Definition: colvarvalue.cpp:620
cvm::real norm() const
Norm of this colvarvalue.
Definition: colvarvalue.h:178
cvm::vector1d< cvm::real > vector1d_value
Generic vector data member.
Definition: colvarvalue.h:77
cvm::rvector rvector_value
3-dimensional vector data member
Definition: colvarvalue.h:71
Type type() const
Get the current type.
Definition: colvarvalue.h:159
colvarvalue(cvm::real const &x)
Copy constructor from real base type.
Definition: colvarvalue.h:126
cvm::real operator[](int const i) const
Get a scalar number out of an element of the vector.
Definition: colvarvalue.h:275
3-dimensional vector, implemented as colvarmodule::rvector
Definition: colvarvalue.h:49
static bool type_checking()
Whether or not the type check is enforced.
Definition: colvarvalue.h:92
void undef_op() const
Undefined operation.
Definition: colvarvalue.cpp:375
colvarvalue dist2_grad(colvarvalue const &x2) const
Derivative with respect to this colvarvalue of the square distance.
Definition: colvarvalue.cpp:526
vector (arbitrary dimension)
Definition: colvarvalue.h:59
std::vector< int > elem_sizes
If vector1d_value is a concatenation of colvarvalues, these mark how many components for each colvarv...
Definition: colvarvalue.h:89
Arbitrary size array (one dimensions) suitable for linear algebra operations (i.e. for floating point numbers it can be used with library functions)
Definition: colvarmodule.h:90
cvm::quaternion quaternion_value
Quaternion data member.
Definition: colvarvalue.h:74
Type
Possible types of value.
Definition: colvarvalue.h:43