Several files are required for the configuration and initialization of a simulation (cf. Input Files for Simulations). However, they are very easy to adapt from existing Examples and finite element meshes of several simple geometries are included in the package of Examples available on the download page.

If a new geometry is required, some more advanced tools are necessary. magpar can import UCD files and MSC.Patran neutral files. The UCD format is very simple, so it should be very easy to write export filters/converters for other mesh generators, too.

The following CAD software and mesh generators have been used to generate geometries and finite element meshes for magpar.


From the Gmsh homepage:

Gmsh: a three-dimensional finite element mesh generator with built-in pre-
and post-processing facilities

Gmsh is an automatic 3D finite element grid generator with a built-in
CAD engine and post-processor. Its design goal is to provide a
simple meshing tool for academic problems with parametric input and
advanced visualization capabilities.

Gmsh is built around four modules: geometry, mesh, solver and post-processing.
The specification of any input to these modules is done either interactively
using the graphical user interface or in ASCII text files using Gmsh's own
scripting language.

After defining the geometry the 3D tetrahedral mesh has to be generated. Gmsh uses TetGen or NETGEN to generate the tetrahedral finite element mesh.

The mesh is then exported using Mesh/Save or File/Save Mesh as a *.msh file. In order to assign different property ids to different parts of the volume it is necessary to define corresponding "Physical groups" (Add/Volume). In addition, it might be necessary to tweak the ids of these physical volumes by hand in the *.geo file to ensure that these physical volumes are numbered consecutively starting from 1.

Sometimes, Gmsh completes the mesh generation but it cannot save the mesh. If this happens, it can help to increase the Linux/Unix resources - in particular the maximum number of open files - in /etc/security/limits.conf:

*                hard    nofile          10000


This issue has been resolved in Gmsh version 2.2.1:

The Python program gmsh: (provided in the $MAGPAR_HOME/src/tools/ directory), can then be used to convert Gmsh mesh files (version 2.0) into UCD/inp files (cf. project.inp, project.out: finite element mesh).

The examples sphere_larmor: Larmor precession and mumag3b: mumag standard problem #3 with 2 cubes use finite element meshes generated with Gmsh.

Optimizing meshes

Sometimes Gmsh will generate a suboptimal mesh! This will often cause magpar to run very slowly (because the mesh structure results in larger numerical errors in the magnetostatic field, which causes the time integrator to take very small time steps) or to fail when the linear solvers (for the calculation of the magnetostatic field, e.g. Au1*u1=divM in Hdemag()) do not converge. You can check for a bad mesh by looking at the .felog generated by magpar at startup:

simname: strip
  id_min: 7241
  id_max: 5335
  Vmax:   22.4616
  Vmin:   0.0000158533
  ratio:  1416840
  Vavg:   6.43932
  Vtot:   62500
edge_len_min: 0.003124
edge_len_max: 7.59121
edge_len_avg: 4.32508

Notice in this example that the ratio of the volume of the largest cell in the mesh to that of the smallest cell is 1416840. Furthermore, the shortest edge (edge_len_min) is 2500 times shorter than the longest edge (edge_len_max). This will probably cause problems for magpar.

If Gmsh produces a poor mesh, there are a few things to try (options to insert at the beginning of the .geo file):

Command-line meshing

Gmsh can be invoked from the command line to generate a 3D mesh from a .geo file, which allows the process to be scripted. This is done by invoking gmsh with the -3 option, as in:

$ gmsh -3 filename.geo

The -optimize and -optimize_netgen options may be added to produce an optimized mesh (equivalent to setting the Mesh.Optimize or Mesh.OptimizeNetgen variable in the .geo file).


From the NETGEN homepage (Linz site):

NETGEN is an automatic 3d tetrahedral mesh generator. It accepts input from
constructive solid geometry (CSG) or boundary representation (BRep) from
STL file format. The connection to a geometry kernel allows the handling of
IGES and STEP files. NETGEN contains modules for mesh optimization and
hierarchical mesh refinement. Netgen is open source based on the LGPL license.
It is available for Unix/Linux and Windows.

Richard Boardman and Hans Fangohr have implemented the tool, which converts neutrally-exported NETGEN meshes into AVS/UCD files (cf. project.inp, project.out: finite element mesh).

NETGEN can be found here:


Salome provides a generic platform for Pre- and Post-Processing for numerical simulations. It is based on an open and flexible architecture made of reusable components.

Salome has powerful tools to generate 3D geometries (even with combinations of bottom-up [points > lines > faces > volumes] and top-down approaches [3D shapes > volumes > faces > lines > points] and tools to generate unions, intersections, cuts, etc.), mesh them, and export them in various formats.

Currently, there are no converters for the FE meshes generated by Salome yet, but Gmsh can import Salome's output.


GiD is the "personal pre- and postprocessor" from the "International Center for Numerical Methods in Engineering" (CIMNE). An evaluation version, which is restricted to 3000 finite elements, is available on the web for free. The installation of GiD is very easy on Windows as well as Linux platforms.

After the installation of GiD the new problem type "inp.gid" should be installed. This is done by copying the directory "$MAGPAR_HOME/src/tools/gid/inp.gid" to the subdirectory "problemtypes" of your GiD installation. If GiD recognized the new problem type, there should be an entry "inp" in the "Data/Problem type" menu.

In order to create a the finite element mesh of a simple geometry and export it in inp format, the following steps are required:

  1. start GiD
  2. set problem type:
    "Data/Problem type/inp"
  3. create a sphere:
  4. enter the center of the sphere, e.g.
  5. enter the desired radius, e.g.
  6. create more geometric objects, e.g. a second sphere:
  7. create a sphere:
  8. enter the center of the sphere, e.g.
  9. enter the desired radius, e.g.
  10. assign "Conditions" (=property ids=grain numbers) - the corresponding material parameters are later defined in project.krn: material properties in the appropriate line (line number=property id):
  11. set grain number
    "grain number 1"
  12. "Assign"
  13. select all objects, which should have property id "1", e.g. the left sphere
  14. press the "Finish" button or the Esc key when finished
  15. set grain number
    "grain number 2"
  16. "Assign"
  17. select all objects, which should have property id "2", e.g. the right sphere
  18. press the "Finish" button or the Esc key when finished, "Close" the dialog window
  19. If you have created independent points, lines, or surfaces delete them all
    (this is not necessary for the two spheres in this simple example):
    only volumes should be left over!
  20. generate a tetrahedral finite element mesh:
  21. Enter the size of elements, e.g.
  22. save the project:
    "Files/Save" "sphere"
  23. generate the inp file:
  24. the inp file will be stored in the project directory "sphere.gid" with the suffix "dat", e.g. "sphere.dat"
  25. rename the inp file:
    mv sphere.dat sphere.inp

Now you can check the validity of the inp file by rendering it with ParaView (cf. section Postprocessing).


MSC.Patran is another CAD program, which includes a mesher to generate finite element meshes. It can export the finite element meshes as "neutral files", which can also be imported by magpar.

magpar - Parallel Finite Element Micromagnetics Package
Copyright (C) 2002-2010 Werner Scholz