1 |
#include <iostream> |
2 |
|
3 |
#include <stdlib.h> |
4 |
#include <stdio.h> |
5 |
|
6 |
#include "utils/simError.h" |
7 |
#include "brains/SimState.hpp" |
8 |
|
9 |
|
10 |
SimState::SimState(){ |
11 |
|
12 |
nElements = 0; |
13 |
arraysAllocated = false; |
14 |
|
15 |
pos = NULL; |
16 |
vel = NULL; |
17 |
frc = NULL; |
18 |
trq = NULL; |
19 |
Amat = NULL; |
20 |
mu = NULL; |
21 |
ul = NULL; |
22 |
} |
23 |
|
24 |
|
25 |
SimState::~SimState(){ |
26 |
|
27 |
if(pos != NULL) delete[] pos; |
28 |
if(vel != NULL) delete[] vel; |
29 |
if(frc != NULL) delete[] frc; |
30 |
if(trq != NULL) delete[] trq; |
31 |
if(Amat != NULL) delete[] Amat; |
32 |
if(mu != NULL) delete[] mu; |
33 |
if(ul != NULL) delete[] ul; |
34 |
|
35 |
} |
36 |
|
37 |
|
38 |
void SimState::createArrays (int the_nElements) { |
39 |
int i, index3, index9; |
40 |
|
41 |
if( arraysAllocated ){ |
42 |
|
43 |
sprintf( painCave.errMsg, |
44 |
"Someone has attempted to allocate SimState arrays before " |
45 |
"deallocating the previous arrays.\n" ); |
46 |
painCave.isFatal = 1; |
47 |
simError(); |
48 |
} |
49 |
|
50 |
nElements = the_nElements; |
51 |
|
52 |
pos = new double[nElements*3]; |
53 |
vel = new double[nElements*3]; |
54 |
frc = new double[nElements*3]; |
55 |
trq = new double[nElements*3]; |
56 |
Amat = new double[nElements*9]; |
57 |
mu = new double[nElements]; |
58 |
ul = new double[nElements*3]; |
59 |
|
60 |
// init directional values to zero |
61 |
|
62 |
for( i=0; i<nElements; i++){ |
63 |
index3 = i*3; |
64 |
index9 = i*9; |
65 |
|
66 |
trq[index3+0] = 0.0; |
67 |
trq[index3+1] = 0.0; |
68 |
trq[index3+2] = 0.0; |
69 |
|
70 |
Amat[index9+0] = 1.0; |
71 |
Amat[index9+1] = 0.0; |
72 |
Amat[index9+2] = 0.0; |
73 |
|
74 |
Amat[index9+3] = 0.0; |
75 |
Amat[index9+4] = 1.0; |
76 |
Amat[index9+5] = 0.0; |
77 |
|
78 |
Amat[index9+6] = 0.0; |
79 |
Amat[index9+7] = 0.0; |
80 |
Amat[index9+8] = 1.0; |
81 |
|
82 |
mu[i] = 0.0; |
83 |
|
84 |
ul[index3+0] = 1.0; |
85 |
ul[index3+1] = 0.0; |
86 |
ul[index3+2] = 0.0; |
87 |
} |
88 |
|
89 |
arraysAllocated = true; |
90 |
} |
91 |
|
92 |
void SimState::destroyArrays( void ){ |
93 |
|
94 |
if(pos != NULL) delete[] pos; |
95 |
if(vel != NULL) delete[] vel; |
96 |
if(frc != NULL) delete[] frc; |
97 |
if(trq != NULL) delete[] trq; |
98 |
if(Amat != NULL) delete[] Amat; |
99 |
if(mu != NULL) delete[] mu; |
100 |
if(ul != NULL) delete[] ul; |
101 |
|
102 |
pos = NULL; |
103 |
vel = NULL; |
104 |
frc = NULL; |
105 |
trq = NULL; |
106 |
Amat = NULL; |
107 |
mu = NULL; |
108 |
ul = NULL; |
109 |
|
110 |
|
111 |
arraysAllocated = false; |
112 |
nElements = 0; |
113 |
} |
114 |
|
115 |
void SimState::getAtomPointers( int index, |
116 |
double** the_pos, |
117 |
double** the_vel, |
118 |
double** the_frc, |
119 |
double** the_trq, |
120 |
double** the_Amat, |
121 |
double** the_mu, |
122 |
double** the_ul){ |
123 |
int index3, index9; |
124 |
|
125 |
if( arraysAllocated ){ |
126 |
|
127 |
index3 = index*3; |
128 |
index9 = index*9; |
129 |
|
130 |
*the_pos = &(pos[index3]); |
131 |
*the_vel = &(vel[index3]); |
132 |
*the_frc = &(frc[index3]); |
133 |
*the_trq = &(trq[index3]); |
134 |
*the_Amat = &(Amat[index9]); |
135 |
*the_mu = &(mu[index]); |
136 |
*the_ul = &(ul[index3]); |
137 |
} |
138 |
else{ |
139 |
|
140 |
sprintf( painCave.errMsg, |
141 |
"Atom %d attempted to access its arrays before they had been" |
142 |
" allocated\n", |
143 |
index ); |
144 |
painCave.isFatal = 1; |
145 |
simError(); |
146 |
} |
147 |
} |