Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

The easiest way to call Netgen from Python

Project description

ngsimple: The easiest way to call Netgen from Python

PyPI

Suppose you wanted to use Netgen in a Python script to generate tetrahedral meshes but did not want to spend time installing the entire Netgen/NGSolve package which ships with it's own Python interpreter. This is a simple wrapper for running Netgen mesh generator inside a container and functions for calling it transparently from any Python interpreter.

This is work-in-progress. Currently using the following container: https://hub.docker.com/r/pymor/ngsolve_py3.7. In future, plan is to use a dedicated more lightweight container.

Installation

Install with

pip install ngsimple

ngsimple requires Docker and docker-py for pulling container images and interacting with the mesh generator. The output mesh is read back to Python using meshio.

Example

The examples demonstrate how ngsimple can be coupled with external packages.

Visualize with vedo

from ngsimple import generate

mesh = generate("""
algebraic3d

solid main = sphere (0, 0, 0; 1);

tlo main;
point (0, 0, 0);
""")

from vedo import show

show(mesh)

Netgen output mesh

Load to scikit-fem and draw

from ngsimple import generate
from skfem.io.meshio import from_meshio
from skfem.visuals.matplotlib import draw, show

m = from_meshio(generate("""algebraic3d

solid cyls = cylinder ( -100, 0, 0; 200, 0, 0; 40 )
          or cylinder ( 100, -100, 100; 100, 200, 100; 40)
          or cylinder ( 0, 100, -100; 0, 100, 200; 40);
solid sculpture = sphere (50, 50, 50; 80) and not cyls
        and not sphere (50, 50, 50; 50);

tlo sculpture -col=[0.5, 0.5, 0.5];"""))

draw(m)
show()

Matplotlib

Load to pyfvm and solve Poisson equation

Snippet adapted from pyfvm example. (GPLv3 license).

from ngsimple import generate
import pyfvm
from pyfvm.form_language import *
import meshzoo
from scipy.sparse import linalg
import meshplex

class Poisson(object):
    def apply(self, u):
        return integrate(lambda x: -n_dot_grad(u(x)), dS) \
             - integrate(lambda x: 1.0, dV)

    def dirichlet(self, u):
        return [(lambda x: u(x) - 0.0, Boundary())]


mesh = generate("""algebraic3d

solid tor = torus ( 0, 0, 0; 1, 0, 0; 2 ; 1 );

tlo tor;""")

mesh = meshplex.MeshTetra(mesh.points, mesh.get_cells_type("tetra"))

matrix, rhs = pyfvm.discretize_linear(Poisson(), mesh)

u = linalg.spsolve(matrix, rhs)

mesh.write('out.vtk', point_data={'u': u})

Paraview

See Netgen tutorials for more information on the format expected by generate.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ngsimple, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size ngsimple-0.1.1-py3-none-any.whl (14.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size ngsimple-0.1.1.tar.gz (13.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page