/
basictests.py
executable file
·163 lines (113 loc) · 4.84 KB
/
basictests.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from math import floor
import opencor as oc
def print_values(data):
data_len = len(data)
if data_len > 6:
print('[ %f, %f, %f, ..., %f, %f, %f ]' %
(data[0], data[1], data[2], data[data_len - 3], data[data_len - 2],
data[data_len - 1]))
else:
print('[ ', end='')
print(*data, sep=", ", end='')
print(' ]')
def values(data, type, indent=''):
if data:
print('%s - %s:' % (indent, type))
for item in data.values():
print('%s - %s = ' % (indent, item.uri()), end='')
try:
print_values(item.values())
except:
print(item.value())
else:
print('%s - %s : n/a' % (indent, type))
def run_simulation(simulation, step):
# Run the simulation
print(' - Run simulation [%d]' % step)
print(' - Settings:')
data = simulation.data()
print(' - Starting point: %f' % data.starting_point())
print(' - Ending point: %f' % data.ending_point())
print(' - Point interval: %f' % data.point_interval())
print(' - ODE solver: %s' % data.ode_solver_name())
print(' - Initial values:')
values(data.constants(), 'Constants', ' ')
values(data.states(), 'States', ' ')
values(data.rates(), 'Rates', ' ')
values(data.algebraic(), 'Algebraic', ' ')
simulation.run()
# Retrieve the number of data points and output some or all of the
# simulation values
results = simulation.results()
states = results.states()
print(' - Result values:')
print(' - Number of points: %d' % len(states['main/x'].values()))
values(results.constants(), 'Constants')
values(states, 'States')
values(results.rates(), 'Rates')
values(results.algebraic(), 'Algebraic')
def test_simulation(title, file_name_or_url, first=True):
if not first:
print()
# Header
print('---------------------------------------')
print(' ' * floor((39 - len(title)) / 2) + title)
print('---------------------------------------')
# Open the simulation
print(' - Open simulation')
if file_name_or_url.startswith('https://'):
simulation = oc.open_simulation(file_name_or_url)
else:
simulation = oc.open_simulation('models/tests/' + file_name_or_url)
# Check whether the simulation is valid and has issues, and list some
# information about it
print(' - Check simulation:')
print(' - Valid: %s' % ('yes' if simulation.valid() else 'no'))
issues = simulation.issues()
if issues:
print(' - Issues:\n - %s' % '\n - '.join(issues))
else:
print(' - Issues: n/a')
# Run #1: run the simulation using the default settings, except if we are
# dealing with a CellML file, in which case we set a few initial
# settings
if file_name_or_url.endswith('.cellml'):
data = simulation.data()
data.set_ending_point(50.0)
data.set_point_interval(0.001)
print(' - Initial settings:')
print(' - Ending point: %f' % data.ending_point())
print(' - Point interval: %f' % data.point_interval())
run_simulation(simulation, 1)
# Run #2: change a few settings and rerun the simulation
simulation.reset()
simulation.clear_results()
data = simulation.data()
data.set_starting_point(10.0)
data.set_ending_point(30.0)
data.set_point_interval(0.1)
data.set_ode_solver('Euler (forward)')
data.set_ode_solver_property('Step', 0.01)
print(' - New settings:')
print(' - Starting point: %f' % data.starting_point())
print(' - Ending point: %f' % data.ending_point())
print(' - Point interval: %f' % data.point_interval())
print(' - ODE solver: %s' % data.ode_solver_name())
run_simulation(simulation, 2)
# Run #3: carry on from the previous run
run_simulation(simulation, 3)
# Close the simulation
oc.close_simulation(simulation)
if __name__ == '__main__':
# Test for a local/remote CellML file
test_simulation('Local CellML file', 'cellml/lorenz.cellml')
test_simulation('Remote CellML file',
'https://raw.githubusercontent.com/opencor/opencor/master/models/tests/cellml/lorenz.cellml', False)
# Test for a local/remote SED-ML file
test_simulation('Local SED-ML file', 'sedml/lorenz.sedml', False)
test_simulation('Remote SED-ML file',
'https://raw.githubusercontent.com/opencor/opencor/master/models/tests/sedml/lorenz.sedml', False)
# Test for a local/remote COMBINE archive
test_simulation('Local COMBINE archive', 'combine/lorenz.omex', False)
test_simulation('Remote COMBINE archive',
'https://raw.githubusercontent.com/opencor/opencor/master/models/tests/combine/lorenz.omex', False)