-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
processed_variable.py
674 lines (606 loc) · 25.2 KB
/
processed_variable.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
#
# Processed Variable class
#
import casadi
import numbers
import numpy as np
import pybamm
import scipy.interpolate as interp
from scipy.integrate import cumulative_trapezoid
class ProcessedVariable(object):
"""
An object that can be evaluated at arbitrary (scalars or vectors) t and x, and
returns the (interpolated) value of the base variable at that t and x.
Parameters
----------
base_variables : list of :class:`pybamm.Symbol`
A list of base variables with a method `evaluate(t,y)`, each entry of which
returns the value of that variable for that particular sub-solution.
A Solution can be comprised of sub-solutions which are the solutions of
different models.
Note that this can be any kind of node in the expression tree, not
just a :class:`pybamm.Variable`.
When evaluated, returns an array of size (m,n)
base_variable_casadis : list of :class:`casadi.Function`
A list of casadi functions. When evaluated, returns the same thing as
`base_Variable.evaluate` (but more efficiently).
solution : :class:`pybamm.Solution`
The solution object to be used to create the processed variables
warn : bool, optional
Whether to raise warnings when trying to evaluate time and length scales.
Default is True.
"""
def __init__(
self,
base_variables,
base_variables_casadi,
solution,
warn=True,
cumtrapz_ic=None,
):
self.base_variables = base_variables
self.base_variables_casadi = base_variables_casadi
self.all_ts = solution.all_ts
self.all_ys = solution.all_ys
self.all_inputs = solution.all_inputs
self.all_inputs_casadi = solution.all_inputs_casadi
self.mesh = base_variables[0].mesh
self.domain = base_variables[0].domain
self.domains = base_variables[0].domains
self.warn = warn
self.cumtrapz_ic = cumtrapz_ic
# Sensitivity starts off uninitialized, only set when called
self._sensitivities = None
self.solution_sensitivities = solution.sensitivities
# Set timescale
self.timescale = solution.timescale_eval
self.t_pts_nondim = solution.t
self.t_pts = solution.t * self.timescale
# Store length scales
self.length_scales = solution.length_scales_eval
# Evaluate base variable at initial time
self.base_eval = self.base_variables_casadi[0](
self.all_ts[0][0], self.all_ys[0][:, 0], self.all_inputs_casadi[0]
).full()
# handle 2D (in space) finite element variables differently
if (
self.mesh
and "current collector" in self.domain
and isinstance(self.mesh, pybamm.ScikitSubMesh2D)
):
self.initialise_2D_scikit_fem()
# check variable shape
else:
if (
isinstance(self.base_eval, numbers.Number)
or len(self.base_eval.shape) == 0
or self.base_eval.shape[0] == 1
):
self.initialise_0D()
else:
n = self.mesh.npts
base_shape = self.base_eval.shape[0]
# Try some shapes that could make the variable a 1D variable
if base_shape in [n, n + 1]:
self.initialise_1D()
else:
# Try some shapes that could make the variable a 2D variable
first_dim_nodes = self.mesh.nodes
first_dim_edges = self.mesh.edges
second_dim_pts = self.base_variables[0].secondary_mesh.nodes
if self.base_eval.size // len(second_dim_pts) in [
len(first_dim_nodes),
len(first_dim_edges),
]:
self.initialise_2D()
else:
# Raise error for 3D variable
raise NotImplementedError(
"Shape not recognized for {} ".format(base_variables[0])
+ "(note processing of 3D variables is not yet implemented)"
)
def initialise_0D(self):
# initialise empty array of the correct size
entries = np.empty(len(self.t_pts))
idx = 0
entries = np.empty(len(self.t_pts))
idx = 0
# Evaluate the base_variable index-by-index
for ts, ys, inputs, base_var_casadi in zip(
self.all_ts, self.all_ys, self.all_inputs_casadi, self.base_variables_casadi
):
for inner_idx, t in enumerate(ts):
t = ts[inner_idx]
y = ys[:, inner_idx]
entries[idx] = float(base_var_casadi(t, y, inputs))
idx += 1
if self.cumtrapz_ic is not None:
entries = cumulative_trapezoid(
entries, self.t_pts_nondim, initial=float(self.cumtrapz_ic)
)
# set up interpolation
if len(self.t_pts) == 1:
# Variable is just a scalar value, but we need to create a callable
# function to be consistent with other processed variables
self._interpolation_function = Interpolant0D(entries)
else:
self._interpolation_function = interp.interp1d(
self.t_pts,
entries,
kind="linear",
fill_value=np.nan,
bounds_error=False,
)
self.entries = entries
self.dimensions = 0
def initialise_1D(self, fixed_t=False):
len_space = self.base_eval.shape[0]
entries = np.empty((len_space, len(self.t_pts)))
# Evaluate the base_variable index-by-index
idx = 0
for ts, ys, inputs, base_var_casadi in zip(
self.all_ts, self.all_ys, self.all_inputs_casadi, self.base_variables_casadi
):
for inner_idx, t in enumerate(ts):
t = ts[inner_idx]
y = ys[:, inner_idx]
entries[:, idx] = base_var_casadi(t, y, inputs).full()[:, 0]
idx += 1
# Get node and edge values
nodes = self.mesh.nodes
edges = self.mesh.edges
if entries.shape[0] == len(nodes):
space = nodes
elif entries.shape[0] == len(edges):
space = edges
# add points outside domain for extrapolation to boundaries
extrap_space_left = np.array([2 * space[0] - space[1]])
extrap_space_right = np.array([2 * space[-1] - space[-2]])
space = np.concatenate([extrap_space_left, space, extrap_space_right])
extrap_entries_left = 2 * entries[0] - entries[1]
extrap_entries_right = 2 * entries[-1] - entries[-2]
entries_for_interp = np.vstack(
[extrap_entries_left, entries, extrap_entries_right]
)
# assign attributes for reference (either x_sol or r_sol)
self.entries = entries
self.dimensions = 1
if self.domain[0].endswith("particle"):
self.first_dimension = "r"
self.r_sol = space
elif self.domain[0] in [
"negative electrode",
"separator",
"positive electrode",
]:
self.first_dimension = "x"
self.x_sol = space
elif self.domain == ["current collector"]:
self.first_dimension = "z"
self.z_sol = space
elif self.domain[0].endswith("particle size"):
self.first_dimension = "R"
self.R_sol = space
else:
self.first_dimension = "x"
self.x_sol = space
# assign attributes for reference
length_scale = self.get_spatial_scale(self.first_dimension, self.domain[0])
pts_for_interp = space * length_scale
self.internal_boundaries = [
bnd * length_scale for bnd in self.mesh.internal_boundaries
]
# Set first_dim_pts to edges for nicer plotting
self.first_dim_pts = edges * length_scale
# set up interpolation
if len(self.t_pts) == 1:
# function of space only
self._interpolation_function = Interpolant1D(
pts_for_interp, entries_for_interp
)
else:
# function of space and time. Note that the order of 't' and 'space'
# is the reverse of what you'd expect
self._interpolation_function = interp.interp2d(
self.t_pts,
pts_for_interp,
entries_for_interp,
kind="linear",
fill_value=np.nan,
bounds_error=False,
)
def initialise_2D(self):
"""
Initialise a 2D object that depends on x and r, x and z, x and R, or R and r.
"""
first_dim_nodes = self.mesh.nodes
first_dim_edges = self.mesh.edges
second_dim_nodes = self.base_variables[0].secondary_mesh.nodes
second_dim_edges = self.base_variables[0].secondary_mesh.edges
if self.base_eval.size // len(second_dim_nodes) == len(first_dim_nodes):
first_dim_pts = first_dim_nodes
elif self.base_eval.size // len(second_dim_nodes) == len(first_dim_edges):
first_dim_pts = first_dim_edges
second_dim_pts = second_dim_nodes
first_dim_size = len(first_dim_pts)
second_dim_size = len(second_dim_pts)
entries = np.empty((first_dim_size, second_dim_size, len(self.t_pts)))
# Evaluate the base_variable index-by-index
idx = 0
for ts, ys, inputs, base_var_casadi in zip(
self.all_ts, self.all_ys, self.all_inputs_casadi, self.base_variables_casadi
):
for inner_idx, t in enumerate(ts):
t = ts[inner_idx]
y = ys[:, inner_idx]
entries[:, :, idx] = np.reshape(
base_var_casadi(t, y, inputs).full(),
[first_dim_size, second_dim_size],
order="F",
)
idx += 1
# add points outside first dimension domain for extrapolation to
# boundaries
extrap_space_first_dim_left = np.array(
[2 * first_dim_pts[0] - first_dim_pts[1]]
)
extrap_space_first_dim_right = np.array(
[2 * first_dim_pts[-1] - first_dim_pts[-2]]
)
first_dim_pts = np.concatenate(
[extrap_space_first_dim_left, first_dim_pts, extrap_space_first_dim_right]
)
extrap_entries_left = np.expand_dims(2 * entries[0] - entries[1], axis=0)
extrap_entries_right = np.expand_dims(2 * entries[-1] - entries[-2], axis=0)
entries_for_interp = np.concatenate(
[extrap_entries_left, entries, extrap_entries_right], axis=0
)
# add points outside second dimension domain for extrapolation to
# boundaries
extrap_space_second_dim_left = np.array(
[2 * second_dim_pts[0] - second_dim_pts[1]]
)
extrap_space_second_dim_right = np.array(
[2 * second_dim_pts[-1] - second_dim_pts[-2]]
)
second_dim_pts = np.concatenate(
[
extrap_space_second_dim_left,
second_dim_pts,
extrap_space_second_dim_right,
]
)
extrap_entries_second_dim_left = np.expand_dims(
2 * entries_for_interp[:, 0, :] - entries_for_interp[:, 1, :], axis=1
)
extrap_entries_second_dim_right = np.expand_dims(
2 * entries_for_interp[:, -1, :] - entries_for_interp[:, -2, :], axis=1
)
entries_for_interp = np.concatenate(
[
extrap_entries_second_dim_left,
entries_for_interp,
extrap_entries_second_dim_right,
],
axis=1,
)
# Process r-x, x-z, r-R, R-x, or R-z
if self.domain[0].endswith("particle") and self.domains["secondary"][
0
].endswith("electrode"):
self.first_dimension = "r"
self.second_dimension = "x"
self.r_sol = first_dim_pts
self.x_sol = second_dim_pts
elif self.domain[0] in [
"negative electrode",
"separator",
"positive electrode",
] and self.domains["secondary"] == ["current collector"]:
self.first_dimension = "x"
self.second_dimension = "z"
self.x_sol = first_dim_pts
self.z_sol = second_dim_pts
elif self.domain[0].endswith("particle") and self.domains["secondary"][
0
].endswith("particle size"):
self.first_dimension = "r"
self.second_dimension = "R"
self.r_sol = first_dim_pts
self.R_sol = second_dim_pts
elif self.domain[0].endswith("particle size") and self.domains["secondary"][
0
].endswith("electrode"):
self.first_dimension = "R"
self.second_dimension = "x"
self.R_sol = first_dim_pts
self.x_sol = second_dim_pts
elif self.domain[0].endswith("particle size") and self.domains["secondary"] == [
"current collector"
]:
self.first_dimension = "R"
self.second_dimension = "z"
self.R_sol = first_dim_pts
self.z_sol = second_dim_pts
else: # pragma: no cover
raise pybamm.DomainError(
f"Cannot process 2D object with domains '{self.domains}'."
)
# assign attributes for reference
self.entries = entries
self.dimensions = 2
first_length_scale = self.get_spatial_scale(
self.first_dimension, self.domain[0]
)
first_dim_pts_for_interp = first_dim_pts * first_length_scale
second_length_scale = self.get_spatial_scale(
self.second_dimension, self.domains["secondary"][0]
)
second_dim_pts_for_interp = second_dim_pts * second_length_scale
# Set pts to edges for nicer plotting
self.first_dim_pts = first_dim_edges * first_length_scale
self.second_dim_pts = second_dim_edges * second_length_scale
# set up interpolation
if len(self.t_pts) == 1:
# function of space only. Note the order of the points is the reverse
# of what you'd expect
self._interpolation_function = Interpolant2D(
first_dim_pts_for_interp, second_dim_pts_for_interp, entries_for_interp
)
else:
# function of space and time.
self._interpolation_function = interp.RegularGridInterpolator(
(first_dim_pts_for_interp, second_dim_pts_for_interp, self.t_pts),
entries_for_interp,
method="linear",
fill_value=np.nan,
bounds_error=False,
)
def initialise_2D_scikit_fem(self):
y_sol = self.mesh.edges["y"]
len_y = len(y_sol)
z_sol = self.mesh.edges["z"]
len_z = len(z_sol)
entries = np.empty((len_y, len_z, len(self.t_pts)))
# Evaluate the base_variable index-by-index
idx = 0
for ts, ys, inputs, base_var_casadi in zip(
self.all_ts, self.all_ys, self.all_inputs_casadi, self.base_variables_casadi
):
for inner_idx, t in enumerate(ts):
t = ts[inner_idx]
y = ys[:, inner_idx]
entries[:, :, idx] = np.reshape(
base_var_casadi(t, y, inputs).full(),
[len_y, len_z],
order="C",
)
idx += 1
# assign attributes for reference
self.entries = entries
self.dimensions = 2
self.y_sol = y_sol
self.z_sol = z_sol
self.first_dimension = "y"
self.second_dimension = "z"
self.first_dim_pts = y_sol * self.get_spatial_scale("y", "current collector")
self.second_dim_pts = z_sol * self.get_spatial_scale("z", "current collector")
# set up interpolation
if len(self.t_pts) == 1:
# function of space only. Note the order of the points is the reverse
# of what you'd expect
self._interpolation_function = Interpolant2D(
self.first_dim_pts, self.second_dim_pts, entries
)
else:
# function of space and time.
self._interpolation_function = interp.RegularGridInterpolator(
(self.first_dim_pts, self.second_dim_pts, self.t_pts),
entries,
method="linear",
fill_value=np.nan,
bounds_error=False,
)
def __call__(self, t=None, x=None, r=None, y=None, z=None, R=None, warn=True):
"""
Evaluate the variable at arbitrary *dimensional* t (and x, r, y, z and/or R),
using interpolation
"""
# If t is None and there is only one value of time in the soluton (i.e.
# the solution is independent of time) then we set t equal to the value
# stored in the solution. If the variable is constant (doesn't depend on
# time) evaluate arbitrarily at the first value of t. Otherwise, raise
# an error
if t is None:
if len(self.t_pts) == 1:
t = self.t_pts
elif len(self.base_variables) == 1 and self.base_variables[0].is_constant():
t = self.t_pts[0]
else:
raise ValueError(
"t cannot be None for variable {}".format(self.base_variables)
)
# Call interpolant of correct spatial dimension
if self.dimensions == 0:
out = self._interpolation_function(t)
elif self.dimensions == 1:
out = self.call_1D(t, x, r, z, R)
elif self.dimensions == 2:
out = self.call_2D(t, x, r, y, z, R)
if warn is True and np.isnan(out).any():
pybamm.logger.warning(
"Calling variable outside interpolation range (returns 'nan')"
)
return out
def call_1D(self, t, x, r, z, R):
"""Evaluate a 1D variable"""
spatial_var = eval_dimension_name(self.first_dimension, x, r, None, z, R)
return self._interpolation_function(t, spatial_var)
def call_2D(self, t, x, r, y, z, R):
"""Evaluate a 2D variable"""
first_dim = eval_dimension_name(self.first_dimension, x, r, y, z, R)
second_dim = eval_dimension_name(self.second_dimension, x, r, y, z, R)
if isinstance(first_dim, np.ndarray):
if isinstance(second_dim, np.ndarray) and isinstance(t, np.ndarray):
first_dim = first_dim[:, np.newaxis, np.newaxis]
second_dim = second_dim[:, np.newaxis]
elif isinstance(second_dim, np.ndarray) or isinstance(t, np.ndarray):
first_dim = first_dim[:, np.newaxis]
else:
if isinstance(second_dim, np.ndarray) and isinstance(t, np.ndarray):
second_dim = second_dim[:, np.newaxis]
return self._interpolation_function((first_dim, second_dim, t))
def get_spatial_scale(self, name, domain):
"""Returns the spatial scale for a named spatial variable"""
try:
if name == "y" and domain == "current collector":
return self.length_scales["current collector y"]
elif name == "z" and domain == "current collector":
return self.length_scales["current collector z"]
else:
return self.length_scales[domain]
except KeyError:
if self.warn: # pragma: no cover
pybamm.logger.warning(
"No length scale set for {}. "
"Using default of 1 [m].".format(domain)
)
return 1
@property
def data(self):
"""Same as entries, but different name"""
return self.entries
@property
def sensitivities(self):
"""
Returns a dictionary of sensitivities for each input parameter.
The keys are the input parameters, and the value is a matrix of size
(n_x * n_t, n_p), where n_x is the number of states, n_t is the number of time
points, and n_p is the size of the input parameter
"""
# No sensitivities if there are no inputs
if len(self.all_inputs[0]) == 0:
return {}
# Otherwise initialise and return sensitivities
if self._sensitivities is None:
if self.solution_sensitivities != {}:
self.initialise_sensitivity_explicit_forward()
else:
raise ValueError(
"Cannot compute sensitivities. The 'calculate_sensitivities' "
"argument of the solver.solve should be changed from 'None' to "
"allow sensitivities calculations. Check solver documentation for "
"details."
)
return self._sensitivities
def initialise_sensitivity_explicit_forward(self):
"Set up the sensitivity dictionary"
inputs_stacked = self.all_inputs_casadi[0]
# Set up symbolic variables
t_casadi = casadi.MX.sym("t")
y_casadi = casadi.MX.sym("y", self.all_ys[0].shape[0])
p_casadi = {
name: casadi.MX.sym(name, value.shape[0])
for name, value in self.all_inputs[0].items()
}
p_casadi_stacked = casadi.vertcat(*[p for p in p_casadi.values()])
# Convert variable to casadi format for differentiating
var_casadi = self.base_variables[0].to_casadi(
t_casadi, y_casadi, inputs=p_casadi
)
dvar_dy = casadi.jacobian(var_casadi, y_casadi)
dvar_dp = casadi.jacobian(var_casadi, p_casadi_stacked)
# Convert to functions and evaluate index-by-index
dvar_dy_func = casadi.Function(
"dvar_dy", [t_casadi, y_casadi, p_casadi_stacked], [dvar_dy]
)
dvar_dp_func = casadi.Function(
"dvar_dp", [t_casadi, y_casadi, p_casadi_stacked], [dvar_dp]
)
for index, (ts, ys) in enumerate(zip(self.all_ts, self.all_ys)):
for idx, t in enumerate(ts):
u = ys[:, idx]
next_dvar_dy_eval = dvar_dy_func(t, u, inputs_stacked)
next_dvar_dp_eval = dvar_dp_func(t, u, inputs_stacked)
if index == 0 and idx == 0:
dvar_dy_eval = next_dvar_dy_eval
dvar_dp_eval = next_dvar_dp_eval
else:
dvar_dy_eval = casadi.diagcat(dvar_dy_eval, next_dvar_dy_eval)
dvar_dp_eval = casadi.vertcat(dvar_dp_eval, next_dvar_dp_eval)
# Compute sensitivity
dy_dp = self.solution_sensitivities["all"]
S_var = dvar_dy_eval @ dy_dp + dvar_dp_eval
sensitivities = {"all": S_var}
# Add the individual sensitivity
start = 0
for name, inp in self.all_inputs[0].items():
end = start + inp.shape[0]
sensitivities[name] = S_var[:, start:end]
start = end
# Save attribute
self._sensitivities = sensitivities
class Interpolant0D:
def __init__(self, entries):
self.entries = entries
def __call__(self, t):
return self.entries
class Interpolant1D:
def __init__(self, pts_for_interp, entries_for_interp):
self.interpolant = interp.interp1d(
pts_for_interp,
entries_for_interp[:, 0],
kind="linear",
fill_value=np.nan,
bounds_error=False,
)
def __call__(self, t, z):
if isinstance(z, np.ndarray):
return self.interpolant(z)[:, np.newaxis]
else:
return self.interpolant(z)
class Interpolant2D:
def __init__(
self, first_dim_pts_for_interp, second_dim_pts_for_interp, entries_for_interp
):
self.interpolant = interp.interp2d(
second_dim_pts_for_interp,
first_dim_pts_for_interp,
entries_for_interp[:, :, 0],
kind="linear",
fill_value=np.nan,
bounds_error=False,
)
def __call__(self, input):
"""
Calls and returns a 2D interpolant of the correct shape depending on the
shape of the input
"""
first_dim, second_dim, _ = input
if isinstance(first_dim, np.ndarray) and isinstance(second_dim, np.ndarray):
first_dim = first_dim[:, 0, 0]
second_dim = second_dim[:, 0]
return self.interpolant(second_dim, first_dim)
elif isinstance(first_dim, np.ndarray):
first_dim = first_dim[:, 0]
return self.interpolant(second_dim, first_dim)[:, 0]
elif isinstance(second_dim, np.ndarray):
second_dim = second_dim[:, 0]
return self.interpolant(second_dim, first_dim)
else:
return self.interpolant(second_dim, first_dim)[0]
def eval_dimension_name(name, x, r, y, z, R):
if name == "x":
out = x
elif name == "r":
out = r
elif name == "y":
out = y
elif name == "z":
out = z
elif name == "R":
out = R
if out is None:
raise ValueError("inputs {} cannot be None".format(name))
else:
return out