Skip to content
This repository
Browse code

*Codechange: gCalcFunc: MAJOR cleanup of. const' a lot of variables a…

…nd add additional variables to reduce x->get* calls

*Codechange: gGravObject: add const to all set* parameters and make all get* methods const
*Codechange: gDebug: add const to debugout() parameters
  • Loading branch information...
commit 35c7cd4cbd5044b733e12bbee4c112ac4b188e9a 1 parent a9f75a4
EoD authored
12 inc/gDebugFunc.h
@@ -5,12 +5,12 @@
5 5 #include "gMathVec.h"
6 6
7 7 void debugout(std::string strdbg, int dbgprio);
8   -void debugout(std::string strdbg, int var, int dbgprio);
9   -void debugout(std::string strdbg, long double var, int dbgprio);
10   -void debugout(std::string strdbg, mdv& var, int dbgprio);
11   -void debugout(std::string strdbg, long long var, int dbgprio);
12   -void debugout(std::string strdbg, mlv& var, int dbgprio);
13   -void debugout(std::string strdbg, std::string var, int dbgprio);
  8 +void debugout(std::string strdbg, const int var, int dbgprio);
  9 +void debugout(std::string strdbg, const long double var, int dbgprio);
  10 +void debugout(std::string strdbg, const mdv& var, int dbgprio);
  11 +void debugout(std::string strdbg, const long long var, int dbgprio);
  12 +void debugout(std::string strdbg, const mlv& var, int dbgprio);
  13 +void debugout(std::string strdbg, const std::string var, int dbgprio);
14 14 //void debugout(std::string strdbg, std::stringstream var, int dbgprio);
15 15 void debugout(std::string a);
16 16
35 inc/gGravObject.h
@@ -14,24 +14,23 @@ class GravObject {
14 14
15 15 GravObject(int id=-1, long double mass=0., long double radius=0.);
16 16
17   - void setCoord(mlv& x);
18   - void addCoords(mlv& x);
19   - long double getAbsMass();
20   - long double getSRTMass();
21   - void setMass(long double a);
22   - void setMass(double a);
23   - long double getRadius();
24   - void setRadius(long double a);
25   - long double getSchwarzschildRadius();
26   - long double drange(GravObject* mp2);
27   - long double dabsspeed();
28   - long double getAbsSpeed();
29   - bool setSpeed(mdv& v);
30   - bool addSpeed(mdv& v);
31   - long double getVolume();
32   - long double getSchwarzschildVolume();
33   - mdv getImpulse();
34   - long double getEnergy();
  17 + void setCoord(const mlv& x);
  18 + void addCoords(const mlv& x);
  19 + long double getAbsMass() const;
  20 + long double getSRTMass() const;
  21 + void setMass(const long double a);
  22 + void setMass(const double a);
  23 + long double getRadius() const;
  24 + void setRadius(const long double a);
  25 + long double getSchwarzschildRadius() const;
  26 + long double drange(const GravObject* mp2);
  27 + long double getAbsSpeed() const;
  28 + bool setSpeed(const mdv& v);
  29 + bool addSpeed(const mdv& v);
  30 + long double getVolume() const;
  31 + long double getSchwarzschildVolume() const;
  32 + mdv getImpulse() const;
  33 + long double getEnergy() const;
35 34 };
36 35
37 36 #endif
2  inc/gMathVec.h
@@ -95,7 +95,7 @@ template <typename T> mv<T>& operator+=(mv<T>& mv1, const mv<T>& mv2) {
95 95 return mv1;
96 96 }
97 97
98   -template <typename T> std::ostream& operator<<(std::ostream& stream, mv<T>& mv1) {
  98 +template <typename T> std::ostream& operator<<(std::ostream& stream, const mv<T>& mv1) {
99 99 stream << "(" << mv1.x << ", " << mv1.y << ", " << mv1.z << ")";
100 100 return stream;
101 101 }
83 src/gCalcFunc.cpp
@@ -42,8 +42,8 @@ int calc::calcForce(GravObject* mpmain, GravStep* vmpsinsert, mdv& mdvforcetotal
42 42 }
43 43
44 44 //distance between objects
45   - mlv mlvdist(0);
46   - mlvdist = mpsec->pos - mpmain->pos;
  45 + const mlv mlvdist = mpsec->pos - mpmain->pos;
  46 + const mdv mdvdist = mlvdist;
47 47 debugout("calcForce() - mlvdist=", mlvdist, 5);
48 48
49 49 //halb-relativistische Gravitations-Kraftberechnung
@@ -57,7 +57,7 @@ int calc::calcForce(GravObject* mpmain, GravStep* vmpsinsert, mdv& mdvforcetotal
57 57 debugout("calcForce() - dforce2=", dforce, 5);
58 58 dforce *= GRAVCONST;
59 59 debugout("calcForce() - dforce3=", dforce, 5);
60   - dforce /= (mdv)mlvdist * (mdv)mlvdist;
  60 + dforce /= mdvdist * mdvdist;
61 61 debugout("calcForce() - dforce4=", dforce, 5);
62 62
63 63 if (isnan(dforce)) {
@@ -73,8 +73,8 @@ int calc::calcForce(GravObject* mpmain, GravStep* vmpsinsert, mdv& mdvforcetotal
73 73 //mdvrquot calculated with MDVector because 'radius' is r / r^3
74 74 mdv mdvrquot(0);
75 75 //Converted from long (mm) to double (meter)
76   - debugout("calcForce() - abs(mlvdist)=", abs((mdv)mlvdist), 5);
77   - mdvrquot = (mdv)mlvdist / abs((mdv)mlvdist);
  76 + debugout("calcForce() - abs(mdvdist)=", abs(mdvdist), 5);
  77 + mdvrquot = mdvdist / abs(mdvdist);
78 78 debugout(" calcForce() - mdvrquot: ", mdvrquot, 5);
79 79 //mdvrquot = MVMath.DivMVNum(MVMath.ConvertToD(mlvdist), MVMath.ConvertToD(mlvdist).abs()); // vec r / |r|
80 80 //debugout(" Mdvrquot.abs,r1,r2,r3: "+mdvrquot.abs()+","+mdvrquot.x1+","+mdvrquot.x2+","+mdvrquot.x3);
@@ -129,7 +129,8 @@ GravStep* calc::calcAcc(GravStep* vmpsinsert, GravStep* vmpsout) {
129 129
130 130
131 131 #ifdef _OPENMP
132   - omp_set_num_threads(omp_get_num_procs() * omp_get_num_procs() / 2 + 1);
  132 + int numProcs = omp_get_num_procs();
  133 + omp_set_num_threads( numProcs * numProcs / 2 + 1);
133 134 #endif
134 135 bool bkillloop = false;
135 136
@@ -172,37 +173,33 @@ GravStep* calc::calcAcc(GravStep* vmpsinsert, GravStep* vmpsout) {
172 173 //debugout(" |MVforcetotal|,x1,x2,x3: "+mvforce.abs()+","+mvforce.x1+","+mvforce.x2+","+mvforce.x3);
173 174
174 175 //relativistic acceleration formula
175   - //Math.sqrtx( Math.powx(LIGHTSPEED,2.0) - Math.powx(mpmain.getSpeed(),2.0) / Math.powx(LIGHTSPEED,2.0));
176   - //TODO reform!
177   - long double da1 = gamma(mpold->getAbsSpeed());
178   - da1 *= powx(LIGHTSPEED, 2) * mvforce.x - mpold->vel.x * (mvforce * mpold->vel);
179   - da1 /= powx(LIGHTSPEED, 2) * mpold->getAbsMass();
180   -
181   - long double da2 = gamma(mpold->getAbsSpeed());
182   - da2 *= powx(LIGHTSPEED, 2) * mvforce.y - mpold->vel.y * (mvforce * mpold->vel);
183   - da2 /= powx(LIGHTSPEED, 2) * mpold->getAbsMass();
184   -
185   - long double da3 = gamma(mpold->getAbsSpeed());
186   - da3 *= powx(LIGHTSPEED, 2) * mvforce.z - mpold->vel.z * (mvforce * mpold->vel);
187   - da3 /= powx(LIGHTSPEED, 2) * mpold->getAbsMass();
188   - //debugout("calcAcc() - Acceleration (a1,a2,a3): "+da1+","+da2+","+da3);
  176 + const long double dLightspeed2 = powx(LIGHTSPEED, 2);
  177 + const long double dSpeedFactor1 = gamma(mpold->getAbsSpeed());
  178 + const long double dSpeedFactor2 = mvforce * mpold->vel;
  179 + const long double dMassFactor = dLightspeed2 * mpold->getAbsMass();
  180 +
  181 + long double da1 = dSpeedFactor1;
  182 + da1 *= dLightspeed2 * mvforce.x - mpold->vel.x * dSpeedFactor2;
  183 + da1 /= dMassFactor;
189 184
190   - debugout(" calcAcc() - da1:", da1, 5);
191   - debugout(" calcAcc() - da2:", da2, 5);
192   - debugout(" calcAcc() - da3:", da3, 5);
  185 + long double da2 = dSpeedFactor1;
  186 + da2 *= dLightspeed2 * mvforce.y - mpold->vel.y * dSpeedFactor2;
  187 + da2 /= dMassFactor;
193 188
194   - mdv mva(da1, da2, da3);
195   - mdv deltav(0);
  189 + long double da3 = dSpeedFactor1;
  190 + da3 *= dLightspeed2 * mvforce.z - mpold->vel.z * dSpeedFactor2;
  191 + da3 /= dMassFactor;
196 192
197   - deltav = mva * dtime_step;
  193 + const mdv mva(da1, da2, da3);
  194 + debugout(" calcAcc() - da = ", mva, 5);
198 195
  196 + const mdv deltav = mva * dtime_step;
199 197 debugout(" calcAcc() - deltav: ", deltav, 5);
200   - debugout(" calcAcc() - abs(deltav)+mpold->getAbsSpeed()=", abs(deltav)+mpold->getAbsSpeed(), 5);
201   - //debugout("calcAcc() - Delta-v (dv1,dv2,dv3): "+deltav.x1+","+deltav.x2+","+deltav.x3);
202 198
203 199 if (abs(deltav)+mpold->getAbsSpeed() > LIGHTSPEED) {
204   - debugout("calcAcc() - Changing dtime_step", 15);
205 200 #ifdef DEBUG
  201 + debugout("calcAcc() - Changing dtime_step", 15);
  202 + debugout("calcAcc() - abs(deltav)+mpold->getAbsSpeed()=", abs(deltav)+mpold->getAbsSpeed(), 5);
206 203 int count = 0;
207 204 #endif
208 205 while ((LIGHTSPEED - mpold->getAbsSpeed()) / abs(mva) < dtime_step) {
@@ -234,21 +231,13 @@ GravStep* calc::calcAcc(GravStep* vmpsinsert, GravStep* vmpsout) {
234 231 cerrors |= cerror::lightspeed;;
235 232 }
236 233
237   - //new function which produces new coords
238   - mlv mlvds(0);
239   -
240   - //calculates the delta-s (ds = v * dtime_step)
241   - //converts mvspeed mdvector to mlvector
242   - //FIX requ?
243   - mlvds = (mlv)(mpnew->vel * dtime_step);
244   - //mlvds = MVMath.ProMVNum(MVMath.ConvertToL(mpold.getMDVSpeed()),dtime_step);
245   - //debugout("calcAcc() - |mdv-v|,v1,v2,v3: "+mpold.getMDVSpeed().abs()+","+mpold.getMDVSpeed().x1+","+mpold.getMDVSpeed().x2+","+mpold.getMDVSpeed().x3);
246   - //debugout("calcAcc() - |mlv-v|,v1,v2,v3: "+MVMath.ConvertToL(mpold.getMDVSpeed()).abs()+","+MVMath.ConvertToL(mpold.getMDVSpeed()).x1+","+MVMath.ConvertToL(mpold.getMDVSpeed()).x2+","+MVMath.ConvertToL(mpold.getMDVSpeed()).x3);
247   - //debugout("calcAcc() - |delta-s|,ds1,ds2,ds3: "+mlvds.abs()+","+mlvds.x1+","+mlvds.x2+","+mlvds.x3);
248   -
249   - //new position = ds + old position
250   - long long limit = 0;
251   - ++limit;
  234 + /*
  235 + * new function which produces the new coordinates
  236 + * calculates the delta-s (ds = v * dtime_step)
  237 + * and converts mvspeed mdvector to mlvector
  238 + */
  239 + const mlv mlvds = mpnew->vel * dtime_step;
  240 +
252 241 //TODO FIX really requ
253 242 /* if(mpold->posx < 0)
254 243 limit = LLONG_MIN - mpold->posx;
@@ -283,10 +272,10 @@ GravStep* calc::calcAcc(GravStep* vmpsinsert, GravStep* vmpsout) {
283 272 return vmpsout;
284 273 }
285 274 */
  275 +
  276 + /* New position = ds + old position */
286 277 mpnew->addCoords(mlvds);
287 278 debugout("calcAcc() - mlvds=",mlvds,5);
288   - //mp.mlvpos = MVMath.AddMV(mlvds, mp.mlvpos);
289   - //debugout("calcAcc() - ID "+mpnew.id+": New Coords(x1,x2,x3): "+mpnew.mlvpos.x1+" , "+mpnew.mlvpos.x2+" , "+mpnew.mlvpos.x3);
290 279 debugout("calcAcc() - new (x,y,z)=",mpnew->pos,5);
291 280 }
292 281 if(bkillloop)
@@ -494,7 +483,7 @@ bool calc::checkSpeedBorder(GravStep* pgs_test, long double dpercentage) {
494 483 //diese for-schleife dient nur daf�r, dass die berechnung exakt wird, falls ein objekt �ber dpercentage% c kommt
495 484 std::vector<GravObject*>::iterator i;
496 485 for (i = pgs_test->objects.begin(); i != pgs_test->objects.end(); ++i) {
497   - if ((*i)->dabsspeed() >= (dpercentage*LIGHTSPEED)) {
  486 + if ((*i)->getAbsSpeed() >= (dpercentage*LIGHTSPEED)) {
498 487 //debugout("checkSpeedBorder() - Object Nr."+i+"/"+mp.id+" has a speed larger "+dpercentage*100.0+"% lightspeed",10);
499 488 return true;
500 489 }
12 src/gDebugFunc.cpp
@@ -17,7 +17,7 @@ void debugout(std::string strdbg, int dbgprio) {
17 17 #endif
18 18 }
19 19
20   -void debugout(std::string strdbg, int var, int dbgprio) {
  20 +void debugout(std::string strdbg, const int var, int dbgprio) {
21 21 #ifdef DEBUG
22 22 if (dbgprio > 100 || dbgprio < 0) {
23 23 dbgprio = 101;
@@ -29,7 +29,7 @@ void debugout(std::string strdbg, int var, int dbgprio) {
29 29 }
30 30 #endif
31 31 }
32   -void debugout(std::string strdbg, long double var, int dbgprio) {
  32 +void debugout(std::string strdbg, const long double var, int dbgprio) {
33 33 #ifdef DEBUG
34 34 if (dbgprio > 100 || dbgprio < 0) {
35 35 dbgprio = 101;
@@ -41,7 +41,7 @@ void debugout(std::string strdbg, long double var, int dbgprio) {
41 41 }
42 42 #endif
43 43 }
44   -void debugout(std::string strdbg, mdv& var, int dbgprio) {
  44 +void debugout(std::string strdbg, const mdv& var, int dbgprio) {
45 45 #ifdef DEBUG
46 46 if (dbgprio > 100 || dbgprio < 0) {
47 47 dbgprio = 101;
@@ -53,7 +53,7 @@ void debugout(std::string strdbg, mdv& var, int dbgprio) {
53 53 }
54 54 #endif
55 55 }
56   -void debugout(std::string strdbg, long long var, int dbgprio) {
  56 +void debugout(std::string strdbg, const long long var, int dbgprio) {
57 57 #ifdef DEBUG
58 58 if (dbgprio > 100 || dbgprio < 0) {
59 59 dbgprio = 101;
@@ -65,7 +65,7 @@ void debugout(std::string strdbg, long long var, int dbgprio) {
65 65 }
66 66 #endif
67 67 }
68   -void debugout(std::string strdbg, mlv& var, int dbgprio) {
  68 +void debugout(std::string strdbg, const mlv& var, int dbgprio) {
69 69 #ifdef DEBUG
70 70 if (dbgprio > 100 || dbgprio < 0) {
71 71 dbgprio = 101;
@@ -78,7 +78,7 @@ void debugout(std::string strdbg, mlv& var, int dbgprio) {
78 78 #endif
79 79 }
80 80
81   -void debugout(std::string strdbg, std::string var, int dbgprio) {
  81 +void debugout(std::string strdbg, const std::string var, int dbgprio) {
82 82 #ifdef DEBUG
83 83 if (dbgprio > 100 || dbgprio < 0) {
84 84 dbgprio = 101;
39 src/gGravObject.cpp
@@ -19,35 +19,35 @@ GravObject::GravObject(int i, long double m, long double r): id(i), mass(m), rad
19 19 pos = mlv();
20 20 }
21 21
22   -void GravObject::setCoord(mlv& x) {
  22 +void GravObject::setCoord(const mlv& x) {
23 23 pos = x;
24 24 }
25   -void GravObject::addCoords(mlv& x) {
  25 +void GravObject::addCoords(const mlv& x) {
26 26 pos += x;
27 27 }
28 28
29   -long double GravObject::getAbsMass() {
  29 +long double GravObject::getAbsMass() const {
30 30 return mass;
31 31 }
32   -long double GravObject::getSRTMass() {
  32 +long double GravObject::getSRTMass() const {
33 33 //falls der v-vektor (0|0|0) ist, ist auch v=0
34 34 if(vel == 0)
35 35 return mass;
36 36
37   - return (mass * gamma(dabsspeed()) );
  37 + return (mass * gamma(getAbsSpeed()) );
38 38 }
39   -void GravObject::setMass(long double a) {
  39 +void GravObject::setMass(const long double a) {
40 40 mass = a;
41 41 }
42   -void GravObject::setMass(double a) {
  42 +void GravObject::setMass(const double a) {
43 43 mass = (long double)a;
44 44 }
45 45
46   -long double GravObject::getRadius() {
  46 +long double GravObject::getRadius() const {
47 47 //debugout("getRadius() - returning", 5);
48 48 return radius;
49 49 }
50   -void GravObject::setRadius(long double a) {
  50 +void GravObject::setRadius(const long double a) {
51 51 debugout("Masspoint - new Radius",11);
52 52 radius = a;
53 53 }
@@ -60,25 +60,22 @@ void GravObject::setRadius(long double a) {
60 60 //mit � über die Kugelfläche definierten � Radialkoordinaten r1 und r2 ist gr��er
61 61 //als die Differenz dieser Radien).
62 62 //de.wikipedia.org/wiki/Ereignishorizont#Schwarzschild-Radius_und_Gravitationsradius
63   -long double GravObject::getSchwarzschildRadius() {
  63 +long double GravObject::getSchwarzschildRadius() const {
64 64 //r = 2Gm / c^2
65 65 //debugout("getSchwarzschildRadius() - returning", 5);
66 66 return (2.0*GRAVCONST*getSRTMass())/powx(LIGHTSPEED, 2);
67 67 }
68 68
69   -long double GravObject::drange(GravObject* mp2) {
  69 +long double GravObject::drange(const GravObject* mp2) {
70 70 //long double drange = sqrtx(dx^2 + dy^2 + dz^2)
71 71 //MLVector mlvrange = MVMath.SubMV(this.getCoordMLV(), mp2.getCoordMLV());
72 72 return abs((mdv)(mp2->pos - pos));
73 73 }
74 74
75   -long double GravObject::dabsspeed() {
  75 +long double GravObject::getAbsSpeed() const {
76 76 return abs(vel);
77 77 }
78   -long double GravObject::getAbsSpeed() {
79   - return dabsspeed();
80   -}
81   -bool GravObject::setSpeed(mdv& v) {
  78 +bool GravObject::setSpeed(const mdv& v) {
82 79 if(abs(v) > LIGHTSPEED)
83 80 return false;
84 81 else {
@@ -86,7 +83,7 @@ bool GravObject::setSpeed(mdv& v) {
86 83 return true;
87 84 }
88 85 }
89   -bool GravObject::addSpeed(mdv& v) {
  86 +bool GravObject::addSpeed(const mdv& v) {
90 87 if(abs(vel + v) > LIGHTSPEED)
91 88 return false;
92 89 else {
@@ -95,21 +92,21 @@ bool GravObject::addSpeed(mdv& v) {
95 92 }
96 93 }
97 94
98   -long double GravObject::getVolume() {
  95 +long double GravObject::getVolume() const {
99 96 return (4.0/3.0*powx(radius, 3))*PI;
100 97 }
101 98
102   -long double GravObject::getSchwarzschildVolume() {
  99 +long double GravObject::getSchwarzschildVolume() const {
103 100 return (4.0/3.0*powx(getSchwarzschildRadius(), 3))*PI;
104 101 }
105 102
106 103 /** Returns the relativistic impulse for the masspoint */
107   -mdv GravObject::getImpulse() {
  104 +mdv GravObject::getImpulse() const {
108 105 return vel * getSRTMass(); //momentum = gamma*absmass*speed
109 106 }
110 107
111 108 /** Returns the relativistic energy for the masspoint */
112   -long double GravObject::getEnergy() {
  109 +long double GravObject::getEnergy() const {
113 110 long double Energy = mass * LIGHTSPEED * LIGHTSPEED;
114 111 Energy *= Energy;
115 112 Energy += LIGHTSPEED * LIGHTSPEED + (getImpulse() * getImpulse());

0 comments on commit 35c7cd4

Please sign in to comment.
Something went wrong with that request. Please try again.