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 :
23 : #include "CoeffsMatrix.h"
24 : #include "CoeffsVector.h"
25 : #include "BasisFunctions.h"
26 :
27 : #include "tools/Tools.h"
28 : #include "core/Value.h"
29 : #include "tools/File.h"
30 : #include "tools/Exception.h"
31 : #include "tools/Random.h"
32 : #include "tools/Communicator.h"
33 :
34 : #include <vector>
35 : #include <cmath>
36 : #include <iostream>
37 : #include <sstream>
38 : #include <cstdio>
39 : #include <cfloat>
40 :
41 :
42 : namespace PLMD {
43 : namespace ves {
44 :
45 0 : CoeffsMatrix::CoeffsMatrix(
46 : const std::string& label,
47 : const std::vector<std::string>& dimension_labels,
48 : const std::vector<unsigned int>& indices_shape,
49 : Communicator& cc,
50 : const bool diagonal,
51 0 : const bool use_iteration_counter):
52 : CoeffsBase(label,dimension_labels,indices_shape,use_iteration_counter),
53 0 : data(0),
54 0 : size_(0),
55 0 : nrows_(0),
56 0 : ncolumns_(0),
57 0 : diagonal_(diagonal),
58 0 : averaging_counter(0),
59 0 : averaging_exp_decay_(0),
60 0 : mycomm(cc) {
61 0 : setupMatrix();
62 0 : }
63 :
64 :
65 0 : CoeffsMatrix::CoeffsMatrix(
66 : const std::string& label,
67 : std::vector<Value*>& args,
68 : std::vector<BasisFunctions*>& basisf,
69 : Communicator& cc,
70 : const bool diagonal,
71 0 : const bool use_iteration_counter):
72 : CoeffsBase(label,args,basisf,use_iteration_counter),
73 0 : data(0),
74 0 : size_(0),
75 0 : nrows_(0),
76 0 : ncolumns_(0),
77 0 : diagonal_(diagonal),
78 0 : averaging_counter(0),
79 0 : averaging_exp_decay_(0),
80 0 : mycomm(cc) {
81 0 : setupMatrix();
82 0 : }
83 :
84 :
85 0 : CoeffsMatrix::CoeffsMatrix(
86 : const std::string& label,
87 : std::vector<std::vector<Value*> >& argsv,
88 : std::vector<std::vector<BasisFunctions*> >& basisfv,
89 : Communicator& cc,
90 : const bool diagonal,
91 : const bool use_iteration_counter,
92 0 : const std::string& multicoeffs_label):
93 : CoeffsBase(label,argsv,basisfv,use_iteration_counter,multicoeffs_label),
94 0 : data(0),
95 0 : size_(0),
96 0 : nrows_(0),
97 0 : ncolumns_(0),
98 0 : diagonal_(diagonal),
99 0 : averaging_counter(0),
100 0 : averaging_exp_decay_(0),
101 0 : mycomm(cc) {
102 0 : setupMatrix();
103 0 : }
104 :
105 :
106 173 : CoeffsMatrix::CoeffsMatrix(
107 : const std::string& label,
108 : CoeffsVector* coeffsVec,
109 : Communicator& cc,
110 173 : const bool diagonal):
111 : CoeffsBase( *(static_cast<CoeffsBase*>(coeffsVec)) ),
112 173 : data(0),
113 173 : size_(0),
114 173 : nrows_(0),
115 173 : ncolumns_(0),
116 173 : diagonal_(diagonal),
117 173 : averaging_counter(0),
118 173 : averaging_exp_decay_(0),
119 173 : mycomm(cc) {
120 173 : setLabels(label);
121 173 : setupMatrix();
122 173 : }
123 :
124 :
125 173 : CoeffsMatrix::~CoeffsMatrix() {}
126 :
127 :
128 173 : void CoeffsMatrix::setupMatrix() {
129 173 : nrows_=numberOfCoeffs();
130 173 : ncolumns_=nrows_;
131 173 : if(diagonal_) {
132 173 : size_=nrows_;
133 : } else {
134 0 : size_=(nrows_*nrows_-nrows_)/2+nrows_;
135 : }
136 173 : clear();
137 173 : }
138 :
139 :
140 441 : size_t CoeffsMatrix::getSize() const {
141 441 : return size_;
142 : }
143 :
144 :
145 23395 : bool CoeffsMatrix::isDiagonal() const {
146 23395 : return diagonal_;
147 : }
148 :
149 :
150 0 : bool CoeffsMatrix::sameShape(CoeffsVector& coeffsvector_in) const {
151 0 : return CoeffsBase::sameShape( *(static_cast<CoeffsBase*>(&coeffsvector_in)) );
152 : }
153 :
154 :
155 0 : bool CoeffsMatrix::sameShape(CoeffsMatrix& coeffsmat_in) const {
156 0 : return CoeffsBase::sameShape( *(static_cast<CoeffsBase*>(&coeffsmat_in)) );
157 : }
158 :
159 :
160 0 : bool CoeffsMatrix::sameShape(CoeffsMatrix& coeffsmat0, CoeffsMatrix& coeffsmat1) {
161 0 : return coeffsmat0.sameShape(coeffsmat1);
162 : }
163 :
164 :
165 0 : bool CoeffsMatrix::sameShape(CoeffsVector& coeffsvec, CoeffsMatrix& coeffsmat) {
166 0 : return coeffsmat.sameShape(coeffsvec);
167 : }
168 :
169 :
170 0 : bool CoeffsMatrix::sameShape(CoeffsMatrix& coeffsmat, CoeffsVector& coeffsvec) {
171 0 : return coeffsmat.sameShape(coeffsvec);
172 : }
173 :
174 :
175 0 : void CoeffsMatrix::sumCommMPI() {
176 0 : mycomm.Sum(data);
177 0 : }
178 :
179 :
180 0 : void CoeffsMatrix::sumCommMPI(Communicator& cc) {
181 0 : cc.Sum(data);
182 0 : }
183 :
184 :
185 0 : void CoeffsMatrix::sumMultiSimCommMPI(Communicator& multi_sim_cc) {
186 0 : if(mycomm.Get_rank()==0) {
187 0 : double nwalkers = static_cast<double>(multi_sim_cc.Get_size());
188 0 : multi_sim_cc.Sum(data);
189 0 : scaleAllValues(1.0/nwalkers);
190 : }
191 0 : mycomm.Bcast(data,0);
192 0 : }
193 :
194 :
195 5426050 : size_t CoeffsMatrix::getMatrixIndex(const size_t index1, const size_t index2) const {
196 : size_t matrix_idx;
197 : plumed_dbg_assert(index1<nrows_);
198 : plumed_dbg_assert(index2<ncolumns_);
199 5426050 : if(diagonal_) {
200 : matrix_idx=index1;
201 0 : } else if (index1<=index2) {
202 0 : matrix_idx=index2+index1*(nrows_-1)-index1*(index1-1)/2;
203 : } else {
204 0 : matrix_idx=index1+index2*(nrows_-1)-index2*(index2-1)/2;
205 : }
206 5426050 : return matrix_idx;
207 : }
208 :
209 :
210 268 : void CoeffsMatrix::clear() {
211 268 : data.resize(getSize());
212 19393 : for(size_t i=0; i<data.size(); i++) {
213 19125 : data[i]=0.0;
214 : }
215 268 : }
216 :
217 :
218 0 : void CoeffsMatrix::setAllValuesToZero() {
219 0 : for(size_t i=0; i<data.size(); i++) {
220 0 : data[i]=0.0;
221 : }
222 0 : }
223 :
224 :
225 27630 : double CoeffsMatrix::getValue(const size_t index1, const size_t index2) const {
226 27630 : return data[getMatrixIndex(index1,index2)];
227 : }
228 :
229 :
230 0 : double CoeffsMatrix::getValue(const std::vector<unsigned int>& indices1, const std::vector<unsigned int>& indices2) const {
231 0 : return getValue(getIndex(indices1),getIndex(indices2));
232 : }
233 :
234 :
235 219 : void CoeffsMatrix::setValue(const size_t index1, const size_t index2, const double value) {
236 219 : data[getMatrixIndex(index1,index2)]=value;
237 219 : }
238 :
239 :
240 0 : void CoeffsMatrix::setValue(const std::vector<unsigned int>& indices1, const std::vector<unsigned int>& indices2, const double value) {
241 0 : setValue(getIndex(indices1),getIndex(indices2),value);
242 0 : }
243 :
244 :
245 0 : double& CoeffsMatrix::operator()(const size_t index1, const size_t index2) {
246 0 : return data[getMatrixIndex(index1,index2)];
247 : }
248 :
249 :
250 1790345 : const double& CoeffsMatrix::operator()(const size_t index1, const size_t index2) const {
251 1790345 : return data[getMatrixIndex(index1,index2)];
252 : }
253 :
254 :
255 0 : double& CoeffsMatrix::operator()(const std::vector<unsigned int>& indices1, const std::vector<unsigned int>& indices2) {
256 0 : return data[getMatrixIndex(getIndex(indices1),getIndex(indices2))];
257 : }
258 :
259 :
260 0 : const double& CoeffsMatrix::operator()(const std::vector<unsigned int>& indices1, const std::vector<unsigned int>& indices2) const {
261 0 : return data[getMatrixIndex(getIndex(indices1),getIndex(indices2))];
262 : }
263 :
264 :
265 22735 : CoeffsVector operator*(const CoeffsMatrix& coeffs_matrix, const CoeffsVector& coeffs_vector) {
266 22735 : CoeffsVector new_coeffs_vector(coeffs_vector);
267 22735 : new_coeffs_vector.clear();
268 22735 : plumed_massert(coeffs_vector.numberOfCoeffs()==coeffs_matrix.numberOfCoeffs(),"CoeffsMatrix and CoeffsVector are of the wrong size");
269 : size_t numcoeffs = coeffs_vector.numberOfCoeffs();
270 22735 : if(coeffs_matrix.isDiagonal()) {
271 1813080 : for(size_t i=0; i<numcoeffs; i++) {
272 1790345 : new_coeffs_vector(i) = coeffs_matrix(i,i)*coeffs_vector(i);
273 : }
274 : } else {
275 0 : for(size_t i=0; i<numcoeffs; i++) {
276 0 : for(size_t j=0; j<numcoeffs; j++) {
277 0 : new_coeffs_vector(i) += coeffs_matrix(i,j)*coeffs_vector(j);
278 : }
279 : }
280 : }
281 22735 : return new_coeffs_vector;
282 0 : }
283 :
284 :
285 0 : void CoeffsMatrix::addToValue(const size_t index1, const size_t index2, const double value) {
286 0 : data[getMatrixIndex(index1,index2)]+=value;
287 0 : }
288 :
289 :
290 0 : void CoeffsMatrix::addToValue(const std::vector<unsigned int>& indices1, const std::vector<unsigned int>& indices2, const double value) {
291 0 : addToValue(getIndex(indices1),getIndex(indices2),value);
292 0 : }
293 :
294 :
295 22810 : void CoeffsMatrix::scaleAllValues(const double scalef) {
296 1823850 : for(size_t i=0; i<data.size(); i++) {
297 1801040 : data[i]*=scalef;
298 : }
299 22810 : }
300 :
301 :
302 22810 : CoeffsMatrix& CoeffsMatrix::operator*=(const double scalef) {
303 22810 : scaleAllValues(scalef);
304 22810 : return *this;
305 : }
306 :
307 :
308 0 : CoeffsMatrix operator*(const double scalef, const CoeffsMatrix& coeffsmatrix) {
309 0 : return CoeffsMatrix(coeffsmatrix)*=scalef;
310 : }
311 :
312 :
313 0 : CoeffsMatrix operator*(const CoeffsMatrix& coeffsmatrix, const double scalef) {
314 0 : return scalef*coeffsmatrix;
315 : }
316 :
317 :
318 0 : CoeffsMatrix& CoeffsMatrix::operator*=(const CoeffsMatrix& other_coeffsmatrix) {
319 0 : plumed_massert(data.size()==other_coeffsmatrix.getSize(),"Coeffs matrices do not have the same size");
320 0 : for(size_t i=0; i<data.size(); i++) {
321 0 : data[i]*=other_coeffsmatrix.data[i];
322 : }
323 0 : return *this;
324 : }
325 :
326 :
327 0 : CoeffsMatrix CoeffsMatrix::operator*(const CoeffsMatrix& other_coeffsmatrix) const {
328 0 : return CoeffsMatrix(*this)*=other_coeffsmatrix;
329 : }
330 :
331 :
332 0 : void CoeffsMatrix::setValues(const double value) {
333 0 : for(size_t i=0; i<data.size(); i++) {
334 0 : data[i]=value;
335 : }
336 0 : }
337 :
338 :
339 22810 : void CoeffsMatrix::setValues(const std::vector<double>& values) {
340 22810 : plumed_massert( data.size()==values.size(), "Incorrect size");
341 1823850 : for(size_t i=0; i<data.size(); i++) {
342 1801040 : data[i]=values[i];
343 : }
344 22810 : }
345 :
346 :
347 0 : void CoeffsMatrix::setValues(const CoeffsMatrix& other_coeffsmatrix) {
348 0 : plumed_massert( data.size()==other_coeffsmatrix.getSize(), "Incorrect size");
349 0 : for(size_t i=0; i<data.size(); i++) {
350 0 : data[i]=other_coeffsmatrix.data[i];
351 : }
352 0 : }
353 :
354 :
355 0 : CoeffsMatrix& CoeffsMatrix::operator=(const double value) {
356 0 : setValues(value);
357 0 : return *this;
358 : }
359 :
360 :
361 22810 : CoeffsMatrix& CoeffsMatrix::operator=(const std::vector<double>& values) {
362 22810 : setValues(values);
363 22810 : return *this;
364 : }
365 :
366 :
367 :
368 :
369 0 : CoeffsMatrix CoeffsMatrix::operator+() const {
370 0 : return *this;
371 : }
372 :
373 :
374 0 : CoeffsMatrix CoeffsMatrix::operator-() const {
375 0 : return CoeffsMatrix(*this)*=-1.0;
376 : }
377 :
378 :
379 0 : void CoeffsMatrix::addToValues(const double value) {
380 0 : for(size_t i=0; i<data.size(); i++) {
381 0 : data[i]+=value;
382 : }
383 0 : }
384 :
385 :
386 0 : void CoeffsMatrix::addToValues(const std::vector<double>& values) {
387 0 : plumed_massert( data.size()==values.size(), "Incorrect size");
388 0 : for(size_t i=0; i<data.size(); i++) {
389 0 : data[i]+=values[i];
390 : }
391 0 : }
392 :
393 :
394 0 : void CoeffsMatrix::addToValues(const CoeffsMatrix& other_coeffsmatrix) {
395 0 : plumed_massert( data.size()==other_coeffsmatrix.getSize(), "Incorrect size");
396 0 : for(size_t i=0; i<data.size(); i++) {
397 0 : data[i]+=other_coeffsmatrix.data[i];
398 : }
399 0 : }
400 :
401 :
402 0 : void CoeffsMatrix::subtractFromValues(const double value) {
403 0 : for(size_t i=0; i<data.size(); i++) {
404 0 : data[i]-=value;
405 : }
406 0 : }
407 :
408 :
409 0 : void CoeffsMatrix::subtractFromValues(const std::vector<double>& values) {
410 0 : plumed_massert( data.size()==values.size(), "Incorrect size");
411 0 : for(size_t i=0; i<data.size(); i++) {
412 0 : data[i]-=values[i];
413 : }
414 0 : }
415 :
416 :
417 0 : void CoeffsMatrix::subtractFromValues(const CoeffsMatrix& other_coeffsmatrix) {
418 0 : plumed_massert( data.size()==other_coeffsmatrix.getSize(), "Incorrect size");
419 0 : for(size_t i=0; i<data.size(); i++) {
420 0 : data[i]-=other_coeffsmatrix.data[i];
421 : }
422 0 : }
423 :
424 :
425 0 : CoeffsMatrix& CoeffsMatrix::operator+=(const double value) {
426 0 : addToValues(value);
427 0 : return *this;
428 : }
429 :
430 :
431 0 : CoeffsMatrix operator+(const double value, const CoeffsMatrix& coeffsmatrix) {
432 0 : return coeffsmatrix+value;
433 : }
434 :
435 :
436 0 : CoeffsMatrix operator+(const CoeffsMatrix& coeffsmatrix, const double value) {
437 0 : return CoeffsMatrix(coeffsmatrix)+=value;
438 : }
439 :
440 :
441 0 : CoeffsMatrix& CoeffsMatrix::operator+=(const std::vector<double>& values) {
442 0 : addToValues(values);
443 0 : return *this;
444 : }
445 :
446 :
447 0 : CoeffsMatrix operator+(const std::vector<double>& values, const CoeffsMatrix& coeffsmatrix) {
448 0 : return coeffsmatrix+values;
449 : }
450 :
451 :
452 0 : CoeffsMatrix operator+(const CoeffsMatrix& coeffsmatrix, const std::vector<double>& values) {
453 0 : return CoeffsMatrix(coeffsmatrix)+=values;
454 : }
455 :
456 :
457 0 : CoeffsMatrix& CoeffsMatrix::operator-=(const double value) {
458 0 : subtractFromValues(value);
459 0 : return *this;
460 : }
461 :
462 :
463 0 : CoeffsMatrix operator-(const double value, const CoeffsMatrix& coeffsmatrix) {
464 0 : return -1.0*coeffsmatrix+value;
465 : }
466 :
467 :
468 0 : CoeffsMatrix operator-(const CoeffsMatrix& coeffsmatrix, const double value) {
469 0 : return CoeffsMatrix(coeffsmatrix)-=value;
470 : }
471 :
472 :
473 0 : CoeffsMatrix& CoeffsMatrix::operator-=(const std::vector<double>& values) {
474 0 : subtractFromValues(values);
475 0 : return *this;
476 : }
477 :
478 :
479 0 : CoeffsMatrix operator-(const std::vector<double>& values, const CoeffsMatrix& coeffsmatrix) {
480 0 : return -1.0*coeffsmatrix+values;
481 : }
482 :
483 :
484 0 : CoeffsMatrix operator-(const CoeffsMatrix& coeffsmatrix, const std::vector<double>& values) {
485 0 : return CoeffsMatrix(coeffsmatrix)-=values;
486 : }
487 :
488 :
489 0 : CoeffsMatrix& CoeffsMatrix::operator+=(const CoeffsMatrix& other_coeffsmatrix) {
490 0 : addToValues(other_coeffsmatrix);
491 0 : return *this;
492 : }
493 :
494 :
495 0 : CoeffsMatrix CoeffsMatrix::operator+(const CoeffsMatrix& other_coeffsmatrix) const {
496 0 : return CoeffsMatrix(*this)+=other_coeffsmatrix;
497 : }
498 :
499 :
500 0 : CoeffsMatrix& CoeffsMatrix::operator-=(const CoeffsMatrix& other_coeffsmatrix) {
501 0 : subtractFromValues(other_coeffsmatrix);
502 0 : return *this;
503 : }
504 :
505 :
506 0 : CoeffsMatrix CoeffsMatrix::operator-(const CoeffsMatrix& other_coeffsmatrix) const {
507 0 : return CoeffsMatrix(*this)-=other_coeffsmatrix;
508 : }
509 :
510 :
511 0 : void CoeffsMatrix::averageMatrices(CoeffsMatrix& coeffsmat0, CoeffsMatrix& coeffsmat1) {
512 0 : plumed_massert(sameShape(coeffsmat0,coeffsmat1),"both CoeffsMatrix objects need to have the same shape");
513 0 : for(size_t i=0; i<coeffsmat0.getSize(); i++) {
514 0 : coeffsmat0.data[i] = coeffsmat1.data[i] = 0.5 * (coeffsmat0.data[i]+coeffsmat1.data[i]);
515 : }
516 0 : }
517 :
518 :
519 0 : void CoeffsMatrix::averageMatrices(const std::vector<CoeffsMatrix*>& coeffsmatSet) {
520 0 : double norm_factor = 1.0/static_cast<double>(coeffsmatSet.size());
521 0 : for(unsigned int k=1; k<coeffsmatSet.size(); k++) {
522 0 : plumed_massert(coeffsmatSet[0]->sameShape(*coeffsmatSet[k]),"All CoeffsMatrix objects need to have the same shape");
523 : }
524 0 : for(size_t i=0; i<coeffsmatSet[0]->getSize(); i++) {
525 : double value = 0.0;
526 0 : for(unsigned int k=0; k<coeffsmatSet.size(); k++) {
527 0 : value += coeffsmatSet[k]->data[i];
528 : }
529 0 : value *= norm_factor;
530 0 : for(unsigned int k=0; k<coeffsmatSet.size(); k++) {
531 0 : coeffsmatSet[k]->data[i] = value;
532 : }
533 : }
534 0 : }
535 :
536 :
537 :
538 0 : double CoeffsMatrix::getMinValue() const {
539 : double min_value=DBL_MAX;
540 0 : for(size_t i=0; i<data.size(); i++) {
541 0 : if(data[i]<min_value) {
542 : min_value=data[i];
543 : }
544 : }
545 0 : return min_value;
546 : }
547 :
548 :
549 0 : double CoeffsMatrix::getMaxValue() const {
550 : double max_value=DBL_MIN;
551 0 : for(size_t i=0; i<data.size(); i++) {
552 0 : if(data[i]>max_value) {
553 : max_value=data[i];
554 : }
555 : }
556 0 : return max_value;
557 : }
558 :
559 :
560 0 : void CoeffsMatrix::randomizeValuesGaussian(int randomSeed) {
561 0 : Random rnd;
562 0 : if (randomSeed<0) {
563 : randomSeed = -randomSeed;
564 : }
565 0 : rnd.setSeed(-randomSeed);
566 0 : for(size_t i=0; i<data.size(); i++) {
567 0 : data[i]=rnd.Gaussian();
568 : }
569 0 : }
570 :
571 :
572 0 : void CoeffsMatrix::resetAveraging() {
573 0 : clear();
574 : resetAveragingCounter();
575 0 : }
576 :
577 :
578 0 : void CoeffsMatrix::addToAverage(const CoeffsMatrix& coeffsmat) {
579 0 : plumed_massert( data.size()==coeffsmat.getSize(), "Incorrect size");
580 : //
581 0 : double aver_decay = 1.0 / ( static_cast<double>(averaging_counter) + 1.0 );
582 0 : if(averaging_exp_decay_>0 && (averaging_counter+1 > averaging_exp_decay_) ) {
583 0 : aver_decay = 1.0 / static_cast<double>(averaging_exp_decay_);
584 : }
585 : //
586 0 : for(size_t i=0; i<data.size(); i++) {
587 0 : data[i]+=(coeffsmat.data[i]-data[i])*aver_decay;
588 : }
589 0 : averaging_counter++;
590 0 : }
591 :
592 :
593 660 : void CoeffsMatrix::writeToFile(OFile& ofile) {
594 660 : writeHeaderToFile(ofile);
595 660 : writeDataToFile(ofile);
596 660 : }
597 :
598 :
599 0 : void CoeffsMatrix::writeToFile(const std::string& filepath, const bool append_file, Action* action_pntr) {
600 0 : OFile file;
601 0 : if(action_pntr!=NULL) {
602 0 : file.link(*action_pntr);
603 : } else {
604 0 : file.link(mycomm);
605 : }
606 0 : if(append_file) {
607 0 : file.enforceRestart();
608 : }
609 0 : file.open(filepath);
610 0 : writeToFile(file);
611 0 : file.close();
612 0 : }
613 :
614 :
615 660 : void CoeffsMatrix::writeMatrixInfoToFile(OFile& ofile) {
616 660 : std::string field_diagonal = "diagonal_matrix";
617 660 : ofile.addConstantField(field_diagonal).printField(field_diagonal,isDiagonal());
618 660 : }
619 :
620 :
621 660 : void CoeffsMatrix::writeHeaderToFile(OFile& ofile) {
622 660 : ofile.clearFields();
623 660 : if(isIterationCounterActive()) {
624 660 : writeIterationCounterAndTimeToFile(ofile);
625 : }
626 660 : writeCoeffsInfoToFile(ofile);
627 660 : writeMatrixInfoToFile(ofile);
628 660 : }
629 :
630 :
631 660 : void CoeffsMatrix::writeDataToFile(OFile& ofile) {
632 660 : if(diagonal_) {
633 660 : writeDataDiagonalToFile(ofile);
634 : } else {
635 0 : writeDataFullToFile(ofile);
636 : }
637 660 : }
638 :
639 :
640 660 : void CoeffsMatrix::writeDataDiagonalToFile(OFile& ofile) {
641 : //
642 660 : std::string field_indices_prefix = "idx_";
643 : std::string field_coeffs = getDataLabel();
644 660 : std::string field_index = "index";
645 : //
646 660 : std::string int_fmt = "%8d";
647 660 : std::string str_separate = "#!-------------------";
648 : //
649 660 : std::vector<char> s1(20);
650 660 : std::vector<unsigned int> indices(numberOfDimensions());
651 660 : std::vector<std::string> ilabels(numberOfDimensions());
652 1520 : for(unsigned int k=0; k<numberOfDimensions(); k++) {
653 1720 : ilabels[k]=field_indices_prefix+getDimensionLabel(k);
654 : }
655 : //
656 28290 : for(size_t i=0; i<numberOfCoeffs(); i++) {
657 27630 : indices=getIndices(i);
658 77540 : for(unsigned int k=0; k<numberOfDimensions(); k++) {
659 49910 : std::snprintf(s1.data(),s1.size(),int_fmt.c_str(),indices[k]);
660 99820 : ofile.printField(ilabels[k],s1.data());
661 : }
662 55260 : ofile.fmtField(" "+getOutputFmt()).printField(field_coeffs,getValue(i,i));
663 : std::snprintf(s1.data(),s1.size(),int_fmt.c_str(),i);
664 27630 : ofile.printField(field_index,s1.data());
665 27630 : ofile.printField();
666 : }
667 660 : ofile.fmtField();
668 : // blank line between iterations to allow proper plotting with gnuplot
669 660 : ofile.printf("%s\n",str_separate.c_str());
670 660 : ofile.printf("\n");
671 660 : ofile.printf("\n");
672 1980 : }
673 :
674 :
675 0 : void CoeffsMatrix::writeDataFullToFile(OFile& ofile) {
676 : //
677 0 : std::string field_index_row = "idx_row";
678 0 : std::string field_index_column = "idx_column";
679 : std::string field_coeffs = getDataLabel();
680 : //
681 0 : std::string int_fmt = "%8d";
682 0 : std::string str_separate = "#!-------------------";
683 : //
684 0 : std::vector<char> s1(20);
685 : //
686 0 : for(size_t i=0; i<nrows_; i++) {
687 0 : for(size_t j=0; j<ncolumns_; j++) {
688 : std::snprintf(s1.data(),s1.size(),int_fmt.c_str(),i);
689 0 : ofile.printField(field_index_row,s1.data());
690 : std::snprintf(s1.data(),s1.size(),int_fmt.c_str(),j);
691 0 : ofile.printField(field_index_column,s1.data());
692 0 : ofile.fmtField(" "+getOutputFmt()).printField(field_coeffs,getValue(i,j));
693 0 : ofile.printField();
694 : }
695 : }
696 0 : ofile.fmtField();
697 : // blank line between iterations to allow proper plotting with gnuplot
698 0 : ofile.printf("%s\n",str_separate.c_str());
699 0 : ofile.printf("\n");
700 0 : ofile.printf("\n");
701 0 : }
702 :
703 :
704 : }
705 : }
|