Issue
My outputs T_results and c_results have values of 0 at the end, which I do not want so I sliced theses arrays excluding their last column and got their respective arrays T_results_cleaned = T_results[:, :-1]
and c_results_cleaned = c_results[:, :-1]
. However,now when I plot with these sliced arrays, I get the following error message TypeError: Dimensions of C (50, 49) should be one smaller than X(50) and Y(50) while using shading='flat' see help(pcolormesh)
Below is my full code that is solving a set of equation using finite differences.
import numpy as np
import matplotlib.pyplot as plt
import time
import pandas as pd
nz = 50
dz = 1 / nz
nt = 50
t = np.linspace(0, 4, nt)
p = np.array([0.01, 0.5, 1.0])
# Initialize concentration and temperature arrays
C = np.zeros(nz)
T = np.zeros(nz)
# Set initial condition for concentration
C[0] = 1.0
# Initialize arrays to store results
c_results = np.zeros((nt, nz))
T_results = np.zeros((nt, nz))
# Define the Arrhenius rate constant function
def k(temperature):
arrh_expr = 4.68 * np.exp(-806 / ((600 - 273) * temperature + 273))
return arrh_expr
# Calculate time derivative at specific spatial points
def rhsC(c0, c1, c2, T1, dz, p):
return (p[0] * (c2 - 2 * c1 + c0) / dz**2 -
p[1] * (c2 - c0) / (2 * dz) -
p[2] * k(T1) * c1**2)
def rhsT(T0, T1, T2, c1, dz, p):
return (0.01 * (T2 - 2 * T1 + T0) / dz**2 -
0.5 * (T2 - T0) / (2 * dz) +
2.45 * k(T1) * c1**2)
# Time-stepping loop
for it in range(nt):
c_results[it, :] = C
T_results[it, :] = T
T_results_cleaned = T_results[:, :-1] #select all rows and all columns except the last column
c_results_cleaned = c_results[:, :-1] #select all rows and all columns except the last column
for iz in range(1, nz - 1):
# Calculate concentration and temperature derivatives
dc_dt = rhsC(C[iz - 1], C[iz], C[iz + 1], T[iz], dz, p)
dT_dt = rhsT(T[iz - 1], T[iz], T[iz + 1], C[iz], dz, p)
# Update concentration and temperature using forward Euler
C[iz] += dz * dc_dt
T[iz] += dz * dT_dt
# Create a mesh for plotting
z = np.linspace(0, 1, nz)
# Plot concentration and temperature profiles
plt.pcolormesh(z, t, c_results_cleaned)
plt.colorbar(label='Concentration')
plt.xlabel('z')
plt.ylabel('t')
plt.title('Concentration Profile')
plt.show()
plt.pcolormesh(z, t, T_results_cleaned)
plt.colorbar(label='Temperature')
plt.xlabel('z')
plt.ylabel('t')
plt.title('Temperature Profile')
plt.show()
I tried slicing the array of z and t in the plt module as well,
plt.pcolormesh(z[:-1], t[:-1], c_results_cleaned)
to match the dimensions but that did not work as well. How can I solve this problem?
Solution
To expand on the comments and if I understood your problem correctly, the plot will either work if use the full data:
plt.pcolormesh(z, t, c_results)
But as I understand, you're concerned with the column of zeros at the end. If you have to remove that, you'll also need to remove a row from the z
variable like this:
plt.pcolormesh(z[:-1], t, c_results_cleaned)
To get a plot like this:
Answered By - Suraj Shourie
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.