Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2025 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : plumed is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #ifndef __PLUMED_tools_ColvarOutput_h
23 : #define __PLUMED_tools_ColvarOutput_h
24 :
25 : #include <vector>
26 :
27 : #include "Pbc.h"
28 : #include "View.h"
29 : #include "View2D.h"
30 : #include "Vector.h"
31 : #include "Tensor.h"
32 : #include "core/Colvar.h"
33 :
34 : namespace PLMD {
35 :
36 : template <typename T>
37 : class ColvarOutput {
38 : private:
39 : class DerivHelper {
40 : private:
41 : std::size_t nderivPerComponent;
42 : T* derivatives;
43 : public:
44 4297068 : DerivHelper(T* d, std::size_t n ) : nderivPerComponent(n), derivatives(d) {}
45 : View2D<T, helpers::dynamic_extent, 3> operator[](std::size_t i) {
46 : //the -9 is to "exclude" the virial (even if tecnically is still accessible)
47 27991233 : return { derivatives + i*nderivPerComponent, nderivPerComponent-9 };
48 : }
49 :
50 7232664 : Vector getAtomDerivatives( std::size_t valueID, std::size_t atomID ) {
51 7232664 : std::size_t base = valueID*nderivPerComponent + 3*atomID;
52 7232664 : return Vector( derivatives[base], derivatives[base+1], derivatives[base+2] );
53 : }
54 :
55 : View<T,3> getView( std::size_t valueID, std::size_t atomID) {
56 : std::size_t base = valueID*nderivPerComponent + 3*atomID;
57 : return View<T,3> { derivatives +base};
58 : }
59 : };
60 : public:
61 : class VirialHelper {
62 : private:
63 : std::size_t nderivPerComponent;
64 : T* derivatives;
65 : public:
66 4297068 : VirialHelper(T* d, std::size_t n ) : nderivPerComponent(n), derivatives(d) {}
67 19518422 : Tensor operator[](std::size_t i) const {
68 19518422 : std::size_t n=(i+1)*nderivPerComponent;
69 19518422 : return Tensor( derivatives[n-9],
70 19518422 : derivatives[n-8],
71 19518422 : derivatives[n-7],
72 19518422 : derivatives[n-6],
73 19518422 : derivatives[n-5],
74 19518422 : derivatives[n-4],
75 19518422 : derivatives[n-3],
76 19518422 : derivatives[n-2],
77 19518422 : derivatives[n-1] );
78 : }
79 : View<T,9> getView(std::size_t i) const {
80 47244 : std::size_t n=(i+1)*nderivPerComponent-9;
81 47244 : return View<T,9> {derivatives+n};
82 : }
83 24064435 : void set( std::size_t i, const Tensor& v ) {
84 24064435 : std::size_t n=(i+1)*nderivPerComponent;
85 24064435 : derivatives[n-9]=v[0][0];
86 24064435 : derivatives[n-8]=v[0][1];
87 24064435 : derivatives[n-7]=v[0][2];
88 24064435 : derivatives[n-6]=v[1][0];
89 24064435 : derivatives[n-5]=v[1][1];
90 24064435 : derivatives[n-4]=v[1][2];
91 24064435 : derivatives[n-3]=v[2][0];
92 24064435 : derivatives[n-2]=v[2][1];
93 24064435 : derivatives[n-1]=v[2][2];
94 24064435 : }
95 : };
96 : std::size_t ncomponents;
97 : View<T> values;
98 : DerivHelper derivs;
99 : VirialHelper virial;
100 : ColvarOutput( View<T> v, std::size_t nderivPerComponent, T *derivatives ):
101 4297068 : ncomponents(v.size()),
102 4012605 : values(v),
103 : derivs(derivatives,nderivPerComponent),
104 : virial(derivatives,nderivPerComponent)
105 : {}
106 :
107 : static ColvarOutput createColvarOutput( std::vector<T>& v,
108 : std::vector<T>& d,
109 : Colvar* action );
110 :
111 : Vector getAtomDerivatives( std::size_t i, std::size_t a ) {
112 820777 : return derivs.getAtomDerivatives(i,a);
113 : }
114 : };
115 :
116 : template <typename T>
117 284463 : ColvarOutput<T> ColvarOutput<T>::createColvarOutput( std::vector<T>& v,
118 : std::vector<T>& d,
119 : Colvar* action ) {
120 : View<T> val(v.data(),v.size());
121 284463 : d.resize( action->getNumberOfComponents()*action->getNumberOfDerivatives() );
122 284463 : return ColvarOutput( val, action->getNumberOfDerivatives(), d.data() );
123 : }
124 : } //namespace PLMD
125 :
126 : #endif // __PLUMED_tools_ColvarOutput_h
|