Skip to content

Commit

Permalink
Fix merge conflicts with master and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorgelmh committed Nov 7, 2024
1 parent d52e5f7 commit cc68a65
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
32 changes: 16 additions & 16 deletions tests/examples/oscillator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np

from component_model.model import Model


Expand All @@ -16,29 +17,28 @@ class HarmonicOscillator(Model):
See also `Wikipedia <https://en.wikipedia.org/wiki/Harmonic_oscillator>`_
"""
def __init__(self, k:float=1.0, c:float=0.0, m:float=1.0):

def __init__(self, k: float = 1.0, c: float = 0.0, m: float = 1.0):
super().__init__("Oscillator", "A simple harmonic oscillator", "Siegfried Eisinger")
self.k = k
self.c = c
self.m = m
self.x = np.array( (0,0,0), float)
self.v = np.array( (0,0,0), float)
self.f = np.array( (0,0,0), float)

self.x = np.array((0, 0, 0), float)
self.v = np.array((0, 0, 0), float)
self.f = np.array((0, 0, 0), float)

def do_step(self, time:float, dt:float):
def do_step(self, time: float, dt: float):
"""Do one simulation step of size dt.
We implement a very simplistic algoritm based on difference calculus.
"""
super().do_step( time, dt) # needed for FMU mechanism
a = (self.f - self.k*self.x - self.c*self.v)/self.m
self.x += self.v* dt #+ a* dt*dt
self.v += a* dt

super().do_step(time, dt) # needed for FMU mechanism
a = (self.f - self.k * self.x - self.c * self.v) / self.m
self.x += self.v * dt # + a* dt*dt
self.v += a * dt

def setup_experiment(self):
super().setup_experiment() # needed for FMU mechanism
super().setup_experiment() # needed for FMU mechanism


class DrivingForce(Model):
Expand All @@ -47,10 +47,10 @@ class DrivingForce(Model):
Args:
func (callable)=lambda t:np.array( (0,0,0), float): A function of t, producing a 3D vector
"""
def __init__(self, func:callable):

def __init__(self, func: callable):
self.func = func
self.out = np.array( (0,0,0), float)
self.out = np.array((0, 0, 0), float)

def do_step(self, time:float, dt:float):
def do_step(self, time: float, dt: float):
self.out = self.func(time)

15 changes: 9 additions & 6 deletions tests/test_bouncing_ball_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def test_bouncing_ball_class(show):
but when it is run from the editor (__main__) it is run from /tests/.
"""
import sys
sys.path.insert(0, str(Path(__file__).parent / 'examples'))

sys.path.insert(0, str(Path(__file__).parent / "examples"))
from bouncing_ball_3d import BouncingBall3D # type: ignore

bb = BouncingBall3D()
Expand Down Expand Up @@ -419,11 +420,13 @@ def test_from_fmu(bouncing_ball_fmu):


if __name__ == "__main__":
#retcode = pytest.main(["-rA", "-v", "--rootdir", "../", "--show", "False", __file__])
#assert retcode == 0, f"Non-zero return code {retcode}"
# retcode = pytest.main(["-rA", "-v", "--rootdir", "../", "--show", "False", __file__])
# assert retcode == 0, f"Non-zero return code {retcode}"
# test_bouncing_ball_class(show=False)
Model.build( str(Path(__file__).parent / "examples" / "bouncing_ball_3d.py"),
dest = (Path(__file__).parent / "test_working_directory"))
Model.build(
str(Path(__file__).parent / "examples" / "bouncing_ball_3d.py"),
dest=(Path(__file__).parent / "test_working_directory"),
)
# test_use_fmu( Path(__file__).parent / "test_working_directory" / "BouncingBall3D.fmu", False)
# test_from_fmu( Path(__file__).parent / "test_working_directory" / "BouncingBall3D.fmu")
test_from_osp( Path(__file__).parent / "test_working_directory" / "BouncingBall3D.fmu")
test_from_osp(Path(__file__).parent / "test_working_directory" / "BouncingBall3D.fmu")
3 changes: 1 addition & 2 deletions tests/test_make_simpletable.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ def _to_et(file: str, sub: str = "modelDescription.xml"):

def test_inputtable_class(interpolate=False):
import sys
sys.path.insert(0, str(Path(__file__).parent / 'examples'))
from input_table import InputTable

sys.path.insert(0, str(Path(__file__).parent / "examples"))

tbl = InputTable(
"TestTable",
Expand Down
34 changes: 17 additions & 17 deletions tests/test_oscillator.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from functools import partial
from math import pi, sin, sqrt
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path


def do_show( time:list, z:list, v:list):
def do_show(time: list, z: list, v: list):
fig, ax = plt.subplots()
ax.plot( time, z, label="z-position")
ax.plot( time, v, label="z-speed")
ax.plot(time, z, label="z-position")
ax.plot(time, v, label="z-speed")
ax.legend()
plt.show()


def force( t:float, ampl:float=1.0, omega:float=0.1):
return np.array( (0,0,ampl* sin(omega*t)), float)
def force(t: float, ampl: float = 1.0, omega: float = 0.1):
return np.array((0, 0, ampl * sin(omega * t)), float)


def test_oscillator_class(show):
Expand All @@ -26,32 +26,32 @@ def test_oscillator_class(show):
but when it is run from the editor (__main__) it is run from /tests/.
"""
import sys
sys.path.insert(0, str(Path(__file__).parent / 'examples'))

sys.path.insert(0, str(Path(__file__).parent / "examples"))
from oscillator import HarmonicOscillator

osc = HarmonicOscillator( k=1.0, c=0.1, m=1.0)
osc = HarmonicOscillator(k=1.0, c=0.1, m=1.0)
osc.x[2] = 1.0
times = []
z = []
v = []
_f = partial( force, ampl=1.0, omega=0.1)
_f = partial(force, ampl=1.0, omega=0.1)
dt = 0.01
time = 0
print("Period", 2*pi/sqrt(osc.k/osc.m))
print("Period", 2 * pi / sqrt(osc.k / osc.m))
for _ in range(10000):
osc.f = _f(time)
osc.do_step( time, dt)
osc.do_step(time, dt)
times.append(time)
z.append( osc.x[2])
v.append( osc.v[2])
z.append(osc.x[2])
v.append(osc.v[2])
time += dt

if show:
do_show( times, z, v)
do_show(times, z, v)


if __name__ == "__main__":
#retcode = pytest.main(["-rA", "-v", "--rootdir", "../", "--show", "False", __file__])
#assert retcode == 0, f"Non-zero return code {retcode}"
# retcode = pytest.main(["-rA", "-v", "--rootdir", "../", "--show", "False", __file__])
# assert retcode == 0, f"Non-zero return code {retcode}"
test_oscillator_class(show=True)

0 comments on commit cc68a65

Please sign in to comment.