File: colvarmodule_utils.h

package info (click to toggle)
lammps 20250204%2Bdfsg.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 474,368 kB
  • sloc: cpp: 1,060,070; python: 27,785; ansic: 8,956; f90: 7,254; sh: 6,044; perl: 4,171; fortran: 2,442; xml: 1,714; makefile: 1,352; objc: 238; lisp: 188; yacc: 58; csh: 16; awk: 14; tcl: 6; javascript: 2
file content (80 lines) | stat: -rw-r--r-- 1,665 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// -*- c++ -*-

// This file is part of the Collective Variables module (Colvars).
// The original version of Colvars and its updates are located at:
// https://github.com/Colvars/colvars
// Please update all Colvars source files before making any changes.
// If you wish to distribute your changes, please submit them to the
// Colvars repository at GitHub.


#ifndef COLVARMODULE_UTILS_H
#define COLVARMODULE_UTILS_H


#include "colvarmodule.h"


template <typename T>
cvm::real get_force_norm2(T const &x)
{
  return x.norm2();
}


template <>
inline cvm::real get_force_norm2(cvm::real const &x)
{
  return x*x;
}


template <typename T, int flag, bool get_index>
cvm::real compute_norm2_stats(std::vector<T> const &v,
                              int *minmax_index = NULL)
{
  cvm::real result = 0.0;
  if (flag == -1) {
    // Initialize for minimum search, using approx. largest float32 value
    result = 1.0e38;
  }

  typename std::vector<T>::const_iterator xi = v.begin();
  size_t i = 0;

  if (get_index) *minmax_index = -1; // Let's not assume minmax_index is initialized to -1

  for ( ; xi != v.end(); xi++, i++) {
    cvm::real const norm2 = get_force_norm2<T>(*xi);
    if (flag == 0) {
      result += norm2;
    }
    if (flag == 1) {
      if (norm2 > result) {
        result = norm2;
        if (get_index) *minmax_index = i;
      }
    }
    if (flag == -1) {
      if (norm2 < result) {
        result = norm2;
        if (get_index) *minmax_index = i;
      }
    }
  }

  size_t const n = v.size();

  if (flag == 0) {
    if (n > 0) {
      result /= cvm::real(n);
    }
  }

  result = cvm::sqrt(result);

  return result;
}


#endif