1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*
Packing of hard spheres via molecular dynamics
Developed by Monica Skoge, 2006, Princeton University
Contact: Aleksandar Donev (adonev@math.princeton.edu) with questions
This code may be used, modified and distributed freely.
Please cite:
"Packing Hyperspheres in High-Dimensional Euclidean Spaces"
M. Skoge, A. Donev, F. H. Stillinger and S. Torquato, 2006
if you use these codes.
*/
#ifndef GRID_FIELD_H
#define GRID_FIELD_H
#include "vector.h"
// ======================================================================
// grid_field
// ======================================================================
// A field of V-vectors on a D dimensional manifold
template<int D, class T>
class grid_field {
public:
int elements;
private:
T* f;
vector<D, int> size; // number of grid points for each dimension
vector<D, int> offset;
public:
grid_field();
grid_field(const vector<D, int>&);
~grid_field();
T& get(const vector<D, int>&);
vector<D, int> get_size() const;
void set_size(const vector<D, int>&);
void set_size(const int);
void initialize(const int i);
};
// grid_field
// ~~~~~~~~~~~~
template<int D, class T>
grid_field<D, T>::grid_field()
: f(0), elements(0)
{
}
// grid_field
// ~~~~~~~~~~~~
template<int D, class T>
grid_field<D, T>::grid_field(const vector<D, int>& s)
: f(0)
{
set_size(s);
}
// ~grid_field
// ~~~~~~~~~~~~~
template <int D, class T>
grid_field<D, T>::~grid_field()
{
if(f != 0)
delete[] f;
}
// get_size
// ~~~~~~~~
template<int D, class T>
inline vector<D, int> grid_field<D, T>::get_size() const
{
return size;
}
// set_size
// ~~~~~~~~
template<int D, class T>
void grid_field<D, T>::set_size(const vector<D, int>& s)
{
if(f != 0)
delete[] f;
size = s;
elements = 1;
for(int i=0; i<D; i++) {
offset[i] = elements;
elements *= size.x[i];
}
f = new T[elements];
}
// set_size
// ~~~~~~~~
template<int D, class T>
void grid_field<D, T>::set_size(const int s)
{
vector<D, int> square;
for(int k=0; k<D; k++)
square[k] = s;
set_size(square);
}
// get
// ~~~
template<int D, class T>
inline T& grid_field<D, T>::get(const vector<D, int>& pos)
{
int p=0;
for(int i=0; i<D; i++)
p += pos.x[i]*offset[i];
return f[p];
}
// initialize
// ~~~
template<int D, class T>
void grid_field<D, T>::initialize(const int value)
{
for(int i=0; i<elements; i++)
f[i] = value;
}
#endif
|