Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

*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
View
@@ -5,12 +5,12 @@
#include "gMathVec.h"
void debugout(std::string strdbg, int dbgprio);
-void debugout(std::string strdbg, int var, int dbgprio);
-void debugout(std::string strdbg, long double var, int dbgprio);
-void debugout(std::string strdbg, mdv& var, int dbgprio);
-void debugout(std::string strdbg, long long var, int dbgprio);
-void debugout(std::string strdbg, mlv& var, int dbgprio);
-void debugout(std::string strdbg, std::string var, int dbgprio);
+void debugout(std::string strdbg, const int var, int dbgprio);
+void debugout(std::string strdbg, const long double var, int dbgprio);
+void debugout(std::string strdbg, const mdv& var, int dbgprio);
+void debugout(std::string strdbg, const long long var, int dbgprio);
+void debugout(std::string strdbg, const mlv& var, int dbgprio);
+void debugout(std::string strdbg, const std::string var, int dbgprio);
//void debugout(std::string strdbg, std::stringstream var, int dbgprio);
void debugout(std::string a);
35 inc/gGravObject.h
View
@@ -14,24 +14,23 @@ class GravObject {
GravObject(int id=-1, long double mass=0., long double radius=0.);
- void setCoord(mlv& x);
- void addCoords(mlv& x);
- long double getAbsMass();
- long double getSRTMass();
- void setMass(long double a);
- void setMass(double a);
- long double getRadius();
- void setRadius(long double a);
- long double getSchwarzschildRadius();
- long double drange(GravObject* mp2);
- long double dabsspeed();
- long double getAbsSpeed();
- bool setSpeed(mdv& v);
- bool addSpeed(mdv& v);
- long double getVolume();
- long double getSchwarzschildVolume();
- mdv getImpulse();
- long double getEnergy();
+ void setCoord(const mlv& x);
+ void addCoords(const mlv& x);
+ long double getAbsMass() const;
+ long double getSRTMass() const;
+ void setMass(const long double a);
+ void setMass(const double a);
+ long double getRadius() const;
+ void setRadius(const long double a);
+ long double getSchwarzschildRadius() const;
+ long double drange(const GravObject* mp2);
+ long double getAbsSpeed() const;
+ bool setSpeed(const mdv& v);
+ bool addSpeed(const mdv& v);
+ long double getVolume() const;
+ long double getSchwarzschildVolume() const;
+ mdv getImpulse() const;
+ long double getEnergy() const;
};
#endif
2  inc/gMathVec.h
View
@@ -95,7 +95,7 @@ template <typename T> mv<T>& operator+=(mv<T>& mv1, const mv<T>& mv2) {
return mv1;
}
-template <typename T> std::ostream& operator<<(std::ostream& stream, mv<T>& mv1) {
+template <typename T> std::ostream& operator<<(std::ostream& stream, const mv<T>& mv1) {
stream << "(" << mv1.x << ", " << mv1.y << ", " << mv1.z << ")";
return stream;
}
83 src/gCalcFunc.cpp
View
@@ -42,8 +42,8 @@ int calc::calcForce(GravObject* mpmain, GravStep* vmpsinsert, mdv& mdvforcetotal
}
//distance between objects
- mlv mlvdist(0);
- mlvdist = mpsec->pos - mpmain->pos;
+ const mlv mlvdist = mpsec->pos - mpmain->pos;
+ const mdv mdvdist = mlvdist;
debugout("calcForce() - mlvdist=", mlvdist, 5);
//halb-relativistische Gravitations-Kraftberechnung
@@ -57,7 +57,7 @@ int calc::calcForce(GravObject* mpmain, GravStep* vmpsinsert, mdv& mdvforcetotal
debugout("calcForce() - dforce2=", dforce, 5);
dforce *= GRAVCONST;
debugout("calcForce() - dforce3=", dforce, 5);
- dforce /= (mdv)mlvdist * (mdv)mlvdist;
+ dforce /= mdvdist * mdvdist;
debugout("calcForce() - dforce4=", dforce, 5);
if (isnan(dforce)) {
@@ -73,8 +73,8 @@ int calc::calcForce(GravObject* mpmain, GravStep* vmpsinsert, mdv& mdvforcetotal
//mdvrquot calculated with MDVector because 'radius' is r / r^3
mdv mdvrquot(0);
//Converted from long (mm) to double (meter)
- debugout("calcForce() - abs(mlvdist)=", abs((mdv)mlvdist), 5);
- mdvrquot = (mdv)mlvdist / abs((mdv)mlvdist);
+ debugout("calcForce() - abs(mdvdist)=", abs(mdvdist), 5);
+ mdvrquot = mdvdist / abs(mdvdist);
debugout(" calcForce() - mdvrquot: ", mdvrquot, 5);
//mdvrquot = MVMath.DivMVNum(MVMath.ConvertToD(mlvdist), MVMath.ConvertToD(mlvdist).abs()); // vec r / |r|
//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) {
#ifdef _OPENMP
- omp_set_num_threads(omp_get_num_procs() * omp_get_num_procs() / 2 + 1);
+ int numProcs = omp_get_num_procs();
+ omp_set_num_threads( numProcs * numProcs / 2 + 1);
#endif
bool bkillloop = false;
@@ -172,37 +173,33 @@ GravStep* calc::calcAcc(GravStep* vmpsinsert, GravStep* vmpsout) {
//debugout(" |MVforcetotal|,x1,x2,x3: "+mvforce.abs()+","+mvforce.x1+","+mvforce.x2+","+mvforce.x3);
//relativistic acceleration formula
- //Math.sqrtx( Math.powx(LIGHTSPEED,2.0) - Math.powx(mpmain.getSpeed(),2.0) / Math.powx(LIGHTSPEED,2.0));
- //TODO reform!
- long double da1 = gamma(mpold->getAbsSpeed());
- da1 *= powx(LIGHTSPEED, 2) * mvforce.x - mpold->vel.x * (mvforce * mpold->vel);
- da1 /= powx(LIGHTSPEED, 2) * mpold->getAbsMass();
-
- long double da2 = gamma(mpold->getAbsSpeed());
- da2 *= powx(LIGHTSPEED, 2) * mvforce.y - mpold->vel.y * (mvforce * mpold->vel);
- da2 /= powx(LIGHTSPEED, 2) * mpold->getAbsMass();
-
- long double da3 = gamma(mpold->getAbsSpeed());
- da3 *= powx(LIGHTSPEED, 2) * mvforce.z - mpold->vel.z * (mvforce * mpold->vel);
- da3 /= powx(LIGHTSPEED, 2) * mpold->getAbsMass();
- //debugout("calcAcc() - Acceleration (a1,a2,a3): "+da1+","+da2+","+da3);
+ const long double dLightspeed2 = powx(LIGHTSPEED, 2);
+ const long double dSpeedFactor1 = gamma(mpold->getAbsSpeed());
+ const long double dSpeedFactor2 = mvforce * mpold->vel;
+ const long double dMassFactor = dLightspeed2 * mpold->getAbsMass();
+
+ long double da1 = dSpeedFactor1;
+ da1 *= dLightspeed2 * mvforce.x - mpold->vel.x * dSpeedFactor2;
+ da1 /= dMassFactor;
- debugout(" calcAcc() - da1:", da1, 5);
- debugout(" calcAcc() - da2:", da2, 5);
- debugout(" calcAcc() - da3:", da3, 5);
+ long double da2 = dSpeedFactor1;
+ da2 *= dLightspeed2 * mvforce.y - mpold->vel.y * dSpeedFactor2;
+ da2 /= dMassFactor;
- mdv mva(da1, da2, da3);
- mdv deltav(0);
+ long double da3 = dSpeedFactor1;
+ da3 *= dLightspeed2 * mvforce.z - mpold->vel.z * dSpeedFactor2;
+ da3 /= dMassFactor;
- deltav = mva * dtime_step;
+ const mdv mva(da1, da2, da3);
+ debugout(" calcAcc() - da = ", mva, 5);
+ const mdv deltav = mva * dtime_step;
debugout(" calcAcc() - deltav: ", deltav, 5);
- debugout(" calcAcc() - abs(deltav)+mpold->getAbsSpeed()=", abs(deltav)+mpold->getAbsSpeed(), 5);
- //debugout("calcAcc() - Delta-v (dv1,dv2,dv3): "+deltav.x1+","+deltav.x2+","+deltav.x3);
if (abs(deltav)+mpold->getAbsSpeed() > LIGHTSPEED) {
- debugout("calcAcc() - Changing dtime_step", 15);
#ifdef DEBUG
+ debugout("calcAcc() - Changing dtime_step", 15);
+ debugout("calcAcc() - abs(deltav)+mpold->getAbsSpeed()=", abs(deltav)+mpold->getAbsSpeed(), 5);
int count = 0;
#endif
while ((LIGHTSPEED - mpold->getAbsSpeed()) / abs(mva) < dtime_step) {
@@ -234,21 +231,13 @@ GravStep* calc::calcAcc(GravStep* vmpsinsert, GravStep* vmpsout) {
cerrors |= cerror::lightspeed;;
}
- //new function which produces new coords
- mlv mlvds(0);
-
- //calculates the delta-s (ds = v * dtime_step)
- //converts mvspeed mdvector to mlvector
- //FIX requ?
- mlvds = (mlv)(mpnew->vel * dtime_step);
- //mlvds = MVMath.ProMVNum(MVMath.ConvertToL(mpold.getMDVSpeed()),dtime_step);
- //debugout("calcAcc() - |mdv-v|,v1,v2,v3: "+mpold.getMDVSpeed().abs()+","+mpold.getMDVSpeed().x1+","+mpold.getMDVSpeed().x2+","+mpold.getMDVSpeed().x3);
- //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);
- //debugout("calcAcc() - |delta-s|,ds1,ds2,ds3: "+mlvds.abs()+","+mlvds.x1+","+mlvds.x2+","+mlvds.x3);
-
- //new position = ds + old position
- long long limit = 0;
- ++limit;
+ /*
+ * new function which produces the new coordinates
+ * calculates the delta-s (ds = v * dtime_step)
+ * and converts mvspeed mdvector to mlvector
+ */
+ const mlv mlvds = mpnew->vel * dtime_step;
+
//TODO FIX really requ
/* if(mpold->posx < 0)
limit = LLONG_MIN - mpold->posx;
@@ -283,10 +272,10 @@ GravStep* calc::calcAcc(GravStep* vmpsinsert, GravStep* vmpsout) {
return vmpsout;
}
*/
+
+ /* New position = ds + old position */
mpnew->addCoords(mlvds);
debugout("calcAcc() - mlvds=",mlvds,5);
- //mp.mlvpos = MVMath.AddMV(mlvds, mp.mlvpos);
- //debugout("calcAcc() - ID "+mpnew.id+": New Coords(x1,x2,x3): "+mpnew.mlvpos.x1+" , "+mpnew.mlvpos.x2+" , "+mpnew.mlvpos.x3);
debugout("calcAcc() - new (x,y,z)=",mpnew->pos,5);
}
if(bkillloop)
@@ -494,7 +483,7 @@ bool calc::checkSpeedBorder(GravStep* pgs_test, long double dpercentage) {
//diese for-schleife dient nur daf�r, dass die berechnung exakt wird, falls ein objekt �ber dpercentage% c kommt
std::vector<GravObject*>::iterator i;
for (i = pgs_test->objects.begin(); i != pgs_test->objects.end(); ++i) {
- if ((*i)->dabsspeed() >= (dpercentage*LIGHTSPEED)) {
+ if ((*i)->getAbsSpeed() >= (dpercentage*LIGHTSPEED)) {
//debugout("checkSpeedBorder() - Object Nr."+i+"/"+mp.id+" has a speed larger "+dpercentage*100.0+"% lightspeed",10);
return true;
}
12 src/gDebugFunc.cpp
View
@@ -17,7 +17,7 @@ void debugout(std::string strdbg, int dbgprio) {
#endif
}
-void debugout(std::string strdbg, int var, int dbgprio) {
+void debugout(std::string strdbg, const int var, int dbgprio) {
#ifdef DEBUG
if (dbgprio > 100 || dbgprio < 0) {
dbgprio = 101;
@@ -29,7 +29,7 @@ void debugout(std::string strdbg, int var, int dbgprio) {
}
#endif
}
-void debugout(std::string strdbg, long double var, int dbgprio) {
+void debugout(std::string strdbg, const long double var, int dbgprio) {
#ifdef DEBUG
if (dbgprio > 100 || dbgprio < 0) {
dbgprio = 101;
@@ -41,7 +41,7 @@ void debugout(std::string strdbg, long double var, int dbgprio) {
}
#endif
}
-void debugout(std::string strdbg, mdv& var, int dbgprio) {
+void debugout(std::string strdbg, const mdv& var, int dbgprio) {
#ifdef DEBUG
if (dbgprio > 100 || dbgprio < 0) {
dbgprio = 101;
@@ -53,7 +53,7 @@ void debugout(std::string strdbg, mdv& var, int dbgprio) {
}
#endif
}
-void debugout(std::string strdbg, long long var, int dbgprio) {
+void debugout(std::string strdbg, const long long var, int dbgprio) {
#ifdef DEBUG
if (dbgprio > 100 || dbgprio < 0) {
dbgprio = 101;
@@ -65,7 +65,7 @@ void debugout(std::string strdbg, long long var, int dbgprio) {
}
#endif
}
-void debugout(std::string strdbg, mlv& var, int dbgprio) {
+void debugout(std::string strdbg, const mlv& var, int dbgprio) {
#ifdef DEBUG
if (dbgprio > 100 || dbgprio < 0) {
dbgprio = 101;
@@ -78,7 +78,7 @@ void debugout(std::string strdbg, mlv& var, int dbgprio) {
#endif
}
-void debugout(std::string strdbg, std::string var, int dbgprio) {
+void debugout(std::string strdbg, const std::string var, int dbgprio) {
#ifdef DEBUG
if (dbgprio > 100 || dbgprio < 0) {
dbgprio = 101;
39 src/gGravObject.cpp
View
@@ -19,35 +19,35 @@ GravObject::GravObject(int i, long double m, long double r): id(i), mass(m), rad
pos = mlv();
}
-void GravObject::setCoord(mlv& x) {
+void GravObject::setCoord(const mlv& x) {
pos = x;
}
-void GravObject::addCoords(mlv& x) {
+void GravObject::addCoords(const mlv& x) {
pos += x;
}
-long double GravObject::getAbsMass() {
+long double GravObject::getAbsMass() const {
return mass;
}
-long double GravObject::getSRTMass() {
+long double GravObject::getSRTMass() const {
//falls der v-vektor (0|0|0) ist, ist auch v=0
if(vel == 0)
return mass;
- return (mass * gamma(dabsspeed()) );
+ return (mass * gamma(getAbsSpeed()) );
}
-void GravObject::setMass(long double a) {
+void GravObject::setMass(const long double a) {
mass = a;
}
-void GravObject::setMass(double a) {
+void GravObject::setMass(const double a) {
mass = (long double)a;
}
-long double GravObject::getRadius() {
+long double GravObject::getRadius() const {
//debugout("getRadius() - returning", 5);
return radius;
}
-void GravObject::setRadius(long double a) {
+void GravObject::setRadius(const long double a) {
debugout("Masspoint - new Radius",11);
radius = a;
}
@@ -60,25 +60,22 @@ void GravObject::setRadius(long double a) {
//mit � über die Kugelfläche definierten � Radialkoordinaten r1 und r2 ist gr��er
//als die Differenz dieser Radien).
//de.wikipedia.org/wiki/Ereignishorizont#Schwarzschild-Radius_und_Gravitationsradius
-long double GravObject::getSchwarzschildRadius() {
+long double GravObject::getSchwarzschildRadius() const {
//r = 2Gm / c^2
//debugout("getSchwarzschildRadius() - returning", 5);
return (2.0*GRAVCONST*getSRTMass())/powx(LIGHTSPEED, 2);
}
-long double GravObject::drange(GravObject* mp2) {
+long double GravObject::drange(const GravObject* mp2) {
//long double drange = sqrtx(dx^2 + dy^2 + dz^2)
//MLVector mlvrange = MVMath.SubMV(this.getCoordMLV(), mp2.getCoordMLV());
return abs((mdv)(mp2->pos - pos));
}
-long double GravObject::dabsspeed() {
+long double GravObject::getAbsSpeed() const {
return abs(vel);
}
-long double GravObject::getAbsSpeed() {
- return dabsspeed();
-}
-bool GravObject::setSpeed(mdv& v) {
+bool GravObject::setSpeed(const mdv& v) {
if(abs(v) > LIGHTSPEED)
return false;
else {
@@ -86,7 +83,7 @@ bool GravObject::setSpeed(mdv& v) {
return true;
}
}
-bool GravObject::addSpeed(mdv& v) {
+bool GravObject::addSpeed(const mdv& v) {
if(abs(vel + v) > LIGHTSPEED)
return false;
else {
@@ -95,21 +92,21 @@ bool GravObject::addSpeed(mdv& v) {
}
}
-long double GravObject::getVolume() {
+long double GravObject::getVolume() const {
return (4.0/3.0*powx(radius, 3))*PI;
}
-long double GravObject::getSchwarzschildVolume() {
+long double GravObject::getSchwarzschildVolume() const {
return (4.0/3.0*powx(getSchwarzschildRadius(), 3))*PI;
}
/** Returns the relativistic impulse for the masspoint */
-mdv GravObject::getImpulse() {
+mdv GravObject::getImpulse() const {
return vel * getSRTMass(); //momentum = gamma*absmass*speed
}
/** Returns the relativistic energy for the masspoint */
-long double GravObject::getEnergy() {
+long double GravObject::getEnergy() const {
long double Energy = mass * LIGHTSPEED * LIGHTSPEED;
Energy *= Energy;
Energy += LIGHTSPEED * LIGHTSPEED + (getImpulse() * getImpulse());
Please sign in to comment.
Something went wrong with that request. Please try again.