Python Bindings for HybMesh¶
Usage¶
Python wrapper contains a source file Hybmesh.py
and shared library core_hmconnection_py
.
To use it in your application (either Python 2 or Python 3)
simply copy the source file into application directory and import
it as normal python file as
from Hybmesh import Hybmesh
Shared library can be left where it is. Its absolute path along with absolute path to installed hybmesh executable is written into Hybmesh.hybmesh_lib_path and Hybmesh.hybmesh_exec_path static properties respectively. Those fields can be reassigned before first construction of Hybmesh object if default values are not correct.
Hybmesh class supports creation under python with
statement. By calling it as
from Hybmesh import Hybmesh
with Hybmesh() as hm:
# ... operations ...
it is guaranteed that superclass object will
be destroyed immediately at the end of with
block and
hybmesh subprocess will be closed.
Otherwise the subprocess will wait until
garbage collector executes superclass destructor.
For detailed description of all methods consult
python wrapper reference
and embedded documentation of Hybmesh.py
source file.
Helloworld Example¶
After installation of hybmesh create a directory with copied Hybmesh.py and the following hmtest.py files.
from Hybmesh import Hybmesh
with Hybmesh() as hm:
g4 = hm.add_unf_rect_grid([0, 0], [1, 1], 2, 2)
g4dims = g4.dims()
print("number of cells = {}".format(g4dims[2]))
Run terminal command
python hmtest.py
Introductory Example¶
The following example illustrates numerical computation of an integral of a function (Gaussian hill) on a grid.
It uses Square in Circle prototype grid with three different outer step sizes. For each cell of the grid it calculates approximate center point, computes target function at that point and multiplies the result by the cell area.
This code could be run both on Python 2 and Python 3 interpreters.
import math
from Hybmesh import Hybmesh
# target function: Gaussian hill with the center at [0, 0]
def expfun(x, y):
return math.exp(-(x*x + y*y)/(0.25))
# create Hybmesh connection under with block to guarantee
# its destruction after all
with Hybmesh() as hm:
# loop over different outer step sizes
for h in [0.2, 0.1, 0.05]:
# create rectangle-in-cirlce grid prototype
g = hm.add_circ_rect_grid([0, 0], 1.0, h)
# get vertices as plain 1D array
vertices = g.raw_vertices()
# get cell-vertices table as plain array.
# Here we already know that grid contains only quadrangular cells,
# hence there is no need to call g.raw_tab("cell_dim") to know
# how the plain cell_vert array should be subdivided.
cell_vert = g.raw_tab("cell_vert")
# calculating integral as the sum of cell areas multiplied by
# cell center function values.
result = 0
it = iter(cell_vert)
for ind in zip(it, it, it, it):
# ind contains four vertex indices for current cell
# x, y - are respective x and y coordinates
x = list(map(lambda i: vertices[2*i], ind))
y = list(map(lambda i: vertices[2*i+1], ind))
# calculate function value at approximate cell center
f = expfun(sum(x)/4.0, sum(y)/4.0)
# calculate area
x1, x2, x3 = x[1] - x[0], x[2] - x[0], x[3] - x[0]
y1, y2, y3 = y[1] - y[0], y[2] - y[0], y[3] - y[0]
area = 0.5*(x1*y2-y1*x2 + x2*y3-y2*x3)
result += f * area
print("h = {}: integral = {}".format(h, result))