Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvarvalue.h
1// -*- c++ -*-
2
3// This file is part of the Collective Variables module (Colvars).
4// The original version of Colvars and its updates are located at:
5// https://github.com/Colvars/colvars
6// Please update all Colvars source files before making any changes.
7// If you wish to distribute your changes, please submit them to the
8// Colvars repository at GitHub.
9
10#ifndef COLVARVALUE_H
11#define COLVARVALUE_H
12
13#include <list>
14
15#include "colvarmodule.h"
16#include "colvartypes.h"
17
18
41
42
44
45public:
46
52 enum Type {
71 };
72
75
78
81
84
87
90 std::vector<Type> elem_types;
91
94 std::vector<int> elem_indices;
95
98 std::vector<int> elem_sizes;
99
101 static inline bool type_checking()
102 {
103 return true;
104 }
105
107 static std::string const type_desc(Type t);
108
110 static std::string const type_keyword(Type t);
111
113 static size_t num_df(Type t);
114
116 static size_t num_dimensions(Type t);
117
119 size_t size() const;
120
123 colvarvalue();
124
127 colvarvalue(Type const &vti);
128
130 colvarvalue(cvm::real const &x);
131
135 colvarvalue(cvm::rvector const &v, Type vti = type_3vector);
136
139
142
144 colvarvalue(colvarvalue const &x);
145
146
148 void reset();
149
154 void apply_constraints();
155
157 inline Type type() const
158 {
159 return value_type;
160 }
161
163 void type(Type const &vti);
164
166 void type(colvarvalue const &x);
167
170 void is_derivative();
171
173 cvm::real norm2() const;
174
176 inline cvm::real norm() const
177 {
178 return cvm::sqrt(this->norm2());
179 }
180
182 cvm::real sum() const;
183
186
188 cvm::real dist2(colvarvalue const &x2) const;
189
191 colvarvalue dist2_grad(colvarvalue const &x2) const;
192
195 static colvarvalue const interpolate(colvarvalue const &x1,
196 colvarvalue const &x2,
197 cvm::real const lambda = 0.5);
198
201
202 void operator += (colvarvalue const &x);
203 void operator -= (colvarvalue const &x);
204 void operator *= (cvm::real const &a);
205 void operator /= (cvm::real const &a);
206
207 // Binary operators (return values)
208 friend colvarvalue operator + (colvarvalue const &x1, colvarvalue const &x2);
209 friend colvarvalue operator - (colvarvalue const &x1, colvarvalue const &x2);
210 friend colvarvalue operator * (colvarvalue const &x, cvm::real const &a);
211 friend colvarvalue operator * (cvm::real const &a, colvarvalue const &x);
212 friend colvarvalue operator / (colvarvalue const &x, cvm::real const &a);
213
215 friend cvm::real operator * (colvarvalue const &x1, colvarvalue const &x2);
216
217 // Cast to scalar
218 inline operator cvm::real() const
219 {
220 if (value_type != type_scalar) {
221 cvm::error("Error: trying to use a variable of type \""+
222 type_desc(value_type)+"\" as one of type \""+
223 type_desc(type_scalar)+"\".\n");
224 }
225 return real_value;
226 }
227
228 // Cast to 3-vector
229 inline operator cvm::rvector() const
230 {
231 if ((value_type != type_3vector) &&
234 cvm::error("Error: trying to use a variable of type \""+
235 type_desc(value_type)+"\" as one of type \""+
236 type_desc(type_3vector)+"\".\n");
237 }
238 return rvector_value;
239 }
240
241 // Cast to quaternion
242 inline operator cvm::quaternion() const
243 {
244 if ((value_type != type_quaternion) &&
246 cvm::error("Error: trying to use a variable of type \""+
247 type_desc(value_type)+"\" as one of type \""+
248 type_desc(type_quaternion)+"\".\n");
249 }
250 return quaternion_value;
251 }
252
253 // Create a n-dimensional vector from one of the basic types, or return the existing vector
254 cvm::vector1d<cvm::real> const as_vector() const;
255
256
258 inline bool is_scalar() const
259 {
260 return (value_type == type_scalar);
261 }
262
263
267 void add_elem(colvarvalue const &x);
268
270 colvarvalue const get_elem(int const i_begin, int const i_end, Type const vt) const;
271
273 colvarvalue const get_elem(int const icv) const;
274
277 void set_elem(int const icv, colvarvalue const &x);
278
280 void set_elem(int const i_begin, int const i_end, colvarvalue const &x);
281
283 void set_random();
284
286 void set_ones(cvm::real assigned_value = 1.0);
287
289 cvm::real operator [] (int const i) const;
290
292 cvm::real & operator [] (int const i);
293
295 static int check_types(colvarvalue const &x1, colvarvalue const &x2);
296
298 static int check_types_assign(Type const &vt1, Type const &vt2);
299
301 void undef_op() const;
302
303private:
304
306 template <typename OST> void write_to_stream_template_(OST &os) const;
307
308public:
309
311 friend std::ostream & operator << (std::ostream &os, colvarvalue const &x);
312
315
316private:
317
319 template <typename IST> void read_from_stream_template_(IST &is);
320
321public:
322
324 friend std::istream & operator >> (std::istream &is, colvarvalue &x);
325
328
332 size_t output_width(size_t const &real_width) const;
333
335 std::string to_simple_string() const;
336
338 int from_simple_string(std::string const &s);
339
340
341 // optimized routines for operations on arrays of colvar values;
342 // xv and result are assumed to have the same number of elements
343
346 static void inner_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 static void inner_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
361 static void p2leg_opt(colvarvalue const &x,
362 std::vector<colvarvalue>::iterator &xv,
363 std::vector<colvarvalue>::iterator const &xv_end,
364 std::vector<cvm::real>::iterator &result);
365
368 static void p2leg_opt(colvarvalue const &x,
369 std::list<colvarvalue>::iterator &xv,
370 std::list<colvarvalue>::iterator const &xv_end,
371 std::vector<cvm::real>::iterator &result);
372
373};
374
375
376inline size_t colvarvalue::size() const
377{
378 switch (value_type) {
380 default:
381 return 0; break;
383 return 1; break;
387 return 3; break;
390 return 4; break;
392 return vector1d_value.size(); break;
393 }
394}
395
396
397inline cvm::real colvarvalue::operator [] (int const i) const
398{
399 switch (value_type) {
401 default:
402 cvm::error("Error: trying to access a colvar value "
403 "that is not initialized.\n", COLVARS_BUG_ERROR);
404 return 0.0; break;
406 return real_value; break;
410 return rvector_value[i]; break;
413 return quaternion_value[i]; break;
415 return vector1d_value[i]; break;
416 }
417}
418
419
421{
422 switch (value_type) {
424 default:
425 cvm::error("Error: trying to access a colvar value "
426 "that is not initialized.\n", COLVARS_BUG_ERROR);
427 return real_value; break;
429 return real_value; break;
433 return rvector_value[i]; break;
436 return quaternion_value[i]; break;
438 return vector1d_value[i]; break;
439 }
440}
441
442
444 colvarvalue const &x2)
445{
447 return COLVARS_OK;
448 }
449
450 if (x1.type() != x2.type()) {
451 if (((x1.type() == type_unit3vector) &&
452 (x2.type() == type_unit3vectorderiv)) ||
453 ((x2.type() == type_unit3vector) &&
454 (x1.type() == type_unit3vectorderiv)) ||
455 ((x1.type() == type_quaternion) &&
456 (x2.type() == type_quaternionderiv)) ||
457 ((x2.type() == type_quaternion) &&
458 (x1.type() == type_quaternionderiv))) {
459 return COLVARS_OK;
460 } else {
461 cvm::error("Trying to perform an operation between two colvar "
462 "values with different types, \""+
464 "\" and \""+
466 "\".\n");
467 return COLVARS_ERROR;
468 }
469 }
470
471 if (x1.type() == type_vector) {
472 if (x1.vector1d_value.size() != x2.vector1d_value.size()) {
473 cvm::error("Trying to perform an operation between two vector colvar "
474 "values with different sizes, "+
475 cvm::to_str(x1.vector1d_value.size())+
476 " and "+
477 cvm::to_str(x2.vector1d_value.size())+
478 ".\n");
479 return COLVARS_ERROR;
480 }
481 }
482 return COLVARS_OK;
483}
484
485
487 colvarvalue::Type const &vt2)
488{
490 return COLVARS_OK;
491 }
492
493 if (vt1 != type_notset) {
494 if (((vt1 == type_unit3vector) &&
495 (vt2 == type_unit3vectorderiv)) ||
496 ((vt2 == type_unit3vector) &&
497 (vt1 == type_unit3vectorderiv)) ||
498 ((vt1 == type_quaternion) &&
499 (vt2 == type_quaternionderiv)) ||
500 ((vt2 == type_quaternion) &&
501 (vt1 == type_quaternionderiv))) {
502 return COLVARS_OK;
503 } else {
504 if (vt1 != vt2) {
505 cvm::error("Trying to assign a colvar value with type \""+
506 type_desc(vt2)+"\" to one with type \""+
507 type_desc(vt1)+"\".\n");
508 return COLVARS_ERROR;
509 }
510 }
511 }
512 return COLVARS_OK;
513}
514
515
517{
518 check_types_assign(this->type(), x.type());
519 value_type = x.type();
520
521 switch (this->type()) {
523 this->real_value = x.real_value;
524 break;
528 this->rvector_value = x.rvector_value;
529 break;
532 this->quaternion_value = x.quaternion_value;
533 break;
539 break;
541 default:
542 undef_op();
543 break;
544 }
545 return *this;
546}
547
548
549inline void colvarvalue::operator += (colvarvalue const &x)
550{
551 colvarvalue::check_types(*this, x);
552
553 switch (this->type()) {
555 this->real_value += x.real_value;
556 break;
560 this->rvector_value += x.rvector_value;
561 break;
564 this->quaternion_value += x.quaternion_value;
565 break;
567 this->vector1d_value += x.vector1d_value;
568 break;
570 default:
571 undef_op();
572 }
573}
574
575
576inline void colvarvalue::operator -= (colvarvalue const &x)
577{
578 colvarvalue::check_types(*this, x);
579
580 switch (value_type) {
583 break;
588 break;
592 break;
594 this->vector1d_value -= x.vector1d_value;
595 break;
597 default:
598 undef_op();
599 }
600}
601
602
603inline void colvarvalue::operator *= (cvm::real const &a)
604{
605 switch (value_type) {
607 real_value *= a;
608 break;
611 rvector_value *= a;
612 break;
615 quaternion_value *= a;
616 break;
618 this->vector1d_value *= a;
619 break;
621 default:
622 undef_op();
623 }
624}
625
626
627inline void colvarvalue::operator /= (cvm::real const &a)
628{
629 switch (value_type) {
631 real_value /= a; break;
635 rvector_value /= a; break;
638 quaternion_value /= a; break;
640 this->vector1d_value /= a;
641 break;
643 default:
644 undef_op();
645 }
646}
647
648
649inline cvm::vector1d<cvm::real> const colvarvalue::as_vector() const
650{
651 switch (value_type) {
653 {
655 v[0] = real_value;
656 return v;
657 }
661 return rvector_value.as_vector();
664 return quaternion_value.as_vector();
666 return vector1d_value;
668 default:
669 return cvm::vector1d<cvm::real>(0);
670 }
671}
672
673
675{
676 switch (value_type) {
678 return (this->real_value)*(this->real_value);
682 return (this->rvector_value).norm2();
685 return (this->quaternion_value).norm2();
687 if (elem_types.size() > 0) {
688 // if we have information about non-scalar types, use it
689 cvm::real result = 0.0;
690 size_t i;
691 for (i = 0; i < elem_types.size(); i++) {
692 result += (this->get_elem(i)).norm2();
693 }
694 return result;
695 } else {
696 return vector1d_value.norm2();
697 }
698 break;
700 default:
701 return 0.0;
702 }
703}
704
705
707{
708 switch (value_type) {
710 return (this->real_value);
714 return (this->rvector_value).x + (this->rvector_value).y +
715 (this->rvector_value).z;
718 return (this->quaternion_value).q0 + (this->quaternion_value).q1 +
719 (this->quaternion_value).q2 + (this->quaternion_value).q3;
721 return (this->vector1d_value).sum();
723 default:
724 return 0.0;
725 }
726}
727
728
730{
731 colvarvalue::check_types(*this, x2);
732
733 switch (this->type()) {
735 return (this->real_value - x2.real_value)*(this->real_value - x2.real_value);
737 return (this->rvector_value - x2.rvector_value).norm2();
740 // angle between (*this) and x2 is the distance
741 return cvm::acos(this->rvector_value * x2.rvector_value) * cvm::acos(this->rvector_value * x2.rvector_value);
744 // angle between (*this) and x2 is the distance, the quaternion
745 // object has it implemented internally
746 return this->quaternion_value.dist2(x2.quaternion_value);
748 return (this->vector1d_value - x2.vector1d_value).norm2();
750 default:
751 this->undef_op();
752 return 0.0;
753 };
754}
755
756
757#endif
1-dimensional vector of real numbers with four components and a quaternion algebra
Definition: colvartypes.h:958
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:1239
vector of real numbers with three components
Definition: colvartypes.h:727
Arbitrary size array (one dimensions) suitable for linear algebra operations (i.e....
Definition: colvartypes.h:37
cvm::real norm2() const
Squared norm.
Definition: colvartypes.h:228
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:95
static int error(std::string const &message, int code=-1)
Print a message to the main log and set global error code.
Definition: colvarmodule.cpp:2027
static real sqrt(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:133
static real acos(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:157
static std::string to_str(char const *s)
Convert to string for output purposes.
Definition: colvarmodule.cpp:2373
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:43
int from_simple_string(std::string const &s)
Parses value from a script-friendly string (space separated list)
Definition: colvarvalue.cpp:696
Type
Possible types of value.
Definition: colvarvalue.h:52
@ type_notset
Undefined type.
Definition: colvarvalue.h:54
@ type_3vector
3-dimensional vector, implemented as colvarmodule::rvector
Definition: colvarvalue.h:58
@ type_vector
vector (arbitrary dimension)
Definition: colvarvalue.h:68
@ type_unit3vectorderiv
3-dimensional vector that is a derivative of a unitvector
Definition: colvarvalue.h:62
@ type_quaternionderiv
4-dimensional vector that is a derivative of a quaternion
Definition: colvarvalue.h:66
@ type_quaternion
4-dimensional unit vector representing a rotation, implemented as colvarmodule::quaternion
Definition: colvarvalue.h:64
@ type_unit3vector
3-dimensional unit vector, implemented as colvarmodule::rvector
Definition: colvarvalue.h:60
@ type_all
Needed to iterate through enum.
Definition: colvarvalue.h:70
@ type_scalar
Scalar number, implemented as colvarmodule::real (default)
Definition: colvarvalue.h:56
cvm::real dist2(colvarvalue const &x2) const
Square distance between this colvarvalue and another.
Definition: colvarvalue.h:729
cvm::quaternion quaternion_value
Quaternion data member.
Definition: colvarvalue.h:83
std::vector< int > elem_sizes
If vector1d_value is a concatenation of colvarvalues, these mark how many components for each colvarv...
Definition: colvarvalue.h:98
colvarvalue()
Default constructor: this class defaults to a scalar number and always behaves like it unless you cha...
Definition: colvarvalue.cpp:18
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:330
cvm::real real_value
Real data member.
Definition: colvarvalue.h:77
Type type() const
Get the current type.
Definition: colvarvalue.h:157
colvarvalue & operator=(colvarvalue const &x)
Assignment operator (type of x is checked)
Definition: colvarvalue.h:516
void read_from_stream_template_(IST &is)
Generic stream reading function (formatted and not)
Definition: colvarvalue.cpp:774
void apply_constraints()
If the variable has constraints (e.g. unitvector or quaternion), transform it to satisfy them; this f...
Definition: colvarvalue.cpp:226
cvm::real norm() const
Norm of this colvarvalue.
Definition: colvarvalue.h:176
cvm::real operator[](int const i) const
Get a scalar number out of an element of the vector.
Definition: colvarvalue.h:397
void is_derivative()
Definition: colvarvalue.cpp:290
void set_ones(cvm::real assigned_value=1.0)
Make each element equal to the given argument.
Definition: colvarvalue.cpp:409
void reset()
Set to the null value for the data type currently defined.
Definition: colvarvalue.cpp:201
static std::string const type_desc(Type t)
Runtime description of value types.
Definition: colvarvalue.cpp:107
colvarvalue ones() const
Return a colvarvalue object of the same type and all components set to 1.
std::vector< Type > elem_types
If vector1d_value is a concatenation of colvarvalues, keep track of the individual types.
Definition: colvarvalue.h:90
static colvarvalue const interpolate(colvarvalue const &x1, colvarvalue const &x2, cvm::real const lambda=0.5)
Definition: colvarvalue.cpp:627
void set_elem(int const icv, colvarvalue const &x)
Definition: colvarvalue.cpp:364
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:845
size_t size() const
Number of dimensions of this variable.
Definition: colvarvalue.h:376
static bool type_checking()
Whether or not the type check is enforced.
Definition: colvarvalue.h:101
static size_t num_dimensions(Type t)
Number of dimensions for each supported type (used to allocate vector1d_value)
Definition: colvarvalue.cpp:179
static std::string const type_keyword(Type t)
User keywords for specifying value types in the configuration.
Definition: colvarvalue.cpp:132
size_t output_width(size_t const &real_width) const
Definition: colvarvalue.cpp:823
void set_random()
Make each element a random number in N(0,1)
Definition: colvarvalue.cpp:375
friend std::istream & operator>>(std::istream &is, colvarvalue &x)
Formatted input operator.
Definition: colvarvalue.cpp:810
std::vector< int > elem_indices
If vector1d_value is a concatenation of colvarvalues, these mark the initial components of each colva...
Definition: colvarvalue.h:94
void write_to_stream_template_(OST &os) const
Generic stream writing function (formatted and not)
Definition: colvarvalue.cpp:724
bool is_scalar() const
Whether this variable is a real number.
Definition: colvarvalue.h:258
void undef_op() const
Undefined operation.
Definition: colvarvalue.cpp:443
cvm::vector1d< cvm::real > vector1d_value
Generic vector data member.
Definition: colvarvalue.h:86
friend std::ostream & operator<<(std::ostream &os, colvarvalue const &x)
Formatted output operator.
Definition: colvarvalue.cpp:750
cvm::real sum() const
Sum of the components of this colvarvalue (if more than one dimension)
Definition: colvarvalue.h:706
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:486
Type value_type
Current type of this colvarvalue object.
Definition: colvarvalue.h:74
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:443
colvarvalue dist2_grad(colvarvalue const &x2) const
Derivative with respect to this colvarvalue of the square distance.
Definition: colvarvalue.cpp:594
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:927
static size_t num_df(Type t)
Number of degrees of freedom for each supported type.
Definition: colvarvalue.cpp:156
void add_elem(colvarvalue const &x)
Definition: colvarvalue.cpp:314
std::string to_simple_string() const
Formats value as a script-friendly string (space separated list)
Definition: colvarvalue.cpp:669
cvm::rvector rvector_value
3-dimensional vector data member
Definition: colvarvalue.h:80
cvm::real norm2() const
Square norm of this colvarvalue.
Definition: colvarvalue.h:674
Definition: colvars_memstream.h:30
Collective variables main module.