GY-63_MS5611/libraries/relativity/relativity.h

200 lines
3.8 KiB
C
Raw Normal View History

2021-05-30 08:16:54 -04:00
#pragma once
//
// FILE: relativity.h
// AUTHOR: Rob Tillaart
2022-08-04 06:11:24 -04:00
// VERSION: 0.1.3
2021-05-30 08:16:54 -04:00
// PURPOSE: Collection relativity formulas
// URL: https://github.com/RobTillaart/relativity
//
// HISTORY:
// 0.1.0 2021-05-29 initial version
2021-06-07 02:51:29 -04:00
// 0.1.1 2021-06-02 fix in tests
2021-12-27 14:50:10 -05:00
// 0.1.2 2021-12-27 update library.json, readme, license, minor edits
2022-08-04 06:11:24 -04:00
// 0.1.3 2022-08-04 fix constructor
2021-05-30 08:16:54 -04:00
#include "Arduino.h"
2022-08-04 06:11:24 -04:00
#define RELATIVITY_LIB_VERSION (F("0.1.3"))
2021-05-30 08:16:54 -04:00
class relativity
{
public:
relativity()
{
2022-08-04 06:11:24 -04:00
_speed = 0.0;
_factor = 1.0;
_gamma = 1.0;
2021-05-30 08:16:54 -04:00
}
double getC()
{
return _c;
};
double getG()
{
return _G;
};
double factor(double speed)
{
double alpha = speed * _divc;
return sqrt(1 - alpha * alpha);
}
double gamma(double speed)
{
return 1.0 / factor(speed);
}
///////////////////////////////////////////////////////////////////
//
2022-08-04 06:11:24 -04:00
// relativistic corrections for speed
2021-05-30 08:16:54 -04:00
//
double relativeTime(double time, double speed)
{
return time * factor(speed);
}
double relativeLength(double length, double speed)
{
return length * factor(speed);
}
double relativeMass(double mass, double speed)
{
return mass / factor(speed);
}
double EnergyMass(double mass, double speed)
{
return relativeMass(mass, speed) * _c2;
}
2022-08-04 06:11:24 -04:00
// set speed only once for the 3 values
2021-05-30 08:16:54 -04:00
void setSpeed(double speed = 0)
{
2022-08-04 06:11:24 -04:00
_speed = speed;
2021-05-30 08:16:54 -04:00
_factor = factor(speed);
2022-08-04 06:11:24 -04:00
_gamma = gamma(speed);
2021-05-30 08:16:54 -04:00
}
double getSpeed()
{
return _speed;
}
double relativeTime(double time)
{
return time * _factor;
}
double relativeLength(double length)
{
return length * _factor;
}
double relativeMass(double mass)
{
return mass * _gamma;
}
double EnergyMass(double mass)
{
return relativeMass(mass) * _c2;
}
///////////////////////////////////////////////////////////////////
//
2022-08-04 06:11:24 -04:00
// relativistic corrections for gravity
2021-05-30 08:16:54 -04:00
//
double gravitationalTime(double time, double mass, double radius)
{
2022-08-04 06:11:24 -04:00
// formula tries to stay within float range
2021-05-30 08:16:54 -04:00
return time / sqrt(1 - (mass / (radius * _c2)) * (2 * _G) );
}
2022-08-04 06:11:24 -04:00
// returns radius in km.
2021-12-27 14:50:10 -05:00
double radiusEarth(double longitude = 45) // 0..90
2021-05-30 08:16:54 -04:00
{
2022-08-04 06:11:24 -04:00
// https://www.youtube.com/watch?v=hYMvJum9_Do @ 8:00
// radius polar: 6357 km
// radius equator: 6378 km
// difference: 21 km
2021-05-30 08:16:54 -04:00
double radians = longitude * (PI / 180.0);
2022-08-04 06:11:24 -04:00
// approx of the graph in YouTube with a cosine
2021-05-30 08:16:54 -04:00
return 6367.5 + 10.5 * cos(radians * 2);
}
2022-08-04 06:11:24 -04:00
// mass in
2021-05-30 08:16:54 -04:00
double getPlanetMass(uint8_t n) // sun = 0; mercury = 1 etc
{
return massPlanets[n];
}
2022-08-04 06:11:24 -04:00
// radius in km
2021-05-30 08:16:54 -04:00
double getPlanetRadius(uint8_t n) // sun = 0; mercury = 1 etc
{
return radiusPlanets[n];
}
private:
const double _c = 299792458.0; // speed of light
const double _c2 = _c * _c; // sol squared
const double _divc = 1.0/_c; // sol inverse
const double _G = 6.6742e-11; // gravitational constant
2022-08-04 06:11:24 -04:00
// wikipedia
// kg
2021-05-30 08:16:54 -04:00
const double massPlanets[10] =
{
1.9891e30, // Sun
330.11e21, // Mercury
4867.5e21, // Venus
5972.4e21, // Earth
641.71e21, // Mars
1898187e21, // Jupiter
568317e21, // Saturn
102413e21, // Neptune
86813e21, // Uranus
13.03e21, // Pluto
};
2022-08-04 06:11:24 -04:00
// km
2021-05-30 08:16:54 -04:00
const double radiusPlanets[10] =
{
695508, // Sun
2439.4, // Mercury
6052, // Venus
6371, // Earth
3389.5, // Mars
69911, // Jupiter
58232, // Saturn
24622, // Neptune
3981, // Uranus
1188.3, // Pluto
};
2022-08-04 06:11:24 -04:00
// cache
2021-05-30 08:16:54 -04:00
double _speed = 0.0;
double _factor = 1.0;
double _gamma = 1.0;
};
// -- END OF FILE --
2021-12-27 14:50:10 -05:00