-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmanta2vdb.py
More file actions
86 lines (63 loc) · 2.35 KB
/
manta2vdb.py
File metadata and controls
86 lines (63 loc) · 2.35 KB
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
import os
from argparse import ArgumentParser
import numpy as np
from pathlib import Path
# parse args
parser = ArgumentParser()
parser.add_argument(
"-d", "--directory", required=True, help="input directory for processing"
)
parser.add_argument(
"-res",
"--resolution",
nargs="+",
required=True,
help="X Y Z resolution of the phiflow simulation",
)
args = parser.parse_args()
#initialize mantaflow fields
res = [int(x) for x in args.resolution]
if len(res) != 3:
raise ValueError("Expected 3 resolution values: X Y Z")
gs = [res[2], res[1], res[0]]
s = FluidSolver(name="low-res", gridSize=vec3(gs[2], gs[1], gs[0]), dim=3)
# prepare grids
gV = s.create(MACGrid)
gD = s.create(RealGrid)
vort = s.create(VecGrid)
vortn = s.create(RealGrid)
# input / output
input_dir = Path(args.directory)
if not input_dir.exists():
raise ValueError(f"{input_dir} does not exist, choose a sim that exists.")
output_dir = input_dir/'to_vdb/'
output_dir.mkdir(parents=True, exist_ok=True)
# Find all smoke and velocity files in input directory
vel_files = []
smoke_files = []
for dirpath, dirnames, filenames in os.walk(input_dir):
for file in filenames:
if file.endswith(".npz"):
full_data_file = os.path.join(dirpath, file)
if "velocity" in file:
vel_files.append(full_data_file)
if "smoke" in file:
smoke_files.append(full_data_file)
# save velocity and vorticity to OpenVDB
for file_name in vel_files:
data = np.load(file_name)["data"]
data = data[:-1:, :-1, :-1, ::] # crop, and flip XYZ vel channels again!
copyArrayToGridMAC(target=gV, source=data)
computeVorticity(gV, vort, vortn)
vn = np.zeros(shape=(gs[0], gs[1], gs[2], 1), dtype=np.float32)
copyGridToArrayReal(target=vn, source=vortn)
copyArrayToGridReal(target=vortn, source=vn)
# write vdb file
gV.save(output_dir.__str__() + f"/{file_name.split('.')[0].rsplit('/')[-1]}.vdb")
vortn.save(output_dir.__str__() + f"/{file_name.split('.')[0].rsplit('/')[-1]}.vdb".replace("velocity", "vorticity"))
# save smoke to OpenVDB
for file_name in smoke_files:
data = np.load(file_name)["data"]
copyArrayToGridReal(target=gD, source=data)
gD.save(output_dir.__str__() + f"/{file_name.split('.')[0].rsplit('/')[-1]}.vdb")
print(f"\n\nThe files are saved in:\n {output_dir}")