-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNeperMicrostructure.py
More file actions
172 lines (149 loc) · 5.71 KB
/
Copy pathNeperMicrostructure.py
File metadata and controls
172 lines (149 loc) · 5.71 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import subprocess
from pathlib import Path
import h5py
import numpy as np
from MSUtils.general.grid import GridSpec
from MSUtils.general.MicrostructureImage import MicrostructureImage
def _line(file) -> str:
return file.readline().decode().strip()
def _expect(file, expected: str) -> None:
if _line(file) != expected:
raise ValueError(f"Expected {expected!r} in {file.name}.")
def _read_faces(filename: str | Path):
with Path(filename).open(encoding="utf-8") as file:
for line in file:
values = line.split()
poly_a, poly_b, vertex_count = map(int, values[:3])
vertices = np.asarray(values[3:], dtype=float).reshape(vertex_count, 3)
yield poly_a, poly_b, vertices
class NeperMicrostructure(MicrostructureImage):
"""Generate and read a Neper 5 raster tessellation."""
def __init__(
self,
output_stem,
*,
neper_executable,
Nx,
Ny,
Nz,
L,
num_grains,
morphology,
orientation,
periodicity,
crystal_symmetry,
seed,
extra_args=(),
):
output_stem = Path(output_stem).resolve()
output_stem.parent.mkdir(parents=True, exist_ok=True)
command = [
str(neper_executable),
"-T",
"-n",
str(num_grains),
"-id",
str(seed),
"-domain",
f"cube({','.join(map(str, L))})",
"-tesrsize",
f"{Nx}:{Ny}:{Nz}",
"-tesrformat",
"binary16",
"-morpho",
morphology,
"-crysym",
crystal_symmetry,
"-ori",
orientation,
"-oridescriptor",
"rotmat:active",
"-periodicity",
periodicity,
"-statface",
"polys,vernb,vercoos",
"-statcell",
"coo,vol,area,sphericity,facenb",
*map(str, extra_args),
"-format",
"tesr,obj",
"-o",
output_stem.name,
]
subprocess.run(command, check=True, cwd=output_stem.parent)
self.tesr_filename = output_stem.with_suffix(".tesr")
with self.tesr_filename.open("rb") as file:
for marker in ("***tesr", "**format", "2.2", "**general", "3"):
_expect(file, marker)
self.resolution = tuple(map(int, _line(file).split()))
self.voxel_size = tuple(map(float, _line(file).split()))
self.origin = (0.0, 0.0, 0.0)
_expect(file, "**cell")
self.grain_count = int(_line(file))
_expect(file, "*id")
grain_ids = []
while len(grain_ids) < self.grain_count:
grain_ids.extend(map(int, _line(file).split()))
self.grain_ids = np.asarray(grain_ids)
_expect(file, "*ori")
_expect(file, "rotmat:active")
self.rotation_matrices = np.array(
[_line(file).split() for _ in range(self.grain_count)], dtype=float
).reshape(self.grain_count, 3, 3)
_expect(file, "*crysym")
self.crystal_symmetry = _line(file)
_expect(file, "**data")
_expect(file, "binary16")
voxel_count = int(np.prod(self.resolution))
self.image = np.fromfile(file, dtype="<u2", count=voxel_count)
self.image = np.ascontiguousarray(
self.image.reshape(self.resolution, order="F"), dtype=np.int32
)
self.void_present = bool(np.any(self.image == 0))
self.image += int(self.void_present) - 1
if self.void_present:
self.rotation_matrices = np.concatenate(
(np.eye(3)[None], self.rotation_matrices)
)
lengths = tuple(
size * spacing for size, spacing in zip(self.resolution, self.voxel_size)
)
super().__init__(
image=self.image,
grid=GridSpec(shape=self.resolution, lengths=lengths),
)
self._characterize(output_stem)
def _characterize(self, output_stem: Path) -> None:
stats = np.loadtxt(output_stem.with_suffix(".stcell"), ndmin=2)
self.crystal_centroids = stats[:, :3] # Cell centroids.
self.crystal_volumes = stats[:, 3] # Exact cell volumes.
self.crystal_surface_areas = stats[:, 4] # Total cell surface areas.
self.crystal_sphericities = stats[:, 5] # Equal-volume sphere area / area.
self.crystal_face_counts = stats[:, 6].astype(int) # Faces per cell.
self.interface_area = 0.0
self.Ltensor = np.zeros((3, 3)) # Area-weighted second normal moment.
for _, _, vertices in _read_faces(output_stem.with_suffix(".stface")):
area_vector = 0.5 * np.sum(
np.cross(vertices, np.roll(vertices, -1, axis=0)), axis=0
)
area = np.linalg.norm(area_vector)
normal = area_vector / area
self.interface_area += area
self.Ltensor += area * np.einsum("i,j->ij", normal, normal)
def write_h5(
self,
h5_filename: str | Path,
grp_name: str,
order: str = "zyx",
compression_level: int = 6,
) -> None:
"""Write the microstructure and its rotation matrices."""
group_name = grp_name.strip("/")
super().write(
h5_filename, f"{group_name}/microstructure", order, compression_level
)
with h5py.File(self.h5_filename, "a") as h5_file:
group = h5_file[group_name or "/"]
if "rotation_matrices" in group:
del group["rotation_matrices"]
group.create_dataset("rotation_matrices", data=self.rotation_matrices)