add relativity

This commit is contained in:
rob tillaart 2021-05-30 14:16:54 +02:00
parent 1fd79bf4a2
commit 07b5f9ef94
13 changed files with 695 additions and 0 deletions

View File

@ -0,0 +1,7 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- leonardo
- due
- zero

View File

@ -0,0 +1,13 @@
name: Arduino-lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update
compliance: strict

View File

@ -0,0 +1,13 @@
---
name: Arduino CI
on: [push, pull_request]
jobs:
arduino_ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: Arduino-CI/action@master
# Arduino-CI/action@v0.1.1

View File

@ -0,0 +1,18 @@
name: JSON check
on:
push:
paths:
- '**.json'
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:
pattern: "\\.json$"

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021-2021 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,67 @@
[![Arduino CI](https://github.com/RobTillaart/relativity/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/relativity/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/relativity.svg?maxAge=3600)](https://github.com/RobTillaart/relativity/releases)
# relativity
Arduino library with relativity functions.
## Description
This experimental library implements a number of functions that give indication of the time dilation etc due to relativistic speed.
Goal: Educational purposes or when one puts a cubesat into space :)
## Interface
- **relativity()** constructor
- **double getC()** returns speed of light
- **double getG()** returns gravitational constant
- **double factor(double speed)** returns sqrt(1-v2/c2)
- **double gamma(double speed)** returns 1/sqrt(1-v2/c2)
- **double relativeTime(double time, double speed)** returns the relative time for given time and speed.
- **double relativeLength(double length, double speed)** returns the relative length for given length and speed.
- **double relativeMass(double mass, double speed)** returns the relative mass for given mass and speed.
- **double EnergyMass(double mass, double speed)** returns the energyMass for given mass and speed. Think E = mc^2.
### Caching variants
These functions are the same however the math to calculate a given factor and gamma is done only once and cached. So these function will perform a bit better, especially if floating point is slow.
- **void setSpeed(double speed = 0)** set the speed once and calculate the factor and gamma to minimize math for next functions. Think caching.
- **double getSpeed()** returns speed set.
- **double getFactor()** returns factor for speed set
- **double getGamma()** returns gamma for speed set.
- **double relativeTime(double time)** returns the relative time for speed set with **setSpeed()**.
- **double relativeLength(double length)** returns the relative length for speed set.
- **double relativeMass(double mass)** returns the relative mass for speed set.
- **double EnergyMass(double mass)** returns the energy mass for for speed set.
### Gravity effects
- **double gravitationalTime(double time, double speed)** returns time dilation due to gravitational effects.
- **double diameterEarth(uint8_t longitude = 45)** calculates the diameter of the Earth given it is not a nice circle but more an ellipse, flatter on the poles and thicker on the equator.
Longitude is in (absolute) degrees.
- **double getPlanetMass(uint8_t n)** returns planet mass in kg where 0 = Sun, 1 = Mercury etc
- **double getPlanetRadius(uint8_t n)** returns planet radius in kg where 0 = Sun, 1 = Mercury etc
## Operations
See examples for typical usage.
## Future
- test test test test
- add more functions
- fix some overflow conditions.
- add moons?
- add caching version of mass / radius;

View File

@ -0,0 +1,49 @@
//
// FILE: gamma_table.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: test formulas
// DATE: 2021-05-29
// URL: https://github.com/RobTillaart/relativity
#include "relativity.h"
uint32_t start, stop;
relativity R;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("\nSpeed of light (m/s):\t");
Serial.println(R.getC());
Serial.println("\n Percentage\t Speed\t\t factor\t\t gamma\n");
for (double perc = 1; perc < 99.9999; perc += (100 - perc) / 10)
{
double v = R.getC() * perc * 0.01;
Serial.print(" ");
Serial.print(perc, 5);
Serial.print("\t ");
Serial.print(v * 0.001);
Serial.print("\t ");
Serial.print(R.factor(v), 6);
Serial.print("\t ");
Serial.print(R.gamma(v ), 6);
Serial.println();
}
Serial.println("done...");
}
void loop()
{
}
// -- END OF FILE --

View File

@ -0,0 +1,38 @@
//
// FILE: gravity_demo.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: test formulas
// DATE: 2021-05-29
// URL: https://github.com/RobTillaart/relativity
#include "relativity.h"
relativity R;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("\nSpeed of light (m/s):\t");
Serial.println(R.getC());
for (uint8_t p = 0; p < 4; p++)
{
Serial.print(p);
Serial.print(":\t");
Serial.println(R.gravitationalTime(1, R.getPlanetMass(p), R.getPlanetRadius(p)));
}
Serial.println("\ndone...");
}
void loop()
{
}
// -- END OF FILE --

View File

@ -0,0 +1,51 @@
//
// FILE: relativity_demo.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: test formulas
// DATE: 2021-05-29
// URL: https://github.com/RobTillaart/relativity
#include "relativity.h"
relativity R;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("\nSpeed of light (m/s):\t");
Serial.println(R.getC());
Serial.println("\n Percentage\t Speed\t\t Time\t\t Length\t\t Mass\n");
for (double perc = 1; perc < 99.9999; perc += (100 - perc) / 10)
{
double v = R.getC() * perc * 0.01;
Serial.print(" ");
Serial.print(perc, 5);
Serial.print("\t ");
Serial.print(v * 0.001);
Serial.print("\t ");
Serial.print(R.relativeTime(1, v), 5);
Serial.print("\t ");
Serial.print(R.relativeLength(1, v ), 5);
Serial.print("\t ");
Serial.print(R.relativeMass(1, v), 3);
Serial.print("\t ");
Serial.print(R.EnergyMass(1, v), 2);
Serial.println();
}
Serial.println("done...");
}
void loop()
{
}
// -- END OF FILE --

View File

@ -0,0 +1,22 @@
{
"name": "relativity",
"keywords": "relativity, speed of light, time, length, mass, dilation",
"description": "library with relativity functions.",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/relativity.git"
},
"version": "0.1.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*"
}

View File

@ -0,0 +1,11 @@
name=relativity
version=0.1.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=library with relativity functions.
paragraph=time and length dilitation, mass increase etc
category=Other
url=https://github.com/RobTillaart/relativity
architectures=*
includes=relativity.h
depends=

View File

@ -0,0 +1,195 @@
#pragma once
//
// FILE: relativity.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: Collection relativity formulas
// URL: https://github.com/RobTillaart/relativity
//
// HISTORY:
// 0.1.0 2021-05-29 initial version
#include "Arduino.h"
#define RELATIVITY_LIB_VERSION (F("0.1.0"))
class relativity
{
public:
relativity()
{
double _speed = 0.0;
double _factor = 1.0;
double _gamma = 1.0;
}
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);
}
///////////////////////////////////////////////////////////////////
//
// relativistic corrections for speed
//
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;
}
// set speed only once for the 3 values
void setSpeed(double speed = 0)
{
_speed = speed;
_factor = factor(speed);
_gamma = gamma(speed);
}
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;
}
///////////////////////////////////////////////////////////////////
//
// relativistic corrections for gravity
//
double gravitationalTime(double time, double mass, double radius)
{
// formula tries to stay within float range
return time / sqrt(1 - (mass / (radius * _c2)) * (2 * _G) );
}
// returns radius in km.
double radiusEarth(uint8_t longitude = 45) // 0..90
{
// https://www.youtube.com/watch?v=hYMvJum9_Do @ 8:00
// radius polar: 6357 km
// radius equator: 6378 km
// difference: 21 km
double radians = longitude * (PI / 180.0);
// approx of the graph in youtube with a cosine
return 6367.5 + 10.5 * cos(radians * 2);
}
// mass in
double getPlanetMass(uint8_t n) // sun = 0; mercury = 1 etc
{
return massPlanets[n];
}
// radius in km
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
// wikipedia
// kg
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
};
// km
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
};
// cache
double _speed = 0.0;
double _factor = 1.0;
double _gamma = 1.0;
};
// -- END OF FILE --

View File

@ -0,0 +1,190 @@
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2021-05-29
// PURPOSE: unit tests for the relativity library
// https://github.com/RobTillaart/TSL235R
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
//
// supported assertions
// ----------------------------
// assertEqual(expected, actual); // a == b
// assertNotEqual(unwanted, actual); // a != b
// assertComparativeEquivalent(expected, actual); // abs(a - b) == 0 or (!(a > b) && !(a < b))
// assertComparativeNotEquivalent(unwanted, actual); // abs(a - b) > 0 or ((a > b) || (a < b))
// assertLess(upperBound, actual); // a < b
// assertMore(lowerBound, actual); // a > b
// assertLessOrEqual(upperBound, actual); // a <= b
// assertMoreOrEqual(lowerBound, actual); // a >= b
// assertTrue(actual);
// assertFalse(actual);
// assertNull(actual);
// // special cases for floats
// assertEqualFloat(expected, actual, epsilon); // fabs(a - b) <= epsilon
// assertNotEqualFloat(unwanted, actual, epsilon); // fabs(a - b) >= epsilon
// assertInfinity(actual); // isinf(a)
// assertNotInfinity(actual); // !isinf(a)
// assertNAN(arg); // isnan(a)
// assertNotNAN(arg); // !isnan(a)
#include <ArduinoUnitTests.h>
#include "Arduino.h"
#include "relativity.h"
unittest_setup()
{
}
unittest_teardown()
{
}
unittest(test_constructor)
{
fprintf(stderr, "VERSION: %s\n", RELATIVITY_LIB_VERSION);
relativity R;
// test constants
assertEqualFloat(299792458.0, R.getC(), 1);
assertEqualFloat(6.6742e-11, R.getG(), 1e-15);
fprintf(stderr, "done...\n");
}
unittest(test_alpha_gamma)
{
relativity R;
assertEqualFloat(1.0, R.factor(0), 0.0001);
assertEqualFloat(0.0, R.factor(R.getC()), 0.0001);
assertEqualFloat(1.0, R.gamma(0), 0.0001);
assertInfinity(R.gamma(R.getC()) );
fprintf(stderr, "\n\tperc\t\tspeed\t\tfactor\t\tgamma\n");
for (double perc = 1; perc < 99.9999; perc += (100 - perc) / 10)
{
double v = R.getC() * perc * 0.01;
fprintf(stderr, "\t%.4f\t\t%.4f\t%.6f\t%.6f\n", perc, v * 0.001, R.factor(v), R.gamma(v));
}
fprintf(stderr, "done...\n");
}
unittest(test_relativeTime)
{
relativity R;
assertEqualFloat(1.0, R.relativeTime(1, 0), 0.0001);
assertEqualFloat(0.0, R.relativeTime(1, R.getC()), 0.0001);
fprintf(stderr, "\n\tperc\t\tspeed\t\ttime\n");
for (double perc = 1; perc < 99.9999; perc += (100 - perc) / 10)
{
double v = R.getC() * perc * 0.01;
fprintf(stderr, "\t%.4f\t\t%.4f\t%.6f\t\n", perc, v * 0.001, R.relativeTime(1, v));
}
fprintf(stderr, "done...\n");
}
unittest(test_relativeLength)
{
relativity R;
assertEqualFloat(1.0, R.relativeLength(1, 0), 0.0001);
assertEqualFloat(0.0, R.relativeLength(1, R.getC()), 0.0001);
fprintf(stderr, "\n\tperc\t\tspeed\t\tlength\n");
for (double perc = 1; perc < 99.9999; perc += (100 - perc) / 10)
{
double v = R.getC() * perc * 0.01;
fprintf(stderr, "\t%.4f\t\t%.4f\t%.6f\t\n", perc, v * 0.001, R.relativeLength(1, v));
}
fprintf(stderr, "done...\n");
}
unittest(test_relativeMass)
{
relativity R;
assertEqualFloat(1.0, R.relativeMass(1, 0), 0.0001);
assertInfinity(R.relativeMass(1, R.getC()) );
fprintf(stderr, "\n\tperc\t\tspeed\t\tmass\n");
for (double perc = 1; perc < 99.9999; perc += (100 - perc) / 10)
{
double v = R.getC() * perc * 0.01;
fprintf(stderr, "\t%.4f\t\t%.4f\t%.4f\t\n", perc, v * 0.001, R.relativeMass(1, v));
}
fprintf(stderr, "done...\n");
}
unittest(test_EnergyMass)
{
relativity R;
assertEqualFloat(8.98755e+16, R.EnergyMass(1, 0), 1e11);
assertInfinity(R.EnergyMass(1, R.getC()) );
fprintf(stderr, "\n\tperc\t\tspeed\t\tenergy\n");
for (double perc = 1; perc < 99.9999; perc += (100 - perc) / 10)
{
double v = R.getC() * perc * 0.01;
fprintf(stderr, "\t%.4f\t\t%.4f\t%e\t\n", perc, v * 0.001, R.EnergyMass(1, v));
}
fprintf(stderr, "done...\n");
}
unittest(test_gravitationalTime)
{
relativity R;
assertEqualFloat(6.6742e-11, R.getG(), 1e-15);
fprintf(stderr, "\n\tplanet\tmass\t\tradius\t\t1 - time\n");
for (uint8_t p = 0; p < 10; p++)
{
double m = R.getPlanetMass(p);
double r = R.getPlanetRadius(p);
fprintf(stderr, "\t%d\t%e\t%e\t%e\n", p, m, r, 1 - R.gravitationalTime(1, m, r));
}
fprintf(stderr, "done...\n");
}
unittest(test_radiusEarth)
{
relativity R;
assertEqualFloat(6357, R.radiusEarth(90), 0.001);
assertEqualFloat(6378, R.radiusEarth(00), 0.001);
fprintf(stderr, "\n\tlon\tdiameter\n");
for (uint8_t lon = 0; lon < 91; lon +=3)
{
double dia = R.radiusEarth(lon);
fprintf(stderr, "\t%d\t%0.1f\n", lon, dia);
}
fprintf(stderr, "done...\n");
}
unittest_main()
// --------