From 21aa3c8db56b78b2d6656cb85ec869813ffd9733 Mon Sep 17 00:00:00 2001 From: Loran425 Date: Mon, 26 Aug 2019 10:31:10 -0400 Subject: [PATCH 1/3] Enable Multiple Force Calculations --- ParticleSimulator_2D.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ParticleSimulator_2D.py b/ParticleSimulator_2D.py index 96ba3c8..f7f2c91 100644 --- a/ParticleSimulator_2D.py +++ b/ParticleSimulator_2D.py @@ -42,7 +42,7 @@ def __init__(self, particletype, position, acceleration, velocity): particle_type = str(random.choice(possible_particle_type)) pos_x = random.randint(19, (size_of_window[0] - 20)) pos_y = random.randint(19, (size_of_window[1] - 20)) - + particles.append(Particle(particle_type, [pos_x, pos_y], [0, 0], [0, 0])) # Test particles @@ -100,7 +100,7 @@ def __init__(self, particletype, position, acceleration, velocity): a_acceleration = particle_a.acceleration a_velocity = particle_a.velocity a_mass = particle_a.mass - + fx_total = 0 fy_total = 0 From dae7ae6612700980d06df697b4c6c77677384958 Mon Sep 17 00:00:00 2001 From: Loran425 Date: Mon, 26 Aug 2019 10:38:44 -0400 Subject: [PATCH 2/3] Refactor force functions Removes permanent false condition in calculate_gravity Simplifies arguments for gravity and electromagnetic forces Removes forces list returning new force lists Reorders arguments for consistency --- FundamentalForces.py | 50 ++++++++++++++++++++--------------------- ParticleSimulator_2D.py | 4 ++-- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/FundamentalForces.py b/FundamentalForces.py index d99b31f..518548e 100644 --- a/FundamentalForces.py +++ b/FundamentalForces.py @@ -3,44 +3,44 @@ import math as m -def calculate_gravity(forces_list, a_pos, a_mass, b_pos, b_mass): - bypass = False - if bypass is False: - x_diff = b_pos[0] - a_pos[0] - y_diff = b_pos[1] - a_pos[1] - hypotenuse = m.sqrt((x_diff ** 2) + (y_diff ** 2)) - sin = x_diff / hypotenuse - cos = y_diff / hypotenuse - - f = (constant_G * a_mass * b_mass) / (hypotenuse ** 2) - - forces_list[0] += f * sin - forces_list[1] = f * cos +def calculate_gravity(a_pos, a_type, b_pos, b_type): + x_diff = b_pos[0] - a_pos[0] + y_diff = b_pos[1] - a_pos[1] + hypotenuse = m.sqrt((x_diff ** 2) + (y_diff ** 2)) + sin = x_diff / hypotenuse + cos = y_diff / hypotenuse + a_mass = particle_mass[a_type] + b_mass = particle_mass[b_type] + + f = (constant_G * a_mass * b_mass) / (hypotenuse ** 2) - return forces_list - else: - return forces_list + fx = f * sin + fy = f * cos + + return fx, fy -def calculate_electromagnetic(forces_list, a_pos, b_pos, a_mass, b_mass, a_type, b_type): - fx, fy = forces_list +def calculate_electromagnetic(a_pos, a_type, b_pos, b_type): if a_type or b_type == 'n': - return forces_list + return [0, 0] x_diff = b_pos[0] - a_pos[0] y_diff = b_pos[1] - a_pos[1] hypotenuse = m.sqrt((x_diff ** 2) + (y_diff ** 2)) sin = x_diff / hypotenuse cos = y_diff / hypotenuse - + a_mass = particle_mass[a_type] + b_mass = particle_mass[b_type] + f = (constant_coulombs_constant * a_mass * b_mass) / (hypotenuse ** 2) if a_type == b_type: - fx += (f * sin) - fy -= (f * cos) + fx = (f * sin) * -1 + fy = (f * cos) * -1 else: - fx += (f * sin) - fy += (f * cos) - return forces_list + fx = (f * sin) + fy = (f * cos) + + return fx, fy # def calculate_strong(forces_list, ) diff --git a/ParticleSimulator_2D.py b/ParticleSimulator_2D.py index f7f2c91..8af566f 100644 --- a/ParticleSimulator_2D.py +++ b/ParticleSimulator_2D.py @@ -112,9 +112,9 @@ def __init__(self, particletype, position, acceleration, velocity): continue force = [0, 0] # Gravity function - force = calculate_gravity(force, a_position, a_mass, b_position, b_mass) + force = calculate_gravity(a_position, a_type, b_position, b_type) # Electromagnetic Function - force = calculate_electromagnetic(force, a_position, b_position, a_mass, b_mass, a_type, b_type) + force = calculate_electromagnetic(a_position, a_type, b_position, b_type) # Strong Nuclear Force Function From e9836d08205704d19487bcbb53d6524acad2b5cf Mon Sep 17 00:00:00 2001 From: Loran425 Date: Sat, 7 Sep 2019 23:55:32 -0400 Subject: [PATCH 3/3] Bug Fixes Fixes Bug where electromagnetic force was always returning 0,0 Fixes Bug where forces were appending rather than piecewise adding --- FundamentalForces.py | 2 +- ParticleSimulator_2D.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/FundamentalForces.py b/FundamentalForces.py index a78d889..d2ab328 100644 --- a/FundamentalForces.py +++ b/FundamentalForces.py @@ -21,7 +21,7 @@ def calculate_gravity(a_pos, a_type, b_pos, b_type): def calculate_electromagnetic(a_pos, a_type, b_pos, b_type): - if a_type or b_type == 'n': + if a_type == "n" or b_type == 'n': return [0, 0] x_diff = b_pos[0] - a_pos[0] diff --git a/ParticleSimulator_2D.py b/ParticleSimulator_2D.py index 13abbc0..381fa29 100644 --- a/ParticleSimulator_2D.py +++ b/ParticleSimulator_2D.py @@ -109,13 +109,13 @@ def __init__(self, particletype, position, acceleration, velocity): continue force = [0, 0] # Gravity function - force += calculate_gravity(a_position, a_type, b_position, b_type) + g_force = calculate_gravity(a_position, a_type, b_position, b_type) # Electromagnetic Function - force += calculate_electromagnetic(a_position, a_type, b_position, b_type) + e_force = calculate_electromagnetic(a_position, a_type, b_position, b_type) # Strong Nuclear Force Function - fx_total += force[0] - fy_total += force[1] + fx_total += force[0] + g_force[0] + e_force[0] + fy_total += force[1] + g_force[1] + e_force[1] a_acceleration[0] = fx_total / a_mass a_acceleration[1] = fy_total / a_mass