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_dimensions(Type t);
114
116 size_t size() const;
117
120 colvarvalue();
121
124 colvarvalue(Type const &vti);
125
127 colvarvalue(cvm::real const &x);
128
132 colvarvalue(cvm::rvector const &v, Type vti = type_3vector);
133
136
139
141 colvarvalue(colvarvalue const &x);
142
143
145 void reset();
146
151 void apply_constraints();
152
154 inline Type type() const
155 {
156 return value_type;
157 }
158
160 void type(Type const &vti);
161
163 void type(colvarvalue const &x);
164
167 void is_derivative();
168
170 cvm::real norm2() const;
171
173 inline cvm::real norm() const
174 {
175 return cvm::sqrt(this->norm2());
176 }
177
179 cvm::real sum() const;
180
183
185 cvm::real dist2(colvarvalue const &x2) const;
186
188 colvarvalue dist2_grad(colvarvalue const &x2) const;
189
192 static colvarvalue const interpolate(colvarvalue const &x1,
193 colvarvalue const &x2,
194 cvm::real const lambda = 0.5);
195
198
199 void operator += (colvarvalue const &x);
200 void operator -= (colvarvalue const &x);
201 void operator *= (cvm::real const &a);
202 void operator /= (cvm::real const &a);
203
204 // Binary operators (return values)
205 friend colvarvalue operator + (colvarvalue const &x1, colvarvalue const &x2);
206 friend colvarvalue operator - (colvarvalue const &x1, colvarvalue const &x2);
207 friend colvarvalue operator * (colvarvalue const &x, cvm::real const &a);
208 friend colvarvalue operator * (cvm::real const &a, colvarvalue const &x);
209 friend colvarvalue operator / (colvarvalue const &x, cvm::real const &a);
210
212 friend cvm::real operator * (colvarvalue const &x1, colvarvalue const &x2);
213
214 // Cast to scalar
215 inline operator cvm::real() const
216 {
217 if (value_type != type_scalar) {
218 cvm::error("Error: trying to use a variable of type \""+
219 type_desc(value_type)+"\" as one of type \""+
220 type_desc(type_scalar)+"\".\n");
221 }
222 return real_value;
223 }
224
225 // Cast to 3-vector
226 inline operator cvm::rvector() const
227 {
228 if ((value_type != type_3vector) &&
231 cvm::error("Error: trying to use a variable of type \""+
232 type_desc(value_type)+"\" as one of type \""+
233 type_desc(type_3vector)+"\".\n");
234 }
235 return rvector_value;
236 }
237
238 // Cast to quaternion
239 inline operator cvm::quaternion() const
240 {
241 if ((value_type != type_quaternion) &&
243 cvm::error("Error: trying to use a variable of type \""+
244 type_desc(value_type)+"\" as one of type \""+
245 type_desc(type_quaternion)+"\".\n");
246 }
247 return quaternion_value;
248 }
249
250 // Create a n-dimensional vector from one of the basic types, or return the existing vector
251 cvm::vector1d<cvm::real> const as_vector() const;
252
253
255 inline bool is_scalar() const
256 {
257 return (value_type == type_scalar);
258 }
259
260
264 void add_elem(colvarvalue const &x);
265
267 colvarvalue const get_elem(int const i_begin, int const i_end, Type const vt) const;
268
270 colvarvalue const get_elem(int const icv) const;
271
274 void set_elem(int const icv, colvarvalue const &x);
275
277 void set_elem(int const i_begin, int const i_end, colvarvalue const &x);
278
280 void set_random();
281
283 void set_ones(cvm::real assigned_value = 1.0);
284
286 cvm::real operator [] (int const i) const;
287
289 cvm::real & operator [] (int const i);
290
292 static int check_types(colvarvalue const &x1, colvarvalue const &x2);
293
295 static int check_types_assign(Type const &vt1, Type const &vt2);
296
298 void undef_op() const;
299
300private:
301
303 template <typename OST> void write_to_stream_template_(OST &os) const;
304
305public:
306
308 friend std::ostream & operator << (std::ostream &os, colvarvalue const &x);
309
312
313private:
314
316 template <typename IST> void read_from_stream_template_(IST &is);
317
318public:
319
321 friend std::istream & operator >> (std::istream &is, colvarvalue &x);
322
325
329 size_t output_width(size_t const &real_width) const;
330
332 std::string to_simple_string() const;
333
335 int from_simple_string(std::string const &s);
336
337
338 // optimized routines for operations on arrays of colvar values;
339 // xv and result are assumed to have the same number of elements
340
343 static void inner_opt(colvarvalue const &x,
344 std::vector<colvarvalue>::iterator &xv,
345 std::vector<colvarvalue>::iterator const &xv_end,
346 std::vector<cvm::real>::iterator &result);
347
350 static void inner_opt(colvarvalue const &x,
351 std::list<colvarvalue>::iterator &xv,
352 std::list<colvarvalue>::iterator const &xv_end,
353 std::vector<cvm::real>::iterator &result);
354
358 static void p2leg_opt(colvarvalue const &x,
359 std::vector<colvarvalue>::iterator &xv,
360 std::vector<colvarvalue>::iterator const &xv_end,
361 std::vector<cvm::real>::iterator &result);
362
365 static void p2leg_opt(colvarvalue const &x,
366 std::list<colvarvalue>::iterator &xv,
367 std::list<colvarvalue>::iterator const &xv_end,
368 std::vector<cvm::real>::iterator &result);
369
370};
371
372
373inline size_t colvarvalue::size() const
374{
375 switch (value_type) {
377 default:
378 return 0; break;
380 return 1; break;
384 return 3; break;
387 return 4; break;
389 return vector1d_value.size(); break;
390 }
391}
392
393
394inline cvm::real colvarvalue::operator [] (int const i) const
395{
396 switch (value_type) {
398 default:
399 cvm::error("Error: trying to access a colvar value "
400 "that is not initialized.\n", COLVARS_BUG_ERROR);
401 return 0.0; break;
403 return real_value; break;
407 return rvector_value[i]; break;
410 return quaternion_value[i]; break;
412 return vector1d_value[i]; break;
413 }
414}
415
416
418{
419 switch (value_type) {
421 default:
422 cvm::error("Error: trying to access a colvar value "
423 "that is not initialized.\n", COLVARS_BUG_ERROR);
424 return real_value; break;
426 return real_value; break;
430 return rvector_value[i]; break;
433 return quaternion_value[i]; break;
435 return vector1d_value[i]; break;
436 }
437}
438
439
441 colvarvalue const &x2)
442{
444 return COLVARS_OK;
445 }
446
447 if (x1.type() != x2.type()) {
448 if (((x1.type() == type_unit3vector) &&
449 (x2.type() == type_unit3vectorderiv)) ||
450 ((x2.type() == type_unit3vector) &&
451 (x1.type() == type_unit3vectorderiv)) ||
452 ((x1.type() == type_quaternion) &&
453 (x2.type() == type_quaternionderiv)) ||
454 ((x2.type() == type_quaternion) &&
455 (x1.type() == type_quaternionderiv))) {
456 return COLVARS_OK;
457 } else {
458 cvm::error("Trying to perform an operation between two colvar "
459 "values with different types, \""+
461 "\" and \""+
463 "\".\n");
464 return COLVARS_ERROR;
465 }
466 }
467
468 if (x1.type() == type_vector) {
469 if (x1.vector1d_value.size() != x2.vector1d_value.size()) {
470 cvm::error("Trying to perform an operation between two vector colvar "
471 "values with different sizes, "+
472 cvm::to_str(x1.vector1d_value.size())+
473 " and "+
474 cvm::to_str(x2.vector1d_value.size())+
475 ".\n");
476 return COLVARS_ERROR;
477 }
478 }
479 return COLVARS_OK;
480}
481
482
484 colvarvalue::Type const &vt2)
485{
487 return COLVARS_OK;
488 }
489
490 if (vt1 != type_notset) {
491 if (((vt1 == type_unit3vector) &&
492 (vt2 == type_unit3vectorderiv)) ||
493 ((vt2 == type_unit3vector) &&
494 (vt1 == type_unit3vectorderiv)) ||
495 ((vt1 == type_quaternion) &&
496 (vt2 == type_quaternionderiv)) ||
497 ((vt2 == type_quaternion) &&
498 (vt1 == type_quaternionderiv))) {
499 return COLVARS_OK;
500 } else {
501 if (vt1 != vt2) {
502 cvm::error("Trying to assign a colvar value with type \""+
503 type_desc(vt2)+"\" to one with type \""+
504 type_desc(vt1)+"\".\n");
505 return COLVARS_ERROR;
506 }
507 }
508 }
509 return COLVARS_OK;
510}
511
512
514{
515 check_types_assign(this->type(), x.type());
516 value_type = x.type();
517
518 switch (this->type()) {
520 this->real_value = x.real_value;
521 break;
525 this->rvector_value = x.rvector_value;
526 break;
529 this->quaternion_value = x.quaternion_value;
530 break;
536 break;
538 default:
539 undef_op();
540 break;
541 }
542 return *this;
543}
544
545
546inline void colvarvalue::operator += (colvarvalue const &x)
547{
548 colvarvalue::check_types(*this, x);
549
550 switch (this->type()) {
552 this->real_value += x.real_value;
553 break;
557 this->rvector_value += x.rvector_value;
558 break;
561 this->quaternion_value += x.quaternion_value;
562 break;
564 this->vector1d_value += x.vector1d_value;
565 break;
567 default:
568 undef_op();
569 }
570}
571
572
573inline void colvarvalue::operator -= (colvarvalue const &x)
574{
575 colvarvalue::check_types(*this, x);
576
577 switch (value_type) {
580 break;
585 break;
589 break;
591 this->vector1d_value -= x.vector1d_value;
592 break;
594 default:
595 undef_op();
596 }
597}
598
599
600inline void colvarvalue::operator *= (cvm::real const &a)
601{
602 switch (value_type) {
604 real_value *= a;
605 break;
608 rvector_value *= a;
609 break;
612 quaternion_value *= a;
613 break;
615 this->vector1d_value *= a;
616 break;
618 default:
619 undef_op();
620 }
621}
622
623
624inline void colvarvalue::operator /= (cvm::real const &a)
625{
626 switch (value_type) {
628 real_value /= a; break;
632 rvector_value /= a; break;
635 quaternion_value /= a; break;
637 this->vector1d_value /= a;
638 break;
640 default:
641 undef_op();
642 }
643}
644
645
646inline cvm::vector1d<cvm::real> const colvarvalue::as_vector() const
647{
648 switch (value_type) {
650 {
652 v[0] = real_value;
653 return v;
654 }
658 return rvector_value.as_vector();
661 return quaternion_value.as_vector();
663 return vector1d_value;
665 default:
666 return cvm::vector1d<cvm::real>(0);
667 }
668}
669
670
671#endif
1-dimensional vector of real numbers with four components and a quaternion algebra
Definition: colvartypes.h:958
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
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:2046
static real sqrt(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:133
static std::string to_str(char const *s)
Convert to string for output purposes.
Definition: colvarmodule.cpp:2392
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:771
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.cpp:626
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:307
cvm::real real_value
Real data member.
Definition: colvarvalue.h:77
Type type() const
Get the current type.
Definition: colvarvalue.h:154
colvarvalue & operator=(colvarvalue const &x)
Assignment operator (type of x is checked)
Definition: colvarvalue.h:513
void read_from_stream_template_(IST &is)
Generic stream reading function (formatted and not)
Definition: colvarvalue.cpp:849
void apply_constraints()
If the variable has constraints (e.g. unitvector or quaternion), transform it to satisfy them; this f...
Definition: colvarvalue.cpp:203
cvm::real norm() const
Norm of this colvarvalue.
Definition: colvarvalue.h:173
cvm::real operator[](int const i) const
Get a scalar number out of an element of the vector.
Definition: colvarvalue.h:394
void is_derivative()
Definition: colvarvalue.cpp:267
void set_ones(cvm::real assigned_value=1.0)
Make each element equal to the given argument.
Definition: colvarvalue.cpp:386
void reset()
Set to the null value for the data type currently defined.
Definition: colvarvalue.cpp:178
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:702
void set_elem(int const icv, colvarvalue const &x)
Definition: colvarvalue.cpp:341
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:920
size_t size() const
Number of dimensions of this variable.
Definition: colvarvalue.h:373
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:156
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:898
void set_random()
Make each element a random number in N(0,1)
Definition: colvarvalue.cpp:352
friend std::istream & operator>>(std::istream &is, colvarvalue &x)
Formatted input operator.
Definition: colvarvalue.cpp:885
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:799
bool is_scalar() const
Whether this variable is a real number.
Definition: colvarvalue.h:255
void undef_op() const
Undefined operation.
Definition: colvarvalue.cpp:420
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:825
cvm::real sum() const
Sum of the components of this colvarvalue (if more than one dimension)
Definition: colvarvalue.cpp:603
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:483
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:440
colvarvalue dist2_grad(colvarvalue const &x2) const
Derivative with respect to this colvarvalue of the square distance.
Definition: colvarvalue.cpp:662
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:1002
void add_elem(colvarvalue const &x)
Definition: colvarvalue.cpp:291
std::string to_simple_string() const
Formats value as a script-friendly string (space separated list)
Definition: colvarvalue.cpp:744
cvm::rvector rvector_value
3-dimensional vector data member
Definition: colvarvalue.h:80
cvm::real norm2() const
Square norm of this colvarvalue.
Definition: colvarvalue.cpp:571
Definition: colvars_memstream.h:30
Collective variables main module.