def initialize_ode_solver(self, y_0, yd_0, t_0): model = Implicit_Problem(self.residual, y_0, yd_0, t_0) model.handle_result = self.handle_result solver = IDA(model) solver.rtol = self.solver_rtol solver.atol = self.solver_atol # * np.array([100, 10, 1e-4, 1e-4]) solver.inith = 0.1 # self.wind.R_g / const.C solver.maxh = self.dt * self.wind.R_g / const.C solver.report_continuously = True solver.display_progress = False solver.verbosity = 50 # 50 = quiet solver.num_threads = 3 # solver.display_progress = True return solver
def main(): res_class = eval(inputs.test_type) # plt.close('all') t_count = time.time() SV_0 = sol_init.SV_0 SV_dot_0 = np.zeros_like(SV_0) t_0 = 0. t_f = 3600. / inputs.C_rate #63006.69900049 93633 algvar = sol_init.algvar atol = np.ones_like(SV_0) * 1e-6 atol[cat.ptr_vec['eps_S8']] = 1e-15 atol[cat.ptr_vec['eps_Li2S']] = 1e-15 atol[cat.ptr_vec['rho_k_el']] = 1e-16 # 1e-16 for Bessler rtol = 1e-6 sim_output = 50 rtol_ch = 1e-6 atol_ch = np.ones_like(SV_0) * 1e-6 atol_ch[cat.ptr_vec['eps_S8']] = 1e-15 atol_ch[cat.ptr_vec['eps_Li2S']] = 1e-15 atol_ch[cat.ptr_vec['rho_k_el']] = 1e-30 rate_tag = str(inputs.C_rate) + "C" # if 'cascade' in inputs.ctifile: # ncols = 2 + inputs.flag_req # else: ncols = 1 + inputs.flag_req fig, axes = plt.subplots(sharey="row", figsize=(9, 12), nrows=3, ncols=(ncols) * inputs.n_cycles, num=1) plt.subplots_adjust(wspace=0.15, hspace=0.4) fig.text(0.35, 0.85, rate_tag, fontsize=20, bbox=dict(facecolor='white', alpha=0.5)) # Set up user function to build figures based on inputs "----------Equilibration----------" print('\nEquilibrating...') # Set external current to 0 for equilibration cat.set_i_ext(0) # cat.nucleation_flag = 1 # Create problem object bat_eq = res_class(res_class.res_fun, SV_0, SV_dot_0, t_0) bat_eq.external_event_detection = True bat_eq.algvar = algvar # Create simulation object sim_eq = IDA(bat_eq) sim_eq.atol = atol sim_eq.rtol = rtol sim_eq.verbosity = sim_output sim_eq.make_consistent('IDA_YA_YDP_INIT') t_eq, SV_eq, SV_dot_eq = sim_eq.simulate(t_f) # Put solution into pandas dataframe with labeled columns SV_eq_df = label_columns(t_eq, SV_eq, an.npoints, sep.npoints, cat.npoints) # Obtain tag strings for dataframe columns tags = tag_strings(SV_eq_df) # plot_sim(tags, SV_eq_df, 'Equilibrating', 0, fig, axes) t_equilibrate = time.time() - t_count print("Equilibration time = ", t_equilibrate, '\n') print('Done equilibrating\n') for cycle_num in np.arange(0, inputs.n_cycles): "------------Discharging-------------" print('Discharging...') # cat.np_L = inputs.np_Li2S_init cat.nucleation_flag = np.zeros((inputs.npoints_cathode, 1)) # New initial conditions from previous simulation if cycle_num == 0: # SV_0 = SV_0 SV_0 = SV_eq[-1, :] # SV_dot_0 = SV_dot_0 SV_dot_0 = SV_dot_eq[-1, :] else: SV_0 = SV_0_cycle SV_dot_0 = SV_dot_0_cycle # Set external current cat.set_i_ext(cat.i_ext_amp) # Update problem instance initial conditions bat_dch = res_class(res_class.res_fun, SV_0, SV_dot_0, t_0) bat_dch.external_event_detection = True bat_dch.algvar = algvar # Re-initialize simulation object sim_dch = IDA(bat_dch) sim_dch.atol = atol sim_dch.rtol = rtol # sim_dch.maxh = 57 sim_dch.verbosity = sim_output sim_dch.make_consistent('IDA_YA_YDP_INIT') t_dch, SV_dch, SV_dot_dch = sim_dch.simulate(131865.32) SV_dch_df = label_columns(t_dch, SV_dch, an.npoints, sep.npoints, cat.npoints) # Obtain tag strings for dataframe columns tags = tag_strings(SV_dch_df) plot_sim(tags, SV_dch_df, 'Discharging', 0 + 2 * cycle_num, fig, axes) # plot_meanPS(SV_dch_df, tags, 'Discharging') print('Done Discharging\n') "--------Re-equilibration---------" if inputs.flag_req == 1: print('Re-equilibrating...') # New initial conditions from previous simulation SV_0 = SV_dch[-1, :] SV_dot_0 = SV_dot_dch[-1, :] # Set external current cat.set_i_ext(0) # Update problem instance initial conditions bat_req = res_class(res_class.res_fun, SV_0, SV_dot_0, t_0) bat_req.external_event_detection = True bat_req.algvar = algvar # Re-initialize simulation object sim_req = IDA(bat_req) sim_req.atol = atol sim_req.rtol = rtol sim_req.verbosity = sim_output sim_req.make_consistent('IDA_YA_YDP_INIT') t_req, SV_req, SV_dot_req = sim_req.simulate(t_f) SV_req_df = label_columns(t_req, SV_req, an.npoints, sep.npoints, cat.npoints) # plot_sim(tags, SV_req_df, 'Re-Equilibrating', 1, fig, axes) print('Done re-equilibrating\n') else: SV_req = SV_dch SV_dot_req = SV_dot_dch "-----------Charging-----------" if 'dog' in inputs.ctifile: print('Charging...') SV_0 = SV_req[-1, :] SV_dot_0 = SV_dot_req[-1, :] SV_0[cat.ptr_vec['eps_S8']] = cat.eps_cutoff cat.set_i_ext(-cat.i_ext_amp) # Update problem instance initial conditions bat_ch = res_class(res_class.res_fun, SV_0, SV_dot_0, t_0) bat_ch.external_event_detection = True bat_ch.algvar = algvar # Re-initialize simulation object sim_ch = IDA(bat_ch) sim_ch.atol = atol_ch sim_ch.rtol = rtol_ch if cycle_num > 0: sim_ch.maxh = 0.1 sim_ch.verbosity = sim_output sim_ch.make_consistent('IDA_YA_YDP_INIT') t_ch, SV_ch, SV_dot_ch = sim_ch.simulate(t_f) SV_ch_df = label_columns(t_ch, SV_ch, an.npoints, sep.npoints, cat.npoints) plot_sim(tags, SV_ch_df, 'Charging', 1 + inputs.flag_req + 2 * cycle_num, fig, axes) plot_meanPS(SV_ch_df, tags, 'Charging') SV_ch_df = 0 # # print('Max S_8(e) concentration = ', max(SV_ch[:, 6])) # SV_0_cycle = SV_ch[-1, :] # SV_dot_0_cycle = SV_ch[-1, :] print('Done Charging\n') exp_data_01C = pd.read_csv(r'0.1C Data.csv', header=None) exp_data_05C = pd.read_csv(r'0.5C Data.csv', header=None) exp_data_1C = pd.read_csv(r'1C Data.csv', header=None) Bessler = pd.read_csv(r'Bessler Dennis Data.csv', header=None) SV_copy = SV_dch_df.copy() SV_copy.loc[:, 'Time'] *= -cat.i_ext_amp * inputs.A_cat / 3600 / ( cat.m_S_tot_0) "Set up your figure" fig = plt.figure(3) ax = fig.add_axes([0.2, 0.2, 0.6, 0.75]) fig.set_size_inches((10., 5.0)) # ax2 = ax.twinx() "Formatting for the figure:" fs = 20 #font size for plots lw = 3.0 #line width for plots # font = plt.matplotlib.font_manager.FontProperties(family='Times New Roman',size=fs-1) for tick in ax.xaxis.get_major_ticks(): tick.label1.set_fontsize(fs) tick.label1.set_fontname('Times New Roman') for tick in ax.yaxis.get_major_ticks(): tick.label1.set_fontsize(fs) tick.label1.set_fontname('Times New Roman') color = matplotlib.cm.plasma(inputs.C_rate) p1, = plt.plot(SV_copy.loc[:, 'Time'], SV_copy.loc[:, 'Phi_ed1'], 'k-', linewidth=lw) # p2, = plt.plot(exp_data_01C.iloc[:,0], exp_data_01C.iloc[:,1], 'ro') # p3, = plt.plot(exp_data_05C.iloc[:,0], exp_data_05C.iloc[:,1], 'co') # p4, = plt.plot(exp_data_1C.iloc[:,0], exp_data_1C.iloc[:,1], 'ko') p5, = plt.plot(Bessler.iloc[:, 0], Bessler.iloc[:, 1], 'mo', ms=4) plt.xlim((0, 1700)) plt.xticks([0, 400, 800, 1200, 1600]) plt.ylim((1.8, 2.6)) plt.ylabel(r'Cell Voltage $[\mathrm{V}]$', fontstyle='normal', fontname='Times new Roman', fontsize=fs + 2, labelpad=5.0) plt.xlabel( r'Capacity $[\mathrm{Ah} \hspace{0.5} \mathrm{kg}^{-1}_{\mathrm{sulfur}}]$', fontstyle='normal', fontname='Times new Roman', fontsize=fs + 2, labelpad=5.0) # plt.legend(["Discharge", "Charge"]) file_name_dch = 'dch' + str(inputs.C_rate) + "C_" + inputs.mech + '.csv' SV_dch = SV_dch_df.copy() SV_dch.loc[:, 'Time'] *= -cat.i_ext_amp * inputs.A_cat / 3600 / (cat.m_S_0 + cat.m_S_el) SV_dch.to_csv(file_name_dch, index=False, header=True) t_elapsed = time.time() - t_count print('t_cpu=', t_elapsed, '\n') return SV_eq_df, SV_dch_df, SV_ch_df, tags
# Volume fraction of SEI per cell res[-1] = SV_dot[-1] return res """------------------------------------------------------------------------""" # Set up problem instance SEI_1D = Implicit_Problem(residual, SV_0, SV_dot_0, t_0) # Define simulation parameters simulation = IDA(SEI_1D) # Create simulation instance simulation.atol = 1e-6 # Solver absolute tolerance simulation.rtol = 1e-6 # Solver relative tolerance simulation.maxh = 0.1 # Solver max step size # Set simulation end time, slope flag (for anode voltage cycle), and run simulation t_f = ((phi_bounds[1] - phi_anode_0)/R)*5 #ncp_list = np.arange(0, t_f, 0.15) #ncp = 10000 # Run simulation t, SV, SV_dot = simulation.simulate(t_f) # %% Organize data and plot SV_df = pd.DataFrame(SV)
def main(): res_class = eval(inputs.test_type) # plt.close('all') t_count = time.time() SV_0 = sol_init.SV_0 SV_dot_0 = np.zeros_like(SV_0) t_0 = 0. t_f = 3600. / inputs.C_rate algvar = sol_init.algvar atol = np.ones_like(SV_0) * 1e-5 atol[cat.ptr_vec['eps_S8']] = 1e-25 atol[cat.ptr_vec['eps_Li2S']] = 1e-25 atol[cat.ptr_vec['rho_k_el']] = 1e-25 # atol = 1e-30; rtol = 1e-4 sim_output = 50 rate_tag = str(inputs.C_rate) + "C" fig, axes = plt.subplots(sharey="row", figsize=(9, 12), nrows=3, ncols=1) plt.subplots_adjust(wspace=0.15, hspace=0.4) fig.text(0.15, 0.8, rate_tag, fontsize=20, bbox=dict(facecolor='white', alpha=0.5)) # Set up user function to build figures based on inputs "----------Equilibration----------" print('\nEquilibrating...') # Set external current to 0 for equilibration cat.set_i_ext(0) # Create problem object bat_eq = res_class(res_class.res_fun, SV_0, SV_dot_0, t_0) bat_eq.external_event_detection = True bat_eq.algvar = algvar # Create simulation object sim_eq = IDA(bat_eq) sim_eq.atol = atol sim_eq.rtol = rtol sim_eq.verbosity = sim_output sim_eq.make_consistent('IDA_YA_YDP_INIT') t_eq, SV_eq, SV_dot_eq = sim_eq.simulate(t_f) # Put solution into pandas dataframe with labeled columns SV_eq_df = label_columns(t_eq, SV_eq, an.npoints, sep.npoints, cat.npoints) # SV_eq_df = [] # Obtain tag strings for dataframe columns tags = tag_strings(SV_eq_df) # plot_sim(tags, SV_eq_df, 'Equilibrating', 0, fig, axes) # print(SV_eq_df[tags['rho_el'][4:10]].iloc[-1]) print('Done equilibrating\n') "------------Discharging-------------" print('Discharging...') # New initial conditions from previous simulation SV_0 = SV_eq[-1, :] SV_dot_0 = SV_dot_eq[-1, :] # Set external current cat.set_i_ext(cat.i_ext_amp) # Update problem instance initial conditions bat_dch = res_class(res_class.res_fun, SV_0, SV_dot_0, t_0) bat_dch.external_event_detection = True bat_dch.algvar = algvar # Re-initialize simulation object sim_dch = IDA(bat_dch) sim_dch.atol = atol sim_dch.rtol = rtol sim_dch.maxh = 5 sim_dch.verbosity = sim_output sim_dch.make_consistent('IDA_YA_YDP_INIT') t_dch, SV_dch, SV_dot_dch = sim_dch.simulate(t_f) # if hasattr(cathode, 'get_tflag'): # t_flag_ch = cathode.get_tflag SV_dch_df = label_columns(t_dch, SV_dch, an.npoints, sep.npoints, cat.npoints) # SV_dch_df = [] # Obtain tag strings for dataframe columns # tags = tag_strings(SV_ch_df) plot_sim(tags, SV_dch_df, 'Discharging', 1, fig, axes) plot_meanPS(SV_dch_df, tags) print('Done Discharging\n') "--------Re-equilibration---------" # if inputs.flag_req == 1: # # print('Re-equilibrating...') # # # New initial conditions from previous simulation # SV_0 = SV_ch[-1, :] # SV_dot_0 = SV_dot_ch[-1, :] # # # Set external current # cat.set_i_ext(0) # # # Update problem instance initial conditions # bat_req = res_class(res_class.res_fun, SV_0, SV_dot_0, t_0) # bat_req.external_event_detection = True # bat_req.algvar = algvar # # # Re-initialize simulation object # sim_req = IDA(bat_req) # sim_req.atol = atol # sim_req.rtol = rtol # sim_req.verbosity = sim_output # sim_req.make_consistent('IDA_YA_YDP_INIT') # # t_req, SV_req, SV_dot_req = sim_req.simulate(t_f) # # SV_req_df = label_columns(t_req, SV_req, an.npoints, sep.npoints, cat.npoints) # ## plot_sim(tags, SV_req_df, 'Re-Equilibrating', 2-1, fig, axes) # # print('Done re-equilibrating\n') # else: # SV_req = SV_ch # SV_dot_req = SV_dot_ch "-----------Charging-----------" # print('Charging...') # # SV_0 = SV_req[-1, :] # SV_dot_0 = SV_dot_req[-1, :] # # cat.set_i_ext(-cat.i_ext_amp) # # # Update problem instance initial conditions # bat_dch = res_class(res_class.res_fun, SV_0, SV_dot_0, t_0) # bat_dch.external_event_detection = True # bat_dch.algvar = algvar # # # Re-initialize simulation object # sim_dch = IDA(bat_dch) # sim_dch.atol = atol # sim_dch.rtol = rtol # sim_dch.verbosity = sim_output # sim_dch.make_consistent('IDA_YA_YDP_INIT') # # t_dch, SV_dch, SV_dot_dch = sim_dch.simulate(t_f) # ## if hasattr(cathode, 'get_tflag'): ## t_flag_dch = cathode.get_tflag # # SV_dch_df = label_columns(t_dch, SV_dch, an.npoints, sep.npoints, cat.npoints) # # plot_sim(tags, SV_dch_df, 'Charging', 3, fig, axes) # # print('Done Charging\n') t_elapsed = time.time() - t_count print('t_cpu=', t_elapsed, '\n') return SV_eq_df, SV_dch_df, tags #SV_eq_df, SV_req_df #, SV_dch_df