Note
Go to the end to download the full example code.
Poisson2#
Poisson equation with unit load.
Reference: https://scikit-fem.readthedocs.io/en/latest/gettingstarted.html

15 import matplotlib.pyplot as plt
16 import numpy as np
17
18 from EasyFEA import Display, ElemType, Models, Simulations
19 from EasyFEA.FEM import Field, BiLinearForm, LinearForm
20 from EasyFEA.Geoms import Domain
21
22 if __name__ == "__main__":
23 Display.Clear()
24
25 # ----------------------------------------------
26 # Mesh
27 # ----------------------------------------------
28
29 contour = Domain((0, 0), (1, 1), 1 / 8)
30
31 mesh = contour.Mesh_2D([], ElemType.TRI15, isOrganised=True)
32
33 nodes = mesh.Nodes_Tags(["L0", "L1", "L2", "L3"])
34
35 # ----------------------------------------------
36 # Formulations
37 # ----------------------------------------------
38
39 field = Field(mesh.groupElem, 1)
40
41 @BiLinearForm
42 def bilinear_form(u: Field, v: Field):
43 return u.grad.dot(v.grad)
44
45 @LinearForm
46 def linear_form(v: Field):
47 x, y, _ = v.Get_coords()
48 f = np.sin(np.pi * x) * np.sin(np.pi * y)
49 return f * v
50
51 weakForms = Models.WeakForms(field, computeK=bilinear_form, computeF=linear_form)
52
53 # ----------------------------------------------
54 # Simulation
55 # ----------------------------------------------
56
57 simu = Simulations.WeakForms(mesh, weakForms)
58
59 simu.add_dirichlet(nodes, [0], ["u"])
60
61 simu.Solve()
62
63 # ----------------------------------------------
64 # Results
65 # ----------------------------------------------
66
67 Display.Plot_Result(simu, "u", plotMesh=True)
68
69 # compute error
70 x, y, z = mesh.coord.T
71 u_an = 1 / 2 / np.pi**2 * np.sin(np.pi * x) * np.sin(np.pi * y)
72 error = np.linalg.norm(u_an - simu.u) / np.linalg.norm(u_an)
73
74 plt.show()
Total running time of the script: (0 minutes 0.163 seconds)