Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testovi1 #6

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/cestica.cpython-39.pyc
Binary file not shown.
Binary file added __pycache__/interakcija.cpython-39.pyc
Binary file not shown.
Binary file added __pycache__/simulacija.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
38 changes: 38 additions & 0 deletions cestica.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from interakcija import Interakcija
import math
from sympy import symbols, diff

class Cestica():
def __init__(self, pozicija, brzina, masa, naelektrisanje):
self._pozicija = pozicija
self._brzina = brzina
self._masa = masa
self._naelektrisanje = naelektrisanje

@property
def pozicija(self):
return self._pozicija

@pozicija.setter
def pozicija(self, nova_pozicija):
self._pozicija = nova_pozicija

@property
def brzina(self):
return self._brzina

@brzina.setter
def brzina(self, nova_brzina):
self._brzina = nova_brzina

def __str__(self):
return f"Cestica: pozicija {self._pozicija}, brzina {self._brzina}, masa {self._masa}, naelektrisanje {self._naelektrisanje}"

def azuriranje_pozicije(self, t):
nova_pozicija = Interakcija.Ojlerov_algoritam(self, t)
self._pozicija = nova_pozicija

def azuriranje_brzine(self, sila, t):
ax, ay = Interakcija.ubrzanje(self, sila)
self._brzina = (self._brzina[0] + ax * t, self._brzina[1] + ay * t)

46 changes: 46 additions & 0 deletions interakcija.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import math
from sympy import symbols, diff
k = 9 * 10**9
class Interakcija():
def rastojanje(cestica1, cestica2):
x1, y1 = cestica1._pozicija
x2, y2 = cestica2._pozicija
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)

def Kulonov_zakon(cestica1, cestica2):
r = Interakcija.rastojanje(cestica1, cestica2)
x1, y1 = cestica1._pozicija
x2, y2 = cestica2._pozicija
f = k * abs(cestica1._naelektrisanje * cestica2._naelektrisanje) / (r**3)
fx = f * abs(x2 - x1)
fy = f * abs(y2 - y1)
return (fx, fy)


def sila(cestica1, cestica2):
return Interakcija.Kulonov_zakon(cestica1, cestica2)

def ubrzanje(cestica, sila):
fx, fy = sila
ax = fx / cestica._masa
ay = fy / cestica._masa
return (ax, ay)

def ukupna_sila(sile):
x = 0
y = 0
for fx, fy in sile:
x += fx
y += fy

return (x, y)

def Ojlerov_algoritam(cestica, t):
x, y = cestica._pozicija
vx, vy = cestica._brzina

x += vx * t
y += vy * t

return (x, y)

29 changes: 29 additions & 0 deletions simulacija.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import math
from sympy import symbols, diff
import random
from itertools import combinations
from cestica import Cestica
k = 9 * 10**9
class Simulacija():
def __init__(self, broj_cestica, sirina_okvira, visina_okvira, trajanje):
self.broj_cestica = broj_cestica
self.sirina_okvira = sirina_okvira
self.visina_okvira = visina_okvira
self.trajanje_animacije = trajanje
self.cestice = self.inicijalizacija_cestica()

def inicijalizacija_cestica(self):
cestice = []
for _ in range(self.broj_cestica):
pozicija = [random.uniform(0, self.sirina_okvira), random.uniform(0, self.visina_okvira)]
brzina = [random.uniform(-0.1, 0.1), random.uniform(-0.1, 0.1)]
masa = random.uniform(0.1, 10)
naelektrisanje = 4
cestica = Cestica(pozicija, brzina, masa, naelektrisanje)
cestice.append(cestica)
return cestice

def parovi_cestica(self):
parovi = list(combinations(self.cestice, 2))
return parovi

17 changes: 17 additions & 0 deletions test_cestica.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
from cestica import Cestica

def test_azuriranje_pozicije():
cestica = Cestica((0, 0), (1, 1), 1, 0)
cestica.azuriranje_pozicije(1)
nova_pozicija = cestica._pozicija
assert nova_pozicija == (1, 1)

def test_azuriranje_brzine():
cestica = Cestica((0, 0), (1, 1), 1, 0)
cestica.azuriranje_brzine((2, 2), 1)
nova_brzina = cestica._brzina
assert nova_brzina == (3, 3)

if __name__ == "__main__":
pytest.main()
33 changes: 33 additions & 0 deletions test_interakcija.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import math
from sympy import symbols, diff
from interakcija import Interakcija
from cestica import Cestica
import pytest
k = 9 * 10**9

def test_rastojanje():
cestica1 = Cestica((1, 2), (3, 4), 5, 6)
cestica2 = Cestica((4, 5), (6, 7), 8, 9)
assert Interakcija.rastojanje(cestica1, cestica2) == math.sqrt((1 - 4)**2 + (2 - 5)**2)

def test_Kulonov_zakon():
cestica1 = Cestica((1, 2), (3, 4), 5, 6)
cestica2 = Cestica((4, 5), (6, 7), 8, 9)
assert Interakcija.Kulonov_zakon(cestica1, cestica2) == (k * abs(cestica1._naelektrisanje * cestica2._naelektrisanje) / (Interakcija.rastojanje(cestica1, cestica2)**3) * abs(4 - 1), k * abs(cestica1._naelektrisanje * cestica2._naelektrisanje) / (Interakcija.rastojanje(cestica1, cestica2)**3) * abs(5 - 2))

def test_ubrzanje():
cestica = Cestica((1, 2), (3, 4), 5, 6)
sila = (1, 2)
assert Interakcija.ubrzanje(cestica, sila) == (1 / 5, 2 / 5)

def test_ukupna_sila():
sile = [(1, 2), (3, 4), (5, 6)]
assert Interakcija.ukupna_sila(sile) == (1 + 3 + 5, 2 + 4 + 6)

def test_Ojlerov_algoritam():
cestica = Cestica((1, 2), (3, 4), 5, 6)
t = 2
assert Interakcija.Ojlerov_algoritam(cestica, t) == (1 + 3 * t, 2 + 4 * t)

if __name__ == "__main__":
pytest.main()
17 changes: 17 additions & 0 deletions test_simulacija.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
from sympy import diff
from simulacija import Simulacija

@pytest.fixture
def simulacija():
return Simulacija(4, 10, 10, 100)

def test_inicijalizacija_cestica(simulacija):
assert len(simulacija.cestice) == 4

def test_parovi_cestica(simulacija):
parovi = simulacija.parovi_cestica()
assert len(parovi) == (simulacija.broj_cestica * (simulacija.broj_cestica - 1)) // 2

if __name__ == "__main__":
pytest.main()
Loading