CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
ConfigDoF.cc
Go to the documentation of this file.
2 #include "casm/clex/ConfigDoF.hh"
6 #include "casm/clex/PrimClex.hh"
7 #include "casm/clex/Supercell.hh"
9 
10 
11 namespace CASM {
12 
13  ConfigDoF::ConfigDoF(Index _N, double _tol) :
14  m_N(_N),
15  m_occupation(m_N),
16  m_deformation(Eigen::Matrix3d::Identity()),
17  m_has_deformation(false),
18  m_tol(_tol) {
19  }
20 
21  //*******************************************************************************
22 
23  ConfigDoF::ConfigDoF(const Array<int> &_occ, double _tol):
24  m_N(_occ.size()),
25  m_occupation(_occ),
26  m_deformation(Eigen::Matrix3d::Identity()),
27  m_has_deformation(false),
28  m_tol(_tol) {
29  }
30 
31  //*******************************************************************************
32 
33  ConfigDoF::ConfigDoF(const Array<int> &_occ, const Eigen::MatrixXd &_disp, const Eigen::Matrix3d &_deformation, double _tol):
34  m_N(_occ.size()),
35  m_occupation(_occ),
36  m_displacement(_disp),
37  m_deformation(_deformation),
38  m_has_deformation(false),
39  m_tol(_tol) {
40  if(size() != occupation().size() || size() != displacement().cols()) {
41  std::cerr << "CRITICAL ERROR: Attempting to initialize ConfigDoF with data structures of incompatible size: \n"
42  << " occupation().size() = " << occupation().size() << " and displacement().size() = " << displacement().size() << "\n"
43  << " Exiting...\n";
44  exit(1);
45  }
46 
47  }
48 
49  //*******************************************************************************
50 
52  m_N = 0;
56  }
57 
59  _occupation().clear();
60  }
61 
64  }
65 
67  _deformation() = Eigen::Matrix3d::Identity();
68  m_has_deformation = false;
69  }
70 
71  //*******************************************************************************
72 
74  _deformation().swap(RHS._deformation());
75  _occupation().swap(RHS._occupation());
76  _displacement().swap(RHS._displacement());
77  std::swap(m_N, RHS.m_N);
78  std::swap(m_tol, RHS.m_tol);
80  }
81 
82  //*******************************************************************************
83 
84  void ConfigDoF::set_occupation(const Array<int> &new_occupation) {
85  if(!m_N)
86  m_N = new_occupation.size();
87 
88  if(m_N != new_occupation.size()) {
89  std::cerr << "CRITICAL ERROR: In ConfigDoF::set_occupation(), attempting to set occupation to size " << new_occupation.size() << ",\n"
90  << " which does not match initialized size of ConfigDoF -> " << size() << "\n"
91  << " Exiting...\n";
92  assert(0);
93  exit(1);
94  }
95 
96  _occupation() = new_occupation;
97 
98  }
99 
100  //*******************************************************************************
101 
102  void ConfigDoF::set_displacement(const displacement_matrix_t &new_displacement) {
103  if(!m_N)
104  m_N = new_displacement.cols();
105 
106  if(m_N != new_displacement.cols()) {
107  std::cerr << "CRITICAL ERROR: In ConfigDoF::set_displacement(), attempting to set displacement to size " << new_displacement.cols() << ",\n"
108  << " which does not match initialized size of ConfigDoF -> " << size() << "\n"
109  << " Exiting...\n";
110  assert(0);
111  exit(1);
112  }
113 
114  _displacement() = new_displacement;
115  }
116 
117  //*******************************************************************************
118 
119  void ConfigDoF::set_deformation(const Eigen::Matrix3d &new_deformation) {
120  m_has_deformation = true;
121  _deformation() = new_deformation;
122  }
123 
124  //*******************************************************************************
125 
127  json = jsonParser::object();
128  if(occupation().size())
129  json["occupation"] = occupation();
130  if(displacement().size())
131  json["displacement"] = displacement();
132  if(has_deformation())
133  json["deformation"] = deformation();
134 
135  return json;
136  }
137 
138  //*******************************************************************************
139  void ConfigDoF::from_json(const jsonParser &json) {
140  clear();
141  json.get_if(_occupation(), "occupation");
142  m_N = occupation().size();
143 
144  json.get_if(_displacement(), "displacement");
145  if(displacement().cols() && size() && displacement().cols() != size()) {
146  std::cerr << "CRITICAL ERROR: In ConfigDoF::from_json(), parsing displacement having size " << displacement().cols() << ",\n"
147  << " which does not match initialized size of ConfigDoF -> " << size() << "\n"
148  << " Exiting...\n";
149  assert(0);
150  exit(1);
151  }
152 
153  if(json.contains("deformation")) {
154  CASM::from_json(_deformation(), json["deformation"]);
155  m_has_deformation = true;
156  }
157  }
158 
159  //*******************************************************************************
160 
161  jsonParser &to_json(const ConfigDoF &value, jsonParser &json) {
162  return value.to_json(json);
163  }
164 
165  //*******************************************************************************
166 
167  void from_json(ConfigDoF &value, const jsonParser &json) {
168  value.from_json(json);
169  }
170 
171  //*******************************************************************************
172 
173  // Calculate transformed ConfigDoF from PermuteIterator via
174  // transformed_configdof = permute_iterator * configdof
176  Eigen::Matrix3d fg_cart_op = it.sym_op().matrix();
177  if(dof.has_deformation())
178  dof.set_deformation(fg_cart_op * dof.deformation() * fg_cart_op.transpose());
179 
180  Permutation tperm(it.combined_permute());
181  if(dof.occupation().size())
182  dof.set_occupation(tperm * dof.occupation());
183 
184  if(dof.displacement().cols()) {
185  Eigen::MatrixXd new_disp = fg_cart_op * dof.displacement();
186  dof.set_displacement(Eigen::MatrixXd(3, dof.size()));
187  for(Index i = 0; i < dof.size(); i++)
188  dof.disp(i) = new_disp.col(tperm[i]);
189  }
190  return dof;
191  }
192 
193  //*******************************************************************************
194 
195  void swap(ConfigDoF &A, ConfigDoF &B) {
196  A.swap(B);
197  }
198 
200  Correlation correlations(const ConfigDoF &configdof, const Supercell &scel, Clexulator &clexulator) {
201 
202  //Size of the supercell will be used for normalizing correlations to a per primitive cell value
203  int scel_vol = scel.volume();
204 
205  Correlation correlations = Correlation::Zero(clexulator.corr_size());
206 
207  //Inform Clexulator of the bitstring
208 
209  //TODO: This will probably get more complicated with displacements and stuff
210  clexulator.set_config_occ(configdof.occupation().begin());
211  //mc_clexor.set_config_disp(mc_confdof.m_displacements.begin()); //or whatever
212  //mc_clexor.set_config_strain(mc_confdof.m_strain.begin()); //or whatever
213 
214  //Holds contribution to global correlations from a particular neighborhood
215  std::vector<double> tcorr(clexulator.corr_size(), 0.0);
216  //std::vector<double> corr(clexulator.corr_size(), 0.0);
217 
218  for(int v = 0; v < scel_vol; v++) {
219 
220  //Point the Clexulator to the right neighborhood
221  clexulator.set_nlist(scel.nlist().sites(v).data());
222 
223  //Fill up contributions
224  clexulator.calc_global_corr_contribution(&tcorr[0]);
225 
226  //Add contributions to total correlations
227  for(int i = 0; i < tcorr.size(); i++) {
228  correlations[i] += tcorr[i];
229  }
230 
231  }
232 
233  // normalize by supercell volume
234  for(int i = 0; i < clexulator.corr_size(); i++) {
235  correlations[i] /= (double) scel_vol;
236  }
237 
238  return correlations;
239  }
240 
242  Eigen::VectorXd correlations_vec(const ConfigDoF &configdof, const Supercell &scel, Clexulator &clexulator) {
243 
244  //Size of the supercell will be used for normalizing correlations to a per primitive cell value
245  int scel_vol = scel.volume();
246 
247  Eigen::VectorXd correlations = Eigen::VectorXd::Zero(clexulator.corr_size());
248 
249  //Inform Clexulator of the bitstring
250 
251  //TODO: This will probably get more complicated with displacements and stuff
252  clexulator.set_config_occ(configdof.occupation().begin());
253  //mc_clexor.set_config_disp(mc_confdof.m_displacements.begin()); //or whatever
254  //mc_clexor.set_config_strain(mc_confdof.m_strain.begin()); //or whatever
255 
256  //Holds contribution to global correlations from a particular neighborhood
258  //std::vector<double> corr(clexulator.corr_size(), 0.0);
259 
260  for(int v = 0; v < scel_vol; v++) {
261 
262  //Point the Clexulator to the right neighborhood
263  clexulator.set_nlist(scel.nlist().sites(v).data());
264 
265  //Fill up contributions
266  clexulator.calc_global_corr_contribution(tcorr.data());
267 
268  correlations += tcorr;
269 
270  }
271 
272  correlations /= (double) scel_vol;
273 
274  return correlations;
275  }
276 
279 
280  // [basis_site][site_occupant_index]
281  auto convert = get_index_converter(scel.get_prim(), scel.get_prim().get_struc_molecule());
282 
283  // create an array to count the number of each molecule
284  Array<int> num_each_molecule(scel.get_prim().get_struc_molecule().size(), 0);
285 
286  // count the number of each molecule
287  for(Index i = 0; i < configdof.size(); i++) {
288  num_each_molecule[ convert[ scel.get_b(i) ][ configdof.occ(i)] ]++;
289  }
290 
291  return num_each_molecule;
292  }
293 
295  Eigen::VectorXi get_num_each_molecule_vec(const ConfigDoF &configdof, const Supercell &scel) {
296 
297  // [basis_site][site_occupant_index]
298  auto convert = get_index_converter(scel.get_prim(), scel.get_prim().get_struc_molecule());
299 
300  // create an array to count the number of each molecule
301  Eigen::VectorXi num_each_molecule = Eigen::VectorXi::Zero(scel.get_prim().get_struc_molecule().size());
302 
303  // count the number of each molecule
304  for(Index i = 0; i < configdof.size(); i++) {
305  num_each_molecule(convert[ scel.get_b(i) ][ configdof.occ(i)])++;
306  }
307 
308  return num_each_molecule;
309  }
310 
312  Eigen::VectorXd comp_n(const ConfigDoF &configdof, const Supercell &scel) {
313  return get_num_each_molecule_vec(configdof, scel).cast<double>() / scel.volume();
314  }
315 
316 }
317 
Eigen::MatrixXd MatrixXd
bool m_has_deformation
Definition: ConfigDoF.hh:154
int & occ(Index i)
Definition: ConfigDoF.hh:61
ReturnArray< int > get_num_each_molecule(const ConfigDoF &configdof, const Supercell &scel)
Returns num_each_molecule[ molecule_type], where 'molecule_type' is ordered as Structure::get_struc_m...
Definition: ConfigDoF.cc:278
void from_json(ClexDescription &desc, const jsonParser &json)
void set_displacement(const displacement_matrix_t &_displacement)
Definition: ConfigDoF.cc:102
Index size() const
Definition: Array.hh:145
bool get_if(T &t, const std::string &key, Args...args) const
Definition: jsonParser.hh:740
const Structure & get_prim() const
Definition: Supercell.cc:74
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
std::vector< Molecule > get_struc_molecule() const
Returns an Array of each possible Molecule in this Structure.
Definition: Structure.cc:146
Array< int > & _occupation()
Definition: ConfigDoF.hh:162
Correlation correlations(const ConfigDoF &configdof, const Supercell &scel, Clexulator &clexulator)
Returns correlations using 'clexulator'. Supercell needs a correctly populated neighbor list...
Definition: ConfigDoF.cc:200
size_type corr_size() const
Number of correlations.
Definition: Clexulator.hh:370
void swap(ConfigDoF &RHS)
Definition: ConfigDoF.cc:73
Main CASM namespace.
Definition: complete.cpp:8
void from_json(const jsonParser &json)
Definition: ConfigDoF.cc:139
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:37
void swap(Array< T > &RHS)
Definition: Array.hh:449
Index size() const
*** ACCESSORS ***
Definition: ConfigDoF.hh:53
const Eigen::Matrix3d & deformation() const
Definition: ConfigDoF.hh:85
displacement_matrix_t & _displacement()
Definition: ConfigDoF.hh:166
void swap(ConfigDoF &A, ConfigDoF &B)
Definition: ConfigDoF.cc:195
Eigen::VectorXi get_num_each_molecule_vec(const ConfigDoF &configdof, const Supercell &scel)
Returns num_each_molecule(molecule_type), where 'molecule_type' is ordered as Structure::get_struc_mo...
Definition: ConfigDoF.cc:295
Eigen::VectorXd comp_n(const ConfigDoF &configdof, const Supercell &scel)
Returns comp_n, the number of each molecule per primitive cell, ordered as Structure::get_struc_molec...
Definition: ConfigDoF.cc:312
const matrix_type & matrix() const
Const access of entire cartesian symmetry matrix.
Definition: SymOp.hh:57
void set_config_occ(const int *_occ_ptr)
Set pointer to data structure containing occupation variables.
Definition: Clexulator.hh:400
void clear()
Definition: Array.hh:216
void calc_global_corr_contribution(double *corr_begin) const
Calculate contribution to global correlations from one unit cell.
Definition: Clexulator.hh:430
const SuperNeighborList & nlist() const
Returns the SuperNeighborList.
Definition: Supercell.cc:79
EigenIndex Index
For long integer indexing:
Index volume() const
Return number of primitive cells that fit inside of *this.
Definition: Supercell.hh:212
Evaluates correlations.
Definition: Clexulator.hh:240
A container class for the different degrees of freedom a Configuration might have.
Definition: ConfigDoF.hh:27
jsonParser & to_json(jsonParser &json) const
Definition: ConfigDoF.cc:126
Eigen::VectorXd VectorXd
std::vector< std::vector< Index > > get_index_converter(const Structure &struc, std::vector< Molecule > mol_list)
Helper Functions.
Definition: Structure.cc:1297
void set_deformation(const Eigen::Matrix3d &_deformation)
set_deformation sets ConfigDoF::has_deformation() to true
Definition: ConfigDoF.cc:119
Index m_N
Number of sites in the Configuration.
Definition: ConfigDoF.hh:141
Eigen::VectorXd Correlation
Definition: Correlation.hh:9
Eigen::Matrix3d Matrix3d
void clear_displacement()
Definition: ConfigDoF.cc:62
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:500
const displacement_matrix_t & displacement() const
Definition: ConfigDoF.hh:81
const Array< int > & occupation() const
Definition: ConfigDoF.hh:69
void clear_occupation()
Definition: ConfigDoF.cc:58
Eigen::Matrix3d & _deformation()
Definition: ConfigDoF.hh:170
bool has_deformation() const
Definition: ConfigDoF.hh:98
Eigen::MatrixXd displacement_matrix_t
Definition: ConfigDoF.hh:32
T const * begin() const
Definition: Array.hh:185
void set_nlist(const long int *_nlist_ptr)
Set pointer to neighbor list.
Definition: Clexulator.hh:413
const std::vector< size_type > & sites(size_type unitcell_index) const
const Access the list of sites neighboring a particular unit cell
displacement_t disp(Index i)
Definition: ConfigDoF.hh:73
Index get_b(Index i) const
Definition: Supercell.hh:259
void clear_deformation()
Definition: ConfigDoF.cc:66
static jsonParser object()
Returns an empty json object.
Definition: jsonParser.hh:329
Eigen::VectorXd correlations_vec(const ConfigDoF &configdof, const Supercell &scel, Clexulator &clexulator)
Returns correlations using 'clexulator'. Supercell needs a correctly populated neighbor list...
Definition: ConfigDoF.cc:242
Object & apply(const Transform &f, Object &obj, Args &&...args)
void set_occupation(const Array< int > &_occupation)
Definition: ConfigDoF.cc:84
Permutation combined_permute() const
Returns the combination of factor_group permutation and translation permutation.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW ConfigDoF(Index N=0, double _tol=TOL)
fixes alignment of m_deformation
Definition: ConfigDoF.cc:13