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(
55  "Translating %s to %s with dX: %g dY: %g dZ: %g ...."
56  % (inputFileName, outputFileName, dX, dY, dZ)
57  )
58 
59  ffd = pyBlock("plot3d", fileName=inputFileName, FFD=True)
60 
61  for ivol in range(ffd.nVol):
62  vals = ffd.vols[ivol].coef
63  vals[:, :, :, 0] = vals[:, :, :, 0] + dX
64  vals[:, :, :, 1] = vals[:, :, :, 1] + dY
65  vals[:, :, :, 2] = vals[:, :, :, 2] + dZ
66  ffd.writePlot3dCoef(outputFileName)
67 
68  print(
69  "Translating %s to %s with dX: %g dT: %g dZ: %g Done!"
70  % (inputFileName, outputFileName, dX, dY, dZ)
71  )
72 elif mode == "rotate":
73  axis = str(sys.argv[4])
74  deg = float(sys.argv[5])
75 
76  print(
77  "Rotating %s to %s wrt to the %s axis by %g degree...."
78  % (inputFileName, outputFileName, axis, deg)
79  )
80 
81  ffd = pyBlock("plot3d", fileName=inputFileName, FFD=True)
82 
83  theta = deg * np.pi / 180.0
84 
85  for ivol in range(ffd.nVol):
86  vals = ffd.vols[ivol].coef
87  for i in range(vals.shape[0]):
88  for j in range(vals.shape[1]):
89  for k in range(vals.shape[2]):
90  if axis == "x":
91  yRef = vals[i, j, k, 1]
92  zRef = vals[i, j, k, 2]
93  vals[i, j, k, 1] = np.cos(theta) * yRef - np.sin(theta) * zRef
94  vals[i, j, k, 2] = np.sin(theta) * yRef + np.cos(theta) * zRef
95  elif axis == "y":
96  xRef = vals[i, j, k, 0]
97  zRef = vals[i, j, k, 2]
98  vals[i, j, k, 0] = np.cos(theta) * xRef + np.sin(theta) * zRef
99  vals[i, j, k, 2] = -np.sin(theta) * xRef + np.cos(theta) * zRef
100  elif axis == "z":
101  xRef = vals[i, j, k, 0]
102  yRef = vals[i, j, k, 1]
103  vals[i, j, k, 0] = np.cos(theta) * xRef - np.sin(theta) * yRef
104  vals[i, j, k, 1] = np.sin(theta) * xRef + np.cos(theta) * yRef
105  else:
106  print("Axis %s not supported! Options are: x, y, or z"%s)
107 
108  ffd.writePlot3dCoef(outputFileName)
109 
110  print(
111  "Rotating %s to %s wrt to the %s axis by %g degree Done!"
112  % (inputFileName, outputFileName, axis, deg)
113  )
114 else:
115  print("Mode %s not supported! Options are: scale, translate, or rotate"%s)