a = BilinearForm(fes, symmetric=True)
a += grad(u)*grad(v)*dx
a.Assemble()
In NGSolve, the BilinearForm
class represents a bilinear form defined on a finite element space. A bilinear form is a function that takes two finite element functions as input and returns a scalar. It is used to define the left-hand side of a weak formulation of a PDE.
In the code you provided, a bilinear form a
is defined on the finite element space fes
using the BilinearForm
constructor. The symmetric
parameter specifies that the form is symmetric, i.e. a(u, v) = a(v, u)
for all u
, v
in the finite element space.
The form is then defined using the +=
operator and the grad
and dx
functions. The grad
function returns the gradient of a finite element function, and the dx
function integrates the product of two functions over the domain.
Finally, the form is assembled using the Assemble
method. This processes the form definition and creates the underlying matrix representation of the form. The assembled form can then be used to define the left-hand side of a finite element problem and solve it.
f = LinearForm(fes)
f += x*v*dx
f.Assemble()
In NGSolve, the BilinearForm
class represents a bilinear form defined on a finite element space. A bilinear form is a function that takes two finite element functions as input and returns a scalar. It is used to define the left-hand side of a weak formulation of a PDE.
In the code you provided, a bilinear form a
is defined on the finite element space fes
using the BilinearForm
constructor. The symmetric
parameter specifies that the form is symmetric, i.e. a(u, v) = a(v, u)
for all u
, v
in the finite element space.
The form is then defined using the +=
operator and the grad
and dx
functions. The grad
function returns the gradient of a finite element function, and the dx
function integrates the product of two functions over the domain.
Finally, the form is assembled using the Assemble
method. This processes the form definition and creates the underlying matrix representation of the form. The assembled form can then be used to define the left-hand side of a finite element problem and solve it.
print(f.vec)
In NGSolve, the vec
attribute of a LinearForm
object represents the coefficient vector of the linear form. The coefficient vector is a dense NumPy array that contains the coefficients of the linear form in a canonical ordering.
This will print the coefficient vector of the linear form f
. The coefficients in the vector are ordered according to the canonical ordering of the finite element space.
For example, if the finite element space has 10 degrees of freedom, the coefficient vector will have length 10 and will contain the coefficients of the linear form in the following order:
[f_0, f_1, f_2, ..., f_9]
Where f_i
is the coefficient of the linear form corresponding to the i-th degree of freedom.
You can modify the coefficients of the linear form by assigning to the vec
attribute:
f.vec[:] = 0 # set all coefficients to 0
f.vec[3] = 1 # set the 4th coefficient to 1
You can also perform arithmetic operations on the coefficient vector using NumPy functions:
f.vec += 1 # add 1 to all coefficients
f.vec *= 2 # multiply all coefficients by 2
print(a.mat)
The a.mat
attribute returns the stiffness matrix of the bilinear form a
. The stiffness matrix is a square matrix that represents the coefficients of the linear system that needs to be solved to obtain the finite element solution of a PDE.
In general, the stiffness matrix is constructed by assembling the local stiffness matrices for each element in the mesh, which are obtained by evaluating the bilinear form on each element. The entries of the stiffness matrix depend on the specific form of the bilinear form, as well as the geometry and discretization of the domain.
You can print the stiffness matrix to see its structure and values. However, keep in mind that for large meshes, the stiffness matrix can be very large and dense, so it may not be practical to print it in its entirety.
gfu.vec.data = \
a.mat.Inverse(freedofs=fes.FreeDofs()) * f.vec
Draw(gfu)
In NGSolve, the vec
attribute of a GridFunction
object represents the coefficient vector of the function. The coefficient vector is a dense NumPy array that contains the coefficients of the function in a canonical ordering.
In the code you provided, the GridFunction
gfu
is defined on the finite element space fes
, and its coefficient vector is set using the following expression:
gfu.vec.data = a.mat.Inverse(freedofs=fes.FreeDofs()) * f.vec
Here, a.mat
is the matrix representation of the bilinear form a
, and f.vec
is the coefficient vector of the linear form f
. The matrix inverse is taken using the Inverse
method, and the freedofs
parameter specifies the indices of the free degrees of freedom in the finite element space. The inverse matrix is then multiplied by the coefficient vector of the linear form to obtain the coefficient vector of the GridFunction
gfu
.
Finally, the Draw
function is used to visualize the GridFunction
gfu
. This function plots the function using a color map that indicates the function values.
print(gfu.vec)
The gfu.vec
attribute returns the coefficient vector of the finite element function gfu
. The coefficient vector is a one-dimensional array that contains the values of the finite element function at the degrees of freedom of the function space.
The coefficient vector can be used to obtain the numerical solution of a PDE at the degrees of freedom of the function space. It can also be used to visualize the solution using the Draw
function, or to compute quantities of interest such as integrals of the solution over the domain.
You can print the coefficient vector to see its values. However, keep in mind that for large meshes, the coefficient vector can be very large, so it may not be practical to print it in its entirety.
Leave a Reply