I am new to using Python, and I wrote the following code to calculate the target density. The problem I have is that I need to achieve a smooth transition, or a “bridge,” between the circular ends and the flat (rectangular) section, as shown in the picture below.
Any information or suggestions would be greatly appreciated.
Thank you in advance.
import numpy as np
import matplotlib.pyplot as plt
# Define parameters
targ_rod_thickness = 0.6 # Thickness of the flat section in microns
targ_length = 25 # Total length of the target in microns
targ_ball_radius = 2 # Radius of the spherical balls in microns
critical_density = 1.73e27 # Critical density per m^3
targ_dens = 30 * critical_density # Density value for Hydrogen target
# ############### Grid resolution ###############
dx = 0.01 # Resolution
x = np.arange(-5, targ_length + 5 + dx, dx) # Positive x-coordinates
y = np.arange(-4 * targ_rod_thickness, 4 * targ_rod_thickness + dx, dx) # y-coordinates for thickness range
X, Y = np.meshgrid(x, y) # A 2D grid of coordinates
# ############# Initialize density matrix #############
density = np.zeros_like(X)
# Density for Flat Section (Central Rod)
for i in range(X.size):
if (-targ_rod_thickness <= Y.flat[i] <= targ_rod_thickness and
targ_ball_radius <= X.flat[i] <= (targ_length - targ_ball_radius)):
density.flat[i] = targ_dens # Set density for the rod region
else:
density.flat[i] = 0 # Otherwise, keep density as 0
# ################### Density for Circular Ends #####################
# Right end circle
ball_right_condition = ((X - (targ_length - targ_ball_radius))**2 + Y**2) <= targ_ball_radius**2
density[ball_right_condition] = targ_dens
# Left end circle
ball_left_condition = ((X - targ_ball_radius)**2 + Y**2) <= targ_ball_radius**2
density[ball_left_condition] = targ_dens
# ################### Plot the Target #####################
plt.figure()
plt.contourf(X, Y, density, levels=40, cmap='Blues') # Filled contour plot
plt.axis('equal')
plt.xlabel('X (µm)')
plt.ylabel('Y (µm)')
plt.title('Gas Jet Target')
plt.colorbar(label='Density')
plt.show()