CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Clexulator.hh
Go to the documentation of this file.
1 #ifndef CLEXULATOR_HH
2 #define CLEXULATOR_HH
3 #include <cstddef>
4 
5 #include "casm/external/boost.hh"
9 #include "casm/casm_io/Log.hh"
10 
11 namespace CASM {
12 
13  namespace Clexulator_impl {
14 
16  class Base {
17 
18  public:
19 
20  typedef unsigned int size_type;
21 
22 
23  Base(size_type _nlist_size, size_type _corr_size) :
24  m_nlist_size(_nlist_size),
25  m_corr_size(_corr_size) {}
26 
27  virtual ~Base() {}
28 
30  size_type nlist_size() const {
31  return m_nlist_size;
32  }
33 
35  size_type corr_size() const {
36  return m_corr_size;
37  }
38 
40  std::unique_ptr<Base> clone() const {
41  return std::unique_ptr<Base>(_clone());
42  }
43 
46  const std::set<UnitCellCoord> &neighborhood() const {
47  return m_neighborhood;
48  }
49 
52  const std::set<UnitCellCoord> &neighborhood(size_type linear_orbit_index) const {
53  return m_orbit_neighborhood[linear_orbit_index];
54  }
55 
58  return m_weight_matrix;
59  }
60 
70  void set_config_occ(const int *_occ_ptr) {
71  m_occ_ptr = _occ_ptr;
72  }
73 
83  void set_nlist(const long int *_nlist_ptr) {
84  m_nlist_ptr = _nlist_ptr;
85  return;
86  };
87 
101  virtual void calc_global_corr_contribution(double *corr_begin) const = 0;
102 
118  virtual void calc_restricted_global_corr_contribution(double *corr_begin, size_type const *ind_list_begin, size_type const *ind_list_end) const = 0;
119 
134  virtual void calc_point_corr(int b_index, double *corr_begin) const = 0;
135 
152  virtual void calc_restricted_point_corr(int b_index, double *corr_begin, size_type const *ind_list_begin, size_type const *ind_list_end) const = 0;
153 
170  virtual void calc_delta_point_corr(int b_index, int occ_i, int occ_f, double *corr_begin) const = 0;
171 
190  virtual void calc_restricted_delta_point_corr(int b_index,
191  int occ_i,
192  int occ_f,
193  double *corr_begin,
194  size_type const *ind_list_begin,
195  size_type const *ind_list_end) const = 0;
196 
197 
198  private:
199 
201  virtual Base *_clone() const = 0;
202 
204  size_type m_nlist_size;
205 
207  size_type m_corr_size;
208 
209 
210  protected:
211 
213  const int *m_occ_ptr;
214 
216  const long int *m_nlist_ptr;
217 
220  std::set<UnitCellCoord> m_neighborhood;
221 
224  std::vector<std::set<UnitCellCoord> > m_orbit_neighborhood;
225 
228 
229  };
230  }
231 
240  class Clexulator {
241 
242  public:
243 
245 
246 
248 
269  Clexulator(std::string name,
270  boost::filesystem::path dirpath,
271  PrimNeighborList &nlist,
272  const Logging &logging,
273  std::string compile_options,
274  std::string so_options) {
275 
276  namespace fs = boost::filesystem;
277 
278  // Construct the RuntimeLibrary that will store the loaded clexulator library
279  try {
280  m_lib = std::make_shared<RuntimeLibrary>(
281  (dirpath / name).string(),
282  compile_options,
283  so_options,
284  "compile time depends on how many basis functions are included");
285  }
286  catch(std::exception &e) {
287  logging.log() << "Clexulator construction failed: could not construct runtime library." << std::endl;
288  throw;
289  }
290 
291  // Get the Clexulator factory function
292  std::function<Clexulator_impl::Base* (void)> factory;
293  factory = m_lib->get_function<Clexulator_impl::Base* (void)>("make_" + name);
294 
295  // Use the factory to construct the clexulator and store it in m_clex
296  m_clex.reset(factory());
297 
298  // Check nlist has the right weight_matrix
299  if(nlist.weight_matrix() != m_clex->weight_matrix()) {
300  std::cerr << "Error in Clexulator constructor: weight matrix of neighbor "
301  "list does not match the weight matrix used to print the "
302  "clexulator." << std::endl;
303  std::cerr << "nlist weight matrix: \n" << nlist.weight_matrix() << std::endl;
304  std::cerr << "clexulator weight matrix: \n" << m_clex->weight_matrix() << std::endl;
305  throw std::runtime_error(
306  "Error in Clexulator constructor: weight matrix of neighbor list does "
307  "not match the weight matrix used to print the clexulator. Try 'casm bset -uf'.");
308  }
309 
310  // Expand the given neighbor list as necessary
311  nlist.expand(neighborhood().begin(), neighborhood().end());
312 
313  }
314 
315 
317  Clexulator(const Clexulator &B) :
318  m_name(B.name()),
319  m_lib(B.m_lib) {
320 
321  if(B.m_clex.get() != nullptr) {
322  m_clex.reset(B.m_clex->clone().release());
323  }
324  }
325 
328  swap(*this, B);
329  }
330 
332  // ensure Clexulator is deleted before library
333  delete m_clex.release();
334  }
335 
338 
339  swap(*this, B);
340 
341  return *this;
342  }
343 
345  friend void swap(Clexulator &first, Clexulator &second) {
346 
347  using std::swap;
348 
349  swap(first.m_name, second.m_name);
350  swap(first.m_clex, second.m_clex);
351  swap(first.m_lib, second.m_lib);
352  }
353 
355  bool initialized() const {
356  return m_lib.get() != nullptr;
357  }
358 
360  std::string name() const {
361  return m_name;
362  }
363 
365  size_type nlist_size() const {
366  return m_clex->nlist_size();
367  }
368 
370  size_type corr_size() const {
371  return m_clex->corr_size();
372  }
373 
376  const std::set<UnitCellCoord> &neighborhood() const {
377  return m_clex->neighborhood();
378  }
379 
382  const std::set<UnitCellCoord> &neighborhood(size_type linear_orbit_index) const {
383  return m_clex->neighborhood(linear_orbit_index);
384  }
385 
388  return m_clex->weight_matrix();
389  }
390 
400  void set_config_occ(const int *_occ_ptr) {
401  return m_clex->set_config_occ(_occ_ptr);
402  }
403 
413  void set_nlist(const long int *_nlist_ptr) {
414  return m_clex->set_nlist(_nlist_ptr);
415  };
416 
430  void calc_global_corr_contribution(double *corr_begin) const {
431  m_clex->calc_global_corr_contribution(corr_begin);
432  }
433 
449  void calc_restricted_global_corr_contribution(double *corr_begin, size_type const *ind_list_begin, size_type const *ind_list_end) const {
450  m_clex->calc_restricted_global_corr_contribution(corr_begin, ind_list_begin, ind_list_end);
451  }
452 
467  void calc_point_corr(int b_index, double *corr_begin) const {
468  m_clex->calc_point_corr(b_index, corr_begin);
469  }
470 
487  void calc_restricted_point_corr(int b_index, double *corr_begin, size_type const *ind_list_begin, size_type const *ind_list_end) const {
488  m_clex->calc_restricted_point_corr(b_index, corr_begin, ind_list_begin, ind_list_end);
489  }
490 
507  void calc_delta_point_corr(int b_index, int occ_i, int occ_f, double *corr_begin) const {
508  m_clex->calc_delta_point_corr(b_index, occ_i, occ_f, corr_begin);
509  }
510 
530  int occ_i,
531  int occ_f,
532  double *corr_begin,
533  size_type const *ind_list_begin,
534  size_type const *ind_list_end) const {
535  m_clex->calc_restricted_delta_point_corr(b_index, occ_i, occ_f, corr_begin, ind_list_begin, ind_list_end);
536  }
537 
538 
539  private:
540 
541  std::string m_name;
542  std::unique_ptr<Clexulator_impl::Base> m_clex;
543  std::shared_ptr<RuntimeLibrary> m_lib;
544 
545  };
546 
547 }
548 
549 #endif
friend void swap(Clexulator &first, Clexulator &second)
Swap.
Definition: Clexulator.hh:345
std::vector< std::set< UnitCellCoord > > m_orbit_neighborhood
The UnitCellCoord involved in calculating the basis functions for a particular orbit, relative origin UnitCell.
Definition: Clexulator.hh:224
size_type m_nlist_size
The neighbor list size.
Definition: Clexulator.hh:204
std::shared_ptr< RuntimeLibrary > m_lib
Definition: Clexulator.hh:543
virtual void calc_delta_point_corr(int b_index, int occ_i, int occ_f, double *corr_begin) const =0
Calculate the change in point correlations due to changing an occupant.
const PrimNeighborList::Matrix3Type & weight_matrix() const
The weight matrix used for ordering the neighbor list.
Definition: Clexulator.hh:57
Clexulator_impl::Base::size_type size_type
Definition: Clexulator.hh:244
Clexulator(const Clexulator &B)
Copy constructor.
Definition: Clexulator.hh:317
size_type m_corr_size
The number of correlations.
Definition: Clexulator.hh:207
void set_config_occ(const int *_occ_ptr)
Set pointer to data structure containing occupation variables.
Definition: Clexulator.hh:70
bool initialized() const
Is runtime library loaded?
Definition: Clexulator.hh:355
size_type corr_size() const
Number of correlations.
Definition: Clexulator.hh:370
Main CASM namespace.
Definition: complete.cpp:8
size_type corr_size() const
Number of correlations.
Definition: Clexulator.hh:35
virtual void calc_global_corr_contribution(double *corr_begin) const =0
Calculate contribution to global correlations from one unit cell.
void expand(UnitCellCoord uccoord)
Expand the neighbor list to include the given UnitCellCoord.
Definition: NeighborList.cc:14
Log & log() const
Definition: Log.hh:255
const std::set< UnitCellCoord > & neighborhood() const
The UnitCellCoord involved in calculating the basis functions, relative origin UnitCell.
Definition: Clexulator.hh:46
size_type nlist_size() const
Neighbor list size.
Definition: Clexulator.hh:365
PrimNeighborList::Matrix3Type m_weight_matrix
The weight matrix used for ordering the neighbor list.
Definition: Clexulator.hh:227
void calc_point_corr(int b_index, double *corr_begin) const
Calculate point correlations about basis site 'b_index'.
Definition: Clexulator.hh:467
Clexulator(std::string name, boost::filesystem::path dirpath, PrimNeighborList &nlist, const Logging &logging, std::string compile_options, std::string so_options)
Construct a Clexulator.
Definition: Clexulator.hh:269
void calc_restricted_global_corr_contribution(double *corr_begin, size_type const *ind_list_begin, size_type const *ind_list_end) const
Calculate contribution to select global correlations from one unit cell.
Definition: Clexulator.hh:449
void swap(ConfigDoF &A, ConfigDoF &B)
Definition: ConfigDoF.cc:195
size_type nlist_size() const
Neighbor list size.
Definition: Clexulator.hh:30
void set_config_occ(const int *_occ_ptr)
Set pointer to data structure containing occupation variables.
Definition: Clexulator.hh:400
void calc_global_corr_contribution(double *corr_begin) const
Calculate contribution to global correlations from one unit cell.
Definition: Clexulator.hh:430
void calc_delta_point_corr(int b_index, int occ_i, int occ_f, double *corr_begin) const
Calculate the change in point correlations due to changing an occupant.
Definition: Clexulator.hh:507
Evaluates correlations.
Definition: Clexulator.hh:240
std::unique_ptr< Clexulator_impl::Base > m_clex
Definition: Clexulator.hh:542
const long int * m_nlist_ptr
Pointer to neighbor list.
Definition: Clexulator.hh:216
std::string name() const
Name.
Definition: Clexulator.hh:360
virtual void calc_restricted_global_corr_contribution(double *corr_begin, size_type const *ind_list_begin, size_type const *ind_list_end) const =0
Calculate contribution to select global correlations from one unit cell.
const PrimNeighborList::Matrix3Type & weight_matrix() const
The weight matrix used for ordering the neighbor list.
Definition: Clexulator.hh:387
void calc_restricted_point_corr(int b_index, double *corr_begin, size_type const *ind_list_begin, size_type const *ind_list_end) const
Calculate select point correlations about basis site 'b_index'.
Definition: Clexulator.hh:487
virtual void calc_restricted_point_corr(int b_index, double *corr_begin, size_type const *ind_list_begin, size_type const *ind_list_end) const =0
Calculate select point correlations about basis site 'b_index'.
const std::set< UnitCellCoord > & neighborhood() const
The UnitCellCoord involved in calculating the basis functions, relative origin UnitCell.
Definition: Clexulator.hh:376
const std::set< UnitCellCoord > & neighborhood(size_type linear_orbit_index) const
The UnitCellCoord involved in calculating the basis functions for a particular orbit, relative origin UnitCell.
Definition: Clexulator.hh:52
void calc_restricted_delta_point_corr(int b_index, int occ_i, int occ_f, double *corr_begin, size_type const *ind_list_begin, size_type const *ind_list_end) const
Calculate the change in select point correlations due to changing an occupant.
Definition: Clexulator.hh:529
virtual void calc_point_corr(int b_index, double *corr_begin) const =0
Calculate point correlations about basis site 'b_index'.
const int * m_occ_ptr
Pointer to beginning of data structure containing occupation variables.
Definition: Clexulator.hh:213
The PrimNeighborList gives the coordinates of UnitCell that are neighbors of the origin UnitCell...
Definition: NeighborList.hh:24
virtual void calc_restricted_delta_point_corr(int b_index, int occ_i, int occ_f, double *corr_begin, size_type const *ind_list_begin, size_type const *ind_list_end) const =0
Calculate the change in select point correlations due to changing an occupant.
void set_nlist(const long int *_nlist_ptr)
Set pointer to neighbor list.
Definition: Clexulator.hh:83
virtual Base * _clone() const =0
Clone the Clexulator.
void set_nlist(const long int *_nlist_ptr)
Set pointer to neighbor list.
Definition: Clexulator.hh:413
Matrix3Type weight_matrix() const
Return the weighting matrix W used to define the canonical order.
Definition: NeighborList.cc:9
Eigen::Matrix3l Matrix3Type
Definition: NeighborList.hh:31
Abstract base class for cluster expansion correlation calculations.
Definition: Clexulator.hh:16
std::string m_name
Definition: Clexulator.hh:541
std::unique_ptr< Base > clone() const
Clone the Clexulator.
Definition: Clexulator.hh:40
Base(size_type _nlist_size, size_type _corr_size)
Definition: Clexulator.hh:23
Clexulator(Clexulator &&B)
Move constructor.
Definition: Clexulator.hh:327
Clexulator & operator=(Clexulator B)
Assignment operator.
Definition: Clexulator.hh:337
std::set< UnitCellCoord > m_neighborhood
The UnitCellCoord involved in calculating the basis functions, relative origin UnitCell.
Definition: Clexulator.hh:220
const std::set< UnitCellCoord > & neighborhood(size_type linear_orbit_index) const
The UnitCellCoord involved in calculating the basis functions for a particular orbit, relative origin UnitCell.
Definition: Clexulator.hh:382