NAMD
FreeEnergyVector.C
Go to the documentation of this file.
1 
7 // written by David Hurwitz.
8 
9 #include <math.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "InfoStream.h"
13 #include "FreeEnergyEnums.h"
14 #include "FreeEnergyAssert.h"
15 #include "Vector.h"
16 #include "FreeEnergyVector.h"
17 
19 //------------------------------------------------
20 // init this vector to (0,0,0) for kRegular Type
21 // init this vector to (x,y,z) for kRandom Type
22 // where x, y, and z are in the range (0:1)
23 //
24 // Note: The assumption is made that srand() has
25 // already been called.
26 //------------------------------------------------
27  if (Type == kRegular) {
28  m_x = 0.0;
29  m_y = 0.0;
30  m_z = 0.0;
31  }
32  else {
33  ASSERT(Type == kRandom);
34  Random();
35  }
36 }
37 
38 
39 AVector::AVector(double x, double y, double z) {
40 //-------------------------------------------
41 // init this vector to (x,y,z)
42 //-------------------------------------------
43  m_x = x;
44  m_y = y;
45  m_z = z;
46 }
47 
48 
50 //-------------------------------------------
51 // init this vector to Vector
52 //-------------------------------------------
53  m_x = Vector.m_x;
54  m_y = Vector.m_y;
55  m_z = Vector.m_z;
56 }
57 
58 
60 //------------------------------------------------
61 // replace this vector with a new random vector
62 // where x, y, and z are in the range (0:1)
63 //------------------------------------------------
64  double RandMax;
65 
66  RandMax = RAND_MAX;
67  m_x = (double)rand() / RandMax;
68  m_y = (double)rand() / RandMax;
69  m_z = (double)rand() / RandMax;
70  ASSERT(m_x <= 1.0);
71  ASSERT(m_y <= 1.0);
72  ASSERT(m_z <= 1.0);
73  return(*this);
74 }
75 
76 
77 Bool_t AVector::Set(double x, double y, double z) {
78 //-------------------------------------------
79 // int this vector to (x,y,z)
80 //-------------------------------------------
81  m_x = x;
82  m_y = y;
83  m_z = z;
84  return(kTrue);
85 }
86 
87 
89 //----------------------------------------------------------
90 // create a new vector: the sum of this one and passed one
91 //----------------------------------------------------------
92  AVector Point(vector.m_x+m_x, vector.m_y+m_y, vector.m_z+m_z);
93  return(Point);
94 }
95 
96 
98 //----------------------------------------------------------
99 // create a new vector: this one minus the passed one
100 //----------------------------------------------------------
101  AVector Point(m_x-vector.m_x, m_y-vector.m_y, m_z-vector.m_z);
102  return(Point);
103 }
104 
105 
106 AVector AVector::operator/ (double divisor) {
107 //---------------------------------------------------
108 // create a new vector: this one divided by divisor
109 //---------------------------------------------------
110  AVector Point(m_x/divisor, m_y/divisor, m_z/divisor);
111  return(Point);
112 }
113 
114 
115 AVector& AVector::operator/= (double divisor) {
116 //------------------------------------------------------
117 // divide this vector by divisor and return a ref to it
118 //------------------------------------------------------
119  m_x /= divisor;
120  m_y /= divisor;
121  m_z /= divisor;
122  return(*this);
123 }
124 
125 
126 AVector& AVector::operator*= (double scalar) {
127 //------------------------------------------------------
128 // multiply this vector by scalar and return a ref to it
129 //------------------------------------------------------
130  m_x *= scalar;
131  m_y *= scalar;
132  m_z *= scalar;
133  return(*this);
134 }
135 
136 
137 AVector AVector::operator* (double scalar) {
138 //---------------------------------------------------
139 // create a new vector: this one divided by divisor
140 //---------------------------------------------------
141  AVector Point(m_x*scalar, m_y*scalar, m_z*scalar);
142  return(Point);
143 }
144 
145 
146 void SetEqual(AVector& Vec1, const Vector& Vec2) {
147 //------------------------------------------------------------------------
148 // used for casting Vector -> AVector
149 //------------------------------------------------------------------------
150  Vec1.m_x = Vec2.x;
151  Vec1.m_y = Vec2.y;
152  Vec1.m_z = Vec2.z;
153 }
154 
155 
156 void SetEqual(Vector& Vec1, const AVector& Vec2) {
157 //------------------------------------------------------------------------
158 // used for casting AVector -> Vector
159 //------------------------------------------------------------------------
160  Vec1.x = Vec2.m_x;
161  Vec1.y = Vec2.m_y;
162  Vec1.z = Vec2.m_z;
163 }
164 
165 
167 //-------------------------------------------
168 // set this vector to the passed one
169 //-------------------------------------------
170  m_x = vector.m_x;
171  m_y = vector.m_y;
172  m_z = vector.m_z;
173  return(*this);
174 }
175 
176 
178 //--------------------------------------------------
179 // set this vector to this one plus the passed one
180 //--------------------------------------------------
181  m_x += vector.m_x;
182  m_y += vector.m_y;
183  m_z += vector.m_z;
184  return(*this);
185 }
186 
187 
188 double& AVector::operator[] (int index) {
189 //-------------------------------------------------------------------
190 // return one element of this vector
191 // note: this op is used to get AND set an element
192 // (since it returns double&)
193 //-------------------------------------------------------------------
194  ASSERT( (index>=0) && (index<3) );
195 
196  switch(index) {
197  case 0: return(m_x);
198  case 1: return(m_y);
199  case 2: return(m_z);
200  default: return(m_x); // should never get here
201  }
202 }
203 
204 
205 double AVector::Dist() {
206 //-------------------------------------------------------------------
207 // calculate distance from this point to (0, 0, 0)
208 //-------------------------------------------------------------------
209  return( sqrt(m_x*m_x + m_y*m_y + m_z*m_z) );
210 }
211 
212 
214 //-------------------------------------------------------------------
215 // calculate distance-squared from this point to (0, 0, 0)
216 //-------------------------------------------------------------------
217  return(m_x*m_x + m_y*m_y + m_z*m_z);
218 }
219 
220 
221 double AVector::Dist(const AVector& Vector) {
222 //-------------------------------------------------------------------
223 // calculate distance between this point and Vector
224 //-------------------------------------------------------------------
225  double d1 = (m_x - Vector.m_x);
226  double d2 = (m_y - Vector.m_y);
227  double d3 = (m_z - Vector.m_z);
228  return( sqrt(d1*d1 + d2*d2 + d3*d3) );
229 }
230 
231 
233 //-------------------------------------------------------------------
234 // calculate distance-squared between this point and Vector
235 //-------------------------------------------------------------------
236  double d1 = (m_x - Vector.m_x);
237  double d2 = (m_y - Vector.m_y);
238  double d3 = (m_z - Vector.m_z);
239  return(d1*d1 + d2*d2 + d3*d3);
240 }
241 
242 
244 //-------------------------------------------------------------------
245 // calculate this vector crossed with Vector (this x Vector).
246 // see Mathematical Handbook, p.118.
247 //-------------------------------------------------------------------
248  AVector CrossProduct;
249 
250  CrossProduct.m_x = (m_y * Vector.m_z) - (m_z * Vector.m_y);
251  CrossProduct.m_y = (m_z * Vector.m_x) - (m_x * Vector.m_z);
252  CrossProduct.m_z = (m_x * Vector.m_y) - (m_y * Vector.m_x);
253  return(CrossProduct);
254 }
255 
256 
257 double AVector::dot(const AVector& Vector) {
258 //-------------------------------------------------------------------
259 // calculate dot product of this vector and Vector
260 // see Mathematical Handbook, p.118.
261 //-------------------------------------------------------------------
262  return(m_x*Vector.m_x + m_y*Vector.m_y + m_z*Vector.m_z);
263 }
264 
265 
266 void AVector::Out() {
267 //-------------------------------------------------------------------
268 // write it
269 //-------------------------------------------------------------------
270  char Str1[20], Str2[20], Str3[20];
271 
272  sprintf(Str1, "%8.3f", m_x);
273  sprintf(Str2, "%8.3f", m_y);
274  sprintf(Str3, "%8.3f", m_z);
275  iout << "(" << Str1 << "," << Str2 << "," << Str3 << ")";
276 }
277 
278 
280 //-------------------------------------------------------------------
281 // write it to standard output
282 //-------------------------------------------------------------------
283  char Word1[20], Word2[20], Word3[20];
284 
285  if ( (fabs(m_x)<99999) && (fabs(m_y)<99999) && (fabs(m_z)<99999) ) {
286  sprintf(Word1, "%10.3f", m_x);
287  sprintf(Word2, "%10.3f", m_y);
288  sprintf(Word3, "%10.3f", m_z);
289  }
290  else {
291  sprintf(Word1, "%10.2e", m_x);
292  sprintf(Word2, "%10.2e", m_y);
293  sprintf(Word3, "%10.2e", m_z);
294  }
295  iout << "( " << Word1 << " " << Word2 << " " << Word3 << " )";
296 }
297 
298 
299 AVector& AVector::Scale(AVector& SmallVec, AVector& BigVec) {
300 //-------------------------------------------------------------------
301 // scale this vector, whose (x,y,z) are in the range (0:1),
302 // to be in the range (SmallVec:BigVec)
303 //-------------------------------------------------------------------
304  m_x = SmallVec.m_x + (BigVec.m_x - SmallVec.m_x) * m_x;
305  m_y = SmallVec.m_y + (BigVec.m_y - SmallVec.m_y) * m_y;
306  m_z = SmallVec.m_z + (BigVec.m_z - SmallVec.m_z) * m_z;
307  return(*this);
308 }
309 
Bool_t Set(double x, double y, double z)
AVector operator+(const AVector &Vector)
void Output()
AVector & operator+=(const AVector &Vector)
AVector cross(const AVector &Vector)
AVector & Scale(AVector &SmallVec, AVector &BigVec)
AVector & operator*=(double scalar)
AVector operator*(double scalar)
Definition: Vector.h:64
AVector & operator/=(double divisor)
BigReal z
Definition: Vector.h:66
#define iout
Definition: InfoStream.h:87
double & operator[](int index)
AVector operator/(double divisor)
double dot(const AVector &Vector)
Bool_t
#define ASSERT(E)
double m_z
gridSize z
BigReal x
Definition: Vector.h:66
void SetEqual(AVector &Vec1, const Vector &Vec2)
AVector operator-(const AVector &Vector)
BigReal y
Definition: Vector.h:66
AVector & operator=(const AVector &Vector)
double m_x
double Dist()
double DistSqr()
gridSize y
Vector_t
AVector & Random()
AVector(Vector_t Type=kRegular)
gridSize x
double m_y