dafoam_plot3dtransform.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """
3 This script transforms the coordinates in a plot3D file. We support three modes: scale, translate, or rotate
4 
5 Usage:
6 
7  python dafoam_plot3dscale.py scale plot3d_file_input.xyz plot3d_file_output.xyz 2 2 2
8  This will scale the x, y, and z coordinates by a factor of 2
9 
10  python dafoam_plot3dscale.py translate plot3d_file_input.xyz plot3d_file_output.xyz 1 2 3
11  This will translate the x, y, and z coordinates by 1, 2, and 3
12 
13  python dafoam_plot3dscale.py rotate plot3d_file_input.xyz plot3d_file_output.xyz x 10
14  This will rotate the x, y, and z coordinates with respect to the x axis by 10 degree
15 """
16 
17 import sys
18 from pygeo import *
19 import numpy as np
20 
21 mode = sys.argv[1]
22 
23 inputFileName = sys.argv[2]
24 outputFileName = sys.argv[3]
25 
26 if mode == "scale":
27  scaleX = float(sys.argv[4])
28  scaleY = float(sys.argv[5])
29  scaleZ = float(sys.argv[6])
30 
31  print(
32  "Scaling %s to %s with scaleX: %g scaleY: %g scaleZ: %g ...."
33  % (inputFileName, outputFileName, scaleX, scaleY, scaleZ)
34  )
35 
36  ffd = pyBlock("plot3d", fileName=inputFileName, FFD=True)
37 
38  for ivol in range(ffd.nVol):
39  vals = ffd.vols[ivol].coef
40  vals[:, :, :, 0] = vals[:, :, :, 0] * scaleX
41  vals[:, :, :, 1] = vals[:, :, :, 1] * scaleY
42  vals[:, :, :, 2] = vals[:, :, :, 2] * scaleZ
43  ffd.writePlot3dCoef(outputFileName)
44 
45  print(
46  "Scaling %s to %s with scaleX: %g scaleY: %g scaleZ: %g Done!"
47  % (inputFileName, outputFileName, scaleX, scaleY, scaleZ)
48  )
49 elif mode == "translate":
50  dX = float(sys.argv[4])
51  dY = float(sys.argv[5])
52  dZ = float(sys.argv[6])
53 
54  print("Translating %s to %s with dX: %g dY: %g dZ: %g ...." % (inputFileName, outputFileName, dX, dY, dZ))
55 
56  ffd = pyBlock("plot3d", fileName=inputFileName, FFD=True)
57 
58  for ivol in range(ffd.nVol):
59  vals = ffd.vols[ivol].coef
60  vals[:, :, :, 0] = vals[:, :, :, 0] + dX
61  vals[:, :, :, 1] = vals[:, :, :, 1] + dY
62  vals[:, :, :, 2] = vals[:, :, :, 2] + dZ
63  ffd.writePlot3dCoef(outputFileName)
64 
65  print("Translating %s to %s with dX: %g dT: %g dZ: %g Done!" % (inputFileName, outputFileName, dX, dY, dZ))
66 elif mode == "rotate":
67  axis = str(sys.argv[4])
68  deg = float(sys.argv[5])
69 
70  print("Rotating %s to %s wrt to the %s axis by %g degree...." % (inputFileName, outputFileName, axis, deg))
71 
72  ffd = pyBlock("plot3d", fileName=inputFileName, FFD=True)
73 
74  theta = deg * np.pi / 180.0
75 
76  for ivol in range(ffd.nVol):
77  vals = ffd.vols[ivol].coef
78  for i in range(vals.shape[0]):
79  for j in range(vals.shape[1]):
80  for k in range(vals.shape[2]):
81  if axis == "x":
82  yRef = vals[i, j, k, 1]
83  zRef = vals[i, j, k, 2]
84  vals[i, j, k, 1] = np.cos(theta) * yRef - np.sin(theta) * zRef
85  vals[i, j, k, 2] = np.sin(theta) * yRef + np.cos(theta) * zRef
86  elif axis == "y":
87  xRef = vals[i, j, k, 0]
88  zRef = vals[i, j, k, 2]
89  vals[i, j, k, 0] = np.cos(theta) * xRef + np.sin(theta) * zRef
90  vals[i, j, k, 2] = -np.sin(theta) * xRef + np.cos(theta) * zRef
91  elif axis == "z":
92  xRef = vals[i, j, k, 0]
93  yRef = vals[i, j, k, 1]
94  vals[i, j, k, 0] = np.cos(theta) * xRef - np.sin(theta) * yRef
95  vals[i, j, k, 1] = np.sin(theta) * xRef + np.cos(theta) * yRef
96  else:
97  print("Axis %s not supported! Options are: x, y, or z" % s)
98 
99  ffd.writePlot3dCoef(outputFileName)
100 
101  print("Rotating %s to %s wrt to the %s axis by %g degree Done!" % (inputFileName, outputFileName, axis, deg))
102 else:
103  print("Mode %s not supported! Options are: scale, translate, or rotate" % s)