Simpatico  v1.10
PeriodicExternal.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 "PeriodicExternal.h"
9 #include <iostream>
10 
11 namespace Simp
12 {
13 
14  using namespace Util;
15 
16  /*
17  * Constructor.
18  */
20  : externalParameter_(),
21  nWaveVectors_(),
22  C_(),
23  periodicity_(),
24  interfaceWidth_(),
25  boundaryPtr_(0),
26  nAtomType_(0),
27  isInitialized_(false)
28  { setClassName("PeriodicExternal"); }
29 
30  /*
31  * Copy constructor.
32  */
34  : externalParameter_(other.externalParameter_),
35  nWaveVectors_(other.nWaveVectors_),
36  C_(other.C_),
37  periodicity_(other.periodicity_),
38  interfaceWidth_(other.interfaceWidth_),
39  boundaryPtr_(other.boundaryPtr_),
40  nAtomType_(other.nAtomType_),
41  isInitialized_(other.isInitialized_)
42  {
43  prefactor_.allocate(nAtomType_);
44  for (int i=0; i < nAtomType_; ++i) {
45  prefactor_[i] = other.prefactor_[i];
46  }
47  waveVectors_.allocate(nWaveVectors_);
48  for (int j=0; j < Dimension; ++j) {
49  for (int i=0; i < nWaveVectors_; ++i) {
50  waveVectors_[i][j] = other.waveVectors_[i][j];
51  }
52  }
53  phases_.allocate(nWaveVectors_);
54  for (int i=0; i < nWaveVectors_; ++i) {
55  phases_[i] = other.phases_[i];
56  }
57  for (int i=0; i < Dimension; ++i) {
58  shift_[i] = other.shift_[i];
59  }
60  }
61 
62  /*
63  * Assignment operator.
64  */
66  {
67  externalParameter_ = other.externalParameter_;
68  nWaveVectors_ = other.nWaveVectors_;
69  C_ = other.C_;
70  periodicity_ = other.periodicity_;
71  interfaceWidth_ = other.interfaceWidth_;
72  boundaryPtr_ = other.boundaryPtr_;
73  nAtomType_ = other.nAtomType_;
74  isInitialized_ = other.isInitialized_;
75  for (int i=0; i < nAtomType_; ++i) {
76  prefactor_[i] = other.prefactor_[i];
77  }
78  for (int j=0; j < Dimension; ++j) {
79  for (int i=0; i < nWaveVectors_; ++i) {
80  waveVectors_[i][j] = other.waveVectors_[i][j];
81  }
82  }
83  phases_.allocate(nWaveVectors_);
84  for (int i=0; i < nWaveVectors_; ++i) {
85  phases_[i] = other.phases_[i];
86  }
87  for (int i=0; i < Dimension; ++i) {
88  shift_[i] = other.shift_[i];
89  }
90  return *this;
91  }
92 
93  /*
94  * Set nAtomType
95  */
96  void PeriodicExternal::setNAtomType(int nAtomType)
97  {
98  if (nAtomType <= 0) {
99  UTIL_THROW("nAtomType <= 0");
100  }
101  if (nAtomType > MaxAtomType) {
102  UTIL_THROW("nAtomType > PeriodicExternal::MaxAtomType");
103  }
104  nAtomType_ = nAtomType;
105  }
106 
108  {
109  // Preconditions
110  if (!isInitialized_) {
111  UTIL_THROW("PeriodicExternal potential is not initialized");
112  }
113 
114  externalParameter_ = externalParameter;
115  }
116 
117 
118  /*
119  * Set pointer to the Boundary.
120  */
122  { boundaryPtr_ = &boundary; }
123 
124  /*
125  * Read potential parameters from file.
126  */
127  void PeriodicExternal::readParameters(std::istream &in)
128  {
129  UTIL_CHECK(nAtomType_ > 0);
130  UTIL_CHECK(boundaryPtr_);
131 
132  // Read parameters
133  prefactor_.allocate(nAtomType_);
134  readDArray<double>(in, "prefactor", prefactor_, nAtomType_);
135  read<double>(in, "externalParameter", externalParameter_);
136  read<int>(in, "nWaveVectors", nWaveVectors_);
137  read<double>(in, "C", C_);
138  waveVectors_.allocate(nWaveVectors_);
139  readDArray<Vector>(in, "waveVectors", waveVectors_, nWaveVectors_);
140  phases_.allocate(nWaveVectors_);
141  readDArray<double>(in, "phases", phases_, nWaveVectors_);
142  read<Vector>(in, "shift", shift_);
143  read<double>(in, "interfaceWidth", interfaceWidth_);
144  read<int>(in, "periodicity", periodicity_);
145 
146  isInitialized_ = true;
147  }
148 
149  /*
150  * Load internal state from an archive.
151  */
153  {
154  UTIL_CHECK(nAtomType_ > 0);
155  prefactor_.allocate(nAtomType_);
156  loadDArray<double>(ar, "prefactor", prefactor_, nAtomType_);
157  loadParameter<double>(ar, "externalParameter", externalParameter_);
158  loadParameter<int>(ar, "nWavevectors", nWaveVectors_);
159  loadParameter<double>(ar, "C", C_);
160  waveVectors_.allocate(nWaveVectors_);
161  loadDArray<Vector>(ar, "waveVectors", waveVectors_, nWaveVectors_);
162  phases_.allocate(nWaveVectors_);
163  loadDArray<double>(ar, "phases", phases_, nWaveVectors_);
164  loadParameter<Vector>(ar, "shift", shift_);
165  loadParameter<double>(ar, "interfaceWidth", interfaceWidth_);
166  loadParameter<int>(ar, "periodicity", periodicity_);
167  isInitialized_ = true;
168  }
169 
170  /*
171  * Save internal state to an archive.
172  */
174  {
175  ar << prefactor_;
176  ar << externalParameter_;
177  ar << nWaveVectors_;
178  ar << C_;
179  ar << waveVectors_;
180  ar << phases_;
181  ar << shift_;
182  ar << interfaceWidth_;
183  ar << periodicity_;
184  }
185 
186  /*
187  * Set a potential energy parameter, identified by a string.
188  */
189  void PeriodicExternal::set(std::string name, double value)
190  {
191  if (name == "externalParameter") {
192  externalParameter_ = value;
193  } else
194  if (name == "C") {
195  C_ = value;
196  } else
197  if (name == "interfaceWidth") {
198  interfaceWidth_ = value;
199  } else
200  if (name == "periodicity") {
201  periodicity_ = value;
202  } else {
203  UTIL_THROW("Unrecognized parameter name");
204  }
205  }
206 
207  /*
208  * Get a parameter value, identified by a string.
209  */
210  double PeriodicExternal::get(std::string name) const
211  {
212  double value = 0.0;
213  if (name == "externalParameter") {
214  value = externalParameter_;
215  } else
216  if (name == "C") {
217  value = C_;
218  } else
219  if (name == "interfaceWidth") {
220  value = interfaceWidth_;
221  } else
222  if (name == "periodicity") {
223  value = periodicity_;
224  } else {
225  UTIL_THROW("Unrecognized parameter name");
226  }
227  return value;
228  }
229 
231  {
232  return externalParameter_;
233  }
234 
235  /*
236  * Return name string "PeriodicExternal".
237  */
238  std::string PeriodicExternal::className() const
239  { return std::string("PeriodicExternal"); }
240 
241 }
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
void setExternalParameter(double externalParameter)
Sets external parameter.
void setBoundary(Boundary &boundary)
Set pointer to Boundary.
An orthorhombic periodic unit cell.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
void readParameters(std::istream &in)
Read potential parameters, and initialize other variables.
Classes used by all simpatico molecular simulations.
std::string className() const
Return name string "PeriodicExternal".
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
double externalParameter() const
Returns external parameter.
Saving archive for binary istream.
A clipped cosine potential that induces ordering along directions specified by waveIntVectors, w_i.
#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.
PeriodicExternal()
Default constructor.
void setNAtomType(int nAtomType)
Set nAtomType value.
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
PeriodicExternal & operator=(const PeriodicExternal &other)
Assignment.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.