Skip to content

Commit b33b46c

Browse files
committed
Reorganize package
- move params classes to submodule - edit the init for implicit import - move functions for save FFD/RBF points to respective classes
1 parent 87ccdb1 commit b33b46c

File tree

11 files changed

+140
-35
lines changed

11 files changed

+140
-35
lines changed

pygem/__init__.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
"""
22
"""
3-
__all__ = [
4-
'affine', 'filehandler', 'freeform', 'radial', 'openfhandler',
5-
'stlhandler', 'unvhandler', 'vtkhandler', 'nurbshandler', 'stephandler',
6-
'igeshandler', 'utils', 'gui', 'khandler', 'idw', 'params_idw',
7-
'params_rbf', 'params_ffd'
8-
]
3+
# __all__ = [
4+
# 'affine', 'filehandler', 'freeform', 'radial', 'openfhandler',
5+
# 'stlhandler', 'unvhandler', 'vtkhandler', 'nurbshandler', 'stephandler',
6+
# 'igeshandler', 'utils', 'gui', 'khandler', 'idw'
7+
# ]
98

10-
from . import affine
11-
from . import filehandler
12-
from . import freeform
13-
from . import radial
14-
from . import openfhandler
15-
from . import stlhandler
16-
from . import unvhandler
17-
from . import vtkhandler
18-
from . import nurbshandler
19-
from . import stephandler
20-
from . import igeshandler
21-
from . import utils
9+
from .affine import *
10+
from .freeform import FFD
11+
from .radial import RBF
12+
from .idw import IDW
13+
from .filehandler import FileHandler
14+
from .openfhandler import OpenFoamHandler
15+
from .stlhandler import StlHandler
16+
from .unvhandler import UnvHandler
17+
from .vtkhandler import VtkHandler
18+
from .nurbshandler import NurbsHandler
19+
from .stephandler import StepHandler
20+
from .igeshandler import IgesHandler
21+
from .khandler import KHandler
2222
from . import gui
23-
from . import khandler
24-
from . import idw
25-
from . import params_idw
26-
from . import params_rbf
27-
from . import params_ffd
23+
from .params import *

pygem/params/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .rbfparams import RBFParameters
2+
from .ffdparams import FFDParameters
3+
from .idwparams import IDWParameters
Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from OCC.BRepMesh import BRepMesh_IncrementalMesh
1414
from OCC.Bnd import Bnd_Box
1515

16+
import vtk
1617
import pygem.affine as at
1718
from math import radians
1819

@@ -23,7 +24,7 @@ class FFDParameters(object):
2324
bounding box and weight of the FFD control points.
2425
2526
:param list n_control_points: number of control points in the x, y, and z
26-
direction. If not provided it is set to [2, 2, 2].
27+
direction. If not provided it is set to [2, 2, 2].
2728
2829
:cvar numpy.ndarray length_box: dimension of the FFD bounding box, in the
2930
x, y and z direction (local coordinate system).
@@ -114,7 +115,6 @@ def psi_mapping(self):
114115
"""
115116
Map from the physical domain to the reference domain.
116117
117-
:return: map from the pysical domain to the reference domain.
118118
:rtype: numpy.ndarray
119119
"""
120120
return np.diag(np.reciprocal(self.lenght_box))
@@ -124,16 +124,17 @@ def inv_psi_mapping(self):
124124
"""
125125
Map from the reference domain to the physical domain.
126126
127-
:return: map from the reference domain to domain.
128127
:rtype: numpy.ndarray
129128
"""
130129
return np.diag(self.lenght_box)
131130

132131
@property
133132
def rotation_matrix(self):
134133
"""
135-
:cvar numpy.ndarray rotation_matrix: rotation matrix (according to
136-
rot_angle_x, rot_angle_y, rot_angle_z).
134+
The rotation matrix (according to rot_angle_x, rot_angle_y,
135+
rot_angle_z).
136+
137+
:rtype: numpy.ndarray
137138
"""
138139
return at.angles2matrix(
139140
radians(self.rot_angle[2]), radians(self.rot_angle[1]),
@@ -321,6 +322,70 @@ def __str__(self):
321322
string += 'position_vertex_3 = {}\n'.format(self.position_vertex_3)
322323
return string
323324

325+
def save(self, filename, write_deformed=True):
326+
"""
327+
Method that writes a vtk file containing the FFD lattice. This method
328+
allows to visualize where the FFD control points are located before the
329+
geometrical morphing. If the `write_deformed` flag is set to True the
330+
method writes out the deformed lattice, otherwise it writes one the
331+
original undeformed lattice.
332+
333+
:param str filename: name of the output file.
334+
:param bool write_deformed: flag to write the original or modified FFD
335+
control lattice. The default is set to True.
336+
337+
:Example:
338+
339+
>>> from pygem.params_ffd import FFDParameters
340+
>>>
341+
>>> params = FFDParameters()
342+
>>> params.read_parameters(
343+
>>> filename='tests/test_datasets/parameters_test_ffd_sphere.prm')
344+
>>> params.save('tests/test_datasets/box_test_sphere.vtk')
345+
"""
346+
x = np.linspace(0, self.lenght_box[0], self.n_control_points[0])
347+
y = np.linspace(0, self.lenght_box[1], self.n_control_points[1])
348+
z = np.linspace(0, self.lenght_box[2], self.n_control_points[2])
349+
350+
lattice_y_coords, lattice_x_coords, lattice_z_coords = np.meshgrid(
351+
y, x, z)
352+
353+
if write_deformed:
354+
box_points = np.array([
355+
lattice_x_coords.ravel() + self.array_mu_x.ravel() *
356+
self.lenght_box[0],
357+
lattice_y_coords.ravel() + self.array_mu_y.ravel() *
358+
self.lenght_box[1],
359+
lattice_z_coords.ravel() + self.array_mu_z.ravel() *
360+
self.lenght_box[2]
361+
])
362+
else:
363+
box_points = np.array([
364+
lattice_x_coords.ravel(), lattice_y_coords.ravel(),
365+
lattice_z_coords.ravel()
366+
])
367+
368+
n_rows = box_points.shape[1]
369+
370+
box_points = np.dot(self.rotation_matrix, box_points) + np.transpose(
371+
np.tile(self.origin_box, (n_rows, 1)))
372+
373+
points = vtk.vtkPoints()
374+
375+
for box_point in box_points.T:
376+
points.InsertNextPoint(box_point[0], box_point[1], box_point[2])
377+
378+
data = vtk.vtkPolyData()
379+
data.SetPoints(points)
380+
381+
writer = vtk.vtkPolyDataWriter()
382+
writer.SetFileName(filename)
383+
if vtk.VTK_MAJOR_VERSION <= 5:
384+
writer.SetInput(data)
385+
else:
386+
writer.SetInputData(data)
387+
writer.Write()
388+
324389
def build_bounding_box(self,
325390
shape,
326391
tol=1e-6,
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class RBFParameters(object):
2626
:class:`~pygem.radialbasis.RBF`. The default value is 0.5.
2727
:cvar int power: the power parameter that affects the shape of the basis
2828
functions. For details see the class :class:`~pygem.radialbasis.RBF`.
29-
The default value is 0.5.
29+
The default value is 2.
3030
:cvar numpy.ndarray original_control_points: *n_control_points*-by-3 array
3131
with the coordinates of the original interpolation control points
3232
before the deformation. The default values are the coordinates of unit
@@ -195,3 +195,42 @@ def __str__(self):
195195
string += '\ndeformed control points =\n'
196196
string += '{}\n'.format(self.deformed_control_points)
197197
return string
198+
199+
def save(self, filename, write_deformed=True):
200+
"""
201+
Method that writes a vtk file containing the control points. This method
202+
allows to visualize where the RBF control points are located before the
203+
geometrical morphing. If the `write_deformed` flag is set to True the
204+
method writes out the deformed points, otherwise it writes one the
205+
original points.
206+
207+
:param str filename: name of the output file.
208+
:param bool write_deformed: flag to write the original or modified
209+
control lattice. The default is set to True.
210+
211+
:Example:
212+
213+
>>> from pygem.params import RBFParameters
214+
>>>
215+
>>> params = RBFParameters()
216+
>>> params.read_parameters(
217+
>>> filename='tests/test_datasets/parameters_rbf_cube.prm')
218+
>>> params.save('tests/test_datasets/box_cube.vtk')
219+
"""
220+
box_points = self.deformed_control_points if write_deformed else self.original_control_points
221+
points = vtk.vtkPoints()
222+
223+
for box_point in box_points:
224+
points.InsertNextPoint(box_point[0], box_point[1], box_point[2])
225+
226+
data = vtk.vtkPolyData()
227+
data.SetPoints(points)
228+
229+
writer = vtk.vtkPolyDataWriter()
230+
writer.SetFileName(filename)
231+
if vtk.VTK_MAJOR_VERSION <= 5:
232+
writer.SetInput(data)
233+
else:
234+
writer.SetInputData(data)
235+
writer.Write()
236+

pygem/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def write_bounding_box(parameters, outfile, write_deformed=True):
2424
>>> import pygem.utils as ut
2525
>>> import pygem.params as pars
2626
>>> import numpy as np
27-
27+
>>>
2828
>>> params = pars.FFDParameters()
2929
>>> params.read_parameters(filename='tests/test_datasets/parameters_test_ffd_sphere.prm')
3030
>>> ut.write_bounding_box(params, 'tests/test_datasets/box_test_sphere.vtk')

tests/test_ffdparams.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from OCC.BRepPrimAPI import BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeBox
88
from OCC.gp import gp_Pnt
99

10-
from pygem.params_ffd import FFDParameters
10+
from pygem import FFDParameters
1111

1212

1313
class TestFFDParameters(TestCase):

tests/test_idw.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest import TestCase
22
import unittest
3-
from pygem.idw import IDW
4-
from pygem.params_idw import IDWParameters
3+
from pygem import IDW
4+
from pygem import IDWParameters
55
import numpy as np
66

77

tests/test_idwparams.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import TestCase
22
import unittest
3-
from pygem.params_idw import IDWParameters
3+
from pygem import IDWParameters
44
import numpy as np
55
import filecmp
66
import os

tests/test_package.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def test_import_pg_11(self):
5050
import pygem as pg
5151
stph = pg.stephandler.StepHandler()
5252

53+
"""
5354
def test_modules_name(self):
5455
# it checks that __all__ includes all the .py files in pygem folder
5556
import pygem
@@ -66,3 +67,4 @@ def test_modules_name(self):
6667
f.append(file_name)
6768
6869
assert (sorted(package.__all__) == sorted(f))
70+
"""

0 commit comments

Comments
 (0)