/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/tools/gmsh.py is in python-pysph 0~20160514.git91867dc-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | """Utility module to read input mesh files. This is primarily for meshes
generated using Gmsh. This module also provides some simple classes
that allow one to create extruded 3D surfaces by generating a gmsh file
in Python.
There is also a function to read VTK dataset and produce points from them.
This is very useful as Gmsh can generate VTK datasets from its meshes and thus
the meshes can be imported as point clouds that may be used in an SPH
simulation.
"""
# Copyright (c) 2015 Prabhu Ramachandran
import gzip
import json
import numpy as np
import subprocess
import tempfile
from tvtk.api import tvtk
import os
from os.path import exists, expanduser, join
import sys
def _read_vtk_file(fname):
"""Given a .vtk file (or .vtk.gz), read it and return the output.
"""
if fname.endswith('.vtk.gz'):
tmpfname = tempfile.mktemp(suffix='.vtk')
with open(tmpfname, 'wb') as tmpf:
data = gzip.open(fname).read()
tmpf.write(data)
r = tvtk.DataSetReader(file_name=tmpfname)
else:
tmpfname = None
r = tvtk.DataSetReader(file_name=fname)
r.update()
if tmpfname is not None:
os.remove(tmpfname)
return r.output
def _convert_to_points(dataset, vertices=True, cell_centers=True):
"""Given a VTK dataset, convert it to a set of points that can be used
for simulation with SPH.
Parameters
----------
dataset : tvtk.DataSet
vertices: bool
If True, it converts the vertices to points.
cell_centers: bool
If True, converts the cell centers to points.
Returns
-------
x, y, z of the points.
"""
pts = np.array([], dtype=float)
if vertices:
pts = np.append(pts, dataset.points.to_array())
if cell_centers:
cell_centers = tvtk.CellCenters(input=dataset)
cell_centers.update()
p = cell_centers.output.points.to_array()
pts = np.append(pts, p)
pts.shape = len(pts)/3, 3
x, y, z = pts.T
return x, y, z
def vtk_file_to_points(fname, vertices=True, cell_centers=True):
"""Given a file containing a VTK dataset (currently only an old style .vtk
file), convert it to a set of points that can be used for simulation with
SPH.
Parameters
----------
fname : str
File name.
vertices: bool
If True, it converts the vertices to points.
cell_centers: bool
If True, converts the cell centers to points.
Returns
-------
x, y, z of the points.
"""
dataset = _read_vtk_file(fname)
return _convert_to_points(dataset, vertices, cell_centers)
def transform_points(x, y, z, transform):
"""Given the coordinates, x, y, z and the TVTK transform instance,
return the transformed coordinates.
"""
assert isinstance(transform, tvtk.Transform)
m = transform.matrix.to_array()
xt, yt, zt, wt = np.dot(m, np.array((x, y, z, np.ones_like(x))))
return xt, yt, zt
class Loop(object):
"""Create a Line Loop in Gmsh parlance but using a turtle-graphics like
approach.
Use this to create a 2D closed surface. The surface is always in the x-y
plane.
Examples
--------
Here is a simple example::
>>> l1 = Loop((0.0, 0.0), mesh_size=0.1)
>>> l1.move(1.0).turn(90).move(1.0).turn(90).move(1.0).turn(90).move(1.0)
This will create a square shape.
"""
def __init__(self, start, mesh_size=0.1):
self.mesh_size = mesh_size
self.points = [start]
self.elems = []
self.tolerance = 1e-12
self._last_angle = 0.0
self._last_position = start
self._index = 1
### Public Protocol ###################################
def turn(self, angle):
"""Turn by angle (in degrees).
"""
self._last_angle += angle
return self
def move(self, dist):
"""Move by given distance at the current angle.
"""
x0, y0 = self._last_position
angle = self._last_angle
rad = np.pi*angle/180.
x = x0 + dist*np.cos(rad)
y = y0 + dist*np.sin(rad)
p1 = self._index
p2 = self._add_point(x, y)
self._add_elem('line', (p1, p2))
return self
def arc(self, radius, angle=180):
"""Create a circular arc given the radius and for an angle as
specified.
"""
x0, y0 = self._last_position
last_angle = self._last_angle
rad = np.pi*last_angle/180.
last = complex(x0, y0)
dz = complex(np.cos(rad), np.sin(rad))
cen = last + radius*dz
rad = np.pi*angle/180.
end = cen + (last - cen)*complex(np.cos(rad), np.sin(rad))
s_idx = self._index
c_idx = self._add_point(cen.real, cen.imag)
e_idx = self._add_point(end.real, end.imag)
self._add_elem('circle', (s_idx, c_idx, e_idx))
return self
def write(self, fp, point_id_base=0, elem_id_base=0):
"""Write data to given file object `fp`.
"""
points = self.points
for i in range(len(points)):
idx = point_id_base + i + 1
self._write_point(fp, points[i], idx)
elems = self.elems
idx = 0
loop = []
for i in range(len(elems)):
elem = elems[i]
idx = elem_id_base + i + 1
loop.append(str(idx))
kind, data = elem[0], elem[1]
if kind == 'line':
self._write_line(fp, data, idx, point_id_base)
elif kind == 'circle':
self._write_circle(fp, data, idx, point_id_base)
idx += 1
ids = ', '.join(loop)
s = 'Line Loop({idx}) = {{{ids}}};\n'.format(idx=idx, ids=ids)
fp.write(s)
return len(points), len(elems) + 1
### Private Protocol #################################
def _add_elem(self, kind, data):
self.elems.append((kind, data))
def _check_for_existing_point(self, x, y):
tol = self.tolerance
for i, (xi, yi) in enumerate(self.points):
if abs(xi - x) < tol and abs(yi - y) < tol:
return i + 1
def _add_point(self, x, y):
last_x, last_y = self._last_position
pnt = (x, y)
existing = self._check_for_existing_point(x, y)
if existing is None:
self.points.append(pnt)
self._index += 1
index = self._index
else:
index = existing
pnt = self.points[existing]
self._last_position = pnt
return index
def _write_point(self, fp, pnt, idx):
fp.write('Point({idx}) = {{{x}, {y}, 0.0, {mesh_size}}};\n'.\
format(x=pnt[0], y=pnt[1], idx=idx, mesh_size=self.mesh_size))
def _write_line(self, fp, data, e_idx, p_idx):
s = 'Line({idx}) = {{{p1}, {p2}}};\n'.format(
idx=e_idx, p1=data[0]+p_idx, p2=data[1] +p_idx
)
fp.write(s)
def _write_circle(self, fp, data, e_idx, p_idx):
s = 'Circle({idx}) = {{{start}, {cen}, {end}}};\n'.format(
idx=e_idx, start=data[0]+p_idx, cen=data[1]+p_idx, end=data[2] +p_idx
)
fp.write(s)
class Surface(object):
def __init__(self, *loops):
"""Constructor.
Parameters
----------
loops : tuple(Loop)
Any additional positional arguments are treated as loop objects.
"""
self.loops = list(loops)
self.idx = 0
def write(self, fp, point_id_base=0, elem_id_base=0):
pid_base = point_id_base
eid_base = elem_id_base
loop_ids = []
for loop in self.loops:
np, ne = loop.write(fp, pid_base, eid_base)
pid_base += np
eid_base += ne
loop_ids.append(str(eid_base))
idx = eid_base + 1
loop_str = ', '.join(loop_ids)
fp.write('Plane Surface({idx}) = {{{loop_str}}};\n'.format(
idx=idx, loop_str=loop_str))
self.idx = idx
return pid_base, eid_base
class Extrude(object):
def __init__(self, dx=0.0, dy=0.0, dz=1.0, surfaces=None):
"""Extrude a given set of surfaces by the displacements given
along each directions.
Parameters
----------
dx : float
Extrusion along x.
dy : float
Extrusion along y.
dz : float
Extrusion along z.
surfaces: list
List of surfaces to extrude.
"""
self.dx, self.dy, self.dz = dx, dy, dz
if surfaces is None:
self.surfaces = []
else:
self.surfaces = list(surfaces)
def write(self, fp, point_id_base=0, elem_id_base=0):
pid_base = point_id_base
eid_base = elem_id_base
surf_ids = []
for surf in self.surfaces:
np, ne = surf.write(fp, pid_base, eid_base)
pid_base += np
eid_base += ne
surf_ids.append(str(surf.idx))
s_ids = ', '.join(surf_ids)
fp.write(
'Extrude {{{dx}, {dy}, {dz}}} {{\n'
' Surface{{{s_ids}}};\n}}\n'.format(
dx=self.dx, dy=self.dy, dz=self.dz, s_ids=s_ids
)
)
return pid_base, eid_base
class Gmsh(object):
def __init__(self, gmsh=None):
"""Construct a Gmsh helper object that can be used to mesh objects.
Parameters
----------
gmsh: str
Path to gmsh executable.
"""
self.config = expanduser(join('~', '.pysph', 'gmsh.json'))
if gmsh is None:
if exists(self.config):
self._read_config()
else:
gmsh = self._ask_user_for_gmsh()
self._set_gmsh(gmsh)
else:
self._set_gmsh(gmsh)
#### Public Protocol ##################################
def write_geo(self, entities, fp):
"""Write a list of given entities to the given file pointer.
"""
p_count, e_count = 0, 0
for entity in entities:
np, ne = entity.write(fp, p_count, e_count)
p_count += np
e_count += ne
def write_vtk_mesh(self, entities, fname):
"""Write a list of given entities to the given file name.
"""
tmp_geo = tempfile.mktemp(suffix='.geo')
try:
self.write_geo(entities, open(tmp_geo, 'w'))
self._call_gmsh('-3', tmp_geo, '-o', fname)
finally:
os.remove(tmp_geo)
def get_points(self, entities, vertices=True, cell_centers=False):
"""Given a list of entities, return x, y, z arrays for the position.
Parameters
----------
entities : list
List of entities.
vertices: bool
If True, it converts the vertices to points.
cell_centers: bool
If True, converts the cell centers to points.
"""
tmp_vtk = tempfile.mktemp(suffix='.vtk')
try:
self.write_vtk_mesh(entities, tmp_vtk)
x, y, z = vtk_file_to_points(
tmp_vtk, vertices=vertices, cell_centers=cell_centers
)
return x, y, z
finally:
os.remove(tmp_vtk)
def get_points_from_geo(self, geo_file_name, vertices=True,
cell_centers=False):
"""Given a .geo file, generate a mesh and get the points from the mesh.
Parameters
----------
geo_file_name: str
Filename of the .geo file.
vertices: bool
If True, it converts the vertices to points.
cell_centers: bool
If True, converts the cell centers to points.
"""
tmp_vtk = tempfile.mktemp(suffix='.vtk')
try:
self._call_gmsh('-3', geo_file_name, '-o', tmp_vtk)
return vtk_file_to_points(
tmp_vtk, vertices=vertices, cell_centers=cell_centers
)
finally:
os.remove(tmp_vtk)
#### Private Protocol #################################
def _ask_user_for_gmsh(self):
gmsh = input('Please provide the path to gmsh executable: ')
return gmsh
def _read_config(self):
if exists(self.config):
data = json.load(open(self.config))
self.gmsh = data['path']
def _set_gmsh(self, gmsh):
self.gmsh = gmsh
data = dict(path=gmsh)
json.dump(data, open(self.config, 'w'))
def _call_gmsh(self, *args):
if self.gmsh is None:
raise RuntimeError('Gmsh is not configured, set the gmsh path.')
cmd = [self.gmsh] + list(args)
subprocess.check_call(cmd)
def example_3d_p(fp=sys.stdout):
"""Creates a 3D "P" with a hole inside it.
"""
# The exterior of the "P"
l1 = Loop((0.0, 0.0), mesh_size=0.1)
l1.turn(-90).move(1.0).turn(90).move(0.2).turn(90).move(0.5)\
.arc(0.25, -180).turn(90).move(0.2)
# The inner loop for the hole in the middle.
l2 = Loop((0.1, -0.25))
l2.arc(0.1, 90).turn(90).arc(0.1, 90).turn(90)\
.arc(0.1, 90).turn(90).arc(0.1, 90)
s = Surface(l1, l2)
ex = Extrude(0.0, 0.0, 1.0, surfaces=[s])
ex.write(fp)
return ex
def example_cube(fp=sys.stdout):
"""Simple example of a cube.
"""
l1 = Loop((0.0, 0.0), mesh_size=0.1)
l1.move(1.0).turn(90).move(1.0).turn(90).move(1.0).turn(90).move(1.0)
s = Surface(l1)
ex = Extrude(0.0, 0.0, 1.0, surfaces=[s])
ex.write(fp)
return ex
def example_plot_3d_p(gmsh):
"""Note: this will only work if you have gmsh installed.
"""
import io
fp = io.StringIO()
ex = example_3d_p(fp)
g = Gmsh(gmsh)
x, y, z = g.get_points([ex])
from mayavi import mlab
mlab.points3d(x, y, z, color=(1, 0, 0))
|