Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,41 +10,18 @@ load_dotenv()
|
|
10 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
11 |
|
12 |
# Functions for Antenna Calculations
|
13 |
-
def calculate_microstrip_patch(frequency, permittivity, thickness, tangent_loss
|
14 |
c = 3e8 # Speed of light in m/s
|
15 |
wavelength = c / frequency
|
16 |
effective_wavelength = wavelength / np.sqrt(permittivity)
|
17 |
patch_length = effective_wavelength / 2
|
18 |
patch_width = wavelength / (2 * np.sqrt(1 + permittivity))
|
19 |
-
|
20 |
-
# Adjustments for tangent loss (approximation)
|
21 |
-
adjustment_factor = 1 + tangent_loss / 10
|
22 |
-
patch_length *= adjustment_factor
|
23 |
-
patch_width *= adjustment_factor
|
24 |
-
|
25 |
-
# Consider impedance adjustment (this will be a simplified factor here)
|
26 |
-
if impedance == 50:
|
27 |
-
impedance_factor = 1.0
|
28 |
-
elif impedance == 73:
|
29 |
-
impedance_factor = 1.1
|
30 |
-
else:
|
31 |
-
impedance_factor = 1.0
|
32 |
-
|
33 |
-
patch_length *= impedance_factor
|
34 |
-
patch_width *= impedance_factor
|
35 |
-
|
36 |
return patch_length, patch_width, thickness
|
37 |
|
38 |
-
def
|
39 |
c = 3e8 # Speed of light in m/s
|
40 |
wavelength = c / frequency
|
41 |
-
dipole_length = wavelength / 2
|
42 |
-
|
43 |
-
# Consider impedance adjustments (simplified)
|
44 |
-
if impedance == 50:
|
45 |
-
dipole_length *= 1.0
|
46 |
-
elif impedance == 73:
|
47 |
-
dipole_length *= 1.05 # A minor adjustment for 73 ohms impedance
|
48 |
return dipole_length
|
49 |
|
50 |
def calculate_s11(frequency):
|
@@ -85,16 +62,6 @@ def plot_3d_microstrip_patch(patch_length, patch_width, thickness):
|
|
85 |
fig.update_layout(title="3D Microstrip Patch Antenna", showlegend=False)
|
86 |
return fig
|
87 |
|
88 |
-
def plot_3d_dipole_antenna(dipole_length):
|
89 |
-
fig = go.Figure()
|
90 |
-
fig.add_trace(go.Scatter3d(
|
91 |
-
x=[0, dipole_length], y=[0, 0], z=[0, 0], marker=dict(size=5, color='red'),
|
92 |
-
line=dict(color='red', width=4), name="Dipole Antenna"
|
93 |
-
))
|
94 |
-
fig.update_layout(title="3D Dipole Antenna", scene=dict(
|
95 |
-
xaxis_title='Length (m)', yaxis_title='Y', zaxis_title='Z'))
|
96 |
-
return fig
|
97 |
-
|
98 |
def plot_s11_graph(frequencies, s11_values):
|
99 |
fig = go.Figure()
|
100 |
fig.add_trace(go.Scatter(x=frequencies, y=s11_values, mode='lines', name="S11"))
|
@@ -121,7 +88,7 @@ def design_antenna(antenna_type, frequency, permittivity, thickness, tangent_los
|
|
121 |
frequencies = np.linspace(frequency - 0.5, frequency + 0.5, 100)
|
122 |
|
123 |
if antenna_type == "Microstrip Patch":
|
124 |
-
patch_length, patch_width, thickness = calculate_microstrip_patch(frequency_hz, permittivity, thickness, tangent_loss
|
125 |
s11_values = [calculate_s11(f) for f in frequencies]
|
126 |
directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
|
127 |
theta = np.linspace(-180, 180, 360)
|
@@ -137,12 +104,10 @@ def design_antenna(antenna_type, frequency, permittivity, thickness, tangent_los
|
|
137 |
f"Operating Frequency: {frequency:.2f} GHz\n"
|
138 |
f"S11 at Operating Frequency: {s11_values[len(s11_values)//2]:.2f} dB\n"
|
139 |
f"Patch Dimensions: {patch_length:.2f} m x {patch_width:.2f} m x {thickness:.2f} m\n"
|
140 |
-
f"
|
141 |
-
f"Input Impedance: {impedance} Ohms\n"
|
142 |
)
|
143 |
-
|
144 |
elif antenna_type == "Dipole":
|
145 |
-
dipole_length =
|
146 |
s11_values = [calculate_s11(f) for f in frequencies]
|
147 |
directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
|
148 |
theta = np.linspace(-180, 180, 360)
|
@@ -151,19 +116,15 @@ def design_antenna(antenna_type, frequency, permittivity, thickness, tangent_los
|
|
151 |
s11_graph = plot_s11_graph(frequencies, s11_values)
|
152 |
directivity_gain_graph = plot_directivity_and_gain(frequencies, directivities, gains)
|
153 |
radiation_graph = plot_radiation_pattern(theta, gain_pattern)
|
154 |
-
antenna_3d =
|
155 |
|
156 |
output = (
|
157 |
f"Design Type: Dipole Antenna\n"
|
158 |
f"Operating Frequency: {frequency:.2f} GHz\n"
|
159 |
f"S11 at Operating Frequency: {s11_values[len(s11_values)//2]:.2f} dB\n"
|
160 |
f"Dipole Length: {dipole_length:.2f} m\n"
|
161 |
-
f"Input Impedance: {impedance} Ohms
|
162 |
)
|
163 |
-
|
164 |
-
else:
|
165 |
-
output = "Currently, only Microstrip Patch and Dipole Antenna are supported."
|
166 |
-
s11_graph, directivity_gain_graph, radiation_graph, antenna_3d = None, None, None, None
|
167 |
|
168 |
return output, s11_graph, directivity_gain_graph, radiation_graph, antenna_3d
|
169 |
|
@@ -175,4 +136,18 @@ with gr.Blocks() as demo:
|
|
175 |
permittivity = gr.Number(value=4.4, label="Substrate Permittivity")
|
176 |
thickness = gr.Number(value=0.01, label="Substrate Thickness (m)")
|
177 |
tangent_loss = gr.Number(value=0.02, label="Substrate Tangent Loss (tan δ)", step=0.01)
|
178 |
-
impedance = gr.Dropdown([50, 73], label="Input Impedance (Ohms)", value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
11 |
|
12 |
# Functions for Antenna Calculations
|
13 |
+
def calculate_microstrip_patch(frequency, permittivity, thickness, tangent_loss):
|
14 |
c = 3e8 # Speed of light in m/s
|
15 |
wavelength = c / frequency
|
16 |
effective_wavelength = wavelength / np.sqrt(permittivity)
|
17 |
patch_length = effective_wavelength / 2
|
18 |
patch_width = wavelength / (2 * np.sqrt(1 + permittivity))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
return patch_length, patch_width, thickness
|
20 |
|
21 |
+
def calculate_dipole(frequency):
|
22 |
c = 3e8 # Speed of light in m/s
|
23 |
wavelength = c / frequency
|
24 |
+
dipole_length = wavelength / 2
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
return dipole_length
|
26 |
|
27 |
def calculate_s11(frequency):
|
|
|
62 |
fig.update_layout(title="3D Microstrip Patch Antenna", showlegend=False)
|
63 |
return fig
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
def plot_s11_graph(frequencies, s11_values):
|
66 |
fig = go.Figure()
|
67 |
fig.add_trace(go.Scatter(x=frequencies, y=s11_values, mode='lines', name="S11"))
|
|
|
88 |
frequencies = np.linspace(frequency - 0.5, frequency + 0.5, 100)
|
89 |
|
90 |
if antenna_type == "Microstrip Patch":
|
91 |
+
patch_length, patch_width, thickness = calculate_microstrip_patch(frequency_hz, permittivity, thickness, tangent_loss)
|
92 |
s11_values = [calculate_s11(f) for f in frequencies]
|
93 |
directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
|
94 |
theta = np.linspace(-180, 180, 360)
|
|
|
104 |
f"Operating Frequency: {frequency:.2f} GHz\n"
|
105 |
f"S11 at Operating Frequency: {s11_values[len(s11_values)//2]:.2f} dB\n"
|
106 |
f"Patch Dimensions: {patch_length:.2f} m x {patch_width:.2f} m x {thickness:.2f} m\n"
|
107 |
+
f"Input Impedance: {impedance} Ohms"
|
|
|
108 |
)
|
|
|
109 |
elif antenna_type == "Dipole":
|
110 |
+
dipole_length = calculate_dipole(frequency_hz)
|
111 |
s11_values = [calculate_s11(f) for f in frequencies]
|
112 |
directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
|
113 |
theta = np.linspace(-180, 180, 360)
|
|
|
116 |
s11_graph = plot_s11_graph(frequencies, s11_values)
|
117 |
directivity_gain_graph = plot_directivity_and_gain(frequencies, directivities, gains)
|
118 |
radiation_graph = plot_radiation_pattern(theta, gain_pattern)
|
119 |
+
antenna_3d = None # Dipole does not have a 3D plot
|
120 |
|
121 |
output = (
|
122 |
f"Design Type: Dipole Antenna\n"
|
123 |
f"Operating Frequency: {frequency:.2f} GHz\n"
|
124 |
f"S11 at Operating Frequency: {s11_values[len(s11_values)//2]:.2f} dB\n"
|
125 |
f"Dipole Length: {dipole_length:.2f} m\n"
|
126 |
+
f"Input Impedance: {impedance} Ohms"
|
127 |
)
|
|
|
|
|
|
|
|
|
128 |
|
129 |
return output, s11_graph, directivity_gain_graph, radiation_graph, antenna_3d
|
130 |
|
|
|
136 |
permittivity = gr.Number(value=4.4, label="Substrate Permittivity")
|
137 |
thickness = gr.Number(value=0.01, label="Substrate Thickness (m)")
|
138 |
tangent_loss = gr.Number(value=0.02, label="Substrate Tangent Loss (tan δ)", step=0.01)
|
139 |
+
impedance = gr.Dropdown([50, 73], label="Input Impedance (Ohms)", value=50)
|
140 |
+
design_button = gr.Button("Design Antenna")
|
141 |
+
output_text = gr.Textbox(label="Design Results")
|
142 |
+
s11_plot = gr.Plot(label="S11 Plot")
|
143 |
+
directivity_gain_plot = gr.Plot(label="Directivity and Gain Plot")
|
144 |
+
radiation_pattern_plot = gr.Plot(label="Radiation Pattern")
|
145 |
+
antenna_3d_display = gr.Plot(label="3D Antenna Visualization")
|
146 |
+
|
147 |
+
design_button.click(
|
148 |
+
design_antenna,
|
149 |
+
inputs=[antenna_type, frequency, permittivity, thickness, tangent_loss, impedance],
|
150 |
+
outputs=[output_text, s11_plot, directivity_gain_plot, radiation_pattern_plot, antenna_3d_display]
|
151 |
+
)
|
152 |
+
|
153 |
+
demo.launch()
|