-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgravity.cc
231 lines (172 loc) · 6.19 KB
/
gravity.cc
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
Gravity Simulator
gravity.cc
member function definition file
Author: Eric Y. Date: 25 Aug 2014
*/
#include <cmath>
#include <cstdlib>
#include <iostream>
#include "gravity.h"
using namespace std;
///Default constructor
/**ParticleSystem()
**uses predefined variables when constructing object
**/
ParticleSystem::ParticleSystem(){
srand48( time(NULL) );
setNumDims(2);
setNumParticles(500);
setdT(0.1);
setTmax(100.0);
double lowerLims[2] = {-10,-10};
double upperLims[2] = {10,10};
iLLims = new double[iNumDims];
iULims = new double[iNumDims];
setLLims( lowerLims );
setULims( upperLims );
createBins();
clearMatrix( iVelocities );
// cout << "\n" << iNumParticles << "\n" << iNumDims << endl;
initPositions();
setInit(1);
}
///Destructor
/**~ParticleSystem()
**Frees dynamically allocated memory
**/
ParticleSystem::~ParticleSystem(){
delete [] iLLims;
delete [] iULims;
for( unsigned long particle = 0; particle < iNumParticles; ++particle){
delete [] iGravities[particle];
delete [] iVelocities[particle];
delete [] iPositions[particle];
}
delete [] iGravities;
delete [] iVelocities;
delete [] iPositions;
}
///Member Function createBins()
/**Dynamically allocated matricies to hole program data
**i.e. gravity, acceleration, velocities and positions
**/
void ParticleSystem::createBins(){
iGravities = new double*[iNumParticles];
for( unsigned long particle = 0; particle < iNumParticles; particle++)
iGravities[particle] = new double[iNumDims];
iVelocities = new double*[iNumParticles];
for( unsigned long particle = 0; particle < iNumParticles; particle++)
iVelocities[particle] = new double[iNumDims];
iPositions = new double*[iNumParticles];
for( unsigned long particle = 0; particle < iNumParticles; ++particle)
iPositions[particle] = new double[iNumDims];
}
///Member Function clearMatrix( double **)
/**Clears contents of input array
**/
void ParticleSystem::clearMatrix( double **aMatrix ){
for( unsigned long rowLoop = 0; rowLoop < iNumParticles; rowLoop++ ){
for( unsigned long colLoop = 0; colLoop < iNumDims; colLoop++ )
aMatrix[rowLoop][colLoop] = 0.0;
}
}
///Member Function setLLims( douuble *aLLims)
/**Fills the internal array with the lower bounds of plotting from input array
**/
void ParticleSystem::setLLims( double *aLLims ){
for( unsigned long dim = 0; dim < iNumDims; ++dim )
iLLims[dim] = aLLims[dim];
}
///Member Function setULims( douuble *aLLims)
/**Fills the internal array with the upper bounds of plotting from input array
**/
void ParticleSystem::setULims( double *aULims ){
for( unsigned long dim = 0; dim < iNumDims; ++dim )
iULims[dim] = aULims[dim];
}
///Member Function setLLims( douuble *aLLims)
/**Fills the internal array with the lower bounds of plotting from input array
**/
void ParticleSystem::initPositions(){
for(unsigned long particle = 0; particle < iNumParticles; ++particle){
for(unsigned long dim = 0; dim < iNumDims; ++dim)
iPositions[particle][dim] = ( iULims[dim]-iLLims[dim] ) * drand48() + iLLims[dim];
}
}
///Member Function dumpState()
/**Prints all the particle locations for the current state
**/
void ParticleSystem::dumpState(){
for(unsigned long particle = 0; particle < iNumParticles; ++particle){
for(unsigned long dim = 0; dim < iNumDims; ++dim)
cout << iPositions[particle][dim] << " ";
cout << endl;
}
}
///Member Function gravForce(double *, double *)
/**Calculates a gravittional force component between to particles w/ input position arrays
** F = G * m * m / r ^ (3) * <displacement vector component>
**/
double ParticleSystem::gravForce( unsigned long aDim, /*unsigned long numDims,*/ double *thisParticle, double *otherParticle ){
double gForce;
double distance = 0;
for(unsigned long dim = 0; dim < iNumDims; dim++)
distance += pow( (otherParticle[dim] - thisParticle[dim]), 2);
distance = pow(distance, 0.5);
//
//Avoiding a divide by zero error
if( pow(distance, -2.0) > 1E2 ){
if(otherParticle[aDim] > thisParticle[aDim]) return 1;
else return -1;
}
gForce = gConst / ( pow(distance, 3) ) * (otherParticle[aDim] - thisParticle[aDim]);
return gForce;
}
///Member Function findGravity()
/**using the gravForce function this function fills the matrix of
** Grav. Acceleration components for each particle
**/
void ParticleSystem::findGravity(){
clearMatrix( iGravities );
//for each particle
for(unsigned long thisParticle = 0; thisParticle < iNumParticles; thisParticle++){
//find the gravity from each other particle and make a running sum
for(unsigned long otherParticle = 0; otherParticle < iNumParticles; otherParticle++){
for(unsigned long thisDim = 0; thisDim < iNumDims; thisDim++){
if( otherParticle != thisParticle ){
iGravities[thisParticle][thisDim] +=
gravForce(thisDim, iPositions[thisParticle], iPositions[otherParticle]);
}
}
}
}
}
///Member Function stepVelocities()
/**for each particle in each dimension
** Change the velocities by dv = g * dt
**/
void ParticleSystem::stepVelocities(){
//for each particle
for(unsigned long particle = 0; particle < iNumParticles; particle++){
//in each dimension
for(unsigned long dim = 0; dim < iNumDims; dim++){
//Change the velocities by dv = g * dt
iVelocities[particle][dim] += iGravities[particle][dim] * idT;
}
}
}
///Member Function stepVelocities()
/**for each particle in each dimension
** Change the position by dx = v * dt
**/
void ParticleSystem::stepPositions(){
//for each particle
for(unsigned long particle = 0; particle < iNumParticles; particle++){
//in each dimension
for(unsigned long dim = 0; dim < iNumDims; dim++){
//Change the position by dx = v * dt
iPositions[particle][dim] += iVelocities[particle][dim] * idT;
}
}
}