Simpatico  v1.10
EwaldInteraction.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 "EwaldInteraction.h"
9 #include <util/math/Constants.h>
10 #ifdef UTIL_MPI
11 #include <util/mpi/MpiLoader.h>
12 #endif
13 
14 #include <iostream>
15 #include <cstring>
16 #include <cmath>
17 
18 namespace Simp
19 {
20 
21  using namespace Util;
22 
23  /*
24  * Constructor.
25  */
27  : epsilon_(0.0),
28  alpha_(0.0),
29  rSpaceCutoff_(0.0),
30  isInitialized_(false)
31  { setClassName("EwaldInteraction");}
32 
33  /*
34  * Copy constructor.
35  */
37  : epsilon_(other.epsilon_),
38  alpha_(other.alpha_),
39  rSpaceCutoff_(other.rSpaceCutoff_),
40  rSpaceCutoffSq_(other.rSpaceCutoffSq_),
41  ce_(other.ce_),
42  cf_(other.cf_),
43  cg_(other.cg_),
44  isInitialized_(other.isInitialized_)
45  {}
46 
47  /*
48  * Assignment operator.
49  */
51  {
52  epsilon_ = other.epsilon_;
53  alpha_ = other.alpha_;
54  rSpaceCutoff_ = other.rSpaceCutoff_;
55  rSpaceCutoffSq_ = other.rSpaceCutoffSq_;
56  ce_ = other.ce_;
57  cf_ = other.cf_;
58  cg_ = other.cg_;
59  isInitialized_ = other.isInitialized_;
60  return *this;
61  }
62 
63  /*
64  * Read parameters (epsilon, alpha, rSpaceCutoff) from file.
65  */
66  void EwaldInteraction::readParameters(std::istream &in)
67  {
68  read<double>(in, "epsilon", epsilon_);
69  read<double>(in, "alpha", alpha_);
70  read<double>(in, "rSpaceCutoff", rSpaceCutoff_);
71  setDerivedConstants();
72  isInitialized_ = true;
73  }
74 
75  /*
76  * Load internal state from an archive.
77  */
79  {
80  // Load all parameters that appear in parameter file
81  loadParameter<double>(ar, "epsilon", epsilon_);
82  loadParameter<double>(ar, "alpha", alpha_);
83  loadParameter<double>(ar, "rSpaceCutoff", rSpaceCutoff_);
84  setDerivedConstants();
85  isInitialized_ = true;
86  }
87 
88  /*
89  * Save internal state to an archive.
90  */
92  {
93  ar << epsilon_;
94  ar << alpha_;
95  ar << rSpaceCutoff_;
96  }
97 
98  /*
99  * Modify a parameter, identified by a string.
100  */
101  void EwaldInteraction::set(std::string name, double value)
102  {
103  if (name == "epsilon") {
104  epsilon_ = value;
105  } else
106  if (name == "alpha") {
107  alpha_ = value;
108  } else
109  if (name == "rSpaceCutoff") {
110  rSpaceCutoff_ = value;
111  } else {
112  UTIL_THROW("Unrecognized parameter name");
113  }
114  setDerivedConstants();
115  }
116 
117  /*
118  * Get a parameter value, identified by a string.
119  */
120  double EwaldInteraction::get(std::string name) const
121  {
122  double value = 0.0;
123  if (name == "epsilon") {
124  value = epsilon_;
125  } else
126  if (name == "alpha") {
127  value = alpha_;
128  } else
129  if (name == "rSpaceCutoff") {
130  value = rSpaceCutoff_;
131  } else {
132  UTIL_THROW("Unrecognized parameter name");
133  }
134  return value;
135  }
136 
137  void EwaldInteraction::setDerivedConstants()
138  {
139  rSpaceCutoffSq_ = rSpaceCutoff_*rSpaceCutoff_;
140  double pi = Constants::Pi;
141  ce_ = 1.0/(4.0*pi*epsilon_);
142  cf_ = 2.0*alpha_/sqrt(pi);
143  cg_ = -0.25/(alpha_*alpha_);
144  }
145 
146 }
EwaldInteraction & operator=(const EwaldInteraction &other)
Assignment.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Classes used by all simpatico molecular simulations.
Saving / output archive for binary ostream.
EwaldInteraction()
Default constructor.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Implementation of r-space and k-space Ewald Coulomb interactions.
Utility classes for scientific computation.
Definition: accumulators.mod:1
void set(std::string name, double value)
Modify a parameter, identified by a string.
double get(std::string name) const
Get a parameter value, identified by a string.
Saving archive for binary istream.
static const double Pi
Trigonometric constant Pi.
Definition: Constants.h:35
void setClassName(const char *className)
Set class name string.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
void readParameters(std::istream &in)
Read epsilon, alpha, and rCutoff.