-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
Copy pathbase_battery_model.py
977 lines (888 loc) · 40.7 KB
/
base_battery_model.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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
#
# Base battery model class
#
import pybamm
import warnings
class BaseBatteryModel(pybamm.BaseModel):
"""
Base model class with some default settings and required variables
Attributes
----------
options: dict
A dictionary of options to be passed to the model. The options that can
be set are listed below. Note that not all of the options are compatible with
each other and with all of the models implemented in PyBaMM. Each option is
optional and takes a default value if not provided.
* "cell geometry" : str
Sets the geometry of the cell. Can be "pouch" (default) or
"arbitrary". The arbitrary geometry option solves a 1D electrochemical
model with prescribed cell volume and cross-sectional area, and
(if thermal effects are included) solves a lumped thermal model
with prescribed surface area for cooling.
* "convection" : str
Whether to include the effects of convection in the model. Can be
"none" (default), "uniform transverse" or "full transverse".
Must be "none" for lithium-ion models.
* "current collector" : str
Sets the current collector model to use. Can be "uniform" (default),
"potential pair" or "potential pair quite conductive".
* "dimensionality" : int
Sets the dimension of the current collector problem. Can be 0
(default), 1 or 2.
* "external submodels" : list
A list of the submodels that you would like to supply an external
variable for instead of solving in PyBaMM. The entries of the lists
are strings that correspond to the submodel names in the keys
of `self.submodels`.
* "interfacial surface area" : str
Sets the model for the interfacial surface area. Can be "constant"
(default) or "varying". Not currently implemented in any of the models.
* "loss of active material" : str
Sets the model for loss of active material. Can be "none" (default),
"positive", "negative" or "both" to enable it for the specific
electrode.
* "particle" : str
Sets the submodel to use to describe behaviour within the particle.
Can be "Fickian diffusion" (default), "uniform profile",
"quadratic profile", or "quartic profile".
* "particle shape" : str
Sets the model shape of the electrode particles. This is used to
calculate the surface area to volume ratio. Can be "spherical"
(default) or "user". For the "user" option the surface area per
unit volume can be passed as a parameter, and is therefore not
necessarily consistent with the particle shape.
* "particle cracking" : str
Sets the model to account for mechanical effects and particle
cracking. Can be "none", "no cracking", "negative", "positive" or
"both".
All options other than "none" account for the effects of swelling
of electrode particles, cell thickness change, and stress-assisted
diffusion. The options "negative", "positive" or "both" additionally
account for crack propagation in the negative, positive or both
electrodes, respectively.
* "sei" : str
Set the sei submodel to be used. Options are:
- "none": :class:`pybamm.sei.NoSEI` (no SEI growth)
- "constant": :class:`pybamm.sei.Constant` (constant SEI thickness)
- "reaction limited": :class:`pybamm.sei.ReactionLimited`
- "solvent-diffusion limited": \
:class:`pybamm.sei.SolventDiffusionLimited`
- "electron-migration limited": \
:class:`pybamm.sei.ElectronMigrationLimited`
- "interstitial-diffusion limited": \
:class:`pybamm.sei.InterstitialDiffusionLimited`
- "ec reaction limited": \
:class:`pybamm.sei.EcReactionLimited`
* "sei film resistance" : str
Set the submodel for additional term in the overpotential due to SEI.
The default value is "none" if the "sei" option is "none", and
"distributed" otherwise. This is because the "distributed" model is more
complex than the model with no additional resistance, which adds
unnecessary complexity if there is no SEI in the first place
- "none": no additional resistance\
.. math::
\\eta_r = \\frac{F}{RT} * (\\phi_s - \\phi_e - U)
- "distributed": properly included additional resistance term\
.. math::
\\eta_r = \\frac{F}{RT}
* (\\phi_s - \\phi_e - U - R_{sei} * L_{sei} * j)
- "average": constant additional resistance term (approximation to the \
true model). This model can give similar results to the \
"distributed" case without needing to make j an algebraic state\
.. math::
\\eta_r = \\frac{F}{RT}
* (\\phi_s - \\phi_e - U - R_{sei} * L_{sei} * \\frac{I}{aL})
* "sei porosity change" : str
Whether to include porosity change due to SEI formation, can be "false"
(default) or "true".
* "side reactions" : list
Contains a list of any side reactions to include. Default is []. If this
list is not empty (i.e. side reactions are included in the model), then
"surface form" cannot be 'false'.
* "surface form" : str
Whether to use the surface formulation of the problem. Can be "false"
(default), "differential" or "algebraic".
* "thermal" : str
Sets the thermal model to use. Can be "isothermal" (default), "lumped",
"x-lumped", or "x-full".
* "total interfacial current density as a state" : str
Whether to make a state for the total interfacial current density and
solve an algebraic equation for it. Default is "false", unless "sei film
resistance" is distributed in which case it is automatically set to
"true".
**Extends:** :class:`pybamm.BaseModel`
"""
def __init__(self, options=None, name="Unnamed battery model"):
super().__init__(name)
self.options = options
self.submodels = {}
self._built = False
self._built_fundamental_and_external = False
@property
def default_parameter_values(self):
# Default parameter values
# Lion parameters left as default parameter set for tests
return pybamm.ParameterValues(chemistry=pybamm.parameter_sets.Marquis2019)
@property
def default_geometry(self):
return pybamm.battery_geometry(
current_collector_dimension=self.options["dimensionality"]
)
@property
def default_var_pts(self):
var = pybamm.standard_spatial_vars
base_var_pts = {
var.x_n: 20,
var.x_s: 20,
var.x_p: 20,
var.r_n: 30,
var.r_p: 30,
var.y: 10,
var.z: 10,
}
# Reduce the default points for 2D current collectors
if self.options["dimensionality"] == 2:
base_var_pts.update({var.x_n: 10, var.x_s: 10, var.x_p: 10})
return base_var_pts
@property
def default_submesh_types(self):
base_submeshes = {
"negative electrode": pybamm.MeshGenerator(pybamm.Uniform1DSubMesh),
"separator": pybamm.MeshGenerator(pybamm.Uniform1DSubMesh),
"positive electrode": pybamm.MeshGenerator(pybamm.Uniform1DSubMesh),
"negative particle": pybamm.MeshGenerator(pybamm.Uniform1DSubMesh),
"positive particle": pybamm.MeshGenerator(pybamm.Uniform1DSubMesh),
}
if self.options["dimensionality"] == 0:
base_submeshes["current collector"] = pybamm.MeshGenerator(pybamm.SubMesh0D)
elif self.options["dimensionality"] == 1:
base_submeshes["current collector"] = pybamm.MeshGenerator(
pybamm.Uniform1DSubMesh
)
elif self.options["dimensionality"] == 2:
base_submeshes["current collector"] = pybamm.MeshGenerator(
pybamm.ScikitUniform2DSubMesh
)
return base_submeshes
@property
def default_spatial_methods(self):
base_spatial_methods = {
"macroscale": pybamm.FiniteVolume(),
"negative particle": pybamm.FiniteVolume(),
"positive particle": pybamm.FiniteVolume(),
}
if self.options["dimensionality"] == 0:
# 0D submesh - use base spatial method
base_spatial_methods[
"current collector"
] = pybamm.ZeroDimensionalSpatialMethod()
elif self.options["dimensionality"] == 1:
base_spatial_methods["current collector"] = pybamm.FiniteVolume()
elif self.options["dimensionality"] == 2:
base_spatial_methods["current collector"] = pybamm.ScikitFiniteElement()
return base_spatial_methods
@property
def options(self):
return self._options
@options.setter
def options(self, extra_options):
default_options = {
"operating mode": "current",
"dimensionality": 0,
"surface form": "false",
"convection": "none",
"side reactions": [],
"interfacial surface area": "constant",
"current collector": "uniform",
"particle": "Fickian diffusion",
"particle shape": "spherical",
"electrolyte conductivity": "default",
"thermal": "isothermal",
"cell geometry": "none",
"external submodels": [],
"sei": "none",
"sei porosity change": "false",
"loss of active material": "none",
"working electrode": "none",
"particle cracking": "none",
"total interfacial current density as a state": "false",
}
# Change the default for cell geometry based on which thermal option is provided
extra_options = extra_options or {}
thermal_option = extra_options.get(
"thermal", "none"
) # return "none" if option not given
if thermal_option in ["none", "isothermal", "lumped"]:
default_options["cell geometry"] = "arbitrary"
else:
default_options["cell geometry"] = "pouch"
# The "cell geometry" option will still be overridden by extra_options if
# provided
# Change the default for SEI film resistance based on which sei option is
# provided
# extra_options = extra_options or {}
sei_option = extra_options.get(
"sei", "none"
) # return "none" if option not given
if sei_option == "none":
default_options["sei film resistance"] = "none"
else:
default_options["sei film resistance"] = "distributed"
# The "sei film resistance" option will still be overridden by extra_options if
# provided
options = pybamm.FuzzyDict(default_options)
# any extra options overwrite the default options
for name, opt in extra_options.items():
if name in default_options:
options[name] = opt
else:
raise pybamm.OptionError(
"Option '{}' not recognised. Best matches are {}".format(
name, options.get_best_matches(name)
)
)
# If "sei film resistance" is "distributed" then "total interfacial current
# density as a state" must be "true"
if options["sei film resistance"] == "distributed":
options["total interfacial current density as a state"] = "true"
# Check that extra_options did not try to provide a clashing option
if (
extra_options.get("total interfacial current density as a state")
== "false"
):
raise pybamm.OptionError(
"If 'sei film resistance' is 'distributed' then 'total interfacial "
"current density as a state' must be 'true'"
)
# Options that are incompatible with models
if isinstance(self, pybamm.lithium_ion.BaseModel):
if options["convection"] != "none":
raise pybamm.OptionError(
"convection not implemented for lithium-ion models"
)
if (
options["thermal"] in ["x-lumped", "x-full"]
and options["cell geometry"] != "pouch"
):
raise pybamm.OptionError(
options["thermal"] + " model must have pouch geometry."
)
if isinstance(self, pybamm.lead_acid.BaseModel):
if options["thermal"] != "isothermal" and options["dimensionality"] != 0:
raise pybamm.OptionError(
"Lead-acid models can only have thermal "
"effects if dimensionality is 0."
)
if options["sei"] != "none" or options["sei film resistance"] != "none":
raise pybamm.OptionError("Lead-acid models cannot have SEI formation")
# Some standard checks to make sure options are compatible
if not (
options["operating mode"] in ["current", "voltage", "power"]
or callable(options["operating mode"])
):
raise pybamm.OptionError(
"operating mode '{}' not recognised".format(options["operating mode"])
)
if (
isinstance(self, (pybamm.lead_acid.LOQS, pybamm.lead_acid.Composite))
and options["surface form"] == "false"
):
if len(options["side reactions"]) > 0:
raise pybamm.OptionError(
"""must use surface formulation to solve {!s} with side reactions
""".format(
self
)
)
if options["surface form"] not in ["false", "differential", "algebraic"]:
raise pybamm.OptionError(
"surface form '{}' not recognised".format(options["surface form"])
)
if options["convection"] not in [
"none",
"uniform transverse",
"full transverse",
]:
raise pybamm.OptionError(
"convection option '{}' not recognised".format(options["convection"])
)
if options["current collector"] not in [
"uniform",
"potential pair",
"potential pair quite conductive",
]:
raise pybamm.OptionError(
"current collector model '{}' not recognised".format(
options["current collector"]
)
)
if options["dimensionality"] not in [0, 1, 2]:
raise pybamm.OptionError(
"Dimension of current collectors must be 0, 1, or 2, not {}".format(
options["dimensionality"]
)
)
if options["thermal"] not in ["isothermal", "lumped", "x-lumped", "x-full"]:
raise pybamm.OptionError(
"Unknown thermal model '{}'".format(options["thermal"])
)
if options["cell geometry"] not in ["arbitrary", "pouch"]:
raise pybamm.OptionError(
"Unknown geometry '{}'".format(options["cell geometry"])
)
if options["sei"] not in [
"none",
"constant",
"reaction limited",
"solvent-diffusion limited",
"electron-migration limited",
"interstitial-diffusion limited",
"ec reaction limited",
]:
raise pybamm.OptionError("Unknown sei model '{}'".format(options["sei"]))
if options["sei film resistance"] not in ["none", "distributed", "average"]:
raise pybamm.OptionError(
"Unknown sei film resistance model '{}'".format(
options["sei film resistance"]
)
)
if options["sei porosity change"] not in ["true", "false"]:
if options["sei porosity change"] in [True, False]:
raise pybamm.OptionError(
"sei porosity change must now be given in string format "
"('true' or 'false')"
)
raise pybamm.OptionError(
"Unknown sei porosity change '{}'".format(
options["sei porosity change"]
)
)
if options["loss of active material"] not in [
"none",
"negative",
"positive",
"both",
]:
raise pybamm.OptionError(
"Unknown loss of active material '{}'".format(
options["loss of active material"]
)
)
if options["particle cracking"] not in [
"none",
"no cracking",
"negative",
"positive",
"both",
]:
raise pybamm.OptionError(
"Unknown particle cracking '{}'".format(options["particle cracking"])
)
if options["dimensionality"] == 0:
if options["current collector"] not in ["uniform"]:
raise pybamm.OptionError(
"current collector model must be uniform in 0D model"
)
if options["convection"] == "full transverse":
raise pybamm.OptionError(
"cannot have transverse convection in 0D model"
)
if options["particle"] not in [
"Fickian diffusion",
"fast diffusion",
"uniform profile",
"quadratic profile",
"quartic profile",
]:
raise pybamm.OptionError(
"particle model '{}' not recognised".format(options["particle"])
)
if options["particle"] == "fast diffusion":
raise NotImplementedError(
"The 'fast diffusion' option has been renamed. "
"Use 'uniform profile' instead."
)
if options["particle shape"] not in ["spherical", "user", "no particles"]:
raise pybamm.OptionError(
"particle shape '{}' not recognised".format(options["particle shape"])
)
if options["thermal"] == "x-lumped" and options["dimensionality"] == 1:
warnings.warn(
"1+1D Thermal models are only valid if both tabs are "
"placed at the top of the cell."
)
if options["electrolyte conductivity"] not in [
"default",
"full",
"leading order",
"composite",
"integrated",
]:
raise pybamm.OptionError(
"electrolyte conductivity model '{}' not recognised".format(
options["electrolyte conductivity"]
)
)
self._options = options
def set_standard_output_variables(self):
# Time
self.variables.update(
{
"Time": pybamm.t,
"Time [s]": pybamm.t * self.timescale,
"Time [min]": pybamm.t * self.timescale / 60,
"Time [h]": pybamm.t * self.timescale / 3600,
}
)
# Spatial
var = pybamm.standard_spatial_vars
L_x = self.param.L_x
L_y = self.param.L_y
L_z = self.param.L_z
self.variables.update(
{
"x": var.x,
"x [m]": var.x * L_x,
"x_n": var.x_n,
"x_n [m]": var.x_n * L_x,
"x_s": var.x_s,
"x_s [m]": var.x_s * L_x,
"x_p": var.x_p,
"x_p [m]": var.x_p * L_x,
}
)
if self.options["dimensionality"] == 1:
self.variables.update({"z": var.z, "z [m]": var.z * L_z})
elif self.options["dimensionality"] == 2:
self.variables.update(
{"y": var.y, "y [m]": var.y * L_y, "z": var.z, "z [m]": var.z * L_z}
)
# Initialize "total reaction" variables
# These will get populated by the "get_coupled_variables" methods, and then used
# later by "set_rhs" or "set_algebraic", which ensures that we always have
# added all the necessary variables by the time the sum is used
self.variables.update(
{
"Sum of electrolyte reaction source terms": 0,
"Sum of negative electrode electrolyte reaction source terms": 0,
"Sum of positive electrode electrolyte reaction source terms": 0,
"Sum of x-averaged negative electrode "
"electrolyte reaction source terms": 0,
"Sum of x-averaged positive electrode "
"electrolyte reaction source terms": 0,
"Sum of interfacial current densities": 0,
"Sum of negative electrode interfacial current densities": 0,
"Sum of positive electrode interfacial current densities": 0,
"Sum of x-averaged negative electrode interfacial current densities": 0,
"Sum of x-averaged positive electrode interfacial current densities": 0,
}
)
def build_fundamental_and_external(self):
# Get the fundamental variables
for submodel_name, submodel in self.submodels.items():
pybamm.logger.debug(
"Getting fundamental variables for {} submodel ({})".format(
submodel_name, self.name
)
)
self.variables.update(submodel.get_fundamental_variables())
# set the submodels that are external
for sub in self.options["external submodels"]:
self.submodels[sub].external = True
# Set any external variables
self.external_variables = []
for submodel_name, submodel in self.submodels.items():
pybamm.logger.debug(
"Getting external variables for {} submodel ({})".format(
submodel_name, self.name
)
)
external_variables = submodel.get_external_variables()
self.external_variables += external_variables
self._built_fundamental_and_external = True
def build_coupled_variables(self):
# Note: pybamm will try to get the coupled variables for the submodels in the
# order they are set by the user. If this fails for a particular submodel,
# return to it later and try again. If setting coupled variables fails and
# there are no more submodels to try, raise an error.
submodels = list(self.submodels.keys())
count = 0
# For this part the FuzzyDict of variables is briefly converted back into a
# normal dictionary for speed with KeyErrors
self._variables = dict(self._variables)
while len(submodels) > 0:
count += 1
for submodel_name, submodel in self.submodels.items():
if submodel_name in submodels:
pybamm.logger.debug(
"Getting coupled variables for {} submodel ({})".format(
submodel_name, self.name
)
)
try:
self.variables.update(
submodel.get_coupled_variables(self.variables)
)
submodels.remove(submodel_name)
except KeyError as key:
if len(submodels) == 1 or count == 100:
# no more submodels to try
raise pybamm.ModelError(
"Missing variable for submodel '{}': {}.\n".format(
submodel_name, key
)
+ "Check the selected "
"submodels provide all of the required variables."
)
else:
# try setting coupled variables on next loop through
pybamm.logger.debug(
"Can't find {}, trying other submodels first".format(
key
)
)
# Convert variables back into FuzzyDict
self._variables = pybamm.FuzzyDict(self._variables)
def build_model_equations(self):
# Set model equations
for submodel_name, submodel in self.submodels.items():
if submodel.external is False:
pybamm.logger.debug(
"Setting rhs for {} submodel ({})".format(submodel_name, self.name)
)
submodel.set_rhs(self.variables)
pybamm.logger.debug(
"Setting algebraic for {} submodel ({})".format(
submodel_name, self.name
)
)
submodel.set_algebraic(self.variables)
pybamm.logger.debug(
"Setting boundary conditions for {} submodel ({})".format(
submodel_name, self.name
)
)
submodel.set_boundary_conditions(self.variables)
pybamm.logger.debug(
"Setting initial conditions for {} submodel ({})".format(
submodel_name, self.name
)
)
submodel.set_initial_conditions(self.variables)
submodel.set_events(self.variables)
pybamm.logger.debug(
"Updating {} submodel ({})".format(submodel_name, self.name)
)
self.update(submodel)
self.check_no_repeated_keys()
def build_model(self):
# Check if already built
if self._built:
raise pybamm.ModelError(
"""Model already built. If you are adding a new submodel, try using
`model.update` instead."""
)
pybamm.logger.info("Start building {}".format(self.name))
if self._built_fundamental_and_external is False:
self.build_fundamental_and_external()
self.build_coupled_variables()
self.build_model_equations()
pybamm.logger.debug("Setting voltage variables ({})".format(self.name))
self.set_voltage_variables()
pybamm.logger.debug("Setting SoC variables ({})".format(self.name))
self.set_soc_variables()
# Massive hack for consistent delta_phi = phi_s - phi_e with SPMe
# This needs to be corrected
if isinstance(self, pybamm.lithium_ion.SPMe):
for domain in ["Negative", "Positive"]:
phi_s = self.variables[domain + " electrode potential"]
phi_e = self.variables[domain + " electrolyte potential"]
delta_phi = phi_s - phi_e
s = self.submodels[domain.lower() + " interface"]
var = s._get_standard_surface_potential_difference_variables(delta_phi)
self.variables.update(var)
self._built = True
pybamm.logger.info("Finish building {}".format(self.name))
def new_empty_copy(self):
"See :meth:`pybamm.BaseModel.new_empty_copy()`"
new_model = self.__class__(name=self.name, options=self.options, build=False)
new_model.use_jacobian = self.use_jacobian
new_model.use_simplify = self.use_simplify
new_model.convert_to_format = self.convert_to_format
new_model.timescale = self.timescale
new_model.length_scales = self.length_scales
return new_model
def set_external_circuit_submodel(self):
"""
Define how the external circuit defines the boundary conditions for the model,
e.g. (not necessarily constant-) current, voltage, etc
"""
if self.options["operating mode"] == "current":
self.submodels["external circuit"] = pybamm.external_circuit.CurrentControl(
self.param
)
elif self.options["operating mode"] == "voltage":
self.submodels[
"external circuit"
] = pybamm.external_circuit.VoltageFunctionControl(self.param)
elif self.options["operating mode"] == "power":
self.submodels[
"external circuit"
] = pybamm.external_circuit.PowerFunctionControl(self.param)
elif callable(self.options["operating mode"]):
self.submodels[
"external circuit"
] = pybamm.external_circuit.FunctionControl(
self.param, self.options["operating mode"]
)
def set_tortuosity_submodels(self):
self.submodels["electrolyte tortuosity"] = pybamm.tortuosity.Bruggeman(
self.param, "Electrolyte"
)
self.submodels["electrode tortuosity"] = pybamm.tortuosity.Bruggeman(
self.param, "Electrode"
)
def set_thermal_submodel(self):
if self.options["thermal"] == "isothermal":
thermal_submodel = pybamm.thermal.isothermal.Isothermal(self.param)
elif self.options["thermal"] == "lumped":
thermal_submodel = pybamm.thermal.Lumped(
self.param,
cc_dimension=self.options["dimensionality"],
geometry=self.options["cell geometry"],
)
elif self.options["thermal"] == "x-lumped":
if self.options["dimensionality"] == 0:
# With 0D current collectors x-lumped is equivalent to lumped pouch
thermal_submodel = pybamm.thermal.Lumped(self.param, geometry="pouch")
elif self.options["dimensionality"] == 1:
thermal_submodel = pybamm.thermal.pouch_cell.CurrentCollector1D(
self.param
)
elif self.options["dimensionality"] == 2:
thermal_submodel = pybamm.thermal.pouch_cell.CurrentCollector2D(
self.param
)
elif self.options["thermal"] == "x-full":
if self.options["dimensionality"] == 0:
thermal_submodel = pybamm.thermal.OneDimensionalX(self.param)
elif self.options["dimensionality"] == 1:
raise NotImplementedError(
"""X-full thermal submodels do not
yet support 1D current collectors"""
)
elif self.options["dimensionality"] == 2:
raise NotImplementedError(
"""X-full thermal submodels do
not yet support 2D current collectors"""
)
self.submodels["thermal"] = thermal_submodel
def set_current_collector_submodel(self):
if self.options["current collector"] in ["uniform"]:
submodel = pybamm.current_collector.Uniform(self.param)
elif self.options["current collector"] == "potential pair":
if self.options["dimensionality"] == 1:
submodel = pybamm.current_collector.PotentialPair1plus1D(self.param)
elif self.options["dimensionality"] == 2:
submodel = pybamm.current_collector.PotentialPair2plus1D(self.param)
self.submodels["current collector"] = submodel
def set_voltage_variables(self):
ocp_n = self.variables["Negative electrode open circuit potential"]
ocp_p = self.variables["Positive electrode open circuit potential"]
ocp_n_av = self.variables[
"X-averaged negative electrode open circuit potential"
]
ocp_p_av = self.variables[
"X-averaged positive electrode open circuit potential"
]
ocp_n_dim = self.variables["Negative electrode open circuit potential [V]"]
ocp_p_dim = self.variables["Positive electrode open circuit potential [V]"]
ocp_n_av_dim = self.variables[
"X-averaged negative electrode open circuit potential [V]"
]
ocp_p_av_dim = self.variables[
"X-averaged positive electrode open circuit potential [V]"
]
ocp_n_left = pybamm.boundary_value(ocp_n, "left")
ocp_n_left_dim = pybamm.boundary_value(ocp_n_dim, "left")
ocp_p_right = pybamm.boundary_value(ocp_p, "right")
ocp_p_right_dim = pybamm.boundary_value(ocp_p_dim, "right")
ocv_av = ocp_p_av - ocp_n_av
ocv_av_dim = ocp_p_av_dim - ocp_n_av_dim
ocv = ocp_p_right - ocp_n_left
ocv_dim = ocp_p_right_dim - ocp_n_left_dim
# overpotentials
eta_r_n_av = self.variables[
"X-averaged negative electrode reaction overpotential"
]
eta_r_n_av_dim = self.variables[
"X-averaged negative electrode reaction overpotential [V]"
]
eta_r_p_av = self.variables[
"X-averaged positive electrode reaction overpotential"
]
eta_r_p_av_dim = self.variables[
"X-averaged positive electrode reaction overpotential [V]"
]
delta_phi_s_n_av = self.variables["X-averaged negative electrode ohmic losses"]
delta_phi_s_n_av_dim = self.variables[
"X-averaged negative electrode ohmic losses [V]"
]
delta_phi_s_p_av = self.variables["X-averaged positive electrode ohmic losses"]
delta_phi_s_p_av_dim = self.variables[
"X-averaged positive electrode ohmic losses [V]"
]
delta_phi_s_av = delta_phi_s_p_av - delta_phi_s_n_av
delta_phi_s_av_dim = delta_phi_s_p_av_dim - delta_phi_s_n_av_dim
eta_r_av = eta_r_p_av - eta_r_n_av
eta_r_av_dim = eta_r_p_av_dim - eta_r_n_av_dim
# SEI film overpotential
eta_sei_n_av = self.variables[
"X-averaged negative electrode sei film overpotential"
]
eta_sei_p_av = self.variables[
"X-averaged positive electrode sei film overpotential"
]
eta_sei_n_av_dim = self.variables[
"X-averaged negative electrode sei film overpotential [V]"
]
eta_sei_p_av_dim = self.variables[
"X-averaged positive electrode sei film overpotential [V]"
]
eta_sei_av = eta_sei_n_av + eta_sei_p_av
eta_sei_av_dim = eta_sei_n_av_dim + eta_sei_p_av_dim
# TODO: add current collector losses to the voltage in 3D
self.variables.update(
{
"X-averaged open circuit voltage": ocv_av,
"Measured open circuit voltage": ocv,
"X-averaged open circuit voltage [V]": ocv_av_dim,
"Measured open circuit voltage [V]": ocv_dim,
"X-averaged reaction overpotential": eta_r_av,
"X-averaged reaction overpotential [V]": eta_r_av_dim,
"X-averaged sei film overpotential": eta_sei_av,
"X-averaged sei film overpotential [V]": eta_sei_av_dim,
"X-averaged solid phase ohmic losses": delta_phi_s_av,
"X-averaged solid phase ohmic losses [V]": delta_phi_s_av_dim,
}
)
# Battery-wide variables
V_dim = self.variables["Terminal voltage [V]"]
eta_e_av = self.variables.get("X-averaged electrolyte ohmic losses", 0)
eta_c_av = self.variables.get("X-averaged concentration overpotential", 0)
eta_e_av_dim = self.variables.get("X-averaged electrolyte ohmic losses [V]", 0)
eta_c_av_dim = self.variables.get(
"X-averaged concentration overpotential [V]", 0
)
num_cells = pybamm.Parameter(
"Number of cells connected in series to make a battery"
)
self.variables.update(
{
"X-averaged battery open circuit voltage [V]": ocv_av_dim * num_cells,
"Measured battery open circuit voltage [V]": ocv_dim * num_cells,
"X-averaged battery reaction overpotential [V]": eta_r_av_dim
* num_cells,
"X-averaged battery solid phase ohmic losses [V]": delta_phi_s_av_dim
* num_cells,
"X-averaged battery electrolyte ohmic losses [V]": eta_e_av_dim
* num_cells,
"X-averaged battery concentration overpotential [V]": eta_c_av_dim
* num_cells,
"Battery voltage [V]": V_dim * num_cells,
}
)
# Variables for calculating the equivalent circuit model (ECM) resistance
# Need to compare OCV to initial value to capture this as an overpotential
ocv_init = self.param.U_p(
self.param.c_p_init(1), self.param.T_init
) - self.param.U_n(self.param.c_n_init(0), self.param.T_init)
ocv_init_dim = (
self.param.U_p_ref
- self.param.U_n_ref
+ self.param.potential_scale * ocv_init
)
eta_ocv = ocv - ocv_init
eta_ocv_dim = ocv_dim - ocv_init_dim
# Current collector current density for working out euiqvalent resistance
# based on Ohm's Law
i_cc = self.variables["Current collector current density"]
i_cc_dim = self.variables["Current collector current density [A.m-2]"]
# Gather all overpotentials
v_ecm = -(eta_ocv + eta_r_av + eta_c_av + eta_e_av + delta_phi_s_av)
v_ecm_dim = -(
eta_ocv_dim
+ eta_r_av_dim
+ eta_c_av_dim
+ eta_e_av_dim
+ delta_phi_s_av_dim
)
# Current collector area for turning resistivity into resistance
A_cc = self.param.A_cc
self.variables.update(
{
"Change in measured open circuit voltage": eta_ocv,
"Change in measured open circuit voltage [V]": eta_ocv_dim,
"Local ECM resistance": v_ecm / (i_cc * A_cc),
"Local ECM resistance [Ohm]": v_ecm_dim / (i_cc_dim * A_cc),
}
)
# Cut-off voltage
voltage = self.variables["Terminal voltage"]
self.events.append(
pybamm.Event(
"Minimum voltage",
voltage - self.param.voltage_low_cut,
pybamm.EventType.TERMINATION,
)
)
self.events.append(
pybamm.Event(
"Maximum voltage",
voltage - self.param.voltage_high_cut,
pybamm.EventType.TERMINATION,
)
)
# Power
I_dim = self.variables["Current [A]"]
self.variables.update({"Terminal power [W]": I_dim * V_dim})
def set_soc_variables(self):
"""
Set variables relating to the state of charge.
This function is overriden by the base battery models
"""
pass
def process_parameters_and_discretise(self, symbol, parameter_values, disc):
"""
Process parameters and discretise a symbol using supplied parameter values
and discretisation. Note: care should be taken if using spatial operators
on dimensional symbols. Operators in pybamm are written in non-dimensional
form, so may need to be scaled by the appropriate length scale. It is
recommended to use this method on non-dimensional symbols.
Parameters
----------
symbol : :class:`pybamm.Symbol`
Symbol to be processed
parameter_values : :class:`pybamm.ParameterValues`
The parameter values to use during processing
disc : :class:`pybamm.Discretisation`
The discrisation to use
Returns
-------
:class:`pybamm.Symbol`
Processed symbol
"""
# Set y slices
if disc.y_slices == {}:
variables = list(self.rhs.keys()) + list(self.algebraic.keys())
disc.set_variable_slices(variables)
# Set boundary condtions (also requires setting parameter values)
if disc.bcs == {}:
self.boundary_conditions = parameter_values.process_boundary_conditions(
self
)
disc.bcs = disc.process_boundary_conditions(self)
# Process
param_symbol = parameter_values.process_symbol(symbol)
disc_symbol = disc.process_symbol(param_symbol)
return disc_symbol