-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmpc_initialize.py
More file actions
132 lines (117 loc) · 4.71 KB
/
mpc_initialize.py
File metadata and controls
132 lines (117 loc) · 4.71 KB
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
"""@package mpc_initialize
This module contains functions for the correct initialization of PyMpc.
More details here.
"""
import os
import PyMpc.Utils
import importlib
def _list_subdirs_name(current_dir, to_skip = []):
""" returns a list of all directories in current_dir,
relative to current_dir,
and skipping all subdirectories.
"""
first_level_dirs = []
first_done = False # skip the first one! it is the current_dir itself.
for path, subdirs, files in os.walk(current_dir):
if first_done:
path = os.path.relpath(path, current_dir)
if not path in to_skip:
first_level_dirs.append(path)
del subdirs[:] # only one level deep
else:
first_done = True
return first_level_dirs
def _get_external_solvers_names():
solvers_dir = PyMpc.Utils.get_external_solvers_dir()
skip_directories = ([
'__pycache__',
'.git',
'STKOMonitor',
])
ext_solvers = _list_subdirs_name(solvers_dir, skip_directories)
return ext_solvers
def initialize():
"""Function called by the application to initialize the global python interface.
This function is called once in each application run.
"""
print('--------------------------------------------------------')
print(' Python Interface Initialization ')
print(' (global) ')
print('--------------------------------------------------------')
print('')
print('Available external solvers:')
for x in _get_external_solvers_names():
print(' ',x)
print('')
# Massimo Petracca: 16/03/2021:
# The backend should be set before we import matplotlib.pyplot for the first time
# so we can do it here
import matplotlib
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
def initialize_get_available_solvers():
"""Function called by the application to get a list of available external solvers
@todo send descriptions as well
"""
return _get_external_solvers_names()
def initialize_on_new_document():
"""Function called by the application to initialize the document-wise python interface.
This function is called everytime a new document is created or open.
"""
print('--------------------------------------------------------')
print(' Python Interface Initialization ')
print(' (document-wise) ')
print('--------------------------------------------------------')
print(' Initializing python interface of new document ')
print('--------------------------------------------------------')
print('')
# get the current document
doc = PyMpc.App.caeDocument()
active_solver_name = doc.solverName
if active_solver_name == '':
# no solver for this document.
# just wait for the user to set an external solver
print('No external solver set for this document.')
else:
# we have an external solver.
# initialize it. (a consistency check will be performed)
ext_solvers = _get_external_solvers_names()
if len(ext_solvers) > 0 :
try:
active_ext_solver_module = importlib.import_module(active_solver_name + '.mpc_solver_initialize')
except ModuleNotFoundError:
raise Exception(f'Cannot find the specified external solver "{active_solver_name}" among the available ones: {ext_solvers}')
active_ext_solver_module.initialize()
else:
print('Warning: no external solver available')
def initialize_on_set_external_solver(new_active_solver):
"""Function called by the application to initialize the document-wise
python interface when the active external solver changes.
"""
print('--------------------------------------------------------')
print(' Python Interface Initialization ')
print(' (document-wise) ')
print('--------------------------------------------------------')
print(' Initializing python interface on active solver changed ')
print('--------------------------------------------------------')
print('')
# get the current document
doc = PyMpc.App.caeDocument()
if new_active_solver == '':
# an empty string is meant as null solver!
print('Unloading external solver ...')
doc.unregisterMetaDataAll()
doc.solverName = new_active_solver
else:
# set the new solver.
ext_solvers = _get_external_solvers_names()
if len(ext_solvers) > 0 :
if not new_active_solver in ext_solvers:
raise Exception(f'Cannot find the specified external solver "{new_active_solver}" among the available ones: {ext_solvers}')
active_ext_solver_module = importlib.import_module(new_active_solver + '.mpc_solver_initialize')
active_ext_solver_module.initialize()
# not needed. the active_ext_solver_module.initialize() method should set it!
# but let's do it anyway...
doc.solverName = new_active_solver
else:
print('Warning: no external solver available')