Page MenuHomec4science

neigh_container.hh
No OneTemporary

File Metadata

Created
Tue, Aug 6, 16:18

neigh_container.hh

#include "../neigh_list.h"
#include <vector>
#ifndef NEIGH_CONTAINER_H
#define NEIGH_CONTAINER_H
using namespace LAMMPS_NS;
/** Container to efficiently store data associated with neighbourhood
structures
**/
class NeighContainer {
private:
inline const int offset(size_t index) const {
return this->firstneigh[index] - this->firstneigh[0];
}
public:
void set(const NeighList * list,
const Atom * atom,
double * const eatom,
double ** const vatom) {
this->inum = list->inum;
this->ilist = list->ilist;
this->numneigh = list->numneigh;
this->firstneigh = list->firstneigh;
this->x = atom->x;
this->f = atom->f;
this->type = atom->type;
this->eatom = eatom;
this->vatom = vatom;
this-> nb_pairs = 0.;
for (int i = 0; i < this->inum; ++i) {
this->nb_pairs += numneigh[i];
}
}
NeighContainer (){};
NeighContainer (const NeighList * list,
const Atom * atom,
double * const eatom,
double ** const vatom) {
this->set(list, atom, eatom, vatom);
}
virtual ~NeighContainer(){};
/* ---------------------------------------------------------------------- */
//! yields the field associated with atom i in the container
class iterator {
public:
inline iterator (const NeighContainer & my_neigh_container,
bool end = false)
:i(0), neigh_cont(my_neigh_container){
if (end) this->skip_to_end();}
inline iterator & operator++(){this->i++;};
inline bool operator!=(const iterator & other){
return this->i != other.i;};
inline iterator & operator*() {return *this;}
inline void skip_to_end() {this->i = this->neigh_cont.inum; };
inline double * get_x() {return this->neigh_cont.x[this->i];}
inline double * get_f() {return this->neigh_cont.f[this->i];}
inline int & get_type() {return this->neigh_cont.type[this->i];}
inline double & get_eatom() {return this->neigh_cont.eatom[this->i];}
inline double * get_vatom() {return this->neigh_cont.vatom[this->i];}
inline const size_t get_offset() const {
return this->neigh_cont.offset(this->i);}
inline const int & get_id() const {return this->i;}
template<typename T>
class i_container {
public:
void resize(const NeighContainer & nc, const size_t & len_=1) {
this->len=len_;
this->values.resize(this->len*nc.inum);}
void set(const T& val) {
std::uninitialized_fill_n(this->values.begin(), this->values.size(), val);}
inline T& operator[](const iterator & it) {
return this->values[len*it.i];}
inline T* operator()(const iterator & it) {
return &this->values[len*it.i];}
protected:
std::vector<T> values;
size_t len = 1;
};
/* ---------------------------------------------------------------------- */
//! yields the field associated with atom j in the container
class jterator {
public:
inline jterator (const iterator & my_iterator, bool end = false)
:j(0), iter(my_iterator){if (end) this->skip_to_end();};
inline jterator & operator++(){this->j++;};
inline bool operator!=(const jterator & other){
return this->j != other.j;};
inline jterator & operator*() {return *this;}
inline int & get_j_index() const {
return this->iter.neigh_cont.firstneigh[this->iter.i][this->j];
}
inline const int & get_id() const {return this->get_j_index();}
inline void skip_to_end() {
this->j = this->iter.neigh_cont.numneigh[this->iter.i];};
inline double * get_x() {
return this->iter.neigh_cont.x[this->get_j_index()];}
inline double * get_f() {
return this->iter.neigh_cont.f[this->get_j_index()];}
inline int & get_type() {
return this->iter.neigh_cont.type[this->get_j_index()];}
inline double & get_eatom() {
return this->iter.neigh_cont.eatom[this->get_j_index()];}
inline double * get_vatom() {
return this->iter.neigh_cont.vatom[this->get_j_index()];}
inline const iterator& get_i() const {return this->iter;}
inline const iterator get_j() const {
iterator it(this->iter.neigh_cont);
it.i = this->get_j_index();
return it;
}
template<typename T>
class ij_container: public i_container<T> {
public:
void resize(const NeighContainer & nc, const size_t & len_=1) {
this->len=len_;
this->values.resize(this->len*nc.nb_pairs);}
inline T& operator[](const iterator::jterator & it) {
return this->values[this->len*(it.iter.get_offset()+it.j)];}
inline T* operator()(const iterator::jterator & it) {
return &this->values[this->len*(it.iter.get_offset()+it.j)];}
};
private:
int j;
const iterator& iter;
};
/* ---------------------------------------------------------------------- */
inline jterator begin() const {return jterator(*this);}
inline const jterator end() const {return jterator(*this, true);}
size_t size(){return this->neigh_cont.numneigh[this->i];}
private:
int i;
const NeighContainer & neigh_cont;
};
/* ---------------------------------------------------------------------- */
inline iterator begin(){return iterator(*this);}
inline const iterator end() const {return iterator(*this, true);}
size_t size(){return this->inum;};
/* ---------------------------------------------------------------------- */
protected:
int inum;
unsigned int nb_pairs;
int * ilist;
int * numneigh;
int ** firstneigh;
double **x;
double **f;
int * type;
double * eatom;
double ** vatom;
};
/* ---------------------------------------------------------------------- */
using index_pair = std::array<int,2>;
inline const index_pair & ordered(index_pair & id) {
if (id[0] > id[1]) {
auto tmp {std::move(id[0])};
id[0] = std::move(id[1]);
id[1] = std::move(tmp);
}
}
/* ---------------------------------------------------------------------- */
using index_pair = std::array<int,2>;
inline const index_pair ordered(const int& i, const int& j) {
return (i < j) ? index_pair({i, j}) : index_pair({j, i});
}
#endif /* NEIGH_CONTAINER_H */

Event Timeline