-
Notifications
You must be signed in to change notification settings - Fork 11
/
bat_can_init.py
121 lines (99 loc) · 3.93 KB
/
bat_can_init.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
# bat_can_init.py
"""
BatCan - Battery Modeling in Cantera
Initialize the battery simulation.
Initialize variables, parameters, and Cantera objects for the simulation.
- Reads user inputs.
- Creates necessary Cantera objects.
- Reads and stores necessary parameters.
- Initializes the solution vector
Returns:
- Initial solution vector SV_0
- Dictionary of cantera objects obj
- Dictionary of paramters params
- Dictionary of pointers which allow the simulation to located needed terms.
"""
def initialize(input_file=None, fit=False):
# Import necessary modules:
from ruamel.yaml import YAML
from pathlib import Path
import cantera as ct
import numpy as np
import importlib
"""
=========================================================================
READ IN INPUTS
=========================================================================
"""
path = Path(input_file)
yaml = YAML(typ='safe')
inputs = yaml.load(path)
#===========================================================================
# LOAD ANODE / WORKING ELECTRODE
#===========================================================================
if 'anode' in inputs['cell-description']:
anode_inputs = inputs['cell-description']['anode']
else:
raise ValueError('Please specify an anode type.')
#===========================================================================
# LOAD SEPARATOR
#===========================================================================
if 'separator' in inputs['cell-description']:
sep_inputs = inputs['cell-description']['separator']
else:
# Generic separator:
sep_inputs = 'separator'
#===========================================================================
# LOAD CATHODE / COUNTER ELECTRODE
#===========================================================================
if 'cathode' in inputs['cell-description']:
cathode_inputs = inputs['cell-description']['cathode']
else:
raise ValueError('Please specify a cathode type.')
#===========================================================================
# LOAD SIMULATION PARAMETERS
#===========================================================================
if 'parameters' in inputs:
parameters = inputs['parameters']
# Read T and P and convert units to K and Pa, as necessary:
parameters['T'], parameters['P'] = read_conditions(parameters)
else:
raise ValueError('Please specify simulation parameters.')
if fit:
fit_parameters = inputs['fit-parameters']
return (anode_inputs, sep_inputs, cathode_inputs, parameters,
fit_parameters)
else:
return anode_inputs, sep_inputs, cathode_inputs, parameters
def read_conditions(params):
# Read the pressure from the input paramters and convert units to Pa,
# if necessary.
# Read in the user inputs:
temperature, pressure = params['T'], params['P']
# Split the values from the units:
pres, p_units = pressure.split()
temp, T_units = temperature.split()
# Convert values to floats:
temp = float(temp)
pres = float(pres)
# Read the temperature units and convert to K as necessary:
if T_units=="C":
temp += 273.15
elif T_units=="F":
temp = (temp + 459.67)* 5. / 9.
elif T_units!="K":
ValueError("Please provide temperature in an accepted unit:",
" C, K, or F.")
# Read the pressure units and convert to Pa as necessary:
if p_units=="atm":
pres *= 101325
elif p_units=="kPa":
pres *= 1.e3
elif p_units=="MPa":
pres *= 1.e6
elif p_units=="bar":
pres *= 1.e5
elif p_units!="Pa":
ValueError("Please provide pressure in an accepted unit: Pa, kPa,",
" MPa, atm, or bar.")
return temp, pres