It is a example and explanation of which is described in docu.ngsolve.org

Let us solve the Poisson problem of inding u satisfying

 \Delta u=f

in the unit square

u=0

on the bottom and right parts of the boundary

{\partial u \over \partial n}=0

on the remaining boundary parts.

from ngsolve import *
from ngsolve.webgui import Draw
from netgen.geom2d import unit_square

The ngsolve module is a finite element library for solving partial differential equations (PDEs) in Python. The Draw function from the ngsolve.webgui module is a visualization tool that allows you to view the results of a computation. The unit_square object from the netgen.geom2d module is a predefined geometry object that represents a unit square in 2D.

You can use ngsolve to define a finite element function space, create a finite element function on that space, and then use various methods to solve a PDE on that function.

mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))
mesh.nv, mesh.ne   # number of vertices & elements

The mesh object is a finite element mesh that represents the geometry of the domain on which you want to solve a PDE. The Mesh constructor takes in a single argument, which is a mesh object generated from the GenerateMesh method of the unit_square object. The GenerateMesh method discretizes the geometry into a finite element mesh with a maximum element size of maxh.

The nv attribute of the mesh object returns the number of vertices in the mesh, and the ne attribute returns the number of elements in the mesh. You can use these attributes to get information about the size of the mesh.

For example, the code you provided generates a finite element mesh with a maximum element size of 0.2 on the unit square geometry, and then prints the number of vertices and elements in the mesh.

fes = H1(mesh, order=2, dirichlet="bottom|right")
fes.ndof  # number of unknowns in this space

The H1 class in NGSolve represents the finite element space of continuous, piecewise polynomial functions with square-integrable first derivatives on a given mesh.

The order parameter specifies the polynomial degree of the functions in the space. For example, if order=2, the functions in the space are quadratics.

The dirichlet parameter specifies the boundary conditions on the space. In this case, the boundary condition is applied to the “bottom” and “right” boundaries of the mesh. The boundary conditions can be specified using a string that lists the boundary names separated by the | symbol, or as a list of integers that specify the boundary marker indices.

The ndof attribute of the H1 object returns the number of degrees of freedom in the finite element space. This is the number of unknowns that need to be solved for in the finite element discretization of the PDE. The number of degrees of freedom is equal to the dimension of the finite element space.

For example, if the mesh has 10 elements and order=2, the number of degrees of freedom would be:

ndof = (10+1) * 2 = 22

This is because each element has two degrees of freedom (one for each end), and there are 10+1=11 elements in the mesh (including the one element at each end). The total number of degrees of freedom is therefore 11 * 2 = 22.

u = fes.TrialFunction()  # symbolic object
v = fes.TestFunction()   # symbolic object
gfu = GridFunction(fes)  # solution

The u and v objects are symbolic finite element functions defined on the fes function space. They are called “trial” and “test” functions, respectively, and are used to define the weak form of a PDE.

The TrialFunction method returns a symbolic trial function defined on the fes function space, and the TestFunction method returns a symbolic test function defined on the same space. These functions can be used to define the weak form of a PDE by specifying the appropriate integrals over the domain.

The gfu object is a finite element function defined on the fes function space. It represents the numerical solution of the PDE, and can be used to visualize the solution or compute quantities of interest. The GridFunction constructor takes in a single argument, which is the finite element function space on which the function is defined.

For example, the code you provided creates symbolic trial and test functions on the fes function space, and then creates a finite element function gfu on the same space to hold the solution of the PDE.

u, v = fes.TnT()

In NGSolve, the TnT method of a finite element space returns a tuple (u, v) of trial and test functions defined on the space. The trial function u represents the unknown function that is being solved for, and the test function v is used to define the weak form of the PDE.


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *