Skip to content

sirshisram/py_proj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

A class called “vectors” was created, whose objective is to carry out 3D-vector related operations. math package was imported. The initializer definition with arguments x, y, z was created with the x, y and z initialized to 0.

Then, two definitions were created to display/print the vector. “repr” is responsible for displaying the object, while “str” is responsible for printing the object as a string. This was done using f-strings.

getitem” will help in indexing the variables of the vector. For x, y and z, the indices are 0, 1, and 2 respectively. Any other index value will raise an IndexError.

add” and “sub” are responsible for carrying out vector addition and vector subtraction respectively.

In “mul”, vector dot product is carried out if the argument passed is two vectors, and scalar product is carried out if the arguments passed is a vector and an int/float. If it is neither the case, a TypeError is raised. Similarly, “truediv”, division is done when the arguments passed is a vector and an int/float. If it is neither, a TypeError is raised. “mag” and “norm” returns the magnitude of the vector and the normalized vector respectively. With this, the program was saved as “star_sys_vectors.py.”

A new program is created wherein, matplotlib.pyplot, itertools and math packages are imported. The “vectors” class from star_sys_vectors.py is imported. A class called “Sys” is created, which will help in adding, updating the motion, rendering and accounting the interactions between bodies of the system. The initializer is created where size and proj_2d are fed as arguments. The proj_2d refers to the 2D projections (axes, grids, planes, tick-labels) that are rendered as a result of the 3D subplot render. This can be used as a toggle switch so as to visualize with and without this projection. A list called “bodies” will hold the list of bodies present in the system. Using subplots, the set of axes are rendered, and the projection was brought to 3D via “subplot_kw”. This will also introduce the 2D planes for the x-y, y-z and z-x planes. These planes can be enabled or disabled by changing the Boolean value of proj_2d accordingly.

In “add_body”, the bodies that are to be added to the system are appended into the “bodies” list. In “update_all”, the body’s motion is updated by calling “motion” and “render” of the “Sys_Body” class. The sort function is used to sort the bodies via the lambda function, wherein the aforementioned is exploited to visualize the motion of a body behind other bodies.

In “render_all”, the axes, the grids and the tick-labels that are rendered as a result of the subplot was removed (this can be toggled with proj_2d). This was done so as to improvise the visualization. The pause function of pyplot was used in updating the output plot for every 1ms. This helps in visualizing the motion of the planets as and when the plot is updated.

“calc_interxn” is responsible for calculating the interactions between the different rendered bodies in the system. “enumerate()“adds a counter to an iterable and returns in in the form of an enumerating object. This is later used in the nested for loop. index + 1 is chosen so as to avoid to re-account the interaction of two same bodies twice. Inside the second loop, the “gravity_effects” for the first body is called with the second body used as an argument. Another class called “Sys_Body” is created. This class will handle the motion, the rendering and the gravitational effects of the bodies of the Sys class. The initializer is defined, where system, mass, position and velocity are the attributes of the Sys_Body class. Through ‘system’, the main 3D plot is rendered while passing the size and the Boolean value for proj_2d. In display_size, we take the max value of the logarithm of mass, and display log base and minimum display size. This will help in generalizing the size of the body purely based on the mass.

motion redefines the position attribute with the velocity attribute of the body. This is responsible for the motion of the rendered body. In render, the body is rendered via subplots. “markersize” helps the 3D visualization by changing the size of the marker depending on its location on the x-axis. Now, colour is defined as an attribute of the body. The planets that are rendered are markers. “o” is the symbol for a circle render. If the 2D projection is set to true, a second plot is added. “gravity_effects” accounts the gravitational interactions of the bodies in the system. *args helps to pass a variable no. of arguments to the function. Instead of using F = G (m_1 m_2)/(r_12^2 ) , the gravitational constant G is ignored since the parameters used are in arbitrary units, i.e., F = (m_1 m_2)/(r_12^2 ).In the for-loop, calculates the acceleration for each interaction. Reverse (“rev”) is a parameter that ensures the opposite acceleration is applied to the second body since the two bodies when interacting is being pulled towards each other.

A new class “Star_Body” is created with Sys_Body as the parent class. For this project, the mass is set to a large number, with fixed position and the star being at rest and the colour set here is orange.

Similarly, “Planet_Body” is created with Sys_Body as the parent class. For this project, the mass is set to a large number, with fixed position and the star being at rest the colour set here is chosen from the colour_set list via itertools.

With this, the program is saved as “star_sys_main.py” Now, a new program “star_sys_exec.py” is created where the functions from star_sys_main is called. This is like a sandbox where a desired star system can be designed. From star_sys_main, Sys, Star_Body and Planet_Body is imported. The space (system) is rendered by calling Sys. 400 (or any arbitrary value) is the size set here, with proj_2d defaulted to False. This can be toggled True so as to visualize the simulation with the axes and the 2D planes. The star is rendered by passing system through Star_Body. Similarly, the planets are rendered by passing system, mass, position and velocity attributes to Planet_Body. In this case, three planets have been defined. “while True” means that the program loops forever, and in that case, we call calc_interxn, update_all, and render_all. This makes sure that the system is simulated until the user stops it.

About

A Simple Star Simulation using Matplotlib

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages