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_colvar_ColvarInput_h 23 : #define __PLUMED_colvar_ColvarInput_h 24 : 25 : #include <vector> 26 : 27 : #include "tools/Pbc.h" 28 : #include "tools/View.h" 29 : #include "tools/View2D.h" 30 : #include "tools/Vector.h" 31 : #include "tools/Tensor.h" 32 : #include "tools/ColvarOutput.h" 33 : 34 : namespace PLMD { 35 : 36 : class Colvar; 37 : 38 : namespace colvar { 39 : 40 : template <typename T=double> 41 : struct ColvarInput { 42 : unsigned mode; 43 : const Pbc& pbc; 44 : View2D<const T,helpers::dynamic_extent,3> pos; 45 : View<const T> mass; 46 : View<const T> charges; 47 3009232 : ColvarInput( unsigned m, 48 : unsigned natoms, 49 : const T* p, 50 : const T* w, 51 : const T* q, 52 : const Pbc& box ) : 53 3009232 : mode(m), 54 2724769 : pbc(box), 55 284463 : pos(p,natoms), 56 : mass(w,natoms), 57 : charges(q,natoms) { 58 : } 59 : static ColvarInput createColvarInput( unsigned m, const std::vector<Vector>& p, const Colvar* colv ); 60 : static void setBoxDerivativesNoPbc( const ColvarInput<T>& inpt, ColvarOutput<T>& out ); 61 : /// same as setBoxDerivativesNoPbc but with no extra memory allocations 62 : static void setBoxDerivativesNoPbc_inplace( const ColvarInput<T>& inpt, ColvarOutput<T>& out ); 63 : }; 64 : 65 : template <typename T> 66 284463 : ColvarInput<T> ColvarInput<T>::createColvarInput( unsigned m, 67 : const std::vector<Vector>& p, 68 : const Colvar* colv ) { 69 : //this won't work with floating point 70 : return ColvarInput( m, 71 : p.size(), 72 : &p[0][0], 73 : colv->getMasses().data(), 74 284463 : colv->getCharges(true).data(), 75 284463 : colv->getPbc() ); 76 : } 77 : 78 : template <typename T> 79 2813221 : void ColvarInput<T>::setBoxDerivativesNoPbc( const ColvarInput<T>& inpt, ColvarOutput<T>& out ) { 80 : //Both version passes the tests, we should discuss wicht onw might be better 81 : 82 2813221 : unsigned nat=inpt.pos.size(); 83 5873157 : for(unsigned i=0; i<out.ncomponents; ++i) { 84 3059936 : Tensor v; 85 : v.zero(); 86 9469963 : for(unsigned j=0; j<nat; j++) { 87 6410027 : v-=Tensor(Vector(inpt.pos[j][0],inpt.pos[j][1],inpt.pos[j][2]), 88 12820054 : out.derivs.getAtomDerivatives(i,j)); 89 : } 90 3059936 : out.virial.set( i, v ); 91 : } 92 2813221 : } 93 : 94 : template <typename T> 95 : void ColvarInput<T>::setBoxDerivativesNoPbc_inplace( const ColvarInput<T>& inpt, ColvarOutput<T>& out ) { 96 : //now with no extra allocated memory: 97 : unsigned nat=inpt.pos.size(); 98 : for(unsigned i=0; i<out.ncomponents; ++i) { 99 : auto v = out.virial.getView(i); 100 : LoopUnroller<9>::_zero(v.data()); 101 : for(unsigned j=0; j<nat; j++) { 102 : const auto deriv = out.derivs.getView(i,j); 103 : v[0] -= inpt.pos[j][0]*deriv[0]; 104 : v[1] -= inpt.pos[j][0]*deriv[1]; 105 : v[2] -= inpt.pos[j][0]*deriv[2]; 106 : 107 : v[3] -= inpt.pos[j][1]*deriv[0]; 108 : v[4] -= inpt.pos[j][1]*deriv[1]; 109 : v[5] -= inpt.pos[j][1]*deriv[2]; 110 : 111 : v[6] -= inpt.pos[j][2]*deriv[0]; 112 : v[7] -= inpt.pos[j][2]*deriv[1]; 113 : v[8] -= inpt.pos[j][2]*deriv[2]; 114 : } 115 : } 116 : } 117 : } // namespace colvar 118 : } //namespace PLMD 119 : #endif // __PLUMED_colvar_ColvarInput_h