-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPL_YEAST_COB_Batch_fmpy_explore.py
681 lines (572 loc) · 26.4 KB
/
BPL_YEAST_COB_Batch_fmpy_explore.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# Figure - Simulation of batch reactor with yeast
# with functions added to facilitate explorative simulation work
#
# Author: Jan Peter Axelsson
#------------------------------------------------------------------------------------------------------------------
# 2022-11-21 - Constraint-based approach
# 2022-11-24 - FMU-explore 0.9.6a now with more option for simu() when doing evaluation etc - should simplify?
# 2023-02-24 - Consolidate FMU-explore to 0.9.6 and means parCheck and par() udpate and simu() with opts as arg
# 2023-05-26 - Added plotType TimeSeries2 for the Modelica paper
# 2023-05-29 - Update to FMU-explore 0.9.7
# 2023-05-31 - Adjustments for simplifications of the model and included qO2 for logging as well
# 2023-05-31 - Adjusted for FMPy
# 2023-05-31 - Quick fix for OM FMU wtih small negative ethanol conc
# 2023-05-31 - Adjusted to from importlib.meetadata import version
# 2023-09-12 - Updated to FMU-explore 0.9.8 and introduced process diagram
# 2024-03-07 - Update FMU-explore 0.9.9 - now with _0 replaced with _start everywhere - NPC in capital letters'
# 2024-05-14 - Polish the script
# 2024-05-20 - Updated the OpenModelica version to 1.23.0-dev
# 2024-06-01 - Corrected model_get() to handle string values as well - improvement very small and keep ver 1.0.0
# 2024-08-13 - Corrected model_get() to handle calculatedParameters - call it ver 1.0.1
#------------------------------------------------------------------------------------------------------------------
# Setup framework
import sys
import platform
import locale
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as img
import zipfile
from fmpy import simulate_fmu
from fmpy import read_model_description
import fmpy as fmpy
from itertools import cycle
from importlib.metadata import version
# Set the environment - for Linux a JSON-file in the FMU is read
if platform.system() == 'Linux': locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
#------------------------------------------------------------------------------------------------------------------
# Setup application FMU
#------------------------------------------------------------------------------------------------------------------
# Provde the right FMU and load for different platforms in user dialogue:
global fmu_model, model
if platform.system() == 'Windows':
print('Windows - run FMU pre-compiled JModelica 2.14')
flag_vendor = 'JM'
flag_type = 'CS'
fmu_model ='BPL_YEAST_COB_Batch_windows_jm_cs.fmu'
model_description = read_model_description(fmu_model)
elif platform.system() == 'Linux':
flag_vendor = 'OM'
flag_type = 'ME'
if flag_vendor in ['OM','om']:
print('Linux - run FMU pre-comiled OpenModelica')
if flag_type in ['CS','cs']:
fmu_model ='BPL_YEAST_COB.Batch_linux_om_cs.fmu'
model = load_fmu(fmu_model, log_level=0)
if flag_type in ['ME','me']:
fmu_model ='BPL_YEAST_COB.Batch_linux_om_me.fmu'
model_description = read_model_description(fmu_model)
else:
print('There is no FMU for this platform')
# Provide various opts-profiles
if flag_type in ['CS', 'cs']:
opts_std = {'NCP': 500}
opts_fast = {'NCP': 100}
elif flag_type in ['ME', 'me']:
opts_std = {'NCP': 500}
opts_fast = {'NCP': 100}
else:
print('There is no FMU for this platform')
# Provide various MSL and BPL versions
if flag_vendor in ['JM', 'jm']:
constants = [v for v in model_description.modelVariables if v.causality == 'local']
MSL_usage = [x[1] for x in [(constants[k].name, constants[k].start) for k in range(len(constants))] if 'MSL.usage' in x[0]][0]
MSL_version = [x[1] for x in [(constants[k].name, constants[k].start) for k in range(len(constants))] if 'MSL.version' in x[0]][0]
BPL_version = [x[1] for x in [(constants[k].name, constants[k].start) for k in range(len(constants))] if 'BPL.version' in x[0]][0]
elif flag_vendor in ['OM', 'om']:
MSL_usage = '3.2.3 - used components: RealInput, RealOutput'
MSL_version = '3.2.3'
BPL_version = 'Bioprocess Library version 2.2.2 - GUI'
else:
print('There is no FMU for this platform')
# Simulation time
global simulationTime; simulationTime = 12.0
global prevFinalTime; prevFinalTime = 0
# Dictionary of time discrete states
timeDiscreteStates = {}
# Define a minimal compoent list of the model as a starting point for describe('parts')
component_list_minimum = ['bioreactor', 'bioreactor.culture']
# Provide process diagram on disk
fmu_process_diagram ='BPL_GUI_YEAST_COB_Batch_process_diagram_om.png'
#------------------------------------------------------------------------------------------------------------------
# Specific application constructs: stateDict, parDict, diagrams, newplot(), describe()
#------------------------------------------------------------------------------------------------------------------
# Create stateDict that later will be used to store final state and used for initialization in 'cont':
global stateDict; stateDict = {}
stateDict = {variable.derivative.name:None for variable in model_description.modelVariables \
if variable.derivative is not None}
stateDict.update(timeDiscreteStates)
global stateDictInitial; stateDictInitial = {}
for key in stateDict.keys():
if not key[-1] == ']':
if key[-3:] == 'I.y':
stateDictInitial[key] = key[:-10]+'I_start'
elif key[-3:] == 'D.x':
stateDictInitial[key] = key[:-10]+'D_start'
else:
stateDictInitial[key] = key+'_start'
elif key[-3] == '[':
stateDictInitial[key] = key[:-3]+'_start'+key[-3:]
elif key[-4] == '[':
stateDictInitial[key] = key[:-4]+'_start'+key[-4:]
elif key[-5] == '[':
stateDictInitial[key] = key[:-5]+'_start'+key[-5:]
else:
print('The state vector has more than 1000 states')
break
global stateDictInitialLoc; stateDictInitialLoc = {}
for value in stateDictInitial.values():
stateDictInitialLoc[value] = value
# Create dictionaries parDict and parLocation
global parDict; parDict = {}
parDict['V_start'] = 4.5
parDict['VX_start'] = 1.0
parDict['VG_start'] = 10.0
parDict['VE_start'] = 0.0
parDict['mum'] = 0
parDict['qGr'] = 0
parDict['qEr'] = 0
parDict['qO2'] = 0
global parLocation; parLocation = {}
parLocation['V_start'] = 'bioreactor.V_start'
parLocation['VX_start'] = 'bioreactor.m_start[1]'
parLocation['VG_start'] = 'bioreactor.m_start[2]'
parLocation['VE_start'] = 'bioreactor.m_start[3]'
parLocation['mum'] = 'bioreactor.culture.mum'
parLocation['qGr'] = 'bioreactor.culture.qGr'
parLocation['qEr'] = 'bioreactor.culture.qEr'
parLocation['qO2'] = 'bioreactor.culture.qO2'
# Extra only for describe()
global key_variables; key_variables = []
parLocation['mu'] = 'bioreactor.culture.mu'; key_variables.append(parLocation['mu'])
# Parameter value check - especially for hysteresis to avoid runtime error
global parCheck; parCheck = []
parCheck.append("parDict['V_start'] > 0")
parCheck.append("parDict['VX_start'] >= 0")
parCheck.append("parDict['VG_start'] >= 0")
# Create list of diagrams to be plotted by simu()
global diagrams
diagrams = []
# Define standard plots
def newplot(title='Batch cultivation', plotType='TimeSeries'):
""" Standard plot window
title = '' """
# Reset pens
setLines()
# Subplots
global ax1, ax2, ax3, ax11, ax12, ax21, ax22
# Plot diagram
if plotType == 'TimeSeries':
# Transfer of argument to global variable
plt.figure()
ax1 = plt.subplot(3,1,1)
ax2 = plt.subplot(3,1,2)
ax3 = plt.subplot(3,1,3)
ax1.set_title(title)
ax1.grid()
ax1.set_ylabel('G [g/L]')
ax2.grid()
ax2.set_ylabel('E [1/h]')
ax3.grid()
ax3.set_ylabel('X [g/L]')
ax3.set_xlabel('Time [h]')
diagrams.clear()
diagrams.append("ax1.plot(sim_res['time'],sim_res['bioreactor.c[2]'], label='G', color='b', linestyle=linetype)")
diagrams.append("ax2.plot(sim_res['time'],sim_res['bioreactor.c[3]'], label='E', color='b', linestyle=linetype)")
diagrams.append("ax3.plot(sim_res['time'],sim_res['bioreactor.c[1]'], label='X', color='b', linestyle=linetype)")
elif plotType == 'TimeSeries2':
# Transfer of argument to global variable
plt.figure()
ax1 = plt.subplot(3,1,1)
ax2 = plt.subplot(3,1,2)
ax3 = plt.subplot(3,1,3)
ax1.set_title(title)
ax1.grid()
ax1.set_ylabel('X [g/L]')
ax2.grid()
ax2.set_ylabel('mu [1/h]')
ax3.grid()
ax3.set_ylabel('G, E [g/L]')
ax3.set_xlabel('Time [h]')
diagrams.clear()
diagrams.append("ax1.plot(sim_res['time'],sim_res['bioreactor.c[1]'], label='X', color='r', linestyle=linetype)")
diagrams.append("ax2.plot(sim_res['time'],sim_res['bioreactor.culture.mu'], label='mu', color='r', linestyle=linetype)")
diagrams.append("ax3.plot(sim_res['time'],sim_res['bioreactor.c[2]'], label='G', color='b', linestyle=linetype)")
diagrams.append("ax3.plot(sim_res['time'],sim_res['bioreactor.c[3]'], label='E', color='g', linestyle=linetype)")
diagrams.append("ax3.legend(['G','E'])")
elif plotType == 'Extended':
# Transfer of argument to global variable
plt.figure()
ax11 = plt.subplot(2,2,1)
ax12 = plt.subplot(2,2,2)
ax21 = plt.subplot(2,2,3)
ax22 = plt.subplot(2,2,4)
ax11.set_title(title)
ax11.grid()
ax11.set_ylabel('G and E [g/L]')
ax21.grid()
ax21.set_ylabel('X [g/L]')
ax21.set_xlabel('Time [h]')
ax12.grid()
ax12.set_ylabel('mu [1/h]')
ax22.grid()
ax22.set_ylabel('qO2 [mole/(g*h)]')
ax22.set_xlabel('Time [h]')
diagrams.clear()
diagrams.append("ax11.plot(sim_res['time'],sim_res['bioreactor.c[2]'], label='G', color='b', linestyle='-')")
diagrams.append("ax11.plot(sim_res['time'],sim_res['bioreactor.c[3]'], label='E', color='r', linestyle='-')")
diagrams.append("ax21.plot(sim_res['time'],sim_res['bioreactor.c[1]'], label='X', color='b', linestyle='-')")
diagrams.append("ax12.plot(sim_res['time'],sim_res['bioreactor.culture.mu'], label='mu', color='b', linestyle='-')")
diagrams.append("ax22.plot(sim_res['time'],sim_res['bioreactor.culture.qO2'], 'b-')")
elif diagram == 'PhasePlane':
plt.figure()
ax1 = plt.subplot(1,1,1)
ax1.set_title(title)
ax1.grid()
ax1.set_ylabel('G')
ax1.set_xlabel('X')
else:
print("Plot window type not correct")
# Define and extend describe for the current application
def describe(name, decimals=3):
"""Look up description of culture, media, as well as parameters and variables in the model code"""
if name == 'culture':
print('Saccharomyces cerevisae - default parameters for strain H1022')
elif name in ['broth', 'liquidphase', 'media']:
X = model.get('liquidphase.X')[0];
X_description = model.get_variable_description('liquidphase.X');
X_mw = model.get('liquidphase.mw[1]')[0]
G = model.get('liquidphase.G')[0];
G_description = model.get_variable_description('liquidphase.G');
G_mw = model.get('liquidphase.mw[2]')[0]
E = model.get('liquidphase.E')[0];
E_description = model.get_variable_description('liquidphase.E');
E_mw = model.get('liquidphase.mw[3]')[0]
print('Reactor broth substances included in the model')
print()
print(X_description, ' index = ', X, 'molecular weight = ', X_mw, 'Da')
print(G_description, 'index = ', G, 'molecular weight = ', G_mw, 'Da')
print(E_description, 'index = ', E, 'molecular weight = ', E_mw, 'Da')
elif name in ['parts']:
describe_parts(component_list_minimum)
elif name in ['MSL']:
describe_MSL()
else:
describe_general(name, decimals)
#------------------------------------------------------------------------------------------------------------------
# General code
FMU_explore = 'FMU-explore for FMPy version 1.0.1'
#------------------------------------------------------------------------------------------------------------------
# Define function par() for parameter update
def par(parDict=parDict, parCheck=parCheck, parLocation=parLocation, *x, **x_kwarg):
""" Set parameter values if available in the predefined dictionaryt parDict. """
x_kwarg.update(*x)
x_temp = {}
for key in x_kwarg.keys():
if key in parDict.keys():
x_temp.update({key: x_kwarg[key]})
else:
print('Error:', key, '- seems not an accessible parameter - check the spelling')
parDict.update(x_temp)
parErrors = [requirement for requirement in parCheck if not(eval(requirement))]
if not parErrors == []:
print('Error - the following requirements do not hold:')
for index, item in enumerate(parErrors): print(item)
# Define function init() for initial values update
def init(parDict=parDict, *x, **x_kwarg):
""" Set initial values and the name should contain string '_start' to be accepted.
The function can handle general parameter string location names if entered as a dictionary. """
x_kwarg.update(*x)
x_init={}
for key in x_kwarg.keys():
if '_start' in key:
x_init.update({key: x_kwarg[key]})
else:
print('Error:', key, '- seems not an initial value, use par() instead - check the spelling')
parDict.update(x_init)
# Define fuctions similar to pyfmi model.get(), model.get_variable_descirption(), model.get_variable_unit()
def model_get(parLoc, model_description=model_description):
""" Function corresponds to pyfmi model.get() but returns just a value and not a list"""
par_var = model_description.modelVariables
for k in range(len(par_var)):
if par_var[k].name == parLoc:
try:
if (par_var[k].causality in ['local']) & (par_var[k].variability in ['constant']):
value = float(par_var[k].start)
elif par_var[k].causality in ['parameter']:
value = float(par_var[k].start)
elif par_var[k].causality in ['calculatedParameter']:
value = float(sim_res[par_var[k].name][0])
elif par_var[k].name in start_values.keys():
value = start_values[par_var[k].name]
elif par_var[k].variability == 'continuous':
try:
timeSeries = sim_res[par_var[k].name]
value = float(timeSeries[-1])
except (AttributeError, ValueError):
value = None
print('Variable not logged')
else:
value = None
except NameError:
print('Error: Information available after first simulation')
value = None
return value
def model_get_variable_description(parLoc, model_description=model_description):
""" Function corresponds to pyfmi model.get_variable_description() but returns just a value and not a list"""
par_var = model_description.modelVariables
# value = [x[1] for x in [(par_var[k].name, par_var[k].description) for k in range(len(par_var))] if parLoc in x[0]]
value = [x.description for x in par_var if parLoc in x.name]
return value[0]
def model_get_variable_unit(parLoc, model_description=model_description):
""" Function corresponds to pyfmi model.get_variable_unit() but returns just a value and not a list"""
par_var = model_description.modelVariables
# value = [x[1] for x in [(par_var[k].name, par_var[k].unit) for k in range(len(par_var))] if parLoc in x[0]]
value = [x.unit for x in par_var if parLoc in x.name]
return value[0]
# Define function disp() for display of initial values and parameters
def disp(name='', decimals=3, mode='short'):
""" Display intial values and parameters in the model that include "name" and is in parLocation list.
Note, it does not take the value from the dictionary par but from the model. """
def dict_reverser(d):
seen = set()
return {v: k for k, v in d.items() if v not in seen or seen.add(v)}
if mode in ['short']:
k = 0
for Location in [parLocation[k] for k in parDict.keys()]:
if name in Location:
if type(model_get(Location)) != np.bool_:
print(dict_reverser(parLocation)[Location] , ':', np.round(model_get(Location),decimals))
else:
print(dict_reverser(parLocation)[Location] , ':', model_get(Location))
else:
k = k+1
if k == len(parLocation):
for parName in parDict.keys():
if name in parName:
if type(model_get(Location)) != np.bool_:
print(parName,':', np.round(model_get(parLocation[parName]),decimals))
else:
print(parName,':', model_get(parLocation[parName])[0])
if mode in ['long','location']:
k = 0
for Location in [parLocation[k] for k in parDict.keys()]:
if name in Location:
if type(model_get(Location)) != np.bool_:
print(Location,':', dict_reverser(parLocation)[Location] , ':', np.round(model_get(Location),decimals))
else:
k = k+1
if k == len(parLocation):
for parName in parDict.keys():
if name in parName:
if type(model_get(Location)) != np.bool_:
print(parLocation[parName], ':', dict_reverser(parLocation)[Location], ':', parName,':',
np.round(model_get(parLocation[parName]),decimals))
# Line types
def setLines(lines=['-','--',':','-.']):
"""Set list of linetypes used in plots"""
global linecycler
linecycler = cycle(lines)
# Show plots from sim_res, just that
def show(diagrams=diagrams):
"""Show diagrams chosen by newplot()"""
# Plot pen
linetype = next(linecycler)
# Plot diagrams
for command in diagrams: eval(command)
# Define simulation
def simu(simulationTime=simulationTime, mode='Initial', options=opts_std, diagrams=diagrams):
"""Model loaded and given intial values and parameter before, and plot window also setup before."""
# Global variables
global sim_res, prevFinalTime, stateDict, stateDictInitial, stateDictInitialLoc, start_values
# Simulation flag
simulationDone = False
# Internal help function to extract variables to be stored
def extract_variables(diagrams):
output = []
variables = [v for v in model_description.modelVariables if v.causality == 'local']
for j in range(len(diagrams)):
for k in range(len(variables)):
if variables[k].name in diagrams[j]:
output.append(variables[k].name)
return output
# Run simulation
if mode in ['Initial', 'initial', 'init']:
start_values = {parLocation[k]:parDict[k] for k in parDict.keys()}
# Simulate
sim_res = simulate_fmu(
filename = fmu_model,
validate = False,
start_time = 0,
stop_time = simulationTime,
output_interval = simulationTime/options['NCP'],
record_events = True,
start_values = start_values,
fmi_call_logger = None,
output = list(set(extract_variables(diagrams) + list(stateDict.keys()) + key_variables))
)
simulationDone = True
elif mode in ['Continued', 'continued', 'cont']:
if prevFinalTime == 0:
print("Error: Simulation is first done with default mode = init'")
else:
# Update parDictMod and create parLocationMod
parDictRed = parDict.copy()
parLocationRed = parLocation.copy()
for key in parDict.keys():
if parLocation[key] in stateDictInitial.values():
del parDictRed[key]
del parLocationRed[key]
parLocationMod = dict(list(parLocationRed.items()) + list(stateDictInitialLoc.items()))
# Create parDictMod and parLocationMod
parDictMod = dict(list(parDictRed.items()) +
[(stateDictInitial[key], stateDict[key]) for key in stateDict.keys()])
start_values = {parLocationMod[k]:parDictMod[k] for k in parDictMod.keys()}
# Simulate
sim_res = simulate_fmu(
filename = fmu_model,
validate = False,
start_time = prevFinalTime,
stop_time = prevFinalTime + simulationTime,
output_interval = simulationTime/options['NCP'],
record_events = True,
start_values = start_values,
fmi_call_logger = None,
output = list(set(extract_variables(diagrams) + list(stateDict.keys()) + key_variables))
)
simulationDone = True
else:
print("Error: Simulation mode not correct")
if simulationDone:
# Plot diagrams from simulation
linetype = next(linecycler)
for command in diagrams: eval(command)
# Store final state values in stateDict:
for key in stateDict.keys(): stateDict[key] = model_get(key)
# Store time from where simulation will start next time
prevFinalTime = sim_res['time'][-1]
else:
print('Error: No simulation done')
# Describe model parts of the combined system
def describe_parts(component_list=[]):
"""List all parts of the model"""
def model_component(variable_name):
i = 0
name = ''
finished = False
if not variable_name[0] == '_':
while not finished:
name = name + variable_name[i]
if i == len(variable_name)-1:
finished = True
elif variable_name[i+1] in ['.', '(']:
finished = True
else:
i=i+1
if name in ['der', 'temp_1', 'temp_2', 'temp_3', 'temp_4', 'temp_5', 'temp_6', 'temp_7']: name = ''
return name
# variables = list(model.get_model_variables().keys())
variables = [v.name for v in model_description.modelVariables]
for i in range(len(variables)):
component = model_component(variables[i])
if (component not in component_list) \
& (component not in ['','BPL', 'Customer', 'today[1]', 'today[2]', 'today[3]', 'temp_2', 'temp_3']):
component_list.append(component)
print(sorted(component_list, key=str.casefold))
# Describe MSL
def describe_MSL(flag_vendor=flag_vendor):
"""List MSL version and components used"""
print('MSL:', MSL_usage)
# Describe parameters and variables in the Modelica code
def describe_general(name, decimals):
if name == 'time':
description = 'Time'
unit = 'h'
print(description,'[',unit,']')
elif name in parLocation.keys():
description = model_get_variable_description(parLocation[name])
value = model_get(parLocation[name])
try:
unit = model_get_variable_unit(parLocation[name])
except FMUException:
unit =''
if unit =='':
if type(value) != np.bool_:
print(description, ':', np.round(value, decimals))
else:
print(description, ':', value)
else:
print(description, ':', np.round(value, decimals), '[',unit,']')
else:
description = model_get_variable_description(name)
value = model_get(name)
try:
unit = model_get_variable_unit(name)
except FMUException:
unit =''
if unit =='':
if type(value) != np.bool_:
print(description, ':', np.round(value, decimals))
else:
print(description, ':', value)
else:
print(description, ':', np.round(value, decimals), '[',unit,']')
# Plot process diagram
def process_diagram(fmu_model=fmu_model, fmu_process_diagram=fmu_process_diagram):
try:
process_diagram = zipfile.ZipFile(fmu_model, 'r').open('documentation/processDiagram.png')
except KeyError:
print('No processDiagram.png file in the FMU, but try the file on disk.')
process_diagram = fmu_process_diagram
try:
plt.imshow(img.imread(process_diagram))
plt.axis('off')
plt.show()
except FileNotFoundError:
print('And no such file on disk either')
# Describe framework
def BPL_info():
print()
print('Model for bioreactor has been setup. Key commands:')
print(' - par() - change of parameters and initial values')
print(' - init() - change initial values only')
print(' - simu() - simulate and plot')
print(' - newplot() - make a new plot')
print(' - show() - show plot from previous simulation')
print(' - disp() - display parameters and initial values from the last simulation')
print(' - describe() - describe culture, broth, parameters, variables with values/units')
print()
print('Note that both disp() and describe() takes values from the last simulation')
print('and the command process_diagram() brings up the main configuration')
print()
print('Brief information about a command by help(), eg help(simu)')
print('Key system information is listed with the command system_info()')
def system_info():
"""Print system information"""
# FMU_type = model.__class__.__name__
constants = [v for v in model_description.modelVariables if v.causality == 'local']
print()
print('System information')
print(' -OS:', platform.system())
print(' -Python:', platform.python_version())
try:
scipy_ver = scipy.__version__
print(' -Scipy:',scipy_ver)
except NameError:
print(' -Scipy: not installed in the notebook')
print(' -FMPy:', version('fmpy'))
print(' -FMU by:', read_model_description(fmu_model).generationTool)
print(' -FMI:', read_model_description(fmu_model).fmiVersion)
if model_description.modelExchange is None:
print(' -Type: CS')
else:
print(' -Type: ME')
print(' -Name:', read_model_description(fmu_model).modelName)
print(' -Generated:', read_model_description(fmu_model).generationDateAndTime)
print(' -MSL:', MSL_version)
print(' -Description:', BPL_version)
print(' -Interaction:', FMU_explore)
#------------------------------------------------------------------------------------------------------------------
# Startup
#------------------------------------------------------------------------------------------------------------------
BPL_info()