Simpatico  v1.10
SphericalTabulatedExternal.cpp
1 /*
2 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "SphericalTabulatedExternal.h"
9 
10 #include <iostream>
11 
12 namespace Simp
13 {
14 
15  using namespace Util;
16 
17  /*
18  * Constructor.
19  */
21  : rMax_(0.0),
22  rMaxSq_(0.0),
23  dr_(0.0),
24  boundaryPtr_(0),
25  nr_(0),
26  nAtomType_(0),
27  isInitialized_(false)
28  { setClassName("SphericalTabulatedExternal"); }
29 
30  /*
31  * Copy constructor.
32  */
34  other)
35  : rMax_(other.rMax_),
36  rMaxSq_(other.rMaxSq_),
37  dr_(0.0),
38  boundaryPtr_(other.boundaryPtr_),
39  nr_(0),
40  nAtomType_(other.nAtomType_),
41  isInitialized_(false)
42  {
43  if (other.potentials_.isAllocated()) {
44  UTIL_CHECK(other.potentials_.capacity() == nAtomType_);
45  potentials_.allocate(nAtomType_);
46  int i, j;
47  for (i = 0; i < nAtomType_; ++i) {
48  UTIL_CHECK(other.potentials_[i].isAllocated());
49  UTIL_CHECK(other.potentials_[i].capacity() == nr_);
50  potentials_[i].allocate(nr_);
51  for (j = 0; j < nr_; ++j) {
52  potentials_[i][j] = other.potentials_[i][j];
53  }
54  }
55  }
56  isInitialized_ = other.isInitialized_;
57  }
58 
59  /*
60  * Assignment operator.
61  */
64  {
65  rMax_ = other.rMax_;
66  rMaxSq_ = other.rMaxSq_;
67  dr_ = other.dr_;
68  boundaryPtr_ = other.boundaryPtr_;
69  boundaryPtr_ = other.boundaryPtr_;
70  nr_ = other.nr_;
71  nAtomType_ = other.nAtomType_;
72  if (other.potentials_.isAllocated()) {
73  UTIL_CHECK(other.potentials_.capacity() == nAtomType_);
74  if (!potentials_.isAllocated()) {
75  potentials_.allocate(nAtomType_);
76  }
77  UTIL_CHECK(potentials_.capacity() == nAtomType_);
78  int i, j;
79  for (i = 0; i < nAtomType_; ++i) {
80  UTIL_CHECK(other.potentials_[i].capacity() == nr_);
81  if (!potentials_[i].isAllocated()) {
82  potentials_[i].allocate(nr_);
83  }
84  UTIL_CHECK(potentials_[i].capacity() == nr_);
85  for (j = 0; j < nr_; ++j) {
86  potentials_[i][j] = other.potentials_[i][j];
87  }
88  }
89  }
90  isInitialized_ = other.isInitialized_;
91 
92  return *this;
93  }
94 
95  /*
96  * Set nAtomType
97  */
99  {
100  if (nAtomType <= 0) {
101  UTIL_THROW("nAtomType <= 0");
102  }
103  if (nAtomType > MaxAtomType) {
104  UTIL_THROW("nAtomType > SphericalTabulatedExternal::MaxAtomType");
105  }
106  nAtomType_ = nAtomType;
107  }
108 
109  /*
110  * Set pointer to the Boundary.
111  */
113  { boundaryPtr_ = &boundary; }
114 
115  /*
116  * Read potential parameters from file.
117  */
119  {
120  if (nAtomType_ == 0) {
121  UTIL_THROW("nAtomType must be set before readParam");
122  }
123  if (!boundaryPtr_) {
124  UTIL_THROW("Boundary must be set before readParam");
125  }
126 
127  // Read parameters
128  read<double>(in, "rMax", rMax_);
129  read<int>(in, "nr", nr_);
130  read<std::string>(in, "filename", filename_);
131 
132  rMaxSq_ = rMax_*rMax_;
133  dr_ = rMax_/double(nr_ - 1);
134 
135  // Allocate arrays containing tabulated potentials
136  potentials_.allocate(nAtomType_);
137  for (int i = 0; i < nAtomType_; ++i) {
138  potentials_[i].allocate(nr_);
139  }
140  std::cout << "Allocated potentials" << std::endl;
141 
142  // Read tabulated potential from file
143  std::ifstream file;
144  file.open(filename_.c_str());
145  UTIL_CHECK(file.is_open());
146  int i, j, k;
147  for (j = 0; j < nr_; ++j) {
148  file >> k;
149  UTIL_CHECK(k == j);
150  for (i = 0; i < nAtomType_; ++i) {
151  file >> potentials_[i][j];
152  }
153  }
154  file.close();
155 
156  // Shift potential to be zero at rMax
157  std::cout << std::endl;
158  for (j = 0; j < nr_; ++j) {
159  std::cout << " " << j;
160  for (i = 0; i < nAtomType_; ++i) {
161  potentials_[i][j] -= potentials_[i][nr_ - 1];
162  std::cout << " " << potentials_[i][j];
163  }
164  std::cout << std::endl;
165  }
166  isInitialized_ = true;
167  }
168 
169  /*
170  * Load internal state from an archive.
171  */
173  {
174  ar >> nAtomType_;
175  if (nAtomType_ <= 0) {
176  UTIL_THROW( "nAtomType must be positive");
177  }
178  if (!boundaryPtr_) {
179  UTIL_THROW("Boundary must be set before loadParameters");
180  }
181  loadParameter<double> (ar, "rMax", rMax_);
182  loadParameter<int> (ar, "nr", nr_);
183  rMaxSq_ = rMax_*rMax_;
184  dr_ = rMax_/double(nr_ - 1);
185 
186  // Allocate and read arrays containing tabulated potentials
187  if (!potentials_.isAllocated()) {
188  potentials_.allocate(nAtomType_);
189  }
190  UTIL_CHECK(potentials_.capacity() == nAtomType_);
191  int i, j;
192  for (i = 0; i < nAtomType_; ++i) {
193  if (!potentials_[i].isAllocated()) {
194  potentials_[i].allocate(nr_);
195  }
196  UTIL_CHECK(potentials_[i].capacity() == nr_);
197  for (j = 0; j < nr_; ++j) {
198  ar >> potentials_[i][j];
199  }
200  }
201 
202  isInitialized_ = true;
203  }
204 
205  /*
206  * Save internal state to an archive.
207  */
209  {
210  ar << nAtomType_;
211  ar << rMax_;
212  ar << nr_;
213  UTIL_CHECK(potentials_.isAllocated());
214  int i, j;
215  for (i = 0; i < nAtomType_; ++i) {
216  UTIL_CHECK(potentials_[i].isAllocated());
217  for (j = 0; j < nr_; ++j) {
218  ar << potentials_[i][j];
219  }
220  }
221  }
222 
223  /*
224  * Set a potential energy parameter, identified by a string.
225  */
226  void SphericalTabulatedExternal::set(std::string name, double value)
227  {
228  if (name == "rMax") {
229  rMax_ = value;
230  } else {
231  UTIL_THROW("Unrecognized parameter name");
232  }
233  }
234 
235  /*
236  * Get a parameter value, identified by a string.
237  */
238  double SphericalTabulatedExternal::get(std::string name) const
239  {
240  double value = 0.0;
241  if (name == "rMax") {
242  value = rMax_;
243  } else
244  if (name == "nr") {
245  value = nr_;
246  } else {
247  UTIL_THROW("Unrecognized parameter name");
248  }
249  return value;
250  }
251 
256  { return std::string("SphericalTabulatedExternal"); }
257 
258 
259 }
SphericalTabulatedExternal & operator=(const SphericalTabulatedExternal &other)
Assignment.
An orthorhombic periodic unit cell.
Classes used by all simpatico molecular simulations.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
std::string className() const
Return name string for this interaction class.
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
Utility classes for scientific computation.
Definition: accumulators.mod:1
void readParameters(std::istream &in)
Read potential parameters, and initialize other variables.
Saving archive for binary istream.
The potential is independent on the type of atoms.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
void setClassName(const char *className)
Set class name string.
void setBoundary(Boundary &boundary)
Set pointer to Boundary.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
void setNAtomType(int nAtomType)
Set nAtomType value.