queso-0.53.0
TeuchosVector.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2015 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/Defines.h>
26 
27 #ifdef QUESO_HAS_TRILINOS
28 
29 #include <queso/TeuchosVector.h>
30 
31 namespace QUESO {
32 
33 using std:: cout;
34 using std:: endl;
35 
36 // constructor with dimension -----------------------
37 TeuchosVector::TeuchosVector(const BaseEnvironment& env, const Map& map)
38  :
39  Vector(env,map)
40 {
41  m_vec.size(map.NumGlobalElements());
42 
43  queso_require_msg(m_vec != NULL, "null vector generated");
44 
45  queso_require_equal_to_msg(m_vec.length(), map.NumMyElements(), "incompatible local vec size");
46 
47  queso_require_equal_to_msg(m_vec.length(), map.NumGlobalElements(), "incompatible global vec size");
48 
49  queso_require_equal_to_msg(m_vec.length(), m_map.NumMyElements(), "incompatible own vec size");
50 
51  //std::cout << "In TeuchosVector::constructor(env,map)"
52  // << "\n m_vec.length() = " << m_vec.length()
53  // << "\n map.NumGlobalElements() = " << map.NumGlobalElements()
54  // << "\n map.NumMyElements() = " << map.NumMyElements()
55  // << std::endl;
56 }
57 
58 // ---------------------------------------------------
59 TeuchosVector::TeuchosVector(const BaseEnvironment& env, const Map& map, double value)
60  :
61  Vector(env,map)
62 {
63  m_vec.size(map.NumGlobalElements());
64  m_vec = value;
65 
66  queso_require_msg(m_vec != NULL, "null vector generated");
67 
68  queso_require_equal_to_msg(m_vec.length(), map.NumMyElements(), "incompatible local vec size");
69 
70  queso_require_equal_to_msg(m_vec.length(), map.NumGlobalElements(), "incompatible global vec size");
71 
72  queso_require_equal_to_msg(m_vec.length(), m_map.NumMyElements(), "incompatible own vec size");
73 }
74 
75 // ---------------------------------------------------
76 TeuchosVector::TeuchosVector(const BaseEnvironment& env, double d1, double d2, const Map& map)
77  :
78  Vector(env,map)
79  {
80  m_vec.size(map.NumGlobalElements());
81 
82  queso_require_msg(m_vec != NULL, "null vector generated");
83 
84  queso_require_equal_to_msg(m_vec.length(), map.NumMyElements(), "incompatible local vec size");
85 
86  queso_require_equal_to_msg(m_vec.length(), map.NumGlobalElements(), "incompatible global vec size");
87 
88  for (int i = 0; i < m_vec.length(); ++i) {
89  double alpha = (double) i / ((double) m_vec.length() - 1.);
90  (*this)[i] = (1.-alpha)*d1 + alpha*d2;
91  }
92 
93  queso_require_equal_to_msg(m_vec.length(), m_map.NumMyElements(), "incompatible own vec size");
94 }
95 
96 // ---------------------------------------------------
97 TeuchosVector::TeuchosVector(const TeuchosVector& v, double d1, double d2)
98  :
99  Vector(v.env(),v.map())
100 {
101  m_vec.size(v.sizeLocal());
102 
103  queso_require_msg(m_vec != NULL, "null vector generated");
104 
105  queso_require_equal_to_msg(m_vec.length(), v.map().NumMyElements(), "incompatible local vec size");
106 
107  queso_require_equal_to_msg(m_vec.length(), v.map().NumGlobalElements(), "incompatible global vec size");
108 
109  for ( int i = 0; i < m_vec.length(); ++i) {
110  double alpha = (double) i / ((double) m_vec.length() - 1.);
111  (*this)[i] = (1.-alpha)*d1 + alpha*d2;
112  }
113 
114  queso_require_equal_to_msg(m_vec.length(), m_map.NumMyElements(), "incompatible own vec size");
115 }
116 
117 // ---------------------------------------------------
118 TeuchosVector::TeuchosVector(const TeuchosVector& v) // mox
119  :
120  Vector(v.env(),v.map())
121  {
122  m_vec.size(v.sizeLocal());
123 
124  queso_require_msg(m_vec != NULL, "null vector generated");
125 
126  queso_require_equal_to_msg(m_vec.length(), v.map().NumMyElements(), "incompatible local vec size");
127 
128  queso_require_equal_to_msg(m_vec.length(), v.map().NumGlobalElements(), "incompatible global vec size");
129  this->copy(v);
130 
131  queso_require_equal_to_msg(m_vec.length(), m_map.NumMyElements(), "incompatible own vec size");
132 }
133 
134 
135 // destructor --------------------------------------
136 TeuchosVector::~TeuchosVector()
137 {
138 };
139 
140 // Set methods ------------------------------------
141 //-------------------------------------------------
142 // assigns values a to all entrances in the vector
143 TeuchosVector& TeuchosVector::operator=(double a)
144 {
145  m_vec.putScalar(a);
146  return *this;
147 }
148 
149 //-------------------------------------------------
150 TeuchosVector& TeuchosVector::operator=(const TeuchosVector& rhs)
151 {
152  unsigned int size1 = m_vec.length();
153  unsigned int size2 = rhs.sizeLocal();
154 
155  queso_require_equal_to_msg(size1, size2, "the vectors do NOT have the same size.\n");
156 
157  if (size1==size2){
158  for (unsigned int i=0;i<size1;i++){
159  m_vec[i]=rhs[i];
160  }
161  }
162 
163  return *this;
164 }
165 
166 //-------------------------------------------------
167 TeuchosVector& TeuchosVector::operator*=(double a)
168 {
169  m_vec.scale(a); //Scale this vector by a; *this = a*this.
170  return *this;
171 }
172 
173  //-------------------------------------------------
174 TeuchosVector& TeuchosVector::operator/=(double a)
175 {
176  (*this) *= (1.0/a);
177  return *this;
178 }
179 
180 //-------------------------------------------------
181 TeuchosVector& TeuchosVector::operator*=(const TeuchosVector& rhs)
182 {
183  unsigned int size1 = this->sizeLocal();
184  unsigned int size2 = rhs.sizeLocal();
185  queso_require_equal_to_msg(size1, size2, "the vectors do NOT have the same size.\n");
186  if (size1==size2){
187  for (unsigned int i = 0; i < size1; ++i) {
188  (*this)[i] *= rhs[i];
189  }
190  }
191  return *this;
192 }
193 
194 //-------------------------------------------------
195 TeuchosVector& TeuchosVector::operator/=(const TeuchosVector& rhs)
196 {
197  unsigned int size1 = this->sizeLocal();
198  unsigned int size2 = rhs.sizeLocal();
199 
200  queso_require_equal_to_msg(size1, size2, "the vectors do NOT have the same size.\n");
201  if (size1==size2){
202  for (unsigned int i = 0; i < size1; ++i) {
203  (*this)[i] /= rhs[i];
204  }
205  }
206  return *this;
207 }
208 
209 //-------------------------------------------------
210 TeuchosVector& TeuchosVector::operator+=(const TeuchosVector& rhs)
211 {
212  unsigned int size1 = this->sizeLocal();
213  unsigned int size2 = rhs.sizeLocal();
214 
215  queso_require_equal_to_msg(size1, size2, "the vectors do NOT have the same size.\n");
216 
217  if (size1==size2){
218  for (unsigned int i = 0; i < size1; ++i) {
219  (*this)[i] += rhs[i];
220  }
221  }
222  return *this;
223 }
224 
225 //-------------------------------------------------
226 TeuchosVector& TeuchosVector::operator-=(const TeuchosVector& rhs)
227 {
228  unsigned int size1 = this->sizeLocal();
229  unsigned int size2 = rhs.sizeLocal();
230 
231  queso_require_equal_to_msg(size1, size2, "the vectors do NOT have the same size.\n");
232 
233  if (size1==size2){
234  for (unsigned int i = 0; i < size1; ++i) {
235  (*this)[i] -= rhs[i];
236  }
237  }
238 
239  return *this;
240 }
241 
242 
243 // Accessor methods --------------------------------
244 //-------------------------------------------------
245 double& TeuchosVector::operator[](unsigned int i)
246 {
247  return m_vec[i];
248 }
249 
250 //-------------------------------------------------
251 const double& TeuchosVector::operator[](unsigned int i) const
252 {
253  return m_vec[i];
254 }
255 
256 // Attribute methods ------------------------------
257 //-------------------------------------------------
258 unsigned int TeuchosVector::sizeLocal() const
259 {
260  queso_require_equal_to_msg(m_vec.length(), (int) m_map.NumMyElements(), "incompatible vec size");
261 
262  return m_vec.length();
263 }
264 
265 //-------------------------------------------------
266 unsigned int TeuchosVector::sizeGlobal() const
267 {
268  queso_require_equal_to_msg(m_vec.length(), (int) m_map.NumGlobalElements(), "incompatible vec size");
269  return m_vec.length();
270 }
271 
272 //-------------------------------------------------
273 // TODO: needs to be checked. It may not be used at all. Kemelli 4/30/13.
274 double*
275 TeuchosVector::values()
276 {
277  return m_vec.values();
278 };
279 
280 // Getting Max and Min values -------------------
281 // Max ------------------------------------------
282 double TeuchosVector::getMaxValue( ) const //dummy
283 {
284  const unsigned int size = this->sizeLocal();
285  std::vector<double> aux;
286 
287  for (unsigned int i=0; i<size; i++ ) {
288  aux.push_back((*this)[i]) ;
289  }
290 
291  return *max_element (aux.begin(),aux.end());
292 }
293 
294 // Min ------------------------------------------
295 double TeuchosVector::getMinValue( ) const //dummy
296 {
297  const unsigned int size = this->sizeLocal();
298  std::vector<double> aux;
299 
300  for (unsigned int i=0; i<size; i++ ) {
301  aux.push_back((*this)[i]) ;
302  }
303 
304  return *min_element (aux.begin(),aux.end());
305 
306 }
307 
308 // Max index -----------------------------------
309 int TeuchosVector::getMaxValueIndex( ) const //dummy
310 {
311  const unsigned int size = this->sizeLocal();
312  std::vector<double> vect;
313 
314  for (unsigned int i=0; i<size; i++ ) {
315  vect.push_back((*this)[i]) ;
316  }
317  std::vector<double>::iterator iter_max = max_element(vect.begin(), vect.end());
318  return distance(vect.begin(), iter_max);
319 }
320 
321 // Min index -----------------------------------
322 int TeuchosVector::getMinValueIndex( ) const //dummy
323 {
324  const unsigned int size = this->sizeLocal();
325  std::vector<double> vect;
326 
327  for (unsigned int i=0; i<size; i++ ) {
328  vect.push_back((*this)[i]) ;
329  }
330  std::vector<double>::iterator iter_min = min_element(vect.begin(), vect.end());
331  return distance(vect.begin(), iter_min);
332 }
333 
334 // Max and index -------------------------------
335 void TeuchosVector::getMaxValueAndIndex( double& max_value, int& max_value_index ) //dummy
336 {
337  const unsigned int size = this->sizeLocal();
338  std::vector<double> vect;
339 
340  for (unsigned int i=0; i<size; i++ ) {
341  vect.push_back((*this)[i]) ;
342  }
343  std::vector<double>::iterator iter_max = max_element(vect.begin(), vect.end());
344 
345  max_value = *iter_max;
346  max_value_index = distance(vect.begin(), iter_max);
347 
348  return;
349 }
350 
351 // Min and index -------------------------------
352 void TeuchosVector::getMinValueAndIndex( double& min_value, int& min_value_index ) //dummy
353 {
354  const unsigned int size = this->sizeLocal();
355  std::vector<double> vect;
356 
357  for (unsigned int i=0; i<size; i++ ) {
358  vect.push_back((*this)[i]) ;
359  }
360  std::vector<double>::iterator iter_min = min_element(vect.begin(), vect.end());
361 
362  min_value = *iter_min;
363  min_value_index = distance(vect.begin(), iter_min);
364 
365  return;
366 }
367 
368 // Norm methods ------------------------------------
369 // -------------------------------------------------
370 double TeuchosVector::norm2Sq() const
371 {
372  return (m_vec).dot(m_vec );
373 }
374 
375 //-------------------------------------------------
376 double TeuchosVector::norm2() const
377 {
378  return std::sqrt(this->norm2Sq());
379 }
380 
381 //-------------------------------------------------
382 double TeuchosVector::norm1() const
383 {
384  double result = 0.;
385 
386  unsigned int size = this->sizeLocal();
387  for (unsigned int i = 0; i < size; ++i) {
388  result += fabs((*this)[i]);
389  }
390 
391  return result;
392 }
393 
394 //-------------------------------------------------
395 double TeuchosVector::normInf() const
396 {
397  double result = 0.;
398 
399  unsigned int size = this->sizeLocal();
400  double aux = 0.;
401  for (unsigned int i = 0; i < size; ++i) {
402  aux = fabs((*this)[i]);
403  if (aux > result) result = aux;
404  }
405 
406  return result;
407 }
408 
409 // Comparison methods -----------------------------
410 //-------------------------------------------------
411 bool
412 TeuchosVector::atLeastOneComponentSmallerThan(const TeuchosVector& rhs) const
413 {
414  queso_require_equal_to_msg(this->sizeLocal(), rhs.sizeLocal(), "vectors have different sizes");
415 
416  bool result = false;
417  unsigned int i = 0;
418  unsigned int size = this->sizeLocal();
419  while ((i < size) && (result == false)) {
420  result = ( (*this)[i] < rhs[i] );
421  i++;
422  };
423 
424  return result;
425 }
426 
427 //-------------------------------------------------
428 bool
429 TeuchosVector::atLeastOneComponentBiggerThan(const TeuchosVector& rhs) const
430 {
431  queso_require_equal_to_msg(this->sizeLocal(), rhs.sizeLocal(), "vectors have different sizes");
432 
433  bool result = false;
434  unsigned int i = 0;
435  unsigned int size = this->sizeLocal();
436  while ((i < size) && (result == false)) {
437  result = ( (*this)[i] > rhs[i] );
438  i++;
439  };
440 
441  return result;
442 }
443 
444 //-------------------------------------------------
445 bool
446 TeuchosVector::atLeastOneComponentSmallerOrEqualThan(const TeuchosVector& rhs) const
447 {
448  queso_require_equal_to_msg(this->sizeLocal(), rhs.sizeLocal(), "vectors have different sizes");
449 
450  bool result = false;
451  unsigned int i = 0;
452  unsigned int size = this->sizeLocal();
453  while ((i < size) && (result == false)) {
454  result = ( (*this)[i] <= rhs[i] ); // prudencio 2012-02-06
455  i++;
456  };
457 
458  return result;
459 }
460 
461 //-------------------------------------------------
462 bool
463 TeuchosVector::atLeastOneComponentBiggerOrEqualThan(const TeuchosVector& rhs) const
464 {
465  queso_require_equal_to_msg(this->sizeLocal(), rhs.sizeLocal(), "vectors have different sizes");
466 
467  bool result = false;
468  unsigned int i = 0;
469  unsigned int size = this->sizeLocal();
470  while ((i < size) && (result == false)) {
471  result = ( (*this)[i] >= rhs[i] ); // prudencio 2012-02-06
472  i++;
473  };
474 
475  return result;
476 }
477 
478 
479 // Get/Set methods -----------------------------------
480 //----------------------------------------------------
481 void TeuchosVector::cwSet(double value)
482 {
483  (*this)=value;
484 
485  return;
486 }
487 
488 //----------------------------------------------------
489 void TeuchosVector::cwSet(unsigned int initialPos, const TeuchosVector& vec)
490 {
491  queso_require_less_msg(initialPos, this->sizeLocal(), "invalid initialPos");
492 
493  queso_require_less_equal_msg((initialPos +vec.sizeLocal()), this->sizeLocal(), "invalid vec.sizeLocal()");
494 
495  for (unsigned int i = 0; i < vec.sizeLocal(); ++i) {
496  (*this)[initialPos+i] = vec[i];
497  }
498 
499  return;
500 }
501 
502 
503 //----------------------------------------------------
504 /* extracts elements from vector (*this) starting at position initialPos and save in vec */
505 void TeuchosVector::cwExtract(unsigned int initialPos, TeuchosVector& vec) const
506 {
507  queso_require_less_msg(initialPos, this->sizeLocal(), "invalid initialPos");
508 
509  queso_require_less_equal_msg((initialPos +vec.sizeLocal()), this->sizeLocal(), "invalid vec.sizeLocal()");
510 
511  for (unsigned int i = 0; i < vec.sizeLocal(); ++i) {
512  vec[i] = (*this)[initialPos+i];
513  }
514 
515  return;
516 }
517 
518 //----------------------------------------------------
519 void TeuchosVector::cwInvert()
520 {
521  unsigned int size = this->sizeLocal();
522  for (unsigned int i = 0; i < size; ++i) {
523  (*this)[i] = 1./(*this)[i];
524  }
525 
526  return;
527 }
528 
529 //----------------------------------------------------
530 void TeuchosVector::cwSqrt()
531 {
532  unsigned int size = this->sizeLocal();
533  for (unsigned int i = 0; i < size; ++i) {
534  (*this)[i] = sqrt((*this)[i]);
535  }
536 
537  return;
538 }
539 
540 //----------------------------------------------------
541 void
542 TeuchosVector::cwSetConcatenated(const TeuchosVector& v1, const TeuchosVector& v2)
543 {
544  queso_require_equal_to_msg(this->sizeLocal(), v1.sizeLocal() + v2.sizeLocal(), "incompatible vector sizes");
545 
546 // if ( this->sizeLocal() != v1.sizeLocal() + v2.sizeLocal() ) {
547 // std::cout << "ERROR in TeuchosVector:: cwSetConcatenated ---> the vectors' sizes are not compatible.\n";
548 // cout << " in TeuchosVector:: cwSetConcatenated ---> resizing resulting vector... new size = "
549 // << v1.sizeLocal()+v1.sizeLocal() <<endl;
550 //
551 // m_vec.resize(v1.sizeLocal()+v1.sizeLocal());
552 // }
553 
554  for (unsigned int i = 0; i < v1.sizeLocal(); ++i) {
555  (*this)[i] = v1[i];
556  }
557 
558  for (unsigned int i = 0; i < v2.sizeLocal(); ++i) {
559  (*this)[v1.sizeLocal()+i] = v2[i];
560  }
561 
562  return;
563 }
564 
565 // -------------------------------------------------
566 //updated on 3/18, to use the RngBase+Boost
567 void TeuchosVector::cwSetGaussian(double mean, double stdDev)
568 {
569  for (unsigned int i = 0; i < this->sizeLocal(); ++i) {
570  (*this)[i] = mean + m_env.rngObject()->gaussianSample(stdDev);
571  }
572  return;
573 };
574 
575 // -------------------------------------------------
576 //updated on 3/18, to use the RngBase+Boost
577 void TeuchosVector::cwSetGaussian(const TeuchosVector& meanVec, const TeuchosVector& stdDevVec)
578 {
579  for (unsigned int i = 0; i < this->sizeLocal(); ++i) {
580  (*this)[i] = meanVec[i] + m_env.rngObject()->gaussianSample(stdDevVec[i]);
581  }
582  return;
583 };
584 
585 
586 //----------------------------------------------------
587 //implemented/checked 2/26/13
588 //updated on 3/18, to use the RngBase+Boost
589  void TeuchosVector::cwSetUniform(const TeuchosVector& aVec, const TeuchosVector& bVec)
590 {
591  for (unsigned int i = 0; i < this->sizeLocal(); ++i) {
592  (*this)[i] = aVec[i] + (bVec[i]-aVec[i])*m_env.rngObject()->uniformSample();
593  }
594  return;
595 }
596 
597 
598 // -------------------------------------------------
599 //updated on 3/18, to use the RngBase+Boost
600 void TeuchosVector::cwSetBeta(const TeuchosVector& alpha, const TeuchosVector& beta)
601 {
602  queso_require_equal_to_msg(this->sizeLocal(), alpha.sizeLocal(), "incompatible alpha size");
603 
604  queso_require_equal_to_msg(this->sizeLocal(), beta.sizeLocal(), "incompatible beta size");
605 
606  for (unsigned int i = 0; i < this->sizeLocal(); ++i)
607  {
608  (*this)[i] = m_env.rngObject()->betaSample(alpha[i],beta[i]);
609 
610  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99))
611  {
612  *m_env.subDisplayFile() << "In TeuchosVector::cwSetBeta()"
613  << ": fullRank " << m_env.fullRank()
614  << ", i = " << i
615  << ", alpha[i] = " << alpha[i]
616  << ", beta[i] = " << beta[i]
617  << ", sample = " << (*this)[i]
618  << std::endl;
619  }
620  }
621  return;
622 };
623 
624 // -------------------------------------------------
625 //updated on 3/18, to use the RngBase+Boost
626 void TeuchosVector::cwSetGamma(const TeuchosVector& aVec, const TeuchosVector& bVec)
627 {
628  queso_require_equal_to_msg(this->sizeLocal(), aVec.sizeLocal(), "incompatible a size");
629 
630  queso_require_equal_to_msg(this->sizeLocal(), bVec.sizeLocal(), "incompatible b size");
631 
632  for (unsigned int i = 0; i < this->sizeLocal(); ++i) {
633  (*this)[i] = m_env.rngObject()->gammaSample(aVec[i],bVec[i]);
634  }
635  return;
636 }
637 
638 // -------------------------------------------------
639 //updated on 3/18, to use the RngBase+Boost
640 // Using Gamma Distribution to calculate InverseGamma.
641 // Note the divisions: 1.0/b and the 1.0/generator; they are crucial
642 void TeuchosVector::cwSetInverseGamma(const TeuchosVector& alpha, const TeuchosVector& beta)
643 {
644  queso_require_equal_to_msg(this->sizeLocal(), alpha.sizeLocal(), "incompatible alpha size");
645 
646  queso_require_equal_to_msg(this->sizeLocal(), beta.sizeLocal(), "incompatible beta size");
647 
648  for (unsigned int i = 0; i < this->sizeLocal(); ++i) {
649  (*this)[i] = 1./m_env.rngObject()->gammaSample(alpha[i],1./beta[i]);
650  }
651  return;
652 }
653 
654 
655 // Miscellaneous methods -------------------------
656 // absolute values -------------------------------
657 // tested 1/8/13
658 TeuchosVector
659 TeuchosVector::abs() const
660 {
661  TeuchosVector abs_of_this_vec( *this );
662 
663  unsigned int size = abs_of_this_vec.sizeLocal();
664 
665  for( unsigned int i = 0; i < size; ++i )
666  {
667  abs_of_this_vec[i] = std::fabs( (*this)[i] );
668  }
669 
670  return abs_of_this_vec;
671 
672 }
673 
674 // -------------------------------------------------
675 void
676 TeuchosVector::copy_to_std_vector(std::vector<double>& vec)
677 {
678  unsigned int size = this->sizeLocal();
679  vec.resize(size);
680 
681  for (unsigned int i = 0; i < size; ++i)
682  vec[i] = m_vec[i];
683 
684  return;
685 }
686 
687 // -------------------------------------------------
688 void
689 TeuchosVector::copy_from_std_vector(const std::vector<double> vec)
690 {
691  unsigned int size1 = vec.size(), size2= this->sizeLocal();
692 
693  queso_require_equal_to_msg(size1, size2, "vectors have different sizes");
694 
695  for (unsigned int i = 0; i < size1; ++i)
696  m_vec[i] = vec[i];
697 
698  return;
699 }
700 
701 // -------------------------------------------------
702 void TeuchosVector::sort()
703 {
704  std::vector<double> vec;
705 
706  (*this).copy_to_std_vector(vec);
707 
708  // using default comparison (operator <):
709  std::sort (vec.begin(), vec.end());
710 
711  (*this).copy_from_std_vector(vec);
712 };
713 
714 // -------------------------------------------------
715 double TeuchosVector::sumOfComponents() const
716 {
717  double result = 0.;
718  unsigned int size = this->sizeLocal();
719  for (unsigned int i = 0; i < size; ++i) {
720  result += (*this)[i];
721  }
722 
723  return result;
724 }
725 
726 // -------------------------------------------------
727 // added 2/28/13
728 void
729 TeuchosVector::mpiBcast(int srcRank, const MpiComm& bcastComm)
730 {
731  // Filter out those nodes that should not participate
732  if (bcastComm.MyPID() < 0) return;
733 
734  // Check 'srcRank'
735  queso_require_msg(!((srcRank < 0) || (srcRank >= bcastComm.NumProc())), "invalud srcRank");
736 
737  // Check number of participant nodes
738  double localNumNodes = 1.;
739  double totalNumNodes = 0.;
740  bcastComm.Allreduce((void *) &localNumNodes, (void *) &totalNumNodes, (int) 1, RawValue_MPI_DOUBLE, RawValue_MPI_SUM,
741  "TeuchosVector::mpiBcast()",
742  "failed MPI.Allreduce() for numNodes");
743  queso_require_equal_to_msg(((int) totalNumNodes), bcastComm.NumProc(), "inconsistent numNodes");
744 
745  // Check that all participant nodes have the same vector size
746  double localVectorSize = this->sizeLocal();
747  double sumOfVectorSizes = 0.;
748  bcastComm.Allreduce((void *) &localVectorSize, (void *) &sumOfVectorSizes, (int) 1, RawValue_MPI_DOUBLE, RawValue_MPI_SUM,
749  "TeuchosVector::mpiBcast()",
750  "failed MPI.Allreduce() for vectorSize");
751 
752  if ( ((unsigned int) sumOfVectorSizes) != ((unsigned int)(totalNumNodes*localVectorSize)) ) {
753  std::cerr << "rank " << bcastComm.MyPID()
754  << ": sumOfVectorSizes = " << sumOfVectorSizes
755  << ", totalNumNodes = " << totalNumNodes
756  << ", localVectorSize = " << localVectorSize
757  << std::endl;
758  }
759  bcastComm.Barrier();
760  queso_require_equal_to_msg(((unsigned int) sumOfVectorSizes), ((unsigned int)(totalNumNodes*localVectorSize)), "inconsistent vectorSize");
761 
762  // Ok, bcast data
763  std::vector<double> dataBuffer((unsigned int) localVectorSize, 0.);
764  if (bcastComm.MyPID() == srcRank) {
765  for (unsigned int i = 0; i < dataBuffer.size(); ++i) {
766  dataBuffer[i] = (*this)[i];
767  }
768  }
769 
770  bcastComm.Bcast((void *) &dataBuffer[0], (int) localVectorSize, RawValue_MPI_DOUBLE, srcRank,
771  "TeuchosVector::mpiBcast()",
772  "failed MPI.Bcast()");
773 
774  if (bcastComm.MyPID() != srcRank) {
775  for (unsigned int i = 0; i < dataBuffer.size(); ++i) {
776  (*this)[i] = dataBuffer[i];
777  }
778  }
779 
780  return;
781 }
782 
783 // -------------------------------------------------
784 // added 2/28/13
785 void
786 TeuchosVector::mpiAllReduce(RawType_MPI_Op mpiOperation, const MpiComm& opComm, TeuchosVector& resultVec) const
787 {
788  // Filter out those nodes that should not participate
789  if (opComm.MyPID() < 0) return;
790 
791  unsigned int size = this->sizeLocal();
792  queso_require_equal_to_msg(size, resultVec.sizeLocal(), "different vector sizes");
793 
794  for (unsigned int i = 0; i < size; ++i) {
795  double srcValue = (*this)[i];
796  double resultValue = 0.;
797  opComm.Allreduce((void *) &srcValue, (void *) &resultValue, (int) 1, RawValue_MPI_DOUBLE, mpiOperation,
798  "TeuchosVector::mpiAllReduce()",
799  "failed MPI.Allreduce()");
800  resultVec[i] = resultValue;
801  }
802 
803  return;
804 }
805 
806 // -------------------------------------------------
807 // added 2/28/13
808 void
809 TeuchosVector::mpiAllQuantile(double probability, const MpiComm& opComm, TeuchosVector& resultVec) const
810 {
811  // Filter out those nodes that should not participate
812  if (opComm.MyPID() < 0) return;
813 
814  queso_require_msg(!((probability < 0.) || (1. < probability)), "invalid input");
815 
816  unsigned int size = this->sizeLocal();
817  queso_require_equal_to_msg(size, resultVec.sizeLocal(), "different vector sizes");
818 
819  for (unsigned int i = 0; i < size; ++i) {
820  double auxDouble = (int) (*this)[i];
821  std::vector<double> vecOfDoubles(opComm.NumProc(),0.);
822  opComm.Gather((void *) &auxDouble, 1, RawValue_MPI_DOUBLE, (void *) &vecOfDoubles[0], (int) 1, RawValue_MPI_DOUBLE, 0,
823  "TeuchosVector::mpiAllQuantile()",
824  "failed MPI.Gather()");
825 
826  std::sort(vecOfDoubles.begin(), vecOfDoubles.end());
827 
828  double result = vecOfDoubles[(unsigned int)( probability*((double)(vecOfDoubles.size()-1)) )];
829 
830  opComm.Bcast((void *) &result, (int) 1, RawValue_MPI_DOUBLE, 0,
831  "TeuchosVector::mpiAllQuantile()",
832  "failed MPI.Bcast()");
833 
834  resultVec[i] = result;
835  }
836 
837  return;
838 }
839 
840 // -------------------------------------------------
841 // added/tested 2/28/13
842 void
843 TeuchosVector::matlabLinearInterpExtrap(
844  const TeuchosVector& x1Vec,
845  const TeuchosVector& y1Vec,
846  const TeuchosVector& x2Vec)
847 {
848  queso_require_greater_msg(x1Vec.sizeLocal(), 1, "invalid 'x1' size");
849 
850  queso_require_equal_to_msg(x1Vec.sizeLocal(), y1Vec.sizeLocal(), "invalid 'x1' and 'y1' sizes");
851 
852  queso_require_equal_to_msg(x2Vec.sizeLocal(), this->sizeLocal(), "invalid 'x2' and 'this' sizes");
853 
854  for (unsigned int i = 1; i < x1Vec.sizeLocal(); ++i) { // Yes, '1'
855  queso_require_greater_msg(x1Vec[i], x1Vec[i-1], "invalid 'x1' values");
856  }
857 
858  for (unsigned int id2 = 0; id2 < x2Vec.sizeLocal(); ++id2) {
859  double x2 = x2Vec[id2];
860  unsigned int id1 = 0;
861  bool found1 = false;
862  for (id1 = 0; id1 < x1Vec.sizeLocal(); ++id1) {
863  if (x2 <= x1Vec[id1]) {
864  found1 = true;
865  break;
866  }
867  }
868  bool makeLinearModel = false;
869  double xa = 0.;
870  double xb = 0.;
871  double ya = 0.;
872  double yb = 0.;
873  if (x2 == x1Vec[id1]) {
874  (*this)[id2] = y1Vec[id1];
875  }
876  else if (x2 < x1Vec[0]) {
877  // Extrapolation case
878  makeLinearModel = true;
879  xa = x1Vec[0];
880  xb = x1Vec[1];
881  ya = y1Vec[0];
882  yb = y1Vec[1];
883  }
884  else if (found1 == true) {
885  // Interpolation case
886  makeLinearModel = true;
887  xa = x1Vec[id1-1];
888  xb = x1Vec[id1];
889  ya = y1Vec[id1-1];
890  yb = y1Vec[id1];
891  }
892  else {
893  // Extrapolation case
894  makeLinearModel = true;
895  xa = x1Vec[x1Vec.sizeLocal()-2];
896  xb = x1Vec[x1Vec.sizeLocal()-1];
897  ya = y1Vec[x1Vec.sizeLocal()-2];
898  yb = y1Vec[x1Vec.sizeLocal()-1];
899  }
900 
901  if (makeLinearModel) {
902  double rate = (yb-ya)/(xb-xa);
903  (*this)[id2] = ya + (x2-xa)*rate;
904  }
905  }
906  return;
907 }
908 
909 // -------------------------------------------------
910 // added 2/28/13
911 void
912 TeuchosVector::matlabDiff(
913  unsigned int firstPositionToStoreDiff,
914  double valueForRemainderPosition,
915  TeuchosVector& outputVec) const
916 {
917  unsigned int size = this->sizeLocal();
918 
919  queso_require_less_equal_msg(firstPositionToStoreDiff, 1, "invalid firstPositionToStoreDiff");
920 
921  queso_require_equal_to_msg(size, outputVec.sizeLocal(), "invalid size of outputVecs");
922 
923  for (unsigned int i = 0; i < (size-1); ++i) {
924  outputVec[firstPositionToStoreDiff+i] = (*this)[i+1]-(*this)[i];
925  }
926  if (firstPositionToStoreDiff == 0) {
927  outputVec[size-1] = valueForRemainderPosition;
928  }
929  else {
930  outputVec[0] = valueForRemainderPosition;
931  }
932 
933  return;
934 }
935 
936 // I/O methods -------------------------------------
937 // -------------------------------------------------
938 void
939 TeuchosVector::print(std::ostream& os) const
940 {
941  unsigned int size = this->sizeLocal();
942 
943  std::ostream::fmtflags curr_fmt = os.flags();
944 
945  if (m_printScientific) {
946  unsigned int savedPrecision = os.precision();
947  os.precision(16);
948 
949  if (m_printHorizontally) {
950  for (unsigned int i = 0; i < size; ++i) {
951  os << std::scientific << (*this)[i]
952  << " ";
953  }
954  }
955  else {
956  for (unsigned int i = 0; i < size; ++i) {
957  os << std::scientific << (*this)[i]
958  << std::endl;
959  }
960  }
961 
962  os.precision(savedPrecision);
963  }
964  else {
965  if (m_printHorizontally) {
966  for (unsigned int i = 0; i < size; ++i) {
967  os << std::dec << (*this)[i]
968  << " ";
969  }
970  }
971  else {
972  for (unsigned int i = 0; i < size; ++i) {
973  os << std::dec << (*this)[i]
974  << std::endl;
975  }
976  }
977  }
978 
979  os.flags(curr_fmt);
980  return;
981 }
982 
983 // -------------------------------------------------
984 void
985 TeuchosVector::subReadContents(
986  const std::string& fileName,
987  const std::string& fileType,
988  const std::set<unsigned int>& allowedSubEnvIds)
989 {
990  queso_require_greater_equal_msg(m_env.subRank(), 0, "unexpected subRank");
991 
992  queso_require_less_equal_msg(this->numOfProcsForStorage(), 1, "implemented just for sequential vectors for now");
993 
994  FilePtrSetStruct filePtrSet;
995  if (m_env.openInputFile(fileName,
996  fileType, // "m or hdf"
997  allowedSubEnvIds,
998  filePtrSet)) {
999  double subReadSize = this->sizeLocal();
1000 
1001  // In the logic below, the id of a line' begins with value 0 (zero)
1002  unsigned int idOfMyFirstLine = 1;
1003  unsigned int idOfMyLastLine = this->sizeLocal();
1004  unsigned int numParams = 1; // Yes, just '1'
1005 
1006  // Read number of chain positions in the file by taking care of the first line,
1007  // which resembles something like 'variable_name = zeros(n_positions,m_params);'
1008  std::string tmpString;
1009 
1010  // Read 'variable name' string
1011  *filePtrSet.ifsVar >> tmpString;
1012  //std::cout << "Just read '" << tmpString << "'" << std::endl;
1013 
1014  // Read '=' sign
1015  *filePtrSet.ifsVar >> tmpString;
1016  //std::cout << "Just read '" << tmpString << "'" << std::endl;
1017  queso_require_equal_to_msg(tmpString, "=", "string should be the '=' sign");
1018 
1019  // Read 'zeros(n_positions,n_params)' string
1020  *filePtrSet.ifsVar >> tmpString;
1021  //std::cout << "Just read '" << tmpString << "'" << std::endl;
1022  unsigned int posInTmpString = 6;
1023 
1024  // Isolate 'n_positions' in a string
1025  char nPositionsString[tmpString.size()-posInTmpString+1];
1026  unsigned int posInPositionsString = 0;
1027  do {
1028  queso_require_less_msg(posInTmpString, tmpString.size(), "symbol ',' not found in first line of file");
1029  nPositionsString[posInPositionsString++] = tmpString[posInTmpString++];
1030  } while (tmpString[posInTmpString] != ',');
1031  nPositionsString[posInPositionsString] = '\0';
1032 
1033  // Isolate 'n_params' in a string
1034  posInTmpString++; // Avoid reading ',' char
1035  char nParamsString[tmpString.size()-posInTmpString+1];
1036  unsigned int posInParamsString = 0;
1037  do {
1038  queso_require_less_msg(posInTmpString, tmpString.size(), "symbol ')' not found in first line of file");
1039  nParamsString[posInParamsString++] = tmpString[posInTmpString++];
1040  } while (tmpString[posInTmpString] != ')');
1041  nParamsString[posInParamsString] = '\0';
1042 
1043  // Convert 'n_positions' and 'n_params' strings to numbers
1044  unsigned int sizeOfVecInFile = (unsigned int) strtod(nPositionsString,NULL);
1045  unsigned int numParamsInFile = (unsigned int) strtod(nParamsString, NULL);
1046  if (m_env.subDisplayFile()) {
1047  *m_env.subDisplayFile() << "In TeuchosVector::subReadContents()"
1048  << ": fullRank " << m_env.fullRank()
1049  << ", sizeOfVecInFile = " << sizeOfVecInFile
1050  << ", numParamsInFile = " << numParamsInFile
1051  << ", this->sizeLocal() = " << this->sizeLocal()
1052  << std::endl;
1053  }
1054 
1055  // Check if [size of vec in file] >= [requested sub vec size]
1056  queso_require_greater_equal_msg(sizeOfVecInFile, subReadSize, "size of vec in file is not big enough");
1057 
1058  // Check if [num params in file] == [num params in current vec]
1059  queso_require_equal_to_msg(numParamsInFile, numParams, "number of parameters of vec in file is different than number of parameters in this vec object");
1060 
1061  // Code common to any core in a communicator
1062  unsigned int maxCharsPerLine = 64*numParams; // Up to about 60 characters to represent each parameter value
1063 
1064  unsigned int lineId = 0;
1065  while (lineId < idOfMyFirstLine) {
1066  filePtrSet.ifsVar->ignore(maxCharsPerLine,'\n');
1067  lineId++;
1068  };
1069 
1070  if (m_env.subDisplayFile()) {
1071  *m_env.subDisplayFile() << "In TeuchosVector::subReadContents()"
1072  << ": beginning to read input actual data"
1073  << std::endl;
1074  }
1075 
1076  // Take care of initial part of the first data line,
1077  // which resembles something like 'variable_name = [value1 value2 ...'
1078  // Read 'variable name' string
1079  *filePtrSet.ifsVar >> tmpString;
1080  //std::cout << "Core 0 just read '" << tmpString << "'" << std::endl;
1081 
1082  // Read '=' sign
1083  *filePtrSet.ifsVar >> tmpString;
1084  //std::cout << "Core 0 just read '" << tmpString << "'" << std::endl;
1085  queso_require_equal_to_msg(tmpString, "=", "in core 0, string should be the '=' sign");
1086 
1087  // Take into account the ' [' portion
1088  std::streampos tmpPos = filePtrSet.ifsVar->tellg();
1089  filePtrSet.ifsVar->seekg(tmpPos+(std::streampos)2);
1090 
1091  if (m_env.subDisplayFile()) {
1092  *m_env.subDisplayFile() << "In TeuchosVector::subReadContents()"
1093  << ": beginning to read lines with numbers only"
1094  << ", lineId = " << lineId
1095  << ", idOfMyFirstLine = " << idOfMyFirstLine
1096  << ", idOfMyLastLine = " << idOfMyLastLine
1097  << std::endl;
1098  }
1099 
1100  while (lineId <= idOfMyLastLine) {
1101  *filePtrSet.ifsVar >> (*this)[lineId - idOfMyFirstLine];
1102  lineId++;
1103  };
1104 
1105  m_env.closeFile(filePtrSet,fileType);
1106  }
1107 
1108  return;
1109 }
1110 
1111 // -------------------------------------------------
1112 void
1113 TeuchosVector::subWriteContents(
1114  const std::string& varNamePrefix,
1115  const std::string& fileName,
1116  const std::string& fileType,
1117  const std::set<unsigned int>& allowedSubEnvIds) const
1118 {
1119  queso_require_greater_equal_msg(m_env.subRank(), 0, "unexpected subRank");
1120 
1121  queso_require_less_equal_msg(this->numOfProcsForStorage(), 1, "implemented just for sequential vectors for now");
1122 
1123  FilePtrSetStruct filePtrSet;
1124  if (m_env.openOutputFile(fileName,
1125  fileType, // "m or hdf"
1126  allowedSubEnvIds,
1127  false,
1128  filePtrSet)) {
1129  *filePtrSet.ofsVar << varNamePrefix << "_sub" << m_env.subIdString() << " = zeros(" << this->sizeLocal()
1130  << "," << 1
1131  << ");"
1132  << std::endl;
1133  *filePtrSet.ofsVar << varNamePrefix << "_sub" << m_env.subIdString() << " = [";
1134 
1135  bool savedVectorPrintScientific = this->getPrintScientific();
1136  bool savedVectorPrintHorizontally = this->getPrintHorizontally();
1137  this->setPrintScientific (true);
1138  this->setPrintHorizontally(false);
1139  *filePtrSet.ofsVar << *this;
1140  //<< std::endl; // No need for 'endl' because horizontally = 'false'
1141  this->setPrintHorizontally(savedVectorPrintHorizontally);
1142  this->setPrintScientific (savedVectorPrintScientific);
1143 
1144  *filePtrSet.ofsVar << "];\n";
1145 
1146  m_env.closeFile(filePtrSet,fileType);
1147  }
1148 
1149  return;
1150 }
1151 
1152 
1153 // -------------------------------------------------
1154 // Private metfods ---------------------------------
1155 // -------------------------------------------------
1156 
1157 void
1158 TeuchosVector::copy(const TeuchosVector& rhs)
1159 {
1160  this->Vector::base_copy(rhs);
1161 
1162  unsigned int size1 = m_vec.length();
1163  unsigned int size2 = rhs.sizeLocal();
1164 
1165  if (size1==size2){
1166  for (unsigned int i=0;i<size1;i++){
1167  m_vec[i]=rhs[i];
1168  }
1169  }
1170  return;
1171 }
1172 
1173 
1174 // -------------------------------------------------
1175 // Operators outside class definition --------------
1176 // -------------------------------------------------
1177 
1178 // -------------------------------------------------
1179 TeuchosVector operator/(double a, const TeuchosVector& x)
1180 {
1181  TeuchosVector answer(x); //copy x to answer
1182  answer.cwInvert();
1183  answer *= a;
1184  return answer;
1185 }
1186 
1187 // -------------------------------------------------
1188 TeuchosVector operator/(const TeuchosVector& x, const TeuchosVector& y)
1189 {
1190  TeuchosVector answer(x);
1191  answer /= y;
1192  return answer;
1193 }
1194 
1195 // -------------------------------------------------
1196 TeuchosVector operator*(double a, const TeuchosVector& x)
1197 {
1198  TeuchosVector answer(x);
1199  answer *= a;
1200  return answer;
1201 }
1202 
1203 // -------------------------------------------------
1204 TeuchosVector operator*(const TeuchosVector& x, const TeuchosVector& y)
1205 {
1206  TeuchosVector answer(x);
1207  answer *= y;
1208  return answer;
1209 }
1210 
1211 // -------------------------------------------------
1212 double scalarProduct(const TeuchosVector& x, const TeuchosVector& y)
1213 {
1214  unsigned int size1 = x.sizeLocal();
1215  unsigned int size2 = y.sizeLocal();
1216 
1217  queso_require_equal_to_msg(size1, size2, "different sizes of x and y");
1218  double result = 0.;
1219  for (unsigned int i = 0; i < size1; ++i) {
1220  result += x[i]*y[i];
1221  }
1222 
1223  return result;
1224 }
1225 
1226 // -------------------------------------------------
1227 TeuchosVector operator+(const TeuchosVector& x, const TeuchosVector& y)
1228 {
1229  TeuchosVector answer(x);
1230  answer += y;
1231  return answer;
1232 }
1233 
1234 // -------------------------------------------------
1235 TeuchosVector operator-(const TeuchosVector& x, const TeuchosVector& y)
1236 {
1237  TeuchosVector answer(x);
1238  answer -= y;
1239  return answer;
1240 }
1241 
1242 // -------------------------------------------------
1243 bool
1244 operator== (const TeuchosVector& lhs, const TeuchosVector& rhs)
1245 {
1246  bool answer = true;
1247  unsigned int size1 = lhs.sizeLocal();
1248  unsigned int size2 = rhs.sizeLocal();
1249 
1250  queso_require_equal_to_msg(size1, size2, "different sizes of lhs and rhs");
1251 
1252  for (unsigned int i = 0; i < size1; ++i) {
1253  if (lhs[i] != rhs[i]) {
1254  answer = false;
1255  break;
1256  }
1257  }
1258 
1259  return answer;
1260 }
1261 
1262 // -------------------------------------------------
1263 std::ostream&
1264 operator<<(std::ostream& os, const TeuchosVector& obj)
1265 {
1266  obj.print(os);
1267 
1268  return os;
1269 }
1270 
1271 // -------------------------------------------------
1272 
1273 } // End namespace QUESO
1274 
1275 #endif // ifdef QUESO_HAS_TRILINOS
GslMatrix operator*(double a, const GslMatrix &mat)
Definition: GslMatrix.C:1972
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
virtual void base_copy(const Vector &src)
Copies base data from vector src to this vector.
Definition: Vector.C:101
MPI_Op RawType_MPI_Op
Definition: MpiComm.h:41
#define queso_require_less_equal_msg(expr1, expr2, msg)
Definition: asserts.h:89
std::ostream & operator<<(std::ostream &os, const BaseEnvironment &obj)
GslVector operator/(double a, const GslVector &x)
Definition: GslVector.C:1117
GslMatrix operator+(const GslMatrix &m1, const GslMatrix &m2)
Definition: GslMatrix.C:2012
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
double scalarProduct(const GslVector &x, const GslVector &y)
Definition: GslVector.C:1150
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
GslMatrix operator-(const GslMatrix &m1, const GslMatrix &m2)
Definition: GslMatrix.C:2019
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
#define queso_require_greater_msg(expr1, expr2, msg)
Definition: asserts.h:88
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
bool operator==(const GslVector &lhs, const GslVector &rhs)
Definition: GslVector.C:1181
and that you are informed that you can do these things To protect your we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it For if you distribute copies of the whether gratis or for a you must give the recipients all the rights that we gave you You must make sure that receive or can get the source code If you link other code with the you must provide complete object files to the so that they can relink them with the library after making changes to the library and recompiling it And you must show them these terms so they know their rights We protect your rights with a two step which gives you legal permission to copy
Definition: License.txt:60

Generated on Thu Jun 11 2015 13:52:32 for queso-0.53.0 by  doxygen 1.8.5