Skip to content

Commit

Permalink
Removed merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
eisDNV committed Oct 29, 2024
2 parents a199a6c + 94746a5 commit 7c4db00
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 2 deletions.
8 changes: 8 additions & 0 deletions case_study/assertion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from typing import Any

from sympy import Symbol, sympify

if self.t_bounce < self.time: # calculate first bounce
self.t_bounce, self.p_bounce = self.next_bounce()

class Assertion:
"""Define Assertion objects for checking expectations with respect to simulation results.
Expand All @@ -17,7 +21,11 @@ class Assertion:
Any unknown symbol within the expression is defined as sympy.Symbol and is expected to match a variable.
"""

<<<<<<< HEAD
ns: dict = {}
=======
ns: dict[str, Any] = {}
>>>>>>> 94746a5a6e13c97614b3070264fdb39028eefb95

def __init__(self, expr: str):
self._expr = Assertion.do_sympify(expr)
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ build-backend = "setuptools.build_meta"
"libcosimpy>=0.0.2",
"fmpy>=0.3.21",
"matplotlib>=3.7.1",
"pint>=0.24.3",
"sympy>=1.13.3",
"component-model>=0.0.1"
]
#dynamic = ["version"]

Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ libcosimpy>=0.0.1
FMPy>=0.3.20
matplotlib>=3.7.1
docutils>=0.20.1
sympy>=1.13.3
component-model>=0.0.1
9 changes: 9 additions & 0 deletions tests/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,28 @@ def test_assertion():
assert res, "True at some point"
assert ass.assert_series([("t", _t), ("x", _x)], "interval") == (81, 91)
assert ass.assert_series([("t", _t), ("x", _x)], "count") == 10
<<<<<<< HEAD
with pytest.raises(ValueError, match="Unknown return type 'Hello'") as err:
=======
with pytest.raises(ValueError, match="Unknown return type 'Hello'"):
>>>>>>> 94746a5a6e13c97614b3070264fdb39028eefb95
ass.assert_series([("t", _t), ("x", _x)], "Hello")
# Checking equivalence. '==' does not work
ass = Assertion("(y<=4) & (y>=4)")
assert ass.symbols == {"y": y}
assert Assertion.ns == {"t": t, "x": x, "y": y}
assert ass.assert_single([("y", 4)])
assert not ass.assert_series([("y", _y)], ret="bool")
<<<<<<< HEAD
with pytest.raises(
ValueError, match="'==' cannot be used to check equivalence. Use 'a-b' and check against 0"
) as err:
ass = Assertion("y==4")
print(err)
=======
with pytest.raises(ValueError, match="'==' cannot be used to check equivalence. Use 'a-b' and check against 0"):
ass = Assertion("y==4")
>>>>>>> 94746a5a6e13c97614b3070264fdb39028eefb95
ass = Assertion("y-4")
assert 0 == ass.assert_single([("y", 4)])

Expand Down
57 changes: 57 additions & 0 deletions tests/test_bouncing_ball_3d.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from math import sqrt
from pathlib import Path
from shutil import copy

<<<<<<< HEAD
import pytest
from case_study.case import Case, Cases

# from component_model.tests.examples.bouncing_ball_3d import BouncingBall3D
from component_model.model import Model
from fmpy import plot_result, simulate_fmu
=======
from component_model.model import Model
from fmpy import plot_result, simulate_fmu

>>>>>>> 94746a5a6e13c97614b3070264fdb39028eefb95


def arrays_equal(res: tuple, expected: tuple, eps=1e-7):
Expand All @@ -17,6 +24,7 @@ def arrays_equal(res: tuple, expected: tuple, eps=1e-7):
assert abs(x - y) < eps, f"Element {i} not nearly equal in {x}, {y}"


<<<<<<< HEAD
@pytest.fixture(scope="session")
def ensure_fmu():
return _ensure_fmu()
Expand All @@ -34,6 +42,13 @@ def _ensure_fmu():
dest=build_path,
)
return fmu_path
=======
def test_make_fmu(): # chdir):
fmu_path = Model.build(
str(Path(__file__).parent / "data" / "BouncingBall3D" / "bouncing_ball_3d.py"), dest=Path(Path.cwd())
)
copy(fmu_path, Path(__file__).parent / "data" / "BouncingBall3D")
>>>>>>> 94746a5a6e13c97614b3070264fdb39028eefb95


def test_run_fmpy(show):
Expand Down Expand Up @@ -61,6 +76,7 @@ def test_run_fmpy(show):
assert len(result)
if show:
plot_result(result)
<<<<<<< HEAD
# no more testing than that. This is done in component-model tests


Expand Down Expand Up @@ -124,6 +140,37 @@ def check_case(
arrays_equal(
results.res.jspath(f"$['{t_before+dt}'].bb.x"),
[t_bounce * v[0] + v[0] * e * ddt, 0, (v_bounce * e * ddt - 0.5 * g * ddt**2) / hf],
=======
t_bounce = sqrt(2 * 10 * 0.0254 / 9.81)
v_bounce = 9.81 * t_bounce # speed in z-direction
x_bounce = t_bounce / 1.0 # x-position where it bounces in m
# Note: default values are reported at time 0!
nearly_equal(result[0], (0, 0, 0, 10, 1, 0, 0, sqrt(2 * 10 / 9.81), 0, 0)) # time,pos-3, speed-3, p_bounce-3
print(result[1])
"""
arrays_equal(
result(bb),
(
0.01,
0.01,
0,
(10 * 0.0254 - 0.5 * 9.81 * 0.01**2) / 0.0254,
1,
0,
-9.81 * 0.01,
sqrt(2 * 10 * 0.0254 / 9.81),
0,
0,
),
)
"""
t_before = int(sqrt(2 / 9.81) / dt) * dt # just before bounce
print("BEFORE", t_before, result[int(t_before / dt)])
nearly_equal(
result[int(t_before / dt)],
(t_before, 1 * t_before, 0, 1.0 - 0.5 * 9.81 * t_before * t_before, 1, 0, -9.81 * t_before, x_bounce, 0, 0),
eps=0.003,
>>>>>>> 94746a5a6e13c97614b3070264fdb39028eefb95
)
arrays_equal(results.res.jspath(f"$['{t_before+dt}'].bb.v"), [e * v[0], 0, (v_bounce * e - g * ddt)])
arrays_equal(results.res.jspath(f"$['{t_before+dt}'].bb.x_b"), [x_bounce2, 0, 0])
Expand Down Expand Up @@ -163,10 +210,20 @@ def test_run_cases():


if __name__ == "__main__":
<<<<<<< HEAD
retcode = pytest.main(["-rA", "-v", __file__, "--show", "True"])
assert retcode == 0, f"Non-zero return code {retcode}"
# import os
# os.chdir(Path(__file__).parent.absolute() / "test_working_directory")
# fmu = _ensure_fmu()
# test_run_fmpy( show=True)
# test_run_cases()
=======
# retcode = pytest.main(["-rA", "-v", __file__, "--show", "True"])
# assert retcode == 0, f"Non-zero return code {retcode}"
import os

os.chdir(Path(__file__).parent.absolute() / "test_working_directory")
test_make_fmu()
test_run_fmpy(show=True)
>>>>>>> 94746a5a6e13c97614b3070264fdb39028eefb95
5 changes: 5 additions & 0 deletions tests/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ def _file(file: str = "BouncingBall.cases"):


def test_cases_management():
<<<<<<< HEAD
cases = Cases(_file("data/SimpleTable/test.cases"))
=======
cases = Cases(Path.cwd().parent / "data" / "SimpleTable" / "test.cases")
assert cases.results.results == {}
>>>>>>> 94746a5a6e13c97614b3070264fdb39028eefb95
assert cases.case_var_by_ref(0, 1) == (
"x",
(1,),
Expand Down
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ source = case_study
branch = True

[coverage:report]
fail_under = 10.0
show_missing = True
skip_covered = True

Expand All @@ -24,4 +23,4 @@ deps =
pytest>=7.4
pytest-cov>=4.1
commands =
pytest --cov --cov-config tox.ini {posargs}
pytest {posargs}

0 comments on commit 7c4db00

Please sign in to comment.