jhylands

A simulation of Newtonian gravity

Aug 11th, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.74 KB | None | 0 0
  1. 'nan means not a number
  2. Module Module1
  3.     Public Structure vector
  4.         Dim x As Double
  5.         Dim y As Double
  6.         Dim z As Double
  7.     End Structure
  8.     Public Structure objects ' these are all the variables needed for an indavidual object
  9.         Dim position As vector
  10.         Dim velocity As vector
  11.         Dim mass As Double
  12.     End Structure
  13.     Sub Main()
  14.         Const G As Double = -0.0000000000667384 ' newtons gravitational constant
  15.         Dim bitrate As Int64  ' 1/bitrate = the time that acceloration is assumed for
  16. bitrate:
  17.         Try
  18.             Console.WriteLine("Please enter how many fractions of a second you want to asume the acceloration is constant for? 1/this number = the number of seconds the acceloration is constant for...")
  19.             bitrate = Console.ReadLine()
  20.         Catch
  21.             Console.WriteLine("A user error occured, this is most likely becasue you did not enter a integer value!")
  22.             GoTo bitrate
  23.         End Try
  24.  
  25.         Dim NumberOfObjects As Integer ' the number of objects in the simulation
  26. NumberOfObjects:
  27.         Try
  28.             Console.WriteLine("How many objects?") ' getting the person to enter the number of objects
  29.             NumberOfObjects = Console.ReadLine() ' retiving the number of objects
  30.         Catch
  31.             Console.WriteLine("A user error occured, this is most likely because the last entry was not an integer value!")
  32.             GoTo NumberOfObjects
  33.         End Try
  34.         Dim runtime As Int64  ' this is the amount of runs the computer has currently run
  35.         Dim runlength As Int64  'the total number of runs the computer will run
  36.         Dim i As Integer ' a counter used mainly for the current object
  37.         Dim n As Integer ' a counter used for the object acting on the current object
  38.         Dim sumofaceloration As vector ' sum of the acceloration acting in one direction
  39.         Dim objects(NumberOfObjects - 1) As objects ' definning the objects
  40.         'getting the input data
  41.         Console.WriteLine("Please enter the following:")
  42.         For i = 0 To NumberOfObjects - 1
  43. objects:
  44.             Try
  45.                 Console.WriteLine("Object " & i + 1 & "'s x position...")
  46.                 objects(i).position.x = Console.ReadLine()
  47.                 Console.WriteLine("Object " & i + 1 & "'s y position...")
  48.                 objects(i).position.y = Console.ReadLine()
  49.                 Console.WriteLine("Object " & i + 1 & "'s z position...")
  50.                 objects(i).position.z = Console.ReadLine()
  51.                 Console.WriteLine("Object " & i + 1 & "'s x velocity...")
  52.                 objects(i).velocity.x = Console.ReadLine()
  53.                 Console.WriteLine("Object " & i + 1 & "'s y velocity...")
  54.                 objects(i).velocity.y = Console.ReadLine()
  55.                 Console.WriteLine("Object " & i + 1 & "'s z velocity...")
  56.                 objects(i).velocity.z = Console.ReadLine()
  57.                 Console.WriteLine("Object " & i + 1 & "'s mass...")
  58.                 objects(i).mass = Console.ReadLine()
  59.             Catch
  60.                 Console.WriteLine("One or more of the numbers you entered for the last object where incorrect please try again!")
  61.                 GoTo objects
  62.             End Try
  63.  
  64.         Next
  65. constinues:
  66.         Try
  67.             Console.WriteLine("How long do you want the simulation to go on for? The number you type in will be the number of seconds that the simulation is run for, this must be an integer...")
  68.             runlength = Console.ReadLine() * bitrate
  69.             Console.WriteLine("Total loops needed: " & runlength)
  70.         Catch
  71.             Console.WriteLine("The last number you entered contained an error! It should have been an integer value representing the number of seconds you would like to run the simulation for!")
  72.             GoTo bitrate
  73.         End Try
  74.  
  75.         'running the simulation
  76.         For runtime = 1 To runlength
  77.             For i = 0 To NumberOfObjects - 1 ' calculate the movement of objects
  78.  
  79.                 sumofaceloration.x = 0 ' making sure thar the last acceloration isn't put into this one
  80.                 sumofaceloration.y = 0
  81.                 sumofaceloration.z = 0
  82.  
  83.  
  84.                 For n = 0 To NumberOfObjects - 1 ' sumation of aceloration in x, over n except when x = n
  85.  
  86.                     If n <> i Then ' to stop the program working out the force due to itself
  87.                         'working out the acceloration caused by gravity
  88.                         sumofaceloration.x = sumofaceloration.x + (G * objects(n).mass * (objects(i).position.x - objects(n).position.x)) / ((objects(i).position.x - objects(n).position.x) ^ 2 + (objects(i).position.y - objects(n).position.y) ^ 2 + (objects(i).position.z - objects(n).position.z) ^ 2) ^ 1.5
  89.                         sumofaceloration.y = sumofaceloration.y + (G * objects(n).mass * (objects(i).position.y - objects(n).position.y)) / ((objects(i).position.x - objects(n).position.x) ^ 2 + (objects(i).position.y - objects(n).position.y) ^ 2 + (objects(i).position.z - objects(n).position.z) ^ 2) ^ 1.5
  90.                         sumofaceloration.z = sumofaceloration.z + (G * objects(n).mass * (objects(i).position.z - objects(n).position.z)) / (((objects(i).position.x - objects(n).position.x) ^ 2 + (objects(i).position.y - objects(n).position.y) ^ 2 + (objects(i).position.z - objects(n).position.z) ^ 2) * ((objects(i).position.x - objects(n).position.x) ^ 2 + (objects(i).position.x + objects(n).position.x) ^ 2) ^ 0.5)
  91.  
  92.                     End If
  93.  
  94.                 Next
  95.                 ' calcuating the new velocity
  96.                 objects(i).velocity.x = objects(i).velocity.x + sumofaceloration.x / bitrate
  97.                 objects(i).velocity.y = objects(i).velocity.y + sumofaceloration.y / bitrate
  98.                 objects(i).velocity.z = objects(i).velocity.z + sumofaceloration.z / bitrate
  99.  
  100.  
  101.             Next
  102.  
  103.  
  104.             For i = 0 To NumberOfObjects - 1 ' calculating the new position
  105.                 objects(i).position.x = objects(i).position.x + objects(i).velocity.x / bitrate
  106.                 objects(i).position.y = objects(i).position.y + objects(i).velocity.y / bitrate
  107.                 objects(i).position.z = objects(i).position.z + objects(i).velocity.z / bitrate
  108.  
  109.             Next
  110.         Next
  111.  
  112.         'printing the end result
  113.         Console.WriteLine("Results:")
  114.         For i = 0 To NumberOfObjects - 1
  115.             Console.WriteLine(i + 1 & ":")
  116.             Console.WriteLine("x position: " & objects(i).position.x)
  117.             Console.WriteLine("y position: " & objects(i).position.y)
  118.             Console.WriteLine("z position: " & objects(i).position.z)
  119.             Console.WriteLine("x velocity: " & objects(i).velocity.x)
  120.             Console.WriteLine("y velocity: " & objects(i).velocity.y)
  121.             Console.WriteLine("z velocity: " & objects(i).velocity.z)
  122.  
  123.         Next
  124.         GoTo constinues
  125.  
  126.  
  127.     End Sub
  128.  
  129. End Module
Advertisement
Add Comment
Please, Sign In to add comment