Simpatico  v1.10
DSymmMatrixParam.h
1 #ifndef UTIL_D_SYMM_MATRIX_PARAM_H
2 #define UTIL_D_SYMM_MATRIX_PARAM_H
3 
4 /*
5 * Util Package - C++ Utilities for Scientific Computation
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <util/param/Parameter.h>
12 #include <util/containers/DMatrix.h>
13 #include <util/format/Int.h>
14 #ifdef UTIL_MPI
15 #include <util/mpi/MpiSendRecv.h>
16 #endif
17 #include <util/global.h>
18 
19 #include <iomanip>
20 
21 namespace Util
22 {
23 
29  template <class Type>
30  class DSymmMatrixParam : public Parameter
31  {
32 
33  public:
34 
43  DSymmMatrixParam(const char *label, DMatrix<Type>& matrix, int n, bool isRequired = true);
44 
48  void writeParam(std::ostream &out);
49 
50  protected:
51 
57  virtual void readValue(std::istream& in);
58 
64  virtual void loadValue(Serializable::IArchive& ar);
65 
71  virtual void saveValue(Serializable::OArchive& ar);
72 
73  #ifdef UTIL_MPI
74 
77  virtual void bcastValue();
78  #endif
79 
80  private:
81 
83  DMatrix<Type>* matrixPtr_;
84 
86  int n_;
87 
88  };
89 
90  /*
91  * DMatrix constructor.
92  */
93  template <class Type>
95  : Parameter(label, isRequired),
96  matrixPtr_(&matrix),
97  n_(n)
98  {}
99 
100  /*
101  * Read a DMatrix from isteam.
102  */
103  template <class Type>
104  void DSymmMatrixParam<Type>::readValue(std::istream &in)
105  {
106  // Preconditions
107  if (!(matrixPtr_->isAllocated())) {
108  UTIL_THROW("Cannot read unallocated DMatrix");
109  }
110  if (n_ != matrixPtr_->capacity1()) {
111  UTIL_THROW("Error: Logical size n_ != DMatrix<Type>::capacity1()");
112  }
113  if (n_ != matrixPtr_->capacity2()) {
114  UTIL_THROW("Error: Logical size n_ != DMatrix<Type>::capacity2()");
115  }
116 
117  // Create matrix of int/bool flags for error checking
118  DMatrix<int> flags;
119  flags.allocate(n_, n_);
120  int i, j;
121  for (i = 0; i < n_; ++i) {
122  for (j = 0; j < n_; ++j) {
123  flags(i,j) = 0;
124  }
125  }
126 
127  double value;
128  for (int k = 0; k < n_*(n_ + 1)/2; ++k) {
129  in >> i >> j >> value;
130  UTIL_CHECK(flags(i,j) == 0);
131  (*matrixPtr_)(i, j) = value;
132  flags(i, j) = 1;
133  if (i != j) {
134  UTIL_CHECK(flags(j,i) == 0);
135  (*matrixPtr_)(j, i) = value;
136  flags(j, i) = 1;
137  }
138  }
139 
140  }
141 
142  /*
143  * Load a DMatrix from input archive.
144  */
145  template <class Type>
147  {
148  if (!(matrixPtr_->isAllocated())) {
149  matrixPtr_->allocate(n_, n_);
150  } else {
151  if (n_ != matrixPtr_->capacity1()) {
152  UTIL_THROW("Error: Logical size n_ != DMatrix<Type>::capacity1()");
153  }
154  if (n_ != matrixPtr_->capacity2()) {
155  UTIL_THROW("Error: Logical size n_ != DMatrix<Type>::capacity2()");
156  }
157  }
158  ar >> *matrixPtr_;
159  }
160 
161  /*
162  * Save a DMatrix to an output archive.
163  */
164  template <class Type>
166  {
167  if (n_ != matrixPtr_->capacity1()) {
168  UTIL_THROW("Error: Logical size n_ != DMatrix<Type>::capacity1()");
169  }
170  if (n_ != matrixPtr_->capacity2()) {
171  UTIL_THROW("Error: Logical size n_ != DMatrix<Type>::capacity2()");
172  }
173  ar << *matrixPtr_;
174  }
175 
176  #ifdef UTIL_MPI
177  /*
178  * Broadcast a DMatrix.
179  */
180  template <class Type>
182  {
183  if (!(matrixPtr_->isAllocated())) {
184  matrixPtr_->allocate(n_, n_);
185  } else {
186  if (n_ != matrixPtr_->capacity1()) {
187  UTIL_THROW("Error: Logical size n_ > DMatrix<Type>::capacity1()");
188  }
189  if (n_ != matrixPtr_->capacity2()) {
190  UTIL_THROW("Error: Logical size n_ > DMatrix<Type>::capacity2()");
191  }
192  }
193  bcast<Type>(ioCommunicator(), *matrixPtr_, n_, n_, 0);
194  }
195  #endif
196 
197  /*
198  * Write a DSymmMatrixParam.
199  */
200  template <class Type>
201  void DSymmMatrixParam<Type>::writeParam(std::ostream &out)
202  {
203  if (isActive()) {
204  // Preconditions
205  if (!(matrixPtr_->isAllocated())) {
206  UTIL_THROW("Cannot read unallocated DMatrix");
207  }
208  if (n_ > matrixPtr_->capacity1()) {
209  UTIL_THROW("Error: Logical size n_ > DMatrix<Type>::capacity1()");
210  }
211  if (n_ > matrixPtr_->capacity2()) {
212  UTIL_THROW("Error: Logical size n_ > DMatrix<Type>::capacity2()");
213  }
214 
215  Label space("");
216  int i, j;
217  for (i = 0; i < n_; ++i) {
218  for (j = 0; j <= i; ++j) {
219  if (i == 0 && j == 0) {
220  out << indent() << label_;
221  } else {
222  out << indent() << space;
223  }
224  out << Int(i, 4) << " " << Int(j, 4) << " "
225  << std::right << std::scientific
226  << std::setprecision(Parameter::Precision)
227  << std::setw(Parameter::Width)
228  << (*matrixPtr_)(i, j)
229  << std::endl;
230  }
231  }
232  }
233  }
234 
235 }
236 #endif
void allocate(int capacity1, int capacity2)
Allocate memory for a matrix.
Definition: DMatrix.h:170
std::string label() const
Return label string.
Definition: Parameter.cpp:158
Label label_
Label object that contains parameter label string.
Definition: Parameter.h:185
A single variable in a parameter file.
Definition: Parameter.h:45
File containing preprocessor macros for error handling.
Saving / output archive for binary ostream.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
int capacity2() const
Get number of columns (range of the second array index).
Definition: Matrix.h:132
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
MPI::Intracomm & ioCommunicator() const
Get the MPI communicator by reference.
Definition: MpiFileIo.h:105
Utility classes for scientific computation.
Definition: accumulators.mod:1
DSymmMatrixParam(const char *label, DMatrix< Type > &matrix, int n, bool isRequired=true)
Constructor.
Wrapper for an int, for formatted ostream output.
Definition: Int.h:36
static const int Precision
Precision for io of floating point data field.
Definition: Parameter.h:56
bool isActive() const
Is this parameter active?
Definition: Parameter.cpp:170
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
int capacity1() const
Get number of rows (range of the first array index).
Definition: Matrix.h:125
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
A label string in a file format.
Definition: Label.h:36
Saving archive for binary istream.
static const int Width
Width of output field for a scalar variable.
Definition: Parameter.h:53
This file contains templates for global functions send<T>, recv<T> and bcast<T>.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:164
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
std::string indent() const
Return indent string for this object (string of spaces).
bool isAllocated() const
Return true if the DMatrix has been allocated, false otherwise.
Definition: DMatrix.h:202
void writeParam(std::ostream &out)
Write DMatrix to file.
A Parameter associated with a symmetric DMatrix.