2D Interpolation liefert Ausreißer

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
MarcoG
User
Beiträge: 1
Registriert: Montag 27. März 2023, 11:18

Hallo Leute, ich habe folgendes Problem:
Ich habe einen Code, der das Verhalten einer Batterie nachbilden soll. Dazu lese ich folgende Daten ein:

Temperature Capacity OCV
0 -450 4,10
0 -600 4,0
0 -1200 3,55
0 .. ...
0 -6000 ...
0 ... ...
0 -12000 2,99
15 -600 4,06
15 -900 ...
15 ... ...
15 ... ...
15 -10300 2,66
25 -420 4,10
25 -1000 ...
25 ... ...
25 ... ...
25 -12300 2,6

Die OCV Werte werden nach der Kapazität und der Temperatur interpoliert. Dies funktioniert gut, wenn ich nur zwei dataframes mit concat() miteinander verbinde, jedoch nicht wenn ich drei dataframes ( output_T0, output_T15 und output_T25) miteinander verbinde.
Ich bin froh um jegliche Hilfe in diesem Bereich.

Ich bin leider neu hier und ein Bild einfügen funktioniert irgendiwe nicht, hier noch der Artikel von stack overflow zu meiner Frage: https://stackoverflow.com/questions/757 ... -more-than

Mein Code den ich soweit habe:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.interpolate import griddata, LinearNDInterpolator


'''
Define the Simulation Parameters:
dt = Timestep for calculation
Start_SOC = State of Charge of the battery at the beginning of the cycle
battery_capacity = Capacity of the Cell
temp = Temperature i want to look at
s = Cells in series of the module
p = Cells in parallel of the module
'''

dt = 2
start_soc = 80
cell_capacity = 3320 #mAh
temp = 25

s=1
p=1

#-------------------------------------------------------------------------------------------------------------------------------------------------------#
#-------------------------------------------------------------------------------------------------------------------------------------------------------#

'''import the current_data file'''

current_file= pd.read_csv("current_input.csv", sep=";")
current_file.fillna(0, inplace=True) # none = 0

#----------------------------------------------------------------------------------------------------------------------------------------------------#
#----------------------------------------------------------------------------------------------------------------------------------------------------#
'''Calculate the start parameters of the simulation and create the function to interpolate the data'''

position = -(1 - start_soc/100) * cell_capacity * 3.6
V0 = 0
V1 = 0
results = pd.DataFrame()

#---------------------------------------------------------------------------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------------------------------------------------------------------------#
output_T0 = pd.read_csv("output_T0.csv")
output_T25 = pd.read_csv("output_T25.csv")
output_T15 = pd.read_csv("output_T15.csv")
output_T0["Temperature"] = 0
output_T25["Temperature"] = 25
output_T15["Temperature"] = 15

combined_df = pd.concat([output_T0,output_T25,output_T15],axis = 0, ignore_index = True)

capacity_vals = combined_df["Capacity (As)"].values
temperature_vals = combined_df["Temperature"].values


new_capacity = np.linspace(min(capacity_vals), max(capacity_vals), 1000) #Create a new range for the capacity, default: 1000 entries
new_temperature = np.linspace(min(temperature_vals), max(temperature_vals), 1000) #Create a new range for the temperature, default: 1000 entries
new_capacity_mesh, new_temperature_mesh = np.meshgrid(new_capacity, new_temperature) #create the mesh onto which the values are interpolated


new_ocv_mesh = griddata((capacity_vals, temperature_vals), combined_df["OCV (Volts)"].values, (new_capacity_mesh, new_temperature_mesh), method='linear')
new_rint_mesh = griddata((capacity_vals, temperature_vals), combined_df["Rint (Ohm)"].values, (new_capacity_mesh, new_temperature_mesh), method='linear')
new_r0_mesh = griddata((capacity_vals, temperature_vals), combined_df["R0 (Ohm)"].values, (new_capacity_mesh, new_temperature_mesh), method='linear')
new_r1_mesh = griddata((capacity_vals, temperature_vals), combined_df["R1 (Ohm)"].values, (new_capacity_mesh, new_temperature_mesh), method="linear")
new_c0_mesh = griddata((capacity_vals, temperature_vals), combined_df["C0 (Farad)"].values, (new_capacity_mesh, new_temperature_mesh), method='linear')
new_c1_mesh = griddata((capacity_vals, temperature_vals), combined_df["C1 (Farad)"].values, (new_capacity_mesh, new_temperature_mesh), method='linear')

'''Define the function that gives back the battery Parameters at a certain point'''

def get_battery_params(capacity,temperature):

capacity_idx = np.abs(new_capacity - (position)).argmin()
temperature_idx = np.abs(new_temperature - (temperature)).argmin()
battery_params ={
"Rint": new_rint_mesh[temperature_idx][capacity_idx],
"R0": new_r0_mesh[temperature_idx][capacity_idx],
"R1": new_r1_mesh[temperature_idx][capacity_idx],
"C0": new_c0_mesh[temperature_idx][capacity_idx],
"C1": new_c1_mesh[temperature_idx][capacity_idx],
"OCV": new_ocv_mesh[temperature_idx][capacity_idx],
}

return battery_params

#---------------------------------------------------------------------------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------------------------------------------------------------------------#

for n in range(len(current_file)):

'''This section loops through the current input file and extracts the corresponding current for each time interval.
It calculate the steps i want to look at and loops through all the timesteps, extracting the values for the calculation at the current position
and temperature. It then calculates the RC Response and the terminal Voltage, as well as the losses in the module and saves them into a results dataframe
'''
current = current_file["Current (A)"][n]/p


if results.empty:
time_interval = current_file["Time (sec)"][n]
else:
time_interval = abs(current_file["Time (sec)"][n] - current_file["Time (sec)"][n-1])

steps = int(time_interval/dt)

for i in range(steps):


R0 = get_battery_params(position,temp)["R0"]
R1 = get_battery_params(position,temp)["R1"]
C0 = get_battery_params(position,temp)["C0"]
C1 = get_battery_params(position,temp)["C1"]
Rint = get_battery_params(position,temp)["Rint"]
OCV = get_battery_params(position,temp)["OCV"]

V0 =(np.exp(-dt/(R0*C0)) * V0)+ R0 * (1-np.exp(-dt/(R0*C0)))*current #First RC Branch
V1 = (np.exp(-dt/(R1*C1)) * V1)+ R1 * (1-np.exp(-dt/(R1*C1)))*current # Second RC Branch

Vt = (OCV - Rint * current - V0 - V1)*s
Ploss = abs((OCV*s-Vt)*current*p)
end_position = position + (current*dt)

new_row = {
"V0 (Volts)": V0,
"V1 (Volts)": V1,
"Vt (Volts)": Vt,
"End Position": end_position,
"OCV (Volts)": OCV,
"Ploss": Ploss,
"Rint":Rint,
"C0":C0,
"C1":C1

}

results = pd.concat([results, pd.DataFrame([new_row])], ignore_index=True)
position = end_position
#--------------------------------------------------------------------------------------------------------------------------------------------------------#
#--------------------------------------------------------------------------------------------------------------------------------------------------------#
#plt.contourf(new_capacity_mesh, new_temperature_mesh,new_c1_mesh)


fig, axs = plt.subplots(3, figsize = (10,15))
axs[0].plot(results.index.values,results['Vt (Volts)'].values)
axs[1].plot(results.index.values,results["C0"].values)
axs[1].plot(results.index.values,results["C1"].values)
axs[2].plot(results.index.values,results["Rint"].values)
Antworten