How can I add curves to the current profile?

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()

Not sure about Python manipulating that precise curvature transition. Note that there is a radius associated with the curvature that you wish to include between the circles on both ends and the flat horizontal column. There is also a starting point on the circles from which you wish those curvature transitions to begin.

You can use FreeCAD, however, for simplicity.

Hello Paul,
Thank you for your reply. No, I can’t use FreeCAD. I should have this profile because it represents the shape of a target, which I will simulate later.

I think Python can handle these curvatures. For sure, there must be functions to create them. As for the coordinates (starting point, endpoint, radius), they are not critical—I can adjust them once I figure out how to make the transition between the circular ends and the flat section.

Best regards,

You are using matplotlib.pyplot tool suite. Here is the documentation:

https://matplotlib.org/3.5.3/api/_as_gen/matplotlib.pyplot.fill_between.html

There are a few tools alluding to “filling” between curves. You might have to define points from the original circles and horizontal column as shown here for best results.