Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e0c40dd

Browse filesBrowse files
authored
Bugfix to exercise in object-oriented programming (empa-scientific-it#71)
1 parent 71b1266 commit e0c40dd
Copy full SHA for e0c40dd

File tree

Expand file treeCollapse file tree

2 files changed

+37
-27
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+37
-27
lines changed

‎object_oriented_programming.ipynb

Copy file name to clipboardExpand all lines: object_oriented_programming.ipynb
+23-15Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@
14041404
"\n",
14051405
"2. To determine the sign of the velocity change, consider the moons' positions. For example, if `G` stands for Ganymede and `C` for Callisto:\n",
14061406
"\n",
1407-
" * If `Gx = 5` (the `x` position of Ganymede) and `Cx = 3`, then Ganymede's `x` velocity changes by `+1` (because `Gx > Cx`), and Callisto's `x` velocity must change by `-1` (because `Cx < Gx`).\n",
1407+
" * If `Gx = 3` (the `x` position of Ganymede) and `Cx = 5`, then Ganymede's `x` velocity changes by `+1` (because `5 > 3`), and Callisto's `x` velocity must change by `-1` (because `3 < 5`).\n",
14081408
" * If the positions on a given axis **are the same**, then the velocity on that axis doesn't change at all.\n",
14091409
" \n",
14101410
"3. Once the gravity has been calculated and the velocity updated, we should also update the position: simply **add the velocity** of each moon to its current position. For example, if Europa's position is `x=1, y=2, z=3` and its velocity `x=-2, y=0, z=3`, then the new position would be `x=-1, y=2, z=6`."
@@ -1434,8 +1434,8 @@
14341434
"After simulating **10 time steps**, the system's configurations is\n",
14351435
"\n",
14361436
"```\n",
1437-
"Ganymede: x= 2, y= 1, z=-3, vx=-3, vy=-2, vz=1\n",
1438-
"Io: x= 1, y=-8, z= 0, vx=-1, vy= 1, vz=3\n",
1437+
"Ganymede: x= 2, y= 1, z=-3, vx=-3, vy=-2, vz= 1\n",
1438+
"Io: x= 1, y=-8, z= 0, vx=-1, vy= 1, vz= 3\n",
14391439
"Europa: x= 3, y=-6, z= 1, vx= 3, vy= 2, vz=-3\n",
14401440
"Callisto: x= 2, y= 0, z= 4, vx= 1, vy=-1, vz=-1\n",
14411441
"```\n",
@@ -1452,17 +1452,6 @@
14521452
"Sum of total energy: `36 + 45 + 80 + 18 = 179`"
14531453
]
14541454
},
1455-
{
1456-
"cell_type": "markdown",
1457-
"id": "024c6e45-4204-4930-9e1c-47a7a472236c",
1458-
"metadata": {},
1459-
"source": [
1460-
"<div class=\"alert alert-block alert-warning\">\n",
1461-
"<h4><b>Question</b></h4>\n",
1462-
" What is the <b>total energy of the system</b> after simulating the moons for <b>1000 time steps</b>?\n",
1463-
"</div>"
1464-
]
1465-
},
14661455
{
14671456
"cell_type": "markdown",
14681457
"id": "ccb911c1-625a-45e9-b21d-7057bcc2672e",
@@ -1485,7 +1474,7 @@
14851474
"source": [
14861475
"class Universe:\n",
14871476
" \"\"\"A class for a universe\"\"\"\n",
1488-
" # Write here your implementation here of the Universe class \n"
1477+
" # Write here your implementation here of the Universe class "
14891478
]
14901479
},
14911480
{
@@ -1504,6 +1493,17 @@
15041493
"</div>"
15051494
]
15061495
},
1496+
{
1497+
"cell_type": "markdown",
1498+
"id": "17a227ba-6c7a-4df1-b635-1c6f95ef8bc6",
1499+
"metadata": {},
1500+
"source": [
1501+
"<div class=\"alert alert-block alert-warning\">\n",
1502+
"<h4><b>Question</b></h4>\n",
1503+
" What is the <b>average</b> of the total energy of the system after simulating the universe for <b>1000 time steps</b>?\n",
1504+
"</div>"
1505+
]
1506+
},
15071507
{
15081508
"cell_type": "code",
15091509
"execution_count": null,
@@ -1519,6 +1519,14 @@
15191519
" # Write your solution here\n",
15201520
" pass"
15211521
]
1522+
},
1523+
{
1524+
"cell_type": "code",
1525+
"execution_count": null,
1526+
"id": "21e23134-7fc3-42ce-8024-9f9583abfc9f",
1527+
"metadata": {},
1528+
"outputs": [],
1529+
"source": []
15221530
}
15231531
],
15241532
"metadata": {

‎tutorial/tests/test_object_oriented_programming.py

Copy file name to clipboardExpand all lines: tutorial/tests/test_object_oriented_programming.py
+14-12Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import List
44

55
import pytest
6+
from numpy import average
67

78

89
#
@@ -61,7 +62,7 @@ def read_data(name: str, data_dir: str = "data") -> pathlib.Path:
6162
"""Read input data"""
6263
current_module = sys.modules[__name__]
6364
return (
64-
pathlib.Path(current_module.__file__).parent / f"{data_dir}/{name}"
65+
pathlib.Path(current_module.__file__).parent / f"{data_dir}/{name}"
6566
).resolve()
6667

6768

@@ -120,15 +121,15 @@ def update_velocities(self, moon: "Moon") -> None:
120121
"""Update the velocity of the moon"""
121122
for n, position in enumerate(self.positions):
122123
if position > moon.positions[n]:
123-
delta = 1
124-
elif position < moon.positions[n]:
125124
delta = -1
125+
elif position < moon.positions[n]:
126+
delta = 1
126127
else:
127128
delta = 0
128129

129130
if delta:
130-
self.velocities[n] -= delta
131-
moon.velocities[n] += delta
131+
self.velocities[n] += delta
132+
moon.velocities[n] -= delta
132133

133134
def update_positions(self) -> None:
134135
"""Update the position of the moon"""
@@ -165,18 +166,20 @@ def test_moons(moons: str, function_to_test):
165166
class Universe:
166167
"""A class for a universe"""
167168

168-
def __init__(self, moons: str) -> None:
169-
self.moons = [Moon(moon) for moon in moons.splitlines()]
169+
def __init__(self, universe_start: str) -> None:
170+
self.moons = [Moon(moon) for moon in universe_start.splitlines()]
170171

171-
def evolve(self) -> None:
172+
def evolve(self) -> "Universe":
172173
"""Evolve the universe"""
173174
for n, moon_i in enumerate(self.moons[:-1]):
174-
for moon_j in self.moons[n + 1 :]:
175+
for moon_j in self.moons[n + 1:]:
175176
moon_i.update_velocities(moon_j)
176177

177178
for moon in self.moons:
178179
moon.update_positions()
179180

181+
return self
182+
180183
@property
181184
def energy(self) -> int:
182185
"""Return the total energy of the universe"""
@@ -194,6 +197,5 @@ def __repr__(self) -> str:
194197
@pytest.mark.parametrize("universe_start", universes)
195198
def test_n_body(universe_start: str, function_to_test) -> None:
196199
universe = Universe(universe_start)
197-
for _ in range(1000):
198-
universe.evolve()
199-
assert function_to_test(universe) == universe.energy
200+
energy = [universe.evolve().energy for _ in range(1000)]
201+
assert function_to_test(universe_start) == pytest.approx(average(energy))

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.