forked from BattMoTeam/BattMo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComputationalGraphPlot.m
307 lines (197 loc) · 8.77 KB
/
ComputationalGraphPlot.m
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
classdef ComputationalGraphPlot < handle
properties (SetAccess = private)
computationalGraphTool
nodenames % local copy from computationalGraphTool
A % local copy from computationalGraphTool
end
properties
filters % list of filters
nodeinds % current index
markStaticVariables % default = true
plotOptions
end
methods
function cgp = ComputationalGraphPlot(computationalGraphTool)
cgt = computationalGraphTool;
cgp.computationalGraphTool = cgt;
cgp.A = cgt.adjencyMatrix;
cgp.nodenames = cgt.nodenames;
cgp.nodeinds = (1 : numel(cgp.nodenames))';
cgp.markStaticVariables = true;
cgp.plotOptions = {};
end
function cgp = addFilters(cgp, filters, varargin)
opt = struct('doPlot', true);
[opt, extras] = merge_options(opt, varargin{:});
for ifilter = 1 : numel(filters)
filter = filters{ifilter};
cgp = cgp.addFilter(filter, 'doPlot', false, extras{:});
end
if opt.doPlot
cgp.plot();
end
end
function cgp = addFilter(cgp, filter, varargin)
opt = struct('doPlot', true, ...
'printFilters', true);
opt = merge_options(opt, varargin{:});
cgp.filters{end + 1} = filter;
if opt.doPlot
cgp = cgp.applyFilters('doPlot', opt.doPlot, ...
'printFilters', opt.printFilters);
end
end
function cgp = printFilters(cgp)
filters = cgp.filters;
fprintf('\nFilter list:\n\n')
for ifilter = 1 : numel(filters)
filter = filters{ifilter};
if iscell(filter)
str = sprintf('%s: ''%s''', filter{1}, filter{2});
else
str = filter;
end
fprintf('%d) %s\n', ifilter, str);
end
end
function cgp = reset(cgp, filter, varargin)
opt = struct('doPlot', true);
opt = merge_options(opt, varargin{:});
cgp.nodeinds = (1 : numel(cgp.nodenames))';
if nargin > 1
cgp = cgp.applyFilter(filter, varargin{:});
return
end
if opt.doPlot
cgp.plot();
end
end
function cgp = resetFilters(cgp)
cgp = cgp.reset();
cgp.filters = {};
end
function cgp = applyFilters(cgp, varargin)
opt = struct('doPlot', true, ...
'printFilters', true);
[opt, extras] = merge_options(opt, varargin{:});
cgp = cgp.reset();
filters = cgp.filters;
for ifilter = 1 : numel(filters)
filter = filters{ifilter};
[cgp, filter] = cgp.applyFilter(filter, 'doPlot', false, extras{:});
filters{ifilter} = filter;
end
cgp.filters = filters;
if opt.printFilters
cgp.printFilters();
end
if opt.doPlot
cgp.plot();
end
end
function cgp = removeLast(cgp, varargin)
opt = struct('doPlot', true, ...
'printFilters', true);
opt = merge_options(opt, varargin{:});
cgp.filters = cgp.filters(1 : end - 1);
cgp = cgp.applyFilters('doPlot', opt.doPlot, ...
'printFilters', opt.printFilters);
end
function [cgp, filter] = applyFilter(cgp, filter, varargin)
opt = struct('doPlot', true);
opt = merge_options(opt, varargin{:});
nodenames = cgp.nodenames;
A = cgp.A;
if iscell(filter)
action = filter{1};
regstr = filter{2};
else
action = filter;
end
switch action
case 'select'
inds = regexpSelect(nodenames(cgp.nodeinds), regstr);
cgp.nodeinds = cgp.nodeinds(inds);
case 'remove'
inds = regexpSelect(nodenames(cgp.nodeinds), regstr);
cgp.nodeinds(inds) = [];
case 'addParents'
nodeinds = getDependencyVarNameInds(cgp.nodeinds, A);
cgp.nodeinds = unique(nodeinds);
case 'addChildren'
nodeinds = getDependencyVarNameInds(cgp.nodeinds, A');
cgp.nodeinds = unique(nodeinds);
otherwise
filter = {'select', filter};
cgp = applyFilter(cgp, filter, varargin{:});
end
if opt.doPlot
cgp.plot();
end
end
function help(cgp)
% Print help for interactive use
str = {};
str{end + 1} = '';
str{end + 1} = 'Commands available for ComputationalGraphPlot object given by cgp';
str{end + 1} = '';
str{end + 1} = 'cgp.plot() : Plot graph';
str{end + 1} = 'cgp.addFilter(regstr) : Select the nodes that match the regular expression regstr';
str{end + 1} = ' This action is added in the filter list';
str{end + 1} = 'cgp.removeLast() : Remove last filter in list';
str{end + 1} = 'cgp.resetFilters() : Clear filter list';
str{end + 1} = 'cgp.addFilter(''addParents'') : Add parents of the visible nodes';
str{end + 1} = ' This action is added to the filter list';
str{end + 1} = 'cgp.addFilter(''addChildren'') : Add children of the visible nodes';
str{end + 1} = ' This action is added to the filter list';
str{end + 1} = 'cgp.remove({''remove'', regstr}) : Remove from the visible nodes the nodes that match the regular expression';
str{end + 1} = ' This action is added to the filter list';
str = strjoin(str, newline);
fprintf('%s\n', str);
end
function h = plot(cgp)
nodeinds = cgp.nodeinds;
A = cgp.A(nodeinds, nodeinds);
nodenames = cgp.nodenames(nodeinds);
g = digraph(A, nodenames);
h = plot(g, cgp.plotOptions{:});
if nargout < 1
clear h
end
end
function h = plotModelGraph(cgp, modelname)
cgt = cgp.computationalGraphTool;
if isempty(cgt.modelnames)
cgt = cgt.setupModelGraph();
cgp.computationalGraphTool = cgt;
end
A = cgt.modelAdjencyMatrix;
modelnames = cgt.modelnames;
if nargin > 1
inds = regexpSelect(modelnames, modelname);
A = A(inds, inds);
modelnames = modelnames(inds);
end
g = digraph(A, modelnames);
h = plot(g);
if nargout < 1
clear h
end
end
end
end
%{
Copyright 2021-2024 SINTEF Industry, Sustainable Energy Technology
and SINTEF Digital, Mathematics & Cybernetics.
This file is part of The Battery Modeling Toolbox BattMo
BattMo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BattMo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BattMo. If not, see <http://www.gnu.org/licenses/>.
%}