Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2016-2018 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_CoeffsVector_h
23 : #define __PLUMED_ves_CoeffsVector_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 CoeffsMatrix;
44 :
45 :
46 10994 : class CoeffsVector:
47 : public CoeffsBase
48 : {
49 : public:
50 : private:
51 : std::vector<double> data;
52 : //
53 : unsigned int averaging_counter;
54 : unsigned int averaging_exp_decay_;
55 : //
56 : Communicator& mycomm;
57 : //
58 : CoeffsVector& operator=(const CoeffsVector&);
59 : public:
60 : explicit CoeffsVector(
61 : const std::string&,
62 : const std::vector<std::string>&,
63 : const std::vector<unsigned int>&,
64 : Communicator&,
65 : const bool use_counter=false);
66 : //
67 : explicit CoeffsVector(
68 : const std::string&,
69 : std::vector<Value*>&,
70 : std::vector<BasisFunctions*>&,
71 : Communicator&,
72 : const bool use_counter=false);
73 : //
74 : explicit CoeffsVector(
75 : const std::string&,
76 : std::vector<std::vector<Value*> >&,
77 : std::vector<std::vector<BasisFunctions*> >&,
78 : Communicator&,
79 : const bool use_counter=false,
80 : const std::string& multicoeffs_label="bias");
81 : //
82 : explicit CoeffsVector(
83 : const std::string&,
84 : CoeffsMatrix*,
85 : Communicator&);
86 : //
87 : ~CoeffsVector();
88 : //
89 : size_t getSize() const {return numberOfCoeffs();}
90 : // clear coeffs
91 : void clear();
92 : void setAllValuesToZero();
93 : //
94 : std::vector<double> getDataAsVector() const {return data;}
95 : //
96 : bool sameShape(CoeffsVector&) const;
97 : bool sameShape(CoeffsMatrix&) const;
98 : static bool sameShape(CoeffsVector&, CoeffsVector&);
99 : //
100 : void resizeCoeffs(const std::vector<unsigned int>&);
101 : void resizeCoeffs(std::vector<BasisFunctions*>&);
102 : //
103 : void sumCommMPI();
104 : void sumCommMPI(Communicator& cc);
105 : //
106 : void sumMultiSimCommMPI(Communicator&);
107 : // get value
108 : double getValue(const size_t) const;
109 : double getValue(const std::vector<unsigned int>&) const;
110 : double& operator[](const size_t index);
111 : const double& operator[](const size_t index) const;
112 : double& operator[](const std::vector<unsigned int>&);
113 : const double& operator[](const std::vector<unsigned int>&) const;
114 : double& operator()(const size_t index);
115 : const double& operator()(const size_t index) const;
116 : double& operator()(const std::vector<unsigned int>&);
117 : const double& operator()(const std::vector<unsigned int>&) const;
118 : // set value
119 : void setValue(const size_t, const double);
120 : void setValue(const std::vector<unsigned int>&, const double);
121 : // add to value
122 : void addToValue(const size_t, const double);
123 : void addToValue(const std::vector<unsigned int>&, const double);
124 : // scale all values
125 : void scaleAllValues(const double);
126 : CoeffsVector& operator*=(const double);
127 : friend CoeffsVector operator*(const double, const CoeffsVector&);
128 : friend CoeffsVector operator*(const CoeffsVector&, const double);
129 : CoeffsVector& operator*=(const CoeffsVector&);
130 : CoeffsVector operator*(const CoeffsVector&) const;
131 : // set all values
132 : void setValues(const double);
133 : void setValues(const std::vector<double>&);
134 : void setValues(const CoeffsVector&);
135 : CoeffsVector& operator=(const double);
136 : CoeffsVector& operator=(const std::vector<double>&);
137 : // CoeffsVector& operator=(const CoeffsVector&);
138 : // add to all values
139 : CoeffsVector operator+() const;
140 : CoeffsVector operator-() const;
141 : void addToValues(const double);
142 : void addToValues(const std::vector<double>&);
143 : void addToValues(const CoeffsVector&);
144 : void subtractFromValues(const double);
145 : void subtractFromValues(const std::vector<double>&);
146 : void subtractFromValues(const CoeffsVector&);
147 : CoeffsVector& operator+=(const double);
148 : friend CoeffsVector operator+(const double, const CoeffsVector&);
149 : friend CoeffsVector operator+(const CoeffsVector&, const double);
150 : CoeffsVector& operator+=(const std::vector<double>&);
151 : friend CoeffsVector operator+(const std::vector<double>&, const CoeffsVector&);
152 : friend CoeffsVector operator+(const CoeffsVector&, const std::vector<double>&);
153 : CoeffsVector& operator-=(const double);
154 : friend CoeffsVector operator-(const double, const CoeffsVector&);
155 : friend CoeffsVector operator-(const CoeffsVector&, const double);
156 : CoeffsVector& operator-=(const std::vector<double>&);
157 : friend CoeffsVector operator-(const std::vector<double>&, const CoeffsVector&);
158 : friend CoeffsVector operator-(const CoeffsVector&, const std::vector<double>&);
159 : CoeffsVector& operator+=(const CoeffsVector&);
160 : CoeffsVector operator+(const CoeffsVector&) const;
161 : CoeffsVector& operator-=(const CoeffsVector&);
162 : CoeffsVector operator-(const CoeffsVector&) const;
163 : //
164 : void setValuesFromDifferentShape(const CoeffsVector&);
165 : //
166 : static void averageVectors(CoeffsVector&, CoeffsVector&);
167 : static void averageVectors(const std::vector<CoeffsVector*>&);
168 : //
169 : double getMinValue() const;
170 : double getMinValue(size_t&) const;
171 : double getMinAbsValue() const;
172 : double getMinAbsValue(size_t&) const;
173 : //
174 : double getMaxValue() const;
175 : double getMaxValue(size_t&) const;
176 : double getMaxAbsValue() const;
177 : double getMaxAbsValue(size_t&) const;
178 : //
179 : double getNorm() const;
180 : double getL1Norm() const;
181 : double getL2Norm() const;
182 : double getLpNorm(const double) const;
183 : double getRMS() const;
184 : //
185 : void normalizeCoeffs();
186 : // Random values
187 : void randomizeValuesGaussian(int);
188 : //
189 0 : void resetAveragingCounter() {averaging_counter=0;}
190 1 : void setupExponentiallyDecayingAveraging(const unsigned int averaging_exp_decay_in) {averaging_exp_decay_=averaging_exp_decay_in;}
191 : void turnOffExponentiallyDecayingAveraging() {averaging_exp_decay_=0;}
192 : void resetAveraging();
193 : void addToAverage(const CoeffsVector&);
194 : //
195 : size_t countValues(const double) const;
196 :
197 : // file input/output stuff
198 : void writeToFile(const std::string&, const bool print_description=false, const bool append_file=false, Action* action_pntr=NULL);
199 : void writeToFile(OFile&, const bool print_description=false);
200 : void writeToFile(OFile& ofile, CoeffsVector*, const bool print_coeffs_descriptions=false);
201 : static void writeToFile(const std::string&, const std::vector<CoeffsVector*>&, const bool print_description=false, const bool append_file=false, Action* action_pntr=NULL);
202 : static void writeToFile(OFile&, const std::vector<CoeffsVector*>&, const bool print_description=false);
203 : private:
204 : void writeHeaderToFile(OFile&) const;
205 : static void writeDataToFile(OFile&, const std::vector<CoeffsVector*>&, const bool print_description=false);
206 : public:
207 : size_t readFromFile(IFile&, const bool ignore_missing_coeffs=false, const bool ignore_header=false);
208 : size_t readFromFile(const std::string&, const bool ignore_missing_coeffs=false, const bool ignore_header=false);
209 : size_t readOneSetFromFile(IFile& ifile, const bool ignore_header=false);
210 : private:
211 : void readHeaderFromFile(IFile&, const bool ignore_coeffs_info=false);
212 : size_t readDataFromFile(IFile&, const bool ignore_missing_coeffs=false);
213 : public:
214 0 : Communicator& getCommunicator() const {return mycomm;}
215 : };
216 :
217 :
218 : inline
219 : double CoeffsVector::getValue(const size_t index) const {
220 131824425 : return data[index];
221 : }
222 :
223 :
224 : inline
225 : double CoeffsVector::getValue(const std::vector<unsigned int>& indices) const {
226 : return data[getIndex(indices)];
227 : }
228 :
229 :
230 : }
231 :
232 : }
233 :
234 :
235 : #endif
|