Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2016-2021 The VES code team
3 : (see the PEOPLE-VES file at the root of this folder for a list of names)
4 :
5 : See http://www.ves-code.org for more information.
6 :
7 : This file is part of VES code module.
8 :
9 : The VES code module 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 : The VES code module 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 the VES code module. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #ifndef __PLUMED_ves_CoeffsMatrix_h
23 : #define __PLUMED_ves_CoeffsMatrix_h
24 :
25 : #include "CoeffsBase.h"
26 :
27 : #include <vector>
28 : #include <string>
29 : #include <cmath>
30 :
31 :
32 : namespace PLMD {
33 :
34 : class Action;
35 : class Value;
36 : class IFile;
37 : class OFile;
38 : class Communicator;
39 :
40 : namespace ves {
41 :
42 : class BasisFunctions;
43 : class CoeffsVector;
44 :
45 :
46 : class CoeffsMatrix:
47 : public CoeffsBase
48 : {
49 : public:
50 : private:
51 : std::vector<double> data;
52 : //
53 : size_t size_;
54 : size_t nrows_;
55 : size_t ncolumns_;
56 : //
57 : bool diagonal_;
58 : //
59 : unsigned int averaging_counter;
60 : unsigned int averaging_exp_decay_;
61 : //
62 : Communicator& mycomm;
63 : //
64 : void setupMatrix();
65 : //
66 : CoeffsMatrix& operator=(const CoeffsMatrix&);
67 : public:
68 : explicit CoeffsMatrix(
69 : const std::string&,
70 : const std::vector<std::string>&,
71 : const std::vector<unsigned int>&,
72 : Communicator& cc,
73 : const bool diagonal=true,
74 : const bool use_iteration_counter=false);
75 : //
76 : explicit CoeffsMatrix(
77 : const std::string&,
78 : std::vector<Value*>&,
79 : std::vector<BasisFunctions*>&,
80 : Communicator& cc,
81 : const bool diagonal=true,
82 : const bool use_iteration_counter=false);
83 : //
84 : explicit CoeffsMatrix(
85 : const std::string&,
86 : std::vector<std::vector<Value*> >& argsv,
87 : std::vector<std::vector<BasisFunctions*> >& basisfv,
88 : Communicator& cc,
89 : const bool diagonal=true,
90 : const bool use_iteration_counter=false,
91 : const std::string& multicoeffs_label="bias");
92 : //
93 : explicit CoeffsMatrix(
94 : const std::string&,
95 : CoeffsVector*,
96 : Communicator& cc,
97 : const bool diagonal=true);
98 : //
99 : ~CoeffsMatrix();
100 : //
101 : size_t getSize() const;
102 : //
103 : bool isSymmetric() const;
104 : bool isDiagonal() const;
105 : //
106 : bool sameShape(CoeffsVector&) const;
107 : bool sameShape(CoeffsMatrix&) const;
108 : static bool sameShape(CoeffsMatrix&, CoeffsMatrix&);
109 : static bool sameShape(CoeffsVector&, CoeffsMatrix&);
110 : static bool sameShape(CoeffsMatrix&, CoeffsVector&);
111 : //
112 : void sumCommMPI();
113 : void sumCommMPI(Communicator&);
114 : //
115 : void sumMultiSimCommMPI(Communicator&);
116 : //
117 : size_t getMatrixIndex(const size_t, const size_t) const;
118 : //
119 : // clear coeffs
120 : void clear();
121 : void setAllValuesToZero();
122 : //
123 : std::vector<double> getDataAsVector() const {return data;}
124 : // get value
125 : double getValue(const size_t, const size_t) const;
126 : double getValue(const std::vector<unsigned int>&, const std::vector<unsigned int>&) const;
127 : // set value
128 : void setValue(const size_t, const size_t, const double);
129 : void setValue(const std::vector<unsigned int>&, const std::vector<unsigned int>&, const double);
130 : double& operator()(const size_t, const size_t);
131 : const double& operator()(const size_t, const size_t) const;
132 : double& operator()(const std::vector<unsigned int>&, const std::vector<unsigned int>&);
133 : const double& operator()(const std::vector<unsigned int>&, const std::vector<unsigned int>&) const;
134 : //
135 : friend CoeffsVector operator*(const CoeffsMatrix&, const CoeffsVector&);
136 : // add to value
137 : void addToValue(const size_t, const size_t, const double);
138 : void addToValue(const std::vector<unsigned int>&, const std::vector<unsigned int>&, const double);
139 : // scale all values
140 : void scaleAllValues(const double);
141 : CoeffsMatrix& operator*=(const double);
142 : friend CoeffsMatrix operator*(const double, const CoeffsMatrix&);
143 : friend CoeffsMatrix operator*(const CoeffsMatrix&, const double);
144 : CoeffsMatrix& operator*=(const CoeffsMatrix&);
145 : CoeffsMatrix operator*(const CoeffsMatrix&) const;
146 : // set all values
147 : void setValues(const double);
148 : void setValues(const std::vector<double>&);
149 : void setValues(const CoeffsMatrix&);
150 : CoeffsMatrix& operator=(const double);
151 : CoeffsMatrix& operator=(const std::vector<double>&);
152 : // CoeffsMatrix& operator=(const CoeffsMatrix&);
153 : // add to all values
154 : CoeffsMatrix operator+() const;
155 : CoeffsMatrix operator-() const;
156 : void addToValues(const double);
157 : void addToValues(const std::vector<double>&);
158 : void addToValues(const CoeffsMatrix&);
159 : void subtractFromValues(const double);
160 : void subtractFromValues(const std::vector<double>&);
161 : void subtractFromValues(const CoeffsMatrix&);
162 : CoeffsMatrix& operator+=(const double);
163 : friend CoeffsMatrix operator+(const double, const CoeffsMatrix&);
164 : friend CoeffsMatrix operator+(const CoeffsMatrix&, const double);
165 : CoeffsMatrix& operator+=(const std::vector<double>&);
166 : friend CoeffsMatrix operator+(const std::vector<double>&, const CoeffsMatrix&);
167 : friend CoeffsMatrix operator+(const CoeffsMatrix&, const std::vector<double>&);
168 : CoeffsMatrix& operator-=(const double);
169 : friend CoeffsMatrix operator-(const double, const CoeffsMatrix&);
170 : friend CoeffsMatrix operator-(const CoeffsMatrix&, const double);
171 : CoeffsMatrix& operator-=(const std::vector<double>&);
172 : friend CoeffsMatrix operator-(const std::vector<double>&, const CoeffsMatrix&);
173 : friend CoeffsMatrix operator-(const CoeffsMatrix&, const std::vector<double>&);
174 : CoeffsMatrix& operator+=(const CoeffsMatrix&);
175 : CoeffsMatrix operator+(const CoeffsMatrix&) const;
176 : CoeffsMatrix& operator-=(const CoeffsMatrix&);
177 : CoeffsMatrix operator-(const CoeffsMatrix&) const;
178 : //
179 : static void averageMatrices(CoeffsMatrix&, CoeffsMatrix&);
180 : static void averageMatrices(const std::vector<CoeffsMatrix*>&);
181 : //
182 : double getMinValue() const;
183 : double getMaxValue() const;
184 : //
185 : void randomizeValuesGaussian(int);
186 : //
187 0 : void resetAveragingCounter() {averaging_counter=0;}
188 : void setupExponentiallyDecayingAveraging(const unsigned int averaging_exp_decay_in) {averaging_exp_decay_=averaging_exp_decay_in;}
189 : void turnOffExponentiallyDecayingAveraging() { averaging_exp_decay_=0;}
190 : void resetAveraging();
191 : void addToAverage(const CoeffsMatrix&);
192 : void addToAverage(const CoeffsMatrix&, const unsigned int);
193 : //
194 : // file input/output stuff
195 : void writeToFile(OFile&);
196 : void writeToFile(const std::string&, const bool append_file=false, Action* action_pntr=NULL);
197 : private:
198 : void writeDataToFile(OFile&);
199 : void writeMatrixInfoToFile(OFile&);
200 : void writeHeaderToFile(OFile&);
201 : void writeDataDiagonalToFile(OFile&);
202 : void writeDataFullToFile(OFile&);
203 : public:
204 : Communicator& getCommunicator() const {return mycomm;}
205 :
206 : };
207 : }
208 : }
209 :
210 :
211 : #endif
|