-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_poly_3d.py
More file actions
129 lines (91 loc) · 3.08 KB
/
Copy pathwrite_poly_3d.py
File metadata and controls
129 lines (91 loc) · 3.08 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
import numpy as np
import json
import os
import argparse
from h5py import File
from helpers.common import *
from helpers.poly import *
parser = argparse.ArgumentParser(
prog="write_nphase_3d",
description="Writes a 3D n-phase microstructure into a moose input file",
)
parser.add_argument(
"-m",
"--euler_ang_file",
help="Dream3D microstructure file (h5py format)",
required=True,
)
parser.add_argument(
"-N",
help="Number of voxels in each direction (assumes square). If different from the given file, will override to a square mesh (USED ONLY FOR TESTING!!)",
default=None,
)
C11 = 160
C12 = 70
C44 = 60
BASE_NAME = "poly"
RADIAN_TO_DEG = 180 / np.pi
BC_VALS = np.zeros(6)
BC_VALS[0] = 0.001
# BC_VALS[1] = 0.001
# BC_VALS[2] = 0.001
BASE_TEMPLATE = "templates/local3d.i"
def write_euler_to_txt(moose_fname, d3d_fname):
f = File(d3d_fname, "r")
# break into 2 lines for legibility
cell_dat = f["DataContainers"]["SyntheticVolumeDataContainer"]["CellData"]
euler_ang = cell_dat["EulerAngles"][:]
print(euler_ang.shape)
euler_ang = euler_ang.transpose(-1, -2, -3, 0)
# euler_ang = 0 * euler_ang
# euler_ang[0, ...] = np.pi / 4
# euler_ang[2, ...] = np.pi / 4
# get spatial dims
N_x, N_y, N_z = euler_ang.shape[-3:]
print(euler_ang.shape)
euler_ang *= RADIAN_TO_DEG
# write in fortran ordering since Moose uses that
euler_ang = euler_ang.reshape(3, -1, order="C").T
np.savetxt(moose_fname, euler_ang, fmt="%.14f")
return N_x, N_y, N_z
def build_input_crystal(
d3d_fname,
C11,
C12,
C44,
bc_vals,
base_name,
input_dir=INPUT_DIR,
output_dir=OUTPUT_DIR,
N_override=None,
):
with open(BASE_TEMPLATE, "r") as f:
template = "".join(f.readlines())
template = write_BCs(bc_vals, template)
moose_poly_fname = f"{input_dir}/{base_name}_euler_ang.txt"
N_x, N_y, N_z = write_euler_to_txt(moose_poly_fname, d3d_fname)
# override size (assumes smaller than given mesh) for testing purposes
if N_override is not None:
N_x, N_y, N_z = N_override, N_override, N_override
template = write_initial_angles(f"{base_name}_euler_ang.txt", template)
# write mesh size
template = write_mesh_info(N_x, N_y, N_z, template)
# crystal coefficients
template = write_cubic_coeffs(C11, C12, C44, template)
# now write other info
template = template.replace(r"{{base_name}}", f"{base_name}")
template = template.replace(r"{{INPUT_DIR}}", f"{input_dir}")
template = template.replace(r"{{OUTPUT_DIR}}", f"{output_dir}")
template = template.replace(r"{{CRYSTAL_MODE}}", f"true")
template = template.replace(r"{{NPHASE}}", f"false")
# get rid of elastic filler
template = remove_unused(template)
return template
if __name__ == "__main__":
args = parser.parse_args()
os.makedirs(INPUT_DIR, exist_ok=True)
template = build_input_crystal(
args.euler_ang_file, C11, C12, C44, BC_VALS, BASE_NAME, N_override=args.N
)
with open(f"{INPUT_DIR}/{BASE_NAME}.i", "w") as f:
f.writelines(template)