Collective Variables Module - Developer Documentation
Searching...
No Matches
colvarmodule_utils.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
11#ifndef COLVARMODULE_UTILS_H
12#define COLVARMODULE_UTILS_H
13
14
15#include "colvarmodule.h"
16
17
18template <typename T>
19cvm::real get_force_norm2(T const &x)
20{
21 return x.norm2();
22}
23
24
25template <>
26inline cvm::real get_force_norm2(cvm::real const &x)
27{
28 return x*x;
29}
30
31
32template <typename T, int flag, bool get_index>
33cvm::real compute_norm2_stats(std::vector<T> const &v,
34 int *minmax_index = NULL)
35{
36 cvm::real result = 0.0;
37 if (flag == -1) {
38 // Initialize for minimum search, using approx. largest float32 value
39 result = 1.0e38;
40 }
41
42 typename std::vector<T>::const_iterator xi = v.begin();
43 size_t i = 0;
44
45 if (get_index) *minmax_index = -1; // Let's not assume minmax_index is initialized to -1
46
47 for ( ; xi != v.end(); xi++, i++) {
48 cvm::real const norm2 = get_force_norm2<T>(*xi);
49 if (flag == 0) {
50 result += norm2;
51 }
52 if (flag == 1) {
53 if (norm2 > result) {
54 result = norm2;
55 if (get_index) *minmax_index = i;
56 }
57 }
58 if (flag == -1) {
59 if (norm2 < result) {
60 result = norm2;
61 if (get_index) *minmax_index = i;
62 }
63 }
64 }
65
66 size_t const n = v.size();
67
68 if (flag == 0) {
69 if (n > 0) {
70 result /= cvm::real(n);
71 }
72 }
73
74 result = cvm::sqrt(result);
75
76 return result;
77}
78
79
80#endif
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:95
static real sqrt(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:133
Collective variables main module.