-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_spring.py
146 lines (127 loc) · 5.13 KB
/
test_spring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""Tests for block-on-spring turboPy app"""
import numpy as np
import pytest
from turbopy import PhysicsModule, Simulation
from spring import BlockOnSpring
@pytest.fixture(name="sim")
def simulation_fixture():
"""Creates a simulation object used to create a B.O.S. object"""
input_data = {
"Tools": {"ForwardEuler": {}},
"Clock": {"start_time": 0, "end_time": 1, "dt": 1},
"PhysicsModules": {},
"Diagnostics": {},
}
simulation = Simulation(input_data)
simulation.prepare_simulation()
return simulation
def test_sim_config(sim):
"""Test tool configuration"""
assert sim.find_tool_by_name("ForwardEuler") is not None
def test_subclass():
"""Test main PhysicsModule class"""
assert issubclass(BlockOnSpring, PhysicsModule)
def test_instance(sim):
"""Test instantiating a BlockOnSpring object"""
min_config = {"pusher": "ForwardEuler"}
block_instance = BlockOnSpring(sim, min_config)
assert isinstance(block_instance, BlockOnSpring)
def test_attributes(sim):
"""Test setting attributes for a BlockOnSpring"""
config = {
"mass": 1,
"spring_constant": 1,
"pusher": "ForwardEuler",
"x0": [[0, 1, 0]],
}
block = BlockOnSpring(sim, config)
# These attributes get set in __init__()
assert block.mass == 1
assert block.spring_constant == 1
np.testing.assert_allclose(block.position, [[0, 0, 0]])
block.initialize()
# These attributes get set in initialize()
np.testing.assert_allclose(block.position, config["x0"])
@pytest.fixture(name="bos_config")
def bos_fixture():
"""Returns a dictionary of input data to create a B.O.S object"""
block_config = {
"Clock": {"start_time": 0,
"end_time": 10,
"num_steps": 100},
"PhysicsModules": {
"BlockOnSpring": {
"mass": 1,
"spring_constant": 1,
"pusher": "Leapfrog",
"x0": [0, 1, 0],
}
},
"Tools": {
"Leapfrog": {},
"ForwardEuler": {},
"BackwardEuler": {}
},
"Diagnostics": {
# default values come first
"directory": "test_output/",
"output_type": "csv",
"clock": {"filename": "time.csv"},
"BlockDiagnostic": [
{'component': 'momentum', 'filename': 'block_p.csv'},
{'component': 'position', 'filename': 'block_x.csv'}
]
}
}
return block_config
def test_bos_forwardeuler(bos_config):
"""Tests block_on_spring app with ForwardEuler ComputeTool and compares to
output files with a "good" output.
"""
bos_config["PhysicsModules"]["BlockOnSpring"]["pusher"] = "ForwardEuler"
bos_config["Diagnostics"]["directory"] = ("test_data/test_output/"
"output_ForwardEuler/")
sim = Simulation(bos_config)
sim.run()
for filename in ['block_p', 'block_x', 'time']:
ref_data = np.genfromtxt('test_data/reference_output/'
f'output_ForwardEuler/{filename}.csv',
delimiter=',')
tmp_data = np.genfromtxt('test_data/test_output/'
f'output_ForwardEuler/{filename}.csv',
delimiter=',')
assert np.allclose(ref_data, tmp_data)
def test_bos_backwardeuler(bos_config):
"""Tests block_on_spring app with BackwardEuler ComputeTool and compares to
output files with a "good" output.
"""
bos_config["PhysicsModules"]["BlockOnSpring"]["pusher"] = "BackwardEuler"
bos_config["Diagnostics"]["directory"] = ("test_data/test_output/"
"output_BackwardEuler/")
sim = Simulation(bos_config)
sim.run()
for filename in ['block_p', 'block_x', 'time']:
ref_data = np.genfromtxt('test_data/reference_output/'
f'output_BackwardEuler/{filename}.csv',
delimiter=',')
tmp_data = np.genfromtxt('test_data/test_output/'
f'output_BackwardEuler/{filename}.csv',
delimiter=',')
assert np.allclose(ref_data, tmp_data)
def test_bos_leapfrog(bos_config):
"""Tests block_on_spring app with LeapFrog ComputeTool and compares to
output files with a "good" output.
"""
bos_config["PhysicsModules"]["BlockOnSpring"]["pusher"] = "Leapfrog"
bos_config["Diagnostics"]["directory"] = ("test_data/test_output/"
"output_Leapfrog/")
sim = Simulation(bos_config)
sim.run()
for filename in ['block_p', 'block_x', 'time']:
ref_data = np.genfromtxt('test_data/reference_output/'
f'output_Leapfrog/{filename}.csv',
delimiter=',')
tmp_data = np.genfromtxt('test_data/test_output/'
f'output_Leapfrog/{filename}.csv',
delimiter=',')
assert np.allclose(ref_data, tmp_data)