Questions tagged [physics]
Physics is a science that studies the interactions of energy and matter. The physics tag should be used for questions about how physics can be simulated or implemented in programming.
151
questions
2
votes
1
answer
78
views
How can I make my force calculations better?
I am making a simple physics solver (haven't gotten to collision resolution or detection yet, just finished making it mt'ed), and when I apply forces through "W", "A", "S"...
0
votes
1
answer
77
views
Temperature of a Lennard-Jones system
Are the formulas used in getTemperature() and setTemperature() correct?
...
1
vote
2
answers
162
views
Minimalistic implementation of Leapfrog integration algorithm (2)
Please review this C++ listing of an implementation of Leapfrog integration.
This C++ listing is rewritten according to this review.
...
6
votes
2
answers
870
views
Molecular simulation: Minimum Image Convention
Can you review the following code to check to see if the Minimum Image Convention is properly implemented?
...
3
votes
1
answer
131
views
Improving execution time of physics, data acquisition triggering system simulation
Background
I'm attempting to write a physics simulation code, one portion of which involves simulating the triggering system of some equipment. The equipment works as follows: environmental noise (...
6
votes
2
answers
482
views
Means square displacement (MSD)
I have written a code to calculate the MSD of some molecules. The code averages over multiple time origins (sliding time window) and over all the molecules. I have also made it do one extra thing: do ...
2
votes
1
answer
161
views
Simulation of a real capacitor to calculate its capacitance
The goal is to simulate a real flat plate capacitor in 2D. The potential of the armature is fixed to +V and -V, on the edges of the simulation cell the potential is zero. Between the plates of the ...
20
votes
3
answers
4k
views
Planetary simulation in python
I have created a program in python that calculates forces between bodies (i.e earth, moon and a hypothetical moon) and make them move according to the changes in velocity and forces. This is the code ...
5
votes
1
answer
231
views
Calculating the energy of the harmonic oscillator using a Monte Carlo method
The problem
The partition function for the quantum harmonic oscillator can be written in the path integral formulation as
$$Z\propto\int Dx(\tau)\exp\left(-\frac{S_E}{\hbar}\right)=\int Dx(\tau)\exp\...
0
votes
1
answer
229
views
Monte Carlo simulation for the harmonic oscillator
Is there any improvement that can be made to the following code, written to simulate the harmonic oscillator in the path integral formulation with Monte Carlo methods?
...
11
votes
3
answers
516
views
Membrane protein diffusion in different confinement models
I am a junior Software Engineer, C++ is usually my main jam but I started picking up Python for a research project I am doing in college. I am eager to learn as much Python syntax, tricks, best ...
3
votes
0
answers
45
views
Plot of massless particles on the unstable null circular geodesic
My friend and I were trying to reproduce Figure 4(left), page 7, from the paper "Probing phase structure of black holes with Lyapunov exponents", Guo 2022. We succeeded in reproducing the ...
2
votes
2
answers
1k
views
Solving the TDoA multilateration problem in 3-dimensions
Background.
I've written an algorithm to solve the Time Difference of Arrival (TDoA) multilateration problem in 3-dimensions. That is, given the known coordinates of ...
3
votes
2
answers
525
views
VBA functions to calculate sun and moon positions
I have the following VBA code (across three modules) to make UDFs to calculate sun & moon position data. The issue I'm facing is that they are very slow to run as I have over 6000 rows (over 10 ...
6
votes
3
answers
312
views
Predict bouncing ball destination in JavaScript
In a Pong-style 2D game, a ball (a circle of radius BALLRADIUS) can bounce (with perfect elasticity) on the top or bottom of the screen. When it hits the left or right side of the screen, one of the ...
1
vote
1
answer
475
views
Solving a 3D heat diffusion PDE
I am trying to solve a heat diffusion type PDE using a finite difference method.
I would like to preface that I have seriously simplified the code. Just so that anyone who tries to help me, doesn't ...
6
votes
2
answers
765
views
C program that finds local acceleration due to gravity (g)
I have written a program in C that calculates local g, by using height from the sea level and latitude.
NOTE: I have to add an image of the equation because that ...
10
votes
1
answer
922
views
High performance physics simulation - core class design
Below is some stripped down code from a physics simulation. The classes Vector2, Line and ...
1
vote
0
answers
85
views
Advice on Update function in my Game Loop [closed]
I have this update fx for my Player object in game loop, game is top-down in-space thing with boom-booms and such. Me being weak at maths is huge understatement so my implementation is concoction of ...
3
votes
1
answer
503
views
Barnes-Hut \$n\$-body simulation (3D) in C++
I have implemented the Barnes-Hut algorithm for \$n\$-body-simulations (in this case "sort-of" 3D-gravity - see below). I'd appreciate any comments for improving my code (especially ...
4
votes
2
answers
432
views
Clebsch-Gordan Coefficients Implementation
In scientific programming I often come into the position of needing to implement a rather obscure and complicated formula. Clebsch-Gordan coefficients are such a thing.
I'm mainly seeking advice on ...
8
votes
4
answers
1k
views
Python: Class to calculate different variables of a projectile motion with angle, velocity (+ initial height) (first OOP program)
I'd like to hear your constructive criticisms about my first OOP project. I explain more what I actually do in the code's comments, and I tried to be quite explanatory with names etc.
...
5
votes
1
answer
67
views
Exploring the space of 8 parameters of a physics problem to then be evaluated in ODE integrator as LSODA millions of times, efficently
there's a function in one of the scripts I've been working on that I really want to improve.
Disclaimer: Being honest, I'm not really very good at programming but I've been learning day to day and it'...
4
votes
1
answer
411
views
Path of a particle in a non-uniform B field
I have completed my project which simulates the path of a particle trapped in a device known as a magnetic mirror. I would like your thoughts and improvements on my implementation.
Here is the code:
<...
4
votes
1
answer
93
views
Phase diagrams of particles in a mirror
I have completed my project which models the paths of particles in a plasma experiencing a force. The program creates phase diagrams of a particle's perpendicular velocity against its parallel ...
4
votes
2
answers
117
views
Simulate transmission spectrum of extreme ultraviolet laser pulse through laser-dressed finite sample (Python Version)
I am currently participating in research on transient absorption spectroscopy and four wave mixing. In the experimental design, an extreme ultraviolet (XUV) laser pulse and infrared (IR) laser pulse ...
3
votes
0
answers
37
views
MatLab: Plotting Output Electric Field Spectrum
Goal
This code combines two matrices, diagonalizes that matrix, and finds the right and left eigenvectors, right and left eigenvectors are normalized with respect to each other, then it is multiplied ...
8
votes
4
answers
2k
views
Simulation of a falling ball
I made a simple simulation of a falling ball. Is it possible to somehow improve or optimize this code? What tips can you give for development?
...
1
vote
1
answer
47
views
Optimize narrow phase contact detection for phyiscs simulation code
I'm trying to optimize the narrow phase of the contact detection in my code. I'm using OpenMP for multithreading.
That is the starting code:
...
13
votes
2
answers
5k
views
A Flappy bird Game
Here is a project that I have been working on for the past few days.
I have used the SFML library in C++ to make a flappy bird game of my own. I made this as a ...
3
votes
0
answers
345
views
Suvat Calculator - Kinematics
Suvat stands for kinematics or
(s)displacement
(u)initial velocity
(v)final velocity
(a)acceleration
(t)time
This calculator can find the remaining 2 variables given 3 other variables!
Enter x on the ...
1
vote
2
answers
188
views
Better way to calculate double-scattering diffraction using cartesian product of arrays?
This script calculates points in reciprocal space for hexagonal 2D lattices, then uses the cartesian product from itertools to add each vector from one lattice to all of the vectors of the other in ...
2
votes
1
answer
882
views
JS + CANVAS Projectile Motion [closed]
Basically you've got a canvas, and you can click it to launch a ball in a certain angle and speed, everything is working fine, except for the arrow tip of the vector thing. I can't get it to draw ...
6
votes
2
answers
339
views
Simulating the orbits of the Earth, Moon and Sun together in Fortran
This has been racking my brain for hours now. I will try to explain as much as I can. I'm aware this code is horrendously ungraceful, but I'm new to Fortran and making things more efficient is not ...
3
votes
1
answer
78
views
Dynamical system animation framework
Introduction
I am developing a dynamical system simulation/animation framework, which
provides three abstract base classes, DynamicalSystem, ...
3
votes
1
answer
216
views
Molecular dynamics simulation
I'm looking for some fresh ideas on how to optimize a couple of functions in my C++ code. I run the code through a profiler and it shows that they take 20% and 40% of the time respectively. The ...
3
votes
2
answers
2k
views
Diffusion Limited Aggregation Simulator
Based on my answer to another code review question I wrote my own python script for DLA (Diffusion Limited Aggregation).
The DLA algorithm task:
Place seed at the center of the canvas.
Release a ...
3
votes
1
answer
1k
views
N-Body Gravitational Simulation of Point-Masses in Python
I made an n-body gravitational simulation in python. The algorithm does produce an approximate solution, which is shown at the bottom of the post. Additional methods to produce animations (among other ...
4
votes
2
answers
493
views
Modelling solar system, Runge-kutta 4 (n-Body problem)
I have a program that simulations all planets, where the forces due to each planet is considered during each time step.I'm looking to see where I could make some improvements.
main.cpp
...
6
votes
2
answers
858
views
Simulating a Solar System Using Command-Line Graphics
After a relative hiatus of around two years, I've decided to start delving back into the programming world; to start, I've appropriated one of my older projects, CLIGL, and have made a solar system ...
33
votes
9
answers
8k
views
Simple n-body class in C++
As part of my training, I implemented a n-body class in C++ to simulate gravitational interaction of bodies and to get more familiar with features that C++ offers such as object oriented programming.
...
3
votes
1
answer
287
views
Optical dispersion calculation from spectrograms with Python
First, I'd like to provide a little explanation on what my code is supposed to do.
It is part of a middle-sized project. I restructured the code to work on its own, and also added little comments to ...
2
votes
1
answer
1k
views
d3.js canvas performance
I've just rewritten this code (a simple gravitational restricted n-body simulation) to display using canvas, rather than SVG. The reason being that I'd like to be able to run it with many thousands of ...
4
votes
1
answer
88
views
Making basic physics object class
I'm creating a 2D game in JS, and I've made the generic class that all objects in the game will be (I've decided they are all circular for now).
An object has ...
3
votes
1
answer
345
views
Simplified three-body simulation
The code below simulates a given Newtonian 3 body system. Each row per slice is supposed to represent a Cartesian component for the initial trajectory vector and for the distance vectors to the other ...
4
votes
2
answers
2k
views
Trapezoidal rule for set of data
Here is the question from the book of Mark Newman-Computational Physics Exc 5.1
a) Read in the data and, using the trapezoidal rule, calculate from them the approximate distance traveled by the ...
4
votes
2
answers
380
views
Simple spatial grid for particle system
I am going to simulate a particle system, the particles are infinitesimal points which apply forces on the neighbors and I need a fast way of making proximity checks, they are going to have a maximum ...
4
votes
1
answer
791
views
Gravity simulation using Numpy and Pygame
This is a simple gravity simulator coded in Python 3.7 using Numpy and Pygame. I was wondering if it can be optimized further. Initially I had coded it using pure Python lists, using nested loops to ...
5
votes
0
answers
85
views
Butter side up?
I've written a piece of code that determines if a piece toast with butter lands on the butter-side or not, depending on its initial velocity and the table height.
The code is written in Mathematica ...
5
votes
2
answers
864
views
Simulation of spring-loaded inverted pendulum
I've finally committed to moving to Python 3 from MATLAB. I'm currently porting my MATLAB code, and I'm sure I'm missing a lot of common best-practices.
For reference, I'm simulating a spring-loaded ...