hussain2010 commited on
Commit
3a2281e
·
verified ·
1 Parent(s): 96c0068

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import numpy as np
4
+ from groq import Groq
5
+ from dotenv import load_dotenv
6
+ import plotly.graph_objects as go
7
+
8
+ # Load API Key
9
+ 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):
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_s11(frequency):
22
+ # Simulate S11 (just an example function for demonstration)
23
+ s11 = -20 + 5 * np.cos(2 * np.pi * frequency / 10)
24
+ return s11
25
+
26
+ def calculate_directivity_and_gain(frequency):
27
+ directivity = 6.0 + 0.5 * np.log10(frequency) # Approximation
28
+ realized_gain = directivity - 1.5 # Efficiency loss
29
+ return directivity, realized_gain
30
+
31
+ def radiation_pattern(theta, frequency):
32
+ gain = 10 * np.log10(np.abs(np.sin(np.radians(theta))) + 1e-9)
33
+ return gain
34
+
35
+ # Graphing Functions
36
+ def plot_3d_microstrip_patch(patch_length, patch_width, thickness):
37
+ fig = go.Figure()
38
+ # Patch
39
+ fig.add_trace(go.Surface(
40
+ z=[[0, 0], [0, 0]], x=[[0, patch_width], [0, patch_width]],
41
+ y=[[0, 0], [patch_length, patch_length]], colorscale="Viridis", name="Patch"
42
+ ))
43
+ # Substrate
44
+ fig.add_trace(go.Surface(
45
+ z=[[-thickness, -thickness], [-thickness, -thickness]],
46
+ x=[[0, patch_width], [0, patch_width]],
47
+ y=[[0, 0], [patch_length, patch_length]], colorscale="Blues", name="Substrate"
48
+ ))
49
+ # Ground
50
+ fig.add_trace(go.Surface(
51
+ z=[[-thickness, -thickness], [-thickness, -thickness]],
52
+ x=[[0, patch_width], [0, patch_width]],
53
+ y=[[0, 0], [patch_length, patch_length]], colorscale="Greens", name="Ground"
54
+ ))
55
+ fig.update_traces(showscale=False)
56
+ fig.update_layout(title="3D Microstrip Patch Antenna", showlegend=False)
57
+ return fig
58
+
59
+ def plot_s11_graph(frequencies, s11_values):
60
+ fig = go.Figure()
61
+ fig.add_trace(go.Scatter(x=frequencies, y=s11_values, mode='lines', name="S11"))
62
+ fig.update_layout(title="Frequency vs. S11", xaxis_title="Frequency (GHz)", yaxis_title="S11 (dB)")
63
+ return fig
64
+
65
+ def plot_directivity_and_gain(frequencies, directivities, gains):
66
+ fig = go.Figure()
67
+ fig.add_trace(go.Scatter(x=frequencies, y=directivities, mode='lines', name="Directivity"))
68
+ fig.add_trace(go.Scatter(x=frequencies, y=gains, mode='lines', name="Realized Gain"))
69
+ fig.update_layout(title="Frequency vs. Directivity and Realized Gain",
70
+ xaxis_title="Frequency (GHz)", yaxis_title="Gain (dBi)")
71
+ return fig
72
+
73
+ def plot_radiation_pattern(theta, gain_pattern):
74
+ fig = go.Figure()
75
+ fig.add_trace(go.Scatter(x=theta, y=gain_pattern, mode='lines', name="Radiation Pattern"))
76
+ fig.update_layout(title="Radiation Pattern", xaxis_title="Degrees", yaxis_title="Gain (dBi)")
77
+ return fig
78
+
79
+ # Main Function
80
+ def design_antenna(antenna_type, frequency, permittivity, thickness):
81
+ frequency_hz = frequency * 1e9
82
+ frequencies = np.linspace(frequency - 0.5, frequency + 0.5, 100)
83
+
84
+ if antenna_type == "Microstrip Patch":
85
+ patch_length, patch_width, thickness = calculate_microstrip_patch(frequency_hz, permittivity, thickness)
86
+ s11_values = [calculate_s11(f) for f in frequencies]
87
+ directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
88
+ theta = np.linspace(-180, 180, 360)
89
+ gain_pattern = radiation_pattern(theta, frequency_hz)
90
+
91
+ s11_graph = plot_s11_graph(frequencies, s11_values)
92
+ directivity_gain_graph = plot_directivity_and_gain(frequencies, directivities, gains)
93
+ radiation_graph = plot_radiation_pattern(theta, gain_pattern)
94
+ antenna_3d = plot_3d_microstrip_patch(patch_length, patch_width, thickness)
95
+
96
+ output = (
97
+ f"Design Type: Microstrip Patch Antenna\n"
98
+ f"Operating Frequency: {frequency:.2f} GHz\n"
99
+ f"S11 at Operating Frequency: {s11_values[len(s11_values)//2]:.2f} dB\n"
100
+ f"Patch Dimensions: {patch_length:.2f} m x {patch_width:.2f} m x {thickness:.2f} m\n"
101
+ )
102
+ else:
103
+ output = "Currently, only Microstrip Patch Antenna is supported."
104
+ s11_graph, directivity_gain_graph, radiation_graph, antenna_3d = None, None, None, None
105
+
106
+ return output, s11_graph, directivity_gain_graph, radiation_graph, antenna_3d
107
+
108
+ # Gradio Interface
109
+ with gr.Blocks() as demo:
110
+ gr.Markdown("# Antenna Design Tool with Groq API")
111
+ antenna_type = gr.Dropdown(["Microstrip Patch"], label="Select Antenna Type")
112
+ frequency = gr.Slider(1.0, 10.0, step=0.1, label="Operating Frequency (GHz)")
113
+ permittivity = gr.Number(value=4.4, label="Substrate Permittivity")
114
+ thickness = gr.Number(value=0.01, label="Substrate Thickness (m)")
115
+ design_button = gr.Button("Design Antenna")
116
+ output_text = gr.Textbox(label="Design Results")
117
+ s11_plot = gr.Plot(label="S11 Plot")
118
+ directivity_gain_plot = gr.Plot(label="Directivity and Gain Plot")
119
+ radiation_pattern_plot = gr.Plot(label="Radiation Pattern")
120
+ antenna_3d_display = gr.Plot(label="3D Antenna Visualization")
121
+
122
+ design_button.click(
123
+ design_antenna,
124
+ inputs=[antenna_type, frequency, permittivity, thickness],
125
+ outputs=[output_text, s11_plot, directivity_gain_plot, radiation_pattern_plot, antenna_3d_display]
126
+ )
127
+
128
+ demo.launch()