Skip to content

Commit 7c5d919

Browse files
committed
VTK Writer Functionality Added
1 parent ca126a5 commit 7c5d919

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

diffmpm/writers.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import numpy as np
44
from pathlib import Path
5+
from pyevtk.hl import pointsToVTK
56

67
logger = logging.getLogger(__file__)
78

@@ -31,3 +32,66 @@ def write(self, args, transforms, **kwargs):
3132
filepath.parent.mkdir(parents=True)
3233
np.savez(filepath, **arrays)
3334
logger.info(f"Saved particle data for step {step} at {filepath}")
35+
36+
37+
class VTKWriter(Writer):
38+
def write(self, args, transforms, **kwargs):
39+
arrays, step = args
40+
max_digits = int(np.log10(kwargs["max_steps"])) + 1
41+
if step == 0:
42+
req_zeros = max_digits - 1
43+
else:
44+
req_zeros = max_digits - (int(np.log10(step)) + 1)
45+
fileno = f"{'0' * req_zeros}{step}"
46+
filepath = Path(kwargs["out_dir"]).joinpath(f"particles_{fileno}")
47+
if not filepath.parent.is_dir():
48+
filepath.parent.mkdir(parents=True)
49+
coords = np.array(arrays["loc"])
50+
x = coords[:, :, 0].flatten()
51+
y = coords[:, :, 1].flatten()
52+
z = np.zeros_like(x)
53+
strain = np.array(arrays["strain"])
54+
stress = np.array(arrays["stress"])
55+
velocity = np.array(arrays["velocity"])
56+
velocity_x = velocity[:, :, 0].flatten()
57+
veloctiy_y = velocity[:, :, 1].flatten()
58+
velocity_z = np.zeros_like(velocity_x)
59+
strain_xx = strain[:, :, 0].flatten()
60+
strain_yy = strain[:, :, 1].flatten()
61+
strain_zz = strain[:, :, 2].flatten()
62+
strain_xy = strain[:, :, 3].flatten()
63+
strain_xz = strain[:, :, 4].flatten()
64+
strain_yz = strain[:, :, 5].flatten()
65+
stress_xx = stress[:, :, 0].flatten()
66+
stress_yy = stress[:, :, 1].flatten()
67+
stress_zz = stress[:, :, 2].flatten()
68+
stress_xy = stress[:, :, 3].flatten()
69+
stress_xz = stress[:, :, 4].flatten()
70+
stress_yz = stress[:, :, 5].flatten()
71+
x = coords[:, :, 0].flatten()
72+
y = coords[:, :, 1].flatten()
73+
z = np.zeros_like(x)
74+
pointsToVTK(
75+
f"{filepath}",
76+
np.array(x),
77+
np.array(y),
78+
z,
79+
data={
80+
"strain_xx": strain_xx,
81+
"strain_yy": strain_yy,
82+
"strain_zz": strain_zz,
83+
"strain_xy": strain_xy,
84+
"strain_xz": strain_xz,
85+
"strain_yz": strain_yz,
86+
"stress_xx": stress_xx,
87+
"stress_yy": stress_yy,
88+
"stress_zz": stress_zz,
89+
"stress_xy": stress_xy,
90+
"stress_xz": stress_xz,
91+
"stress_yz": stress_yz,
92+
"velocity_x": velocity_x,
93+
"velocity_y": veloctiy_y,
94+
"velocity_z": velocity_z,
95+
},
96+
)
97+
logger.info(f"Saved particle data for step {step} at {filepath}")

0 commit comments

Comments
 (0)