From 5ba4109f0021e7b2c9c66821461742a339e07355 Mon Sep 17 00:00:00 2001 From: pants Date: Wed, 7 Dec 2016 13:29:51 -0500 Subject: added support for hyperuniform lattices using existing hard-sphere jamming routine --- src/spheres_poly/vector.h | 471 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 471 insertions(+) create mode 100644 src/spheres_poly/vector.h (limited to 'src/spheres_poly/vector.h') diff --git a/src/spheres_poly/vector.h b/src/spheres_poly/vector.h new file mode 100644 index 0000000..9ee481f --- /dev/null +++ b/src/spheres_poly/vector.h @@ -0,0 +1,471 @@ +#ifndef VECTOR_H +#define VECTOR_H + +#include +#include + +#define DIM 2 + +template +class vector { + + public: + T x[D]; + + public: + + vector(); + vector(const T[D]); + vector(const vector&); + ~vector(); + + vector& operator+=(const vector&); + vector& operator-=(const vector&); + vector& operator*=(const T); + vector& operator/=(const T); + vector operator+(const vector&) const; + vector operator-(const vector&) const; + vector operator*(const T) const; + vector operator/(const T) const; + vector operator%(const T) const; + bool operator==(const vector &a) const; + + vector integer() const; + vector Double() const; + static vector integer(const vector&); + static vector Double(const vector&); + + T& operator[](const unsigned int); + + double dot(const vector&) const; + static double dot(const vector&, const vector&); + + double norm_squared() const; + static double norm_squared(const vector&); + + void read(std::ifstream&); + void write(std::ofstream&) const; +}; + + +template +std::ostream& operator<<(std::ostream&, const vector&); + + +// constructor +// ~~~~~~~~~~~ +template +vector::vector() +{ + for(int k=0; k +vector::vector(const T x_i[D]) +{ + for(int k=0; k +vector::vector(const vector &v) +{ + for(int k=0; k +vector::~vector() +{ +} + + +// += +// ~~ +template +inline vector& vector::operator+=(const vector &v) +{ + for(int k=0; k +inline vector& vector::operator-=(const vector &v) +{ + for(int k=0; k +inline vector& vector::operator*=(const T s) +{ + for(int k=0; k +inline vector& vector::operator/=(const T s) +{ + for(int k=0; k +inline vector vector::operator+(const vector &a) const +{ + vector c; + + for(int k=0; k +inline vector vector::operator-(const vector &a) const +{ + vector c; + + for(int k=0; k +inline vector vector::operator*(const T s) const +{ + vector c; + + for(int k=0; k +inline vector vector::operator/(const T s) const +{ + vector c; + + for(int k=0; k +inline bool vector::operator==(const vector &a) const +{ + for(int k=0; k +inline vector vector::operator%(const T s) const +{ + vector c; + + for(int k=0; k +inline vector vector::integer() const +{ + vector c; + + for(int k=0; k +inline vector vector::integer(const vector& v) +{ + return v.integer(); +} + + +// double +// ~~~~~~~ +template +inline vector vector::Double() const +{ + vector c; + + for(int k=0; k +inline vector vector::Double(const vector& v) +{ + return v.Double(); +} + + + +// [] +// ~~ +template +inline T& vector::operator[](const unsigned int i) +{ + return x[i]; +} + + +// Dot +// ~~~ +template +inline double vector::dot(const vector &a) const +{ + double d=0; + + for(int k=0; k +inline double vector::dot(const vector &a, const vector &b) +{ + return a.dot(b); +} + + +// NormSquared +// ~~~~~~~~~~~ +template +inline double vector::norm_squared() const +{ + return dot(*this, *this); +} + +template +inline double vector::norm_squared(const vector& v) +{ + return v.norm_squared(); +} + + +// read +// ~~~~ +template +void vector::read(std::ifstream& in) +{ + in.read((char*)x, sizeof(T)*D); +} + +// write +// ~~~~~ +template +void vector::write(std::ofstream& out) const +{ + out.write((const char*)x, sizeof(T)*D); +} + + + +// Insertion +// ~~~~~~~~~ +template +std::ostream& operator<<(std::ostream& os, const vector& v) +{ + os << "("; + + for(int k=0; k +class vector_field { + + public: + int elements; + + private: + vector* f; + vector size; // number of grid points for each dimension + vector offset; + + public: + + vector_field(); + vector_field(const vector&); + ~vector_field(); + + vector get_size() const; + void set_size(const vector&); + + vector& get(const vector&); + + void read(std::ifstream&); + void write(std::ofstream&) const; + + static void swap(vector_field&, vector_field&); +}; + + +// vector_field +// ~~~~~~~~~~~~ +template +vector_field::vector_field() + : f(0), elements(0) +{ +} + + +// vector_field +// ~~~~~~~~~~~~ +template +vector_field::vector_field(const vector& s) + : f(0) +{ + set_size(s); +} + +// ~vector_field +// ~~~~~~~~~~~~~ +template +vector_field::~vector_field() +{ + if(f != 0) + delete[] f; +} + +// get_size +// ~~~~~~~~ +template +inline vector vector_field::get_size() const +{ + return size; +} + +// set_size +// ~~~~~~~~ +template +void vector_field::set_size(const vector& s) +{ + if(f != 0) + delete[] f; + + size = s; + + elements = 1; + for(int i=0; i[elements]; +} + +// get +// ~~~ +template +inline vector& vector_field::get(const vector& pos) +{ + int p=0; + for(int i=0; i +void vector_field::read(std::ifstream& in) +{ + in.read((char*)f, elements*sizeof(T)*V); +} + + +// write +// ~~~~~ +template +void vector_field::write(std::ofstream& out) const +{ + out.write((const char*)f, elements*sizeof(T)*V); +} + +// swap +// ~~~~ +template +void vector_field::swap(vector_field& v1, + vector_field& v2) +{ + vector* f; + + f = v1.f; + v1.f = v2.f; + v2.f = f; +} + + + +#endif -- cgit v1.2.3-70-g09d2