Simpatico  v1.10
DpdPair.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 "DpdPair.h"
9 #ifdef UTIL_MPI
10 #include <util/mpi/MpiLoader.h>
11 #endif
12 
13 #include <iostream>
14 namespace Simp
15 {
16 
17  using namespace Util;
18 
19  /*
20  * Constructor.
21  */
23  : maxPairCutoff_(0.0),
24  nAtomType_(0),
25  isInitialized_(false)
26  { setClassName("DpdPair"); }
27 
28  /*
29  * Copy constructor.
30  */
31  DpdPair::DpdPair(const DpdPair& other)
32  : maxPairCutoff_(other.maxPairCutoff_),
33  nAtomType_(other.nAtomType_),
34  isInitialized_(other.isInitialized_)
35  {
36  int i,j;
37  for (i = 0; i < nAtomType_; ++i) {
38  for (j = 0; j < nAtomType_; ++j) {
39  epsilon_[i][j] = other.epsilon_[i][j];
40  sigma_[i][j] = other.sigma_[i][j];
41  sigmaSq_[i][j] = other.sigmaSq_[i][j];
42  ce_[i][j] = other.ce_[i][j];
43  cf_[i][j] = other.cf_[i][j];
44  }
45  }
46  }
47 
48  /*
49  * Assignment operator.
50  */
52  {
53  maxPairCutoff_ = other.maxPairCutoff_;
54  nAtomType_ = other.nAtomType_;
55  isInitialized_ = other.isInitialized_;
56  int i,j;
57  for (i = 0; i < nAtomType_; ++i) {
58  for (j = 0; j < nAtomType_; ++j) {
59  epsilon_[i][j] = other.epsilon_[i][j];
60  sigma_[i][j] = other.sigma_[i][j];
61  sigmaSq_[i][j] = other.sigmaSq_[i][j];
62  ce_[i][j] = other.ce_[i][j];
63  cf_[i][j] = other.cf_[i][j];
64  }
65  }
66  return *this;
67  }
68 
69  /*
70  * Read potential parameters from file.
71  */
72  void DpdPair::readParameters(std::istream &in)
73  {
74  // Preconditions
75  if (nAtomType_ <= 0) {
76  UTIL_THROW( "nAtomType must be set before readParam");
77  }
78 
79  // Read parameters
80  readCArray2D<double> (in, "epsilon", epsilon_[0],
81  nAtomType_, nAtomType_, MaxAtomType);
82  readCArray2D<double>(in, "sigma", sigma_[0],
83  nAtomType_, nAtomType_, MaxAtomType);
84 
85  // Calculate sigmaSq_, ce_, cf_, and maxPairCutoff_
86  int i, j;
87  maxPairCutoff_ = 0.0;
88  for (i = 0; i < nAtomType_; ++i) {
89  for (j = 0; j < nAtomType_; ++j) {
90  sigmaSq_[i][j] = sigma_[i][j]*sigma_[i][j];
91  if (sigma_[i][j] > maxPairCutoff_) {
92  maxPairCutoff_ = sigma_[i][j];
93  }
94  cf_[i][j] = epsilon_[i][j]/sigmaSq_[i][j];
95  ce_[i][j] = 0.5*cf_[i][j];
96  }
97  }
98 
99  isInitialized_ = true;
100  }
101 
102  /*
103  * Load internal state from an archive.
104  */
106  {
107  // Precondition
108  if (nAtomType_ <= 0) {
109  UTIL_THROW( "nAtomType must be set before loadParameters");
110  }
111 
112  // Read parameters
113  loadCArray2D<double> (ar, "epsilon", epsilon_[0],
114  nAtomType_, nAtomType_, MaxAtomType);
115  loadCArray2D<double>(ar, "sigma", sigma_[0],
116  nAtomType_, nAtomType_, MaxAtomType);
117 
118  #ifdef UTIL_MPI
119  MpiLoader<Serializable::IArchive> loader(*this, ar);
120  loader.load(sigmaSq_[0], nAtomType_, nAtomType_, MaxAtomType);
121  loader.load(cf_[0], nAtomType_, nAtomType_, MaxAtomType);
122  loader.load(ce_[0], nAtomType_, nAtomType_, MaxAtomType);
123  loader.load(maxPairCutoff_);
124  #else
125  ar.unpack(sigmaSq_[0], nAtomType_, nAtomType_, MaxAtomType);
126  ar.unpack(cf_[0], nAtomType_, nAtomType_, MaxAtomType);
127  ar.unpack(ce_[0], nAtomType_, nAtomType_, MaxAtomType);
128  ar >> maxPairCutoff_;
129  #endif
130  isInitialized_ = true;
131  }
132 
133  /*
134  * Save internal state to an archive.
135  */
137  {
138  ar.pack(epsilon_[0], nAtomType_, nAtomType_, MaxAtomType);
139  ar.pack(sigma_[0], nAtomType_, nAtomType_, MaxAtomType);
140  ar.pack(sigmaSq_[0], nAtomType_, nAtomType_, MaxAtomType);
141  ar.pack(cf_[0], nAtomType_, nAtomType_, MaxAtomType);
142  ar.pack(ce_[0], nAtomType_, nAtomType_, MaxAtomType);
143  ar << maxPairCutoff_;
144  }
145 
146  /*
147  * Set nAtomType
148  */
149  void DpdPair::setNAtomType(int nAtomType)
150  {
151  if (nAtomType <= 0) {
152  UTIL_THROW("nAtomType <= 0");
153  }
154  if (nAtomType > MaxAtomType) {
155  UTIL_THROW("nAtomType > DpdPair::MaxAtomType");
156  }
157  nAtomType_ = nAtomType;
158  }
159 
160  /*
161  * Reset epsilon_[i][j] after initialization
162  */
163  void DpdPair::setEpsilon(int i, int j, double epsilon)
164  {
165 
166  // Preconditions
167  if (!isInitialized_) {
168  UTIL_THROW("Cannot modify epsilon before DpdPair is initialized");
169  }
170  if (i < 0 || i >= nAtomType_) {
171  UTIL_THROW("Invalid atom type index i");
172  }
173  if (j < 0 || j >= nAtomType_) {
174  UTIL_THROW("Invalid atom type index j");
175  }
176 
177  epsilon_[i][j] = epsilon;
178  cf_[i][j] = epsilon_[i][j]/sigmaSq_[i][j];
179  ce_[i][j] = 0.5*cf_[i][j];
180 
181  if (j != i) {
182  epsilon_[j][i] = epsilon;
183  cf_[j][i] = cf_[i][j];
184  ce_[j][i] = ce_[i][j];
185  }
186  }
187 
188  /*
189  * Reset sigma_[i][j] after initialization
190  */
191  void DpdPair::setSigma(int i, int j, double sigma)
192  {
193 
194  // Preconditions
195  if (!isInitialized_) {
196  UTIL_THROW("Cannot modify sigma before DpdPair is initialized");
197  }
198  if (i < 0 || i >= nAtomType_) {
199  UTIL_THROW("Invalid atom type index i");
200  }
201  if (j < 0 || j >= nAtomType_) {
202  UTIL_THROW("Invalid atom type index j");
203  }
204 
205  sigma_[i][j] = sigma;
206  sigmaSq_[i][j] = sigma*sigma;
207  cf_[i][j] = epsilon_[i][j]/sigmaSq_[i][j];
208  ce_[i][j] = 0.5*cf_[i][j];
209 
210  // Symmetrize
211  if (j != i) {
212  sigma_[j][i] = sigma;
213  sigmaSq_[j][i] = sigmaSq_[i][j];
214  cf_[j][i] = cf_[i][j];
215  ce_[j][i] = ce_[i][j];
216  }
217 
218  }
219 
220  /*
221  * Get pair interaction strength.
222  */
223  double DpdPair::epsilon(int i, int j) const
224  {
225  assert(i >= 0 && i < nAtomType_);
226  assert(j >= 0 && j < nAtomType_);
227  return epsilon_[i][j];
228  }
229 
230  /*
231  * Get pair interaction strength.
232  */
233  double DpdPair::sigma(int i, int j) const
234  {
235  assert(i >= 0 && i < nAtomType_);
236  assert(j >= 0 && j < nAtomType_);
237  return sigma_[i][j];
238  }
239 
240  /*
241  * Get maximum of pair cutoff distance, for all atom type pairs.
242  */
243  double DpdPair::maxPairCutoff() const
244  { return maxPairCutoff_; }
245 
246  /*
247  * Set a potential energy parameter, identified by a string.
248  */
249  void DpdPair::set(std::string name, int i, int j, double value)
250  {
251  if (name == "epsilon") {
252  epsilon_[i][j] = value;
253  epsilon_[j][i] = value;
254  } else
255  if (name == "sigma") {
256  sigma_[i][j] = value;
257  sigma_[j][i] = value;
258  } else {
259  UTIL_THROW("Unrecognized parameter name");
260  }
261  }
262 
263  /*
264  * Get a parameter value, identified by a string.
265  */
266  double DpdPair::get(std::string name, int i, int j) const
267  {
268  double value = 0.0;
269  if (name == "epsilon") {
270  value = epsilon_[i][j];
271  } else
272  if (name == "sigma") {
273  value = sigma_[i][j];
274  } else {
275  UTIL_THROW("Unrecognized parameter name");
276  }
277  return value;
278  }
279 
280 }
double get(std::string name, int i, int j) const
Get a parameter value, identified by a string.
Definition: DpdPair.cpp:266
void setEpsilon(int i, int j, double epsilon)
Set LJ interaction energy for a specific pair of Atom types.
Definition: DpdPair.cpp:163
Soft pair potential used in dissipative particle dynamics (DPD) simulations of Groot, Warren et al.
Definition: DpdPair.h:31
Classes used by all simpatico molecular simulations.
Saving / output archive for binary ostream.
double sigma(int i, int j) const
Get range for a specific pair of Atom types.
Definition: DpdPair.cpp:233
DpdPair & operator=(const DpdPair &other)
Assignment.
Definition: DpdPair.cpp:51
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void pack(const T &data)
Pack one object of type T.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Definition: DpdPair.cpp:136
Utility classes for scientific computation.
Definition: accumulators.mod:1
void set(std::string name, int i, int j, double value)
Modify a parameter, identified by a string.
Definition: DpdPair.cpp:249
DpdPair()
Constructor.
Definition: DpdPair.cpp:22
void readParameters(std::istream &in)
Read epsilon and sigma, initialize other variables.
Definition: DpdPair.cpp:72
Saving archive for binary istream.
Provides methods for MPI-aware loading of data from input archive.
Definition: MpiLoader.h:43
void setClassName(const char *className)
Set class name string.
void setSigma(int i, int j, double sigma)
Get LJ range for a specific pair of Atom types.
Definition: DpdPair.cpp:191
double epsilon(int i, int j) const
Get interaction energy for a specific pair of Atom types.
Definition: DpdPair.cpp:223
double maxPairCutoff() const
Get maximum of pair cutoff distance, for all atom type pairs.
Definition: DpdPair.cpp:243
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Definition: DpdPair.cpp:105
void setNAtomType(int nAtomType)
Set nAtomType value.
Definition: DpdPair.cpp:149