Professional Documents
Culture Documents
Session 1
Using Netgen/NGSolve
Christoph Lehrenfeld
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 2 / 39
What is Netgen ?
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 3 / 39
Some Meshes generated by Netgen
A machine frame imported via the Step - format, a bunny imported from STL ...
We will define our own simple examples for CSG files later.
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 4 / 39
What is NGSolve ?
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 5 / 39
Interplay of Netgen and NGSolve
NGS−flow NGS−mech
Netgen knows the geometry, and maintains the mesh, does visualization
NGSolve does the finite elements and linear algebra
Connection is via the mesh interface (NGSolve: class MeshAccess)
Application classes can be loaded as shared libraries into NGSolve
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 6 / 39
Simulation of Mechanical Deformation and Stresses
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 7 / 39
Simulation of Electromagnetic Fields
Described by
Maxwell’s equations
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 8 / 39
Incompressible Flows (ngsflow)
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 9 / 39
How to use Netgen/NGSolve
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 10 / 39
Define the geometry
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 11 / 39
(1 − 2) Defining 2D Geometries
6 -
6
6
1 6
- ?
0.1
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 12 / 39
points (splinecurves2dv2)
splinecurves2dv2
2
points
#point number
# x- and y-coordinate
# local mesh size
1 -3 -3
2 3 -3
3 3 3
4 -3 3
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 13 / 39
segments and materials
segments
#domain left and right of the segment
# number of points the segment consists of
# points of the segment
# boundary number
# local mesh size
1 0 2 1 2 -bc=1
1 0 2 2 3 -bc=1
1 0 2 3 4 -bc=1
1 0 2 4 1 -bc=1
0 1 2 5 6 -bc=2 -maxh=0.1
0 1 2 6 7 -bc=2 -maxh=0.1
0 1 2 7 8 -bc=2 -maxh=0.1
0 1 2 8 5 -bc=2 -maxh=0.1
0 1 2 9 10 -bc=3 -maxh=0.1
0 1 2 10 11 -bc=3 -maxh=0.1
0 1 2 11 12 -bc=3 -maxh=0.1
0 1 2 12 9 -bc=3 -maxh=0.1
materials
1 inner -maxh=0.5
#0 outer
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 14 / 39
(3) The 3D CSG - Files
algebraic3d
solid cube = orthobrick (0, 0, 0; 1, 1, 1);
solid sp = sphere (1, 1, 1; 0.5);
solid main = cube and not sp;
tlo main;
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 15 / 39
(4) Meshing
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 16 / 39
(5) NGSolve script file for Poisson example:
Z Z Z
1
Find u ∈ H s.t. ε ∇u · ∇v dx + αuv ds = gv dx ∀ v ∈ H1
Ω ∂Ω ∂Ω
geometry = capacitor.in2d
mesh = capacitor.vol
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 17 / 39
The PDE input file defines the variational problem. You specify the finite element
space, and then, the bilinear-form (left hand side), and the linear form (right hand
side) are build up from elementary blocks called integrators. The table below lists
the names of the integrators needed in the equation above:
R
laplace coef eps RΩ ε∇u · ∇v dx
robin coef alpha αuv ds
RΓR
neumann coef g ΓN
gv ds
Note that the last term is handled by the neumann - integrator with coefficient
αu0 . You might miss Dirichlet boundary conditions. Indeed, NGSolve always
approximates them by Robin b.c. with large conductivity α.
Remark:
There are first implementations for dirichlet boundary conditions, but not
everywhere, not for everything...
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 18 / 39
Loading geometry and mesh
The first two lines load the prepared geometry file and the mesh file.
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 19 / 39
Coefficient functions
The list of numbers correspond to the sub-domains, if the integral is taken over
the domain, or, to parts of the boundary, if the integral is taken over the
boundary, respectively. The boundary splits in our case in to 3 boundary parts.
We specify Robin boundary conditions (with conductivity α = 105 ) on Γ2 to Γ2 ,
and an homogenous Neumann boundary condition on Γ1 .
Attention:
coefficients are not filled up with zeros automatically!
!
#boundaries / #domains ≤ #coefficient entries
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 20 / 39
Definition of the PDE
The next lines define the mathematical objects finite-element space, grid-function,
bilinear-form and a linear-form.
The last line (numproc) calls the solver for boundary value problems (bvp). Here,
a direct solver is used to solve the linear system (SparseCholesky,Pardiso,Mumps).
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 21 / 39
Finite Element Spaces
The definition
define fespace <name> <flaglist>
defines the finite element space <name>.
Example:
define fespace v -order=2 -dim=3
There are various classes of finite element spaces. Default are continuous,
nodal-valued finite element spaces. The following define flags select the type of
spaces
no flag continuous nodal finite element space
-hcurl H(curl) finite elements (Nedelec-type, edge elements)
-hdiv H(div) finite elements (Raviart-Thomas, face elements)
-l2 non-continuous elements, element by element
-l2surf element by element on surface
-h1ho Arbitrary order continuous elements
-hcurlho Arbitrary order H(curl) elements
-hdivho Arbitrary order H(div) elements
-l2ho Arbitrary order non-continuous elements
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 22 / 39
The following flags specify the finite element spaces
-order=<num> Order of finite elements
-dim=<num> Number of fields (copies of fe), 2 for 2D elasticity
-vec -dim=spacedim
-tensor -dim=spacedim*spacedim
-symtensor -dim=spacedim*(spacedim+1) / 2, (sym. stress tensor)
-complex complex valued fe-space
A compound fe-space combines several fe-spaces to a new one. Useful, e.g., for
Reissner-Mindlin plate models containing the deflection w and two rotations beta:
fespace vw -order=2 fespace vbeta -order=1 fespace v -compound
-spaces=[vw,vbeta,vbeta]
The fespace maintains the degrees of freedom. On mesh refinement, the space
provides the grid transfer operator (prolongation). High order fe spaces maintain a
lowest-order fespace of the same type for preconditioning.
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 23 / 39
(6a) Enhancements (preconditioners)
There are different std. preconditioners available
define preconditioner c -type=direct -bilinearform=a
define preconditioner c -type=local -bilinearform=a -smoother=bl
define preconditioner c -type=multigrid -bilinearform=a
-smoothingsteps=1 -smoother=block
define preconditioner c -type=amg -bilinearform=a
multigrid -cycle=
0 Gauss-Seidel
1 V-Cycle Multigrid
2 W-Cycle Multigrid
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 24 / 39
You may as well want to do static condensation:
define fespace v -order=5 -h1ho -eliminate_internal
define bilinearform a -fespace=v -symmetric
-eliminate_internal -linearform=f
But then you have to define the linearform before you define the bilinearform.
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 25 / 39
(6b) Enhancements (fluxes, evaluations)
fluxes:
numproc drawflux np2 -bilinearform=a -solution=u -label=flux
evaluating bilinearforms
of gridfunctions:
define bilinearform aeval -fespace=v -symmetric -nonassemble
laplace coef_eps
numproc evaluate npeval -bilinearform=aeval -gridfunction=u
-gridfunction2=u -filename=evalp.out
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 26 / 39
(6c) Enhancements (estimators, element marking)
error estimators:
define fespace verr -l2 -order=0
define gridfunction err -fespace=verr
refinement marker:
numproc markelements np5 -error=err -minlevel=1 -factor=0.9
numproc drawflux df1 -marked
enable prolongation from coarse gridfunction to finer gridfunction:
define gridfunction u -fespace=v -nested
Mesh refinement in Netgen is done by the marked edge bisection
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 27 / 39
(6d) hp-Refinement
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 28 / 39
How to find out about details of NGSolve-Objects:
“Print Equations” lists all registrated NGSolve-Objects such as :
integrators, FESpaces, preconditioners and numprocs
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 29 / 39
(7) 3D poisson
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 30 / 39
(8) Mechanics - Linear Elasticity (d5 beam.pde )
div(σ) = f Z Z Z Z
σ := Dε(u) ⇒ Dε(u) : ε(v ) dx + α uv ds = fv dx + gv ds
σ·n = g on ΓN Ω ΓP Ω ΓN
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 32 / 39
(10) Maxwell (Example d7 coil.pde)
Calculation of a stationary magnet field
1
curl curl A + σA = j
µ
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 33 / 39
Z Z Z Z
1
weak form: curl A curl v dx + σAv dx + penAv ds = fv dx
Ω µ Ω Γ3 Ω
The PDE-file (Part 2:)
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 34 / 39
Magnetic field visualization:
∇ Curl Div
H1 −→ H(curl) −→ H(div) −→ L2
S S S S
∇ Curl Div
Wh −→ Vh −→ Qh −→ Sh
Used for constructing high order finite elements [J.Schöberl+S. Zaglmayr, ’05,
Thesis Zaglmayr ’06]
Whp = WL1 + span{ϕW
h.o. }
Vhp = VN0 + span{∇ϕW V
h.o. } + span{ϕh.o. } “nograds” kicks red part out
Qhp = QRT 0 + span{curl ϕVh.o. } + span{ϕQ
h.o. }
Shp = SP0 + span{div ϕQ
h.o. }
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 36 / 39
(11) NGSflow - The navierstokeshdiv Solver
Within the NGSflow-package we have got numprocs which care for the FESpace,
Bilinearforms, etc. on their own. So you just have to prescribe
geometry
mesh
boundary conditions
initial conditions (or stokes)
time step
time interval
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 37 / 39
define constant heapsize = 1000000
# boundaries:
# 1 .. inflow
# 2 .. outflow
# 3 .. wall (no slip)
# 4 .. cylinder (no slip)
geometry = collision.in2d
mesh = collision.vol
shared = libnavierstokes_hdivred
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 38 / 39
Thank you for your attention!
Christoph Lehrenfeld (RWTH) An Introduction to Netgen and NGSolve 1 UBC, November 9, 2009 39 / 39