Skip to content

Commit 0e0ab81

Browse files
author
Julius Herb
committed
Code cleanup
1 parent 2b83e26 commit 0e0ab81

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

ntfa.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44
"""
55
import numpy as np
66
import h5py
7-
from utilities import *
8-
from material_parameters import *
7+
import re
8+
from operator import itemgetter
9+
from utilities import read_h5, construct_stress_localization, construct_stress_localization_phases, \
10+
volume_average, volume_average_phases, norm_2
11+
from material_parameters import stiffness_cu, stiffness_wsc, thermal_strain_cu, thermal_strain_wsc
12+
from interpolate_fluctuation_modes import interpolate_fluctuation_modes
13+
from optimize_alpha import opt4
14+
from microstructures import microstructures
915
from tqdm import tqdm
1016

1117

@@ -16,7 +22,7 @@ def read_snapshots(file_name, data_path):
1622
:param file_name: e.g. "input/simple_3d_rve_combo.h5"
1723
:param data_path: the path to the simulation results within the h5 file, e.g. '/ms_1p/dset0_sim'
1824
:return:
19-
strain_snapshots: plastic strain snapshots eps_p
25+
strain_snapshots: plastic strain snapshots eps_p
2026
with shape (n_integration_points, strain_dof, n_frames)
2127
"""
2228
plastic_snapshots = None
@@ -76,8 +82,8 @@ def compute_ntfa_matrices(strain_localization, stress_localization, plastic_mode
7682
n_modes = plastic_modes.shape[2]
7783
n_gp = mesh['n_integration_points']
7884
n_gauss = mesh['n_gauss']
79-
I = np.eye(6)
80-
85+
I2 = np.eye(6)
86+
8187
# slice strain localization operator E into E_eps, E_theta, E_xi
8288
E_eps = strain_localization[:, :, :strain_dof]
8389
E_theta = strain_localization[:, :, strain_dof]
@@ -89,18 +95,18 @@ def compute_ntfa_matrices(strain_localization, stress_localization, plastic_mode
8995
S_xi = stress_localization[:, :, strain_dof + 1:]
9096

9197
# Compute C_bar via < (E_eps + I).T @ S_eps >
92-
C_bar = volume_average((E_eps + I).transpose((0, 2, 1)) @ S_eps)
98+
C_bar = volume_average((E_eps + I2).transpose((0, 2, 1)) @ S_eps)
9399

94100
# Compute tau_theta via < (E_eps + I).T @ S_theta >
95-
tau_theta = volume_average(np.einsum('nij,nj->ni', (E_eps + I).transpose((0, 2, 1)), S_theta))
101+
tau_theta = volume_average(np.einsum('nij,nj->ni', (E_eps + I2).transpose((0, 2, 1)), S_theta))
96102

97103
# Compute A_bar via < (E_eps + I).T @ S_eps >
98-
A_bar = volume_average((E_eps + I).transpose((0, 2, 1)) @ S_xi)
104+
A_bar = volume_average((E_eps + I2).transpose((0, 2, 1)) @ S_xi)
99105

100106
# Compute tau_xi via < (E_theta - P_theta).T @ S_xi >
101107
# Account for the phase-wise thermal strain by an explicit summation over all integration points
102108
tau_xi = np.zeros((1, n_modes))
103-
for gp_id in prange(n_gp):
109+
for gp_id in range(n_gp):
104110
phase_id = mat_id[gp_id // n_gauss]
105111
tau_xi += (np.expand_dims(E_theta[gp_id], axis=1) - mat_thermal_strain[phase_id]).T @ S_xi[gp_id] / n_gp
106112

@@ -110,7 +116,7 @@ def compute_ntfa_matrices(strain_localization, stress_localization, plastic_mode
110116
# Compute D_theta via < (E_theta - P_theta).T @ S_theta >
111117
# Account for the phase-wise thermal strain by an explicit summation over all integration points
112118
D_theta = 0.
113-
for gp_id in prange(n_gp):
119+
for gp_id in range(n_gp):
114120
phase_id = mat_id[gp_id // n_gauss]
115121
D_theta += (np.expand_dims(E_theta[gp_id], axis=1) - mat_thermal_strain[phase_id]).T @ S_theta[gp_id] / n_gp
116122

@@ -163,7 +169,8 @@ def compute_phase_average_stress_localizations(strain_localization, mat_stiffnes
163169
combo_strain_loc0, combo_strain_loc1 = None, None
164170
stress_localization0, stress_localization1, _, _ = construct_stress_localization_phases(
165171
strain_localization, mat_stiffness, mat_thermal_strain, plastic_modes, combo_strain_loc0, combo_strain_loc1, mesh)
166-
average_stress_localization0, average_stress_localization1 = volume_average(stress_localization0), volume_average(stress_localization1)
172+
average_stress_localization0, average_stress_localization1 = \
173+
volume_average(stress_localization0), volume_average(stress_localization1)
167174
return average_stress_localization0, average_stress_localization1
168175

169176

@@ -174,15 +181,13 @@ def compute_tabular_data_for_ms(ms_id, temperatures):
174181
:param ms_id: id of the microstructure
175182
:param temperatures: list of sampling temperatures
176183
"""
177-
file_name, data_path, temp1, temp2, n_tests, sampling_alphas = itemgetter('file_name', 'data_path', 'temp1', 'temp2', 'n_tests',
178-
'sampling_alphas')(microstructures[ms_id])
184+
file_name, data_path, temp1, temp2, n_tests, sampling_alphas = \
185+
itemgetter('file_name', 'data_path', 'temp1', 'temp2', 'n_tests', 'sampling_alphas')(microstructures[ms_id])
179186
sample_temperatures = np.linspace(temp1, temp2, num=n_tests)
180-
sample_alphas = np.linspace(0, 1, num=n_tests)
181187
mesh, samples = read_h5(file_name, data_path, sample_temperatures)
182188
return compute_tabular_data(samples, mesh, temperatures)
183189

184190

185-
#@jit(nopython=True, cache=True, parallel=True, nogil=True)
186191
def compute_tabular_data(samples, mesh, temperatures):
187192
"""
188193
Compute tabular data for a given list of temperatures
@@ -235,17 +240,19 @@ def compute_tabular_data(samples, mesh, temperatures):
235240
E01 = np.ascontiguousarray(np.concatenate((E0, E1), axis=-1))
236241

237242
sampling_C = np.stack((samples[id0]['mat_stiffness'], samples[id1]['mat_stiffness'])).transpose([1, 0, 2, 3])
238-
sampling_eps = np.stack((samples[id0]['mat_thermal_strain'], samples[id1]['mat_thermal_strain'])).transpose([1, 0, 2, 3])
243+
sampling_eps = np.stack((samples[id0]['mat_thermal_strain'],
244+
samples[id1]['mat_thermal_strain'])).transpose([1, 0, 2, 3])
239245

240246
# interpolated quantities using an implicit interpolation scheme with four DOF
241247
approx_C, approx_eps = opt4(sampling_C, sampling_eps, ref_C, ref_eps)
242-
E, _ = interpolate_fluctuation_modes(E01, approx_C, approx_eps, plastic_modes, mat_id, n_gauss, strain_dof, n_modes, n_gp)
248+
E, _ = interpolate_fluctuation_modes(E01, approx_C, approx_eps, plastic_modes, mat_id,
249+
n_gauss, strain_dof, n_modes, n_gp)
243250
S = construct_stress_localization(E, ref_C, ref_eps, plastic_modes, mat_id, n_gauss, strain_dof)
244251

245252
# Compute NTFA matrices
246253
C_bar[:, :, idx], tau_theta[:, idx], A_bar[:, :, idx], tau_xi[:, idx], D_xi[:, :, idx], D_theta[idx] = \
247254
compute_ntfa_matrices(E, S, plastic_modes, ref_eps, mesh)
248-
255+
249256
# Compute phase average stresses
250257
A0_full, A1_full = compute_phase_average_stress_localizations(E, ref_C, ref_eps, plastic_modes, mesh)
251258
A0[:, :, idx], A1[:, :, idx] = A0_full, A1_full
@@ -282,17 +289,17 @@ def save_tabular_data(file_name, data_path, temperatures, C_bar, tau_theta, A_ba
282289
del file[ntfa_path]
283290
dset_ntfa = dset.create_group(ntfa_path)
284291
[dset_ntfa.attrs.create(key, value) for key, value in dset_sim.attrs.items()]
285-
dset_temperatures = dset_ntfa.create_dataset('temperatures', data=temperatures)
286-
dset_C_bar = dset_ntfa.create_dataset('C_bar', data=C_bar)
287-
dset_tau_theta = dset_ntfa.create_dataset('tau_theta', data=tau_theta)
288-
dset_A_bar = dset_ntfa.create_dataset('A_bar', data=A_bar)
289-
dset_tau_xi = dset_ntfa.create_dataset('tau_xi', data=tau_xi)
290-
dset_D_xi = dset_ntfa.create_dataset('D_xi', data=D_xi)
291-
dset_D_theta = dset_ntfa.create_dataset('D_theta', data=D_theta)
292-
dset_A0 = dset_ntfa.create_dataset('A0', data=A0)
293-
dset_A1 = dset_ntfa.create_dataset('A1', data=A1)
294-
dset_C0 = dset_ntfa.create_dataset('C0', data=C0)
295-
dset_C1 = dset_ntfa.create_dataset('C1', data=C1)
292+
dset_ntfa.create_dataset('temperatures', data=temperatures)
293+
dset_ntfa.create_dataset('C_bar', data=C_bar)
294+
dset_ntfa.create_dataset('tau_theta', data=tau_theta)
295+
dset_ntfa.create_dataset('A_bar', data=A_bar)
296+
dset_ntfa.create_dataset('tau_xi', data=tau_xi)
297+
dset_ntfa.create_dataset('D_xi', data=D_xi)
298+
dset_ntfa.create_dataset('D_theta', data=D_theta)
299+
dset_ntfa.create_dataset('A0', data=A0)
300+
dset_ntfa.create_dataset('A1', data=A1)
301+
dset_ntfa.create_dataset('C0', data=C0)
302+
dset_ntfa.create_dataset('C1', data=C1)
296303

297304

298305
def read_tabular_data(file_name, data_path):

utilities.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import contextlib
2-
import re
32
import h5py
43
import matplotlib.pyplot as plt
54
import numpy as np
65
import numpy.linalg as la
76
import scipy.sparse
87
from numba import jit, prange
98
from sympy import symbols, lambdify, Array
10-
from operator import itemgetter
11-
from optimize_alpha import opt4
12-
from interpolate_fluctuation_modes import interpolate_fluctuation_modes
139
from microstructures import *
1410

1511
plt.rcParams.update({

0 commit comments

Comments
 (0)