-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdothick.py
More file actions
156 lines (120 loc) · 4.2 KB
/
Copy pathdothick.py
File metadata and controls
156 lines (120 loc) · 4.2 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
#!/usr/bin/env python3
"""
Create grid_thick.txt from CA DOTABLE grid output using a simple
SCALEPPM-style neighborhood thickening:
If a cell is solid, or lies within R=1 of a solid cell,
write #liqCells = 0.0 in the output file.
Only the #liqCells column is changed. Composition, phase, temperature,
orientation, and all other columns are preserved.
Default:
input = grid.txt
output = grid_thick.txt
"""
from __future__ import annotations
import argparse
from pathlib import Path
import numpy as np
COL_X = 0
COL_Y = 1
COL_LCELLS = 2
SOLID_THRESHOLD = 0.5
RTHICK = 1
def parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(
description="Write grid_thick.txt using simple R=1 display thickening."
)
p.add_argument("input", nargs="?", default="grid.txt",
help="Input grid file [default: grid.txt]")
p.add_argument("-o", "--output", default="grid_thick.txt",
help="Output grid file [default: grid_thick.txt]")
p.add_argument("--threshold", type=float, default=SOLID_THRESHOLD,
help="Solid threshold for #liqCells [default: 0.5]")
return p.parse_args()
def load_grid(path: Path) -> np.ndarray:
rows = []
with path.open("r", encoding="utf-8", errors="replace") as f:
for line in f:
s = line.strip()
if not s:
continue
if s.upper().startswith("X Y"):
continue
parts = s.split()
if len(parts) >= 8:
try:
rows.append([
int(parts[0]), int(parts[1]),
float(parts[2]), float(parts[3]),
float(parts[4]), float(parts[5]),
float(parts[6]), float(parts[7]),
])
continue
except ValueError:
pass
# Legacy fixed-width fallback:
# (2I4,3F10.3,2F10.2,F10.4)
try:
rows.append([
int(line[0:4]), int(line[4:8]),
float(line[8:18]), float(line[18:28]),
float(line[28:38]), float(line[38:48]),
float(line[48:58]), float(line[58:68]),
])
except Exception:
continue
if not rows:
raise ValueError(f"No valid grid data found in {path}")
return np.asarray(rows, dtype=float)
def simple_r1_thicken(data: np.ndarray, threshold: float) -> np.ndarray:
out = data.copy()
x = data[:, COL_X].astype(int)
y = data[:, COL_Y].astype(int)
lc = data[:, COL_LCELLS]
# Accept either 0-based or 1-based grid coordinates.
x0 = int(x.min())
y0 = int(y.min())
ix = x - x0
iy = y - y0
nx = int(ix.max()) + 1
ny = int(iy.max()) + 1
lc_grid = np.full((ny, nx), np.nan, dtype=float)
lc_grid[iy, ix] = lc
solid = np.isfinite(lc_grid) & (lc_grid < threshold)
display_solid = solid.copy()
ys, xs = np.where(solid)
for dy in (-1, 0, 1):
for dx in (-1, 0, 1):
yy = ys + dy
xx = xs + dx
keep = (xx >= 0) & (xx < nx) & (yy >= 0) & (yy < ny)
display_solid[yy[keep], xx[keep]] = True
lc_thick_grid = lc_grid.copy()
lc_thick_grid[display_solid] = 0.0
out[:, COL_LCELLS] = lc_thick_grid[iy, ix]
return out
def save_grid(path: Path, data: np.ndarray) -> None:
np.savetxt(
path,
data,
fmt=[
"%4d", "%4d", "%10.3f", "%10.3f",
"%10.3f", "%10.2f", "%10.2f", "%10.4f",
],
header="X Y #liqCells Lcomp Scomp Phase Temp Orient",
comments="",
)
def main() -> int:
args = parse_args()
in_path = Path(args.input)
out_path = Path(args.output)
data = load_grid(in_path)
out = simple_r1_thicken(data, args.threshold)
save_grid(out_path, out)
changed = int(np.sum(data[:, COL_LCELLS] != out[:, COL_LCELLS]))
print(f"Input : {in_path}")
print(f"Output : {out_path}")
print(f"Changed : {changed} #liqCells entries")
print("Method : SCALEPPM-style R=1 neighborhood thickening")
return 0
if __name__ == "__main__":
raise SystemExit(main())