Hermes

Hermes is a state-of-the-art library of finite element algorithms written in C++. It is being developed jointly at the University of Nevada, Reno (USA), University of West Bohemia in Pilsen (Czech Republic), and Institute of Thermomechanics in Prague (Czech Republic). In NCLab, Hermes is available via its Python wrappers.

coding courses and 3D modeling for kids

An Example

NCLab’s public database provides about a dozen tutorial examples that illustrate how Hermes is used to solve various types of problems. To download them into your NCLab account, access the database through File manager’s File menu and search for keywords “Hermes tutorial”. Additional ca. 100 examples are available in the C++ repository. For illustration, let us show the Python code of example A03 which solves the Poisson equation. More detailed explanation of all steps can be found in the document Using Hermes in NCLab.

Import the Hermes library

import hermes_common
import hermes2d

Set initial polynomial degree

P_INIT = 3

Define material parameters

Aluminum_lambda = 236
Copper_lambda = 386
Heat_source = 5e3

Define weak forms

# Define weak forms:
class PyCustomWeakFormPoisson(hermes2d.PyCustomWeakFormReal):
    def __init__(self, mat_al, lambda_al, mat_cu, lambda_cu, vol_src_term):
        # Define number of equations.
        self.super(1)

        # Jacobian forms.
        self.add_matrix_form(hermes2d.PyDefaultJacobianDiffusion(0, 0, lambda_al, mat_al))
        self.add_matrix_form(hermes2d.PyDefaultJacobianDiffusion(0, 0, lambda_cu, mat_cu))

        # Residual forms.
        self.add_vector_form(hermes2d.PyDefaultResidualDiffusion(0, lambda_al, mat_al))
        self.add_vector_form(hermes2d.PyDefaultResidualDiffusion(0, lambda_cu, mat_cu))
        self.add_vector_form(hermes2d.PyDefaultVectorFormVol(0, -src_term))

Read finite element mesh

mesh = hermes2d.PyMesh()
reader = hermes2d.PyMeshReaderH2DXML()
mesh_stream = '...'
reader.load_stream(mesh_stream, mesh)

Refine mesh as needed

Hermes provides many mesh refinement operations including uniform refinement of all elements, refinements towards a vertex, refinement towards domain boundary, towards a material interface, etc. See the C++ tutorial for more details. For illustration, the following command performs a uniform refinement:

mesh.refine_all_elements()

Define boundary conditions

bc = hermes2d.PyDefaultEssentialBCConstReal(["Bottom", "Inner", "Left", "Outer"], FIXED_BDY_TEMP)
bcs = hermes2d.PyEssentialBCsReal(bc)

Create finite element space

space = hermes2d.PyH1SpaceReal(mesh, bcs, P_INIT)

Initialize weak formulation

wf = PyCustomWeakFormPoisson("Aluminum", LAMBDA_AL, "Copper", LAMBDA_CU, VOLUME_HEAT_SRC)

Initialize discrete problem

dp = hermes2d.PyDiscreteProblemReal(wf, space)

Initialize a Solution

solution = hermes2d.PySolutionReal()

Initialize Newton Solver

newton = hermes2d.PyNewtonSolverReal(dp)

Solve via the Newton’s method

newton.solve(coef)

Translate resulting coefficient vector into a Solution

hermes2d.PySolutionReal().vector_to_solution(newton.get_sln_vector(), space, solution)

Initialize Linearizer

lin = hermes2d.PyLinearizer()

Output linearized solution

lab.writeVTK('~/vtk', lin.save_solution_vtk_to_stream(solution, 'temperature', True, 1))

Read the Solution into Postprocessor and display it

lab.postprocessor(dict (solution = lab.readVTK('~/vtk')))

Hermes Python Wrappers

The wrappers follow very closely the C++ version of the library, so C++ tutorial and library documentation can be used for reference. It is useful to remember a few naming conventions:
 

  • C++ class X is wrapped as PyX.
  • C++ template T is wrapped as PyTReal.
  • C++ template T is wrapped as PyTComplex.
  • Names and arguments of methods on the C++ and >code>Python levels are identical.

NCLab Geometry Editor (GE) and Mesh Editor (ME)

In the above example, finite element mesh was supplied via a XML string. You can also use NCLab’s Geometry Editor (GE) to create a geometry interactively, and then generate the corresponding finite element mesh on the server via the Mesh Editor (ME). NCLab’s public database contains examples showing how the GE and ME can be connected to Hermes. To download them, search for keywords “GE, ME, PP”. A detailed description of the GE and ME can be found on the pages of FEA modules for Elasticity and Electrostatics.