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