admin08077 commited on
Commit
cf28599
·
verified ·
1 Parent(s): 340f7fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -39
app.py CHANGED
@@ -1,6 +1,9 @@
 
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
- import numpy as np
4
  import random
5
  import time
6
  import concurrent.futures
@@ -11,20 +14,6 @@ from sympy import symbols, Eq, solve
11
  from sympy.parsing.sympy_parser import parse_expr
12
  from sklearn.feature_extraction.text import CountVectorizer
13
  from sklearn.ensemble import RandomForestClassifier
14
- from fenics import (
15
- Mesh,
16
- FunctionSpace,
17
- TrialFunction,
18
- TestFunction,
19
- dot,
20
- grad,
21
- dx,
22
- solve,
23
- Function,
24
- Constant,
25
- DirichletBC
26
- )
27
- # Ensure FEniCS is installed
28
 
29
  ########################################
30
  # 1. Domain Assumption Matrix
@@ -99,40 +88,53 @@ class ConfidenceIndex:
99
  return self.index
100
 
101
  ########################################
102
- # 3. HPC PDE concurrency (FEniCS)
103
  ########################################
104
 
105
  class HPCSolver:
106
  """
107
- Solves PDEs using FEniCS and generates visualizations.
108
  """
109
  def solve_pde(self, problem_type: str, mesh_size: int) -> str:
110
  """
111
- Solve a PDE using FEniCS and save a plot.
112
  Supported problem_type: 'Poisson'
113
  """
114
  if problem_type == "Poisson":
115
  try:
116
- mesh = UnitSquareMesh(mesh_size, mesh_size)
117
- V = FunctionSpace(mesh, "P", 1)
118
-
119
- u = TrialFunction(V)
120
- v = TestFunction(V)
121
- f = Constant(-6.0)
122
-
123
- a = dot(grad(u), grad(v)) * dx
124
- L = f * v * dx
125
-
126
- u_sol = Function(V)
127
- solve(a == L, u_sol)
128
-
129
- plt.figure()
130
- plot(u_sol, title="Poisson Solution")
131
- plt.colorbar()
132
- plt.savefig("poisson_solution.png")
133
- plt.close()
134
-
135
- return "Poisson PDE solved. Visualization saved as 'poisson_solution.png'."
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  except Exception as e:
137
  return f"Error solving Poisson PDE: {e}"
138
  else:
@@ -430,7 +432,7 @@ def build_interface():
430
  chat_btn.click(fn=chat_func, inputs=[chat_input], outputs=[chat_output])
431
 
432
  with gr.Tab("HPC PDE Solvers"):
433
- gr.Markdown("**Simulate solving PDEs using FEniCS with concurrency support.**")
434
  with gr.Row():
435
  problem_type = gr.Dropdown(choices=["Poisson"], label="PDE Type", value="Poisson")
436
  mesh_size = gr.Slider(label="Mesh Size", minimum=10, maximum=100, step=10, value=32)
 
1
+ import dolfinx
2
+ from dolfinx import mesh, fem
3
+ from mpi4py import MPI
4
+ import numpy as np
5
  import gradio as gr
6
  import pandas as pd
 
7
  import random
8
  import time
9
  import concurrent.futures
 
14
  from sympy.parsing.sympy_parser import parse_expr
15
  from sklearn.feature_extraction.text import CountVectorizer
16
  from sklearn.ensemble import RandomForestClassifier
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  ########################################
19
  # 1. Domain Assumption Matrix
 
88
  return self.index
89
 
90
  ########################################
91
+ # 3. HPC PDE concurrency (FEniCSx)
92
  ########################################
93
 
94
  class HPCSolver:
95
  """
96
+ Solves PDEs using FEniCSx and generates visualizations.
97
  """
98
  def solve_pde(self, problem_type: str, mesh_size: int) -> str:
99
  """
100
+ Solve a PDE using FEniCSx and save a plot.
101
  Supported problem_type: 'Poisson'
102
  """
103
  if problem_type == "Poisson":
104
  try:
105
+ # Create mesh
106
+ domain = mesh.create_unit_square(MPI.COMM_WORLD, mesh_size, mesh_size)
107
+
108
+ # Define function space
109
+ V = fem.FunctionSpace(domain, ("Lagrange", 1))
110
+
111
+ # Define boundary condition
112
+ u_D = fem.Function(V)
113
+ with u_D.vector.localForm() as loc:
114
+ loc.set(0.0)
115
+
116
+ def boundary(x):
117
+ return np.full(x.shape[1], True)
118
+
119
+ bc = fem.dirichletbc(u_D, fem.locate_dofs_geometrical(V, boundary))
120
+
121
+ # Define variational problem
122
+ u = fem.TrialFunction(V)
123
+ v = fem.TestFunction(V)
124
+ f = fem.Constant(domain, -6.0)
125
+ a = fem.form(fem.dot(fem.grad(u), fem.grad(v)) * fem.dx)
126
+ L = fem.form(f * v * fem.dx)
127
+
128
+ # Compute solution
129
+ u_sol = fem.Function(V)
130
+ fem.petsc.solve(a == L, u_sol, bc)
131
+
132
+ # Plot solution
133
+ with dolfinx.io.XDMFFile(domain.comm, "poisson_solution.xdmf", "w") as xdmf:
134
+ xdmf.write_mesh(domain)
135
+ xdmf.write_function(u_sol)
136
+
137
+ return "Poisson PDE solved. Visualization saved as 'poisson_solution.xdmf'."
138
  except Exception as e:
139
  return f"Error solving Poisson PDE: {e}"
140
  else:
 
432
  chat_btn.click(fn=chat_func, inputs=[chat_input], outputs=[chat_output])
433
 
434
  with gr.Tab("HPC PDE Solvers"):
435
+ gr.Markdown("**Simulate solving PDEs using FEniCSx with concurrency support.**")
436
  with gr.Row():
437
  problem_type = gr.Dropdown(choices=["Poisson"], label="PDE Type", value="Poisson")
438
  mesh_size = gr.Slider(label="Mesh Size", minimum=10, maximum=100, step=10, value=32)