0

I was wanting to create a physics simulation, in which the initial values would be the initial position vectors of all the bodies, the initial velocity vectors of all the bodies, the masses of all the bodies, the charges of all the bodies, and 0 for the initial time passed.

The distance formula for calculating the distance between two bodies in the simulation should be the same as the distance formula in euclidean space.

The force vector on any body from any other body would depend on the product of the charges of both bodies multiplied by a function of the distance f(distance) between both bodies multiplied by (the position vector of that body minus the position vector of the other body) divided by the distance between both bodies. The total force vector on any body would depend on the sum of all the force vectors on that body from the other bodies. The acceleration vector for any body would depend on the total force vector on that body divided by the mass of that body.

In each time step after the initial time step the new position vector for each body would depend on 1/2 multiplied by the previous acceleration vector multiplied by the previous time step squared plus the previous velocity vector multiplied by the previous time step plus the previous position vector. The new velocity vector would depend on the previous acceleration vector multiplied by the previous time step plus the previous velocity vector. Also the new time passed would depend on the previous time passed plus the previous time step.

The formulas need to automatically adjust to continue matching what I said previously if I change the number of dimensions, add a new body, remove an existing body, change the mass of an existing body, change the charge of an existing body, change the position vector of an existing body, change the velocity vector for an existing body, or change the function of distance in the formula for force that I mentioned earlier.

The simulation should work in more than three dimensions of space. Also for the function of distance f(distance) that I mentioned in paragraph three the simulation should support as many functions of distance as possible including trigonometric functions of the distance such as the sin(distance) and cos(distance) of the distance, hyperbolic trig functions of the distance such as cosh(distance) and sinh(distance) polynomials of the distance, powers of the distance such as e^distance, logarithms of the distance, Bessel functions of the distance, the error function of the distance, and combinations of these functions and if possible other functions.

Ideally it should be possible for the simulation to run through millions of time steps per minute in order for the simulation to be fast while minimizing the accumulated errors in the calculations of the simulation.

How would I code a physics simulation that follows all of my criteria above and what type of program would be best for this type of simulation?

  • 1
    This problem, and variations of it, has been, and continues to be, extensively studied in *computational science*. You could do worse than pointing your favourite search engine towards *n-body simulation*. I think your chance of getting a good answer within the limits posed by SO is vanishingly close to `0`. – High Performance Mark Jul 16 '18 at 11:04
  • see [physics engine - phase order and other general information](https://stackoverflow.com/a/26584358/2521214) – Spektre Jul 17 '18 at 07:30
  • If you intend to create this program yourself, I advise you to start with something *much* simpler, then build up. – Beta Jul 23 '18 at 16:41

1 Answers1

0

Lets address these issues one at a time:

To handle an arbitrary number of dimensions, you'll have to store your position, velocity, and acceleration vectors as arrays, and then map some function over those arrays, instead of explicitly accepting x, y, and z or something.

Store your position / velocity / acceleration / force arrays and charge in some object or structure representing that object, and store all of these in a larger array, which you can then iterate over to calculate the force vectors, and update the force arrays accordingly. Once all forces are accounted for, update the position / velocity / acceleration, and reset the force vector.

In order to meet your accuracy and performance requirements, you'll probably have to either write this simulation a compiled language like C. Additionally, to support all your functions of distance you listed, you'll probably need to use a combination of lookup tables, and arithmetic tricks / approximations to obtain the values quickly enough.

Try to write out all your formulas first, and see what algebraic simplifications you can apply, to save yourself extra computational steps (can you double everything to avoid a division by two, can you square something to avoid a square root, etc). Know that division is far slower than multiplication, and square roots are even more expensive (think distance magnitude).

Dillon Davis
  • 6,679
  • 2
  • 15
  • 37