File size: 817 Bytes
e3b871c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from fenics import *
import pyvista as pv
import numpy as np

def run_fenics_simulation():
    # Define mesh and function space
    mesh = UnitSquareMesh(10, 10)
    V = FunctionSpace(mesh, 'P', 1)
    
    # Define boundary condition
    u_D = Expression('x[0]*x[0] - x[1]*x[1]', degree=2)
    bc = DirichletBC(V, u_D, 'on_boundary')
    
    # Define variational problem
    u = TrialFunction(V)
    v = TestFunction(V)
    f = Constant(-6.0)
    a = inner(grad(u), grad(v)) * dx
    L = f * v * dx
    
    # Compute solution
    u = Function(V)
    solve(a == L, u, bc)
    
    # Visualize with PyVista
    u_array = np.array(u.vector())
    grid = pv.StructuredGrid(mesh.coordinates())
    grid.point_arrays['u'] = u_array
    plot = grid.plot(show_edges=True, title='FEniCS Simulation Result')
    return plot