Gravity - Zero Gravity

/************************************************************************************************

* Program name : Zero Gravity *

* Program ver. : 2.0 *

* Created by : SharpDevelop *

* Code author : Perić Željko *

* Code language : C# *

* Date created : 05.02.2012 *

* Time created : 20:44 *

* *

* *

* Program Description : *

* *

* Simple console application for calculating body altitude , *

* distance between planet and body , where the influence *

* of the gravitational field of planet is insignificant , *

* gravity force between planet and body is less or equal 0,001 N. *

* *

* Math formula : F = M * m * G / ( R + r + h )^2 *

* *

* F - gravity force N *

* M - mass of planet Kg *

* m - mas of body Kg *

* G - gravity constant N * m^2 / Kg^2 *

* R - planet radius m *

* r - body radius m *

* h - distance m *

* *

* Program accepts scientific format for large and small numbers. *

* *

* 4,3 e+5 = 4,3 * (10 ^ 5) = 4 300 000 e+5 stands for 10 raised to +5th power *

* *

* 587 e-5 = 587 * (10 ^-5) = 0,00587 e-5 stands for 10 raised to -5th power *

* *

* To enter such number , just type in 4,3e+5 or 587e-5 *

* *

* Number range that this program accept is from 1,7 e-308 up to 1,7 e+308 *

* *

* It is expected that entered values for calculation have adequate planet/body ratio , *

* other wise program looses logical sense , but still it shall give correct result. *

* *

* Example : Make calculation for planet Earth and simple ball *

* *

* *

* planet Earth : M = 5,98 e+024 Kg *

* R = 6378 e+003 m *

* *

* simple ball : m = 2,50 e-001 Kg *

* r = 3,00 e-001 m *

* *

* Result should be : h = 3,09401194708158 e+005 Km or 309 401,194708158 Km *

* *

* This result presumes ideal condition , that there are only these two bodies *

* in universe with no other forces of any kind that can influence them. *

* *

* Program Accuracy Notice : *

* *

* All calculations in program are executed using double type of variable. *

* By default , a double value contains 15 decimal digits of precision , *

* although a maximum of 17 digits is maintained internally. *

* Result is shown in scientific format , rounded at fourteen decimals. *

* This could cause fatal error because , in some cases for accurate result , *

* many more digits are significant , especially when very large or very small *

* numbers are involved. *

* *

* Gravity constant value is rounded at two decimal places , *

* and usually input values that user type in , have it's own precision error... *

* *

* So the result that this program gives is always approximate and never 100 % accurate ! *

* *

* *

* All the best, *

* Author. *

************************************************************************************************/

using System;

namespace Zero_gravity

{

class Program

{

public static void Main(string[] args)

{

//

// Description of Main().

//

// Set values to necessary variables for calculation.

//

// Calculate gravity force at zero altitude

// and show result on console screen

// as formatted string representation of number.

//

// Simulation of body flying away from planet :

//

// Calculate strength of gravity force for every body altitude

// incremented by altitude increment value and show result on

// console screen as formatted string representation of number.

//

// Simulation end's when strength of gravity force becomes

// equal or less than 0,001 N.

//

// Calculate distance between body and planet where gravity force is equal 0,001 N

// and show result on console screen as formatted string representation of number.

//

// Calculated altitude result is divided by 1000 m and represents distance in Km.

//

//

// variable declaration

//

// gravity constant , just two decimal places !!!

const double GravityConstant = 6.67e-11;

double PlanetRadius = 0;

double BodyRadius = 0;

double PlanetMass = 0;

double BodyMass = 0;

double BodyAltitude = 0;

double GravityForce = 0;

double AltitudeIncrement = 0;

double helper = 0;

double helper1 = 0;

// scientific (exponential) number format with 14 decimals

// first three decimals shall always be shown on console

// next eleven are optional , if not equal zero

string format1 = "0.000############ e+000";

// scientific (exponential) number format rounded at 6 decimals

string format2 = "0.000000 e+000";

// cursor column position

int Column = 0;

// cursor row position

int Row = 0;

// counter

double i = 0;

//

// set console : title , window & buffer size , fore & back color and clear console

//

Console.Title = " Zero Gravity - by Perić Željko (periczeljkosmederevo@yahoo.com)";

Console.SetWindowSize(80,42);

Console.SetBufferSize(80,42);

Console.ForegroundColor = ConsoleColor.Green;

Console.BackgroundColor = ConsoleColor.Black;

Console.Clear();

//

// declare new variable 'key' and set it's initial value to 'Y' - 'Yes' ,

// used for getting user's answer from keyboard at the end of program

//

ConsoleKeyInfo key = new ConsoleKeyInfo('Y',ConsoleKey.Y,true,true,true);

//

// restart calculation if answer is 'Y' - 'Yes'

//

while(key.Key == ConsoleKey.Y)

{

//

// set variables to initial values

//

PlanetRadius = 0;

BodyRadius = 0;

PlanetMass = 0;

BodyMass = 0;

BodyAltitude = 0;

GravityForce = 0;

AltitudeIncrement = 0;

helper = 0;

helper1 = 0;

Column = 0;

Row = 0;

i = 0;

//

// write program description to user

//

Console.Clear();

Console.Write("********************************************************************************");

Console.Write(" ");

Console.Write(" Program for calculating body altitude ");

Console.Write(" ");

Console.Write(" ( distance between planet and body ) ");

Console.Write(" ");

Console.Write(" where the influence of the gravitational field ");

Console.Write(" ");

Console.Write(" of planet is insignificant, F is less or equal 0,001 N ");

Console.Write(" ");

Console.Write("********************************************************************************");

Console.Write("\n");

//

// get inital values

//

// warning ,

// there is no part for validation

// of relevance of entered number

// that part try to write yourself

//

// To enter very large or very small number

// use scientific format that program recognize.

//

// Mass of Sun M = 1,99 e+30 Kg

//

// type in 1,99e+30 or 1,99e30 or 1,99E+30 or 1,99E30

//

// Gravity constant G = 6,67 e-11 N * m^2 / Kg^2

//

// type in 6,67e-11 or 6,67E-11

//

//

Console.Write(" Enter mass of planet in kilograms M = ");

PlanetMass = double.Parse(Console.ReadLine().ToString());

Console.Write("\n");

Console.Write(" Enter radius of planet in meters R = ");

PlanetRadius= double.Parse(Console.ReadLine().ToString());

Console.Write("\n");

Console.Write(" Enter mass of body in kilograms m = ");

BodyMass = double.Parse(Console.ReadLine().ToString());

Console.Write("\n");

Console.Write(" Enter radius of body in meters r = ");

BodyRadius= double.Parse(Console.ReadLine().ToString());

Console.Write("\n");

Console.Write(" Enter altitude increment in meters h = ");

AltitudeIncrement = double.Parse(Console.ReadLine().ToString());

Console.Write("\n");

//

// calculate gravity force at zero altitude , and write result

//

GravityForce = BodyMass*PlanetMass*GravityConstant;

helper = PlanetRadius + BodyRadius + BodyAltitude;

helper = helper * helper;

GravityForce = GravityForce / helper;

//

// show result as string using format1

//

Console.Write("********************************************************************************");

Console.Write(" ");

Console.Write(" Garvity force at zero altitude F = ");

Console.Write(GravityForce.ToString(format1) + " N ");

Console.Write("\n");

Console.Write(" ");

Console.Write("********************************************************************************");

Console.Write("\n");

//

// get current cursor column and row position

//

Column = Console.CursorLeft;

Row = Console.CursorTop;

Console.Write(" Press any key to continue . . .");

Console.ReadKey(true);

//

// set cursor position

//

Console.SetCursorPosition(Column,Row);

Console.Write(" ");

Console.Write(" ");

Console.Write(" ");

Console.Write(" ");

Console.Write("********************************************************************************");

//

// increase altidude until gravity force becomes less or equal 0,001

//

while (GravityForce > 0.001) // here you can change condition for gravity strength

{

BodyAltitude = BodyAltitude + AltitudeIncrement;

GravityForce = BodyMass*PlanetMass*GravityConstant;

helper = PlanetRadius + BodyRadius + BodyAltitude;

helper = helper * helper;

GravityForce = GravityForce / helper;

i++;

//

// this question increases speed of calculation by

// writing on console every 1 000 000-th result

//

if(i%1000000 == 0)

{

//

// write results without changing position of text

//

Console.SetCursorPosition(Column,Row);

//

// show result as string using format2

//

Console.Write(" Gravity force F = ");

Console.Write(GravityForce.ToString(format2) + " N ");

Console.Write("\n\n");

Console.Write(" At achieved altitude h = ");

Console.Write((BodyAltitude/1000).ToString(format2) + " Km ");

}

}

//

// write final results without changing position of text

//

Console.SetCursorPosition(Column,Row);

//

// show result as string using format1

//

Console.Write(" Gravity force F = ");

Console.Write(GravityForce.ToString(format1) + " N ");

Console.Write("\n\n");

Console.Write(" At achieved altitude h = ");

Console.Write((BodyAltitude/1000).ToString(format1) + " Km ");

Console.Write("\n\n");

//

// calculate zero gravity altitude directly trough math calculation

// for F = 0,001 N altitude h = ?

//

GravityForce = BodyMass*PlanetMass*GravityConstant;

helper1 = GravityForce / 0.001;

helper1 = Math.Sqrt(helper1);

if (helper1.ToString() == (PlanetRadius + BodyRadius).ToString())

{

helper1 = 0;

}

else

{

helper1 = (helper1 - PlanetRadius - BodyRadius) / 1000;

}

//

// show result as string using format1

//

Console.Write("********************************************************************************");

Console.Write(" ");

Console.Write(" Gravity force F = 0,001 N ");

Console.Write(" ");

Console.Write(" At calculated altitude h = " + helper1.ToString(format1) + " Km \n");

Console.Write(" ");

Console.Write("********************************************************************************");

Console.Write("\n");

//

// end of program

//

Console.Write(" Another calculation (y/n) ? ");

key = Console.ReadKey(true);

}

}

}

}

/************************************************************************

* Program Licence : *

* *

* Copyright 2012 , Perić Željko *

* (periczeljkosmederevo@yahoo.com) *

* *

* According to it's main purpose , this program is licenced *

* under the therms of 'Free Software' licence agreement. *

* *

* If You do not know what those therms applies to *

* please read explanation at the following link : *

* (http://www.gnu.org/philosophy/free-sw.html.en) *

* *

* Since it is Free Software this program has no warranty of any kind. *

************************************************************************

* Ethical Notice : *

* *

* It is not ethical to change program code signed by it's author *

* and then to redistribute it under the same name , especially *

* if it is incorrect. *

* *

* It is recommended that if you make improvement in program code , *

* to make remarks of it and then to sign it by Your's name , *

* for further redistribution as new major version of program. *

* *

* Author name and references of old program code version should be *

* kept , for tracking history of program development. *

* *

* For any further information please contact code author at his email. *

************************************************************************/

/************************************

* List Of Revisions *

************************************

* Minor revision of version 1.0 *

* Author 21.02.2013 *

* new comments added *

* New version number 1.1 *

************************************

* Major revision of version 1.1 *

* Author 22.02.2013 *

* new comments added *

* console user interface changed *

* number format changed *

* spelling checked *

* program licence added *

* New version number 2.0 *

************************************/